| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This class handles the settings admin menu |
| 5 |
* @since 2.4.0 |
| 6 |
* @package Code_Snippets |
| 7 |
*/ |
| 8 |
class Code_Snippets_Settings_Menu extends Code_Snippets_Admin_Menu { |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
|
| 15 |
parent::__construct( 'settings', |
| 16 |
_x( 'Settings', 'menu label', 'code-snippets' ), |
| 17 |
__( 'Snippets Settings', 'code-snippets' ) |
| 18 |
); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Executed when the admin page is loaded |
| 23 |
*/ |
| 24 |
public function load() { |
| 25 |
parent::load(); |
| 26 |
|
| 27 |
if ( isset( $_GET['reset_settings'] ) && $_GET['reset_settings'] ) { |
| 28 |
|
| 29 |
if ( code_snippets_unified_settings() ) { |
| 30 |
delete_site_option( 'code_snippets_settings' ); |
| 31 |
} else { |
| 32 |
delete_option( 'code_snippets_settings' ); |
| 33 |
} |
| 34 |
|
| 35 |
add_settings_error( 'code-snippets-settings-notices', 'settings_reset', __( 'All settings have been reset to their defaults.' ), 'updated' ); |
| 36 |
set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 37 |
|
| 38 |
wp_redirect( esc_url_raw( add_query_arg( 'settings-updated', true, remove_query_arg( 'reset_settings' ) ) ) ); |
| 39 |
exit; |
| 40 |
} |
| 41 |
|
| 42 |
if ( is_network_admin() ) { |
| 43 |
|
| 44 |
if ( code_snippets_unified_settings() ) { |
| 45 |
$this->update_network_options(); |
| 46 |
} else { |
| 47 |
wp_redirect( code_snippets()->get_menu_url( 'settings', 'admin' ) ); |
| 48 |
exit; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Enqueue the stylesheet for the settings menu |
| 55 |
*/ |
| 56 |
public function enqueue_assets() { |
| 57 |
$plugin = code_snippets(); |
| 58 |
|
| 59 |
wp_enqueue_style( |
| 60 |
'code-snippets-edit', |
| 61 |
plugins_url( 'css/min/settings.css', $plugin->file ), |
| 62 |
array(), $plugin->version |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Render the admin screen |
| 68 |
*/ |
| 69 |
public function render() { |
| 70 |
$update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' ); |
| 71 |
|
| 72 |
?> |
| 73 |
<div class="wrap"> |
| 74 |
<h1><?php esc_html_e( 'Settings', 'code-snippets' ); ?></h1> |
| 75 |
|
| 76 |
<?php settings_errors( 'code-snippets-settings-notices' ); ?> |
| 77 |
|
| 78 |
<form action="<?php echo esc_url( $update_url ); ?>" method="post"> |
| 79 |
<?php |
| 80 |
|
| 81 |
settings_fields( 'code-snippets' ); |
| 82 |
do_settings_sections( 'code-snippets' ); |
| 83 |
|
| 84 |
?> |
| 85 |
<p class="submit" style="max-width: 1020px;"> |
| 86 |
<?php submit_button( null, 'primary', 'submit', false ); ?> |
| 87 |
|
| 88 |
<a class="button button-secondary" style="float: right;" |
| 89 |
href="<?php echo esc_url( add_query_arg( 'reset_settings', true ) ); ?>"> |
| 90 |
<?php esc_html_e( 'Reset to Default', 'code-snippets' ); ?> |
| 91 |
</a> |
| 92 |
</p> |
| 93 |
</form> |
| 94 |
</div> |
| 95 |
<?php |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Fill in for the Settings API in the Network Admin |
| 100 |
*/ |
| 101 |
function update_network_options() { |
| 102 |
|
| 103 |
/* Ensure the settings have been saved */ |
| 104 |
if ( ! isset( $_GET['update_site_option'], $_POST['code_snippets_settings'] ) || ! $_GET['update_site_option'] ) { |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
check_admin_referer( 'code-snippets-options' ); |
| 109 |
|
| 110 |
/* Retrieve the saved options and save them to the database */ |
| 111 |
$value = wp_unslash( $_POST['code_snippets_settings'] ); |
| 112 |
update_site_option( 'code_snippets_settings', $value ); |
| 113 |
|
| 114 |
/* Add an updated notice */ |
| 115 |
if ( ! count( get_settings_errors() ) ) { |
| 116 |
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' ); |
| 117 |
} |
| 118 |
set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 119 |
|
| 120 |
/* Redirect back to the settings menu */ |
| 121 |
$goback = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) ); |
| 122 |
wp_redirect( $goback ); |
| 123 |
exit; |
| 124 |
} |
| 125 |
} |
| 126 |
|