Make WordPress Core

Changeset 44121


Ignore:
Timestamp:
12/13/2018 08:11:09 PM (6 years ago)
Author:
desrosj
Message:

General: Extract the code editor settings from wp_enqueue_code_editor().

They're now returned by a new function, wp_get_code_editor_settings(), so they can be reused by the block editor.

Props pento.

Merges [43761] to trunk.

Fixes #45127.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/src/wp-includes/general-template.php

    r44021 r44121  
    31993199 *
    32003200 * @see wp_enqueue_editor()
     3201 * @see wp_get_code_editor_settings();
    32013202 * @see _WP_Editors::parse_settings()
     3203 *
    32023204 * @param array $args {
    32033205 *     Args.
     
    32123214 *     @type array    $htmlhint   JSHint rule overrides.
    32133215 * }
    3214  * @returns array|false Settings for the enqueued code editor, or false if the editor was not enqueued .
     3216 * @return array|false Settings for the enqueued code editor, or false if the editor was not enqueued.
    32153217 */
    32163218function wp_enqueue_code_editor( $args ) {
     
    32193221    }
    32203222
     3223    $settings = wp_get_code_editor_settings( $args );
     3224
     3225    if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
     3226        return false;
     3227    }
     3228
     3229    wp_enqueue_script( 'code-editor' );
     3230    wp_enqueue_style( 'code-editor' );
     3231
     3232    if ( isset( $settings['codemirror']['mode'] ) ) {
     3233        $mode = $settings['codemirror']['mode'];
     3234        if ( is_string( $mode ) ) {
     3235            $mode = array(
     3236                'name' => $mode,
     3237            );
     3238        }
     3239
     3240        if ( ! empty( $settings['codemirror']['lint'] ) ) {
     3241            switch ( $mode['name'] ) {
     3242                case 'css':
     3243                case 'text/css':
     3244                case 'text/x-scss':
     3245                case 'text/x-less':
     3246                    wp_enqueue_script( 'csslint' );
     3247                    break;
     3248                case 'htmlmixed':
     3249                case 'text/html':
     3250                case 'php':
     3251                case 'application/x-httpd-php':
     3252                case 'text/x-php':
     3253                    wp_enqueue_script( 'htmlhint' );
     3254                    wp_enqueue_script( 'csslint' );
     3255                    wp_enqueue_script( 'jshint' );
     3256                    if ( ! current_user_can( 'unfiltered_html' ) ) {
     3257                        wp_enqueue_script( 'htmlhint-kses' );
     3258                    }
     3259                    break;
     3260                case 'javascript':
     3261                case 'application/ecmascript':
     3262                case 'application/json':
     3263                case 'application/javascript':
     3264                case 'application/ld+json':
     3265                case 'text/typescript':
     3266                case 'application/typescript':
     3267                    wp_enqueue_script( 'jshint' );
     3268                    wp_enqueue_script( 'jsonlint' );
     3269                    break;
     3270            }
     3271        }
     3272    }
     3273
     3274    wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
     3275
     3276    /**
     3277     * Fires when scripts and styles are enqueued for the code editor.
     3278     *
     3279     * @since 4.9.0
     3280     *
     3281     * @param array $settings Settings for the enqueued code editor.
     3282     */
     3283    do_action( 'wp_enqueue_code_editor', $settings );
     3284
     3285    return $settings;
     3286}
     3287
     3288/**
     3289 * Generate and return code editor settings.
     3290 *
     3291 * @since 5.0.0
     3292 *
     3293 * @see wp_enqueue_code_editor()
     3294 *
     3295 * @param array $args {
     3296 *     Args.
     3297 *
     3298 *     @type string   $type       The MIME type of the file to be edited.
     3299 *     @type string   $file       Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
     3300 *     @type WP_Theme $theme      Theme being edited when on theme editor.
     3301 *     @type string   $plugin     Plugin being edited when on plugin editor.
     3302 *     @type array    $codemirror Additional CodeMirror setting overrides.
     3303 *     @type array    $csslint    CSSLint rule overrides.
     3304 *     @type array    $jshint     JSHint rule overrides.
     3305 *     @type array    $htmlhint   JSHint rule overrides.
     3306 * }
     3307 * @return array|false Settings for the code editor.
     3308 */
     3309function wp_get_code_editor_settings( $args ) {
    32213310    $settings = array(
    32223311        'codemirror' => array(
     
    35513640     * @param array $settings The array of settings passed to the code editor. A falsey value disables the editor.
    35523641     * @param array $args {
    3553      *     Args passed when calling `wp_enqueue_code_editor()`.
     3642     *     Args passed when calling `get_code_editor_settings()`.
    35543643     *
    35553644     *     @type string   $type       The MIME type of the file to be edited.
     
    35633652     * }
    35643653     */
    3565     $settings = apply_filters( 'wp_code_editor_settings', $settings, $args );
    3566 
    3567     if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
    3568         return false;
    3569     }
    3570 
    3571     wp_enqueue_script( 'code-editor' );
    3572     wp_enqueue_style( 'code-editor' );
    3573 
    3574     if ( isset( $settings['codemirror']['mode'] ) ) {
    3575         $mode = $settings['codemirror']['mode'];
    3576         if ( is_string( $mode ) ) {
    3577             $mode = array(
    3578                 'name' => $mode,
    3579             );
    3580         }
    3581 
    3582         if ( ! empty( $settings['codemirror']['lint'] ) ) {
    3583             switch ( $mode['name'] ) {
    3584                 case 'css':
    3585                 case 'text/css':
    3586                 case 'text/x-scss':
    3587                 case 'text/x-less':
    3588                     wp_enqueue_script( 'csslint' );
    3589                     break;
    3590                 case 'htmlmixed':
    3591                 case 'text/html':
    3592                 case 'php':
    3593                 case 'application/x-httpd-php':
    3594                 case 'text/x-php':
    3595                     wp_enqueue_script( 'htmlhint' );
    3596                     wp_enqueue_script( 'csslint' );
    3597                     wp_enqueue_script( 'jshint' );
    3598                     if ( ! current_user_can( 'unfiltered_html' ) ) {
    3599                         wp_enqueue_script( 'htmlhint-kses' );
    3600                     }
    3601                     break;
    3602                 case 'javascript':
    3603                 case 'application/ecmascript':
    3604                 case 'application/json':
    3605                 case 'application/javascript':
    3606                 case 'application/ld+json':
    3607                 case 'text/typescript':
    3608                 case 'application/typescript':
    3609                     wp_enqueue_script( 'jshint' );
    3610                     wp_enqueue_script( 'jsonlint' );
    3611                     break;
    3612             }
    3613         }
    3614     }
    3615 
    3616     wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
    3617 
    3618     /**
    3619      * Fires when scripts and styles are enqueued for the code editor.
    3620      *
    3621      * @since 4.9.0
    3622      *
    3623      * @param array $settings Settings for the enqueued code editor.
    3624      */
    3625     do_action( 'wp_enqueue_code_editor', $settings );
    3626 
    3627     return $settings;
     3654    return apply_filters( 'wp_code_editor_settings', $settings, $args );
    36283655}
    36293656
Note: See TracChangeset for help on using the changeset viewer.