PluginProbe
Code Snippets / 3.6.6
Code Snippets v3.6.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 / settings / editor-preview.php

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

114 lines 2.9 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();
25
26 foreach ( $themes as $theme ) {
27 wp_enqueue_style(
28 'code-snippets-editor-theme-' . $theme,
29 plugins_url( "dist/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( 'dist/settings.js', $plugin->file ),
39 [ 'code-snippets-code-editor' ],
40 $plugin->version,
41 true
42 );
43
44 wp_set_script_translations( 'code-snippets-settings-menu', 'code-snippets' );
45
46 // Extract the CodeMirror-specific editor settings.
47 $setting_fields = get_settings_fields();
48 $editor_fields = array();
49
50 foreach ( $setting_fields['editor'] as $name => $field ) {
51 if ( empty( $field['codemirror'] ) ) {
52 continue;
53 }
54
55 $editor_fields[] = array(
56 'name' => $name,
57 'type' => $field['type'],
58 'codemirror' => addslashes( $field['codemirror'] ),
59 );
60 }
61
62 // Pass the saved options to the external JavaScript file.
63 $inline_script = '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 /**
69 * Retrieve the list of code editor themes.
70 *
71 * @return array<string, string> List of editor themes.
72 */
73 function get_editor_theme_list(): array {
74 $themes = [
75 'default' => __( 'Default', 'code-snippets' ),
76 ];
77
78 foreach ( get_editor_themes() as $theme ) {
79
80 // Skip mobile themes.
81 if ( '-mobile' === substr( $theme, -7 ) ) {
82 continue;
83 }
84
85 $themes[ $theme ] = ucwords( str_replace( '-', ' ', $theme ) );
86 }
87
88 return $themes;
89 }
90
91 /**
92 * Render the editor preview setting
93 */
94 function render_editor_preview() {
95 $settings = get_settings_values();
96 $settings = $settings['editor'];
97
98 $indent_unit = absint( $settings['indent_unit'] );
99 $tab_size = absint( $settings['tab_size'] );
100
101 $n_tabs = $settings['indent_with_tabs'] ? floor( $indent_unit / $tab_size ) : 0;
102 $n_spaces = $settings['indent_with_tabs'] ? $indent_unit % $tab_size : $indent_unit;
103
104 $indent = str_repeat( "\t", $n_tabs ) . str_repeat( ' ', $n_spaces );
105
106 $code = "add_filter( 'admin_footer_text', function ( \$text ) {\n\n" .
107 $indent . "\$site_name = get_bloginfo( 'name' );\n\n" .
108 $indent . '$text = "Thank you for visiting $site_name.";' . "\n" .
109 $indent . 'return $text;' . "\n" .
110 "} );\n";
111
112 echo '<textarea id="code_snippets_editor_preview">', esc_textarea( $code ), '</textarea>';
113 }
114