From cd8d6b2e8e811a4f10cbb3315366a008c9d46256 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 13 Aug 2024 19:00:52 -0400 Subject: [PATCH 1/8] Update tag visitor registration with unique ID constants Replaced hardcoded ID strings with unique ID constants for tag visitors to ensure consistency and maintainability. Updated the `OD_Tag_Visitor_Registry::register` method to handle dependencies, adding them only if they are not already registered. --- ...itizer-background-image-styled-tag-visitor.php | 5 +++++ .../class-image-prioritizer-img-tag-visitor.php | 5 +++++ plugins/image-prioritizer/helper.php | 4 ++-- .../class-od-tag-visitor-registry.php | 15 +++++++++++++-- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php b/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php index 8e6a01f522..f500a0e234 100644 --- a/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php +++ b/plugins/image-prioritizer/class-image-prioritizer-background-image-styled-tag-visitor.php @@ -19,6 +19,11 @@ */ final class Image_Prioritizer_Background_Image_Styled_Tag_Visitor extends Image_Prioritizer_Tag_Visitor { + /** + * The ID used the register the class + */ + + public const ID = 'image-prioritizer-bg-img'; /** * Visits a tag. * diff --git a/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php b/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php index 4395cdaf83..9682dd7985 100644 --- a/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php +++ b/plugins/image-prioritizer/class-image-prioritizer-img-tag-visitor.php @@ -19,6 +19,11 @@ */ final class Image_Prioritizer_Img_Tag_Visitor extends Image_Prioritizer_Tag_Visitor { + /** + * The ID used the register the class + */ + public const ID = 'image-prioritizer-img'; + /** * Visits a tag. * diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index e214b2c673..600ca59be9 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -32,8 +32,8 @@ function image_prioritizer_render_generator_meta_tag(): void { function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $registry ): void { // Note: The class is invocable (it has an __invoke() method). $img_visitor = new Image_Prioritizer_Img_Tag_Visitor(); - $registry->register( 'img-tags', $img_visitor ); + $registry->register( $img_visitor::ID, $img_visitor ); $bg_image_visitor = new Image_Prioritizer_Background_Image_Styled_Tag_Visitor(); - $registry->register( 'bg-image-tags', $bg_image_visitor ); + $registry->register( $bg_image_visitor::ID, $bg_image_visitor ); } diff --git a/plugins/optimization-detective/class-od-tag-visitor-registry.php b/plugins/optimization-detective/class-od-tag-visitor-registry.php index d57f6f051f..09e7b1a0c9 100644 --- a/plugins/optimization-detective/class-od-tag-visitor-registry.php +++ b/plugins/optimization-detective/class-od-tag-visitor-registry.php @@ -37,9 +37,20 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate { * * @param string $id Identifier for the tag visitor. * @param callable $tag_visitor_callback Tag visitor callback. + * @param array $dependencies array with ID as key and Callable as value */ - public function register( string $id, callable $tag_visitor_callback ): void { - $this->visitors[ $id ] = $tag_visitor_callback; + public function register( string $id, callable $tag_visitor_callback, array $dependencies = array() ): void + { + if (!empty($dependencies)) { + foreach ($dependencies as $dependency_id => $dependency_callback) { + if (!self::is_registered($dependency_id)) { + $this->visitors[$dependency_id] = $dependency_callback; + } + } + } + if (!self::is_registered($id)) { + $this->visitors[$id] = $tag_visitor_callback; + } } /** From 4c73618a049655c9711047e47bd7754ff58636f9 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 15 Aug 2024 16:24:02 -0400 Subject: [PATCH 2/8] Update visitor registration and add constant ID Standardize spacing in visitor registration method for better readability. Additionally, introduce a constant ID in Image_Prioritizer_Tag_Visitor to facilitate consistent class registration. --- .../class-image-prioritizer-tag-visitor.php | 5 +++++ .../class-od-tag-visitor-registry.php | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/plugins/image-prioritizer/class-image-prioritizer-tag-visitor.php b/plugins/image-prioritizer/class-image-prioritizer-tag-visitor.php index b768725a6b..2b251573a4 100644 --- a/plugins/image-prioritizer/class-image-prioritizer-tag-visitor.php +++ b/plugins/image-prioritizer/class-image-prioritizer-tag-visitor.php @@ -19,6 +19,11 @@ */ abstract class Image_Prioritizer_Tag_Visitor { + /** + * The ID used the register the class + */ + public const ID = 'image-prioritizer-tag'; + /** * Visits a tag. * diff --git a/plugins/optimization-detective/class-od-tag-visitor-registry.php b/plugins/optimization-detective/class-od-tag-visitor-registry.php index 09e7b1a0c9..27c66b2d02 100644 --- a/plugins/optimization-detective/class-od-tag-visitor-registry.php +++ b/plugins/optimization-detective/class-od-tag-visitor-registry.php @@ -41,15 +41,15 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate { */ public function register( string $id, callable $tag_visitor_callback, array $dependencies = array() ): void { - if (!empty($dependencies)) { - foreach ($dependencies as $dependency_id => $dependency_callback) { - if (!self::is_registered($dependency_id)) { - $this->visitors[$dependency_id] = $dependency_callback; + if ( ! empty( $dependencies ) ) { + foreach ( $dependencies as $dependency_id => $dependency_callback ) { + if ( ! self::is_registered( $dependency_id ) ) { + $this->visitors[ $dependency_id ] = $dependency_callback; } } } - if (!self::is_registered($id)) { - $this->visitors[$id] = $tag_visitor_callback; + if ( ! self::is_registered( $id ) ) { + $this->visitors[ $id ] = $tag_visitor_callback; } } From 16b896d533fa865989f481333041669ab46b8986 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 15 Aug 2024 18:16:01 -0400 Subject: [PATCH 3/8] Refactor tag visitor registration to ensure proper dependencies Updated the tag visitor registration to use a consistent method for registering dependencies. Specifically, adjusted the `register` method in `class-od-tag-visitor-registry.php` and modified `auto_sizes_register_tag_visitors` to include a conditional dependency for the Image Prioritizer visitor. Removed unnecessary priority in the `add_action` call for better execution order management. --- plugins/auto-sizes/optimization-detective.php | 8 ++++++-- .../class-od-tag-visitor-registry.php | 9 ++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/plugins/auto-sizes/optimization-detective.php b/plugins/auto-sizes/optimization-detective.php index e753fd2f38..35f60c9f0e 100644 --- a/plugins/auto-sizes/optimization-detective.php +++ b/plugins/auto-sizes/optimization-detective.php @@ -59,8 +59,12 @@ function auto_sizes_visit_tag( OD_Tag_Visitor_Context $context ): bool { * @param OD_Tag_Visitor_Registry $registry Tag visitor registry. */ function auto_sizes_register_tag_visitors( OD_Tag_Visitor_Registry $registry ): void { - $registry->register( 'auto-sizes', 'auto_sizes_visit_tag' ); + $registry->register( + 'auto-sizes', + 'auto_sizes_visit_tag', + defined( 'Image_Prioritizer_Img_Tag_Visitor::ID' ) ? array( Image_Prioritizer_Img_Tag_Visitor::ID => new Image_Prioritizer_Img_Tag_Visitor() ) : array() + ); } // Important: The Image Prioritizer's IMG tag visitor is registered at priority 10, so priority 100 ensures that the loading attribute has been correctly set by the time the Auto Sizes visitor runs. -add_action( 'od_register_tag_visitors', 'auto_sizes_register_tag_visitors', 100 ); +add_action( 'od_register_tag_visitors', 'auto_sizes_register_tag_visitors' ); diff --git a/plugins/optimization-detective/class-od-tag-visitor-registry.php b/plugins/optimization-detective/class-od-tag-visitor-registry.php index 27c66b2d02..2cf7220151 100644 --- a/plugins/optimization-detective/class-od-tag-visitor-registry.php +++ b/plugins/optimization-detective/class-od-tag-visitor-registry.php @@ -35,16 +35,15 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate { * * @phpstan-param TagVisitorCallback $tag_visitor_callback * - * @param string $id Identifier for the tag visitor. + * @param string $id Identifier for the tag visitor. * @param callable $tag_visitor_callback Tag visitor callback. - * @param array $dependencies array with ID as key and Callable as value + * @param string[] $dependencies Tag visitors that must run before this tag visitor. */ - public function register( string $id, callable $tag_visitor_callback, array $dependencies = array() ): void - { + public function register( string $id, callable $tag_visitor_callback, array $dependencies = array() ): void { if ( ! empty( $dependencies ) ) { foreach ( $dependencies as $dependency_id => $dependency_callback ) { if ( ! self::is_registered( $dependency_id ) ) { - $this->visitors[ $dependency_id ] = $dependency_callback; + $this->register( $dependency_id, $dependency_callback ); } } } From 1334322e38fe42442f254046838a04ec82e8caa2 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 21 Aug 2024 19:44:59 -0400 Subject: [PATCH 4/8] Update plugins/optimization-detective/class-od-tag-visitor-registry.php Co-authored-by: Weston Ruter --- .../class-od-tag-visitor-registry.php | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/plugins/optimization-detective/class-od-tag-visitor-registry.php b/plugins/optimization-detective/class-od-tag-visitor-registry.php index 2cf7220151..9751e109d8 100644 --- a/plugins/optimization-detective/class-od-tag-visitor-registry.php +++ b/plugins/optimization-detective/class-od-tag-visitor-registry.php @@ -40,16 +40,10 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate { * @param string[] $dependencies Tag visitors that must run before this tag visitor. */ public function register( string $id, callable $tag_visitor_callback, array $dependencies = array() ): void { - if ( ! empty( $dependencies ) ) { - foreach ( $dependencies as $dependency_id => $dependency_callback ) { - if ( ! self::is_registered( $dependency_id ) ) { - $this->register( $dependency_id, $dependency_callback ); - } - } - } - if ( ! self::is_registered( $id ) ) { - $this->visitors[ $id ] = $tag_visitor_callback; - } + $this->visitors[ $id ] = array( + 'callback' => $tag_visitor_callback, + 'dependencies' => $dependencies, + ); } /** From dbdad085ce5f38e3f8b46558a627a1fdf580bffd Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 21 Aug 2024 19:45:18 -0400 Subject: [PATCH 5/8] Update plugins/auto-sizes/optimization-detective.php Co-authored-by: Weston Ruter --- plugins/auto-sizes/optimization-detective.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/auto-sizes/optimization-detective.php b/plugins/auto-sizes/optimization-detective.php index 35f60c9f0e..12f41dbfac 100644 --- a/plugins/auto-sizes/optimization-detective.php +++ b/plugins/auto-sizes/optimization-detective.php @@ -62,7 +62,7 @@ function auto_sizes_register_tag_visitors( OD_Tag_Visitor_Registry $registry ): $registry->register( 'auto-sizes', 'auto_sizes_visit_tag', - defined( 'Image_Prioritizer_Img_Tag_Visitor::ID' ) ? array( Image_Prioritizer_Img_Tag_Visitor::ID => new Image_Prioritizer_Img_Tag_Visitor() ) : array() + defined( 'Image_Prioritizer_Img_Tag_Visitor::ID' ) ? array( Image_Prioritizer_Img_Tag_Visitor::ID ) : array() ); } From 91be88fb270dc29f3a9ea52611544f18f0e094fb Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 21 Aug 2024 19:45:25 -0400 Subject: [PATCH 6/8] Update plugins/image-prioritizer/helper.php Co-authored-by: Weston Ruter --- plugins/image-prioritizer/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index 600ca59be9..eb71d270ec 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -32,7 +32,7 @@ function image_prioritizer_render_generator_meta_tag(): void { function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $registry ): void { // Note: The class is invocable (it has an __invoke() method). $img_visitor = new Image_Prioritizer_Img_Tag_Visitor(); - $registry->register( $img_visitor::ID, $img_visitor ); + $registry->register( Image_Prioritizer_Img_Tag_Visitor::ID, $img_visitor ); $bg_image_visitor = new Image_Prioritizer_Background_Image_Styled_Tag_Visitor(); $registry->register( $bg_image_visitor::ID, $bg_image_visitor ); From 2e640f3cfd24acdea62873eb67c14846d043c5ff Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 21 Aug 2024 19:45:34 -0400 Subject: [PATCH 7/8] Update plugins/image-prioritizer/helper.php Co-authored-by: Weston Ruter --- plugins/image-prioritizer/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/image-prioritizer/helper.php b/plugins/image-prioritizer/helper.php index eb71d270ec..3218e552ca 100644 --- a/plugins/image-prioritizer/helper.php +++ b/plugins/image-prioritizer/helper.php @@ -35,5 +35,5 @@ function image_prioritizer_register_tag_visitors( OD_Tag_Visitor_Registry $regis $registry->register( Image_Prioritizer_Img_Tag_Visitor::ID, $img_visitor ); $bg_image_visitor = new Image_Prioritizer_Background_Image_Styled_Tag_Visitor(); - $registry->register( $bg_image_visitor::ID, $bg_image_visitor ); + $registry->register( Image_Prioritizer_Background_Image_Styled_Tag_Visitor::ID, $bg_image_visitor ); } From e56c75d9f1374b02c41e0be10f1c1bafdabbd46b Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 26 Aug 2024 18:08:35 -0400 Subject: [PATCH 8/8] Refactor tag visitor registration to ensure proper dependencies Updated the tag visitor registration to use a consistent method for registering dependencies. Specifically, adjusted the `register` method in `class-od-tag-visitor-registry.php` and modified `auto_sizes_register_tag_visitors` to include a conditional dependency for the Image Prioritizer visitor. Removed unnecessary priority in the `add_action` call for better execution order management. --- .../class-od-tag-visitor-registry.php | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/plugins/optimization-detective/class-od-tag-visitor-registry.php b/plugins/optimization-detective/class-od-tag-visitor-registry.php index 9751e109d8..95100364e4 100644 --- a/plugins/optimization-detective/class-od-tag-visitor-registry.php +++ b/plugins/optimization-detective/class-od-tag-visitor-registry.php @@ -30,6 +30,13 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate { */ private $visitors = array(); + /** + * sorted_Visitors. + * + * @var array + */ + private $sorted_visitors = array(); + /** * Registers a tag visitor. * @@ -64,7 +71,7 @@ public function is_registered( string $id ): bool { */ public function get_registered( string $id ): ?callable { if ( $this->is_registered( $id ) ) { - return $this->visitors[ $id ]; + return $this->visitors[ $id ]['callback']; } return null; } @@ -89,7 +96,31 @@ public function unregister( string $id ): bool { * @return ArrayIterator ArrayIterator for tag visitors. */ public function getIterator(): ArrayIterator { + // sort the visitors so dependents load first return new ArrayIterator( $this->visitors ); + if( array() !== $this->sorted_visitors ){ + return new ArrayIterator( $this->sorted_visitors ); + } + + foreach( $this->visitors as $key => $visitor ){ + if( $visitor['dependencies'] && array() !== $visitor['dependencies'] ) { + foreach( $visitor['dependencies'] as $dependent ) { + if ( array_key_exists( $dependent, $this->sorted_visitors ) ) { + $this->sorted_visitors[ $key ] = $visitor; + } else { + // remove current location in array + unset( $this->visitors[ $key ] ); + // add to the end + $this->visitors[ $key ] = $visitor; + } + } + } else { + $this->sorted_visitors[ $key ] = $visitor; + } + + } + + return new ArrayIterator( $this->sorted_visitors ); } /**