Sin descripción

Install.php 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. /********************************************
  3. * Copyright (c) 2020, Code Atlantic LLC.
  4. *******************************************/
  5. // Exit if accessed directly.
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit;
  8. }
  9. /**
  10. * Class PUM_Install
  11. *
  12. * @since 1.9.0
  13. */
  14. class PUM_Install {
  15. /**
  16. * @param $network_wide
  17. */
  18. public static function activate_plugin( $network_wide ) {
  19. self::do_multisite( $network_wide, array( __CLASS__, 'activate_site' ) );
  20. }
  21. /**
  22. * @param $network_wide
  23. */
  24. public static function deactivate_plugin( $network_wide ) {
  25. self::do_multisite( $network_wide, [ __CLASS__, 'deactivate_site' ] );
  26. }
  27. /**
  28. *
  29. */
  30. public static function uninstall_plugin() {
  31. self::do_multisite( true, array( __CLASS__, 'uninstall_site' ) );
  32. }
  33. /**
  34. * @param $network_wide
  35. * @param $method
  36. * @param array $args
  37. */
  38. private static function do_multisite( $network_wide, $method, $args = array() ) {
  39. global $wpdb;
  40. if ( is_multisite() && $network_wide ) {
  41. $activated = get_site_option( 'pum_activated', array() );
  42. $blog_ids = $wpdb->get_col( "SELECT blog_id FROM {$wpdb->blogs}" );
  43. // Try to reduce the chances of a timeout with a large number of sites.
  44. if ( count( $blog_ids ) > 2 ) {
  45. ignore_user_abort( true );
  46. if ( ! pum_is_func_disabled( 'set_time_limit' ) ) {
  47. @set_time_limit( 0 );
  48. }
  49. }
  50. foreach ( $blog_ids as $blog_id ) {
  51. switch_to_blog( $blog_id );
  52. call_user_func_array( $method, array( $args ) );
  53. $activated[] = $blog_id;
  54. restore_current_blog();
  55. }
  56. update_site_option( 'pum_activated', $activated );
  57. } else {
  58. call_user_func_array( $method, array( $args ) );
  59. }
  60. }
  61. /**
  62. * Installs the plugin
  63. */
  64. public static function activate_site() {
  65. // Add default values where needed.
  66. $options = array_merge(
  67. get_option( 'popmake_settings', array() ),
  68. array(
  69. 'disable_popup_category_tag' => 1,
  70. )
  71. );
  72. // Setup some default options.
  73. add_option( 'popmake_settings', $options );
  74. add_option( 'pum_version', Popup_Maker::$VER );
  75. pum();
  76. // Setup the Popup & Theme Custom Post Type.
  77. // PUM_Types::register_post_types();.
  78. // Setup the Popup Taxonomies.
  79. // PUM_Types::register_taxonomies( true );.
  80. // Updates stored values for versioning.
  81. // PUM_Utils_Upgrades::update_plugin_version();.
  82. // We used transients before, but since the check for this option runs every admin page load it means 2 queries after its cleared.
  83. // To prevent that we flipped it, now we delete the following option, and check for it.
  84. // If its missing then we know its a fresh install.
  85. delete_option( '_pum_installed' );
  86. // Prepare to redirect to welcome screen, if not seen before.
  87. if ( false === get_option( 'pum_seen_welcome' ) ) {
  88. set_transient( 'pum_activation_redirect', 1, 60 );
  89. }
  90. pum_get_default_theme_id();
  91. pum_install_built_in_themes();
  92. pum_install_example_popups();
  93. // Reset JS/CSS assets for regeneration.
  94. pum_reset_assets();
  95. }
  96. public static function get_option( $key, $default = false ) {
  97. if ( function_exists( 'pum_get_option' ) ) {
  98. return pum_get_option( $key, $default );
  99. }
  100. return PUM_Utils_Options::get( $key, $default );
  101. }
  102. /**
  103. * Run when Popup Maker is deactivated. Completely deletes all data if complete_uninstall is set to true.
  104. *
  105. * @since 1.4
  106. */
  107. public static function deactivate_site() {
  108. /**
  109. * Process complete uninstall
  110. */
  111. if ( self::get_option( 'complete_uninstall' ) ) {
  112. global $wpdb;
  113. // Delete all popups and associated meta.
  114. $wpdb->query( "DELETE a,b,c FROM $wpdb->posts a LEFT JOIN $wpdb->term_relationships b ON (a.ID = b.object_id) LEFT JOIN $wpdb->postmeta c ON (a.ID = c.post_id) WHERE a.post_type IN ('popup', 'popup_theme')" );
  115. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key LIKE 'popup_%'" );
  116. /** Delete All the Taxonomies */
  117. foreach ( array( 'popup_category', 'popup_tag' ) as $taxonomy ) {
  118. // Prepare & excecute SQL, Delete Terms.
  119. $wpdb->get_results( $wpdb->prepare( "DELETE t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ('%s')", $taxonomy ) );
  120. // Delete Taxonomy.
  121. $wpdb->delete( $wpdb->term_taxonomy, array( 'taxonomy' => $taxonomy ), array( '%s' ) );
  122. }
  123. $wpdb->query( "DELETE FROM $wpdb->options WHERE option_name LIKE 'popmake%' OR option_name LIKE '_pum_%' OR option_name LIKE 'pum_%' OR option_name LIKE 'popup_analytics_%'" );
  124. // Delete all Popup Maker related user meta.
  125. $wpdb->query( "DELETE FROM $wpdb->usermeta WHERE meta_key LIKE '_pum_%' OR meta_key lIKE 'pum_%'" );
  126. // Delete subscribers table.
  127. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}pum_subscribers" );
  128. // Delete error log.
  129. PUM_Utils_Logging::instance()->clear_log();
  130. // Reset JS/CSS assets for regeneration.
  131. pum_reset_assets();
  132. // # TODO Delete AssetCache files and folder.
  133. do_action( 'pum_uninstall' );
  134. }
  135. }
  136. /**
  137. * @since 1.9.0
  138. */
  139. public static function uninstall_site() {
  140. }
  141. /**
  142. * Returns an activation failure flag if one exists.
  143. *
  144. * @return string|null
  145. */
  146. public static function get_activation_flag() {
  147. global $wp_version;
  148. $flag = null;
  149. if ( version_compare( PHP_VERSION, Popup_Maker::$MIN_PHP_VER, '<' ) ) {
  150. $flag = 'PHP';
  151. } elseif ( version_compare( $wp_version, Popup_Maker::$MIN_WP_VER, '<' ) ) {
  152. $flag = 'WordPress';
  153. }
  154. return $flag;
  155. }
  156. /**
  157. * Checks if Popup Maker can activate safely.
  158. *
  159. * @return bool
  160. */
  161. public static function meets_activation_requirements() {
  162. return self::get_activation_flag() === null;
  163. }
  164. /**
  165. * Gets activation failure notice message.
  166. *
  167. * @return string
  168. */
  169. public static function get_activation_failure_notice() {
  170. $flag = self::get_activation_flag();
  171. $version = 'PHP' == $flag ? Popup_Maker::$MIN_PHP_VER : Popup_Maker::$MIN_WP_VER;
  172. return sprintf( __( 'The %4$s %1$s %5$s plugin requires %2$s version %3$s or greater.', 'popup-maker' ), Popup_Maker::$NAME, $flag, $version, "<strong>", "</strong>" );
  173. }
  174. /**
  175. *
  176. */
  177. public static function activation_failure_admin_notice() {
  178. ?>
  179. <div class="notice notice-error is-dismissible">
  180. <p><?php esc_html_e( self::get_activation_failure_notice() ); ?></p>
  181. </div>
  182. <?php
  183. }
  184. /**
  185. * Plugin Activation hook function to check for Minimum PHP and WordPress versions
  186. *
  187. * Cannot use static:: in case php 5.2 is used.
  188. */
  189. public static function activation_check() {
  190. if ( self::meets_activation_requirements() ) {
  191. return;
  192. }
  193. // Deactivate automatically due to insufficient PHP or WP Version.
  194. deactivate_plugins( basename( __FILE__ ) );
  195. $notice = self::get_activation_failure_notice();
  196. wp_die( "<p>$notice</p>", __( 'Plugin Activation Error', 'popup-maker' ), array(
  197. 'response' => 200,
  198. 'back_link' => true,
  199. ) );
  200. }
  201. }