-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connectors: Check the request type and unregister connectors that don't support it #1269
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,9 @@ class Connectors { | |
public function __construct( $plugin ) { | ||
$this->plugin = $plugin; | ||
$this->load_connectors(); | ||
|
||
// Run this after the REST API has been initialized. | ||
add_action( 'parse_request', array( $this, 'check_request_type' ), 11 ); | ||
} | ||
|
||
/** | ||
|
@@ -119,16 +122,6 @@ public function load_connectors() { | |
continue; | ||
} | ||
|
||
// Check if the connector events are allowed to be registered in the WP Admin. | ||
if ( is_admin() && ! $class->register_admin ) { | ||
continue; | ||
} | ||
|
||
// Check if the connector events are allowed to be registered in the WP Frontend. | ||
if ( ! is_admin() && ! $class->register_frontend ) { | ||
continue; | ||
} | ||
|
||
// Run any final validations the connector may have before used. | ||
if ( $class->is_dependency_satisfied() ) { | ||
$classes[ $class->name ] = $class; | ||
|
@@ -274,4 +267,30 @@ public function reload_connector( $name ) { | |
$this->connectors[ $name ]->register(); | ||
} | ||
} | ||
|
||
/** | ||
* Determines the request type (admin, api, frontend) and unregisters connectors that don't support it. | ||
*/ | ||
public function check_request_type() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming of this suggests that it only checks for the request type but it does that and also unloading of the connectors. Could we somehow decouple these two actions to make this easier to test? |
||
if ( ! did_action( 'parse_request' ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this solving for? Are there cases when this could be called even when |
||
return; | ||
} | ||
|
||
$is_admin = is_admin(); | ||
$is_api = wp_stream_is_api_request(); | ||
|
||
foreach ( $this->connectors as $connector ) { | ||
if ( ! $connector->is_registered() ) { | ||
continue; | ||
} | ||
|
||
if ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like a lot of nesting and conditionals. Could we somehow refactor this for readability and maybe add unit tests? |
||
( $is_admin && ! $connector->register_admin ) | ||
|| ( $is_api && ! $connector->register_api ) | ||
|| ( ! $is_admin && ! $is_api && ! $connector->register_frontend ) | ||
) { | ||
$connector->unregister(); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By registering all connectors during the plugin init and deregistering some during
parse_request
introduces a period when all connectors are active for some period of time which can lead to issues and situations which are hard to debug.Could we maybe delay registration of all connectors until
parse_request
when the context of the request (admin, REST, frontend) is truly known. Need to verify it, though.