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

Add a filter for programmatically adding entries for Safelist #4343

Closed
NataliaDrause opened this issue Sep 9, 2021 · 5 comments · Fixed by #4444
Closed

Add a filter for programmatically adding entries for Safelist #4343

NataliaDrause opened this issue Sep 9, 2021 · 5 comments · Fixed by #4444
Assignees
Labels
effort: [XS] < 1 day of estimated development time module: remove unused css priority: low Issues that can wait type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Milestone

Comments

@NataliaDrause
Copy link
Contributor

Currently, there is no filter to programmatically safelist selectors for the Remove Unused CSS option.

Such filter could be useful for compatibility development in 3rd-party plugins/themes.

Related request: https://secure.helpscout.net/conversation/1609150673/289293

Slack: https://wp-media.slack.com/archives/C029G1B5HD2/p1631172880106900

@iCaspar
Copy link
Contributor

iCaspar commented Sep 9, 2021

The following should work for themes and plugins wanting to modify the safelist for RUCSS compatibility:

function cg_test_stuff( $safelist ) {
    $safelist[] = '.theme-specific-class';
    return $safelist;
}
add_filter( 'pre_get_rocket_option_remove_unused_css_safelist', 'cg_test_stuff' );

By way of documentation:
$safelist is an array with a single filename, class, id, tag, or attribute per item.
Any item can include the wildcard sequence (.*) indicating to safelist all matching items.
Note that currently we only handle safelisting attributes, without regard to any values.

[
    '/path/to/safelisted.css',   // safelist everything in this file
    '.classname',   // a class indicated by leading dott
    '#idname',   // an id indicated by leading hash
    'h1',   // any valid html tag
    '[title]',   // an attribute indicated by enclosed square brackets
    '#id-(.*)',   // matches #id-1213ab3, #id-anythingyouwant, etc
    '.class(.*)me',   // matches .class123me, .classClassyme, etc
    '#id.classname',   // invalid, two things in the same entry -- will be ignored
    '[title="image-title"]',   // invalid, only attributes are considered -- will be ignored
]

@iCaspar
Copy link
Contributor

iCaspar commented Sep 9, 2021

Note on the above... it will work. It will also actually update the wpr safelist setting next time the WPR settings page is loaded (as it filters on the pre_get_rocket_option).
That may or may not be a desired side-effect.
@GeekPress @Tabrisrp: We could add a filter rocket_pre_send_unused_css_safelist to filter only when shaking at this point:

'rucss_safelist' => $this->options->get( 'remove_unused_css_safelist', [] ),
that would apply only when actually sending for usedCss generation.
Would be an [XS] item.
Thoughts?

Meanwhile the save to wpr options can be avoided by checking for being on the rocket admin page and returning early:

function cg_test_stuff( $safelist ) {
	global $pagenow;
	if ( 'options-general.php' === $pagenow && 'wprocket' === $_GET['page'] ) {
		return $safelist;
	}
	$safelist[] = '.theme-specific-class';
	return $safelist;
}

@Tabrisrp
Copy link
Contributor

Tabrisrp commented Sep 9, 2021

For our other options, we always use a separate, additional filter for custom/3rd parties. Possibly it's a good approach to continue this?

Also, it avoids tying it to the option itself, if for any reason in the future we change option name or anything like that.

@pedddro
Copy link

pedddro commented Sep 27, 2021

Is there a similar filter of "pre_get_rocket_option_remove_unused_css_safelist" but for Optimize CSS? some themes need it to avoid a high CLS while using Optimize CSS such as Elementor. Excluding files does not currently help much.

@alfonso100
Copy link
Contributor

related question from a 3rd party developer: https://secure.helpscout.net/conversation/1662864389/301553/

@GeekPress GeekPress changed the title RUCSS: add a filter for programmatically adding entries for Safelist Add a filter for programmatically adding entries for Safelist Oct 20, 2021
@GeekPress GeekPress added module: remove unused css priority: low Issues that can wait type: enhancement Improvements that slightly enhance existing functionality and are fast to implement labels Oct 20, 2021
@Tabrisrp Tabrisrp self-assigned this Oct 20, 2021
@Tabrisrp Tabrisrp added the effort: [XS] < 1 day of estimated development time label Oct 20, 2021
@GeekPress GeekPress added this to the 3.10.2 milestone Oct 22, 2021
@Tabrisrp Tabrisrp mentioned this issue Oct 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: [XS] < 1 day of estimated development time module: remove unused css priority: low Issues that can wait type: enhancement Improvements that slightly enhance existing functionality and are fast to implement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants