Skip to content

Commit caa2485

Browse files
committed
Simplify updates further
1 parent 7ebd339 commit caa2485

File tree

6 files changed

+28
-58
lines changed

6 files changed

+28
-58
lines changed

includes/Cloudflare/CloudflareFeaturesManager.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ public function __construct() {
2121
add_action( 'add_option_nfd_image_optimization', array( $this, 'on_image_optimization_change' ), 10, 2 );
2222
add_action( 'update_option_nfd_fonts_optimization', array( $this, 'on_fonts_optimization_change' ), 10, 2 );
2323
add_action( 'add_option_nfd_fonts_optimization', array( $this, 'on_fonts_optimization_change' ), 10, 2 );
24-
add_action( 'update_option_nfd_site_capabilities', array( $this, 'on_site_capabilities_change' ), 10, 2 );
25-
add_action( 'add_option_nfd_site_capabilities', array( $this, 'on_site_capabilities_change' ), 10, 2 );
24+
add_action( 'set_transient_nfd_site_capabilities', array( $this, 'on_site_capabilities_change' ), 10, 2 );
2625
}
2726

2827
/**
@@ -46,15 +45,17 @@ public function on_fonts_optimization_change( $old_value, $new_value ) {
4645
}
4746

4847
/**
49-
* Handles site capabilities change and triggers a settings refresh.
48+
* Callback for when the `nfd_site_capabilities` transient is set.
5049
*
51-
* @param mixed $old_value Previous value.
52-
* @param mixed $new_value New value.
50+
* Triggers a refresh of image and font optimization settings based on updated site capabilities.
51+
*
52+
* @param mixed $value The value being set in the transient.
53+
* @param int $expiration The expiration time in seconds.
5354
*/
54-
public function on_site_capabilities_change( $old_value, $new_value ) {
55-
if ( is_array( $new_value ) ) {
56-
ImageSettings::maybe_refresh_with_capabilities( $new_value );
57-
FontSettings::maybe_refresh_with_capabilities( $new_value );
55+
public function on_site_capabilities_change( $value, $expiration ) {
56+
if ( is_array( $value ) ) {
57+
ImageSettings::maybe_refresh_with_capabilities( $value );
58+
FontSettings::maybe_refresh_with_capabilities( $value );
5859
}
5960
}
6061

@@ -68,9 +69,9 @@ private function update_htaccess_header( $image_settings, $fonts_enabled ) {
6869
$images_cloudflare = isset( $image_settings['cloudflare'] ) ? $image_settings['cloudflare'] : array();
6970
$fonts_cloudflare = isset( $fonts_enabled['cloudflare'] ) ? $fonts_enabled['cloudflare'] : array();
7071

71-
$mirage_enabled = ! empty( $images_cloudflare['mirage'] );
72-
$polish_enabled = ! empty( $images_cloudflare['polish'] );
73-
$fonts_enabled_flag = ! empty( $fonts_cloudflare['fonts'] );
72+
$mirage_enabled = ! empty( $images_cloudflare['mirage']['value'] );
73+
$polish_enabled = ! empty( $images_cloudflare['polish']['value'] );
74+
$fonts_enabled_flag = ! empty( $fonts_cloudflare['fonts']['value'] );
7475

7576
$mirage_hash = $mirage_enabled ? substr( sha1( 'mirage' ), 0, 8 ) : '';
7677
$polish_hash = $polish_enabled ? substr( sha1( 'polish' ), 0, 8 ) : '';

includes/Fonts/FontManager.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace NewfoldLabs\WP\Module\Performance\Fonts;
44

5-
use NewfoldLabs\WP\Module\Performance\Cloudflare\CloudflareFeaturesManager;
65
use NewfoldLabs\WP\ModuleLoader\Container;
76
use NewfoldLabs\WP\Module\Performance\Fonts\FontSettings;
8-
use NewfoldLabs\WP\Module\Performance\Permissions;
97

108
/**
119
* Manages the initialization of font optimization settings and listeners.
@@ -21,7 +19,6 @@ class FontManager {
2119
*/
2220
public function __construct( Container $container ) {
2321
$this->initialize_settings( $container );
24-
$this->maybe_initialize_cloudflare_fonts_handler();
2522
}
2623

2724
/**
@@ -32,13 +29,4 @@ public function __construct( Container $container ) {
3229
private function initialize_settings( Container $container ) {
3330
new FontSettings( $container );
3431
}
35-
36-
/**
37-
* Initializes Cloudflare-related font optimization handler if optimization is enabled.
38-
*/
39-
private function maybe_initialize_cloudflare_fonts_handler() {
40-
if ( Permissions::rest_is_authorized_admin() ) {
41-
new CloudflareFeaturesManager();
42-
}
43-
}
4432
}

includes/Fonts/FontSettings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ public static function maybe_refresh_with_capabilities( $capabilities ) {
144144
);
145145
}
146146

147-
if ( is_object( $capabilities ) ) {
148-
$has_fonts = (bool) $capabilities->get( 'hasCloudflareFonts' );
147+
if ( is_array( $capabilities ) ) {
148+
$has_fonts = (bool) $capabilities['hasCloudflareFonts'] ?? false;
149149

150150
if ( $settings['cloudflare']['fonts']['user_set'] ) {
151151
if ( $settings['cloudflare']['fonts']['value'] && ! $has_fonts ) {

includes/Images/ImageManager.php

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace NewfoldLabs\WP\Module\Performance\Images;
44

5-
use NewfoldLabs\WP\Module\Performance\Cloudflare\CloudflareFeaturesManager;
65
use NewfoldLabs\WP\ModuleLoader\Container;
76
use NewfoldLabs\WP\Module\Performance\Permissions;
87
use NewfoldLabs\WP\Module\Performance\Images\RestApi\RestApi;
@@ -47,8 +46,6 @@ private function initialize_services( Container $container ) {
4746
$this->maybe_initialize_marker();
4847
$this->maybe_initialize_image_rewrite_handler( $container );
4948
$this->maybe_initialize_image_limit_banner( $container );
50-
$this->maybe_initialize_cloudflare_polish( $container );
51-
$this->maybe_initialize_cloudflare_mirage( $container );
5249
}
5350

5451
/**
@@ -117,22 +114,4 @@ private function maybe_initialize_image_limit_banner( $container ) {
117114
new ImageLimitBanner( $container );
118115
}
119116
}
120-
121-
/**
122-
* Initializes CloudflarePolishHandler if the capability is available.
123-
*/
124-
private function maybe_initialize_cloudflare_polish() {
125-
if ( Permissions::rest_is_authorized_admin() && ImageSettings::is_optimization_enabled() ) {
126-
new CloudflareFeaturesManager();
127-
}
128-
}
129-
130-
/**
131-
* Initializes CloudflareMirageHandler if the capability is available.
132-
*/
133-
private function maybe_initialize_cloudflare_mirage() {
134-
if ( Permissions::rest_is_authorized_admin() && ImageSettings::is_optimization_enabled() ) {
135-
new CloudflareFeaturesManager();
136-
}
137-
}
138117
}

includes/Images/ImageSettings.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ private static function get_default_settings( $container = null ) {
5959
) {
6060
$capabilities = $container->get( 'capabilities' );
6161

62-
$default['cloudflare']['polish'] = (bool) $capabilities->get( 'hasCloudflarePolish' );
63-
$default['cloudflare']['mirage'] = (bool) $capabilities->get( 'hasCloudflareMirage' );
62+
$default['cloudflare']['polish']['value'] = (bool) $capabilities->get( 'hasCloudflarePolish' );
63+
$default['cloudflare']['mirage']['value'] = (bool) $capabilities->get( 'hasCloudflareMirage' );
6464
}
6565

6666
return $default;
@@ -170,7 +170,7 @@ private function register_settings() {
170170
'properties' => array(
171171
'value' => array(
172172
'type' => 'boolean',
173-
'default' => $this->default_settings['cloudflare']['polish'],
173+
'default' => $this->default_settings['cloudflare']['polish']['value'],
174174
),
175175
'user_set' => array(
176176
'type' => 'boolean',
@@ -185,7 +185,7 @@ private function register_settings() {
185185
'properties' => array(
186186
'value' => array(
187187
'type' => 'boolean',
188-
'default' => $this->default_settings['cloudflare']['mirage'],
188+
'default' => $this->default_settings['cloudflare']['mirage']['value'],
189189
),
190190
'user_set' => array(
191191
'type' => 'boolean',
@@ -246,14 +246,14 @@ public function sanitize_settings( $settings ) {
246246
),
247247
'cloudflare' => array(
248248
'polish' => array(
249-
'value' => isset( $settings['cloudflare']['polish'] )
250-
? (bool) $settings['cloudflare']['polish']
249+
'value' => isset( $settings['cloudflare']['polish']['value'] )
250+
? (bool) $settings['cloudflare']['polish']['value']
251251
: (bool) ( $existing_settings['cloudflare']['polish']['value'] ?? false ),
252252
'user_set' => array_key_exists( 'polish', $settings['cloudflare'] ),
253253
),
254254
'mirage' => array(
255-
'value' => isset( $settings['cloudflare']['mirage'] )
256-
? (bool) $settings['cloudflare']['mirage']
255+
'value' => isset( $settings['cloudflare']['mirage']['value'] )
256+
? (bool) $settings['cloudflare']['mirage']['value']
257257
: (bool) ( $existing_settings['cloudflare']['mirage']['value'] ?? false ),
258258
'user_set' => array_key_exists( 'mirage', $settings['cloudflare'] ),
259259
),
@@ -287,9 +287,9 @@ public static function maybe_refresh_with_capabilities( $capabilities ) {
287287
);
288288
}
289289

290-
if ( is_object( $capabilities ) ) {
291-
$has_polish = (bool) $capabilities->get( 'hasCloudflarePolish' );
292-
$has_mirage = (bool) $capabilities->get( 'hasCloudflareMirage' );
290+
if ( is_array( $capabilities ) ) {
291+
$has_polish = (bool) $capabilities['hasCloudflarePolish'] ?? false;
292+
$has_mirage = (bool) $capabilities['hasCloudflareMirage'] ?? false;
293293

294294
// Polish
295295
if ( $settings['cloudflare']['polish']['user_set'] ) {

includes/Performance.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use NewfoldLabs\WP\Module\Performance\LinkPrefetch\LinkPrefetch;
1111
use NewfoldLabs\WP\Module\Performance\Cache\Cache;
1212
use NewfoldLabs\WP\Module\Performance\Cache\ResponseHeaderManager;
13+
use NewfoldLabs\WP\Module\Performance\Cloudflare\CloudflareFeaturesManager;
1314
use NewfoldLabs\WP\Module\Performance\Fonts\FontManager;
1415
use NewfoldLabs\WP\Module\Performance\Skip404\Skip404;
1516
use NewfoldLabs\WP\Module\Performance\JetpackBoost\JetpackBoost;
@@ -65,6 +66,7 @@ public function __construct( Container $container ) {
6566
new Skip404( $container );
6667
new PerformanceWPCLI();
6768
new Constants( $container );
69+
new CloudflareFeaturesManager( $container );
6870
new ImageManager( $container );
6971
new FontManager( $container );
7072
new HealthChecks( $container );

0 commit comments

Comments
 (0)