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