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

[WIP] Post editor enhancements #222

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
22 changes: 18 additions & 4 deletions js/customize-post-editor-control.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global jQuery, wp, _, tinyMCE */
/* global jQuery, wp, _, tinyMCE, _wpCustomizePostsEditorExports */
/* eslint consistent-this: [ "error", "control" ], no-magic-numbers: [ "error", { "ignore": [1] } ] */

(function( api, $ ) {
Expand All @@ -17,6 +17,17 @@
initialize: function initialize( id, options ) {
var control = this, args;

control.data = {
l10n: {
openEditor: '',
closeEditor: ''
}
};

if ( 'undefined' !== typeof _wpCustomizePostsEditorExports ) {
_.extend( control.data, _wpCustomizePostsEditorExports );
}

_.bindAll(
control,
'handleRemoval',
Expand All @@ -39,7 +50,11 @@
label: api.Posts.data.l10n.fieldContentLabel,
active: true,
setting_property: null,
input_attrs: {}
input_attrs: {},
button_labels: {
open_editor: control.data.l10n.openEditor,
close_editor: control.data.l10n.closeEditor
}
},
options.params || {}
);
Expand Down Expand Up @@ -421,8 +436,7 @@
updateEditorToggleExpandButtonLabel: function updateEditorToggleExpandButtonLabel( expanded ) {
var control = this;

// @todo Allow these labels to be parameters on the control.
control.editorToggleExpandButton.text( expanded ? api.Posts.data.l10n.closeEditor : api.Posts.data.l10n.openEditor );
control.editorToggleExpandButton.text( expanded ? control.params.button_labels.close_editor : control.params.button_labels.open_editor );
},

/**
Expand Down
67 changes: 0 additions & 67 deletions php/class-wp-customize-editor-control.php

This file was deleted.

88 changes: 88 additions & 0 deletions php/class-wp-customize-post-editor-control.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,94 @@ public function enqueue() {
wp_enqueue_script( 'customize-post-editor-control' );
}

/**
* Hooked to `customize_controls_enqueue_scripts` from `WP_Customize_Posts` class.
*/
public static function enqueue_scripts() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sayedwp Actually, maybe you should switch back to using the non-static enqueue() method, but then have a static class variable to capture whether the enqueue has been called:

static protected $enqueued = false;

public function enqueue() {
    if ( self::$enqueued ) {
        return;
    }
    self::$enqueued = true;
    // ...
}

Then you can avoid WP_Customize_Posts from having direct awareness of this class's methods and so could remove:

add_action( 'customize_controls_enqueue_scripts', array( 'WP_Customize_Post_Editor_Control', 'enqueue_scripts' ) );

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@westonruter enqueue method never executes until we add control using add_control. So currently enqueue is just sitting there and not doing anything, we are enqueueing the script as a dependency from register_scripts in Customize_Posts_Plugin

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, good point. I'd consider this a deficiency in the WP_Customize_Manager::register_control_type() that it doesn't call the enqueue method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also we should be able to use customize_controls_enqueue_scripts from inside the WP_Customize_Post_Editor_Control construct but we cannot do that because render_control_template is hooked into customize_controls_print_footer_scripts which is at the footer and customize_controls_enqueue_scripts is in the header, so already late.

https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-customize-manager.php#L301
https://github.com/WordPress/WordPress/blob/master/wp-admin/customize.php#L63-L185

However if we use add_control I think then it should be working because its hooked into customize_register.

self::enqueue_editor();

$exports = array(
'l10n' => array(
'openEditor' => __( 'Open Editor', 'customize-posts' ),
'closeEditor' => __( 'Close Editor', 'customize-posts' ),
),
);

wp_scripts()->add_data( 'customize-post-editor-control', 'data', sprintf( 'var _wpCustomizePostsEditorExports = %s;', wp_json_encode( $exports ) ) );
}

/**
* Enqueue a WP Editor instance we can use for rich text editing.
*/
public static function enqueue_editor() {
add_action( 'customize_controls_print_footer_scripts', array( __CLASS__, 'render_editor' ), 0 );

// Note that WP_Customize_Widgets::print_footer_scripts() happens at priority 10.
add_action( 'customize_controls_print_footer_scripts', array( __CLASS__, 'maybe_do_admin_print_footer_scripts' ), 20 );

// @todo These should be included in _WP_Editors::editor_settings()
if ( false === has_action( 'customize_controls_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ) ) ) {
add_action( 'customize_controls_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ) );
}
}

/**
* Render rich text editor.
*/
public static function render_editor() {
?>
<div id="customize-posts-content-editor-pane">
<div id="customize-posts-content-editor-dragbar">
<span class="screen-reader-text"><?php esc_html_e( 'Resize Editor', 'customize-posts' ); ?></span>
</div>
<h2 id="customize-posts-content-editor-title"></h2>

<?php
// The settings passed in here are derived from those used in edit-form-advanced.php.
wp_editor( '', 'customize-posts-content', array(
'_content_editor_dfw' => false,
'drag_drop_upload' => true,
'tabfocus_elements' => 'content-html,save-post',
'editor_height' => 200,
'default_editor' => 'tinymce',
'tinymce' => array(
'resize' => false,
'wp_autoresize_on' => false,
'add_unload_trigger' => false,
),
) );
?>

</div>
<?php
}

/**
* Do the admin_print_footer_scripts actions if not done already.
*
* Another possibility here is to opt-in selectively to the desired widgets
* via:
* Shortcode_UI::get_instance()->action_admin_enqueue_scripts();
* Shortcake_Bakery::get_instance()->action_admin_enqueue_scripts();
*
* Note that this action is also done in WP_Customize_Widgets::print_footer_scripts()
* at priority 10, so this method runs at a later priority to ensure the action is
* not done twice.
*
* @codeCoverageIgnore
*/
public static function maybe_do_admin_print_footer_scripts() {
if ( ! did_action( 'admin_print_footer_scripts' ) ) {
/** This action is documented in wp-admin/admin-footer.php */
do_action( 'admin_print_footer_scripts' );
}

if ( ! did_action( 'admin_footer-post.php' ) ) {
/** This action is documented in wp-admin/admin-footer.php */
do_action( 'admin_footer-post.php' );
}
}

/**
* Render the Underscore template for this control.
*
Expand Down
85 changes: 1 addition & 84 deletions php/class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ public function __construct( WP_Customize_Manager $manager ) {
require_once dirname( __FILE__ ) . '/class-wp-customize-post-date-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-status-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-editor-control.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-editor-control.php';
require_once ABSPATH . WPINC . '/customize/class-wp-customize-partial.php';
require_once dirname( __FILE__ ) . '/class-wp-customize-post-field-partial.php';

add_action( 'customize_controls_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'customize_controls_init', array( $this, 'enqueue_editor' ) );
add_action( 'customize_controls_enqueue_scripts', array( 'WP_Customize_Post_Editor_Control', 'enqueue_scripts' ) );

add_filter( 'customize_refresh_nonces', array( $this, 'add_customize_nonce' ) );
add_action( 'customize_register', array( $this, 'register_constructs' ), 20 );
Expand Down Expand Up @@ -630,10 +629,7 @@ public function enqueue_scripts() {
'fieldAuthorLabel' => __( 'Author', 'customize-posts' ),
'noTitle' => __( '(no title)', 'customize-posts' ),
'theirChange' => __( 'Their change: %s', 'customize-posts' ),
'openEditor' => __( 'Open Editor', 'customize-posts' ), // @todo Move this into editor control?
'closeEditor' => __( 'Close Editor', 'customize-posts' ),
'invalidDateError' => __( 'Whoops, the provided date is invalid.', 'customize-posts' ),

/* translators: %s post type */
'jumpToPostPlaceholder' => __( 'Jump to %s', 'customize-posts' ),
),
Expand Down Expand Up @@ -692,85 +688,6 @@ public function format_gmt_offset( $offset ) {
return $formatted_offset;
}

/**
* Enqueue a WP Editor instance we can use for rich text editing.
*
* @todo Consider moving this to WP_Customize_Post_Editor_Control::enqueue_scripts().
* @todo This can be added at the customize_controls_enqueue_scripts action.
*/
public function enqueue_editor() {
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_editor' ), 0 );

// Note that WP_Customize_Widgets::print_footer_scripts() happens at priority 10.
add_action( 'customize_controls_print_footer_scripts', array( $this, 'maybe_do_admin_print_footer_scripts' ), 20 );

// @todo These should be included in _WP_Editors::editor_settings()
if ( false === has_action( 'customize_controls_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ) ) ) {
add_action( 'customize_controls_print_footer_scripts', array( '_WP_Editors', 'enqueue_scripts' ) );
}
}

/**
* Render rich text editor.
*
* @todo Consider moving this to WP_Customize_Post_Editor_Control::enqueue_scripts().
*/
public function render_editor() {
?>
<div id="customize-posts-content-editor-pane">
<div id="customize-posts-content-editor-dragbar">
<span class="screen-reader-text"><?php esc_html_e( 'Resize Editor', 'customize-posts' ); ?></span>
</div>
<h2 id="customize-posts-content-editor-title"></h2>

<?php
// The settings passed in here are derived from those used in edit-form-advanced.php.
wp_editor( '', 'customize-posts-content', array(
'_content_editor_dfw' => false,
'drag_drop_upload' => true,
'tabfocus_elements' => 'content-html,save-post',
'editor_height' => 200,
'default_editor' => 'tinymce',
'tinymce' => array(
'resize' => false,
'wp_autoresize_on' => false,
'add_unload_trigger' => false,
),
) );
?>

</div>
<?php
}

/**
* Do the admin_print_footer_scripts actions if not done already.
*
* Another possibility here is to opt-in selectively to the desired widgets
* via:
* Shortcode_UI::get_instance()->action_admin_enqueue_scripts();
* Shortcake_Bakery::get_instance()->action_admin_enqueue_scripts();
*
* Note that this action is also done in WP_Customize_Widgets::print_footer_scripts()
* at priority 10, so this method runs at a later priority to ensure the action is
* not done twice.
*
* @todo Consider moving this to WP_Customize_Post_Editor_Control::enqueue_scripts().
*
* @codeCoverageIgnore
*/
public function maybe_do_admin_print_footer_scripts() {
if ( ! did_action( 'admin_print_footer_scripts' ) ) {
/** This action is documented in wp-admin/admin-footer.php */
do_action( 'admin_print_footer_scripts' );
}

if ( ! did_action( 'admin_footer-post.php' ) ) {
/** This action is documented in wp-admin/admin-footer.php */
do_action( 'admin_footer-post.php' );
}
}

/**
* Sanitize a value as a post ID.
*
Expand Down