Editor: Add CodeMirror-powered code editor with syntax highlighting, linting, and auto-completion.

* Code editor is integrated into the Theme/Plugin Editor, Additional CSS in Customizer, and Custom HTML widget. Code editor is not yet integrated into the post editor, and it may not be until accessibility concerns are addressed.
* The CodeMirror component in the Custom HTML widget is integrated in a similar way to TinyMCE being integrated into the Text widget, adopting the same approach for integrating dynamic JavaScript-initialized fields.
* Linting is performed for JS, CSS, HTML, and JSON via JSHint, CSSLint, HTMLHint, and JSONLint respectively. Linting is not yet supported for PHP.
* When user lacks `unfiltered_html` the capability, the Custom HTML widget will report any Kses-invalid elements and attributes as errors via a custom Kses rule for HTMLHint.
* When linting errors are detected, the user will be prevented from saving the code until the errors are fixed, reducing instances of broken websites.
* The placeholder value is removed from Custom CSS in favor of a fleshed-out section description which now auto-expands when the CSS field is empty. See #39892.
* The CodeMirror library is included as `wp.CodeMirror` to prevent conflicts with any existing `CodeMirror` global.
* An `wp.codeEditor.initialize()` API in JS is provided to convert a `textarea` into CodeMirror, with a `wp_enqueue_code_editor()` function in PHP to manage enqueueing the assets and settings needed to edit a given type of code.
* A user preference is added to manage whether or not "syntax highlighting" is enabled. The feature is opt-out, being enabled by default.
* Allowed file extensions in the theme and plugin editors have been updated to include formats which CodeMirror has modes for: `conf`, `css`, `diff`, `patch`, `html`, `htm`, `http`, `js`, `json`, `jsx`, `less`, `md`, `php`, `phtml`, `php3`, `php4`, `php5`, `php7`, `phps`, `scss`, `sass`, `sh`, `bash`, `sql`, `svg`, `xml`, `yml`, `yaml`, `txt`.

Props westonruter, georgestephanis, obenland, melchoyce, pixolin, mizejewski, michelleweber, afercia, grahamarmfield, samikeijonen, rianrietveld, iseulde.
See #38707.
Fixes #12423, #39892.


git-svn-id: https://develop.svn.wordpress.org/trunk@41376 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Weston Ruter
2017-09-13 06:07:48 +00:00
parent 1b7475645a
commit dbace684e2
32 changed files with 37795 additions and 348 deletions
+1
View File
@@ -128,6 +128,7 @@ function the_modified_author() {
* - plugins_last_view
* - plugins_per_page
* - rich_editing
* - syntax_highlighting
* - user_activation_key
* - user_description
* - user_email
+63 -8
View File
@@ -212,6 +212,21 @@ final class WP_Customize_Manager {
*/
private $_changeset_data;
/**
* Code Editor Settings for Custom CSS.
*
* This variable contains the settings returned by `wp_enqueue_code_editor()` which are then later output
* to the client in `WP_Customize_Manager::customize_pane_settings()`. A value of false means that the
* Custom CSS section or control was removed, or that the Syntax Highlighting user pref was turned off.
*
* @see wp_enqueue_code_editor()
* @see WP_Customize_Manager::enqueue_control_scripts()
* @see WP_Customize_Manager::customize_pane_settings()
* @since 4.9.0
* @var array|false
*/
private $_custom_css_code_editor_settings = false;
/**
* Constructor.
*
@@ -3322,6 +3337,12 @@ final class WP_Customize_Manager {
foreach ( $this->controls as $control ) {
$control->enqueue();
}
if ( $this->get_section( 'custom_css' ) && $this->get_control( 'custom_css' ) ) {
$this->_custom_css_code_editor_settings = wp_enqueue_code_editor( array(
'type' => 'text/css',
) );
}
}
/**
@@ -3579,6 +3600,9 @@ final class WP_Customize_Manager {
'stylesheet' => $this->get_stylesheet(),
'active' => $this->is_theme_active(),
),
'customCss' => array(
'codeEditor' => $this->_custom_css_code_editor_settings,
),
'url' => array(
'preview' => esc_url_raw( $this->get_preview_url() ),
'parent' => esc_url_raw( admin_url() ),
@@ -4177,22 +4201,53 @@ final class WP_Customize_Manager {
) );
/* Custom CSS */
$section_description = '<p>';
$section_description .= __( 'Add your own CSS code here to customize the appearance and layout of your site.', 'better-code-editing' );
$section_description .= sprintf(
' <a href="%1$s" class="external-link" target="_blank">%2$s<span class="screen-reader-text">%3$s</span></a>',
esc_url( __( 'https://codex.wordpress.org/CSS', 'default' ) ),
__( 'Learn more about CSS', 'default' ),
/* translators: accessibility text */
__( '(opens in a new window)', 'default' )
);
$section_description .= '</p>';
$section_description .= '<p>' . __( 'When using a keyboard to navigate:', 'better-code-editing' ) . '</p>';
$section_description .= '<ul>';
$section_description .= '<li>' . __( 'In the CSS edit field, Tab enters a tab character.', 'better-code-editing' ) . '</li>';
$section_description .= '<li>' . __( 'To move keyboard focus, press Esc then Tab for the next element, or Esc then Shift+Tab for the previous element.', 'better-code-editing' ) . '</li>';
$section_description .= '</ul>';
if ( 'false' !== wp_get_current_user()->syntax_highlighting ) {
$section_description .= '<p>';
$section_description .= sprintf(
/* translators: placeholder is link to user profile */
__( 'The edit field automatically highlights code syntax. You can disable this in your %s to work in plain text mode.', 'better-code-editing' ),
sprintf(
' <a href="%1$s" class="external-link" target="_blank">%2$s<span class="screen-reader-text">%3$s</span></a>',
esc_url( get_edit_profile_url() . '#syntax_highlighting' ),
__( 'user profile', 'better-code-editing' ),
/* translators: accessibility text */
__( '(opens in a new window)', 'default' )
)
);
$section_description .= '</p>';
}
$section_description .= '<p class="section-description-buttons">';
$section_description .= '<button type="button" class="button-link section-description-close">' . __( 'Close', 'default' ) . '</button>';
$section_description .= '</p>';
$this->add_section( 'custom_css', array(
'title' => __( 'Additional CSS' ),
'priority' => 200,
'description_hidden' => true,
'description' => sprintf( '%s<br /><a href="%s" class="external-link" target="_blank">%s<span class="screen-reader-text">%s</span></a>',
__( 'CSS allows you to customize the appearance and layout of your site with code. Separate CSS is saved for each of your themes. In the editing area the Tab key enters a tab character. To move below this area by pressing Tab, press the Esc key followed by the Tab key.' ),
esc_url( __( 'https://codex.wordpress.org/CSS' ) ),
__( 'Learn more about CSS' ),
/* translators: accessibility text */
__( '(opens in a new window)' )
),
'description' => $section_description,
) );
$custom_css_setting = new WP_Customize_Custom_CSS_Setting( $this, sprintf( 'custom_css[%s]', get_stylesheet() ), array(
'capability' => 'edit_css',
'default' => sprintf( "/*\n%s\n*/", __( "You can add your own CSS here.\n\nClick the help icon above to learn more." ) ),
'default' => '',
) );
$this->add_setting( $custom_css_setting );
+2
View File
@@ -32,6 +32,8 @@
* @property string $spam
* @property string $deleted
* @property string $locale
* @property string $rich_editing
* @property string $syntax_highlighting
*/
class WP_User {
/**
@@ -148,9 +148,8 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
* Checks for imbalanced braces, brackets, and comments.
* Notifications are rendered when the customizer state is saved.
*
* @todo There are cases where valid CSS can be incorrectly marked as invalid when strings or comments include balancing characters. To fix, CSS tokenization needs to be used.
*
* @since 4.7.0
* @since 4.9.0 Checking for balanced characters has been moved client-side via linting in code editor.
*
* @param string $css The input string.
* @return true|WP_Error True if the input was validated, otherwise WP_Error.
@@ -162,94 +161,6 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
$validity->add( 'illegal_markup', __( 'Markup is not allowed in CSS.' ) );
}
$imbalanced = false;
// Make sure that there is a closing brace for each opening brace.
if ( ! $this->validate_balanced_characters( '{', '}', $css ) ) {
$validity->add( 'imbalanced_curly_brackets', sprintf(
/* translators: 1: {}, 2: }, 3: { */
__( 'Your curly brackets %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.' ),
'<code>{}</code>',
'<code>}</code>',
'<code>{</code>'
) );
$imbalanced = true;
}
// Ensure brackets are balanced.
if ( ! $this->validate_balanced_characters( '[', ']', $css ) ) {
$validity->add( 'imbalanced_braces', sprintf(
/* translators: 1: [], 2: ], 3: [ */
__( 'Your brackets %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.' ),
'<code>[]</code>',
'<code>]</code>',
'<code>[</code>'
) );
$imbalanced = true;
}
// Ensure parentheses are balanced.
if ( ! $this->validate_balanced_characters( '(', ')', $css ) ) {
$validity->add( 'imbalanced_parentheses', sprintf(
/* translators: 1: (), 2: ), 3: ( */
__( 'Your parentheses %1$s are imbalanced. Make sure there is a closing %2$s for every opening %3$s.' ),
'<code>()</code>',
'<code>)</code>',
'<code>(</code>'
) );
$imbalanced = true;
}
// Ensure double quotes are equal.
if ( ! $this->validate_equal_characters( '"', $css ) ) {
$validity->add( 'unequal_double_quotes', sprintf(
/* translators: 1: " (double quote) */
__( 'Your double quotes %1$s are uneven. Make sure there is a closing %1$s for every opening %1$s.' ),
'<code>"</code>'
) );
$imbalanced = true;
}
/*
* Make sure any code comments are closed properly.
*
* The first check could miss stray an unpaired comment closing figure, so if
* The number appears to be balanced, then check for equal numbers
* of opening/closing comment figures.
*
* Although it may initially appear redundant, we use the first method
* to give more specific feedback to the user.
*/
$unclosed_comment_count = $this->validate_count_unclosed_comments( $css );
if ( 0 < $unclosed_comment_count ) {
$validity->add( 'unclosed_comment', sprintf(
/* translators: 1: number of unclosed comments, 2: */ */
_n(
'There is %1$s unclosed code comment. Close each comment with %2$s.',
'There are %1$s unclosed code comments. Close each comment with %2$s.',
$unclosed_comment_count
),
$unclosed_comment_count,
'<code>*/</code>'
) );
$imbalanced = true;
} elseif ( ! $this->validate_balanced_characters( '/*', '*/', $css ) ) {
$validity->add( 'imbalanced_comments', sprintf(
/* translators: 1: */, 2: /* */
__( 'There is an extra %1$s, indicating an end to a comment. Be sure that there is an opening %2$s for every closing %1$s.' ),
'<code>*/</code>',
'<code>/*</code>'
) );
$imbalanced = true;
}
if ( $imbalanced && $this->is_possible_content_error( $css ) ) {
$validity->add( 'possible_false_positive', sprintf(
/* translators: %s: content: ""; */
__( 'Imbalanced/unclosed character errors can be caused by %s declarations. You may need to remove this or add it to a custom CSS file.' ),
'<code>content: "";</code>'
) );
}
if ( empty( $validity->errors ) ) {
$validity = parent::validate( $css );
}
@@ -285,109 +196,4 @@ final class WP_Customize_Custom_CSS_Setting extends WP_Customize_Setting {
return $post_id;
}
/**
* Ensure there are a balanced number of paired characters.
*
* This is used to check that the number of opening and closing
* characters is equal.
*
* For instance, there should be an equal number of braces ("{", "}")
* in the CSS.
*
* @since 4.7.0
*
* @param string $opening_char The opening character.
* @param string $closing_char The closing character.
* @param string $css The CSS input string.
*
* @return bool
*/
private function validate_balanced_characters( $opening_char, $closing_char, $css ) {
return substr_count( $css, $opening_char ) === substr_count( $css, $closing_char );
}
/**
* Ensure there are an even number of paired characters.
*
* This is used to check that the number of a specific
* character is even.
*
* For instance, there should be an even number of double quotes
* in the CSS.
*
* @since 4.7.0
*
* @param string $char A character.
* @param string $css The CSS input string.
* @return bool Equality.
*/
private function validate_equal_characters( $char, $css ) {
$char_count = substr_count( $css, $char );
return ( 0 === $char_count % 2 );
}
/**
* Count unclosed CSS Comments.
*
* Used during validation.
*
* @see self::validate()
*
* @since 4.7.0
*
* @param string $css The CSS input string.
* @return int Count.
*/
private function validate_count_unclosed_comments( $css ) {
$count = 0;
$comments = explode( '/*', $css );
if ( ! is_array( $comments ) || ( 1 >= count( $comments ) ) ) {
return $count;
}
unset( $comments[0] ); // The first item is before the first comment.
foreach ( $comments as $comment ) {
if ( false === strpos( $comment, '*/' ) ) {
$count++;
}
}
return $count;
}
/**
* Find "content:" within a string.
*
* Imbalanced/Unclosed validation errors may be caused
* when a character is used in a "content:" declaration.
*
* This function is used to detect if this is a possible
* cause of the validation error, so that if it is,
* a notification may be added to the Validation Errors.
*
* Example:
* .element::before {
* content: "(\"";
* }
* .element::after {
* content: "\")";
* }
*
* Using ! empty() because strpos() may return non-boolean values
* that evaluate to false. This would be problematic when
* using a strict "false === strpos()" comparison.
*
* @since 4.7.0
*
* @param string $css The CSS input string.
* @return bool
*/
private function is_possible_content_error( $css ) {
$found = preg_match( '/\bcontent\s*:/', $css );
if ( ! empty( $found ) ) {
return true;
}
return false;
}
}
+388
View File
@@ -3115,6 +3115,394 @@ function wp_enqueue_editor() {
_WP_Editors::enqueue_default_editor();
}
/**
* Enqueue assets needed by the code editor for the given settings.
*
* @since 4.9.0
*
* @see wp_enqueue_editor()
* @see _WP_Editors::parse_settings()
* @param array $args {
* Args.
*
* @type string $type The MIME type of the file to be edited.
* @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
* @type array $settings Settings to merge on top of defaults which derive from `$type` or `$file` args.
* @type WP_Theme $theme Theme being edited when on theme editor.
* @type string $plugin Plugin being edited when on plugin editor.
* }
* @returns array|false Settings for the enqueued code editor, or false if the editor was not enqueued .
*/
function wp_enqueue_code_editor( $args ) {
if ( is_user_logged_in() && 'false' === wp_get_current_user()->syntax_highlighting ) {
return false;
}
$settings = array(
'codemirror' => array(
'indentUnit' => 4,
'indentWithTabs' => true,
'inputStyle' => 'contenteditable',
'lineNumbers' => true,
'lineWrapping' => true,
'styleActiveLine' => true,
'continueComments' => true,
'extraKeys' => array(
'Ctrl-Space' => 'autocomplete',
'Ctrl-/' => 'toggleComment',
'Cmd-/' => 'toggleComment',
'Alt-F' => 'findPersistent',
),
'direction' => 'ltr', // Code is shown in LTR even in RTL languages.
),
'csslint' => array(
'errors' => true, // Parsing errors.
'box-model' => true,
'display-property-grouping' => true,
'duplicate-properties' => true,
'known-properties' => true,
'outline-none' => true,
),
'jshint' => array(
// The following are copied from <https://github.com/WordPress/wordpress-develop/blob/4.8.1/.jshintrc>.
'boss' => true,
'curly' => true,
'eqeqeq' => true,
'eqnull' => true,
'es3' => true,
'expr' => true,
'immed' => true,
'noarg' => true,
'nonbsp' => true,
'onevar' => true,
'quotmark' => 'single',
'trailing' => true,
'undef' => true,
'unused' => true,
'browser' => true,
'globals' => array(
'_' => false,
'Backbone' => false,
'jQuery' => false,
'JSON' => false,
'wp' => false,
),
),
'htmlhint' => array(
'tagname-lowercase' => true,
'attr-lowercase' => true,
'attr-value-double-quotes' => true,
'doctype-first' => false,
'tag-pair' => true,
'spec-char-escape' => true,
'id-unique' => true,
'src-not-empty' => true,
'attr-no-duplication' => true,
'alt-require' => true,
'space-tab-mixed-disabled' => 'tab',
'attr-unsafe-chars' => true,
),
);
$type = '';
if ( isset( $args['type'] ) ) {
$type = $args['type'];
// Remap MIME types to ones that CodeMirror modes will recognize.
if ( 'application/x-patch' === $type || 'text/x-patch' === $type ) {
$type = 'text/x-diff';
}
} elseif ( isset( $args['file'] ) && false !== strpos( basename( $args['file'] ), '.' ) ) {
$extension = strtolower( pathinfo( $args['file'], PATHINFO_EXTENSION ) );
foreach ( wp_get_mime_types() as $exts => $mime ) {
if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
$type = $mime;
break;
}
}
// Supply any types that are not matched by wp_get_mime_types().
if ( empty( $type ) ) {
switch ( $extension ) {
case 'conf':
$type = 'text/nginx';
break;
case 'css':
$type = 'text/css';
break;
case 'diff':
case 'patch':
$type = 'text/x-diff';
break;
case 'html':
case 'htm':
$type = 'text/html';
break;
case 'http':
$type = 'message/http';
break;
case 'js':
$type = 'text/javascript';
break;
case 'json':
$type = 'application/json';
break;
case 'jsx':
$type = 'text/jsx';
break;
case 'less':
$type = 'text/x-less';
break;
case 'md':
$type = 'text/x-gfm';
break;
case 'php':
case 'phtml':
case 'php3':
case 'php4':
case 'php5':
case 'php7':
case 'phps':
$type = 'application/x-httpd-php';
break;
case 'scss':
$type = 'text/x-scss';
break;
case 'sass':
$type = 'text/x-sass';
break;
case 'sh':
case 'bash':
$type = 'text/x-sh';
break;
case 'sql':
$type = 'text/x-sql';
break;
case 'svg':
$type = 'application/svg+xml';
break;
case 'xml':
$type = 'text/xml';
break;
case 'yml':
case 'yaml':
$type = 'text/x-yaml';
break;
case 'txt':
default:
$type = 'text/plain';
break;
}
}
}
if ( 'text/css' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'css',
'lint' => true,
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );
} elseif ( 'text/x-scss' === $type || 'text/x-less' === $type || 'text/x-sass' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => $type,
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );
} elseif ( 'text/x-diff' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'diff',
) );
} elseif ( 'text/html' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'htmlmixed',
'lint' => true,
'autoCloseBrackets' => true,
'autoCloseTags' => true,
'matchTags' => array(
'bothTags' => true,
),
) );
if ( ! current_user_can( 'unfiltered_html' ) ) {
$settings['htmlhint']['kses'] = wp_kses_allowed_html( 'post' );
}
} elseif ( 'text/x-gfm' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'gfm',
'highlightFormatting' => true,
) );
} elseif ( 'application/javascript' === $type || 'text/javascript' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'javascript',
'lint' => true,
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );
} elseif ( false !== strpos( $type, 'json' ) ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => array(
'name' => 'javascript',
),
'lint' => true,
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );
if ( 'application/ld+json' === $type ) {
$settings['codemirror']['mode']['jsonld'] = true;
} else {
$settings['codemirror']['mode']['json'] = true;
}
} elseif ( false !== strpos( $type, 'jsx' ) ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'jsx',
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );
} elseif ( 'text/x-markdown' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'markdown',
'highlightFormatting' => true,
) );
} elseif ( 'text/nginx' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'nginx',
) );
} elseif ( 'application/x-httpd-php' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'php',
'autoCloseBrackets' => true,
'autoCloseTags' => true,
'matchBrackets' => true,
'matchTags' => array(
'bothTags' => true,
),
) );
} elseif ( 'text/x-sql' === $type || 'text/x-mysql' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'sql',
'autoCloseBrackets' => true,
'matchBrackets' => true,
) );
} elseif ( false !== strpos( $type, 'xml' ) ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'xml',
'autoCloseBrackets' => true,
'autoCloseTags' => true,
'matchTags' => array(
'bothTags' => true,
),
) );
} elseif ( 'text/x-yaml' === $type ) {
$settings['codemirror'] = array_merge( $settings['codemirror'], array(
'mode' => 'yaml',
) );
} else {
$settings['codemirror']['mode'] = $type;
}
if ( ! empty( $settings['codemirror']['lint'] ) ) {
$settings['codemirror']['gutters'][] = 'CodeMirror-lint-markers';
}
// Let settings supplied via args override any defaults.
if ( isset( $args['settings'] ) ) {
foreach ( $args['settings'] as $key => $value ) {
$settings[ $key ] = array_merge(
$settings[ $key ],
$value
);
}
}
/**
* Filters settings that are passed into the code editor.
*
* Returning a falsey value will disable the syntax-highlighting code editor.
*
* @since 4.9.0
*
* @param array $settings The array of settings passed to the code editor. A falsey value disables the editor.
* @param array $args {
* Args passed when calling `wp_enqueue_code_editor()`.
*
* @type string $type The MIME type of the file to be edited.
* @type string $file Filename being edited.
* @type array $settings Settings to merge on top of defaults which derive from `$type` or `$file` args.
* @type WP_Theme $theme Theme being edited when on theme editor.
* @type string $plugin Plugin being edited when on plugin editor.
* }
*/
$settings = apply_filters( 'wp_code_editor_settings', $settings, $args );
if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
return false;
}
wp_enqueue_script( 'code-editor' );
wp_enqueue_style( 'code-editor' );
wp_enqueue_script( 'codemirror' );
wp_enqueue_style( 'codemirror' );
if ( isset( $settings['codemirror']['mode'] ) ) {
$mode = $settings['codemirror']['mode'];
if ( is_string( $mode ) ) {
$mode = array(
'name' => $mode,
);
}
if ( ! empty( $settings['codemirror']['lint'] ) ) {
switch ( $mode['name'] ) {
case 'css':
case 'text/css':
case 'text/x-scss':
case 'text/x-less':
wp_enqueue_script( 'csslint' );
break;
case 'htmlmixed':
case 'text/html':
case 'php':
case 'application/x-httpd-php':
case 'text/x-php':
wp_enqueue_script( 'htmlhint' );
wp_enqueue_script( 'csslint' );
wp_enqueue_script( 'jshint' );
if ( ! current_user_can( 'unfiltered_html' ) ) {
wp_enqueue_script( 'htmlhint-kses' );
}
break;
case 'javascript':
case 'application/ecmascript':
case 'application/json':
case 'application/javascript':
case 'application/ld+json':
case 'text/typescript':
case 'application/typescript':
wp_enqueue_script( 'jshint' );
wp_enqueue_script( 'jsonlint' );
break;
}
}
}
wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
/**
* Fires when scripts and styles are enqueued for the code editor.
*
* @since 4.9.0
*
* @param array $settings Settings for the enqueued code editor.
*/
do_action( 'wp_enqueue_code_editor', $settings );
return $settings;
}
/**
* Retrieves the contents of the search WordPress query variable.
*
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,30 @@
/* global HTMLHint */
/* eslint no-magic-numbers: ["error", { "ignore": [0, 1] }] */
HTMLHint.addRule({
id: 'kses',
description: 'Element or attribute cannot be used.',
init: function( parser, reporter, options ) {
'use strict';
var self = this;
parser.addListener( 'tagstart', function( event ) {
var attr, col, attrName, allowedAttributes, i, len, tagName;
tagName = event.tagName.toLowerCase();
if ( ! options[ tagName ] ) {
reporter.error( 'Tag <' + event.tagName + '> is not allowed.', event.line, event.col, self, event.raw );
return;
}
allowedAttributes = options[ tagName ];
col = event.col + event.tagName.length + 1;
for ( i = 0, len = event.attrs.length; i < len; i++ ) {
attr = event.attrs[ i ];
attrName = attr.name.toLowerCase();
if ( ! allowedAttributes[ attrName ] ) {
reporter.error( 'Tag attribute [' + attr.raw + ' ] is not allowed.', event.line, col + attr.index, self, attr.raw );
}
}
});
}
});
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+432
View File
@@ -0,0 +1,432 @@
/* Jison generated parser */
var jsonlint = (function(){
var parser = {trace: function trace() { },
yy: {},
symbols_: {"error":2,"JSONString":3,"STRING":4,"JSONNumber":5,"NUMBER":6,"JSONNullLiteral":7,"NULL":8,"JSONBooleanLiteral":9,"TRUE":10,"FALSE":11,"JSONText":12,"JSONValue":13,"EOF":14,"JSONObject":15,"JSONArray":16,"{":17,"}":18,"JSONMemberList":19,"JSONMember":20,":":21,",":22,"[":23,"]":24,"JSONElementList":25,"$accept":0,"$end":1},
terminals_: {2:"error",4:"STRING",6:"NUMBER",8:"NULL",10:"TRUE",11:"FALSE",14:"EOF",17:"{",18:"}",21:":",22:",",23:"[",24:"]"},
productions_: [0,[3,1],[5,1],[7,1],[9,1],[9,1],[12,2],[13,1],[13,1],[13,1],[13,1],[13,1],[13,1],[15,2],[15,3],[20,3],[19,1],[19,3],[16,2],[16,3],[25,1],[25,3]],
performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
var $0 = $$.length - 1;
switch (yystate) {
case 1: // replace escaped characters with actual character
this.$ = yytext.replace(/\\(\\|")/g, "$"+"1")
.replace(/\\n/g,'\n')
.replace(/\\r/g,'\r')
.replace(/\\t/g,'\t')
.replace(/\\v/g,'\v')
.replace(/\\f/g,'\f')
.replace(/\\b/g,'\b');
break;
case 2:this.$ = Number(yytext);
break;
case 3:this.$ = null;
break;
case 4:this.$ = true;
break;
case 5:this.$ = false;
break;
case 6:return this.$ = $$[$0-1];
break;
case 13:this.$ = {};
break;
case 14:this.$ = $$[$0-1];
break;
case 15:this.$ = [$$[$0-2], $$[$0]];
break;
case 16:this.$ = {}; this.$[$$[$0][0]] = $$[$0][1];
break;
case 17:this.$ = $$[$0-2]; $$[$0-2][$$[$0][0]] = $$[$0][1];
break;
case 18:this.$ = [];
break;
case 19:this.$ = $$[$0-1];
break;
case 20:this.$ = [$$[$0]];
break;
case 21:this.$ = $$[$0-2]; $$[$0-2].push($$[$0]);
break;
}
},
table: [{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],12:1,13:2,15:7,16:8,17:[1,14],23:[1,15]},{1:[3]},{14:[1,16]},{14:[2,7],18:[2,7],22:[2,7],24:[2,7]},{14:[2,8],18:[2,8],22:[2,8],24:[2,8]},{14:[2,9],18:[2,9],22:[2,9],24:[2,9]},{14:[2,10],18:[2,10],22:[2,10],24:[2,10]},{14:[2,11],18:[2,11],22:[2,11],24:[2,11]},{14:[2,12],18:[2,12],22:[2,12],24:[2,12]},{14:[2,3],18:[2,3],22:[2,3],24:[2,3]},{14:[2,4],18:[2,4],22:[2,4],24:[2,4]},{14:[2,5],18:[2,5],22:[2,5],24:[2,5]},{14:[2,1],18:[2,1],21:[2,1],22:[2,1],24:[2,1]},{14:[2,2],18:[2,2],22:[2,2],24:[2,2]},{3:20,4:[1,12],18:[1,17],19:18,20:19},{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],13:23,15:7,16:8,17:[1,14],23:[1,15],24:[1,21],25:22},{1:[2,6]},{14:[2,13],18:[2,13],22:[2,13],24:[2,13]},{18:[1,24],22:[1,25]},{18:[2,16],22:[2,16]},{21:[1,26]},{14:[2,18],18:[2,18],22:[2,18],24:[2,18]},{22:[1,28],24:[1,27]},{22:[2,20],24:[2,20]},{14:[2,14],18:[2,14],22:[2,14],24:[2,14]},{3:20,4:[1,12],20:29},{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],13:30,15:7,16:8,17:[1,14],23:[1,15]},{14:[2,19],18:[2,19],22:[2,19],24:[2,19]},{3:5,4:[1,12],5:6,6:[1,13],7:3,8:[1,9],9:4,10:[1,10],11:[1,11],13:31,15:7,16:8,17:[1,14],23:[1,15]},{18:[2,17],22:[2,17]},{18:[2,15],22:[2,15]},{22:[2,21],24:[2,21]}],
defaultActions: {16:[2,6]},
parseError: function parseError(str, hash) {
throw new Error(str);
},
parse: function parse(input) {
var self = this,
stack = [0],
vstack = [null], // semantic value stack
lstack = [], // location stack
table = this.table,
yytext = '',
yylineno = 0,
yyleng = 0,
recovering = 0,
TERROR = 2,
EOF = 1;
//this.reductionCount = this.shiftCount = 0;
this.lexer.setInput(input);
this.lexer.yy = this.yy;
this.yy.lexer = this.lexer;
if (typeof this.lexer.yylloc == 'undefined')
this.lexer.yylloc = {};
var yyloc = this.lexer.yylloc;
lstack.push(yyloc);
if (typeof this.yy.parseError === 'function')
this.parseError = this.yy.parseError;
function popStack (n) {
stack.length = stack.length - 2*n;
vstack.length = vstack.length - n;
lstack.length = lstack.length - n;
}
function lex() {
var token;
token = self.lexer.lex() || 1; // $end = 1
// if token isn't its numeric value, convert
if (typeof token !== 'number') {
token = self.symbols_[token] || token;
}
return token;
}
var symbol, preErrorSymbol, state, action, a, r, yyval={},p,len,newState, expected;
while (true) {
// retreive state number from top of stack
state = stack[stack.length-1];
// use default actions if available
if (this.defaultActions[state]) {
action = this.defaultActions[state];
} else {
if (symbol == null)
symbol = lex();
// read action for current state and first input
action = table[state] && table[state][symbol];
}
// handle parse error
_handle_error:
if (typeof action === 'undefined' || !action.length || !action[0]) {
if (!recovering) {
// Report error
expected = [];
for (p in table[state]) if (this.terminals_[p] && p > 2) {
expected.push("'"+this.terminals_[p]+"'");
}
var errStr = '';
if (this.lexer.showPosition) {
errStr = 'Parse error on line '+(yylineno+1)+":\n"+this.lexer.showPosition()+"\nExpecting "+expected.join(', ') + ", got '" + this.terminals_[symbol]+ "'";
} else {
errStr = 'Parse error on line '+(yylineno+1)+": Unexpected " +
(symbol == 1 /*EOF*/ ? "end of input" :
("'"+(this.terminals_[symbol] || symbol)+"'"));
}
this.parseError(errStr,
{text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
}
// just recovered from another error
if (recovering == 3) {
if (symbol == EOF) {
throw new Error(errStr || 'Parsing halted.');
}
// discard current lookahead and grab another
yyleng = this.lexer.yyleng;
yytext = this.lexer.yytext;
yylineno = this.lexer.yylineno;
yyloc = this.lexer.yylloc;
symbol = lex();
}
// try to recover from error
while (1) {
// check for error recovery rule in this state
if ((TERROR.toString()) in table[state]) {
break;
}
if (state == 0) {
throw new Error(errStr || 'Parsing halted.');
}
popStack(1);
state = stack[stack.length-1];
}
preErrorSymbol = symbol; // save the lookahead token
symbol = TERROR; // insert generic error symbol as new lookahead
state = stack[stack.length-1];
action = table[state] && table[state][TERROR];
recovering = 3; // allow 3 real symbols to be shifted before reporting a new error
}
// this shouldn't happen, unless resolve defaults are off
if (action[0] instanceof Array && action.length > 1) {
throw new Error('Parse Error: multiple actions possible at state: '+state+', token: '+symbol);
}
switch (action[0]) {
case 1: // shift
//this.shiftCount++;
stack.push(symbol);
vstack.push(this.lexer.yytext);
lstack.push(this.lexer.yylloc);
stack.push(action[1]); // push state
symbol = null;
if (!preErrorSymbol) { // normal execution/no error
yyleng = this.lexer.yyleng;
yytext = this.lexer.yytext;
yylineno = this.lexer.yylineno;
yyloc = this.lexer.yylloc;
if (recovering > 0)
recovering--;
} else { // error just occurred, resume old lookahead f/ before error
symbol = preErrorSymbol;
preErrorSymbol = null;
}
break;
case 2: // reduce
//this.reductionCount++;
len = this.productions_[action[1]][1];
// perform semantic action
yyval.$ = vstack[vstack.length-len]; // default to $$ = $1
// default location, uses first token for firsts, last for lasts
yyval._$ = {
first_line: lstack[lstack.length-(len||1)].first_line,
last_line: lstack[lstack.length-1].last_line,
first_column: lstack[lstack.length-(len||1)].first_column,
last_column: lstack[lstack.length-1].last_column
};
r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
if (typeof r !== 'undefined') {
return r;
}
// pop off stack
if (len) {
stack = stack.slice(0,-1*len*2);
vstack = vstack.slice(0, -1*len);
lstack = lstack.slice(0, -1*len);
}
stack.push(this.productions_[action[1]][0]); // push nonterminal (reduce)
vstack.push(yyval.$);
lstack.push(yyval._$);
// goto new state = table[STATE][NONTERMINAL]
newState = table[stack[stack.length-2]][stack[stack.length-1]];
stack.push(newState);
break;
case 3: // accept
return true;
}
}
return true;
}};
/* Jison generated lexer */
var lexer = (function(){
var lexer = ({EOF:1,
parseError:function parseError(str, hash) {
if (this.yy.parseError) {
this.yy.parseError(str, hash);
} else {
throw new Error(str);
}
},
setInput:function (input) {
this._input = input;
this._more = this._less = this.done = false;
this.yylineno = this.yyleng = 0;
this.yytext = this.matched = this.match = '';
this.conditionStack = ['INITIAL'];
this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
return this;
},
input:function () {
var ch = this._input[0];
this.yytext+=ch;
this.yyleng++;
this.match+=ch;
this.matched+=ch;
var lines = ch.match(/\n/);
if (lines) this.yylineno++;
this._input = this._input.slice(1);
return ch;
},
unput:function (ch) {
this._input = ch + this._input;
return this;
},
more:function () {
this._more = true;
return this;
},
less:function (n) {
this._input = this.match.slice(n) + this._input;
},
pastInput:function () {
var past = this.matched.substr(0, this.matched.length - this.match.length);
return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
},
upcomingInput:function () {
var next = this.match;
if (next.length < 20) {
next += this._input.substr(0, 20-next.length);
}
return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
},
showPosition:function () {
var pre = this.pastInput();
var c = new Array(pre.length + 1).join("-");
return pre + this.upcomingInput() + "\n" + c+"^";
},
next:function () {
if (this.done) {
return this.EOF;
}
if (!this._input) this.done = true;
var token,
match,
tempMatch,
index,
col,
lines;
if (!this._more) {
this.yytext = '';
this.match = '';
}
var rules = this._currentRules();
for (var i=0;i < rules.length; i++) {
tempMatch = this._input.match(this.rules[rules[i]]);
if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
match = tempMatch;
index = i;
if (!this.options.flex) break;
}
}
if (match) {
lines = match[0].match(/\n.*/g);
if (lines) this.yylineno += lines.length;
this.yylloc = {first_line: this.yylloc.last_line,
last_line: this.yylineno+1,
first_column: this.yylloc.last_column,
last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length}
this.yytext += match[0];
this.match += match[0];
this.yyleng = this.yytext.length;
this._more = false;
this._input = this._input.slice(match[0].length);
this.matched += match[0];
token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
if (this.done && this._input) this.done = false;
if (token) return token;
else return;
}
if (this._input === "") {
return this.EOF;
} else {
this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
{text: "", token: null, line: this.yylineno});
}
},
lex:function lex() {
var r = this.next();
if (typeof r !== 'undefined') {
return r;
} else {
return this.lex();
}
},
begin:function begin(condition) {
this.conditionStack.push(condition);
},
popState:function popState() {
return this.conditionStack.pop();
},
_currentRules:function _currentRules() {
return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
},
topState:function () {
return this.conditionStack[this.conditionStack.length-2];
},
pushState:function begin(condition) {
this.begin(condition);
}});
lexer.options = {};
lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
var YYSTATE=YY_START
switch($avoiding_name_collisions) {
case 0:/* skip whitespace */
break;
case 1:return 6
break;
case 2:yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2); return 4
break;
case 3:return 17
break;
case 4:return 18
break;
case 5:return 23
break;
case 6:return 24
break;
case 7:return 22
break;
case 8:return 21
break;
case 9:return 10
break;
case 10:return 11
break;
case 11:return 8
break;
case 12:return 14
break;
case 13:return 'INVALID'
break;
}
};
lexer.rules = [/^(?:\s+)/,/^(?:(-?([0-9]|[1-9][0-9]+))(\.[0-9]+)?([eE][-+]?[0-9]+)?\b)/,/^(?:"(?:\\[\\"bfnrt/]|\\u[a-fA-F0-9]{4}|[^\\\0-\x09\x0a-\x1f"])*")/,/^(?:\{)/,/^(?:\})/,/^(?:\[)/,/^(?:\])/,/^(?:,)/,/^(?::)/,/^(?:true\b)/,/^(?:false\b)/,/^(?:null\b)/,/^(?:$)/,/^(?:.)/];
lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13],"inclusive":true}};
;
return lexer;})()
parser.lexer = lexer;
return parser;
})();
if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
exports.parser = jsonlint;
exports.parse = function () { return jsonlint.parse.apply(jsonlint, arguments); }
exports.main = function commonjsMain(args) {
if (!args[1])
throw new Error('Usage: '+args[0]+' FILE');
if (typeof process !== 'undefined') {
var source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8");
} else {
var cwd = require("file").path(require("file").cwd());
var source = cwd.join(args[1]).read({charset: "utf-8"});
}
return exports.parser.parse(source);
}
if (typeof module !== 'undefined' && require.main === module) {
exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
}
}
+22 -1
View File
@@ -466,6 +466,20 @@ function wp_default_scripts( &$scripts ) {
apply_filters( 'mejs_settings', $mejs_settings )
);
$scripts->add( 'codemirror', '/wp-includes/js/codemirror/codemirror.min.js', array(), '5.29.1-alpha-ee20357' );
$scripts->add( 'csslint', '/wp-includes/js/codemirror/csslint.js', array(), '1.0.5' );
$scripts->add( 'jshint', '/wp-includes/js/codemirror/jshint.js', array(), '2.9.5' );
$scripts->add( 'jsonlint', '/wp-includes/js/codemirror/jsonlint.js', array(), '1.6.2' );
$scripts->add( 'htmlhint', '/wp-includes/js/codemirror/htmlhint.js', array(), '0.9.14-xwp' );
$scripts->add( 'htmlhint-kses', '/wp-includes/js/codemirror/htmlhint-kses.js', array( 'htmlhint' ) );
$scripts->add( 'code-editor', "/wp-admin/js/code-editor$suffix.js", array( 'jquery', 'codemirror' ) );
$scripts->add( 'wp-theme-plugin-editor', "/wp-admin/js/theme-plugin-editor$suffix.js", array( 'code-editor', 'jquery', 'jquery-ui-core', 'wp-a11y', 'underscore' ) );
did_action( 'init' ) && $scripts->add_inline_script( 'wp-theme-plugin-editor', sprintf( 'wp.themePluginEditor.l10n = %s;', wp_json_encode( wp_array_slice_assoc(
/* translators: placeholder is error count */
_n_noop( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.' ),
array( 'singular', 'plural' )
) ) ) );
$scripts->add( 'wp-playlist', "/wp-includes/js/mediaelement/wp-playlist$suffix.js", array( 'wp-util', 'backbone', 'mediaelement' ), false, 1 );
$scripts->add( 'zxcvbn-async', "/wp-includes/js/zxcvbn-async$suffix.js", array(), '1.0' );
@@ -551,6 +565,11 @@ function wp_default_scripts( &$scripts ) {
'serverSaveError' => __( 'Failed connecting to the server. Please try saving again.' ),
// Used for overriding the file types allowed in plupload.
'allowedFiles' => __( 'Allowed Files' ),
'customCssError' => wp_array_slice_assoc(
/* translators: placeholder is error count */
_n_noop( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.' ),
array( 'singular', 'plural' )
),
) );
$scripts->add( 'customize-selective-refresh', "/wp-includes/js/customize-selective-refresh$suffix.js", array( 'jquery', 'wp-util', 'customize-preview' ), false, 1 );
@@ -688,7 +707,7 @@ function wp_default_scripts( &$scripts ) {
$scripts->add( 'media-image-widget', "/wp-admin/js/widgets/media-image-widget$suffix.js", array( 'media-widgets' ) );
$scripts->add( 'media-video-widget', "/wp-admin/js/widgets/media-video-widget$suffix.js", array( 'media-widgets', 'media-audiovideo', 'wp-api-request' ) );
$scripts->add( 'text-widgets', "/wp-admin/js/widgets/text-widgets$suffix.js", array( 'jquery', 'backbone', 'editor', 'wp-util', 'wp-a11y' ) );
$scripts->add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );
$scripts->add( 'custom-html-widgets', "/wp-admin/js/widgets/custom-html-widgets$suffix.js", array( 'code-editor', 'jquery', 'backbone', 'wp-util', 'jquery-ui-core', 'wp-a11y' ) );
$scripts->add( 'theme', "/wp-admin/js/theme$suffix.js", array( 'wp-backbone', 'wp-a11y' ), false, 1 );
@@ -929,6 +948,7 @@ function wp_default_styles( &$styles ) {
$styles->add( 'widgets', "/wp-admin/css/widgets$suffix.css", array( 'wp-pointer' ) );
$styles->add( 'site-icon', "/wp-admin/css/site-icon$suffix.css" );
$styles->add( 'l10n', "/wp-admin/css/l10n$suffix.css" );
$styles->add( 'code-editor', "/wp-admin/css/code-editor$suffix.css", array( 'codemirror' ) );
$styles->add( 'wp-admin', false, array( 'dashicons', 'common', 'forms', 'admin-menu', 'dashboard', 'list-tables', 'edit', 'revisions', 'media', 'themes', 'about', 'nav-menus', 'widgets', 'site-icon', 'l10n' ) );
@@ -963,6 +983,7 @@ function wp_default_styles( &$styles ) {
$styles->add( 'mediaelement', "/wp-includes/js/mediaelement/mediaelementplayer-legacy.min.css", array(), '4.2.5-74e01a40' );
$styles->add( 'wp-mediaelement', "/wp-includes/js/mediaelement/wp-mediaelement$suffix.css", array( 'mediaelement' ) );
$styles->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.css', array( 'dashicons' ) );
$styles->add( 'codemirror', '/wp-includes/js/codemirror/codemirror.min.css', array(), '5.29.1-alpha-ee20357' );
// Deprecated CSS
$styles->add( 'deprecated-media', "/wp-admin/css/deprecated-media$suffix.css" );
+7 -2
View File
@@ -1376,7 +1376,7 @@ function validate_username( $username ) {
* Insert a user into the database.
*
* Most of the `$userdata` array fields have filters associated with the values. Exceptions are
* 'ID', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl',
* 'ID', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', 'use_ssl',
* 'user_registered', and 'role'. The filters have the prefix 'pre_user_' followed by the field
* name. An example using 'description' would have the filter called, 'pre_user_description' that
* can be hooked into.
@@ -1410,6 +1410,8 @@ function validate_username( $username ) {
* @type string $description The user's biographical description.
* @type string|bool $rich_editing Whether to enable the rich-editor for the user.
* False if not empty.
* @type string|bool $syntax_highlighting Whether to enable the rich code editor for the user.
* False if not empty.
* @type string|bool $comment_shortcuts Whether to enable comment moderation keyboard
* shortcuts for the user. Default false.
* @type string $admin_color Admin color scheme for the user. Default 'fresh'.
@@ -1623,6 +1625,8 @@ function wp_insert_user( $userdata ) {
$meta['rich_editing'] = empty( $userdata['rich_editing'] ) ? 'true' : $userdata['rich_editing'];
$meta['syntax_highlighting'] = empty( $userdata['syntax_highlighting'] ) ? 'true' : $userdata['syntax_highlighting'];
$meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true';
$admin_color = empty( $userdata['admin_color'] ) ? 'fresh' : $userdata['admin_color'];
@@ -1709,6 +1713,7 @@ function wp_insert_user( $userdata ) {
* @type string $last_name The user's last name.
* @type string $description The user's description.
* @type bool $rich_editing Whether to enable the rich-editor for the user. False if not empty.
* @type bool $syntax_highlighting Whether to enable the rich code editor for the user. False if not empty.
* @type bool $comment_shortcuts Whether to enable keyboard shortcuts for the user. Default false.
* @type string $admin_color The color scheme for a user's admin screen. Default 'fresh'.
* @type int|bool $use_ssl Whether to force SSL on the user's admin area. 0|false if SSL is
@@ -2035,7 +2040,7 @@ function wp_create_user($username, $password, $email = '') {
* @return array List of user keys to be populated in wp_update_user().
*/
function _get_additional_user_keys( $user ) {
$keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'locale' );
$keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'locale' );
return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) );
}
@@ -16,6 +16,14 @@
*/
class WP_Widget_Custom_HTML extends WP_Widget {
/**
* Whether or not the widget has been registered yet.
*
* @since 4.9.0
* @var bool
*/
protected $registered = false;
/**
* Default instance.
*
@@ -45,6 +53,33 @@ class WP_Widget_Custom_HTML extends WP_Widget {
parent::__construct( 'custom_html', __( 'Custom HTML' ), $widget_ops, $control_ops );
}
/**
* Add hooks for enqueueing assets when registering all widget instances of this widget class.
*
* @since 4.9.0
*
* @param integer $number Optional. The unique order number of this widget instance
* compared to other instances of the same class. Default -1.
*/
public function _register_one( $number = -1 ) {
parent::_register_one( $number );
if ( $this->registered ) {
return;
}
$this->registered = true;
wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.idBases.push( %s );', wp_json_encode( $this->id_base ) ) );
// Note that the widgets component in the customizer will also do the 'admin_print_scripts-widgets.php' action in WP_Customize_Widgets::print_scripts().
add_action( 'admin_print_scripts-widgets.php', array( $this, 'enqueue_admin_scripts' ) );
// Note that the widgets component in the customizer will also do the 'admin_footer-widgets.php' action in WP_Customize_Widgets::print_footer_scripts().
add_action( 'admin_footer-widgets.php', array( 'WP_Widget_Custom_HTML', 'render_control_template_scripts' ) );
// Note this action is used to ensure the help text is added to the end.
add_action( 'admin_head-widgets.php', array( 'WP_Widget_Custom_HTML', 'add_help_text' ) );
}
/**
* Outputs the content for the current Custom HTML widget instance.
*
@@ -117,40 +152,128 @@ class WP_Widget_Custom_HTML extends WP_Widget {
return $instance;
}
/**
* Loads the required scripts and styles for the widget control.
*
* @since 4.9.0
*/
public function enqueue_admin_scripts() {
$settings = wp_enqueue_code_editor( array(
'type' => 'text/html',
) );
wp_enqueue_script( 'custom-html-widgets' );
if ( empty( $settings ) ) {
$settings = array(
'disabled' => true,
);
}
wp_add_inline_script( 'custom-html-widgets', sprintf( 'wp.customHtmlWidgets.init( %s );', wp_json_encode( $settings ) ), 'after' );
$l10n = array(
'errorNotice' => wp_array_slice_assoc(
/* translators: placeholder is error count */
_n_noop( 'There is %d error which must be fixed before you can save.', 'There are %d errors which must be fixed before you can save.' ),
array( 'singular', 'plural' )
),
);
wp_add_inline_script( 'custom-html-widgets', sprintf( 'jQuery.extend( wp.customHtmlWidgets.l10n, %s );', wp_json_encode( $l10n ) ), 'after' );
}
/**
* Outputs the Custom HTML widget settings form.
*
* @since 4.8.1
* @since 4.9.0 The form contains only hidden sync inputs. For the control UI, see `WP_Widget_Custom_HTML::render_control_template_scripts()`.
*
* @see WP_Widget_Custom_HTML::render_control_template_scripts()
* @param array $instance Current instance.
* @returns void
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, $this->default_instance );
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'content' ); ?>"><?php _e( 'Content:' ); ?></label>
<textarea class="widefat code" rows="16" cols="20" id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>"><?php echo esc_textarea( $instance['content'] ); ?></textarea>
</p>
<?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
<?php
$probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
$allowed_html = wp_kses_allowed_html( 'post' );
$disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
?>
<?php if ( ! empty( $disallowed_html ) ) : ?>
<p>
<?php _e( 'Some HTML tags are not permitted, including:' ); ?>
<code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
</p>
<?php endif; ?>
<?php endif; ?>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title sync-input" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>"/>
<textarea id="<?php echo $this->get_field_id( 'content' ); ?>" name="<?php echo $this->get_field_name( 'content' ); ?>" class="content sync-input" hidden><?php echo esc_textarea( $instance['content'] ); ?></textarea>
<?php
}
/**
* Render form template scripts.
*
* @since 4.9.0
*/
public static function render_control_template_scripts() {
?>
<script type="text/html" id="tmpl-widget-custom-html-control-fields">
<# var elementIdPrefix = 'el' + String( Math.random() ).replace( /\D/g, '' ) + '_' #>
<p>
<label for="{{ elementIdPrefix }}title"><?php esc_html_e( 'Title:', 'default' ); ?></label>
<input id="{{ elementIdPrefix }}title" type="text" class="widefat title">
</p>
<p>
<label for="{{ elementIdPrefix }}content" class="screen-reader-text"><?php esc_html_e( 'Content:', 'default' ); ?></label>
<textarea id="{{ elementIdPrefix }}content" class="widefat code content" rows="16" cols="20"></textarea>
</p>
<?php if ( ! current_user_can( 'unfiltered_html' ) ) : ?>
<?php
$probably_unsafe_html = array( 'script', 'iframe', 'form', 'input', 'style' );
$allowed_html = wp_kses_allowed_html( 'post' );
$disallowed_html = array_diff( $probably_unsafe_html, array_keys( $allowed_html ) );
?>
<?php if ( ! empty( $disallowed_html ) ) : ?>
<# if ( data.codeEditorDisabled ) { #>
<p>
<?php _e( 'Some HTML tags are not permitted, including:', 'default' ); ?>
<code><?php echo join( '</code>, <code>', $disallowed_html ); ?></code>
</p>
<# } #>
<?php endif; ?>
<?php endif; ?>
<div class="code-editor-error-container"></div>
</script>
<?php
}
/**
* Add help text to widgets admin screen.
*
* @since 4.9.0
*/
public static function add_help_text() {
$screen = get_current_screen();
$content = '<p>';
$content .= __( 'Use the Custom HTML widget to add arbitrary HTML code to your widget areas.' );
$content .= '</p>';
$content .= '<p>' . __( 'When using a keyboard to navigate:' ) . '</p>';
$content .= '<ul>';
$content .= '<li>' . __( 'In the HTML edit field, Tab enters a tab character.' ) . '</li>';
$content .= '<li>' . __( 'To move keyboard focus, press Esc then Tab for the next element, or Esc then Shift+Tab for the previous element.' ) . '</li>';
$content .= '</ul>';
$content .= '<p>';
$content .= sprintf(
/* translators: placeholder is link to user profile */
__( 'The edit field automatically highlights code syntax. You can disable this in your %s to work in plan text mode.' ),
sprintf(
' <a href="%1$s" class="external-link" target="_blank">%2$s<span class="screen-reader-text">%3$s</span></a>',
esc_url( get_edit_profile_url() . '#syntax_highlighting' ),
__( 'user profile' ),
/* translators: accessibility text */
__( '(opens in a new window)', 'default' )
)
);
$content .= '</p>';
$screen->add_help_tab( array(
'id' => 'custom_html_widget',
'title' => __( 'Custom HTML Widget' ),
'content' => $content,
) );
}
}
@@ -353,6 +353,7 @@ class WP_Widget_Text extends WP_Widget {
public function enqueue_admin_scripts() {
wp_enqueue_editor();
wp_enqueue_script( 'text-widgets' );
wp_add_inline_script( 'text-widgets', 'wp.textWidgets.init();', 'after' );
}
/**