From fb8552f88d555a52cfadc7aee9dd9fbd68f182a4 Mon Sep 17 00:00:00 2001 From: Robin Cornett Date: Sat, 6 Jan 2018 13:26:20 -0500 Subject: [PATCH] Add a metabox for Gutenberg Currently the easiest way to make sure the post meta is added to the Gutenberg editor, since the featured image metabox hook/filter is no longer available. Presumably this will eventually be replaced with a true block, or whatever it will be, but this works for the moment. --- ...s-displayfeaturedimagegenesis-postmeta.php | 53 ++++++++++++++++++- .../class-displayfeaturedimagegenesis.php | 7 ++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/includes/class-displayfeaturedimagegenesis-postmeta.php b/includes/class-displayfeaturedimagegenesis-postmeta.php index 9f39846..c0f4e42 100644 --- a/includes/class-displayfeaturedimagegenesis-postmeta.php +++ b/includes/class-displayfeaturedimagegenesis-postmeta.php @@ -10,6 +10,12 @@ */ class Display_Featured_Image_Genesis_Post_Meta { + /** + * ID for our new metabox. + * @var string + */ + protected $metabox = 'displayfeaturedimagegenesis'; + /** * Post meta key to disable buttons * @var string @@ -22,6 +28,41 @@ class Display_Featured_Image_Genesis_Post_Meta { */ protected $move = '_displayfeaturedimagegenesis_move'; + /** + * For Gutenberg, add a new metabox, since the thumbnail hooks are no longer present. + * Should eventually be replaced with a block. + * + * @since 2.7.0 + * @param $post_type + * @param $post + */ + public function add_metabox( $post_type, $post ) { + if ( ! post_type_supports( $post_type, 'thumbnail' ) ) { + return; + } + $classic_editor = filter_input( INPUT_GET, 'classic-editor', FILTER_VALIDATE_BOOLEAN ); + if ( null !== $classic_editor ) { + return; + } + add_meta_box( + $this->metabox, + __( 'Display Featured Image', 'display-featured-image-genesis' ), + array( $this, 'do_metabox' ), + $post_type, + 'side', + 'low' + ); + } + + /** + * Output the metabox. + * @param $post + * @param $args + */ + public function do_metabox( $post, $args ) { + echo $this->get_metabox_content( $post->ID ); + } + /** * Build the metabox with the checkbox setting. * @since 2.5.0 @@ -32,7 +73,17 @@ class Display_Featured_Image_Genesis_Post_Meta { * @return string */ public function meta_box( $content, $post_id ) { + return $this->get_metabox_content( $post_id ) . $content; + } + /** + * Get the metabox content/fields. + * + * @param $post_id + * + * @return string + */ + protected function get_metabox_content( $post_id ) { $output = wp_nonce_field( 'displayfeaturedimagegenesis_post_save', 'displayfeaturedimagegenesis_post_nonce', true, false ); $select = $this->get_select(); if ( $select ) { @@ -47,7 +98,7 @@ class Display_Featured_Image_Genesis_Post_Meta { } } - return $output . $content; + return $output; } /** diff --git a/includes/class-displayfeaturedimagegenesis.php b/includes/class-displayfeaturedimagegenesis.php index df66343..3611511 100644 --- a/includes/class-displayfeaturedimagegenesis.php +++ b/includes/class-displayfeaturedimagegenesis.php @@ -102,7 +102,7 @@ class Display_Featured_Image_Genesis { * @param $settings * @param $taxonomies */ - function __construct( $admin, $author, $common, $customizer, $description, $helptabs, $output, $post_meta, $rss, $settings, $taxonomies, $widgets ) { + public function __construct( $admin, $author, $common, $customizer, $description, $helptabs, $output, $post_meta, $rss, $settings, $taxonomies, $widgets ) { $this->admin = $admin; $this->author = $author; $this->common = $common; @@ -142,10 +142,13 @@ class Display_Featured_Image_Genesis { add_action( 'widgets_init', array( $this->widgets, 'register_shortcodes' ) ); add_action( 'admin_enqueue_scripts', array( $this->widgets, 'enqueue_scripts' ) ); - // Taxonomies, Author, Post Meta + // Taxonomies, Authors add_filter( 'displayfeaturedimagegenesis_get_taxonomies', array( $this->taxonomies, 'remove_post_status_terms' ) ); add_action( 'admin_init', array( $this->taxonomies, 'set_taxonomy_meta' ) ); add_action( 'admin_init', array( $this->author, 'set_author_meta' ) ); + + // Post Meta + add_action( 'add_meta_boxes', array( $this->post_meta, 'add_metabox' ), 10, 2 ); add_filter( 'admin_post_thumbnail_html', array( $this->post_meta, 'meta_box' ), 10, 2 ); add_action( 'save_post', array( $this->post_meta, 'save_meta' ) );