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