-
Notifications
You must be signed in to change notification settings - Fork 381
Expand file tree
/
Copy pathReaderThemeLoader.php
More file actions
421 lines (369 loc) · 12.1 KB
/
ReaderThemeLoader.php
File metadata and controls
421 lines (369 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
* Class ReaderThemeLoader.
*
* @package AmpProject\AmpWP
*/
namespace AmpProject\AmpWP;
use AmpProject\AmpWP\Infrastructure\Registerable;
use AmpProject\AmpWP\Infrastructure\Service;
use AMP_Options_Manager;
use AMP_Theme_Support;
use AmpProject\AmpWP\Admin\ReaderThemes;
use WP_Theme;
use WP_Customize_Manager;
/**
* Switches to the designated Reader theme when template mode enabled and when requesting an AMP page.
*
* This class does not implement Conditional because other services need to always be able to access this
* service in order to determine whether or a Reader theme is loaded, and if so, what the previously-active theme was.
*
* @package AmpProject\AmpWP
* @since 2.0
* @internal
*/
final class ReaderThemeLoader implements Service, Registerable {
/**
* Paired routing service.
*
* @var PairedRouting
*/
private $paired_routing;
/**
* Reader theme.
*
* @var WP_Theme|null
*/
private $reader_theme;
/**
* Active theme.
*
* Theme which was active before switching to the Reader theme.
*
* @var WP_Theme
*/
private $active_theme;
/**
* Whether the active theme was overridden with the Reader theme.
*
* @var bool
*/
private $theme_overridden = false;
/**
* ReaderThemeLoader constructor.
*
* @param PairedRouting $paired_routing Paired routing service.
*/
public function __construct( PairedRouting $paired_routing ) {
$this->paired_routing = $paired_routing;
}
/**
* Is Reader mode with a Reader theme selected.
*
* @param array $options Options to check. If omitted, the currently-saved options are used.
* @return bool Whether new Reader mode.
*/
public function is_enabled( $options = null ) {
// If the theme was overridden then we know it is enabled. We can't check get_template() at this point because
// it will be identical to $reader_theme.
if ( $this->is_theme_overridden() ) {
return true;
}
if ( null === $options ) {
$options = AMP_Options_Manager::get_options();
}
// If Reader mode is not enabled, then a Reader theme is definitely not going to be served.
if (
! isset( $options[ Option::THEME_SUPPORT ] )
||
AMP_Theme_Support::READER_MODE_SLUG !== $options[ Option::THEME_SUPPORT ]
) {
return false;
}
// If the Legacy Reader mode is active, then a Reader theme is not going to be served.
if ( ! isset( $options[ Option::READER_THEME ] ) ) {
return false;
}
$reader_theme = $options[ Option::READER_THEME ];
if ( ReaderThemes::DEFAULT_READER_THEME === $reader_theme ) {
return false;
}
// If the Reader theme does not exist, then we cannot switch to the reader theme. The Legacy Reader theme will
// be used as a fallback instead.
if ( ! wp_get_theme( $reader_theme )->exists() ) {
return false;
}
// Lastly, if the active theme is not the same as the reader theme, then we can switch to the reader theme.
// Otherwise, the site should instead be in Transitional mode. Note that get_stylesheet() is used as opposed
// to get_template() because the active theme should be allowed to be a child theme of a Reader theme.
return get_stylesheet() !== $reader_theme;
}
/**
* Whether the active theme was overridden with the reader theme.
*
* @return bool Whether theme overridden.
*/
public function is_theme_overridden() {
return $this->theme_overridden;
}
/**
* Register the service with the system.
*
* @return void
*/
public function register() {
// The following needs to run at plugins_loaded because that is when _wp_customize_include runs. Otherwise, the
// most logical action would be setup_theme.
add_action( 'plugins_loaded', [ $this, 'override_theme' ], 9 );
add_filter( 'wp_prepare_themes_for_js', [ $this, 'filter_wp_prepare_themes_to_indicate_reader_theme' ] );
add_action( 'admin_print_footer_scripts-themes.php', [ $this, 'inject_theme_single_template_modifications' ] );
}
/**
* Filter themes for JS to remove action to delete the selected Reader theme and show a notice.
*
* @param array $prepared_themes Array of theme data.
* @return array Themes.
*/
public function filter_wp_prepare_themes_to_indicate_reader_theme( $prepared_themes ) {
if ( ! $this->is_enabled() ) {
return $prepared_themes;
}
$reader_theme_obj = $this->get_reader_theme();
if ( ! $reader_theme_obj instanceof WP_Theme ) {
return $prepared_themes;
}
$reader_theme = $reader_theme_obj->get_stylesheet();
if ( isset( $prepared_themes[ $reader_theme ] ) ) {
// Make sure the AMP Reader theme appears right after the active theme in the list.
$stylesheet = get_stylesheet();
if ( isset( $prepared_themes[ $stylesheet ] ) ) {
$prepared_themes = array_merge(
[
$stylesheet => $prepared_themes[ $stylesheet ],
$reader_theme => $prepared_themes[ $reader_theme ],
],
$prepared_themes
);
}
// Prevent Reader theme from being deleted.
unset( $prepared_themes[ $reader_theme ]['actions']['delete'] );
// Make sure the Customize link goes to AMP.
$prepared_themes[ $reader_theme ]['actions']['customize'] = amp_get_customizer_url();
// Force the theme to be styled as Active.
$prepared_themes[ $reader_theme ]['active'] = true;
// Make sure the hacked Backbone template will use the AMP action links.
$prepared_themes[ $reader_theme ]['ampActiveReaderTheme'] = true;
// Add AMP Reader theme notice.
$prepared_themes[ $reader_theme ]['ampReaderThemeNotice'] = sprintf(
wp_kses(
/* translators: placeholder is link to AMP settings screen */
__( 'This has been <a href="%s">selected</a> as the AMP Reader theme.', 'amp' ),
[
'a' => [ 'href' => true ],
]
),
esc_url( add_query_arg( 'page', AMP_Options_Manager::OPTION_NAME, admin_url( 'admin.php' ) ) . '#reader-themes' )
);
}
return $prepared_themes;
}
/**
* Inject new logic into the Backbone templates for rendering a theme lightbox.
*
* This is admittedly hacky, but WordPress doesn't provide a much better option.
*/
public function inject_theme_single_template_modifications() {
if ( ! $this->is_enabled() ) {
return;
}
$reader_theme = $this->get_reader_theme();
if ( ! $reader_theme instanceof WP_Theme ) {
return;
}
?>
<script>
(function( themeSingleTmpl ) {
if ( ! themeSingleTmpl ) {
return;
}
let text = themeSingleTmpl.text;
text = text.replace(
/(?=<p class="theme-description">)/,
`
<# if ( data.ampReaderThemeNotice ) { #>
<div class="notice notice-info notice-alt inline">
<p>{{{ data.ampReaderThemeNotice }}}</p><?php // phpcs:ignore WordPressVIPMinimum.Security.Mustache.OutputNotation -- Contains link and already sanitized with Kses. ?>
</div>
<# } #>
`
);
// Inject our action links.
text = text.replace(
/(<div class="active-theme">)((?:.|\s)+?)(<\/div>)/,
( match, startDiv, actionLinks, endDiv ) => {
return `
${startDiv}
<# if ( data.ampActiveReaderTheme ) { #>
<a href="{{ data.actions.customize }}" class="button button-primary customize load-customize hide-if-no-customize">
<?php esc_html_e( 'Customize', 'default' ); ?>
</a>
<a href="<?php echo esc_url( add_query_arg( 'page', AMP_Options_Manager::OPTION_NAME, admin_url( 'admin.php' ) ) ); ?>" class="button button-secondary">
<?php esc_html_e( 'AMP Settings', 'amp' ); ?>
</a>
<# } else { #>
${actionLinks}
<# } #>
${endDiv}
`;
}
);
themeSingleTmpl.text = text;
})( document.getElementById( 'tmpl-theme-single' ) );
(function ( themeTmpl, ssrThemeNamePrefixElement ) {
// First update the card that was rendered server-side in wp-admin/themes.php.
if ( ssrThemeNamePrefixElement ) {
ssrThemeNamePrefixElement.textContent = <?php echo wp_json_encode( _x( 'Reader:', 'prefix for theme card in list', 'amp' ) ); ?>;
}
// Then update the Mustache template so that future renders will also have the proper prefix.
if ( themeTmpl ) {
themeTmpl.text = themeTmpl.text.replace(
/(<div class="theme-id-container">)(\s*<# if)/,
function ( match, startDiv ) {
return `
${startDiv}
<# if ( data.ampActiveReaderTheme ) { #>
<h2 class="theme-name" id="{{ data.id }}-name">
<span><?php echo esc_html( _x( 'Reader:', 'prefix for theme card in list', 'amp' ) ); ?></span> {{ data.name }}
</h2>
<# } else if
`;
}
);
}
}) (
document.getElementById( 'tmpl-theme' ),
document.querySelector( <?php echo wp_json_encode( sprintf( '#%s-name > span', $reader_theme->get_stylesheet() ) ); ?> )
);
</script>
<?php
}
/**
* Get reader theme.
*
* If the Reader template mode is enabled
*
* @return WP_Theme|null Theme if selected and no errors.
*/
public function get_reader_theme() {
if ( $this->reader_theme instanceof WP_Theme ) {
return $this->reader_theme;
}
$reader_theme_slug = AMP_Options_Manager::get_option( Option::READER_THEME );
if ( ! $reader_theme_slug ) {
return null;
}
$reader_theme = wp_get_theme( $reader_theme_slug );
if ( $reader_theme->errors() ) {
return null;
}
return $reader_theme;
}
/**
* Get active theme.
*
* The theme that was active before switching to the Reader theme.
*
* @return WP_Theme WP_Theme instance.
*/
public function get_active_theme() {
return $this->active_theme;
}
/**
* Switch theme if in Reader mode, a Reader theme was selected, and the AMP query var is present.
*
* Note that AMP_Theme_Support will redirect to the non-AMP version if AMP is not available for the query.
*
* @see WP_Customize_Manager::start_previewing_theme() which provides for much of the inspiration here.
* @see switch_theme() which ensures the new theme includes the old theme's theme mods.
*/
public function override_theme() {
$this->theme_overridden = false;
if ( ! $this->is_enabled() || ! $this->paired_routing->has_endpoint() ) {
return;
}
$theme = $this->get_reader_theme();
if ( ! $theme instanceof WP_Theme ) {
return;
}
$this->active_theme = wp_get_theme();
$this->reader_theme = $theme;
$this->theme_overridden = true;
$get_template = function () {
return $this->reader_theme->get_template();
};
$get_stylesheet = function () {
return $this->reader_theme->get_stylesheet();
};
add_filter( 'stylesheet', $get_stylesheet );
add_filter( 'template', $get_template );
add_filter(
'pre_option_current_theme',
function () {
return $this->reader_theme->display( 'Name' );
}
);
// @link: https://core.trac.wordpress.org/ticket/20027
add_filter( 'pre_option_stylesheet', $get_stylesheet );
add_filter( 'pre_option_template', $get_template );
// Handle custom theme roots.
add_filter(
'pre_option_stylesheet_root',
function () {
return get_raw_theme_root( $this->reader_theme->get_stylesheet(), true );
}
);
add_filter(
'pre_option_template_root',
function () {
return get_raw_theme_root( $this->reader_theme->get_template(), true );
}
);
$this->disable_widgets();
add_filter( 'customize_previewable_devices', [ $this, 'customize_previewable_devices' ] );
add_action( 'customize_register', [ $this, 'remove_customizer_themes_panel' ], 11 );
}
/**
* Disable widgets.
*/
public function disable_widgets() {
add_filter( 'sidebars_widgets', '__return_empty_array', PHP_INT_MAX );
add_filter(
'customize_loaded_components',
static function ( $components ) {
return array_diff( $components, [ 'widgets' ] );
}
);
remove_theme_support( 'widgets-block-editor' );
}
/**
* Make tablet (smartphone) the default device when opening AMP Customizer.
*
* @param array $devices Devices.
* @return array Devices.
*/
public function customize_previewable_devices( $devices ) {
if ( isset( $devices['tablet'] ) ) {
unset( $devices['desktop']['default'] );
$devices['tablet']['default'] = true;
}
return $devices;
}
/**
* Remove themes panel from AMP Customizer.
*
* @param WP_Customize_Manager $wp_customize Customize manager.
*/
public function remove_customizer_themes_panel( WP_Customize_Manager $wp_customize ) {
$wp_customize->remove_panel( 'themes' );
}
}