| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Storage for plugin connection information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @package automattic/jetpack-connection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace Automattic\Jetpack\Connection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Automattic\Jetpack\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use WP_Error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * The class serves a single purpose - to store the data that plugins use the connection, along with some auxiliary information. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Well, we don't really store all that. The information is provided on runtime, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * so all we need to do is to save the data into the class property and retrieve it from there on demand. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @todo Adapt for multisite installations. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class Plugin_Storage { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	const ACTIVE_PLUGINS_OPTION_NAME = 'connection_plugins_active'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * Whether this class was configured for the first time or not. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	private static $configured = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * Refresh list of connected plugins upon intialization. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * @var boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	private static $refresh_connected_plugins = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * Connected plugins. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	private static $plugins = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	 * Whether the plugins were configured. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * To make sure we don't call the configuration process again and again. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	private static $plugins_configuration_finished = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * Add or update the plugin information in the storage. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param string $slug Plugin slug. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param array  $args Plugin arguments, optional. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	public static function upsert( $slug, array $args = array() ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		self::$plugins[ $slug ] = $args; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 		// if plugin is not in the list of active plugins, refresh the list. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		if ( array_key_exists( $slug, get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			self::$refresh_connected_plugins = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * Retrieve the plugin information by slug. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * WARNING: the method cannot be called until Jetpack Config has been run (`plugins_loaded`, priority 2). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * so please make sure not to run the method too early in the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 * @param string $slug The plugin slug. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @return array|null|WP_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	public static function get_one( $slug ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 		$plugins = self::get_all(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		if ( $plugins instanceof WP_Error ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 			return $plugins; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		return empty( $plugins[ $slug ] ) ? null : $plugins[ $slug ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * Retrieve info for all plugins that use the connection. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * WARNING: the method cannot be called until Jetpack Config has been run (`plugins_loaded`, priority 2). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 * so please make sure not to run the method too early in the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @return array|WP_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	public static function get_all() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$maybe_error = self::ensure_configured(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		if ( $maybe_error instanceof WP_Error ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			return $maybe_error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 		return self::$plugins; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * Remove the plugin connection info from Jetpack. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * WARNING: the method cannot be called until Jetpack Config has been run (`plugins_loaded`, priority 2). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * Even if you don't use Jetpack Config, it may be introduced later by other plugins, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * so please make sure not to run the method too early in the code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	 * @param string $slug The plugin slug. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @return bool|WP_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	public static function delete( $slug ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		$maybe_error = self::ensure_configured(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		if ( $maybe_error instanceof WP_Error ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			return $maybe_error; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 		if ( array_key_exists( $slug, self::$plugins ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 			unset( self::$plugins[ $slug ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * The method makes sure that `Jetpack\Config` has finished, and it's now safe to retrieve the list of plugins. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 	 * @return bool|WP_Error | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 	private static function ensure_configured() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		if ( ! self::$configured ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 			return new WP_Error( 'too_early', __( 'You cannot call this method until Jetpack Config is configured', 'jetpack' ) ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 	 * Called once to configure this class after plugins_loaded. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 	 * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  | 	public static function configure() { | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  | 		if ( self::$configured ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  | 			return; | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  | 		// If a plugin was activated or deactivated. | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  | 		$number_of_plugins_differ = count( self::$plugins ) !== count( get_option( self::ACTIVE_PLUGINS_OPTION_NAME, array() ) ); | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 161 |  |  | 		if ( $number_of_plugins_differ || true === self::$refresh_connected_plugins ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 162 |  |  | 			self::update_active_plugins_option(); | 
            
                                                                        
                            
            
                                    
            
            
                | 163 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 165 |  |  | 		add_filter( 'jetpack_sync_options_whitelist', __NAMESPACE__ . '\Plugin_Storage::whitelist_options' ); | 
            
                                                                        
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 167 |  |  | 		self::$configured = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 169 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	 * Adds Plugins Storage options to sync whitelist | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 	 * @param array $options the whitelisted options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	public static function whitelist_options( $options ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 		$options[] = self::ACTIVE_PLUGINS_OPTION_NAME; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		return $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	 * Updates the active plugins option with current list of active plugins. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	 * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	public static function update_active_plugins_option() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 		update_option( self::ACTIVE_PLUGINS_OPTION_NAME, array_keys( self::$plugins ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 191 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |  | 
            
                        
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.