mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-08-11 20:30:23 +00:00
Media: Introduce filters to customize the results from wp_get_loading_optimization_attributes().
This changeset introduces two filters that allow customizing the loading optimization attributes array returned from `wp_get_loading_optimization_attributes()` for individual HTML tags: * The `wp_get_loading_optimization_attributes` filter can be used to modify the results from the WordPress core logic. * The `pre_wp_get_loading_optimization_attributes` filter can be used to use entirely custom logic and effectively short-circuit the core function. Props pereirinha, mukesh27, spacedmonkey, joemcgill. Fixes #58893. git-svn-id: https://develop.svn.wordpress.org/trunk@56651 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -5440,6 +5440,109 @@ EOF;
|
||||
$this->assertSame( 1, wp_increase_content_media_count( 0 ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for pre_wp_get_loading_optimization_attributes filter.
|
||||
*
|
||||
* @ticket 58893
|
||||
*/
|
||||
public function test_pre_wp_get_loading_optimization_attributes_filter() {
|
||||
add_filter(
|
||||
'pre_wp_get_loading_optimization_attributes',
|
||||
static function ( $loading_attrs ) {
|
||||
if ( false === $loading_attrs ) {
|
||||
// Initialize as an empty array.
|
||||
$loading_attrs = array();
|
||||
}
|
||||
$loading_attrs['fetchpriority'] = 'high';
|
||||
|
||||
return $loading_attrs;
|
||||
},
|
||||
10,
|
||||
1
|
||||
);
|
||||
|
||||
$attr = $this->get_width_height_for_high_priority();
|
||||
|
||||
$this->assertSame(
|
||||
array( 'fetchpriority' => 'high' ),
|
||||
wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
|
||||
'The filter did not return early fetchpriority attribute'
|
||||
);
|
||||
|
||||
// Clean up the filter.
|
||||
add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_false' );
|
||||
|
||||
$this->assertSameSets(
|
||||
array( 'loading' => 'lazy' ),
|
||||
wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
|
||||
'The filter did not return the default attributes.'
|
||||
);
|
||||
|
||||
// Return no loading attributes.
|
||||
add_filter( 'pre_wp_get_loading_optimization_attributes', '__return_empty_array' );
|
||||
|
||||
$this->assertSameSets(
|
||||
array(),
|
||||
wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
|
||||
'The filter did not clean up all attributes.'
|
||||
);
|
||||
|
||||
// Modify the loading attributes with any custom attributes.
|
||||
add_filter(
|
||||
'pre_wp_get_loading_optimization_attributes',
|
||||
static function ( $loading_attrs ) {
|
||||
if ( false === $loading_attrs ) {
|
||||
// Initialize as an empty array.
|
||||
$loading_attrs = array();
|
||||
}
|
||||
$loading_attrs['custom_attr'] = 'custom_value';
|
||||
|
||||
return $loading_attrs;
|
||||
},
|
||||
10,
|
||||
1
|
||||
);
|
||||
|
||||
$this->assertSameSets(
|
||||
array( 'custom_attr' => 'custom_value' ),
|
||||
wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
|
||||
'The filter did not return custom attributes.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests for wp_get_loading_optimization_attributes filter.
|
||||
*
|
||||
* @ticket 58893
|
||||
*/
|
||||
public function test_wp_get_loading_optimization_attributes_filter() {
|
||||
$attr = $this->get_width_height_for_high_priority();
|
||||
|
||||
$this->assertSameSets(
|
||||
array( 'loading' => 'lazy' ),
|
||||
wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
|
||||
'Before the filter it will not return the loading attribute.'
|
||||
);
|
||||
|
||||
add_filter(
|
||||
'wp_get_loading_optimization_attributes',
|
||||
static function ( $loading_attrs ) {
|
||||
unset( $loading_attrs['loading'] );
|
||||
$loading_attrs['fetchpriority'] = 'high';
|
||||
|
||||
return $loading_attrs;
|
||||
},
|
||||
10,
|
||||
1
|
||||
);
|
||||
|
||||
$this->assertSameSets(
|
||||
array( 'fetchpriority' => 'high' ),
|
||||
wp_get_loading_optimization_attributes( 'img', $attr, 'the_content' ),
|
||||
'After the filter it will not return the fetchpriority attribute.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to keep track of the last context returned by the 'wp_get_attachment_image_context' filter.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user