Skip to content

Dependencies for tag visitors in Optimization Detective #1419

@westonruter

Description

@westonruter

In at least one case, there is a need currently for a tag visitor to run after another tag visitor has run:

/**
* Registers the tag visitor for image tags.
*
* @since 1.1.0
*
* @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' );
}
// 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 );

The tag visitor in Enhanced Responsive Sizes needs to run after the IMG tag visitor be

The Image Prioritizer's tag visitor is currently registered with the name ID of img-tags which is not ideal:

// Note: The class is invocable (it has an __invoke() method).
$img_visitor = new Image_Prioritizer_Img_Tag_Visitor();
$registry->register( 'img-tags', $img_visitor );

It should rather be prefixed like image-prioritizer-img. The image-prioritizer-img string could even be defined as a class constant like Image_Prioritizer_Img_Tag_Visitor::ID. When Enhanced Responsive Sizes registers its tag visitor, it should be able to explicitly declare this dependency:

function auto_sizes_register_tag_visitors( OD_Tag_Visitor_Registry $registry ): void { 
	$dependencies = array();
	if ( class_exists( 'Image_Prioritizer_Img_Tag_Visitor' ) ) {
		$dependencies[] = Image_Prioritizer_Img_Tag_Visitor::ID;
	}
	$registry->register( 'auto-sizes', 'auto_sizes_visit_tag', $dependencies ); 
} 

Then there wouldn't be a need to rely on action priorities (and the current implementation of OD_Tag_Visitor_Registry) to ensure that one tag visitor runs before another.

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Plugin] Enhanced Responsive ImagesIssues for the Enhanced Responsive Images plugin (formerly Auto Sizes)[Plugin] Image PrioritizerIssues for the Image Prioritizer plugin (dependent on Optimization Detective)[Plugin] Optimization DetectiveIssues for the Optimization Detective plugin[Type] EnhancementA suggestion for improvement of an existing featureblocked

    Projects

    Status

    Done 😃

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions