Skip to content
This repository has been archived by the owner on Dec 17, 2021. It is now read-only.

Add simple support for new block editor #113

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions admin/class-subtitles-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ protected function __construct() {
add_action( 'edit_form_before_permalink', array( &$this, 'build_subtitle_input' ) );
}

/**
* Add Subtitle metabox in pannel for block editor.
*
* @see add_action()
* @link http://codex.wordpress.org/Function_Reference/add_action
*
* @since 4.0.0
*/
add_action( 'add_meta_boxes', array( &$this, 'add_meta_boxes' ) );

/**
* Validate and update the subtitle input field.
*
Expand Down Expand Up @@ -189,6 +199,50 @@ public function build_subtitle_input( $post ) {
do_action( 'edit_form_after_subtitle', $post );
} // end build_subtitle_input()

/**
* Add metabox to block editor pannel.
*
* @access public
*
* @param string $post_type Post type.
* @since 4.0.0
*/
public function add_meta_boxes( $post_type ) {
$post_type_support = post_type_supports( $post_type, self::SUBTITLE_FEATURE_SUPPORT );
if ( ! $post_type_support ) {
return;
}

if ( ! $this->block_editor_supported( $post_type ) ) {
return;
}

add_meta_box(
'subtitle_panel',
__( 'Subtitle', 'subtitles' ),
array( $this, 'build_subtitle_input' ),
$post_type,
'side',
'high'
);
}

/**
* Check if post type supports the block editor.
*
* @access private
*
* @param string $post_type Post type.
* @since 4.0.0
* @return bool
*/
private function block_editor_supported( $post_type ) {
if ( function_exists( 'use_block_editor_for_post_type' ) && use_block_editor_for_post_type( $post_type ) && ! isset( $_GET['classic-editor'] ) ) {
return true;
}
return false;
}

/**
* Validate and save custom metadata associated with Subtitles.
*
Expand Down Expand Up @@ -319,6 +373,18 @@ public function subtitle_admin_scripts( $hook ) {
return;
}

/**
* Bail on these scripts and styles if post type is not supported.
*
* They are not needed for the block editor.
*
* @since 4.0.0
*/
$screen = (object) get_current_screen();
if ( $this->block_editor_supported( $screen->post_type ) || ! post_type_supports( $screen->post_type, self::SUBTITLE_FEATURE_SUPPORT ) ) {
return;
}

/**
* Load in main Subtitles admin stylesheet
*
Expand Down