PluginProbe
Code Snippets / 3.6.3
Code Snippets v3.6.3
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.6.3, at php/admin-menus/class-settings-menu.php

238 lines 5.8 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 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 [],
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 ?>
140 <p class="submit">
141 <?php
142 submit_button( null, 'primary', 'submit', false );
143
144 submit_button(
145 __( 'Reset to Default', 'code-snippets' ),
146 'secondary',
147 sprintf( '%s[%s]', OPTION_NAME, 'reset_settings' ),
148 false
149 );
150 ?>
151 </p>
152 </form>
153 </div>
154 <?php
155 }
156
157 /**
158 * Output snippet settings in tabs
159 */
160 protected function do_settings_tabs() {
161 $sections = $this->get_sections();
162 $active_tab = $this->get_current_section();
163
164 echo '<h2 class="nav-tab-wrapper" id="settings-sections-tabs">';
165
166 foreach ( $sections as $section ) {
167 printf(
168 '<a class="nav-tab%s" data-section="%s" href="%s">%s</a>',
169 esc_attr( $active_tab ) === $section['id'] ? ' nav-tab-active' : '',
170 esc_attr( $section['id'] ),
171 esc_url( add_query_arg( 'section', $section['id'] ) ),
172 esc_html( $section['title'] )
173 );
174 }
175
176 echo '</h2>';
177
178 foreach ( $sections as $section ) {
179
180 if ( $section['title'] ) {
181 printf(
182 '<h2 id="%s-settings" class="settings-section-title">%s</h2>' . "\n",
183 esc_attr( $section['id'] ),
184 esc_html( $section['title'] )
185 );
186 }
187
188 if ( $section['callback'] ) {
189 call_user_func( $section['callback'], $section );
190 }
191
192 printf( '<table class="form-table settings-section %s-settings">', esc_attr( $section['id'] ) );
193
194 do_settings_fields( self::SETTINGS_PAGE, $section['id'] );
195 echo '</table>';
196 }
197 }
198
199 /**
200 * Fill in for the Settings API in the Network Admin
201 */
202 public function update_network_options() {
203
204 // Ensure the settings have been saved.
205 if ( empty( $_GET['update_site_option'] ) || empty( $_POST[ OPTION_NAME ] ) ) {
206 return;
207 }
208
209 check_admin_referer( 'code-snippets-options' );
210
211 // Retrieve the saved options and save them to the database.
212 $value = map_deep( wp_unslash( $_POST[ OPTION_NAME ] ), 'sanitize_key' );
213 update_site_option( OPTION_NAME, $value );
214 wp_cache_delete( CACHE_KEY );
215
216 // Add an updated notice.
217 if ( ! count( get_settings_errors() ) ) {
218 add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'code-snippets' ), 'updated' );
219 }
220
221 set_transient( 'settings_errors', get_settings_errors(), 30 );
222
223 // Redirect back to the settings menu.
224 $redirect = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) );
225 wp_safe_redirect( esc_url_raw( $redirect ) );
226 exit;
227 }
228
229 /**
230 * Empty implementation for print_messages.
231 *
232 * @return void
233 */
234 protected function print_messages() {
235 // none required.
236 }
237 }
238