Summery Summery
Generate and return code editor settings.
Syntax Syntax
Parameters Parameters
- $args
-
(Required) Args.
- 'type'
(string) The MIME type of the file to be edited. - 'file'
(string) Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to$type
param. - 'theme'
(WP_Theme) Theme being edited when on theme editor. - 'plugin'
(string) Plugin being edited when on plugin editor. - 'codemirror'
(array) Additional CodeMirror setting overrides. - 'csslint'
(array) CSSLint rule overrides. - 'jshint'
(array) JSHint rule overrides. - 'htmlhint'
(array) JSHint rule overrides.
- 'type'
Return Return
(array|false) Settings for the code editor.
Source Source
File: wp-includes/general-template.php
* @uses _WP_Editors * @since 4.8.0 */ function wp_enqueue_editor() { if ( ! class_exists( '_WP_Editors', false ) ) { require ABSPATH . WPINC . '/class-wp-editor.php'; } _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_get_code_editor_settings(); * @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 WP_Theme $theme Theme being edited when on theme editor. * @type string $plugin Plugin being edited when on plugin editor. * @type array $codemirror Additional CodeMirror setting overrides. * @type array $csslint CSSLint rule overrides. * @type array $jshint JSHint rule overrides. * @type array $htmlhint JSHint rule overrides. * } * @return 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 = wp_get_code_editor_settings( $args ); if ( empty( $settings ) || empty( $settings['codemirror'] ) ) { return false; } wp_enqueue_script( 'code-editor' ); wp_enqueue_style( 'code-editor' ); 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; } /** * Generate and return code editor settings. * * @since 5.0.0 * * @see wp_enqueue_code_editor() * * @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 WP_Theme $theme Theme being edited when on theme editor. * @type string $plugin Plugin being edited when on plugin editor. * @type array $codemirror Additional CodeMirror setting overrides. * @type array $csslint CSSLint rule overrides. * @type array $jshint JSHint rule overrides. * @type array $htmlhint JSHint rule overrides. * } * @return array|false Settings for the code editor. */ function wp_get_code_editor_settings( $args ) { $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', 'Ctrl-F' => 'findPersistent', 'Cmd-F' => 'findPersistent', ), 'direction' => 'ltr', // Code is shown in LTR even in RTL languages. 'gutters' => array(), ), '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' => false, '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 ( in_array( $type, array( 'text/css', 'text/x-scss', 'text/x-less', 'text/x-sass' ), true ) ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => $type, 'lint' => false, '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,
Advertisement
Changelog Changelog
Version | Description |
---|---|
5.0.0 | Introduced. |