Skip to content

Commit 0a01618

Browse files
authored
Merge branch 'main' into enhance/PRESS7-456-Enable-Plan-Based-Link-Prefetch-Behavior
2 parents 13f0610 + c65d4ff commit 0a01618

File tree

16 files changed

+869
-74
lines changed

16 files changed

+869
-74
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
"wp-cli/wp-cli-bundle": "^2.8.1|^2.12",
4545
"wp-cli/i18n-command": "^2.6",
4646
"10up/wp_mock": "^0.5.0|^1.0.0",
47-
"johnpbloch/wordpress": "6.1.7",
47+
"johnpbloch/wordpress": "6.8.1",
4848
"lucatume/wp-browser": "*",
4949
"phpunit/phpcov": "*"
5050
},

composer.lock

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Performance\Cloudflare;
4+
5+
use NewfoldLabs\WP\Module\Performance\Fonts\FontSettings;
6+
use NewfoldLabs\WP\Module\Performance\Images\ImageSettings;
7+
use WP_Forge\WP_Htaccess_Manager\htaccess;
8+
9+
/**
10+
* Handles detection and tracking of Cloudflare Polish, Mirage, and Font Optimization.
11+
*/
12+
class CloudflareFeaturesManager {
13+
14+
private const MARKER = 'Newfold CF Optimization Header';
15+
16+
/**
17+
* Constructor to register hooks for settings changes.
18+
*/
19+
public function __construct() {
20+
add_action( 'update_option_nfd_image_optimization', array( $this, 'on_image_optimization_change' ), 10, 2 );
21+
add_action( 'add_option_nfd_image_optimization', array( $this, 'on_image_optimization_change' ), 10, 2 );
22+
add_action( 'update_option_nfd_fonts_optimization', array( $this, 'on_fonts_optimization_change' ), 10, 2 );
23+
add_action( 'add_option_nfd_fonts_optimization', array( $this, 'on_fonts_optimization_change' ), 10, 2 );
24+
add_action( 'set_transient_nfd_site_capabilities', array( $this, 'on_site_capabilities_change' ), 10, 2 );
25+
}
26+
27+
/**
28+
* Handles image optimization setting changes.
29+
*
30+
* @param array $old_value Previous value.
31+
* @param array $new_value New value.
32+
*/
33+
public function on_image_optimization_change( $old_value, $new_value ) {
34+
$this->update_htaccess_header( $new_value, get_option( 'nfd_fonts_optimization', false ) );
35+
}
36+
37+
/**
38+
* Handles font optimization setting changes.
39+
*
40+
* @param mixed $old_value Previous value.
41+
* @param mixed $new_value New value.
42+
*/
43+
public function on_fonts_optimization_change( $old_value, $new_value ) {
44+
$this->update_htaccess_header( get_option( 'nfd_image_optimization', array() ), $new_value );
45+
}
46+
47+
/**
48+
* Callback for when the `nfd_site_capabilities` transient is set.
49+
*
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.
54+
*/
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 );
59+
}
60+
}
61+
62+
/**
63+
* Updates the .htaccess header based on current optimization settings.
64+
*
65+
* @param array $image_settings Array of image optimization settings.
66+
* @param mixed $fonts_enabled Whether font optimization is enabled.
67+
*/
68+
private function update_htaccess_header( $image_settings, $fonts_enabled ) {
69+
$images_cloudflare = isset( $image_settings['cloudflare'] ) ? $image_settings['cloudflare'] : array();
70+
$fonts_cloudflare = isset( $fonts_enabled['cloudflare'] ) ? $fonts_enabled['cloudflare'] : array();
71+
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'] );
75+
76+
$mirage_hash = $mirage_enabled ? substr( sha1( 'mirage' ), 0, 8 ) : '';
77+
$polish_hash = $polish_enabled ? substr( sha1( 'polish' ), 0, 8 ) : '';
78+
$fonts_hash = $fonts_enabled_flag ? substr( sha1( 'fonts' ), 0, 8 ) : '';
79+
80+
$header_value = "{$mirage_hash}-{$polish_hash}-{$fonts_hash}";
81+
$rules = array();
82+
83+
if ( $mirage_enabled || $polish_enabled || $fonts_enabled_flag ) {
84+
$rules = array(
85+
'<IfModule mod_rewrite.c>',
86+
"\tRewriteEngine On",
87+
"\t# don’t flag any admin or API endpoints",
88+
"\tRewriteCond %{REQUEST_URI} !/wp-admin/ [NC]",
89+
"\tRewriteCond %{REQUEST_URI} !/wp-login\\.php [NC]",
90+
"\tRewriteCond %{REQUEST_URI} !/wp-json/ [NC]",
91+
"\tRewriteCond %{REQUEST_URI} !/xmlrpc\\.php [NC]",
92+
"\tRewriteCond %{REQUEST_URI} !/admin-ajax\\.php [NC]",
93+
"\t# if we passed all those, set our CF_OPT flag",
94+
"\tRewriteRule .* - [E=CF_OPT:1]",
95+
'</IfModule>',
96+
'<IfModule mod_headers.c>',
97+
"\tHeader set Set-Cookie \"nfd-enable-cf-opt={$header_value}; path=/; Max-Age=86400; HttpOnly\" env=CF_OPT",
98+
'</IfModule>',
99+
);
100+
}
101+
102+
$htaccess = new htaccess( self::MARKER );
103+
if ( empty( $rules ) ) {
104+
$htaccess->removeContent();
105+
} else {
106+
$htaccess->addContent( $rules );
107+
}
108+
}
109+
}

includes/Fonts/FontManager.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Performance\Fonts;
4+
5+
use NewfoldLabs\WP\ModuleLoader\Container;
6+
use NewfoldLabs\WP\Module\Performance\Fonts\FontSettings;
7+
8+
/**
9+
* Manages the initialization of font optimization settings and listeners.
10+
*/
11+
class FontManager {
12+
13+
/**
14+
* Constructor to initialize the FontManager.
15+
*
16+
* Registers settings and conditionally initializes related services.
17+
*
18+
* @param Container $container Dependency injection container.
19+
*/
20+
public function __construct( Container $container ) {
21+
$this->initialize_settings( $container );
22+
}
23+
24+
/**
25+
* Initializes the FontSettings class to register settings.
26+
*
27+
* @param Container $container Dependency injection container.
28+
*/
29+
private function initialize_settings( Container $container ) {
30+
new FontSettings( $container );
31+
}
32+
}

0 commit comments

Comments
 (0)