PluginProbe
Code Snippets / 4.0.0-beta.1
Code Snippets v4.0.0-beta.1
4.0.0-beta.2 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 All 65 releases
code-snippets / php / Admin / Menus / Settings_Menu.php

Settings_Menu.php in Code Snippets 4.0.0-beta.1, at php/Admin/Menus/Settings_Menu.php

327 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Code_Snippets\Admin\Menus;
4
5 use Code_Snippets\Settings\Settings_Fields;
6 use function Code_Snippets\code_snippets;
7 use function Code_Snippets\Settings\are_settings_unified;
8 use function Code_Snippets\Utils\enqueue_code_editor;
9 use function Code_Snippets\Utils\get_editor_themes;
10 use const Code_Snippets\PLUGIN_FILE;
11 use const Code_Snippets\PLUGIN_VERSION;
12 use const Code_Snippets\Settings\OPTION_GROUP;
13 use const Code_Snippets\Settings\OPTION_NAME;
14 use const Code_Snippets\Settings\CACHE_KEY;
15
16 /**
17 * This class handles the settings admin menu
18 *
19 * @since 2.4.0
20 * @package Code_Snippets
21 */
22 class Settings_Menu extends Admin_Menu {
23
24 /**
25 * Settings page name as registered with the Settings API.
26 */
27 public const SETTINGS_PAGE = 'code-snippets';
28
29 /**
30 * Constructor
31 */
32 public function __construct() {
33 parent::__construct(
34 'settings',
35 _x( 'Settings', 'menu label', 'code-snippets' ),
36 __( 'Snippets Settings', 'code-snippets' )
37 );
38 }
39
40 /**
41 * Executed when the admin page is loaded
42 */
43 public function load() {
44 parent::load();
45
46 if ( is_network_admin() ) {
47 if ( are_settings_unified() ) {
48 $this->update_network_options();
49 } else {
50 wp_safe_redirect( code_snippets()->get_menu_url( 'settings', 'admin' ) );
51 exit;
52 }
53 }
54 }
55
56 /**
57 * Enqueue the stylesheet for the settings menu
58 */
59 public function enqueue_assets() {
60 $this->enqueue_codemirror();
61 $handle = 'code-snippets-settings';
62
63 wp_enqueue_style(
64 $handle,
65 plugins_url( 'dist/settings.css', PLUGIN_FILE ),
66 self::$style_deps + [ 'code-editor' ],
67 PLUGIN_VERSION
68 );
69
70 wp_enqueue_script(
71 $handle,
72 plugins_url( 'dist/settings.js', PLUGIN_FILE ),
73 self::$script_deps + [ 'code-snippets-code-editor' ],
74 PLUGIN_VERSION,
75 true
76 );
77
78 wp_set_script_translations( $handle, 'code-snippets' );
79 code_snippets()->localize_script( $handle );
80
81 $this->add_codemirror_settings_script( $handle );
82
83 // Provide configuration and simple i18n for the version switch JS module.
84 wp_localize_script(
85 $handle,
86 'code_snippets_version_switch',
87 [
88 'ajaxurl' => admin_url( 'admin-ajax.php' ),
89 'nonce_switch' => wp_create_nonce( 'code_snippets_version_switch' ),
90 'nonce_refresh' => wp_create_nonce( 'code_snippets_refresh_versions' ),
91 ]
92 );
93
94 wp_localize_script(
95 $handle,
96 '__code_snippets_i18n',
97 [
98 'selectDifferent' => esc_html__( 'Please select a different version to switch to.', 'code-snippets' ),
99 'switching' => esc_html__( 'Switching…', 'code-snippets' ),
100 'processing' => esc_html__( 'Processing version switch. Please wait…', 'code-snippets' ),
101 'error' => esc_html__( 'An error occurred.', 'code-snippets' ),
102 'errorSwitch' => esc_html__( 'An error occurred while switching versions. Please try again.', 'code-snippets' ),
103 'refreshing' => esc_html__( 'Refreshing…', 'code-snippets' ),
104 'refreshed' => esc_html__( 'Refreshed!', 'code-snippets' ),
105 ]
106 );
107 }
108
109 /**
110 * Enqueue the CodeMirror scripts and styles, including all themes.
111 *
112 * @return void
113 */
114 protected function enqueue_codemirror() {
115 enqueue_code_editor( 'php' );
116 $themes = get_editor_themes();
117
118 foreach ( $themes as $theme ) {
119 wp_enqueue_style(
120 'code-snippets-editor-theme-' . $theme,
121 plugins_url( "dist/editor-themes/$theme.css", PLUGIN_FILE ),
122 [ 'code-editor' ],
123 PLUGIN_VERSION
124 );
125 }
126 }
127
128 /**
129 * Load the CodeMirror settings as an inline script variable.
130 *
131 * @param string $handle The handle of the script to which the settings will be added.
132 *
133 * @return void
134 */
135 protected function add_codemirror_settings_script( string $handle ) {
136 $field_definitions = Settings_Fields::get_field_definitions();
137 $editor_fields = [];
138
139 foreach ( $field_definitions['editor'] as $name => $field ) {
140 if ( empty( $field['codemirror'] ) ) {
141 continue;
142 }
143
144 $editor_fields[] = [
145 'name' => $name,
146 'type' => $field['type'],
147 'codemirror' => addslashes( $field['codemirror'] ),
148 ];
149 }
150
151 // Pass the saved options to the external JavaScript file.
152 $inline_script = 'var code_snippets_editor_settings = ' . wp_json_encode( $editor_fields ) . ';';
153
154 wp_add_inline_script( $handle, $inline_script, 'before' );
155 }
156
157 /**
158 * Retrieve the list of settings sections.
159 *
160 * @return array<string, array<string, mixed>>
161 */
162 private function get_sections(): array {
163 global $wp_settings_sections;
164
165 if ( ! isset( $wp_settings_sections[ self::SETTINGS_PAGE ] ) ) {
166 return array();
167 }
168
169 return (array) $wp_settings_sections[ self::SETTINGS_PAGE ];
170 }
171
172 /**
173 * Retrieve the name of the settings section currently being viewed.
174 *
175 * @param string $default_section Name of the default tab displayed.
176 *
177 * @return string
178 */
179 public function get_current_section( string $default_section = 'general' ): string {
180 $sections = $this->get_sections();
181
182 if ( ! $sections ) {
183 return $default_section;
184 }
185
186 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Value is matched to registered sections.
187 $active_tab = isset( $_REQUEST['section'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ) : $default_section;
188 return isset( $sections[ $active_tab ] ) ? $active_tab : $default_section;
189 }
190
191 /**
192 * Render the admin screen
193 */
194 public function render() {
195 $this->render_navigation();
196
197 $update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' );
198 $current_section = $this->get_current_section();
199
200 ?>
201 <div class="wrap" data-active-tab="<?php echo esc_attr( $current_section ); ?>">
202 <h2>
203 <?php
204 esc_html_e( 'Settings', 'code-snippets' );
205
206 if ( code_snippets()->is_compact_menu() ) {
207 $actions = [
208 _x( 'Manage', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url(),
209 _x( 'Add New', 'snippet', 'code-snippets' ) => code_snippets()->get_menu_url( 'add' ),
210 _X( 'Import', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url( 'import' ),
211 ];
212
213 foreach ( $actions as $label => $url ) {
214 printf(
215 '<a href="%s" class="page-title-action">%s</a>',
216 esc_url( $url ),
217 esc_html( $label )
218 );
219 }
220 }
221 ?>
222 </h2>
223
224 <hr class="wp-header-end" />
225
226 <?php settings_errors( OPTION_NAME ); ?>
227
228 <form action="<?php echo esc_url( $update_url ); ?>" method="post">
229 <input type="hidden" name="section" value="<?php echo esc_attr( $current_section ); ?>">
230 <?php
231
232 settings_fields( OPTION_GROUP );
233 $this->do_settings_tabs();
234 ?>
235 <p class="submit">
236 <?php
237 submit_button( null, 'primary', 'submit', false );
238
239 submit_button(
240 __( 'Reset to Default', 'code-snippets' ),
241 'secondary',
242 sprintf( '%s[%s]', OPTION_NAME, 'reset_settings' ),
243 false
244 );
245 ?>
246 </p>
247 </form>
248 </div>
249 <?php
250 }
251
252 /**
253 * Output snippet settings in tabs
254 */
255 protected function do_settings_tabs() {
256 $sections = $this->get_sections();
257 $active_tab = $this->get_current_section();
258
259 echo '<nav class="nav-tab-wrapper" id="settings-sections-tabs" aria-label="' . esc_attr__( 'Settings tabs', 'code-snippets' ) . '">';
260
261 foreach ( $sections as $section ) {
262 printf(
263 '<a class="nav-tab%s" data-section="%s" href="%s">%s</a>',
264 esc_attr( $active_tab ) === $section['id'] ? ' nav-tab-active' : '',
265 esc_attr( $section['id'] ),
266 esc_url( add_query_arg( 'section', $section['id'] ) ),
267 esc_html( $section['title'] )
268 );
269 }
270
271 echo '</nav>';
272
273 foreach ( $sections as $section ) {
274 if ( 'license' === $section['id'] ) {
275 continue;
276 }
277
278 if ( $section['title'] ) {
279 printf(
280 '<h3 id="%s-settings" class="settings-section-title">%s</h3>' . "\n",
281 esc_attr( $section['id'] ),
282 esc_html( $section['title'] )
283 );
284 }
285
286 if ( $section['callback'] ) {
287 call_user_func( $section['callback'], $section );
288 }
289
290 printf( '<div class="settings-section %s-settings"><table class="form-table">', esc_attr( $section['id'] ) );
291
292 do_settings_fields( self::SETTINGS_PAGE, $section['id'] );
293 echo '</table></div>';
294 }
295 }
296
297 /**
298 * Fill in for the Settings API in the Network Admin
299 */
300 public function update_network_options() {
301
302 // Ensure the settings have been saved.
303 if ( empty( $_GET['update_site_option'] ) || empty( $_POST[ OPTION_NAME ] ) ) {
304 return;
305 }
306
307 check_admin_referer( 'code-snippets-options' );
308
309 // Retrieve the saved options and save them to the database.
310 $value = map_deep( wp_unslash( $_POST[ OPTION_NAME ] ), 'sanitize_key' );
311 update_site_option( OPTION_NAME, $value );
312 wp_cache_delete( CACHE_KEY );
313
314 // Add an updated notice.
315 if ( ! count( get_settings_errors() ) ) {
316 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'code-snippets' ), 'updated' );
317 }
318
319 set_transient( 'settings_errors', get_settings_errors(), 30 );
320
321 // Redirect back to the settings menu.
322 $redirect = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) );
323 wp_safe_redirect( esc_url_raw( $redirect ) );
324 exit;
325 }
326 }
327