Skip to content

Commit 6969eae

Browse files
committed
Add helper function to eliminate duplicate translation strings
1 parent 33eee8e commit 6969eae

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

plugins/speculation-rules/settings.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,30 @@ 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+
*
64+
* @param string $field The field name to get description for.
65+
* @param bool $strip_tags Whether to strip HTML tags from the description.
66+
* @return string The translated description string.
67+
*/
68+
function plsr_get_field_description( string $field, bool $strip_tags = false ): 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 when enabling authenticated user support, ensure you have a <a href="%s" target="_blank">persistent object cache</a> configured. This only applies to pages on frontend; admin screens remain excluded.', 'speculation-rules' ),
75+
'https://developer.wordpress.org/advanced-administration/performance/optimization/#object-caching'
76+
),
77+
);
78+
$description = $descriptions[ $field ] ?? '';
79+
80+
return $strip_tags ? wp_strip_all_tags( $description ) : $description;
81+
}
82+
5983
/**
6084
* Returns the default setting value for Speculative Loading configuration.
6185
*
@@ -153,17 +177,17 @@ function plsr_register_setting(): void {
153177
'type' => 'object',
154178
'properties' => array(
155179
'mode' => array(
156-
'description' => __( 'Whether to prefetch or prerender URLs.', 'speculation-rules' ),
180+
'description' => plsr_get_field_description( 'mode', true ),
157181
'type' => 'string',
158182
'enum' => array_keys( plsr_get_mode_labels() ),
159183
),
160184
'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' ),
185+
'description' => plsr_get_field_description( 'eagerness', true ),
162186
'type' => 'string',
163187
'enum' => array_keys( plsr_get_eagerness_labels() ),
164188
),
165189
'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. For optimal performance when enabling authenticated user support, ensure you have a persistent object cache configured. This only applies to pages on frontend; admin screens remain excluded.', 'speculation-rules' ),
190+
'description' => plsr_get_field_description( 'authentication', true ),
167191
'type' => 'string',
168192
'enum' => array_keys( plsr_get_authentication_labels() ),
169193
),
@@ -203,19 +227,15 @@ static function (): void {
203227
$fields = array(
204228
'mode' => array(
205229
'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' ),
230+
'description' => plsr_get_field_description( 'mode' ),
207231
),
208232
'eagerness' => array(
209233
'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' ),
234+
'description' => plsr_get_field_description( 'eagerness' ),
211235
),
212236
'authentication' => array(
213237
'title' => __( 'User Authentication Status', 'speculation-rules' ),
214-
'description' => sprintf(
215-
/* translators: %s: URL to persistent object cache documentation */
216-
__( '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 when enabling authenticated user support, ensure you have a <a href="%s" target="_blank">persistent object cache</a> configured. This only applies to pages on frontend; admin screens remain excluded.', 'speculation-rules' ),
217-
'https://developer.wordpress.org/advanced-administration/performance/optimization/#object-caching'
218-
),
238+
'description' => plsr_get_field_description( 'authentication' ),
219239
),
220240
);
221241
foreach ( $fields as $slug => $args ) {

0 commit comments

Comments
 (0)