PluginProbe
Code Snippets / 2.14.0
Code Snippets v2.14.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 / editor.php

editor.php in Code Snippets 2.14.0, at php/editor.php

113 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 'viewportMargin' => 'Infinity',
21 );
22
23 // add relevant saved setting values to the default attributes
24 $settings = code_snippets_get_settings();
25 $fields = code_snippets_get_settings_fields();
26
27 foreach ( $fields['editor'] as $field_id => $field ) {
28 // the 'codemirror' setting field specifies the name of the attribute
29 $default_atts[ $field['codemirror'] ] = $settings['editor'][ $field_id ];
30 }
31
32 // merge the default attributes with the ones passed into the function
33 $atts = wp_parse_args( $default_atts, $override_atts );
34 $atts = apply_filters( 'code_snippets_codemirror_atts', $atts );
35
36 // ensure number values are not formatted as strings
37 foreach ( array( 'indentUnit', 'tabSize' ) as $number_att ) {
38 $atts[ $number_att ] = intval( $atts[ $number_att ] );
39 }
40
41 // encode the attributes for display if requested
42 if ( $json_encode ) {
43
44 // JSON_UNESCAPED_SLASHES was added in PHP 5.4
45 if ( version_compare( phpversion(), '5.4.0', '>=' ) ) {
46 $atts = json_encode( $atts, JSON_UNESCAPED_SLASHES );
47 } else {
48 // Use a fallback for < 5.4
49 $atts = str_replace( '\\/', '/', json_encode( $atts ) );
50 }
51
52 // Infinity is a constant and needs to be unquoted
53 $atts = str_replace( '"Infinity"', 'Infinity', $atts );
54 }
55
56 return $atts;
57 }
58
59 /**
60 * Register and load the CodeMirror library
61 *
62 * @uses wp_enqueue_style() to add the stylesheets to the queue
63 * @uses wp_enqueue_script() to add the scripts to the queue
64 */
65 function code_snippets_enqueue_editor() {
66 $url = plugin_dir_url( CODE_SNIPPETS_FILE );
67 $plugin_version = code_snippets()->version;
68
69 /* Remove other CodeMirror styles */
70 wp_deregister_style( 'codemirror' );
71 wp_deregister_style( 'wpeditor' );
72
73 /* CodeMirror */
74 wp_enqueue_style( 'code-snippets-editor', $url . 'css/min/editor.css', array(), $plugin_version );
75 wp_enqueue_script( 'code-snippets-editor', $url . 'js/min/editor.js', array(), $plugin_version );
76
77 /* CodeMirror Theme */
78 $theme = code_snippets_get_setting( 'editor', 'theme' );
79
80 if ( 'default' !== $theme ) {
81
82 wp_enqueue_style(
83 'code-snippets-editor-theme-' . $theme,
84 $url . "css/min/editor-themes/$theme.css",
85 array( 'code-snippets-editor' ), $plugin_version
86 );
87 }
88 }
89
90 /**
91 * Retrieve a list of the available CodeMirror themes
92 * @return array the available themes
93 */
94 function code_snippets_get_available_themes() {
95 static $themes = null;
96
97 if ( ! is_null( $themes ) ) {
98 return $themes;
99 }
100
101 $themes = array( 'default' );
102 $themes_dir = plugin_dir_path( CODE_SNIPPETS_FILE ) . 'css/min/editor-themes/';
103 $theme_files = glob( $themes_dir . '*.css' );
104
105 foreach ( $theme_files as $i => $theme ) {
106 $theme = str_replace( $themes_dir, '', $theme );
107 $theme = str_replace( '.css', '', $theme );
108 $themes[] = $theme;
109 }
110
111 return $themes;
112 }
113