PluginProbe
Code Snippets / 3.9.1
Code Snippets v3.9.1
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 / admin-menus / class-settings-menu.php

class-settings-menu.php in Code Snippets 3.9.1, at php/admin-menus/class-settings-menu.php

240 lines 5.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;
4
5 use const Code_Snippets\Settings\CACHE_KEY;
6 use const Code_Snippets\Settings\OPTION_GROUP;
7 use const Code_Snippets\Settings\OPTION_NAME;
8
9 /**
10 * This class handles the settings admin menu
11 *
12 * @since 2.4.0
13 * @package Code_Snippets
14 */
15 class Settings_Menu extends Admin_Menu {
16
17 /**
18 * Settings page name as registered with the Settings API.
19 */
20 public const SETTINGS_PAGE = 'code-snippets';
21
22 /**
23 * Constructor
24 */
25 public function __construct() {
26
27 parent::__construct(
28 'settings',
29 _x( 'Settings', 'menu label', 'code-snippets' ),
30 __( 'Snippets Settings', 'code-snippets' )
31 );
32 }
33
34 /**
35 * Executed when the admin page is loaded
36 */
37 public function load() {
38 parent::load();
39
40 if ( is_network_admin() ) {
41 if ( Settings\are_settings_unified() ) {
42 $this->update_network_options();
43 } else {
44 wp_safe_redirect( code_snippets()->get_menu_url( 'settings', 'admin' ) );
45 exit;
46 }
47 }
48 }
49
50 /**
51 * Enqueue the stylesheet for the settings menu
52 */
53 public function enqueue_assets() {
54 $plugin = code_snippets();
55
56 Settings\enqueue_editor_preview_assets();
57
58 wp_enqueue_style(
59 'code-snippets-settings',
60 plugins_url( 'dist/settings.css', $plugin->file ),
61 [ 'code-editor' ],
62 $plugin->version
63 );
64 }
65
66 /**
67 * Retrieve the list of settings sections.
68 *
69 * @return array<string, array<string, mixed>>
70 */
71 private function get_sections(): array {
72 global $wp_settings_sections;
73
74 if ( ! isset( $wp_settings_sections[ self::SETTINGS_PAGE ] ) ) {
75 return array();
76 }
77
78 return (array) $wp_settings_sections[ self::SETTINGS_PAGE ];
79 }
80
81 /**
82 * Retrieve the name of the settings section currently being viewed.
83 *
84 * @param string $default_section Name of the default tab displayed.
85 *
86 * @return string
87 */
88 public function get_current_section( string $default_section = 'general' ): string {
89 $sections = $this->get_sections();
90
91 if ( ! $sections ) {
92 return $default_section;
93 }
94
95 $active_tab = isset( $_REQUEST['section'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ) : $default_section;
96 return isset( $sections[ $active_tab ] ) ? $active_tab : $default_section;
97 }
98
99 /**
100 * Render the admin screen
101 */
102 public function render() {
103 $update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' );
104 $current_section = $this->get_current_section();
105
106 ?>
107 <div class="code-snippets-settings wrap" data-active-tab="<?php echo esc_attr( $current_section ); ?>">
108 <h1>
109 <?php
110 esc_html_e( 'Settings', 'code-snippets' );
111
112 if ( code_snippets()->is_compact_menu() ) {
113 $actions = [
114 _x( 'Manage', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url(),
115 _x( 'Add New', 'snippet', 'code-snippets' ) => code_snippets()->get_menu_url( 'add' ),
116 _X( 'Import', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url( 'import' ),
117 ];
118
119 foreach ( $actions as $label => $url ) {
120 printf(
121 '<a href="%s" class="page-title-action">%s</a>',
122 esc_url( $url ),
123 esc_html( $label )
124 );
125 }
126 }
127 ?>
128 </h1>
129
130 <?php settings_errors( OPTION_NAME ); ?>
131
132 <form action="<?php echo esc_url( $update_url ); ?>" method="post">
133 <input type="hidden" name="section" value="<?php echo esc_attr( $current_section ); ?>">
134 <?php
135
136 settings_fields( OPTION_GROUP );
137 $this->do_settings_tabs();
138 ?>
139 <p class="submit">
140 <?php
141 submit_button( null, 'primary', 'submit', false );
142
143 submit_button(
144 __( 'Reset to Default', 'code-snippets' ),
145 'secondary',
146 sprintf( '%s[%s]', OPTION_NAME, 'reset_settings' ),
147 false
148 );
149 ?>
150 </p>
151 </form>
152 </div>
153 <?php
154 }
155
156 /**
157 * Output snippet settings in tabs
158 */
159 protected function do_settings_tabs() {
160 $sections = $this->get_sections();
161 $active_tab = $this->get_current_section();
162
163 echo '<h2 class="nav-tab-wrapper" id="settings-sections-tabs">';
164
165 foreach ( $sections as $section ) {
166 printf(
167 '<a class="nav-tab%s" data-section="%s" href="%s">%s</a>',
168 esc_attr( $active_tab ) === $section['id'] ? ' nav-tab-active' : '',
169 esc_attr( $section['id'] ),
170 esc_url( add_query_arg( 'section', $section['id'] ) ),
171 esc_html( $section['title'] )
172 );
173 }
174
175 echo '</h2>';
176
177 foreach ( $sections as $section ) {
178 if ( 'license' === $section['id'] ) {
179 continue;
180 }
181
182 if ( $section['title'] ) {
183 printf(
184 '<h2 id="%s-settings" class="settings-section-title">%s</h2>' . "\n",
185 esc_attr( $section['id'] ),
186 esc_html( $section['title'] )
187 );
188 }
189
190 if ( $section['callback'] ) {
191 call_user_func( $section['callback'], $section );
192 }
193
194 printf( '<div class="settings-section %s-settings"><table class="form-table">', esc_attr( $section['id'] ) );
195
196 do_settings_fields( self::SETTINGS_PAGE, $section['id'] );
197 echo '</table></div>';
198 }
199 }
200
201 /**
202 * Fill in for the Settings API in the Network Admin
203 */
204 public function update_network_options() {
205
206 // Ensure the settings have been saved.
207 if ( empty( $_GET['update_site_option'] ) || empty( $_POST[ OPTION_NAME ] ) ) {
208 return;
209 }
210
211 check_admin_referer( 'code-snippets-options' );
212
213 // Retrieve the saved options and save them to the database.
214 $value = map_deep( wp_unslash( $_POST[ OPTION_NAME ] ), 'sanitize_key' );
215 update_site_option( OPTION_NAME, $value );
216 wp_cache_delete( CACHE_KEY );
217
218 // Add an updated notice.
219 if ( ! count( get_settings_errors() ) ) {
220 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'code-snippets' ), 'updated' );
221 }
222
223 set_transient( 'settings_errors', get_settings_errors(), 30 );
224
225 // Redirect back to the settings menu.
226 $redirect = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) );
227 wp_safe_redirect( esc_url_raw( $redirect ) );
228 exit;
229 }
230
231 /**
232 * Empty implementation for print_messages.
233 *
234 * @return void
235 */
236 protected function print_messages() {
237 // none required.
238 }
239 }
240