Skip to content

Commit 15e51bc

Browse files
authored
Merge pull request #2144 from b1ink0/add/speculative-auth-cache-notice
Add warning notice to Speculative Loading setting for authenticated users when persistent object cache is not present
2 parents 3d88f7b + 639bd9d commit 15e51bc

File tree

2 files changed

+97
-8
lines changed

2 files changed

+97
-8
lines changed

plugins/speculation-rules/settings.php

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ function plsr_get_authentication_labels(): array {
5656
);
5757
}
5858

59+
/**
60+
* Returns translated description strings for settings fields.
61+
*
62+
* @since n.e.x.t
63+
* @access private
64+
*
65+
* @param 'mode'|'eagerness'|'authentication' $field The field name to get description for.
66+
* @return string The translated description string.
67+
*/
68+
function plsr_get_field_description( string $field ): string {
69+
$descriptions = array(
70+
'mode' => __( 'Prerendering will lead to faster load times than prefetching. However, in case of interactive content, prefetching may be a safer choice.', 'speculation-rules' ),
71+
'eagerness' => __( 'The eagerness setting defines the heuristics based on which the loading is triggered. "Eager" will have the minimum delay to start speculative loads, "Conservative" increases the chance that only URLs the user actually navigates to are loaded.', 'speculation-rules' ),
72+
'authentication' => sprintf(
73+
/* translators: %s: URL to persistent object cache documentation */
74+
__( 'Only unauthenticated pages are typically served from cache. So in order to reduce load on the server, speculative loading is not enabled by default for logged-in users. If your server can handle the additional load, you can opt in to speculative loading for all logged-in users or just administrator users only. For optimal performance, regardless of the user authentication status but <em>especially</em> when logged-in, ensure you have a <a href="%s" target="_blank">persistent object cache</a> configured. This only applies to pages on the frontend; admin screens remain excluded.', 'speculation-rules' ),
75+
'https://developer.wordpress.org/advanced-administration/performance/optimization/#object-caching'
76+
),
77+
);
78+
return $descriptions[ $field ] ?? '';
79+
}
80+
5981
/**
6082
* Returns the default setting value for Speculative Loading configuration.
6183
*
@@ -153,17 +175,17 @@ function plsr_register_setting(): void {
153175
'type' => 'object',
154176
'properties' => array(
155177
'mode' => array(
156-
'description' => __( 'Whether to prefetch or prerender URLs.', 'speculation-rules' ),
178+
'description' => wp_strip_all_tags( plsr_get_field_description( 'mode' ) ),
157179
'type' => 'string',
158180
'enum' => array_keys( plsr_get_mode_labels() ),
159181
),
160182
'eagerness' => array(
161-
'description' => __( 'The eagerness setting defines the heuristics based on which the loading is triggered. "Eager" will have the minimum delay to start speculative loads, "Conservative" increases the chance that only URLs the user actually navigates to are loaded.', 'speculation-rules' ),
183+
'description' => wp_strip_all_tags( plsr_get_field_description( 'eagerness' ) ),
162184
'type' => 'string',
163185
'enum' => array_keys( plsr_get_eagerness_labels() ),
164186
),
165187
'authentication' => array(
166-
'description' => __( 'Only unauthenticated pages are typically served from cache. So in order to reduce load on the server, speculative loading is not enabled by default for logged-in users. If your server can handle the additional load, you can opt in to speculative loading for all logged-in users or just administrator users only. This only applies to pages on frontend; admin screens remain excluded.', 'speculation-rules' ),
188+
'description' => wp_strip_all_tags( plsr_get_field_description( 'authentication' ) ),
167189
'type' => 'string',
168190
'enum' => array_keys( plsr_get_authentication_labels() ),
169191
),
@@ -203,15 +225,15 @@ static function (): void {
203225
$fields = array(
204226
'mode' => array(
205227
'title' => __( 'Speculation Mode', 'speculation-rules' ),
206-
'description' => __( 'Prerendering will lead to faster load times than prefetching. However, in case of interactive content, prefetching may be a safer choice.', 'speculation-rules' ),
228+
'description' => plsr_get_field_description( 'mode' ),
207229
),
208230
'eagerness' => array(
209231
'title' => __( 'Eagerness', 'speculation-rules' ),
210-
'description' => __( 'The eagerness setting defines the heuristics based on which the loading is triggered. "Eager" will have the minimum delay to start speculative loads, "Conservative" increases the chance that only URLs the user actually navigates to are loaded.', 'speculation-rules' ),
232+
'description' => plsr_get_field_description( 'eagerness' ),
211233
),
212234
'authentication' => array(
213235
'title' => __( 'User Authentication Status', 'speculation-rules' ),
214-
'description' => __( 'Only unauthenticated pages are typically served from cache. So in order to reduce load on the server, speculative loading is not enabled by default for logged-in users. If your server can handle the additional load, you can opt in to speculative loading for all logged-in users or just administrator users only. This only applies to pages on frontend; admin screens remain excluded.', 'speculation-rules' ),
236+
'description' => plsr_get_field_description( 'authentication' ),
215237
),
216238
);
217239
foreach ( $fields as $slug => $args ) {
@@ -264,7 +286,7 @@ function plsr_render_settings_field( array $args ): void {
264286

265287
$value = $option[ $args['field'] ];
266288
?>
267-
<fieldset>
289+
<fieldset id="<?php echo esc_attr( 'plsr-' . $args['field'] . '-setting' ); ?>">
268290
<legend class="screen-reader-text"><?php echo esc_html( $args['title'] ); ?></legend>
269291
<?php foreach ( $choices as $slug => $label ) : ?>
270292
<p>
@@ -280,8 +302,61 @@ function plsr_render_settings_field( array $args ): void {
280302
</p>
281303
<?php endforeach; ?>
282304

305+
<?php if ( 'authentication' === $args['field'] && ! wp_using_ext_object_cache() ) : ?>
306+
<div id="plsr-auth-notice" class="notice <?php echo esc_attr( 'logged_out' !== $value ? 'notice-warning' : 'notice-info' ); ?> inline">
307+
<p>
308+
<?php
309+
echo wp_kses(
310+
sprintf(
311+
/* translators: %s: URL to persistent object cache documentation */
312+
__( 'Enabling speculative loading for authenticated users may significantly increase the server load. Consider setting up a <a href="%s" target="_blank">persistent object cache</a> before enabling this feature for logged-in users.', 'speculation-rules' ),
313+
'https://developer.wordpress.org/advanced-administration/performance/optimization/#object-caching'
314+
),
315+
array(
316+
'a' => array(
317+
'href' => array(),
318+
'target' => array(),
319+
),
320+
)
321+
);
322+
?>
323+
</p>
324+
</div>
325+
<?php
326+
// phpcs:ignore Squiz.PHP.Heredoc.NotAllowed -- Part of the PCP ruleset. Appealed in <https://github.com/WordPress/plugin-check/issues/792#issuecomment-3214985527>.
327+
$js = <<<'JS'
328+
const authOptions = document.getElementById( 'plsr-authentication-setting' );
329+
const noticeDiv = document.getElementById( 'plsr-auth-notice' );
330+
if ( authOptions && noticeDiv ) {
331+
authOptions.addEventListener( 'change', ( /** @type {Event} */ event ) => {
332+
const target = event.target;
333+
if ( ! ( target instanceof HTMLInputElement && 'radio' === target.type ) ) {
334+
return;
335+
}
336+
const isLoggedOut = ( target.value === 'logged_out' );
337+
noticeDiv.classList.toggle( 'notice-info', isLoggedOut );
338+
noticeDiv.classList.toggle( 'notice-warning', ! isLoggedOut );
339+
} );
340+
}
341+
JS;
342+
// 👆 This 'JS;' line can only be indented two tabs when minimum PHP version is increased to 7.3+.
343+
wp_print_inline_script_tag( $js, array( 'type' => 'module' ) );
344+
?>
345+
<?php endif; ?>
346+
283347
<p class="description" style="max-width: 800px;">
284-
<?php echo esc_html( $args['description'] ); ?>
348+
<?php
349+
echo wp_kses(
350+
$args['description'],
351+
array(
352+
'a' => array(
353+
'href' => array(),
354+
'target' => array(),
355+
),
356+
'em' => array(),
357+
)
358+
);
359+
?>
285360
</p>
286361
</fieldset>
287362
<?php

plugins/speculation-rules/tests/test-speculation-rules-settings.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Test_Speculation_Rules_Settings extends WP_UnitTestCase {
1313
* @covers ::plsr_get_eagerness_labels
1414
* @covers ::plsr_get_authentication_labels
1515
* @covers ::plsr_get_setting_default
16+
* @covers ::plsr_get_field_description
1617
*/
1718
public function test_plsr_register_setting(): void {
1819
unregister_setting( 'reading', 'plsr_speculation_rules' );
@@ -22,6 +23,13 @@ public function test_plsr_register_setting(): void {
2223
plsr_register_setting();
2324
$settings = get_registered_settings();
2425
$this->assertArrayHasKey( 'plsr_speculation_rules', $settings );
26+
foreach ( array( 'mode', 'eagerness', 'authentication' ) as $key ) {
27+
$this->assertTrue( isset( $settings['plsr_speculation_rules']['show_in_rest']['schema']['properties'][ $key ]['description'] ) );
28+
$description = $settings['plsr_speculation_rules']['show_in_rest']['schema']['properties'][ $key ]['description'];
29+
$this->assertIsString( $description );
30+
$this->assertGreaterThan( 0, strlen( $description ) );
31+
$this->assertStringNotContainsString( '<', $description );
32+
}
2533

2634
$settings = plsr_get_setting_default();
2735
$this->assertArrayHasKey( 'mode', $settings );
@@ -31,6 +39,12 @@ public function test_plsr_register_setting(): void {
3139
// Test default settings applied correctly.
3240
$default_settings = plsr_get_setting_default();
3341
$this->assertEquals( $default_settings, get_option( 'plsr_speculation_rules' ) );
42+
43+
foreach ( array( 'mode', 'eagerness', 'authentication' ) as $key ) {
44+
$description = plsr_get_field_description( $key );
45+
$this->assertGreaterThan( 0, strlen( $description ) );
46+
}
47+
$this->assertSame( '', plsr_get_field_description( 'bogus' ) );
3448
}
3549

3650
/**

0 commit comments

Comments
 (0)