Skip to content

Commit ba8ff86

Browse files
committed
Explore refactor of wp-editor code, adds set_modifiable_text()
1 parent 2b2b29a commit ba8ff86

File tree

2 files changed

+103
-26
lines changed

2 files changed

+103
-26
lines changed

src/wp-includes/class-wp-editor.php

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,8 @@ public static function parse_settings( $editor_id, $settings ) {
158158
*/
159159
public static function editor( $content, $editor_id, $settings = array() ) {
160160
$set = self::parse_settings( $editor_id, $settings );
161-
$editor_class = ' class="' . trim( esc_attr( $set['editor_class'] ) . ' wp-editor-area' ) . '"';
162-
$tabindex = $set['tabindex'] ? ' tabindex="' . (int) $set['tabindex'] . '"' : '';
163161
$default_editor = 'html';
164162
$buttons = '';
165-
$autocomplete = '';
166-
$editor_id_attr = esc_attr( $editor_id );
167163

168164
if ( $set['drag_drop_upload'] ) {
169165
self::$drag_drop_upload = true;
@@ -180,19 +176,23 @@ public static function editor( $content, $editor_id, $settings = array() ) {
180176
}
181177

182178
if ( self::$this_tinymce ) {
183-
$autocomplete = ' autocomplete="off"';
184-
185179
if ( self::$this_quicktags ) {
186180
$default_editor = $set['default_editor'] ? $set['default_editor'] : wp_default_editor();
187181
// 'html' is used for the "Text" editor tab.
188182
if ( 'html' !== $default_editor ) {
189183
$default_editor = 'tinymce';
190184
}
191185

192-
$buttons .= '<button type="button" id="' . $editor_id_attr . '-tmce" class="wp-switch-editor switch-tmce"' .
193-
' data-wp-editor-id="' . $editor_id_attr . '">' . _x( 'Visual', 'Name for the Visual editor tab' ) . "</button>\n";
194-
$buttons .= '<button type="button" id="' . $editor_id_attr . '-html" class="wp-switch-editor switch-html"' .
195-
' data-wp-editor-id="' . $editor_id_attr . '">' . _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ) . "</button>\n";
186+
$buttons .= WP_HTML::render( <<<HTML
187+
<button type="button" id="</%id>-tmce" class="wp-switch-editor switch-tmce" data-wp-editor-id="</%id>"></%visual_label></button>
188+
<button type="button" id="</%id>-html" class="wp-switch-editor switch-html" data-wp-editor-id="</%id>"></%text_label></button>
189+
HTML,
190+
array(
191+
'id' => $editor_id,
192+
'text_label' => _x( 'Text', 'Name for the Text editor tab (formerly HTML)' ),
193+
'visual_label' => _x( 'Visual', 'Name for the Visual editor tab' ),
194+
)
195+
);
196196
} else {
197197
$default_editor = 'tinymce';
198198
}
@@ -201,11 +201,13 @@ public static function editor( $content, $editor_id, $settings = array() ) {
201201
$switch_class = 'html' === $default_editor ? 'html-active' : 'tmce-active';
202202
$wrap_class = 'wp-core-ui wp-editor-wrap ' . $switch_class;
203203

204-
if ( $set['_content_editor_dfw'] ) {
205-
$wrap_class .= ' has-dfw';
206-
}
207-
208-
echo '<div id="wp-' . $editor_id_attr . '-wrap" class="' . $wrap_class . '">';
204+
echo WP_HTML::render(
205+
'<div id="wp-</%id>-wrap" class="wp-core-jui wp-editor-wrap </%has_dfw>">',
206+
array(
207+
'id' => $editor_id,
208+
'has_dfw' => $set['_content_editor_dfw'] ? 'has-dfw' : '',
209+
)
210+
);
209211

210212
if ( self::$editor_buttons_css ) {
211213
wp_print_styles( 'editor-buttons' );
@@ -217,7 +219,10 @@ public static function editor( $content, $editor_id, $settings = array() ) {
217219
}
218220

219221
if ( ! empty( $buttons ) || $set['media_buttons'] ) {
220-
echo '<div id="wp-' . $editor_id_attr . '-editor-tools" class="wp-editor-tools hide-if-no-js">';
222+
echo WP_HTML::render(
223+
'<div id="wp-</%id>-editor-tools" class="wp-editor-tools hide-if-no-js">',
224+
array( 'id' => $editor_id )
225+
);
221226

222227
if ( $set['media_buttons'] ) {
223228
self::$has_medialib = true;
@@ -226,7 +231,10 @@ public static function editor( $content, $editor_id, $settings = array() ) {
226231
require ABSPATH . 'wp-admin/includes/media.php';
227232
}
228233

229-
echo '<div id="wp-' . $editor_id_attr . '-media-buttons" class="wp-media-buttons">';
234+
echo WP_HTML::render(
235+
'<div id="wp-</%id>-media-button" class="wp-media-buttons">',
236+
array( 'id' => $editor_id )
237+
);
230238

231239
/**
232240
* Fires after the default media button(s) are displayed.
@@ -249,10 +257,13 @@ public static function editor( $content, $editor_id, $settings = array() ) {
249257
if ( 'content' === $editor_id && ! empty( $GLOBALS['current_screen'] ) && 'post' === $GLOBALS['current_screen']->base ) {
250258
$toolbar_id = 'ed_toolbar';
251259
} else {
252-
$toolbar_id = 'qt_' . $editor_id_attr . '_toolbar';
260+
$toolbar_id = 'qt_' . $editor_id . '_toolbar';
253261
}
254262

255-
$quicktags_toolbar = '<div id="' . $toolbar_id . '" class="quicktags-toolbar hide-if-no-js"></div>';
263+
$quicktags_toolbar = WP_HTML::render(
264+
'<div id="</%id>" class="quicktags-toolbar hide-if-no-js"></div>',
265+
array( 'id' => $toolbar_id )
266+
);
256267
}
257268

258269
/**
@@ -264,10 +275,28 @@ public static function editor( $content, $editor_id, $settings = array() ) {
264275
*/
265276
$the_editor = apply_filters(
266277
'the_editor',
267-
'<div id="wp-' . $editor_id_attr . '-editor-container" class="wp-editor-container">' .
278+
WP_HTML::render(
279+
'<div id="wp-</%id>-editor-container" class="wp-editor-container">',
280+
array( 'id' => $editor_id )
281+
) .
268282
$quicktags_toolbar .
269-
'<textarea' . $editor_class . $height . $tabindex . $autocomplete . ' cols="40" name="' . esc_attr( $set['textarea_name'] ) . '" ' .
270-
'id="' . $editor_id_attr . '">%s</textarea></div>'
283+
WP_HTML::render(
284+
<<<'HTML'
285+
<textarea class="</%editor_class>" ...height tabindex="</%tabindex>"
286+
autocomplete="</%autocomplete>" cols="40" name="</%name>" id="</%id>">%s</textarea>
287+
HTML,
288+
array(
289+
'autocomplete' => self::$this_tinymce ? 'off' : null,
290+
'editor_class' => trim( "{$set['editor_class']} wp-editor-area" ),
291+
'height' => ! empty( $set['editor_height'] )
292+
? array( 'style' => "height: {$set['editor_height']}px;" )
293+
: array( 'rows' => (string) $set['textarea_rows'] ),
294+
'id' => $editor_id,
295+
'name' => $set['textarea_name'],
296+
'tabindex' => $set['tabindex'] ? (string) $set['tabindex'] : null,
297+
)
298+
) .
299+
'</div>'
271300
);
272301

273302
// Prepare the content for the Visual or Text editor, only when TinyMCE is used (back-compat).
@@ -300,12 +329,16 @@ public static function editor( $content, $editor_id, $settings = array() ) {
300329
$content = apply_filters_deprecated( 'richedit_pre', array( $content ), '4.3.0', 'format_for_editor' );
301330
}
302331

303-
if ( false !== stripos( $content, 'textarea' ) ) {
304-
$content = preg_replace( '%</textarea%i', '&lt;/textarea', $content );
332+
$processor = new WP_HTML_Tag_Processor( $the_editor );
333+
while ( $processor->next_tag( 'TEXTAREA' ) ) {
334+
if ( $editor_id === $processor->get_attribute( 'id' ) ) {
335+
$processor->set_modifiable_text( $content );
336+
break;
337+
}
305338
}
339+
$the_editor = $processor->get_updated_html();
306340

307-
printf( $the_editor, $content );
308-
echo "\n</div>\n\n";
341+
echo "{$the_editor}\n</div>\n\n";
309342

310343
self::editor_settings( $editor_id, $set );
311344
}

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,6 +2805,50 @@ public function get_modifiable_text() {
28052805
return $decoded;
28062806
}
28072807

2808+
/**
2809+
* Sets the modifiable text for the matched token, if possible.
2810+
*
2811+
* @param string $text Replace the modifiable text with this string.
2812+
* @return bool Whether the modifiable text was updated.
2813+
*/
2814+
public function set_modifiable_text( $text ) {
2815+
if ( null === $this->text_starts_at || ! is_string( $text ) ) {
2816+
return false;
2817+
}
2818+
2819+
switch ( $this->get_token_name() ) {
2820+
case '#text':
2821+
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
2822+
$this->text_starts_at,
2823+
$this->text_length,
2824+
esc_html( $text )
2825+
);
2826+
break;
2827+
2828+
case 'TEXTAREA':
2829+
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
2830+
$this->text_starts_at,
2831+
$this->text_length,
2832+
preg_replace( '~</textarea~i', '&lt;/textarea', $text )
2833+
);
2834+
break;
2835+
2836+
case 'TITLE':
2837+
$this->lexical_updates[] = new WP_HTML_Text_Replacement(
2838+
$this->text_starts_at,
2839+
$this->text_length,
2840+
preg_replace( '~</title~i', '&lt;/title', $text )
2841+
);
2842+
break;
2843+
2844+
default:
2845+
return false;
2846+
}
2847+
2848+
$this->get_updated_html();
2849+
return true;
2850+
}
2851+
28082852
/**
28092853
* Updates or creates a new attribute on the currently matched tag with the passed value.
28102854
*

0 commit comments

Comments
 (0)