PluginProbe
Code Snippets / 3.6.3
Code Snippets v3.6.3
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.6.3, at php/editor.php

126 lines 3.2 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 wp_enqueue_code_editor(
70 [
71 'type' => $modes[ $type ],
72 'codemirror' => $atts,
73 ]
74 );
75
76 wp_enqueue_script( 'htmlhint' );
77 wp_enqueue_script( 'csslint' );
78 wp_enqueue_script( 'jshint' );
79
80 wp_enqueue_script(
81 'code-snippets-code-editor',
82 plugins_url( 'dist/editor.js', $plugin->file ),
83 [ 'code-editor' ],
84 $plugin->version,
85 true
86 );
87
88 // CodeMirror Theme.
89 $theme = get_setting( 'editor', 'theme' );
90
91 if ( 'default' !== $theme ) {
92 wp_enqueue_style(
93 'code-snippets-editor-theme-' . $theme,
94 plugins_url( "dist/editor-themes/$theme.css", $plugin->file ),
95 [ 'code-editor' ],
96 $plugin->version
97 );
98 }
99 }
100
101 /**
102 * Retrieve a list of the available CodeMirror themes.
103 *
104 * @return array<string> The available themes.
105 */
106 function get_editor_themes(): array {
107 static $themes = null;
108
109 if ( ! is_null( $themes ) ) {
110 return $themes;
111 }
112
113 $themes = array();
114 $themes_dir = plugin_dir_path( PLUGIN_FILE ) . 'dist/editor-themes/';
115
116 $theme_files = glob( $themes_dir . '*.css' );
117
118 foreach ( $theme_files as $theme ) {
119 $theme = str_replace( $themes_dir, '', $theme );
120 $theme = str_replace( '.css', '', $theme );
121 $themes[] = $theme;
122 }
123
124 return $themes;
125 }
126