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

Closes #7239: 3.19 - User interface #7303

Merged
merged 8 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions inc/Engine/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ private function media_section() {
$exclude_lazyload = $this->beacon->get_suggest( 'exclude_lazyload' );
$dimensions = $this->beacon->get_suggest( 'image_dimensions' );
$fonts = $this->beacon->get_suggest( 'host_fonts_locally' );
$fonts_preload = $this->beacon->get_suggest( 'fonts_preload' );

$this->settings->add_page_section(
'media',
Expand Down Expand Up @@ -936,12 +937,10 @@ private function media_section() {
'page' => 'media',
],
'font_optimization_section' => [
'title' => __( 'Fonts', 'rocket' ),
'type' => 'fields_container',
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Download and serve fonts directly from your server. Reduces connections to external servers and minimizes font shifts. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $fonts['url'] ) . '" data-beacon-article="' . esc_attr( $fonts['id'] ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
'help' => $fonts,
'page' => 'media',
'title' => __( 'Fonts', 'rocket' ),
'type' => 'fields_container',
'help' => $fonts,
'page' => 'media',
],
]
);
Expand Down Expand Up @@ -1041,9 +1040,21 @@ private function media_section() {
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'auto_preload_fonts' => [
'type' => 'checkbox',
'label' => __( 'Preload fonts', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Preload above-the-fold fonts to enhance layout stability and optimize text-based LCP elements. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $fonts_preload['url'] ) . '" data-beacon-article="' . esc_attr( $fonts_preload['id'] ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
'section' => 'font_optimization_section',
'page' => 'media',
'default' => 0,
'sanitize_callback' => 'sanitize_checkbox',
],
'host_fonts_locally' => [
'type' => 'checkbox',
'label' => __( 'Self-host Google Fonts', 'rocket' ),
// translators: %1$s = opening <a> tag, %2$s = closing </a> tag.
'description' => sprintf( __( 'Download and serve fonts directly from your server. Reduces connections to external servers and minimizes font shifts. %1$sMore info%2$s', 'rocket' ), '<a href="' . esc_url( $fonts['url'] ) . '" data-beacon-article="' . esc_attr( $fonts['id'] ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ),
'section' => 'font_optimization_section',
'page' => 'media',
'default' => 0,
Expand Down Expand Up @@ -2294,4 +2305,20 @@ public function display_update_notice() {
]
);
}

/**
* Enables the auto preload fonts option if the old preload fonts option is not empty.
*
* This function checks the value of the `rocket_preload_fonts` option.
* If it contains a non-empty value, it updates the `auto_preload_fonts` option to `true`.
* This is useful for ensuring that automatic font preloading is enabled based on legacy settings.
*
* @return void
*/
public function maybe_enable_auto_preload_fonts(): void {
$old_preload_fonts = $this->options->get( 'rocket_preload_fonts', [] );
if ( ! empty( $old_preload_fonts ) ) {
$this->options->set( 'auto_preload_fonts', true );
}
}
}
18 changes: 17 additions & 1 deletion inc/Engine/Admin/Settings/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ public static function get_subscribed_events() {
'rocket_after_settings_radio_options' => [ 'display_radio_options_sub_fields', 11 ],
'rocket_settings_tools_content' => 'display_mobile_cache_option',
'wp_ajax_rocket_enable_mobile_cache' => 'enable_mobile_cache',
'wp_rocket_upgrade' => [ 'enable_separate_cache_files_mobile', 9, 2 ],
'wp_rocket_upgrade' => [
[ 'enable_separate_cache_files_mobile', 9, 2 ],
[ 'maybe_enable_auto_preload_fonts', 9 ],
],
'admin_notices' => 'display_update_notice',
];

Expand Down Expand Up @@ -285,6 +288,19 @@ public function enable_separate_cache_files_mobile( $new_version, $old_version )
$this->page->enable_separate_cache_files_mobile();
}

/**
* Enables the auto preload fonts option if the old preload fonts option is not empty.
*
* This function checks the value of the `rocket_preload_fonts` option.
* If it contains a non-empty value, it updates the `auto_preload_fonts` option to `true`.
* This is useful for ensuring that automatic font preloading is enabled based on legacy settings.
*
* @return void
*/
public function maybe_enable_auto_preload_fonts(): void {
$this->page->maybe_enable_auto_preload_fonts();
}

/**
* Display the update notice.
*
Expand Down
8 changes: 7 additions & 1 deletion inc/Engine/Media/Fonts/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ class Settings {
/**
* Adds the host fonts locally option to WP Rocket options array
*
* @since 3.19 adds auto preload fonts
*
* @param array $options WP Rocket options array.
*
* @return array
*/
public function add_option( array $options ): array {
$options['host_fonts_locally'] = 0;
$options['auto_preload_fonts'] = 0;

return $options;
}

/**
* Sanitizes the option value when saving from the settings page
*
* @since 3.19 adds auto preload fonts
*
* @param array $input Array of sanitized values after being submitted by the form.
* @param AdminSettings $settings Settings class instance.
*
* @return array
*/
public function sanitize_option_value( array $input, AdminSettings $settings ): array {
$input['host_fonts_locally'] = $settings->sanitize_checkbox( $input, 'host_fonts_locally' );
$input['host_fonts_locally'] = $settings->sanitize_checkbox( $input, 'host_fonts_locally' );
$intput['auto_preload_fonts'] = $settings->sanitize_checkbox( $input, 'auto_preload_fonts' );

return $input;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/inc/admin/rocketFirstInstall.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
$integration[ 'image_dimensions' ] = 0;
$integration[ 'exclude_lazyload' ] = [];
$integration['host_fonts_locally'] = 0;
$integration['auto_preload_fonts'] = 0;

return [
'test_data' => [
Expand Down
Loading