Skip to content

Commit 4c8af79

Browse files
committed
Set cookie via header
1 parent 1786774 commit 4c8af79

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

includes/Cloudflare/CloudflareFeaturesManager.php

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

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

5+
use NewfoldLabs\WP\Module\Performance\Fonts\FontSettings;
56
use NewfoldLabs\WP\Module\Performance\Images\ImageSettings;
67
use WP_Forge\WP_Htaccess_Manager\htaccess;
78

@@ -50,6 +51,7 @@ public function on_fonts_optimization_change( $old_value, $new_value ) {
5051
public function on_site_capabilities_change( $old_value, $new_value ) {
5152
if ( is_array( $new_value ) ) {
5253
ImageSettings::maybe_refresh_with_capabilities( $new_value );
54+
FontSettings::maybe_refresh_with_capabilities( $new_value );
5355
}
5456
}
5557

@@ -76,12 +78,19 @@ private function update_htaccess_header( $image_settings, $fonts_enabled ) {
7678

7779
if ( $mirage_enabled || $polish_enabled || $fonts_enabled_flag ) {
7880
$rules = array(
81+
'# BEGIN Newfold CF Optimization Header',
7982
'<IfModule mod_headers.c>',
80-
"\tHeader set X-NFD-CF-Optimization \"{$header_value}\" env=nfd_cf_opt",
83+
"\tHeader set Set-Cookie \"nfd-enable-google-font-replace=true; path=/; Max-Age=86400; HttpOnly\" env=nfd_cf_opt",
8184
'</IfModule>',
82-
'# Match static asset requests',
83-
'SetEnvIf Request_URI "\\.(jpe?g|png|gif|webp|woff2?|ttf|otf|eot|css)$" nfd_cf_opt',
84-
'SetEnvIf Request_URI "fonts.googleapis.com" nfd_cf_opt',
85+
'# Exclude admin and API paths',
86+
'SetEnvIf Request_URI "^/wp-admin/" no_nfd_cf',
87+
'SetEnvIf Request_URI "^/wp-json/" no_nfd_cf',
88+
'SetEnvIf Request_URI "^/xmlrpc.php" no_nfd_cf',
89+
'SetEnvIf Request_URI "^/wp-login.php" no_nfd_cf',
90+
'SetEnvIf Request_URI "^/admin-ajax.php" no_nfd_cf',
91+
'# Apply CF header on all non-admin, non-API requests',
92+
'SetEnvIf Request_URI ".*" nfd_cf_opt=!no_nfd_cf',
93+
'# END Newfold CF Optimization Header',
8594
);
8695
}
8796

includes/Fonts/FontSettings.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,33 @@ public function sanitize_settings( $settings ) {
111111
);
112112
}
113113

114+
/**
115+
* Refreshes legacy font settings based on Cloudflare capabilities.
116+
*
117+
* @param object|null $capabilities Optional capabilities object.
118+
*/
119+
public static function maybe_refresh_with_capabilities( $capabilities ) {
120+
$settings = get_option( self::SETTING_KEY, array() );
121+
122+
// If settings are missing or invalid, fall back to defaults.
123+
if ( empty( $settings ) || ! is_array( $settings ) ) {
124+
$settings = array(
125+
'cloudflare' => array(
126+
'fonts' => false,
127+
'last_updated' => time(),
128+
),
129+
);
130+
}
131+
132+
// Only update if fonts key is not set (i.e. legacy/no-toggle sites).
133+
if ( ! isset( $settings['cloudflare']['fonts'] ) && is_object( $capabilities ) ) {
134+
$settings['cloudflare']['fonts'] = (bool) $capabilities->get( 'hasCloudflareFonts' );
135+
$settings['cloudflare']['last_updated'] = time();
136+
137+
update_option( self::SETTING_KEY, $settings );
138+
}
139+
}
140+
114141
/**
115142
* Checks if Cloudflare font optimization is enabled.
116143
*

includes/Images/ImageSettings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ public function sanitize_settings( $settings ) {
230230
),
231231
);
232232
}
233+
233234
/**
234235
* Refreshes legacy settings with Cloudflare feature flags based on capabilities.
235236
*

0 commit comments

Comments
 (0)