| 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 |
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 |
* Render the admin screen |
| 55 |
*/ |
| 56 |
function render() { |
| 57 |
$update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' ); |
| 58 |
|
| 59 |
?> |
| 60 |
<div class="wrap"> |
| 61 |
<h1><?php esc_html_e( 'Settings', 'code-snippets' ); ?></h1> |
| 62 |
|
| 63 |
<?php settings_errors( 'code-snippets-settings-notices' ); ?> |
| 64 |
|
| 65 |
<form action="<?php echo esc_url( $update_url ); ?>" method="post"> |
| 66 |
<?php |
| 67 |
|
| 68 |
settings_fields( 'code-snippets' ); |
| 69 |
do_settings_sections( 'code-snippets' ); |
| 70 |
|
| 71 |
?> |
| 72 |
<p class="submit" style="max-width: 1020px;"> |
| 73 |
<?php submit_button( null, 'primary', 'submit', false ); ?> |
| 74 |
|
| 75 |
<a class="button button-secondary" style="float: right;" |
| 76 |
href="<?php echo esc_url( add_query_arg( 'reset_settings', true ) ); ?>"> |
| 77 |
<?php esc_html_e( 'Reset to Default', 'code-snippets' ); ?> |
| 78 |
</a> |
| 79 |
</p> |
| 80 |
</form> |
| 81 |
</div> |
| 82 |
<?php |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Fill in for the Settings API in the Network Admin |
| 87 |
*/ |
| 88 |
function update_network_options() { |
| 89 |
|
| 90 |
/* Ensure the settings have been saved */ |
| 91 |
if ( ! isset( $_GET['update_site_option'], $_POST['code_snippets_settings'] ) || ! $_GET['update_site_option'] ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
check_admin_referer( 'code-snippets-options' ); |
| 96 |
|
| 97 |
/* Retrieve the saved options and save them to the database */ |
| 98 |
$value = wp_unslash( $_POST['code_snippets_settings'] ); |
| 99 |
update_site_option( 'code_snippets_settings', $value ); |
| 100 |
|
| 101 |
/* Add an updated notice */ |
| 102 |
if ( ! count( get_settings_errors() ) ) { |
| 103 |
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.' ), 'updated' ); |
| 104 |
} |
| 105 |
set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 106 |
|
| 107 |
/* Redirect back to the settings menu */ |
| 108 |
$goback = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) ); |
| 109 |
wp_redirect( $goback ); |
| 110 |
exit; |
| 111 |
} |
| 112 |
} |
| 113 |
|