Skip to content

Commit

Permalink
Add simple support for new block editor
Browse files Browse the repository at this point in the history
This is a temporary solution so that the Subtitles can still be edited in the new block editor(Gutenberg).

Don’t load styles & scripts on post types that do not support subtitles.

Related to wecobble#104
  • Loading branch information
grappler committed Mar 19, 2019
1 parent 209c5af commit 9fec16b
Showing 1 changed file with 66 additions and 0 deletions.
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

0 comments on commit 9fec16b

Please sign in to comment.