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

Update tag visitor registration with unique ID constants #1469

Open
wants to merge 8 commits into
base: trunk
Choose a base branch
from
8 changes: 6 additions & 2 deletions plugins/auto-sizes/optimization-detective.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) : 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' );
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Member

Choose a reason for hiding this comment

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

This would also make sense to add to Embed_Optimizer_Tag_Visitor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

Choose a reason for hiding this comment

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

Oh yeah, I don't see any change to the Embed_Optimizer_Tag_Visitor class in the Embed Optimizer plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@westonruter I am not sure what is going on here you might need to take this over


/**
* Visits a tag.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions plugins/image-prioritizer/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( Image_Prioritizer_Img_Tag_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( Image_Prioritizer_Background_Image_Styled_Tag_Visitor::ID, $bg_image_visitor );
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,27 @@ final class OD_Tag_Visitor_Registry implements Countable, IteratorAggregate {
*/
private $visitors = array();

/**
* sorted_Visitors.
*
* @var array<string, TagVisitorCallback>
*/
private $sorted_visitors = array();

/**
* Registers a tag visitor.
*
* @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 string[] $dependencies Tag visitors that must run before this tag visitor.
*/
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 {
$this->visitors[ $id ] = array(
'callback' => $tag_visitor_callback,
'dependencies' => $dependencies,
);
}

/**
Expand All @@ -60,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;
}
Expand All @@ -85,7 +96,31 @@ public function unregister( string $id ): bool {
* @return ArrayIterator<string, TagVisitorCallback> 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 );
}

/**
Expand Down
Loading