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

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