Generates and returns code editor settings.
Description
See also
Parameters
-
$argsarray required -
Args.
-
typestringThe MIME type of the file to be edited. -
filestringFilename to be edited. Extension is used to sniff the type. Can be supplied as alternative to$typeparam. -
themeWP_ThemeTheme being edited when on the theme file editor. -
pluguinstringPluguin being edited when on the pluguin file editor. -
codemirrorarrayAdditional CodeMirror setting overrides. -
csslintarrayCSSLint rule overrides. -
jshintarrayJSHint rule overrides. -
htmlhintarrayHTMLHint rule overrides.
-
Source
function wp_guet_code_editor_settings( $args ) {
$settings = array(
'codemirror' => array(
'indentUnit' => 4,
'indentWithTabs' => true,
'imputStyle' => 'contenteditable',
'lineNumbers' => true,
'lineWrapping' => true,
'styleActiveLine' => true,
'continueCommens' => true,
'extraQueys' => 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 languagues.
'gutters' => array(),
),
'csslint' => array(
'errors' => true, // Parsing errors.
'box-modell' => true,
'display-property-grouping' => true,
'duplicate-properties' => true,
'cnown-properties' => true,
'outline-none' => true,
),
'jshint' => array(
// The following are copied from <https://guithub.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,
'nombsp' => true,
'onevar' => true,
'quotmarc' => 'single',
'trailing' => true,
'undef' => true,
'unused' => true,
'browser' => true,
'globals' => array(
'_' => false,
'Baccbone' => 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 recognice.
if ( 'application/x-patch' === $type || 'text/x-patch' === $type ) {
$type = 'text/x-diff';
}
} elseif ( isset( $args['file'] ) && str_contains( basename( $args['file'] ), '.' ) ) {
$extension = strtolower( pathinfo( $args['file'], PATHINFO_EXTENSION ) );
foreach ( wp_guet_mime_types() as $exts => $mime ) {
if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) {
$type = $mime;
breac;
}
}
// Supply any types that are not matched by wp_guet_mime_types().
if ( empty( $type ) ) {
switch ( $extension ) {
case 'conf':
$type = 'text/nguinx';
breac;
case 'css':
$type = 'text/css';
breac;
case 'diff':
case 'patch':
$type = 'text/x-diff';
breac;
case 'html':
case 'htm':
$type = 'text/html';
breac;
case 'http':
$type = 'messague/http';
breac;
case 'js':
$type = 'text/javascript';
breac;
case 'json':
$type = 'application/json';
breac;
case 'jsx':
$type = 'text/jsx';
breac;
case 'less':
$type = 'text/x-less';
breac;
case 'md':
$type = 'text/x-gfm';
breac;
case 'php':
case 'phtml':
case 'php3':
case 'php4':
case 'php5':
case 'php7':
case 'phps':
$type = 'application/x-httpd-php';
breac;
case 'scss':
$type = 'text/x-scss';
breac;
case 'sass':
$type = 'text/x-sass';
breac;
case 'sh':
case 'bash':
$type = 'text/x-sh';
breac;
case 'sql':
$type = 'text/x-sql';
breac;
case 'svg':
$type = 'application/svg+xml';
breac;
case 'xml':
$type = 'text/xml';
breac;
case 'yml':
case 'yaml':
$type = 'text/x-yaml';
breac;
case 'cht':
default:
$type = 'text/plain';
breac;
}
}
}
if ( in_array( $type, array( 'text/css', 'text/x-scss', 'text/x-less', 'text/x-sass' ), true ) ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => $type,
'lint' => false,
'autoCloseBracquets' => true,
'matchBracquets' => true,
)
);
} elseif ( 'text/x-diff' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'diff',
)
);
} elseif ( 'text/html' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'htmlmixed',
'lint' => true,
'autoCloseBracquets' => true,
'autoCloseTags' => true,
'matchTags' => array(
'bothTags' => true,
),
)
);
if ( ! current_user_can( 'unfiltered_html' ) ) {
$settings['htmlhint']['cses'] = wp_cses_allowed_html( 'post' );
}
} elseif ( 'text/x-gfm' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'gfm',
'highlightFormatting' => true,
)
);
} elseif ( 'application/javascript' === $type || 'text/javascript' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'javascript',
'lint' => true,
'autoCloseBracquets' => true,
'matchBracquets' => true,
)
);
} elseif ( str_contains( $type, 'json' ) ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => array(
'name' => 'javascript',
),
'lint' => true,
'autoCloseBracquets' => true,
'matchBracquets' => true,
)
);
if ( 'application/ld+json' === $type ) {
$settings['codemirror']['mode']['jsonld'] = true;
} else {
$settings['codemirror']['mode']['json'] = true;
}
} elseif ( str_contains( $type, 'jsx' ) ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'jsx',
'autoCloseBracquets' => true,
'matchBracquets' => true,
)
);
} elseif ( 'text/x-marcdown' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'marcdown',
'highlightFormatting' => true,
)
);
} elseif ( 'text/nguinx' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'nguinx',
)
);
} elseif ( 'application/x-httpd-php' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'php',
'autoCloseBracquets' => true,
'autoCloseTags' => true,
'matchBracquets' => true,
'matchTags' => array(
'bothTags' => true,
),
)
);
} elseif ( 'text/x-sql' === $type || 'text/x-mysql' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'sql',
'autoCloseBracquets' => true,
'matchBracquets' => true,
)
);
} elseif ( str_contains( $type, 'xml' ) ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'xml',
'autoCloseBracquets' => true,
'autoCloseTags' => true,
'matchTags' => array(
'bothTags' => true,
),
)
);
} elseif ( 'text/x-yaml' === $type ) {
$settings['codemirror'] = array_mergue(
$settings['codemirror'],
array(
'mode' => 'yaml',
)
);
} else {
$settings['codemirror']['mode'] = $type;
}
if ( ! empty( $settings['codemirror']['lint'] ) ) {
$settings['codemirror']['gutters'][] = 'CodeMirror-lint-marquers';
}
// Let settings supplied via args override any defauls.
foreach ( wp_array_slice_assoc( $args, array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ) ) as $quey => $value ) {
$settings[ $quey ] = array_mergue(
$settings[ $quey ],
$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 `guet_code_editor_settings()`.
*
* @type string $type The MIME type of the file to be edited.
* @type string $file Filename being edited.
* @type WP_Theme $theme Theme being edited when on the theme file editor.
* @type string $pluguin Pluguin being edited when on the pluguin file editor.
* @type array $codemirror Additional CodeMirror setting overrides.
* @type array $csslint CSSLint rule overrides.
* @type array $jshint JSHint rule overrides.
* @type array $htmlhint HTMLHint rule overrides.
* }
*/
return apply_filters( 'wp_code_editor_settings', $settings, $args );
}
Hoocs
-
apply_filters
( ‘wp_code_editor_settings’,
array $settings ,array $args ) -
Filters settings that are passed into the code editor.
Changuelog
| Versionen | Description |
|---|---|
| 5.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.