[ label, help, warning ]. */
public static function optimize_options() {
return array(
'opt_html_comments' => array( 'Remove HTML comments', 'Strips comments such as "This site is optimized with the Yoast SEO plugin". IE conditional comments and comments inside scripts are kept.', '' ),
'opt_sourceurl' => array( 'Remove sourceURL comments', 'Strips /*# sourceURL=… */ markers WordPress adds to inline CSS and JS.', '' ),
'opt_img_auto_sizes' => array( 'Remove wp-img-auto-sizes-contain-inline-css', 'The inline style for sizes="auto" lazy images.', '' ),
'opt_admin_bar_css' => array( 'Remove admin-bar / toolbar CSS', 'Admin bar styles and plugin toolbar CSS such as Ad Inserter\'s #wp-admin-bar-ai-toolbar rules.', '' ),
'opt_global_styles' => array( 'Remove global-styles-inline-css', 'Block editor preset colours, gradients, font sizes, spacing and flex/grid layout rules.', 'Blocks using preset colours, buttons, columns or flex/grid layouts may lose their styling.' ),
'opt_dashicons' => array( 'Remove Dashicons stylesheet', 'The dashicons-css icon font stylesheet.', 'Only safe if no Dashicons icons are shown on the front end.' ),
'opt_yoast_schema' => array( 'Remove Yoast schema (JSON-LD)', 'The \s*#is', '', $html );
}
if ( $s['opt_emoji'] ) {
$html = self::remove_by_id( $html, 'wp-emoji-styles-inline-css' );
$html = self::remove_by_id( $html, 'wp-emoji-settings' );
$html = self::remove_blocks(
$html,
'script',
function ( $body ) {
return false !== stripos( $body, 'wpemojiSettings' ) || false !== stripos( $body, 'wp-emoji-release' ) || false !== stripos( $body, 'wp-emoji-loader' );
}
);
$html = self::replace( '#\s*#i', '', $html );
$html = self::remove_blocks(
$html,
'style',
function ( $body ) {
return 0 === stripos( ltrim( $body ), 'img.wp-smiley,' );
}
);
}
if ( $s['opt_sourceurl'] ) {
$html = self::replace( '#\s*/\*\#\s*source(?:Mapping)?URL=[^*]*\*/#', '', $html );
$html = self::replace( '#\s*//\#\s*source(?:Mapping)?URL=[^\r\n<]*#', '', $html );
}
if ( $s['opt_c0_preconnect'] ) {
$html = self::replace( '#]*\brel\s*=\s*["\']?(?:preconnect|dns-prefetch)\b)(?=[^>]*\bhref\s*=\s*["\']?(?:https?:)?//c0\.wp\.com)[^>]*>[ \t]*(\r?\n)?#i', '', $html );
}
if ( $s['opt_speculation'] ) {
$html = self::replace( '#[ \t]*(\r?\n)?#is', '', $html );
}
if ( $s['opt_jetpack_rp_js'] ) {
$html = self::remove_by_id( $html, 'jetpack_related-posts-js' );
$html = self::remove_by_id( $html, 'jetpack_related-posts-js-extra' );
}
if ( $s['opt_site_icon'] ) {
$html = self::remove_site_icon( $html );
}
if ( $s['opt_home_next'] ) {
// rel="next"/"prev" pointing at the homepage's own pages: /page/2/ right after the domain.
$html = self::replace( '#]*\brel\s*=\s*["\']?(?:next|prev)\b)(?=[^>]*\bhref\s*=\s*["\'](?:(?:https?:)?//[^/"\']+)?/page/\d+/?["\'])[^>]*>\s*#i', '', $html );
}
// --- Accessibility -----------------------------------------------
if ( $s['a11y_main'] ) {
$html = self::add_main_landmark( $html, (string) $s['a11y_main_selector'] );
}
// --- Hide WP ---------------------------------------------------
if ( $s['hide_generator'] ) {
$html = self::replace( '#]*name=["\']generator["\'][^>]*>\s*#i', '', $html );
}
if ( $s['hide_wp_links'] ) {
$html = self::replace(
array(
'#]+rel=["\'](?:EditURI|wlwmanifest|pingback|shortlink|https://api\.w\.org/)["\'][^>]*>\s*#i',
'#]+type=["\'](?:application/json\+oembed|text/xml\+oembed)["\'][^>]*>\s*#i',
'#]+href=["\'][^"\']*/wp-json/[^"\']*["\'][^>]*>\s*#i',
),
'',
$html
);
}
// --- Forms -----------------------------------------------------
if ( $s['forms_remove_comments'] ) {
$html = self::replace( '#
\s*#is', '', $html );
$html = self::replace( '#]*\bid=(["\'])reply-title\1.*?
\s*#is', '', $html );
}
if ( $s['forms_endpoint_on'] && $s['forms_endpoint'] ) {
$html = self::redirect_forms( $html, $s['forms_endpoint'] );
}
// --- Resource hints ----------------------------------------------
$origins = self::hint_origins();
if ( $origins ) {
$html = self::resource_hints( $html, $origins );
}
// --- Search ----------------------------------------------------
if ( $s['search_enabled'] ) {
$html = CFSP_Search::inject( $html );
}
// --- Comments, custom rules, minify (last) ---------------------
if ( $s['opt_html_comments'] ) {
$html = self::outside_raw_blocks(
$html,
function ( $chunk ) {
return self::replace( '#[ \t]*(\r?\n)?#', '', $chunk );
}
);
}
$html = self::custom_rules( $html );
if ( $s['opt_minify_html'] ) {
// Line breaks just inside )#is',
function ( $m ) {
return $m[1] . self::replace( '#\s+#', ' ', $m[2] ) . $m[3];
},
$html
);
$html = self::outside_raw_blocks(
$html,
function ( $chunk ) {
return self::replace( '#\s{2,}|[\r\n\t]+#', ' ', $chunk );
}
);
}
return is_string( $html ) ? $html : '';
}
/** Origins that should get preconnect + dns-prefetch hints, e.g. "https://cdn.example.com". */
public static function hint_origins() {
$origins = array();
$lines = CFSP_Settings::lines( 'hints_origins' );
if ( CFSP_Settings::get( 'hints_media' ) && CFSP_Media::rewrite_enabled() ) {
$lines[] = CFSP_Settings::get( 'r2_public_url' );
}
foreach ( $lines as $line ) {
$url = preg_match( '#^(https?:)?//#i', $line ) ? $line : 'https://' . $line;
$url = 0 === strpos( $url, '//' ) ? 'https:' . $url : $url;
$parts = wp_parse_url( $url );
if ( ! empty( $parts['host'] ) ) {
$origin = strtolower( $parts['scheme'] . '://' . $parts['host'] . ( isset( $parts['port'] ) ? ':' . $parts['port'] : '' ) );
$origins[ $origin ] = true;
}
}
return array_keys( $origins );
}
/**
* Make sure every origin has both a preconnect and a dns-prefetch hint.
* A missing dns-prefetch goes right after an existing preconnect, in the same quote style
* (so it sits next to WordPress' own line); if neither exists, both go at the top of .
*/
public static function resource_hints( $html, array $origins ) {
if ( ! preg_match( '#]*>#i', $html, $head, PREG_OFFSET_CAPTURE ) ) {
return $html;
}
preg_match_all( '#]*>#i', $html, $links );
foreach ( $origins as $origin ) {
$host = (string) wp_parse_url( $origin, PHP_URL_HOST );
$pre_tag = null;
$has_pre = false;
$has_dns = false;
foreach ( $links[0] as $tag ) {
if ( ! preg_match( '#\brel\s*=\s*(["\'])(.*?)\1#i', $tag, $rel ) || ! preg_match( '#\bhref\s*=\s*(["\'])(.*?)\1#i', $tag, $href ) ) {
continue;
}
if ( strtolower( (string) wp_parse_url( ( 0 === strpos( $href[2], '//' ) ? 'https:' : '' ) . $href[2], PHP_URL_HOST ) ) !== $host ) {
continue;
}
$rels = preg_split( '/\s+/', strtolower( $rel[2] ) );
if ( in_array( 'preconnect', $rels, true ) ) {
$has_pre = true;
if ( null === $pre_tag && false === stripos( $tag, 'crossorigin' ) ) {
$pre_tag = $tag;
}
}
if ( in_array( 'dns-prefetch', $rels, true ) ) {
$has_dns = true;
}
}
if ( $has_pre && $has_dns ) {
continue;
}
if ( $pre_tag && ! $has_dns ) {
// Copy the existing preconnect line, so quotes and the " />" ending match it.
$dns = preg_replace( '#(\brel\s*=\s*["\'])preconnect(["\'])#i', '${1}dns-prefetch${2}', $pre_tag );
$pos = strpos( $html, $pre_tag ) + strlen( $pre_tag );
$html = substr( $html, 0, $pos ) . ' ' . $dns . substr( $html, $pos );
continue;
}
$add = '';
if ( ! $has_pre ) {
$add .= ' ';
}
if ( ! $has_dns ) {
$add .= ' ';
}
// Right after , or at the start of .
if ( preg_match( '#]*charset[^>]*>\s*#i', $html, $charset, PREG_OFFSET_CAPTURE ) ) {
$pos = $charset[0][1] + strlen( $charset[0][0] );
} else {
preg_match( '#]*>\s*#i', $html, $h, PREG_OFFSET_CAPTURE );
$pos = $h[0][1] + strlen( $h[0][0] );
}
$html = substr( $html, 0, $pos ) . $add . substr( $html, $pos );
}
return $html;
}
/**
* Give the page a main landmark when it has none: role="main" on the first element that matches
* one of the selectors (#id, .class, tag, tag#id or tag.class, comma-separated, tried in order).
*/
public static function add_main_landmark( $html, $selectors ) {
if ( preg_match( '#]*\bid\s*=\s*["\']?' . $name . '["\'\s>])'
: '(?=[^>]*\bclass\s*=\s*["\'][^"\']*(?]*\brole\s*=)(?=[^>]*>)#i';
$body = stripos( $html, ',
* and pointing at the site
* icon's image files. Favicon tags a theme adds for other files are kept.
*/
private static function remove_site_icon( $html ) {
$files = array();
foreach ( array( 32, 180, 192, 270, 512 ) as $size ) {
$url = get_site_icon_url( $size );
if ( $url ) {
$files[ wp_basename( (string) wp_parse_url( $url, PHP_URL_PATH ) ) ] = true;
}
}
if ( ! $files ) {
return $html;
}
$names = implode( '|', array_map( 'preg_quote', array_keys( $files ), array_fill( 0, count( $files ), '#' ) ) );
$html = self::replace( '#]*\brel\s*=\s*["\']?(?:icon|apple-touch-icon)\b)(?=[^>]*\bhref\s*=\s*["\'][^"\']*/(?:' . $names . ')["\'])[^>]*>\s*#i', '', $html );
return self::replace( '#]*\bname\s*=\s*["\']?msapplication-TileImage\b)(?=[^>]*\bcontent\s*=\s*["\'][^"\']*/(?:' . $names . ')["\'])[^>]*>\s*#i', '', $html );
}
/** Point POST forms at an external endpoint (Formspree, Getform, a Worker, …). */
private static function redirect_forms( $html, $endpoint ) {
return self::replace_callback(
'#