PluginProbe
Code Snippets / 3.1.0
Code Snippets v3.1.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 3.1.0, at php/settings/editor-preview.php

110 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * This file handles the editor preview setting
4 *
5 * @since 2.0.0
6 * @package Code_Snippets
7 */
8
9 namespace Code_Snippets\Settings;
10
11 use function Code_Snippets\code_snippets;
12 use function Code_Snippets\enqueue_code_editor;
13 use function Code_Snippets\get_editor_themes;
14
15 /**
16 * Load the CSS and JavaScript for the editor preview field
17 */
18 function enqueue_editor_preview_assets() {
19 $plugin = code_snippets();
20
21 enqueue_code_editor( 'php' );
22
23 // Enqueue all editor themes.
24 $themes = get_editor_themes( false );
25
26 foreach ( $themes as $theme ) {
27 wp_enqueue_style(
28 'code-snippets-editor-theme-' . $theme,
29 plugins_url( "css/min/editor-themes/$theme.css", $plugin->file ),
30 [ 'code-editor' ],
31 $plugin->version
32 );
33 }
34
35 // Enqueue the menu scripts.
36 wp_enqueue_script(
37 'code-snippets-settings-menu',
38 plugins_url( 'js/min/settings.js', $plugin->file ),
39 [ 'code-snippets-code-editor' ],
40 $plugin->version,
41 true
42 );
43
44 // Extract the CodeMirror-specific editor settings.
45 $setting_fields = get_settings_fields();
46 $editor_fields = array();
47
48 foreach ( $setting_fields['editor'] as $name => $field ) {
49 if ( empty( $field['codemirror'] ) ) {
50 continue;
51 }
52
53 $editor_fields[] = array(
54 'name' => $name,
55 'type' => $field['type'],
56 'codemirror' => addslashes( $field['codemirror'] ),
57 );
58 }
59
60 // Pass the saved options to the external JavaScript file.
61 $inline_script = 'var code_snippets_editor_settings = ' . wp_json_encode( $editor_fields ) . ';';
62
63 wp_add_inline_script( 'code-snippets-settings-menu', $inline_script, 'before' );
64 }
65
66 /**
67 * Retrieve the list of code editor themes.
68 *
69 * @return array List of editor themes.
70 */
71 function get_editor_theme_list() {
72 $themes = [];
73
74 foreach ( get_editor_themes() as $theme ) {
75
76 // Skip mobile themes.
77 if ( '-mobile' === substr( $theme, -7 ) ) {
78 continue;
79 }
80
81 $themes[ $theme ] = ucwords( str_replace( '-', ' ', $theme ) );
82 }
83
84 return $themes;
85 }
86
87 /**
88 * Render the editor preview setting
89 */
90 function render_editor_preview() {
91 $settings = get_settings_values();
92 $settings = $settings['editor'];
93
94 $indent_unit = absint( $settings['indent_unit'] );
95 $tab_size = absint( $settings['tab_size'] );
96
97 $n_tabs = $settings['indent_with_tabs'] ? floor( $indent_unit / $tab_size ) : 0;
98 $n_spaces = $settings['indent_with_tabs'] ? $indent_unit % $tab_size : $indent_unit;
99
100 $indent = str_repeat( "\t", $n_tabs ) . str_repeat( ' ', $n_spaces );
101
102 $code = "add_filter( 'admin_footer_text', function ( \$text ) {\n\n" .
103 $indent . "\$site_name = get_bloginfo( 'name' );\n\n" .
104 $indent . '$text = "Thank you for visiting $site_name.";' . "\n" .
105 $indent . 'return $text;' . "\n" .
106 "} );\n";
107
108 echo '<textarea id="code_snippets_editor_preview">', esc_textarea( $code ), '</textarea>';
109 }
110