This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathclass-wp-customize-post-editor-control.php
More file actions
161 lines (144 loc) · 4.75 KB
/
Copy pathclass-wp-customize-post-editor-control.php
File metadata and controls
161 lines (144 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* Customize Post Editor Control Class
*
* @package WordPress
* @subpackage Customize
*/
/**
* Class WP_Customize_Post_Editor_Control
*/
class WP_Customize_Post_Editor_Control extends WP_Customize_Dynamic_Control {
/**
* Posts component.
*
* @var WP_Customize_Posts
*/
public $posts_component;
/**
* Type of control, used by JS.
*
* @access public
* @var string
*/
public $type = 'post_editor';
/**
* Constructor.
*
* @throws Exception If posts component not available.
*
* @param WP_Customize_Manager $manager Manager.
* @param string $id Control id.
* @param array $args Control args.
*/
public function __construct( WP_Customize_Manager $manager, $id, array $args ) {
if ( ! isset( $manager->posts ) || ! ( $manager->posts instanceof WP_Customize_Posts ) ) {
throw new Exception( 'Missing Posts component.' );
}
$this->posts_component = $manager->posts;
parent::__construct( $manager, $id, $args );
}
/**
* Enqueue control related scripts/styles.
*/
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() {
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.
*
* @access protected
* @codeCoverageIgnore
*/
protected function content_template() {
$data = $this->json();
?>
<#
_.defaults( data, <?php echo wp_json_encode( $data ) ?> );
data.input_id = 'input-' + String( Math.random() );
#>
<span class="customize-control-title"><label for="{{ data.input_id }}">{{ data.label }}</label></span>
<# if ( data.description ) { #>
<span class="description customize-control-description">{{ data.description }}</span>
<# } #>
<button id="{{ data.input_id }}" type="button" class="button toggle-post-editor"><?php esc_html_e( 'Open Editor', 'customize-posts' ) ?></button>
<?php
}
}