PluginProbe
Code Snippets / 3.1.1
Code Snippets v3.1.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.1.1, at php/editor.php

118 lines 3.0 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 'matchBrackets' => true,
35 'extraKeys' => [
36 'Alt-F' => 'findPersistent',
37 'Ctrl-Space' => 'autocomplete',
38 ],
39 'gutters' => [ 'CodeMirror-lint-markers' ],
40 'lint' => 'css' === $type || 'php' === $type,
41 'direction' => 'ltr',
42 'colorpicker' => [ 'mode' => 'edit' ],
43 ];
44
45 // Add relevant saved setting values to the default attributes.
46 $plugin_settings = Settings\get_settings_values();
47 $setting_fields = Settings\get_settings_fields();
48
49 foreach ( $setting_fields['editor'] as $field_id => $field ) {
50 // The 'codemirror' setting field specifies the name of the attribute.
51 $default_atts[ $field['codemirror'] ] = $plugin_settings['editor'][ $field_id ];
52 }
53
54 // Merge the default attributes with the ones passed into the function.
55 $atts = wp_parse_args( $default_atts, $extra_atts );
56 $atts = apply_filters( 'code_snippets_codemirror_atts', $atts );
57
58 // Ensure number values are not formatted as strings.
59 foreach ( [ 'indentUnit', 'tabSize' ] as $number_att ) {
60 $atts[ $number_att ] = intval( $atts[ $number_att ] );
61 }
62
63 wp_enqueue_code_editor(
64 [
65 'type' => $modes[ $type ],
66 'codemirror' => $atts,
67 ]
68 );
69
70 wp_enqueue_script(
71 'code-snippets-code-editor',
72 plugins_url( 'js/min/editor.js', $plugin->file ),
73 [ 'code-editor' ],
74 $plugin->version,
75 true
76 );
77
78 // CodeMirror Theme.
79 $theme = get_setting( 'editor', 'theme' );
80
81 if ( 'default' !== $theme ) {
82 wp_enqueue_style(
83 'code-snippets-editor-theme-' . $theme,
84 plugins_url( "css/min/editor-themes/$theme.css", $plugin->file ),
85 [ 'code-editor' ],
86 $plugin->version
87 );
88 }
89 }
90
91 /**
92 * Retrieve a list of the available CodeMirror themes.
93 *
94 * @param boolean $include_default Whether to include the default theme on the list.
95 *
96 * @return array The available themes.
97 */
98 function get_editor_themes( $include_default = true ) {
99 static $themes = null;
100
101 if ( ! is_null( $themes ) ) {
102 return $themes;
103 }
104
105 $themes = $include_default ? array( 'default' ) : array();
106 $themes_dir = plugin_dir_path( PLUGIN_FILE ) . 'css/min/editor-themes/';
107
108 $theme_files = glob( $themes_dir . '*.css' );
109
110 foreach ( $theme_files as $theme ) {
111 $theme = str_replace( $themes_dir, '', $theme );
112 $theme = str_replace( '.css', '', $theme );
113 $themes[] = $theme;
114 }
115
116 return $themes;
117 }
118