From 1d1a43945e7de1a73d92e0bcbc9c654056564352 Mon Sep 17 00:00:00 2001 From: Piotr Delawski Date: Wed, 7 Aug 2024 17:21:51 +0200 Subject: [PATCH 1/2] Check connector dependencies in registry and loop only once Connector classes can be validated in a single loop pass and only registered if they pass all the checks. The checks should involve validating that all the connector dependencies are satisfied. --- classes/class-connectors.php | 84 +++++++++++++++--------------------- 1 file changed, 35 insertions(+), 49 deletions(-) diff --git a/classes/class-connectors.php b/classes/class-connectors.php index 5f5cd35c..9d34fd68 100644 --- a/classes/class-connectors.php +++ b/classes/class-connectors.php @@ -98,6 +98,9 @@ public function load_connectors() { 'wordpress-seo', ); + // Get excluded connectors. + $excluded_connectors = array(); + $classes = array(); foreach ( $connectors as $connector ) { // Load connector class file. @@ -106,57 +109,43 @@ public function load_connectors() { // Set fully qualified class name. $class_name = sprintf( '\WP_Stream\Connector_%s', str_replace( '-', '_', $connector ) ); - // Bail if no class loaded. - if ( ! class_exists( $class_name ) ) { + // Bail if no class loaded or it does not extend WP_Stream\Connector. + if ( ! class_exists( $class_name ) || ! is_subclass_of( $class_name, 'WP_Stream\Connector' ) ) { continue; } // Initialize connector. - $class = new $class_name( $this->plugin->log ); + $class = new $class_name(); + $classes[ $class->name ] = $class; + } + /** + * Allows for adding additional connectors via classes that extend Connector. + * + * @param array $classes An array of Connector objects. + */ + $connector_classes = apply_filters( 'wp_stream_connectors', $classes ); + + foreach ( $connector_classes as $connector ) { // Check if the connector class extends WP_Stream\Connector. - if ( ! is_subclass_of( $class, 'WP_Stream\Connector' ) ) { + if ( ! is_subclass_of( $connector, 'WP_Stream\Connector' ) ) { continue; } // Check if the connector events are allowed to be registered in the WP Admin. - if ( is_admin() && ! $class->register_admin ) { + if ( is_admin() && ! $connector->register_admin ) { continue; } // Check if the connector events are allowed to be registered in the WP Frontend. - if ( ! is_admin() && ! $class->register_frontend ) { + if ( ! is_admin() && ! $connector->register_frontend ) { continue; } // Run any final validations the connector may have before used. - if ( $class->is_dependency_satisfied() ) { - $classes[ $class->name ] = $class; - } - } - - /** - * Allows for adding additional connectors via classes that extend Connector. - * - * @param array $classes An array of Connector objects. - */ - $this->connectors = apply_filters( 'wp_stream_connectors', $classes ); - - if ( empty( $this->connectors ) ) { - return; - } - - foreach ( $this->connectors as $connector ) { - if ( ! method_exists( $connector, 'get_label' ) ) { + if ( ! $connector->is_dependency_satisfied() ) { continue; } - $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); - } - - // Get excluded connectors. - $excluded_connectors = array(); - - foreach ( $this->connectors as $connector ) { // Register error for invalid any connector class. if ( ! method_exists( $connector, 'get_label' ) ) { @@ -180,21 +169,6 @@ public function load_connectors() { continue; } - // Check if the connectors extends the Connector class, if not skip it. - if ( ! is_subclass_of( $connector, '\WP_Stream\Connector' ) ) { - /* translators: %s: connector class name, intended to provide help to developers (e.g. "Connector_BuddyPress") */ - $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 ); - continue; - } - - // Store connector label. - if ( ! in_array( $connector->name, $this->term_labels['stream_connector'], true ) ) { - $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); - } - - $connector_name = $connector->name; - $is_excluded = in_array( $connector_name, $excluded_connectors, true ); - /** * Allows excluded connectors to be overridden and registered. * @@ -202,17 +176,29 @@ public function load_connectors() { * @param string $connector The current connector's slug. * @param array $excluded_connectors An array of all excluded connector slugs. */ - $is_excluded_connector = apply_filters( 'wp_stream_check_connector_is_excluded', $is_excluded, $connector_name, $excluded_connectors ); + $is_excluded_connector = apply_filters( + 'wp_stream_check_connector_is_excluded', + in_array( $connector->name, $excluded_connectors, true ), + $connector->name, + $excluded_connectors + ); if ( $is_excluded_connector ) { continue; } + // Add connector to the registry. + $this->connectors[ $connector->name ] = $connector; + + // Register the connector. $connector->register(); // Link context labels to their connector. $this->contexts[ $connector->name ] = $connector->get_context_labels(); + // Store connector label. + $this->term_labels['stream_connector'][ $connector->name ] = $connector->get_label(); + // Add new terms to our label lookup array. $this->term_labels['stream_action'] = array_merge( $this->term_labels['stream_action'], @@ -229,8 +215,8 @@ public function load_connectors() { /** * Fires after all connectors have been registered. * - * @param array $labels All register connectors labels array - * @param Connectors $connectors The Connectors object + * @param array $labels All register connectors labels array + * @param Connectors $connector_classes The Connectors object */ do_action( 'wp_stream_after_connectors_registration', $labels, $this ); } From 459abce9cbcb907a6eff2d86ee4fe5166d513ede Mon Sep 17 00:00:00 2001 From: Piotr Delawski Date: Fri, 9 Aug 2024 16:27:24 +0200 Subject: [PATCH 2/2] Remove redundant check --- classes/class-connectors.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/class-connectors.php b/classes/class-connectors.php index 9d34fd68..5af5469c 100644 --- a/classes/class-connectors.php +++ b/classes/class-connectors.php @@ -109,8 +109,8 @@ public function load_connectors() { // Set fully qualified class name. $class_name = sprintf( '\WP_Stream\Connector_%s', str_replace( '-', '_', $connector ) ); - // Bail if no class loaded or it does not extend WP_Stream\Connector. - if ( ! class_exists( $class_name ) || ! is_subclass_of( $class_name, 'WP_Stream\Connector' ) ) { + // Bail if no class loaded. + if ( ! class_exists( $class_name ) ) { continue; }