array( $this, 'sanitize_status' ), 'type' => 'string', 'description' => __( 'AMP status.', 'amp' ), 'show_in_rest' => true, 'single' => true, ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) ); add_action( 'post_submitbox_misc_actions', array( $this, 'render_status' ) ); add_action( 'save_post', array( $this, 'save_amp_status' ) ); add_filter( 'preview_post_link', array( $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, array( 'enabled', 'disabled' ), 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 && is_post_type_viewable( $post->post_type ) ); if ( ! $validate ) { return; } // Styles. wp_enqueue_style( self::ASSETS_HANDLE, amp_get_asset_url( 'css/amp-post-meta-box.css' ), false, AMP__VERSION ); // Scripts. wp_enqueue_script( self::ASSETS_HANDLE, amp_get_asset_url( 'js/amp-post-meta-box.js' ), array( 'jquery' ), AMP__VERSION ); wp_add_inline_script( self::ASSETS_HANDLE, sprintf( 'ampPostMetaBox.boot( %s );', wp_json_encode( array( 'previewLink' => esc_url_raw( add_query_arg( amp_get_slug(), '', get_preview_post_link( $post ) ) ), 'canonical' => amp_is_canonical(), 'enabled' => post_supports_amp( $post ), 'canSupport' => count( AMP_Post_Type_Support::get_support_errors( $post ) ) === 0, 'statusInputName' => self::STATUS_INPUT_NAME, 'l10n' => array( 'ampPreviewBtnLabel' => __( 'Preview changes in AMP (opens in new window)', 'amp' ), ), ) ) ) ); } /** * Render AMP status. * * @since 0.6 * @param WP_Post $post Post. */ public function render_status( $post ) { $verify = ( isset( $post->ID ) && is_post_type_viewable( $post->post_type ) && current_user_can( 'edit_post', $post->ID ) && ! amp_is_canonical() ); if ( true !== $verify ) { return; } $errors = AMP_Post_Type_Support::get_support_errors( $post ); $status = post_supports_amp( $post ) ? self::ENABLED_STATUS : self::DISABLED_STATUS; $labels = array( 'enabled' => __( 'Enabled', 'amp' ), 'disabled' => __( 'Disabled', 'amp' ), ); // The preceding variables are used inside the following amp-status.php template. include_once AMP__DIR__ . '/templates/admin/amp-status.php'; } /** * 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 ] ) && isset( $_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 ] // Note: The sanitize_callback has been supplied in the register_meta() call above. ); } } /** * Modify post preview link. * * Add the AMP query var is 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'] ) // WPCS: CSRF ok. && 'do-preview' === sanitize_key( wp_unslash( $_POST['amp-preview'] ) ) // WPCS: CSRF ok. ); if ( $is_amp ) { $link = add_query_arg( amp_get_slug(), true, $link ); } return $link; } }