Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/code-snippets/php/settings/editor-preview.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
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 + // Provide configuration and simple i18n for the version switch JS module.
68 + $version_switch = array(
69 + 'ajaxurl' => admin_url( 'admin-ajax.php' ),
70 + 'nonce_switch' => wp_create_nonce( 'code_snippets_version_switch' ),
71 + 'nonce_refresh' => wp_create_nonce( 'code_snippets_refresh_versions' ),
72 + );
73 +
74 + $strings = array(
75 + 'selectDifferent' => esc_html__( 'Please select a different version to switch to.', 'code-snippets' ),
76 + 'switching' => esc_html__( 'Switching...', 'code-snippets' ),
77 + 'processing' => esc_html__( 'Processing version switch. Please wait...', 'code-snippets' ),
78 + 'error' => esc_html__( 'An error occurred.', 'code-snippets' ),
79 + 'errorSwitch' => esc_html__( 'An error occurred while switching versions. Please try again.', 'code-snippets' ),
80 + 'refreshing' => esc_html__( 'Refreshing...', 'code-snippets' ),
81 + 'refreshed' => esc_html__( 'Refreshed!', 'code-snippets' ),
82 + );
83 +
84 + wp_add_inline_script( 'code-snippets-settings-menu', 'var code_snippets_version_switch = ' . wp_json_encode( $version_switch ) . '; var __code_snippets_i18n = ' . wp_json_encode( $strings ) . ';', 'before' );
85 + }
86 +
87 + /**
88 + * Retrieve the list of code editor themes.
89 + *
90 + * @return array<string, string> List of editor themes.
91 + */
92 + function get_editor_theme_list(): array {
93 + $themes = [
94 + 'default' => __( 'Default', 'code-snippets' ),
95 + ];
96 +
97 + foreach ( get_editor_themes() as $theme ) {
98 +
99 + // Skip mobile themes.
100 + if ( '-mobile' === substr( $theme, -7 ) ) {
101 + continue;
102 + }
103 +
104 + $themes[ $theme ] = ucwords( str_replace( '-', ' ', $theme ) );
105 + }
106 +
107 + return $themes;
108 + }
109 +
110 + /**
111 + * Render the editor preview setting
112 + */
113 + function render_editor_preview() {
114 + $settings = get_settings_values();
115 + $settings = $settings['editor'];
116 +
117 + $indent_unit = absint( $settings['indent_unit'] );
118 + $tab_size = absint( $settings['tab_size'] );
119 +
120 + $n_tabs = $settings['indent_with_tabs'] ? floor( $indent_unit / $tab_size ) : 0;
121 + $n_spaces = $settings['indent_with_tabs'] ? $indent_unit % $tab_size : $indent_unit;
122 +
123 + $indent = str_repeat( "\t", $n_tabs ) . str_repeat( ' ', $n_spaces );
124 +
125 + $code = "add_filter( 'admin_footer_text', function ( \$text ) {\n\n" .
126 + $indent . "\$site_name = get_bloginfo( 'name' );\n\n" .
127 + $indent . '$text = "Thank you for visiting $site_name.";' . "\n" .
128 + $indent . 'return $text;' . "\n" .
129 + "} );\n";
130 +
131 + echo '<textarea id="code_snippets_editor_preview">', esc_textarea( $code ), '</textarea>';
132 + }
133 +