Skip to content
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

Closed
wants to merge 1 commit into from
Closed

Connectors: Check the request type and unregister connectors that don't support it #1269

wants to merge 1 commit into from

Conversation

coreymckrill
Copy link
Contributor

This is an alternative approach to #1264. Since requests to WP APIs are not considered to be is_admin, connectors that are disabled for the "frontend" are also disabled for API requests, which is not always (or maybe ever) intended. In this PR we allow all connectors to be registered, regardless of is_admin, and then take the following steps when the parse_request action is fired (this is when the REST API is initialized):

  1. Check to see what kind of request we have, differentiating between admin, frontend, and api requests.
  2. Cycle through all the connectors and for each, check if it supports that type of request (via the register_admin, register_frontend, and the new register_api class properties)
  3. If a connector does not support the current request type, unregister it.

This addresses the issue of the Posts connector not logging post changes that happen via the REST API, without changing its behavior for actual frontend requests.

Fixes #1195
Fixes #1250

Checklist

  • Project documentation has been updated to reflect the changes in this pull request, if applicable.
  • I have tested the changes in the local development environment (see contributing.md).
  • I have added phpunit tests.

Copy link
Contributor

@kasparsd kasparsd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great! Thanks so much for working on this @coreymckrill!

Please see my initial feedback inline the pull request.

continue;
}

if (
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

* Determines the request type (admin, api, frontend) and unregisters connectors that don't support it.
*/
public function check_request_type() {
if ( ! did_action( 'parse_request' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 parse_request hasn't run?

/**
* Determines the request type (admin, api, frontend) and unregisters connectors that don't support it.
*/
public function check_request_type() {
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

@@ -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 ) {
Copy link
Contributor

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.

@kasparsd
Copy link
Contributor

kasparsd commented Mar 8, 2022

This has been fixed by #1264.

@kasparsd kasparsd closed this Mar 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants