| @@ -1,16 +1,27 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Validates and loads core connectors, integrated connectors, and | |
| 4 | + * connectors registered using the "wp_stream_connectors" hook. | |
| 5 | + * | |
| 6 | + * @package WP_Stream | |
| 7 | + */ | |
| 8 | + | |
| 2 | 9 | namespace WP_Stream; |
| 3 | 10 | |
| 11 | +/** | |
| 12 | + * Class - Connectors | |
| 13 | + */ | |
| 4 | 14 | class Connectors { |
| 5 | 15 | /** |
| 6 | - * Hold Plugin class | |
| 16 | + * Holds instance of plugin object | |
| 17 | + * | |
| 7 | 18 | * @var Plugin |
| 8 | 19 | */ |
| 9 | 20 | public $plugin; |
| 10 | 21 | |
| 11 | 22 | /** |
| 12 | - * Connectors registered | |
| 23 | + * Registered connectors. | |
| 13 | 24 | * |
| 14 | 25 | * @var array |
| 15 | 26 | */ |
| 16 | 27 | public $connectors = array(); |
| @@ -23,8 +34,9 @@ | ||
| 23 | 34 | public $contexts = array(); |
| 24 | 35 | |
| 25 | 36 | /** |
| 26 | 37 | * Action taxonomy terms |
| 38 | + * | |
| 27 | 39 | * Holds slug to localized label association |
| 28 | 40 | * |
| 29 | 41 | * @var array |
| 30 | 42 | */ |
| @@ -43,9 +55,9 @@ | ||
| 43 | 55 | |
| 44 | 56 | /** |
| 45 | 57 | * Class constructor. |
| 46 | 58 | * |
| 47 | - * @param Plugin $plugin The main Plugin class. | |
| 59 | + * @param Plugin $plugin Instance of plugin object. | |
| 48 | 60 | */ |
| 49 | 61 | public function __construct( $plugin ) { |
| 50 | 62 | $this->plugin = $plugin; |
| 51 | 63 | $this->load_connectors(); |
| @@ -55,9 +67,11 @@ | ||
| 55 | 67 | * Load built-in connectors |
| 56 | 68 | */ |
| 57 | 69 | public function load_connectors() { |
| 58 | 70 | $connectors = array( |
| 59 | - // Core | |
| 71 | + /** | |
| 72 | + * Core Connectors | |
| 73 | + */ | |
| 60 | 74 | 'blogs', |
| 61 | 75 | 'comments', |
| 62 | 76 | 'editor', |
| 63 | 77 | 'installer', |
| @@ -68,9 +82,11 @@ | ||
| 68 | 82 | 'taxonomies', |
| 69 | 83 | 'users', |
| 70 | 84 | 'widgets', |
| 71 | 85 | |
| 72 | - // Extras | |
| 86 | + /** | |
| 87 | + * Integrated Connectors | |
| 88 | + */ | |
| 73 | 89 | 'acf', |
| 74 | 90 | 'bbpress', |
| 75 | 91 | 'buddypress', |
| 76 | 92 | 'edd', |
| @@ -75,8 +91,9 @@ | ||
| 75 | 91 | 'buddypress', |
| 76 | 92 | 'edd', |
| 77 | 93 | 'gravityforms', |
| 78 | 94 | 'jetpack', |
| 95 | + 'mercator', | |
| 79 | 96 | 'user-switching', |
| 80 | 97 | 'woocommerce', |
| 81 | 98 | 'wordpress-seo', |
| 82 | 99 | ); |
| @@ -82,17 +99,38 @@ | ||
| 82 | 99 | ); |
| 83 | 100 | |
| 84 | 101 | $classes = array(); |
| 85 | 102 | foreach ( $connectors as $connector ) { |
| 103 | + // Load connector class file. | |
| 86 | 104 | include_once $this->plugin->locations['dir'] . '/connectors/class-connector-' . $connector . '.php'; |
| 105 | + | |
| 106 | + // Set fully qualified class name. | |
| 87 | 107 | $class_name = sprintf( '\WP_Stream\Connector_%s', str_replace( '-', '_', $connector ) ); |
| 108 | + | |
| 109 | + // Bail if no class loaded. | |
| 88 | 110 | if ( ! class_exists( $class_name ) ) { |
| 89 | 111 | continue; |
| 90 | 112 | } |
| 113 | + | |
| 114 | + // Initialize connector. | |
| 91 | 115 | $class = new $class_name( $this->plugin->log ); |
| 92 | - if ( ! method_exists( $class, 'is_dependency_satisfied' ) ) { | |
| 116 | + | |
| 117 | + // Check if the connector class extends WP_Stream\Connector. | |
| 118 | + if ( ! is_subclass_of( $class, 'WP_Stream\Connector' ) ) { | |
| 93 | 119 | continue; |
| 94 | 120 | } |
| 121 | + | |
| 122 | + // Check if the connector events are allowed to be registered in the WP Admin. | |
| 123 | + if ( is_admin() && ! $class->register_admin ) { | |
| 124 | + continue; | |
| 125 | + } | |
| 126 | + | |
| 127 | + // Check if the connector events are allowed to be registered in the WP Frontend. | |
| 128 | + if ( ! is_admin() && ! $class->register_frontend ) { | |
| 129 | + continue; | |
| 130 | + } | |
| 131 | + | |
| 132 | + // Run any final validations the connector may have before used. | |
| 95 | 133 | if ( $class->is_dependency_satisfied() ) { |
| 96 | 134 | $classes[ $class->name ] = $class; |
| 97 | 135 | } |
| 98 | 136 | } |
| @@ -114,25 +152,31 @@ | ||
| 114 | 152 | } |
| 115 | 153 | $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); |
| 116 | 154 | } |
| 117 | 155 | |
| 118 | - // Get excluded connectors | |
| 156 | + // Get excluded connectors. | |
| 119 | 157 | $excluded_connectors = array(); |
| 120 | 158 | |
| 121 | 159 | foreach ( $this->connectors as $connector ) { |
| 160 | + | |
| 161 | + // Register error for invalid any connector class. | |
| 122 | 162 | if ( ! method_exists( $connector, 'get_label' ) ) { |
| 163 | + /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ | |
| 123 | 164 | $this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the get_label method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 124 | 165 | continue; |
| 125 | 166 | } |
| 126 | 167 | if ( ! method_exists( $connector, 'register' ) ) { |
| 168 | + /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ | |
| 127 | 169 | $this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the register method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 128 | 170 | continue; |
| 129 | 171 | } |
| 130 | 172 | if ( ! method_exists( $connector, 'get_context_labels' ) ) { |
| 173 | + /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ | |
| 131 | 174 | $this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the get_context_labels method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 132 | 175 | continue; |
| 133 | 176 | } |
| 134 | 177 | if ( ! method_exists( $connector, 'get_action_labels' ) ) { |
| 178 | + /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ | |
| 135 | 179 | $this->plugin->admin->notice( sprintf( __( '%s class wasn\'t loaded because it doesn\'t implement the get_action_labels method.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 136 | 180 | continue; |
| 137 | 181 | } |
| 138 | 182 | |
| @@ -137,13 +181,14 @@ | ||
| 137 | 181 | } |
| 138 | 182 | |
| 139 | 183 | // Check if the connectors extends the Connector class, if not skip it. |
| 140 | 184 | if ( ! is_subclass_of( $connector, '\WP_Stream\Connector' ) ) { |
| 185 | + /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ | |
| 141 | 186 | $this->plugin->admin->notice( sprintf( __( '%1$s class wasn\'t loaded because it doesn\'t extends the %2$s class.', 'stream' ), $connector->name, 'Connector' ), true ); |
| 142 | 187 | continue; |
| 143 | 188 | } |
| 144 | 189 | |
| 145 | - // Store connector label | |
| 190 | + // Store connector label. | |
| 146 | 191 | if ( ! in_array( $connector->name, $this->term_labels['stream_connector'], true ) ) { |
| 147 | 192 | $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); |
| 148 | 193 | } |
| 149 | 194 | |
| @@ -164,12 +209,12 @@ | ||
| 164 | 209 | } |
| 165 | 210 | |
| 166 | 211 | $connector->register(); |
| 167 | 212 | |
| 168 | - // Link context labels to their connector | |
| 213 | + // Link context labels to their connector. | |
| 169 | 214 | $this->contexts[ $connector->name ] = $connector->get_context_labels(); |
| 170 | 215 | |
| 171 | - // Add new terms to our label lookup array | |
| 216 | + // Add new terms to our label lookup array. | |
| 172 | 217 | $this->term_labels['stream_action'] = array_merge( |
| 173 | 218 | $this->term_labels['stream_action'], |
| 174 | 219 | $connector->get_action_labels() |
| 175 | 220 | ); |
| @@ -187,6 +232,46 @@ | ||
| 187 | 232 | * @param array $labels All register connectors labels array |
| 188 | 233 | * @param Connectors $connectors The Connectors object |
| 189 | 234 | */ |
| 190 | 235 | do_action( 'wp_stream_after_connectors_registration', $labels, $this ); |
| 236 | + } | |
| 237 | + | |
| 238 | + /** | |
| 239 | + * Unregisters the context hooks for all connectors. | |
| 240 | + */ | |
| 241 | + public function unload_connectors() { | |
| 242 | + foreach ( $this->connectors as $connector ) { | |
| 243 | + $connector->unregister(); | |
| 244 | + } | |
| 245 | + } | |
| 246 | + | |
| 247 | + /** | |
| 248 | + * Reregisters the context hooks for all connectors. | |
| 249 | + */ | |
| 250 | + public function reload_connectors() { | |
| 251 | + foreach ( $this->connectors as $connector ) { | |
| 252 | + $connector->register(); | |
| 253 | + } | |
| 254 | + } | |
| 255 | + | |
| 256 | + /** | |
| 257 | + * Unregisters the context hooks for a connectors. | |
| 258 | + * | |
| 259 | + * @param string $name Name of the connector. | |
| 260 | + */ | |
| 261 | + public function unload_connector( $name ) { | |
| 262 | + if ( ! empty( $this->connectors[ $name ] ) ) { | |
| 263 | + $this->connectors[ $name ]->unregister(); | |
| 264 | + } | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 268 | + * Reregisters the context hooks for a connector. | |
| 269 | + * | |
| 270 | + * @param string $name Name of the connector. | |
| 271 | + */ | |
| 272 | + public function reload_connector( $name ) { | |
| 273 | + if ( ! empty( $this->connectors[ $name ] ) ) { | |
| 274 | + $this->connectors[ $name ]->register(); | |
| 275 | + } | |
| 191 | 276 | } |
| 192 | 277 | } |