| @@ -1,12 +1,20 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * @package Polylang | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | /** |
| 4 | - * A generic activation / de-activation class compatble with multisite | |
| 7 | + * A generic activation / de-activation class compatible with multisite | |
| 5 | 8 | * |
| 6 | 9 | * @since 1.7 |
| 7 | 10 | */ |
| 8 | 11 | class PLL_Install_Base { |
| 12 | + /** | |
| 13 | + * The plugin basename. | |
| 14 | + * | |
| 15 | + * @var string | |
| 16 | + */ | |
| 9 | 17 | protected $plugin_basename; |
| 10 | 18 | |
| 11 | 19 | /** |
| 12 | 20 | * Constructor |
| @@ -21,15 +29,10 @@ | ||
| 21 | 29 | // Manages plugin activation and deactivation |
| 22 | 30 | register_activation_hook( $plugin_basename, array( $this, 'activate' ) ); |
| 23 | 31 | register_deactivation_hook( $plugin_basename, array( $this, 'deactivate' ) ); |
| 24 | 32 | |
| 25 | - // Blog creation on multisite. | |
| 26 | - if ( version_compare( $GLOBALS['wp_version'], '5.1', '<' ) ) { | |
| 27 | - // FIXME: Backward compatibility with WP < 5.1. | |
| 28 | - add_action( 'wpmu_new_blog', array( $this, 'wpmu_new_blog' ), 5 ); // Before WP attempts to send mails which can break on some PHP versions | |
| 29 | - } else { | |
| 30 | - add_action( 'wp_insert_site', array( $this, 'new_site' ) ); | |
| 31 | - } | |
| 33 | + // Site creation on multisite. | |
| 34 | + add_action( 'wp_initialize_site', array( $this, 'new_site' ), 50 ); // After WP (prio 10). | |
| 32 | 35 | } |
| 33 | 36 | |
| 34 | 37 | /** |
| 35 | 38 | * Allows to detect plugin deactivation |
| @@ -35,9 +38,9 @@ | ||
| 35 | 38 | * Allows to detect plugin deactivation |
| 36 | 39 | * |
| 37 | 40 | * @since 1.7 |
| 38 | 41 | * |
| 39 | - * @return bool true if the plugin is currently beeing deactivated | |
| 42 | + * @return bool true if the plugin is currently being deactivated | |
| 40 | 43 | */ |
| 41 | 44 | public function is_deactivation() { |
| 42 | 45 | return isset( $_GET['action'], $_GET['plugin'] ) && 'deactivate' === $_GET['action'] && $this->plugin_basename === $_GET['plugin']; // phpcs:ignore WordPress.Security.NonceVerification |
| 43 | 46 | } |
| @@ -42,14 +45,15 @@ | ||
| 42 | 45 | return isset( $_GET['action'], $_GET['plugin'] ) && 'deactivate' === $_GET['action'] && $this->plugin_basename === $_GET['plugin']; // phpcs:ignore WordPress.Security.NonceVerification |
| 43 | 46 | } |
| 44 | 47 | |
| 45 | 48 | /** |
| 46 | - * Activation or deactivation for all blogs | |
| 49 | + * Activation or deactivation for all blogs. | |
| 47 | 50 | * |
| 48 | 51 | * @since 1.2 |
| 49 | 52 | * |
| 50 | - * @param string $what Either 'activate' or 'deactivate' | |
| 51 | - * @param bool $networkwide | |
| 53 | + * @param string $what Either 'activate' or 'deactivate'. | |
| 54 | + * @param bool $networkwide Whether the plugin is (de)activated for all sites in the network or just the current site. | |
| 55 | + * @return void | |
| 52 | 56 | */ |
| 53 | 57 | protected function do_for_all_blogs( $what, $networkwide ) { |
| 54 | 58 | // Network |
| 55 | 59 | if ( is_multisite() && $networkwide ) { |
| @@ -68,13 +72,14 @@ | ||
| 68 | 72 | } |
| 69 | 73 | } |
| 70 | 74 | |
| 71 | 75 | /** |
| 72 | - * Plugin activation for multisite | |
| 76 | + * Plugin activation for multisite. | |
| 73 | 77 | * |
| 74 | 78 | * @since 1.7 |
| 75 | 79 | * |
| 76 | - * @param bool $networkwide | |
| 80 | + * @param bool $networkwide Whether the plugin is activated for all sites in the network or just the current site. | |
| 81 | + * @return void | |
| 77 | 82 | */ |
| 78 | 83 | public function activate( $networkwide ) { |
| 79 | 84 | $this->do_for_all_blogs( 'activate', $networkwide ); |
| 80 | 85 | } |
| @@ -82,19 +87,22 @@ | ||
| 82 | 87 | /** |
| 83 | 88 | * Plugin activation |
| 84 | 89 | * |
| 85 | 90 | * @since 0.5 |
| 91 | + * | |
| 92 | + * @return void | |
| 86 | 93 | */ |
| 87 | 94 | protected function _activate() { |
| 88 | - // Can be overriden in child class | |
| 95 | + // Can be overridden in child class | |
| 89 | 96 | } |
| 90 | 97 | |
| 91 | 98 | /** |
| 92 | - * Plugin deactivation for multisite | |
| 99 | + * Plugin deactivation for multisite. | |
| 93 | 100 | * |
| 94 | 101 | * @since 0.1 |
| 95 | 102 | * |
| 96 | - * @param bool $networkwide | |
| 103 | + * @param bool $networkwide Whether the plugin is deactivated for all sites in the network or just the current site. | |
| 104 | + * @return void | |
| 97 | 105 | */ |
| 98 | 106 | public function deactivate( $networkwide ) { |
| 99 | 107 | $this->do_for_all_blogs( 'deactivate', $networkwide ); |
| 100 | 108 | } |
| @@ -102,11 +110,13 @@ | ||
| 102 | 110 | /** |
| 103 | 111 | * Plugin deactivation |
| 104 | 112 | * |
| 105 | 113 | * @since 0.5 |
| 114 | + * | |
| 115 | + * @return void | |
| 106 | 116 | */ |
| 107 | 117 | protected function _deactivate() { |
| 108 | - // Can be overriden in child class | |
| 118 | + // Can be overridden in child class | |
| 109 | 119 | } |
| 110 | 120 | |
| 111 | 121 | /** |
| 112 | 122 | * Site creation on multisite ( to set default options ) |
| @@ -113,25 +123,12 @@ | ||
| 113 | 123 | * |
| 114 | 124 | * @since 2.6.8 |
| 115 | 125 | * |
| 116 | 126 | * @param WP_Site $new_site New site object. |
| 127 | + * @return void | |
| 117 | 128 | */ |
| 118 | 129 | public function new_site( $new_site ) { |
| 119 | 130 | switch_to_blog( $new_site->id ); |
| 120 | - $this->_activate(); | |
| 121 | - restore_current_blog(); | |
| 122 | - } | |
| 123 | - | |
| 124 | - /** | |
| 125 | - * Blog creation on multisite ( to set default options ) | |
| 126 | - * Backward compatibility with WP < 5.1 | |
| 127 | - * | |
| 128 | - * @since 0.9.4 | |
| 129 | - * | |
| 130 | - * @param int $blog_id | |
| 131 | - */ | |
| 132 | - public function wpmu_new_blog( $blog_id ) { | |
| 133 | - switch_to_blog( $blog_id ); | |
| 134 | 131 | $this->_activate(); |
| 135 | 132 | restore_current_blog(); |
| 136 | 133 | } |
| 137 | 134 | } |