-
Notifications
You must be signed in to change notification settings - Fork 384
/
class-amp-post-meta-box.php
610 lines (538 loc) · 17.5 KB
/
class-amp-post-meta-box.php
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
<?php
/**
* AMP meta box settings.
*
* @package AMP
* @since 0.6
*/
use AmpProject\AmpWP\Services;
/**
* Post meta box class.
*
* @since 0.6
* @internal
*/
class AMP_Post_Meta_Box {
/**
* Assets handle.
*
* @since 0.6
* @var string
*/
const ASSETS_HANDLE = 'amp-post-meta-box';
/**
* Block asset handle.
*
* @since 1.0
* @var string
*/
const BLOCK_ASSET_HANDLE = 'amp-block-editor';
/**
* The enabled status post meta value.
*
* @since 0.6
* @var string
*/
const ENABLED_STATUS = 'enabled';
/**
* The disabled status post meta value.
*
* @since 0.6
* @var string
*/
const DISABLED_STATUS = 'disabled';
/**
* The status post meta key.
*
* @since 0.6
* @var string
*/
const STATUS_POST_META_KEY = 'amp_status';
/**
* The field name for the enabled/disabled radio buttons.
*
* @since 0.6
* @var string
*/
const STATUS_INPUT_NAME = 'amp_status';
/**
* The nonce name.
*
* @since 0.6
* @var string
*/
const NONCE_NAME = 'amp-status-nonce';
/**
* The nonce action.
*
* @since 0.6
* @var string
*/
const NONCE_ACTION = 'amp-update-status';
/**
* The name for the REST API field containing whether AMP is enabled for a post.
*
* @since 2.0
* @var string
*/
const REST_ATTRIBUTE_NAME = 'amp_enabled';
/**
* Initialize.
*
* @since 0.6
*/
public function init() {
register_meta(
'post',
self::STATUS_POST_META_KEY,
[
'sanitize_callback' => [ $this, 'sanitize_status' ],
'auth_callback' => '__return_false',
'type' => 'string',
'description' => __( 'AMP status.', 'amp' ),
'show_in_rest' => false,
'single' => true,
]
);
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_assets' ] );
add_action( 'post_submitbox_misc_actions', [ $this, 'render_status' ] );
add_action( 'save_post', [ $this, 'save_amp_status' ] );
add_action( 'rest_api_init', [ $this, 'add_rest_api_fields' ] );
add_filter( 'preview_post_link', [ $this, 'preview_post_link' ] );
}
/**
* Sanitize status.
*
* @param string $status Status.
* @return string Sanitized status. Empty string when invalid.
*/
public function sanitize_status( $status ) {
$status = strtolower( trim( $status ) );
if ( ! in_array( $status, [ self::ENABLED_STATUS, self::DISABLED_STATUS ], true ) ) {
/*
* In lieu of actual validation being available, clear the status entirely
* so that the underlying default status will be used instead.
* In the future it would be ideal if register_meta() accepted a
* validate_callback as well which the REST API could leverage.
*/
$status = '';
}
return $status;
}
/**
* Enqueue admin assets.
*
* @since 0.6
*/
public function enqueue_admin_assets() {
$post = get_post();
$screen = get_current_screen();
$validate = (
isset( $screen->base ) &&
'post' === $screen->base &&
empty( $screen->is_block_editor ) &&
in_array( $post->post_type, AMP_Post_Type_Support::get_supported_post_types(), true )
);
if ( ! $validate ) {
return;
}
wp_enqueue_style(
self::ASSETS_HANDLE,
amp_get_asset_url( 'css/amp-post-meta-box.css' ),
false,
AMP__VERSION
);
wp_styles()->add_data( self::ASSETS_HANDLE, 'rtl', 'replace' );
// Abort if version of WordPress is too old.
if ( ! Services::get( 'dependency_support' )->has_support_from_core() ) {
return;
}
$asset_file = AMP__DIR__ . '/assets/js/' . self::ASSETS_HANDLE . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter
wp_enqueue_script(
self::ASSETS_HANDLE,
amp_get_asset_url( 'js/' . self::ASSETS_HANDLE . '.js' ),
$dependencies,
$version,
false
);
if ( ! amp_is_legacy() ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$support_errors = $availability['errors'];
} else {
$support_errors = AMP_Post_Type_Support::get_support_errors( $post );
}
wp_add_inline_script(
self::ASSETS_HANDLE,
sprintf(
'ampPostMetaBox.boot( %s );',
wp_json_encode(
[
'previewLink' => esc_url_raw( amp_add_paired_endpoint( get_preview_post_link( $post ) ) ),
'canonical' => amp_is_canonical(),
'enabled' => empty( $support_errors ),
'canSupport' => 0 === count( array_diff( $support_errors, [ 'post-status-disabled' ] ) ),
'statusInputName' => self::STATUS_INPUT_NAME,
'l10n' => [
'ampPreviewBtnLabel' => __( 'Preview changes in AMP (opens in new window)', 'amp' ),
],
]
)
)
);
}
/**
* Enqueues block assets.
*
* @since 1.0
*/
public function enqueue_block_assets() {
$post = get_post();
// Block validation script uses features only available beginning with WP 5.6.
$dependency_support = Services::get( 'dependency_support' );
if ( ! $dependency_support->has_support() ) {
return; // @codeCoverageIgnore
}
// Only enqueue scripts on the block editor for AMP-enabled posts.
$editor_support = Services::get( 'editor.editor_support' );
if ( ! $editor_support->is_current_screen_block_editor_for_amp_enabled_post_type() ) {
return;
}
$status_and_errors = self::get_status_and_errors( $post );
// Skip proceeding if there are errors blocking AMP and the user can't do anything about it.
if ( ! empty( $status_and_errors['errors'] ) && ! current_user_can( 'manage_options' ) ) {
return;
}
wp_enqueue_style(
self::BLOCK_ASSET_HANDLE,
amp_get_asset_url( 'css/' . self::BLOCK_ASSET_HANDLE . '.css' ),
[],
AMP__VERSION
);
wp_styles()->add_data( self::BLOCK_ASSET_HANDLE, 'rtl', 'replace' );
$asset_file = AMP__DIR__ . '/assets/js/' . self::BLOCK_ASSET_HANDLE . '.asset.php';
$asset = require $asset_file;
$dependencies = $asset['dependencies'];
$version = $asset['version'];
wp_enqueue_script(
self::BLOCK_ASSET_HANDLE,
amp_get_asset_url( 'js/' . self::BLOCK_ASSET_HANDLE . '.js' ),
$dependencies,
$version,
true
);
$is_standard_mode = amp_is_canonical();
list( $featured_image_minimum_width, $featured_image_minimum_height ) = self::get_featured_image_dimensions();
$data = [
'ampUrl' => $is_standard_mode ? null : amp_add_paired_endpoint( get_permalink( $post ) ),
'ampPreviewLink' => $is_standard_mode ? null : amp_add_paired_endpoint( get_preview_post_link( $post ) ),
'errorMessages' => $this->get_error_messages( $status_and_errors['errors'] ),
'hasThemeSupport' => ! amp_is_legacy(),
'isDevToolsEnabled' => Services::get( 'dev_tools.user_access' )->is_user_enabled(),
'isStandardMode' => $is_standard_mode,
'featuredImageMinimumWidth' => $featured_image_minimum_width,
'featuredImageMinimumHeight' => $featured_image_minimum_height,
'ampBlocksInUse' => $is_standard_mode ? $this->get_amp_blocks_in_use() : [],
];
wp_add_inline_script(
self::BLOCK_ASSET_HANDLE,
sprintf( 'var ampBlockEditor = %s;', wp_json_encode( $data ) ),
'before'
);
if ( function_exists( 'wp_set_script_translations' ) ) {
wp_set_script_translations( self::BLOCK_ASSET_HANDLE, 'amp' );
} elseif ( function_exists( 'wp_get_jed_locale_data' ) || function_exists( 'gutenberg_get_jed_locale_data' ) ) {
$locale_data = function_exists( 'wp_get_jed_locale_data' ) ? wp_get_jed_locale_data( 'amp' ) : gutenberg_get_jed_locale_data( 'amp' );
$translations = wp_json_encode( $locale_data );
wp_add_inline_script(
self::BLOCK_ASSET_HANDLE,
'wp.i18n.setLocaleData( ' . $translations . ', "amp" );',
'after'
);
}
}
/**
* Returns a tuple of width and height featured image dimensions after filtering.
*
* @return int[] {
* Minimum dimensions.
*
* @type int $0 Image width in pixels. May be zero to disable the dimension constraint.
* @type int $1 Image height in pixels. May be zero to disable the dimension constraint.
* }
*/
public static function get_featured_image_dimensions() {
$default_width = 1200;
$default_height = 675;
/**
* Filters the minimum height required for a featured image.
*
* @since 2.0.9
*
* @param int $featured_image_minimum_height The minimum height of the image, defaults to 675.
* Returning a number less than or equal to zero disables the minimum constraint.
*/
$featured_image_minimum_height = (int) apply_filters( 'amp_featured_image_minimum_height', $default_height );
/**
* Filters the minimum width required for a featured image.
*
* @since 2.0.9
*
* @param int $featured_image_minimum_width The minimum width of the image, defaults to 1200.
* Returning a number less than or equal to zero disables the minimum constraint.
*/
$featured_image_minimum_width = (int) apply_filters( 'amp_featured_image_minimum_width', $default_width );
return [
max( $featured_image_minimum_width, 0 ),
max( $featured_image_minimum_height, 0 ),
];
}
/**
* Render AMP status.
*
* @since 0.6
* @param WP_Post $post Post.
*/
public function render_status( $post ) {
$verify = (
isset( $post->ID )
&&
in_array( $post->post_type, AMP_Post_Type_Support::get_supported_post_types(), true )
&&
current_user_can( 'edit_post', $post->ID )
);
if ( true !== $verify ) {
return;
}
$status_and_errors = self::get_status_and_errors( $post );
$status = $status_and_errors['status']; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable -- Used in amp-enabled-classic-editor-toggle.php.
$errors = $status_and_errors['errors'];
// Skip showing any error message if the user doesn't have the ability to do anything about it.
if ( ! empty( $errors ) && ! current_user_can( 'manage_options' ) ) {
return;
}
// phpcs:disable VariableAnalysis.CodeAnalysis.VariableAnalysis
$error_messages = $this->get_error_messages( $errors );
$labels = [
'enabled' => __( 'Enabled', 'amp' ),
'disabled' => __( 'Disabled', 'amp' ),
];
// phpcs:enable VariableAnalysis.CodeAnalysis.VariableAnalysis
// The preceding variables are used inside the following amp-status.php template.
include AMP__DIR__ . '/includes/templates/amp-enabled-classic-editor-toggle.php';
}
/**
* Gets the AMP enabled status and errors.
*
* @since 1.0
* @param WP_Post $post The post to check.
* @return array {
* The status and errors.
*
* @type string $status The AMP enabled status.
* @type string[] $errors AMP errors.
* }
*/
public static function get_status_and_errors( $post ) {
/*
* When theme support is present then theme templates can be served in AMP and we check first if the template is available.
* Checking for template availability will include a check for get_support_errors. Otherwise, if theme support is not present
* then we just check get_support_errors.
*/
if ( ! amp_is_legacy() ) {
$availability = AMP_Theme_Support::get_template_availability( $post );
$status = $availability['supported'] ? self::ENABLED_STATUS : self::DISABLED_STATUS;
$errors = array_diff( $availability['errors'], [ 'post-status-disabled' ] ); // Subtract the status which the metabox will allow to be toggled.
} else {
$errors = AMP_Post_Type_Support::get_support_errors( $post );
$status = empty( $errors ) ? self::ENABLED_STATUS : self::DISABLED_STATUS;
$errors = array_diff( $errors, [ 'post-status-disabled' ] ); // Subtract the status which the metabox will allow to be toggled.
}
return compact( 'status', 'errors' );
}
/**
* Gets the AMP enabled error message(s).
*
* @since 1.0
* @see AMP_Post_Type_Support::get_support_errors()
*
* @param string[] $errors The AMP enabled errors.
* @return array $error_messages The error messages, as an array of strings.
*/
public function get_error_messages( $errors ) {
$settings_screen_url = admin_url( 'admin.php?page=' . AMP_Options_Manager::OPTION_NAME );
$error_messages = [];
if ( in_array( 'template_unsupported', $errors, true ) || in_array( 'no_matching_template', $errors, true ) ) {
$error_messages[] = sprintf(
/* translators: %s is a link to the AMP settings screen */
__( 'There are no <a href="%s" target="_blank">supported templates</a>.', 'amp' ),
esc_url( $settings_screen_url )
);
}
if ( in_array( 'post-type-support', $errors, true ) ) {
$error_messages[] = sprintf(
/* translators: %s is a link to the AMP settings screen */
__( 'This post type is not <a href="%s" target="_blank">enabled</a>.', 'amp' ),
esc_url( $settings_screen_url )
);
}
if ( in_array( 'skip-post', $errors, true ) ) {
$error_messages[] = __( 'A plugin or theme has disabled AMP support.', 'amp' );
}
if ( in_array( 'invalid-post', $errors, true ) ) {
$error_messages[] = __( 'The post data could not be successfully retrieved.', 'amp' );
}
if ( count( array_diff( $errors, [ 'post-type-support', 'skip-post', 'template_unsupported', 'no_matching_template', 'invalid-post' ] ) ) > 0 ) {
$error_messages[] = __( 'Unavailable for an unknown reason.', 'amp' );
}
return $error_messages;
}
/**
* Save AMP Status.
*
* @since 0.6
* @param int $post_id The Post ID.
*/
public function save_amp_status( $post_id ) {
$verify = (
isset( $_POST[ self::NONCE_NAME ], $_POST[ self::STATUS_INPUT_NAME ] )
&&
wp_verify_nonce( sanitize_key( wp_unslash( $_POST[ self::NONCE_NAME ] ) ), self::NONCE_ACTION )
&&
current_user_can( 'edit_post', $post_id )
&&
! wp_is_post_revision( $post_id )
&&
! wp_is_post_autosave( $post_id )
);
if ( true === $verify ) {
update_post_meta(
$post_id,
self::STATUS_POST_META_KEY,
$_POST[ self::STATUS_INPUT_NAME ] // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- The sanitize_callback has been supplied in the register_meta() call above.
);
}
}
/**
* Modify post preview link.
*
* Add the AMP query var if the amp-preview flag is set.
*
* @since 0.6
*
* @param string $link The post preview link.
* @return string Preview URL.
*/
public function preview_post_link( $link ) {
$is_amp = (
isset( $_POST['amp-preview'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
&&
'do-preview' === sanitize_key( wp_unslash( $_POST['amp-preview'] ) ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
);
if ( $is_amp ) {
$link = amp_add_paired_endpoint( $link );
}
return $link;
}
/**
* Add a REST API field to display whether AMP is enabled on supported post types.
*
* @since 2.0
*
* @return void
*/
public function add_rest_api_fields() {
register_rest_field(
AMP_Post_Type_Support::get_post_types_for_rest_api(),
self::REST_ATTRIBUTE_NAME,
[
'get_callback' => [ $this, 'get_amp_enabled_rest_field' ],
'update_callback' => [ $this, 'update_amp_enabled_rest_field' ],
'schema' => [
'description' => __( 'AMP enabled', 'amp' ),
'type' => 'boolean',
],
]
);
}
/**
* Get the value of whether AMP is enabled for a REST API request.
*
* @since 2.0
*
* @param array $post_data Post data.
* @return bool Whether AMP is enabled on post.
*/
public function get_amp_enabled_rest_field( $post_data ) {
$status = $this->sanitize_status( get_post_meta( $post_data['id'], self::STATUS_POST_META_KEY, true ) );
if ( '' === $status ) {
$post = get_post( $post_data['id'] );
$status_and_errors = self::get_status_and_errors( $post );
if ( isset( $status_and_errors['status'] ) ) {
$status = $status_and_errors['status'];
}
}
return self::ENABLED_STATUS === $status;
}
/**
* Update whether AMP is enabled for a REST API request.
*
* @since 2.0
*
* @param bool $is_enabled Whether AMP is enabled.
* @param WP_Post $post Post being updated.
* @return null|WP_Error Null on success, WP_Error object on failure.
*/
public function update_amp_enabled_rest_field( $is_enabled, $post ) {
if ( ! in_array( $post->post_type, AMP_Post_Type_Support::get_post_types_for_rest_api(), true ) ) {
return new WP_Error(
'rest_invalid_post_type',
sprintf(
/* translators: %s: The name of the post type. */
__( 'AMP is not supported for the "%s" post type.', 'amp' ),
$post->post_type
),
[ 'status' => 400 ]
);
}
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return new WP_Error(
'rest_insufficient_permission',
__( 'Insufficient permissions to change whether AMP is enabled.', 'amp' ),
[ 'status' => 403 ]
);
}
$status = $is_enabled ? self::ENABLED_STATUS : self::DISABLED_STATUS;
// Note: The sanitize_callback has been supplied in the register_meta() call above.
$updated = update_post_meta(
$post->ID,
self::STATUS_POST_META_KEY,
$status
);
if ( false === $updated ) {
return new WP_Error(
'rest_update_failed',
__( 'The AMP enabled status failed to be updated.', 'amp' ),
[ 'status' => 500 ]
);
}
return null;
}
/**
* Get the list of AMP block names used in the current post.
*
* @since 2.1
*
* @return string[]
*/
public function get_amp_blocks_in_use() {
// Normalize the AMP block names to include the `amp/` namespace.
$amp_blocks = substr_replace( AMP_Editor_Blocks::AMP_BLOCKS, 'amp/', 0, 0 );
$amp_blocks_in_use = array_filter( $amp_blocks, 'has_block' );
return array_values( $amp_blocks_in_use );
}
}