← All changes
|
jetpack_vendor/automattic/jetpack-connection/src/class-plugin-storage.php
+60
-67
12.0.3
→
16.3-beta
View file →
| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Connection; |
| 9 | 9 | |
| 10 | +use Jetpack_Options; | |
| 10 | 11 | use WP_Error; |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * The class serves a single purpose - to store the data which plugins use the connection, along with some auxiliary information. |
| @@ -16,14 +17,11 @@ | ||
| 16 | 17 | |
| 17 | 18 | const ACTIVE_PLUGINS_OPTION_NAME = 'jetpack_connection_active_plugins'; |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | - * Options where disabled plugins were stored | |
| 21 | - * | |
| 22 | - * @deprecated since 1.39.0. | |
| 23 | - * @var string | |
| 21 | + * Transient name used as flag to indicate that the active connected plugins list needs refreshing. | |
| 24 | 22 | */ |
| 25 | - const PLUGINS_DISABLED_OPTION_NAME = 'jetpack_connection_disabled_plugins'; | |
| 23 | + const ACTIVE_PLUGINS_REFRESH_FLAG = 'jetpack_connection_active_plugins_refresh'; | |
| 26 | 24 | |
| 27 | 25 | /** |
| 28 | 26 | * Whether this class was configured for the first time or not. |
| 29 | 27 | * |
| @@ -31,15 +29,8 @@ | ||
| 31 | 29 | */ |
| 32 | 30 | private static $configured = false; |
| 33 | 31 | |
| 34 | 32 | /** |
| 35 | - * Refresh list of connected plugins upon intialization. | |
| 36 | - * | |
| 37 | - * @var boolean | |
| 38 | - */ | |
| 39 | - private static $refresh_connected_plugins = false; | |
| 40 | - | |
| 41 | - /** | |
| 42 | 33 | * Connected plugins. |
| 43 | 34 | * |
| 44 | 35 | * @var array |
| 45 | 36 | */ |
| @@ -64,13 +55,8 @@ | ||
| 64 | 55 | */ |
| 65 | 56 | public static function upsert( $slug, array $args = array() ) { |
| 66 | 57 | self::$plugins[ $slug ] = $args; |
| 67 | 58 | |
| 68 | - // if plugin is not in the list of active plugins, refresh the list. | |
| 69 | - if ( ! array_key_exists( $slug, (array) get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) ) ) { | |
| 70 | - self::$refresh_connected_plugins = true; | |
| 71 | - } | |
| 72 | - | |
| 73 | 59 | return true; |
| 74 | 60 | } |
| 75 | 61 | |
| 76 | 62 | /** |
| @@ -98,15 +84,11 @@ | ||
| 98 | 84 | * WARNING: the method cannot be called until Plugin_Storage::configure is called, which happens on plugins_loaded |
| 99 | 85 | * Even if you don't use Jetpack Config, it may be introduced later by other plugins, |
| 100 | 86 | * so please make sure not to run the method too early in the code. |
| 101 | 87 | * |
| 102 | - * @since 1.39.0 deprecated the $connected_only argument. | |
| 103 | - * | |
| 104 | - * @param null $deprecated null plugins that were explicitly disconnected. Deprecated, there's no such a thing as disconnecting only specific plugins anymore. | |
| 105 | - * | |
| 106 | 88 | * @return array|WP_Error |
| 107 | 89 | */ |
| 108 | - public static function get_all( $deprecated = null ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 90 | + public static function get_all() { | |
| 109 | 91 | $maybe_error = self::ensure_configured(); |
| 110 | 92 | |
| 111 | 93 | if ( $maybe_error instanceof WP_Error ) { |
| 112 | 94 | return $maybe_error; |
| @@ -149,9 +131,12 @@ | ||
| 149 | 131 | return new WP_Error( 'too_early', __( 'You cannot call this method until Jetpack Config is configured', 'jetpack-connection' ) ); |
| 150 | 132 | } |
| 151 | 133 | |
| 152 | 134 | if ( is_multisite() && get_current_blog_id() !== self::$current_blog_id ) { |
| 153 | - self::$plugins = (array) get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ); | |
| 135 | + if ( self::$current_blog_id ) { | |
| 136 | + // If blog ID got changed, pull the list of active plugins for that blog from the database. | |
| 137 | + self::$plugins = (array) get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ); | |
| 138 | + } | |
| 154 | 139 | self::$current_blog_id = get_current_blog_id(); |
| 155 | 140 | } |
| 156 | 141 | |
| 157 | 142 | return true; |
| @@ -166,8 +151,46 @@ | ||
| 166 | 151 | if ( self::$configured ) { |
| 167 | 152 | return; |
| 168 | 153 | } |
| 169 | 154 | |
| 155 | + self::$configured = true; | |
| 156 | + | |
| 157 | + add_action( 'update_option_active_plugins', array( __CLASS__, 'set_flag_to_refresh_active_connected_plugins' ) ); | |
| 158 | + | |
| 159 | + self::maybe_update_active_connected_plugins(); | |
| 160 | + } | |
| 161 | + | |
| 162 | + /** | |
| 163 | + * Set a flag to indicate that the active connected plugins list needs to be updated. | |
| 164 | + * This will happen when the `active_plugins` option is updated. | |
| 165 | + * | |
| 166 | + * @see configure | |
| 167 | + */ | |
| 168 | + public static function set_flag_to_refresh_active_connected_plugins() { | |
| 169 | + set_transient( self::ACTIVE_PLUGINS_REFRESH_FLAG, time() ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * Determine if we need to update the active connected plugins list. | |
| 174 | + */ | |
| 175 | + public static function maybe_update_active_connected_plugins() { | |
| 176 | + $maybe_error = self::ensure_configured(); | |
| 177 | + | |
| 178 | + if ( $maybe_error instanceof WP_Error ) { | |
| 179 | + return; | |
| 180 | + } | |
| 181 | + // Only attempt to update the option if the corresponding flag is set. | |
| 182 | + if ( ! get_transient( self::ACTIVE_PLUGINS_REFRESH_FLAG ) ) { | |
| 183 | + return; | |
| 184 | + } | |
| 185 | + // Only attempt to update the option on POST requests. | |
| 186 | + // This will prevent the option from being updated multiple times due to concurrent requests. | |
| 187 | + if ( ! ( isset( $_SERVER['REQUEST_METHOD'] ) && 'POST' === $_SERVER['REQUEST_METHOD'] ) ) { | |
| 188 | + return; | |
| 189 | + } | |
| 190 | + | |
| 191 | + delete_transient( self::ACTIVE_PLUGINS_REFRESH_FLAG ); | |
| 192 | + | |
| 170 | 193 | if ( is_multisite() ) { |
| 171 | 194 | self::$current_blog_id = get_current_blog_id(); |
| 172 | 195 | } |
| 173 | 196 | |
| @@ -172,15 +195,16 @@ | ||
| 172 | 195 | } |
| 173 | 196 | |
| 174 | 197 | // If a plugin was activated or deactivated. |
| 175 | 198 | // self::$plugins is populated in Config::ensure_options_connection(). |
| 176 | - $number_of_plugins_differ = count( self::$plugins ) !== count( (array) get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) ); | |
| 199 | + $configured_plugin_keys = array_keys( self::$plugins ); | |
| 200 | + $stored_plugin_keys = array_keys( (array) get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) ); | |
| 201 | + sort( $configured_plugin_keys ); | |
| 202 | + sort( $stored_plugin_keys ); | |
| 177 | 203 | |
| 178 | - if ( $number_of_plugins_differ || true === self::$refresh_connected_plugins ) { | |
| 204 | + if ( $configured_plugin_keys !== $stored_plugin_keys ) { | |
| 179 | 205 | self::update_active_plugins_option(); |
| 180 | 206 | } |
| 181 | - | |
| 182 | - self::$configured = true; | |
| 183 | 207 | } |
| 184 | 208 | |
| 185 | 209 | /** |
| 186 | 210 | * Updates the active plugins option with current list of active plugins. |
| @@ -187,54 +211,22 @@ | ||
| 187 | 211 | * |
| 188 | 212 | * @return void |
| 189 | 213 | */ |
| 190 | 214 | public static function update_active_plugins_option() { |
| 191 | - // Note: Since this options is synced to wpcom, if you change its structure, you have to update the sanitizer at wpcom side. | |
| 215 | + // Note: Since this option is synced to wpcom, if you change its structure, you have to update the sanitizer at wpcom side. | |
| 192 | 216 | update_option( self::ACTIVE_PLUGINS_OPTION_NAME, self::$plugins ); |
| 193 | - | |
| 194 | 217 | if ( ! class_exists( 'Automattic\Jetpack\Sync\Settings' ) || ! \Automattic\Jetpack\Sync\Settings::is_sync_enabled() ) { |
| 195 | 218 | self::update_active_plugins_wpcom_no_sync_fallback(); |
| 219 | + // Remove the checksum for active plugins, so it gets recalculated when sync gets activated. | |
| 220 | + $jetpack_callables_sync_checksum = Jetpack_Options::get_raw_option( 'jetpack_callables_sync_checksum' ); | |
| 221 | + if ( isset( $jetpack_callables_sync_checksum['jetpack_connection_active_plugins'] ) ) { | |
| 222 | + unset( $jetpack_callables_sync_checksum['jetpack_connection_active_plugins'] ); | |
| 223 | + Jetpack_Options::update_raw_option( 'jetpack_callables_sync_checksum', $jetpack_callables_sync_checksum ); | |
| 224 | + } | |
| 196 | 225 | } |
| 197 | 226 | } |
| 198 | 227 | |
| 199 | 228 | /** |
| 200 | - * Add the plugin to the set of disconnected ones. | |
| 201 | - * | |
| 202 | - * @deprecated since 1.39.0. | |
| 203 | - * | |
| 204 | - * @param string $slug Plugin slug. | |
| 205 | - * | |
| 206 | - * @return bool | |
| 207 | - */ | |
| 208 | - public static function disable_plugin( $slug ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 209 | - return true; | |
| 210 | - } | |
| 211 | - | |
| 212 | - /** | |
| 213 | - * Remove the plugin from the set of disconnected ones. | |
| 214 | - * | |
| 215 | - * @deprecated since 1.39.0. | |
| 216 | - * | |
| 217 | - * @param string $slug Plugin slug. | |
| 218 | - * | |
| 219 | - * @return bool | |
| 220 | - */ | |
| 221 | - public static function enable_plugin( $slug ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 222 | - return true; | |
| 223 | - } | |
| 224 | - | |
| 225 | - /** | |
| 226 | - * Get all plugins that were disconnected by user. | |
| 227 | - * | |
| 228 | - * @deprecated since 1.39.0. | |
| 229 | - * | |
| 230 | - * @return array | |
| 231 | - */ | |
| 232 | - public static function get_all_disabled_plugins() { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 233 | - return array(); | |
| 234 | - } | |
| 235 | - | |
| 236 | - /** | |
| 237 | 229 | * Update active plugins option with current list of active plugins on WPCOM. |
| 238 | 230 | * This is a fallback to ensure this option is always up to date on WPCOM in case |
| 239 | 231 | * Sync is not present or disabled. |
| 240 | 232 | * |
| @@ -250,9 +242,10 @@ | ||
| 250 | 242 | |
| 251 | 243 | $body = wp_json_encode( |
| 252 | 244 | array( |
| 253 | 245 | 'active_connected_plugins' => self::$plugins, |
| 254 | - ) | |
| 246 | + ), | |
| 247 | + JSON_UNESCAPED_SLASHES | |
| 255 | 248 | ); |
| 256 | 249 | |
| 257 | 250 | Client::wpcom_json_api_request_as_blog( |
| 258 | 251 | sprintf( '/sites/%d/jetpack-active-connected-plugins', $site_id ), |