Skip to content

Commit 33eee8e

Browse files
committed
Make notice persistent to eliminate layout shifts
1 parent e37a5cd commit 33eee8e

File tree

1 file changed

+57
-43
lines changed

1 file changed

+57
-43
lines changed

plugins/speculation-rules/settings.php

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -267,37 +267,17 @@ function plsr_render_settings_field( array $args ): void {
267267
}
268268

269269
$value = $option[ $args['field'] ];
270-
$has_object_cache = wp_using_ext_object_cache();
271270
$authenticated_options = array( 'logged_out_and_admins', 'any' );
272-
$show_warning = 'authentication' === $args['field'] && ! $has_object_cache && in_array( $value, $authenticated_options, true );
271+
$show_notice = 'authentication' === $args['field'] && ! wp_using_ext_object_cache();
272+
$show_warning = $show_notice && in_array( $value, $authenticated_options, true );
273273
?>
274-
275-
<?php if ( 'authentication' === $args['field'] && ! $has_object_cache ) : ?>
276-
<div id="plsr-auth-warning" class="notice notice-warning inline" <?php echo $show_warning ? '' : 'hidden'; ?>>
277-
<p>
278-
<strong><?php esc_html_e( 'Warning:', 'speculation-rules' ); ?></strong>
279-
<?php
280-
printf(
281-
/* translators: %s: URL to persistent object cache documentation */
282-
esc_html__( 'Enabling speculative loading for authenticated users without a persistent object cache may significantly increase server load. Consider setting up a %s before enabling this feature for logged-in users.', 'speculation-rules' ),
283-
sprintf(
284-
'<a href="%s" target="_blank">%s</a>',
285-
'https://developer.wordpress.org/advanced-administration/performance/optimization/#object-caching',
286-
esc_html__( 'persistent object cache', 'speculation-rules' )
287-
)
288-
);
289-
?>
290-
</p>
291-
</div>
292-
<?php endif; ?>
293-
294274
<fieldset>
295275
<legend class="screen-reader-text"><?php echo esc_html( $args['title'] ); ?></legend>
296276
<?php foreach ( $choices as $slug => $label ) : ?>
297277
<p>
298278
<label>
299279
<input
300-
class="plsr-auth-radio"
280+
<?php echo $show_notice ? 'class="plsr-auth-radio"' : ''; ?>
301281
name="<?php echo esc_attr( "plsr_speculation_rules[{$args['field']}]" ); ?>"
302282
type="radio"
303283
value="<?php echo esc_attr( $slug ); ?>"
@@ -321,28 +301,62 @@ class="plsr-auth-radio"
321301
);
322302
?>
323303
</p>
304+
305+
<?php if ( $show_notice ) : ?>
306+
<div id="plsr-auth-notice" class="notice <?php echo $show_warning ? 'notice-warning' : 'notice-info'; ?> inline">
307+
<p>
308+
<strong id="plsr-notice-label" <?php echo $show_warning ? '' : 'hidden'; ?>><?php esc_html_e( 'Warning: ', 'speculation-rules' ); ?></strong>
309+
<?php
310+
echo wp_kses(
311+
sprintf(
312+
/* translators: %s: URL to persistent object cache documentation */
313+
__( 'Enabling speculative loading for authenticated users without a persistent object cache may significantly increase 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' ),
314+
'https://developer.wordpress.org/advanced-administration/performance/optimization/#object-caching'
315+
),
316+
array(
317+
'a' => array(
318+
'href' => array(),
319+
'target' => array(),
320+
),
321+
)
322+
);
323+
?>
324+
</p>
325+
</div>
326+
<?php endif; ?>
324327
</fieldset>
325-
326-
<?php if ( 'authentication' === $args['field'] && ! $has_object_cache ) : ?>
327-
<script>
328-
( function () {
329-
const authRadios = document.querySelectorAll( '.plsr-auth-radio' );
330-
const warningDiv = document.getElementById( 'plsr-auth-warning' );
331-
const authenticatedOptions = <?php echo wp_json_encode( $authenticated_options, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ); ?>;
332-
333-
if ( ! authRadios.length || ! warningDiv ) {
334-
return;
335-
}
336-
337-
authRadios.forEach( function ( radio ) {
338-
radio.addEventListener( 'change', function ( e ) {
339-
warningDiv.hidden = ! authenticatedOptions.includes( e.target.value );
340-
} );
341-
} );
342-
} )();
343-
</script>
344-
<?php endif; ?>
328+
345329
<?php
330+
if ( $show_notice ) {
331+
wp_print_inline_script_tag(
332+
sprintf(
333+
'const authRadios = document.querySelectorAll( ".plsr-auth-radio" );
334+
const noticeDiv = document.getElementById( "plsr-auth-notice" );
335+
const noticeLabel = document.getElementById( "plsr-notice-label" );
336+
const authenticatedOptions = %s;
337+
338+
if ( authRadios.length && noticeDiv && noticeLabel ) {
339+
authRadios.forEach( function ( radio ) {
340+
radio.addEventListener( "change", function ( e ) {
341+
const isAuthOption = authenticatedOptions.includes( e.target.value );
342+
343+
if ( isAuthOption ) {
344+
noticeDiv.classList.remove( "notice-info" );
345+
noticeDiv.classList.add( "notice-warning" );
346+
noticeLabel.hidden = false;
347+
} else {
348+
noticeDiv.classList.remove( "notice-warning" );
349+
noticeDiv.classList.add( "notice-info" );
350+
noticeLabel.hidden = true;
351+
}
352+
} );
353+
} );
354+
}',
355+
wp_json_encode( $authenticated_options, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES )
356+
),
357+
array( 'type' => 'module' )
358+
);
359+
}
346360
}
347361

348362
/**

0 commit comments

Comments
 (0)