| 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( |
| 36 |
'code-snippets-settings-notices', |
| 37 |
'settings_reset', |
| 38 |
__( 'All settings have been reset to their defaults.', 'code-snippets' ), |
| 39 |
'updated' |
| 40 |
); |
| 41 |
|
| 42 |
set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 43 |
|
| 44 |
wp_redirect( esc_url_raw( add_query_arg( 'settings-updated', true, remove_query_arg( 'reset_settings' ) ) ) ); |
| 45 |
exit; |
| 46 |
} |
| 47 |
|
| 48 |
if ( is_network_admin() ) { |
| 49 |
|
| 50 |
if ( code_snippets_unified_settings() ) { |
| 51 |
$this->update_network_options(); |
| 52 |
} else { |
| 53 |
wp_redirect( code_snippets()->get_menu_url( 'settings', 'admin' ) ); |
| 54 |
exit; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Enqueue the stylesheet for the settings menu |
| 61 |
*/ |
| 62 |
public function enqueue_assets() { |
| 63 |
$plugin = code_snippets(); |
| 64 |
|
| 65 |
wp_enqueue_style( |
| 66 |
'code-snippets-edit', |
| 67 |
plugins_url( 'css/min/settings.css', $plugin->file ), |
| 68 |
array(), $plugin->version |
| 69 |
); |
| 70 |
|
| 71 |
code_snippets_editor_settings_preview_assets(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Render the admin screen |
| 76 |
*/ |
| 77 |
public function render() { |
| 78 |
$update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' ); |
| 79 |
|
| 80 |
?> |
| 81 |
<div class="wrap"> |
| 82 |
<h1><?php esc_html_e( 'Settings', 'code-snippets' ); |
| 83 |
|
| 84 |
if ( code_snippets()->admin->is_compact_menu() ) { |
| 85 |
|
| 86 |
printf( '<a href="%2$s" class="page-title-action">%1$s</a>', |
| 87 |
esc_html_x( 'Manage', 'snippets', 'code-snippets' ), |
| 88 |
code_snippets()->get_menu_url() |
| 89 |
); |
| 90 |
|
| 91 |
printf( '<a href="%2$s" class="page-title-action">%1$s</a>', |
| 92 |
esc_html_x( 'Add New', 'snippet', 'code-snippets' ), |
| 93 |
code_snippets()->get_menu_url( 'add' ) |
| 94 |
); |
| 95 |
|
| 96 |
printf( '<a href="%2$s" class="page-title-action">%1$s</a>', |
| 97 |
esc_html_x( 'Import', 'snippets', 'code-snippets' ), |
| 98 |
code_snippets()->get_menu_url( 'import' ) |
| 99 |
); |
| 100 |
} |
| 101 |
|
| 102 |
?></h1> |
| 103 |
|
| 104 |
<?php settings_errors( 'code-snippets-settings-notices' ); ?> |
| 105 |
|
| 106 |
<form action="<?php echo esc_url( $update_url ); ?>" method="post"> |
| 107 |
<?php |
| 108 |
|
| 109 |
settings_fields( 'code-snippets' ); |
| 110 |
do_settings_sections( 'code-snippets' ); |
| 111 |
|
| 112 |
?> |
| 113 |
<p class="submit" style="max-width: 1020px;"> |
| 114 |
<?php submit_button( null, 'primary', 'submit', false ); ?> |
| 115 |
|
| 116 |
<a class="button button-secondary" style="float: right;" |
| 117 |
href="<?php echo esc_url( add_query_arg( 'reset_settings', true ) ); ?>"> |
| 118 |
<?php esc_html_e( 'Reset to Default', 'code-snippets' ); ?> |
| 119 |
</a> |
| 120 |
</p> |
| 121 |
</form> |
| 122 |
</div> |
| 123 |
<?php |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Fill in for the Settings API in the Network Admin |
| 128 |
*/ |
| 129 |
function update_network_options() { |
| 130 |
|
| 131 |
/* Ensure the settings have been saved */ |
| 132 |
if ( ! isset( $_GET['update_site_option'], $_POST['code_snippets_settings'] ) || ! $_GET['update_site_option'] ) { |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
check_admin_referer( 'code-snippets-options' ); |
| 137 |
|
| 138 |
/* Retrieve the saved options and save them to the database */ |
| 139 |
$value = wp_unslash( $_POST['code_snippets_settings'] ); |
| 140 |
update_site_option( 'code_snippets_settings', $value ); |
| 141 |
|
| 142 |
/* Add an updated notice */ |
| 143 |
if ( ! count( get_settings_errors() ) ) { |
| 144 |
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'code-snippets' ), 'updated' ); |
| 145 |
} |
| 146 |
set_transient( 'settings_errors', get_settings_errors(), 30 ); |
| 147 |
|
| 148 |
/* Redirect back to the settings menu */ |
| 149 |
$goback = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) ); |
| 150 |
wp_redirect( $goback ); |
| 151 |
exit; |
| 152 |
} |
| 153 |
} |
| 154 |
|