settings = Plugin::get_settings( 'performance' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); Utils::keep_hidden_settings( 'performance', $this->get_hidden_keys() ); if ( empty( $this->settings['page_head_cleanup'] ) ) { return; } if ( ! empty( $this->settings['page_head_global_styles'] ) && $this->global_styles_available() ) { remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 ); } if ( ! empty( $this->settings['page_head_block_library'] ) || ! empty( $this->settings['page_head_dashicons'] ) ) { add_action( 'wp_print_styles', [ $this, 'wp_print_styles' ], 100 ); } if ( ! empty( $this->settings['page_head_jquery_migrate'] ) ) { add_action( 'wp_default_scripts', [ $this, 'wp_default_scripts' ] ); if ( did_action( 'wp_default_scripts' ) ) { $this->wp_default_scripts( wp_scripts() ); } } if ( ! empty( $this->settings['page_head_comment_reply'] ) ) { add_action( 'wp_print_scripts', [ $this, 'wp_print_scripts' ], 100 ); } } /** * Checks if the global styles removal can work on this site (WordPress 5.8 and newer). * * @return bool True when the switch applies. */ private function global_styles_available(): bool { return Utils::wp_at_least( '5.8' ); } /** * Checks if the block library removal can work on this site (classic themes only). * * @return bool True when the switch applies. */ private function block_library_available(): bool { return ! Utils::is_block_theme(); } /** * Lists the settings keys that are hidden on this site, so a save does not wipe them. * * @return array The hidden setting keys. */ private function get_hidden_keys(): array { $hidden_keys = []; if ( ! $this->global_styles_available() ) { $hidden_keys[] = 'page_head_global_styles'; } if ( ! $this->block_library_available() ) { $hidden_keys[] = 'page_head_block_library'; } return $hidden_keys; } /** * Wraps a breakage warning in the plugin's warning box. * * @param string $text The warning, already translated. * * @return string The warning markup. */ private function warning( string $text ): string { return '' . esc_html( $text ) . ''; } /** * Builds one child switch. * * @param string $key The setting key, e.g. page_head_dashicons. * @param string $label The translated label. * @param string $description The translated description, may include warning markup. * * @return array The field definition. */ private function child_field( string $key, string $label, string $description ): array { return [ 'type' => 'switch', 'id' => str_replace( '_', '-', $key ), 'name' => 'adminease[performance][' . $key . ']', 'value' => $this->settings[ $key ] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => $label, 'field_description' => $description, 'attributes' => [ 'data-parent' => 'page-head-cleanup', ], ]; } /** * Adds the Page Head cleanup switch and its smaller switches to the Performance tab. * * @param array $fields The settings fields keyed by tab. * * @return array The modified settings fields. */ public function adminease_settings_fields( array $fields ): array { $child_fields = []; if ( $this->global_styles_available() ) { $child_fields[] = $this->child_field( 'page_head_global_styles', __( 'Remove global styles', 'adminease' ), esc_html__( 'Stops WordPress from adding the theme colour and spacing styles to every page.', 'adminease' ) . ' ' . $this->warning( __( 'Warning: this can change how your site looks. On block themes (also called full site editing themes) most of the design comes from these styles, so the site can lose its colours, spacing and layout. Classic themes can lose colour classes used by the editor. Check your site after switching this on, and switch it off again if anything looks wrong.', 'adminease' ) ) ); } if ( $this->block_library_available() ) { $child_fields[] = $this->child_field( 'page_head_block_library', __( 'Remove block library styles', 'adminease' ), esc_html__( 'Stops WordPress from loading the standard block styles file on every page. Only shown for classic themes.', 'adminease' ) . ' ' . $this->warning( __( 'Warning: this can change how your site looks. Buttons, columns, galleries, quotes and other blocks can lose their styling if your theme does not style them itself. Check pages that use blocks after switching this on, and switch it off again if anything looks wrong.', 'adminease' ) ) ); } $child_fields[] = $this->child_field( 'page_head_dashicons', __( 'Remove Dashicons for visitors', 'adminease' ), esc_html__( 'Stops WordPress from loading its admin icon font for visitors who are not logged in.', 'adminease' ) . ' ' . $this->warning( __( 'Warning: some themes and plugins use these icons on the public site, for example in menus, search boxes, ratings and sliders. If they do, those icons will show as empty boxes or disappear for visitors. Check your site while logged out after switching this on, and switch it off again if any icons are missing.', 'adminease' ) ) ); $child_fields[] = $this->child_field( 'page_head_jquery_migrate', __( 'Remove jQuery Migrate', 'adminease' ), esc_html__( 'Stops WordPress from loading a small script that keeps very old jQuery code working. It is not loaded in the admin area or on the login page.', 'adminease' ) . ' ' . $this->warning( __( 'Warning: this can break parts of your site. Older themes and plugins that were written for old versions of jQuery can stop working, for example sliders, menus, popups and forms. The page itself may still look fine while a button or feature quietly stops working. Test every important feature after switching this on, and switch it off again if anything stops working.', 'adminease' ) ) ); $child_fields[] = $this->child_field( 'page_head_comment_reply', __( 'Load the comment reply script only where needed', 'adminease' ), esc_html__( 'Stops WordPress from loading the comment reply script on pages that have no comment form with replies, which is most pages.', 'adminease' ) ); $fields['performance']['fields'][] = [ 'type' => 'switch', 'id' => 'page-head-cleanup', 'name' => 'adminease[performance][page_head_cleanup]', 'value' => $this->settings['page_head_cleanup'] ?? '', 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Clean up the page head', 'adminease' ), 'description' => esc_html__( 'Stops WordPress from loading styles and scripts on your public pages that many sites never use, so pages load a little faster. Turn on the smaller options you want below.', 'adminease' ) . ' ' . $this->warning( __( 'Warning: several of these options can change how your site looks or break features that depend on what is removed. Switch them on one at a time, check your site after each one, and switch it off again if anything looks wrong.', 'adminease' ) ), 'child_fields' => $child_fields, ]; return $fields; } /** * Removes the block library styles and, for visitors who are not logged in, Dashicons. * * @return void */ public function wp_print_styles() { if ( is_admin() ) { return; } if ( ! empty( $this->settings['page_head_block_library'] ) && $this->block_library_available() ) { wp_dequeue_style( 'wp-block-library' ); wp_dequeue_style( 'wp-block-library-theme' ); } if ( ! empty( $this->settings['page_head_dashicons'] ) && ! is_user_logged_in() ) { wp_dequeue_style( 'dashicons' ); } } /** * Takes jQuery Migrate out of the scripts jQuery loads with, on the public site only. * * @param \WP_Scripts $scripts The script registry. * * @return void */ public function wp_default_scripts( $scripts ) { if ( is_admin() ) { return; } if ( isset( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) { return; } if ( ! $scripts instanceof \WP_Scripts || ! isset( $scripts->registered['jquery'] ) ) { return; } $jquery = $scripts->registered['jquery']; if ( ! empty( $jquery->deps ) ) { $jquery->deps = array_values( array_diff( $jquery->deps, [ 'jquery-migrate' ] ) ); } } /** * Removes the comment reply script from pages that cannot use it. * * @return void */ public function wp_print_scripts() { if ( is_admin() ) { return; } if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { return; } wp_dequeue_script( 'comment-reply' ); } }