PluginProbe
Code Snippets / 2.13.0
Code Snippets v2.13.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 / settings / editor-preview.php

editor-preview.php in Code Snippets 2.13.0, at php/settings/editor-preview.php

107 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * This file handles the editor preview setting
5 *
6 * @since 2.0
7 * @package Code_Snippets
8 */
9
10 /**
11 * Load the CSS and JavaScript for the editor preview field
12 *
13 * @param string $hook The current page hook
14 */
15 function code_snippets_editor_settings_preview_assets( $hook ) {
16 $plugin = code_snippets();
17
18 // Only load on the settings page
19 if ( $plugin->get_menu_hook( 'settings' ) !== $hook ) {
20 return;
21 }
22
23 // Enqueue scripts for the editor preview
24 code_snippets_enqueue_editor();
25
26 // Enqueue all editor themes
27 $themes = code_snippets_get_available_themes();
28
29 foreach ( $themes as $theme ) {
30
31 wp_enqueue_style(
32 'code-snippets-editor-theme-' . $theme,
33 plugins_url( "css/min/editor-themes/$theme.css", $plugin->file ),
34 array( 'code-snippets-editor' ), $plugin->version
35 );
36 }
37
38 // Enqueue the menu scripts
39 wp_enqueue_script(
40 'code-snippets-settings-menu',
41 plugins_url( 'js/min/settings.js', $plugin->file ),
42 array( 'code-snippets-editor' ), $plugin->version, true
43 );
44
45 // Extract the CodeMirror-specific editor settings
46 $setting_fields = code_snippets_get_settings_fields();
47 $editor_fields = array();
48
49 foreach ( $setting_fields['editor'] as $name => $field ) {
50 if ( empty( $field['codemirror'] ) ) {
51 continue;
52 }
53
54 $editor_fields[] = array(
55 'name' => $name,
56 'type' => $field['type'],
57 'codemirror' => addslashes( $field['codemirror'] ),
58 );
59 }
60
61 // Pass the saved options to the external JavaScript file
62 $inline_script = 'var code_snippets_editor_atts = ' . code_snippets_get_editor_atts( array(), true ) . ';';
63 $inline_script .= "\n" . 'var code_snippets_editor_settings = ' . wp_json_encode( $editor_fields ) . ';';
64
65 wp_add_inline_script( 'code-snippets-settings-menu', $inline_script, 'before' );
66 }
67
68 add_action( 'admin_enqueue_scripts', 'code_snippets_editor_settings_preview_assets' );
69
70 /**
71 * Render a theme select field
72 *
73 * @param array $atts
74 */
75 function code_snippets_codemirror_theme_select_field( $atts ) {
76
77 $saved_value = code_snippets_get_setting( $atts['section'], $atts['id'] );
78
79 echo '<select name="code_snippets_settings[editor][theme]">';
80 echo '<option value="default"' . selected( 'default', $saved_value, false ) . '>Default</option>';
81
82 // print a dropdown entry for each theme
83 foreach ( code_snippets_get_available_themes() as $theme ) {
84
85 // skip mobile themes
86 if ( 'ambiance-mobile' === $theme ) {
87 continue;
88 }
89
90 printf(
91 '<option value="%s"%s>%s</option>',
92 $theme,
93 selected( $theme, $saved_value, false ),
94 ucwords( str_replace( '-', ' ', $theme ) )
95 );
96 }
97
98 echo '</select>';
99 }
100
101 /**
102 * Render the editor preview setting
103 */
104 function code_snippets_settings_editor_preview() {
105 echo '<div id="code_snippets_editor_preview"></div>';
106 }
107