PluginProbe
Code Snippets / 3.2.1
Code Snippets v3.2.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.2.1, at php/editor.php

122 lines 3.1 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 $extra_atts Pass an array of attributes to override the saved ones.
17 */
18 function enqueue_code_editor( $type, $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(
77 'code-snippets-code-editor',
78 plugins_url( 'dist/editor.js', $plugin->file ),
79 [ 'code-editor' ],
80 $plugin->version,
81 true
82 );
83
84 // CodeMirror Theme.
85 $theme = get_setting( 'editor', 'theme' );
86
87 if ( 'default' !== $theme ) {
88 wp_enqueue_style(
89 'code-snippets-editor-theme-' . $theme,
90 plugins_url( "dist/editor-themes/$theme.css", $plugin->file ),
91 [ 'code-editor' ],
92 $plugin->version
93 );
94 }
95 }
96
97 /**
98 * Retrieve a list of the available CodeMirror themes.
99 *
100 * @return array The available themes.
101 */
102 function get_editor_themes() {
103 static $themes = null;
104
105 if ( ! is_null( $themes ) ) {
106 return $themes;
107 }
108
109 $themes = array();
110 $themes_dir = plugin_dir_path( PLUGIN_FILE ) . 'dist/editor-themes/';
111
112 $theme_files = glob( $themes_dir . '*.css' );
113
114 foreach ( $theme_files as $theme ) {
115 $theme = str_replace( $themes_dir, '', $theme );
116 $theme = str_replace( '.css', '', $theme );
117 $themes[] = $theme;
118 }
119
120 return $themes;
121 }
122