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 #7280: Preload Fonts - Enable feature automatically for specific cases #7319

Open
wants to merge 18 commits into
base: feature/3.19
Choose a base branch
from
Open
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
16 changes: 0 additions & 16 deletions inc/Engine/Admin/Settings/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -2305,20 +2305,4 @@ 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: 1 addition & 17 deletions inc/Engine/Admin/Settings/Subscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ 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 ],
[ 'maybe_enable_auto_preload_fonts', 9 ],
],
'wp_rocket_upgrade' => [ 'enable_separate_cache_files_mobile', 9, 2 ],
'admin_notices' => 'display_update_notice',
];

Expand Down Expand Up @@ -288,19 +285,6 @@ 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
49 changes: 49 additions & 0 deletions inc/Engine/Media/PreloadFonts/Admin/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
namespace WP_Rocket\Engine\Media\PreloadFonts\Admin;

use WP_Rocket\Admin\Options_Data;
use WP_Rocket\Admin\Options as Options_API;

class Settings {
/**
* WP Rocket options instance.
*
* @var Options_Data
*/
private $options;


/**
* WP Rocket Options API Instance.
*
* @var Options_API
*/
private $options_api;

/**
* Creates an instance of the class.
*
* @param Options_Data $option_data WP Rocket Options instance.
* @param Options_API $options_api WP Rocket Options API instance.
*/
public function __construct( Options_Data $option_data, Options_API $options_api ) {
$this->options = $option_data;
$this->options_api = $options_api;
}

/**
* 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 {
if ( ! empty( $this->options->get( 'preload_fonts', [] ) ) ) {
$this->options->set( 'auto_preload_fonts', true );
$this->options_api->set( 'auto_preload_fonts', true );
}
}
}
58 changes: 58 additions & 0 deletions inc/Engine/Media/PreloadFonts/Admin/Subscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace WP_Rocket\Engine\Media\PreloadFonts\Admin;

use WP_Rocket\Event_Management\Subscriber_Interface;
use WP_Rocket\Engine\Media\PreloadFonts\Admin\Settings;

/**
* Preload Fonts admin controller
*
* @since 3.19
*/
class Subscriber implements Subscriber_Interface {
/**
* Controller instance
*
* @var Settings
*/
private $controller;

/**
* Creates an instance of the object.
*
* @param Settings $controller Controller instance.
*/
public function __construct( Settings $controller ) {
$this->controller = $controller;
}

/**
* Returns an array of events this subscriber listens to
*
* @return array[]
*/
public static function get_subscribed_events(): array {
return [
'wp_rocket_upgrade' => [ 'maybe_enable_auto_preload_fonts', 9, 2 ],
];
}

/**
* 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.
*
* @param string $new_version New plugin version.
* @param string $old_version Previous plugin version.
*
* @return void
*/
public function maybe_enable_auto_preload_fonts( $new_version, $old_version ): void {

Check warning on line 52 in inc/Engine/Media/PreloadFonts/Admin/Subscriber.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/PreloadFonts/Admin/Subscriber.php#L52

Avoid unused parameters such as '$new_version'.
if ( version_compare( $old_version, '3.19', '<' ) ) {
return;
}
$this->controller->maybe_enable_auto_preload_fonts();
}
}
21 changes: 20 additions & 1 deletion inc/Engine/Media/PreloadFonts/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use WP_Rocket\Engine\Media\PreloadFonts\Context\Context;
use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Controller as FrontendController;
use WP_Rocket\Engine\Media\PreloadFonts\Frontend\Subscriber as FrontendSubscriber;
use WP_Rocket\Engine\Media\PreloadFonts\Admin\Subscriber as AdminSubscriber;
use WP_Rocket\Engine\Media\PreloadFonts\Admin\Settings as AdminSettings;

class ServiceProvider extends AbstractServiceProvider {
/**
Expand All @@ -29,6 +31,8 @@
'preload_fonts_frontend_subscriber',
'preload_fonts_front_controller',
'preload_fonts_factory',
'preload_fonts_admin_subscriber',
'preload_fonts_admin_settings',
];

/**
Expand All @@ -51,7 +55,8 @@
$this->getContainer()->addShared( 'preload_fonts_table', PreloadFontsTable::class );

$this->getContainer()->get( 'preload_fonts_table' );
$options = $this->getContainer()->get( 'options' );
$options = $this->getContainer()->get( 'options' );
$options_api = $this->getContainer()->get( 'options_api' );

Check warning on line 59 in inc/Engine/Media/PreloadFonts/ServiceProvider.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

inc/Engine/Media/PreloadFonts/ServiceProvider.php#L59

Avoid unused local variables such as '$options_api'.

$this->getContainer()->add( 'preload_fonts_query', PreloadFontsQuery::class );
$this->getContainer()->add( 'preload_fonts_context', Context::class )
Expand Down Expand Up @@ -90,5 +95,19 @@
$this->getContainer()->get( 'preload_fonts_context' ),
]
);

$this->getContainer()->addShared( 'preload_fonts_admin_settings', AdminSettings::class )
->addArguments(
[
$this->getContainer()->get( 'options' ),
$this->getContainer()->get( 'options_api' ),
]
);
$this->getContainer()->add( 'preload_fonts_admin_subscriber', AdminSubscriber::class )
->addArguments(
[
$this->getContainer()->get( 'preload_fonts_admin_settings' ),
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,6 @@
],
],
'expected' => $html_output_with_beacon_and_only_lrc_opt,

],
'shouldNotDuplicateBeaconOnAPage' => [
'config' => [
Expand Down
Loading