Make WordPress Core

Changeset 43761


Ignore:
Timestamp:
10/19/2018 06:44:16 AM (8 years ago)
Author:
pento
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.

See #45127.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/general-template.php

    r43444 r43761  
    31223122 *
    31233123 * @see wp_enqueue_editor()
     3124 * @see wp_get_code_editor_settings();
    31243125 * @see _WP_Editors::parse_settings()
     3126 *
    31253127 * @param array $args {
    31263128 *     Args.
    … …  
    31353137 *     @type array    $htmlhint   JSHint rule overrides.
    31363138 * }
    3137  * @returns array|false Settings for the enqueued code editor, or false if the editor was not enqueued .
     3139 * @return array|false Settings for the enqueued code editor, or false if the editor was not enqueued.
    31383140 */
    31393141function wp_enqueue_code_editor( $args ) {
    … …  
    31423144        }
    31433145
     3146        $settings = wp_get_code_editor_settings( $args );
     3147
     3148        if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
     3149                return false;
     3150        }
     3151
     3152        wp_enqueue_script( 'code-editor' );
     3153        wp_enqueue_style( 'code-editor' );
     3154
     3155        if ( isset( $settings['codemirror']['mode'] ) ) {
     3156                $mode = $settings['codemirror']['mode'];
     3157                if ( is_string( $mode ) ) {
     3158                        $mode = array(
     3159                                'name' => $mode,
     3160                        );
     3161                }
     3162
     3163                if ( ! empty( $settings['codemirror']['lint'] ) ) {
     3164                        switch ( $mode['name'] ) {
     3165                                case 'css':
     3166                                case 'text/css':
     3167                                case 'text/x-scss':
     3168                                case 'text/x-less':
     3169                                        wp_enqueue_script( 'csslint' );
     3170                                        break;
     3171                                case 'htmlmixed':
     3172                                case 'text/html':
     3173                                case 'php':
     3174                                case 'application/x-httpd-php':
     3175                                case 'text/x-php':
     3176                                        wp_enqueue_script( 'htmlhint' );
     3177                                        wp_enqueue_script( 'csslint' );
     3178                                        wp_enqueue_script( 'jshint' );
     3179                                        if ( ! current_user_can( 'unfiltered_html' ) ) {
     3180                                                wp_enqueue_script( 'htmlhint-kses' );
     3181                                        }
     3182                                        break;
     3183                                case 'javascript':
     3184                                case 'application/ecmascript':
     3185                                case 'application/json':
     3186                                case 'application/javascript':
     3187                                case 'application/ld+json':
     3188                                case 'text/typescript':
     3189                                case 'application/typescript':
     3190                                        wp_enqueue_script( 'jshint' );
     3191                                        wp_enqueue_script( 'jsonlint' );
     3192                                        break;
     3193                        }
     3194                }
     3195        }
     3196
     3197        wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
     3198
     3199        /**
     3200         * Fires when scripts and styles are enqueued for the code editor.
     3201         *
     3202         * @since 4.9.0
     3203         *
     3204         * @param array $settings Settings for the enqueued code editor.
     3205         */
     3206        do_action( 'wp_enqueue_code_editor', $settings );
     3207
     3208        return $settings;
     3209}
     3210
     3211/**
     3212 * Generate and return code editor settings.
     3213 *
     3214 * @since 5.0.0
     3215 *
     3216 * @see wp_enqueue_code_editor()
     3217 *
     3218 * @param array $args {
     3219 *     Args.
     3220 *
     3221 *     @type string   $type       The MIME type of the file to be edited.
     3222 *     @type string   $file       Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param.
     3223 *     @type WP_Theme $theme      Theme being edited when on theme editor.
     3224 *     @type string   $plugin     Plugin being edited when on plugin editor.
     3225 *     @type array    $codemirror Additional CodeMirror setting overrides.
     3226 *     @type array    $csslint    CSSLint rule overrides.
     3227 *     @type array    $jshint     JSHint rule overrides.
     3228 *     @type array    $htmlhint   JSHint rule overrides.
     3229 * }
     3230 * @return array|false Settings for the code editor.
     3231 */
     3232function wp_get_code_editor_settings( $args ) {
    31443233        $settings = array(
    31453234                'codemirror' => array(
    … …  
    34323521         * @param array $settings The array of settings passed to the code editor. A falsey value disables the editor.
    34333522         * @param array $args {
    3434          *     Args passed when calling `wp_enqueue_code_editor()`.
     3523         *     Args passed when calling `get_code_editor_settings()`.
    34353524         *
    34363525         *     @type string   $type       The MIME type of the file to be edited.
    … …  
    34443533         * }
    34453534         */
    3446         $settings = apply_filters( 'wp_code_editor_settings', $settings, $args );
    3447 
    3448         if ( empty( $settings ) || empty( $settings['codemirror'] ) ) {
    3449                 return false;
    3450         }
    3451 
    3452         wp_enqueue_script( 'code-editor' );
    3453         wp_enqueue_style( 'code-editor' );
    3454 
    3455         if ( isset( $settings['codemirror']['mode'] ) ) {
    3456                 $mode = $settings['codemirror']['mode'];
    3457                 if ( is_string( $mode ) ) {
    3458                         $mode = array(
    3459                                 'name' => $mode,
    3460                         );
    3461                 }
    3462 
    3463                 if ( ! empty( $settings['codemirror']['lint'] ) ) {
    3464                         switch ( $mode['name'] ) {
    3465                                 case 'css':
    3466                                 case 'text/css':
    3467                                 case 'text/x-scss':
    3468                                 case 'text/x-less':
    3469                                         wp_enqueue_script( 'csslint' );
    3470                                         break;
    3471                                 case 'htmlmixed':
    3472                                 case 'text/html':
    3473                                 case 'php':
    3474                                 case 'application/x-httpd-php':
    3475                                 case 'text/x-php':
    3476                                         wp_enqueue_script( 'htmlhint' );
    3477                                         wp_enqueue_script( 'csslint' );
    3478                                         wp_enqueue_script( 'jshint' );
    3479                                         if ( ! current_user_can( 'unfiltered_html' ) ) {
    3480                                                 wp_enqueue_script( 'htmlhint-kses' );
    3481                                         }
    3482                                         break;
    3483                                 case 'javascript':
    3484                                 case 'application/ecmascript':
    3485                                 case 'application/json':
    3486                                 case 'application/javascript':
    3487                                 case 'application/ld+json':
    3488                                 case 'text/typescript':
    3489                                 case 'application/typescript':
    3490                                         wp_enqueue_script( 'jshint' );
    3491                                         wp_enqueue_script( 'jsonlint' );
    3492                                         break;
    3493                         }
    3494                 }
    3495         }
    3496 
    3497         wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) );
    3498 
    3499         /**
    3500          * Fires when scripts and styles are enqueued for the code editor.
    3501          *
    3502          * @since 4.9.0
    3503          *
    3504          * @param array $settings Settings for the enqueued code editor.
    3505          */
    3506         do_action( 'wp_enqueue_code_editor', $settings );
    3507 
    3508         return $settings;
     3535        return apply_filters( 'wp_code_editor_settings', $settings, $args );
    35093536}
    35103537
Note: See TracChangeset for help on using the changeset viewer.