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

[2.1] Ensure services that are delayed and have requirements are scheduled correctly #6553

Merged
merged 6 commits into from
Aug 23, 2021

Conversation

westonruter
Copy link
Member

Summary

Cherry-picks #6548 onto the 2.1 branch, with the exception of 1db8a17 which is not relevant to the 2.1 branch which does not include the changes from #6518.

Checklist

  • My code is tested and passes existing tests.
  • My code follows the Engineering Guidelines (updates are often made to the guidelines, check it out periodically).

@westonruter westonruter added this to the v2.1.4 milestone Aug 23, 2021
@westonruter westonruter changed the title Fix/service registration dependencies iteration 2.1 [2.1] Ensure services that are delayed and have requirements are scheduled correctly Aug 23, 2021
@westonruter westonruter enabled auto-merge (squash) August 23, 2021 21:40
@github-actions
Copy link
Contributor

Plugin builds for 0ab4cb1 are ready 🛎️!

@westonruter westonruter disabled auto-merge August 23, 2021 21:41
@westonruter westonruter merged commit ac002db into 2.1 Aug 23, 2021
@westonruter westonruter deleted the fix/service-registration-dependencies-iteration-2.1 branch August 23, 2021 21:41
@pierlon pierlon self-assigned this Aug 31, 2021
@pierlon
Copy link
Contributor

pierlon commented Aug 31, 2021

QA Passed

To test this change, I first had to modify the plugin to enable service filtering:

diff --git a/src/AmpWpPlugin.php b/src/AmpWpPlugin.php
--- a/src/AmpWpPlugin.php	(revision 87e6a798bcf5c92bd44872453def3c21af33f823)
+++ b/src/AmpWpPlugin.php	(date 1630414970084)
@@ -47,7 +47,7 @@
 	 */
 
 	// Whether to enable filtering by default or not.
-	const ENABLE_FILTERS_DEFAULT = false;
+	const ENABLE_FILTERS_DEFAULT = true;
 
 	/**
 	 * Prefix to use for all actions and filters.

Then I installed the following plugin:

<?php

/**
 * Plugin Name: Services Test (QA 6553)
 */

namespace ServicesTest;

use AmpProject\AmpWP\Infrastructure\Delayed;
use AmpProject\AmpWP\Infrastructure\HasRequirements;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;

class Service_A implements Service, Delayed {

	public static function get_registration_action() {
		return 'init';
	}
}

class Service_B implements Service, Delayed {

	public static function get_registration_action() {
		return 'wp_loaded';
	}
}

class Service_C implements Service, HasRequirements, Delayed, Registerable {

	protected $registered_action;

	public static function get_requirements() {
		return [ 'service_a', 'service_b' ];
	}

	public static function get_registration_action() {
		return 'admin_init';
	}

	public function register() {
		$this->registered_action = current_action();
		add_action( 'admin_notices', [ $this, 'render_notice' ] );
	}

	public function render_notice() {
		$notice_type = 'admin_init' === $this->registered_action ? 'notice-info' : 'notice-error';
		?>
		<div class="amp-plugin-notice notice <?php echo $notice_type ?>" id="service-c-notice">
			<p>Service C was registered at <code><?php esc_html_e( $this->registered_action ) ?></code>.</p>
		</div>
		<?php
	}
}

add_filter(
	'amp_services',
	static function ($services) {
		return array_merge(
			$services,
			[
				'service_a' => Service_A::class,
				'service_b' => Service_B::class,
				'service_c' => Service_C::class,
			]
		);
	}
);

After activating the plugin, here is the before and after showing at what action Service_C was scheduled:

Before After
image image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants