PluginProbe
Code Snippets / 3.8.1
Code Snippets v3.8.1
3.10.2 3.10.1 3.10.0 3.10.0-beta.2 3.10.0-beta.1 4.0.0-beta.1 3.9.6 trunk 2.10.0 2.10.1 2.12.0 2.12.1 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.14.1 2.14.2 2.14.3 2.14.4 2.14.5 2.14.6 3.0.0 3.0.1 All 64 releases
code-snippets / php / editor.php

editor.php in Code Snippets 3.8.1, at php/editor.php

133 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Functions for using the built-in code editor library
4 *
5 * @package Code_Snippets
6 */
7
8 namespace Code_Snippets;
9
10 use function Code_Snippets\Settings\get_setting;
11
12 /**
13 * Register and load the CodeMirror library.
14 *
15 * @param string $type Type of code editor – either 'php', 'css', 'js', or 'html'.
16 * @param array<string, mixed> $extra_atts Pass a list of attributes to override the saved ones.
17 */
18 function enqueue_code_editor( string $type, array $extra_atts = [] ) {
19 $plugin = code_snippets();
20
21 $modes = [
22 'css' => 'text/css',
23 'php' => 'php-snippet',
24 'js' => 'javascript',
25 'html' => 'application/x-httpd-php',
26 ];
27
28 if ( ! isset( $modes[ $type ] ) ) {
29 $type = 'php';
30 }
31
32 $default_atts = [
33 'mode' => $modes[ $type ],
34 'inputStyle' => 'textarea',
35 'matchBrackets' => true,
36 'extraKeys' => [
37 'Alt-F' => 'findPersistent',
38 'Ctrl-Space' => 'autocomplete',
39 'Ctrl-/' => 'toggleComment',
40 'Cmd-/' => 'toggleComment',
41 'Alt-Up' => 'swapLineUp',
42 'Alt-Down' => 'swapLineDown',
43 ],
44 'gutters' => [ 'CodeMirror-lint-markers', 'CodeMirror-foldgutter' ],
45 'lint' => 'css' === $type || 'php' === $type,
46 'direction' => 'ltr',
47 'colorpicker' => [ 'mode' => 'edit' ],
48 'foldOptions' => [ 'widget' => '...' ],
49 ];
50
51 // Add relevant saved setting values to the default attributes.
52 $plugin_settings = Settings\get_settings_values();
53 $setting_fields = Settings\get_settings_fields();
54
55 foreach ( $setting_fields['editor'] as $field_id => $field ) {
56 // The 'codemirror' setting field specifies the name of the attribute.
57 $default_atts[ $field['codemirror'] ] = $plugin_settings['editor'][ $field_id ];
58 }
59
60 // Merge the default attributes with the ones passed into the function.
61 $atts = wp_parse_args( $default_atts, $extra_atts );
62 $atts = apply_filters( 'code_snippets_codemirror_atts', $atts );
63
64 // Ensure number values are not formatted as strings.
65 foreach ( [ 'indentUnit', 'tabSize' ] as $number_att ) {
66 $atts[ $number_att ] = intval( $atts[ $number_att ] );
67 }
68
69 // Remove fontSize from the options and add it as an inline style.
70 if ( isset( $atts['fontSize'] ) ) {
71 $font_size = intval( $atts['fontSize'] );
72 unset( $atts['fontSize'] );
73 wp_add_inline_style( 'code-editor', ".CodeMirror { font-size: {$font_size}px !important; }" );
74 }
75
76 wp_enqueue_code_editor(
77 [
78 'type' => $modes[ $type ],
79 'codemirror' => $atts,
80 ]
81 );
82
83 wp_enqueue_script( 'htmlhint' );
84 wp_enqueue_script( 'csslint' );
85 wp_enqueue_script( 'jshint' );
86
87 wp_enqueue_script(
88 'code-snippets-code-editor',
89 plugins_url( 'dist/editor.js', $plugin->file ),
90 [ 'code-editor' ],
91 $plugin->version,
92 true
93 );
94
95 // CodeMirror Theme.
96 $theme = get_setting( 'editor', 'theme' );
97
98 if ( 'default' !== $theme ) {
99 wp_enqueue_style(
100 'code-snippets-editor-theme-' . $theme,
101 plugins_url( "dist/editor-themes/$theme.css", $plugin->file ),
102 [ 'code-editor' ],
103 $plugin->version
104 );
105 }
106 }
107
108 /**
109 * Retrieve a list of the available CodeMirror themes.
110 *
111 * @return array<string> The available themes.
112 */
113 function get_editor_themes(): array {
114 static $themes = null;
115
116 if ( ! is_null( $themes ) ) {
117 return $themes;
118 }
119
120 $themes = array();
121 $themes_dir = plugin_dir_path( PLUGIN_FILE ) . 'dist/editor-themes/';
122
123 $theme_files = glob( $themes_dir . '*.css' );
124
125 foreach ( $theme_files as $theme ) {
126 $theme = str_replace( $themes_dir, '', $theme );
127 $theme = str_replace( '.css', '', $theme );
128 $themes[] = $theme;
129 }
130
131 return $themes;
132 }
133