Skip to content

Improve definition and filtering of sanitizer default args#1478

Closed
westonruter wants to merge 1 commit intodevelopfrom
add/default-args
Closed

Improve definition and filtering of sanitizer default args#1478
westonruter wants to merge 1 commit intodevelopfrom
add/default-args

Conversation

@westonruter
Copy link
Copy Markdown
Member

At the moment the default args for the sanitizers are not mode available for plugins to filter. This means, for example, if a plugin wants to augment default args they have to currently copy all of them:

add_filter( 'amp_content_sanitizers', function( $sanitizers ) {
	$sanitizers['AMP_Style_Sanitizer']['dynamic_element_selectors'] = array_merge(
		// In case another filter already defined dynamic_element_selectors.
		! empty( $sanitizers['AMP_Style_Sanitizer']['dynamic_element_selectors'] ) ? $sanitizers['AMP_Style_Sanitizer']['dynamic_element_selectors'] : array(),
		// Duplicated from protected AMP_Style_Sanitizer::$DEFAULT_ARGS.
		array(
			'amp-list',
			'amp-live-list',
			'[submit-error]',
			'[submit-success]',
		),
		// Duplicated from protected AMP_Style_Sanitizer::$DEFAULT_ARGS.
		array(
			'#jp-relatedposts',
			'.jp-relatedposts',
			'.jp-relatedposts-items',
			'.jp-relatedposts-post',
		)
	);
	return $sanitizers;
} );

This is not ideal. This PR allows you to instead do:

add_filter( 'amp_content_sanitizers', function( $sanitizers ) {
	$sanitizers['AMP_Style_Sanitizer']['dynamic_element_selectors'] = array_merge(
		$sanitizers['AMP_Style_Sanitizer']['dynamic_element_selectors'],
		array(
			'#jp-relatedposts',
			'.jp-relatedposts',
			'.jp-relatedposts-items',
			'.jp-relatedposts-post',
		)
	);
	return $sanitizers;
} );

The AMP_Base_Sanitizer class gets a new get_default_args() static method which is called up front when obtaining the list of sanitizers and their args. This then exposes the default args to the amp_content_sanitizers filter to customize.

This PR also deprecates the DEFAULT_ARGS class variable and consolidates the location of default args to be all defined in this get_default_args() methods.

@westonruter westonruter added this to the v1.1 milestone Sep 28, 2018
'AMP_Tag_And_Attribute_Sanitizer', // Always ultimate.
);

// @todo This doesn't help for classes that get added via amp_content_sanitizers. Does that matter?
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

In practice this probably doesn't matter. If someone is adding a new sanitizer, they'd be doing so via a filter anyway. They would just be required to supply the defaults when they do so. For example:

add_filter( 'amp_content_sanitizers', function( $sanitizers ) {
    require_once __DIR__ . '/class-sanitizer.php'
    return array_merge(
        $sanitizers,
        array(
            '\Foo\Sanitizer' => Sanitizer::get_default_args(),
        )
    );
} );

@westonruter
Copy link
Copy Markdown
Member Author

This should be considered as part of the whole refactor.

@googlebot googlebot added the cla: yes Signed the Google CLA label Sep 6, 2019
Move amp_get_content_sanitizers defaults to get_default_args
@amedina amedina removed this from the v2.0 milestone Mar 31, 2020
@westonruter
Copy link
Copy Markdown
Member Author

Closing this as stale, and the approach we take should probably be part of the modularization effort. See #4570.

@schlessera schlessera deleted the add/default-args branch November 6, 2020 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Signed the Google CLA Sanitizers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants