| @@ -92,17 +92,13 @@ | ||
| 92 | 92 | 'edd', |
| 93 | 93 | 'gravityforms', |
| 94 | 94 | 'jetpack', |
| 95 | 95 | 'mercator', |
| 96 | - 'two-factor', | |
| 97 | 96 | 'user-switching', |
| 98 | 97 | 'woocommerce', |
| 99 | 98 | 'wordpress-seo', |
| 100 | 99 | ); |
| 101 | 100 | |
| 102 | - // Get excluded connectors. | |
| 103 | - $excluded_connectors = array(); | |
| 104 | - | |
| 105 | 101 | $classes = array(); |
| 106 | 102 | foreach ( $connectors as $connector ) { |
| 107 | 103 | // Load connector class file. |
| 108 | 104 | include_once $this->plugin->locations['dir'] . '/connectors/class-connector-' . $connector . '.php'; |
| @@ -115,40 +111,54 @@ | ||
| 115 | 111 | continue; |
| 116 | 112 | } |
| 117 | 113 | |
| 118 | 114 | // Initialize connector. |
| 119 | - $class = new $class_name(); | |
| 120 | - $classes[ $class->name ] = $class; | |
| 121 | - } | |
| 115 | + $class = new $class_name( $this->plugin->log ); | |
| 122 | 116 | |
| 123 | - /** | |
| 124 | - * Allows for adding additional connectors via classes that extend Connector. | |
| 125 | - * | |
| 126 | - * @param array $classes An array of Connector objects. | |
| 127 | - */ | |
| 128 | - $connector_classes = apply_filters( 'wp_stream_connectors', $classes ); | |
| 129 | - | |
| 130 | - foreach ( $connector_classes as $connector ) { | |
| 131 | 117 | // Check if the connector class extends WP_Stream\Connector. |
| 132 | - if ( ! is_subclass_of( $connector, 'WP_Stream\Connector' ) ) { | |
| 118 | + if ( ! is_subclass_of( $class, 'WP_Stream\Connector' ) ) { | |
| 133 | 119 | continue; |
| 134 | 120 | } |
| 135 | 121 | |
| 136 | 122 | // Check if the connector events are allowed to be registered in the WP Admin. |
| 137 | - if ( is_admin() && ! $connector->register_admin ) { | |
| 123 | + if ( is_admin() && ! $class->register_admin ) { | |
| 138 | 124 | continue; |
| 139 | 125 | } |
| 140 | 126 | |
| 141 | 127 | // Check if the connector events are allowed to be registered in the WP Frontend. |
| 142 | - if ( ! is_admin() && ! $connector->register_frontend ) { | |
| 128 | + if ( ! is_admin() && ! $class->register_frontend ) { | |
| 143 | 129 | continue; |
| 144 | 130 | } |
| 145 | 131 | |
| 146 | 132 | // Run any final validations the connector may have before used. |
| 147 | - if ( ! $connector->is_dependency_satisfied() ) { | |
| 133 | + if ( $class->is_dependency_satisfied() ) { | |
| 134 | + $classes[ $class->name ] = $class; | |
| 135 | + } | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Allows for adding additional connectors via classes that extend Connector. | |
| 140 | + * | |
| 141 | + * @param array $classes An array of Connector objects. | |
| 142 | + */ | |
| 143 | + $this->connectors = apply_filters( 'wp_stream_connectors', $classes ); | |
| 144 | + | |
| 145 | + if ( empty( $this->connectors ) ) { | |
| 146 | + return; | |
| 147 | + } | |
| 148 | + | |
| 149 | + foreach ( $this->connectors as $connector ) { | |
| 150 | + if ( ! method_exists( $connector, 'get_label' ) ) { | |
| 148 | 151 | continue; |
| 149 | 152 | } |
| 153 | + $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); | |
| 154 | + } | |
| 150 | 155 | |
| 156 | + // Get excluded connectors. | |
| 157 | + $excluded_connectors = array(); | |
| 158 | + | |
| 159 | + foreach ( $this->connectors as $connector ) { | |
| 160 | + | |
| 151 | 161 | // Register error for invalid any connector class. |
| 152 | 162 | if ( ! method_exists( $connector, 'get_label' ) ) { |
| 153 | 163 | /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ |
| 154 | 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 ); |
| @@ -169,8 +179,23 @@ | ||
| 169 | 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 ); |
| 170 | 180 | continue; |
| 171 | 181 | } |
| 172 | 182 | |
| 183 | + // Check if the connectors extends the Connector class, if not skip it. | |
| 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") */ | |
| 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 ); | |
| 187 | + continue; | |
| 188 | + } | |
| 189 | + | |
| 190 | + // Store connector label. | |
| 191 | + if ( ! in_array( $connector->name, $this->term_labels['stream_connector'], true ) ) { | |
| 192 | + $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); | |
| 193 | + } | |
| 194 | + | |
| 195 | + $connector_name = $connector->name; | |
| 196 | + $is_excluded = in_array( $connector_name, $excluded_connectors, true ); | |
| 197 | + | |
| 173 | 198 | /** |
| 174 | 199 | * Allows excluded connectors to be overridden and registered. |
| 175 | 200 | * |
| 176 | 201 | * @param bool $is_excluded True if excluded, otherwise false. |
| @@ -176,31 +201,19 @@ | ||
| 176 | 201 | * @param bool $is_excluded True if excluded, otherwise false. |
| 177 | 202 | * @param string $connector The current connector's slug. |
| 178 | 203 | * @param array $excluded_connectors An array of all excluded connector slugs. |
| 179 | 204 | */ |
| 180 | - $is_excluded_connector = apply_filters( | |
| 181 | - 'wp_stream_check_connector_is_excluded', | |
| 182 | - in_array( $connector->name, $excluded_connectors, true ), | |
| 183 | - $connector->name, | |
| 184 | - $excluded_connectors | |
| 185 | - ); | |
| 205 | + $is_excluded_connector = apply_filters( 'wp_stream_check_connector_is_excluded', $is_excluded, $connector_name, $excluded_connectors ); | |
| 186 | 206 | |
| 187 | 207 | if ( $is_excluded_connector ) { |
| 188 | 208 | continue; |
| 189 | 209 | } |
| 190 | 210 | |
| 191 | - // Add connector to the registry. | |
| 192 | - $this->connectors[ $connector->name ] = $connector; | |
| 193 | - | |
| 194 | - // Register the connector. | |
| 195 | 211 | $connector->register(); |
| 196 | 212 | |
| 197 | 213 | // Link context labels to their connector. |
| 198 | 214 | $this->contexts[ $connector->name ] = $connector->get_context_labels(); |
| 199 | 215 | |
| 200 | - // Store connector label. | |
| 201 | - $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); | |
| 202 | - | |
| 203 | 216 | // Add new terms to our label lookup array. |
| 204 | 217 | $this->term_labels['stream_action'] = array_merge( |
| 205 | 218 | $this->term_labels['stream_action'], |
| 206 | 219 | $connector->get_action_labels() |
| @@ -215,178 +228,12 @@ | ||
| 215 | 228 | |
| 216 | 229 | /** |
| 217 | 230 | * Fires after all connectors have been registered. |
| 218 | 231 | * |
| 219 | - * @param array $labels All register connectors labels array | |
| 220 | - * @param Connectors $connector_classes The Connectors object | |
| 232 | + * @param array $labels All register connectors labels array | |
| 233 | + * @param Connectors $connectors The Connectors object | |
| 221 | 234 | */ |
| 222 | 235 | do_action( 'wp_stream_after_connectors_registration', $labels, $this ); |
| 223 | - } | |
| 224 | - | |
| 225 | - /** | |
| 226 | - * Return the slugs of all registered connectors. | |
| 227 | - * | |
| 228 | - * @return string[] | |
| 229 | - */ | |
| 230 | - public function get_slugs() { | |
| 231 | - return array_keys( (array) $this->connectors ); | |
| 232 | - } | |
| 233 | - | |
| 234 | - /** | |
| 235 | - * Return a normalized list of all registered connectors and their | |
| 236 | - * context/action labels. | |
| 237 | - * | |
| 238 | - * @return array<int, array{slug:string,label:string,contexts:array<string,string>,actions:array<string,string>}> | |
| 239 | - */ | |
| 240 | - public function get_all() { | |
| 241 | - $out = array(); | |
| 242 | - | |
| 243 | - foreach ( (array) $this->connectors as $slug => $connector ) { | |
| 244 | - $out[] = array( | |
| 245 | - 'slug' => (string) $slug, | |
| 246 | - 'label' => method_exists( $connector, 'get_label' ) ? (string) $connector->get_label() : (string) $slug, | |
| 247 | - 'contexts' => method_exists( $connector, 'get_context_labels' ) ? (array) $connector->get_context_labels() : array(), | |
| 248 | - 'actions' => method_exists( $connector, 'get_action_labels' ) ? (array) $connector->get_action_labels() : array(), | |
| 249 | - ); | |
| 250 | - } | |
| 251 | - | |
| 252 | - return $out; | |
| 253 | - } | |
| 254 | - | |
| 255 | - /** | |
| 256 | - * Return metadata for every Stream connector, including ones that | |
| 257 | - * load_connectors() skipped because their `register_frontend` is false | |
| 258 | - * and the current request isn't wp-admin (REST is `! is_admin()`, so | |
| 259 | - * connectors like "settings", "editor", "menus" are otherwise invisible | |
| 260 | - * to abilities). Walks the connector class list once and asks each | |
| 261 | - * instance for its identity, never calling register() — so no hooks | |
| 262 | - * fire and the live $this->connectors registry stays exactly as | |
| 263 | - * load_connectors() built it. | |
| 264 | - * | |
| 265 | - * Intended for the abilities layer (stream/get-connectors, | |
| 266 | - * stream/create-exclusion-rule validation) where the answer should | |
| 267 | - * reflect "what connectors exist on this site" rather than "what | |
| 268 | - * connectors loaded their hooks for this specific request". | |
| 269 | - * | |
| 270 | - * @return array<int, array{slug:string,label:string,contexts:array<string,string>,actions:array<string,string>}> | |
| 271 | - */ | |
| 272 | - public function get_all_including_admin_only() { | |
| 273 | - $out = array(); | |
| 274 | - | |
| 275 | - foreach ( $this->build_full_connector_classes() as $connector ) { | |
| 276 | - $out[] = array( | |
| 277 | - 'slug' => (string) $connector->name, | |
| 278 | - 'label' => method_exists( $connector, 'get_label' ) ? (string) $connector->get_label() : (string) $connector->name, | |
| 279 | - 'contexts' => method_exists( $connector, 'get_context_labels' ) ? (array) $connector->get_context_labels() : array(), | |
| 280 | - 'actions' => method_exists( $connector, 'get_action_labels' ) ? (array) $connector->get_action_labels() : array(), | |
| 281 | - ); | |
| 282 | - } | |
| 283 | - | |
| 284 | - /** | |
| 285 | - * Filter the connector list surfaced to the Abilities API. | |
| 286 | - * | |
| 287 | - * Mirrors `wp_stream_connectors` for the abilities-facing list so | |
| 288 | - * third parties can add/remove entries shown to REST/MCP callers | |
| 289 | - * without affecting which connectors actually register hooks. | |
| 290 | - * | |
| 291 | - * @param array $out Connector metadata entries. | |
| 292 | - */ | |
| 293 | - return apply_filters( 'wp_stream_abilities_connectors', $out ); | |
| 294 | - } | |
| 295 | - | |
| 296 | - /** | |
| 297 | - * Return slugs of every Stream connector, including admin-only ones. | |
| 298 | - * Companion to get_all_including_admin_only(); used by abilities that | |
| 299 | - * need to validate an incoming connector slug (e.g. stream/create- | |
| 300 | - * exclusion-rule) against the full site-wide list rather than the | |
| 301 | - * request-context-gated $this->connectors registry. | |
| 302 | - * | |
| 303 | - * @return string[] | |
| 304 | - */ | |
| 305 | - public function get_all_slugs_including_admin_only() { | |
| 306 | - $slugs = array(); | |
| 307 | - | |
| 308 | - foreach ( $this->build_full_connector_classes() as $connector ) { | |
| 309 | - $slugs[] = (string) $connector->name; | |
| 310 | - } | |
| 311 | - | |
| 312 | - return $slugs; | |
| 313 | - } | |
| 314 | - | |
| 315 | - /** | |
| 316 | - * Instantiate every built-in and integrated connector class and return | |
| 317 | - * the ones whose dependencies are satisfied, regardless of the | |
| 318 | - * `register_admin` / `register_frontend` gates that load_connectors() | |
| 319 | - * applies. Helper for the metadata-only accessors above; does not | |
| 320 | - * register hooks and does not mutate $this->connectors. | |
| 321 | - * | |
| 322 | - * @return Connector[] | |
| 323 | - */ | |
| 324 | - protected function build_full_connector_classes() { | |
| 325 | - // Same canonical slug list load_connectors() uses. Keep in sync. | |
| 326 | - $connectors = array( | |
| 327 | - // Core Connectors. | |
| 328 | - 'blogs', | |
| 329 | - 'comments', | |
| 330 | - 'editor', | |
| 331 | - 'installer', | |
| 332 | - 'media', | |
| 333 | - 'menus', | |
| 334 | - 'posts', | |
| 335 | - 'settings', | |
| 336 | - 'taxonomies', | |
| 337 | - 'users', | |
| 338 | - 'widgets', | |
| 339 | - | |
| 340 | - // Integrated Connectors. | |
| 341 | - 'acf', | |
| 342 | - 'bbpress', | |
| 343 | - 'buddypress', | |
| 344 | - 'edd', | |
| 345 | - 'gravityforms', | |
| 346 | - 'jetpack', | |
| 347 | - 'mercator', | |
| 348 | - 'two-factor', | |
| 349 | - 'user-switching', | |
| 350 | - 'woocommerce', | |
| 351 | - 'wordpress-seo', | |
| 352 | - ); | |
| 353 | - | |
| 354 | - $classes = array(); | |
| 355 | - foreach ( $connectors as $connector ) { | |
| 356 | - include_once $this->plugin->locations['dir'] . '/connectors/class-connector-' . $connector . '.php'; | |
| 357 | - | |
| 358 | - $class_name = sprintf( '\WP_Stream\Connector_%s', str_replace( '-', '_', $connector ) ); | |
| 359 | - if ( ! class_exists( $class_name ) ) { | |
| 360 | - continue; | |
| 361 | - } | |
| 362 | - | |
| 363 | - $class = new $class_name(); | |
| 364 | - $classes[ $class->name ] = $class; | |
| 365 | - } | |
| 366 | - | |
| 367 | - /** This filter is documented in classes/class-connectors.php */ | |
| 368 | - $connector_classes = apply_filters( 'wp_stream_connectors', $classes ); | |
| 369 | - | |
| 370 | - $out = array(); | |
| 371 | - foreach ( $connector_classes as $connector ) { | |
| 372 | - if ( ! is_subclass_of( $connector, 'WP_Stream\Connector' ) ) { | |
| 373 | - continue; | |
| 374 | - } | |
| 375 | - if ( ! $connector->is_dependency_satisfied() ) { | |
| 376 | - continue; | |
| 377 | - } | |
| 378 | - if ( | |
| 379 | - ! method_exists( $connector, 'get_label' ) | |
| 380 | - || ! method_exists( $connector, 'get_context_labels' ) | |
| 381 | - || ! method_exists( $connector, 'get_action_labels' ) | |
| 382 | - ) { | |
| 383 | - continue; | |
| 384 | - } | |
| 385 | - $out[] = $connector; | |
| 386 | - } | |
| 387 | - | |
| 388 | - return $out; | |
| 389 | 236 | } |
| 390 | 237 | |
| 391 | 238 | /** |
| 392 | 239 | * Unregisters the context hooks for all connectors. |