PluginProbe
Code Snippets / 3.10.0
Code Snippets v3.10.0
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 / Utils / editor.php

editor.php in Code Snippets 3.10.0, at php/Utils/editor.php

171 lines 4.3 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\Utils;
9
10 use Code_Snippets\Settings\Settings_Fields;
11 use function Code_Snippets\Settings\get_setting;
12 use function Code_Snippets\Settings\get_settings_values;
13 use const Code_Snippets\PLUGIN_FILE;
14 use const Code_Snippets\PLUGIN_VERSION;
15
16 /**
17 * Register and load the CodeMirror library.
18 *
19 * @param string $type Type of code editor – either 'php', 'css', 'js', or 'html'.
20 * @param array<string, mixed> $extra_atts Pass a list of attributes to override the saved ones.
21 */
22 function enqueue_code_editor( string $type, array $extra_atts = [] ) {
23 $modes = [
24 'css' => 'text/css',
25 'php' => 'php-snippet',
26 'js' => 'javascript',
27 'html' => 'application/x-httpd-php',
28 ];
29
30 if ( ! isset( $modes[ $type ] ) ) {
31 $type = 'php';
32 }
33
34 $default_atts = [
35 'mode' => $modes[ $type ],
36 'inputStyle' => 'textarea',
37 'matchBrackets' => true,
38 'extraKeys' => [
39 'Alt-F' => 'findPersistent',
40 'Ctrl-Space' => 'autocomplete',
41 'Ctrl-/' => 'toggleComment',
42 'Cmd-/' => 'toggleComment',
43 'Alt-Up' => 'swapLineUp',
44 'Alt-Down' => 'swapLineDown',
45 ],
46 'gutters' => [ 'CodeMirror-lint-markers', 'CodeMirror-foldgutter' ],
47 'lint' => 'css' === $type || 'php' === $type,
48 'direction' => 'ltr',
49 'colorpicker' => [ 'mode' => 'edit' ],
50 'foldOptions' => [ 'widget' => '...' ],
51 ];
52
53 // Add relevant saved setting values to the default attributes.
54 $plugin_settings = get_settings_values();
55 $field_definitions = Settings_Fields::get_field_definitions();
56
57 foreach ( $field_definitions['editor'] as $field_id => $field ) {
58 // The 'codemirror' setting field specifies the name of the attribute.
59 $default_atts[ $field['codemirror'] ] = $plugin_settings['editor'][ $field_id ];
60 }
61
62 // Merge the default attributes with the ones passed into the function.
63 $atts = wp_parse_args( $default_atts, $extra_atts );
64 $atts = apply_filters( 'code_snippets_codemirror_atts', $atts );
65
66 // Ensure number values are not formatted as strings.
67 foreach ( [ 'indentUnit', 'tabSize' ] as $number_att ) {
68 $atts[ $number_att ] = intval( $atts[ $number_att ] );
69 }
70
71 // Remove fontSize from the options and add it as an inline style.
72 if ( isset( $atts['fontSize'] ) ) {
73 $font_size = intval( $atts['fontSize'] );
74 unset( $atts['fontSize'] );
75 wp_add_inline_style( 'code-editor', ".CodeMirror { font-size: {$font_size}px !important; }" );
76 }
77
78 wp_enqueue_code_editor(
79 [
80 'type' => $modes[ $type ],
81 'codemirror' => $atts,
82 ]
83 );
84
85 wp_enqueue_script( 'htmlhint' );
86 wp_enqueue_script( 'csslint' );
87 wp_enqueue_script( 'jshint' );
88
89 wp_enqueue_script(
90 'code-snippets-code-editor',
91 plugins_url( 'dist/editor.js', PLUGIN_FILE ),
92 [ 'code-editor' ],
93 PLUGIN_VERSION,
94 [ 'in_footer' => true ]
95 );
96
97 enqueue_code_editor_theme();
98 }
99
100 /**
101 * Load the CodeMirror assets required for read-only code previews.
102 *
103 * @param string $type Type of code editor – either 'php', 'css', 'js', or 'html'.
104 *
105 * @return void
106 */
107 function enqueue_code_preview_editor( string $type ): void {
108 $modes = [
109 'css' => 'text/css',
110 'php' => 'text/x-php',
111 'js' => 'javascript',
112 'html' => 'application/x-httpd-php',
113 ];
114
115 wp_enqueue_code_editor(
116 [
117 'type' => $modes[ $type ] ?? $modes['php'],
118 'codemirror' => [
119 'lint' => false,
120 'readOnly' => true,
121 ],
122 ]
123 );
124
125 enqueue_code_editor_theme();
126 }
127
128 /**
129 * Load the configured CodeMirror theme.
130 *
131 * @return void
132 */
133 function enqueue_code_editor_theme(): void {
134 $theme = get_setting( 'editor', 'theme' );
135
136 if ( 'default' !== $theme ) {
137 wp_enqueue_style(
138 'code-snippets-editor-theme-' . $theme,
139 plugins_url( "dist/editor-themes/$theme.css", PLUGIN_FILE ),
140 [ 'code-editor' ],
141 PLUGIN_VERSION
142 );
143 }
144 }
145
146 /**
147 * Retrieve a list of the available CodeMirror themes.
148 *
149 * @return array<string> The available themes.
150 */
151 function get_editor_themes(): array {
152 static $themes = null;
153
154 if ( ! is_null( $themes ) ) {
155 return $themes;
156 }
157
158 $themes = array();
159 $themes_dir = plugin_dir_path( PLUGIN_FILE ) . 'dist/editor-themes/';
160
161 $theme_files = glob( $themes_dir . '*.css' );
162
163 foreach ( $theme_files as $theme ) {
164 $theme = str_replace( $themes_dir, '', $theme );
165 $theme = str_replace( '.css', '', $theme );
166 $themes[] = $theme;
167 }
168
169 return $themes;
170 }
171