PluginProbe
Code Snippets / 2.14.6
Code Snippets v2.14.6
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 2.14.6, at php/editor.php

115 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Get the attributes for the code editor
5 *
6 * @param array $override_atts Pass an array of attributes to override the saved ones
7 * @param bool $json_encode Encode the data as JSON
8 *
9 * @return array|string Array if $json_encode is false, JSON string if it is true
10 */
11 function code_snippets_get_editor_atts( $override_atts, $json_encode ) {
12
13 // default attributes for the CodeMirror editor
14 $default_atts = array(
15 'mode' => 'php-snippet',
16 'matchBrackets' => true,
17 'extraKeys' => array( 'Alt-F' => 'findPersistent' ),
18 'gutters' => array( 'CodeMirror-lint-markers' ),
19 'lint' => true,
20 'direction' => 'ltr',
21 'viewportMargin' => 'Infinity',
22 );
23
24 // add relevant saved setting values to the default attributes
25 $settings = code_snippets_get_settings();
26 $fields = code_snippets_get_settings_fields();
27
28 foreach ( $fields['editor'] as $field_id => $field ) {
29 // the 'codemirror' setting field specifies the name of the attribute
30 $default_atts[ $field['codemirror'] ] = $settings['editor'][ $field_id ];
31 }
32
33 // merge the default attributes with the ones passed into the function
34 $atts = wp_parse_args( $default_atts, $override_atts );
35 $atts = apply_filters( 'code_snippets_codemirror_atts', $atts );
36
37 // ensure number values are not formatted as strings
38 foreach ( array( 'indentUnit', 'tabSize' ) as $number_att ) {
39 $atts[ $number_att ] = intval( $atts[ $number_att ] );
40 }
41
42 // encode the attributes for display if requested
43 if ( $json_encode ) {
44
45 // JSON_UNESCAPED_SLASHES was added in PHP 5.4
46 if ( version_compare( phpversion(), '5.4.0', '>=' ) ) {
47 $atts = wp_json_encode( $atts, JSON_UNESCAPED_SLASHES );
48 } else {
49 // Use a fallback for < 5.4
50 $atts = str_replace( '\\/', '/', wp_json_encode( $atts ) );
51 }
52
53 // Infinity is a constant and needs to be unquoted
54 $atts = str_replace( '"Infinity"', 'Infinity', $atts );
55 }
56
57 return $atts;
58 }
59
60 /**
61 * Register and load the CodeMirror library
62 *
63 * @uses wp_enqueue_style() to add the stylesheets to the queue
64 * @uses wp_enqueue_script() to add the scripts to the queue
65 */
66 function code_snippets_enqueue_editor() {
67 $url = plugin_dir_url( CODE_SNIPPETS_FILE );
68 $plugin_version = code_snippets()->version;
69
70 /* Remove other CodeMirror styles */
71 wp_deregister_style( 'codemirror' );
72 wp_deregister_style( 'wpeditor' );
73
74 /* CodeMirror */
75 wp_enqueue_style( 'code-snippets-editor', $url . 'css/min/editor.css', array(), $plugin_version );
76 wp_enqueue_script( 'code-snippets-editor', $url . 'js/min/editor.js', array(), $plugin_version, false );
77
78 /* CodeMirror Theme */
79 $theme = code_snippets_get_setting( 'editor', 'theme' );
80
81 if ( 'default' !== $theme ) {
82
83 wp_enqueue_style(
84 'code-snippets-editor-theme-' . $theme,
85 $url . "css/min/editor-themes/$theme.css",
86 array( 'code-snippets-editor' ),
87 $plugin_version
88 );
89 }
90 }
91
92 /**
93 * Retrieve a list of the available CodeMirror themes
94 * @return array the available themes
95 */
96 function code_snippets_get_available_themes() {
97 static $themes = null;
98
99 if ( ! is_null( $themes ) ) {
100 return $themes;
101 }
102
103 $themes = array( 'default' );
104 $themes_dir = plugin_dir_path( CODE_SNIPPETS_FILE ) . 'css/min/editor-themes/';
105 $theme_files = glob( $themes_dir . '*.css' );
106
107 foreach ( $theme_files as $i => $theme ) {
108 $theme = str_replace( $themes_dir, '', $theme );
109 $theme = str_replace( '.css', '', $theme );
110 $themes[] = $theme;
111 }
112
113 return $themes;
114 }
115