update_network_options();
} else {
wp_redirect( code_snippets()->get_menu_url( 'settings', 'admin' ) );
exit;
}
}
}
/**
* Enqueue the stylesheet for the settings menu
*/
public function enqueue_assets() {
$plugin = code_snippets();
Settings\enqueue_editor_preview_assets();
wp_enqueue_style(
'code-snippets-settings',
plugins_url( 'css/min/settings.css', $plugin->file ),
[],
$plugin->version
);
}
/**
* Retrieve the list of settings sections.
*
* @return array
*/
private function get_sections() {
global $wp_settings_sections;
if ( ! isset( $wp_settings_sections[ self::SETTINGS_PAGE ] ) ) {
return array();
}
return (array) $wp_settings_sections[ self::SETTINGS_PAGE ];
}
/**
* Retrieve the name of the settings section currently being viewed.
*
* @param string $default Name of the default tab displayed.
*
* @return string
*/
public function get_current_section( $default = 'general' ) {
$sections = $this->get_sections();
if ( ! $sections ) {
return $default;
}
$active_tab = isset( $_REQUEST['section'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['section'] ) ) : $default;
return isset( $sections[ $active_tab ] ) ? $active_tab : $default;
}
/**
* Render the admin screen
*/
public function render() {
$update_url = is_network_admin() ? add_query_arg( 'update_site_option', true ) : admin_url( 'options.php' );
$current_section = $this->get_current_section();
?>
is_compact_menu() ) {
$actions = [
_x( 'Manage', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url(),
_x( 'Add New', 'snippet', 'code-snippets' ) => code_snippets()->get_menu_url( 'add' ),
_X( 'Import', 'snippets', 'code-snippets' ) => code_snippets()->get_menu_url( 'import' ),
];
foreach ( $actions as $label => $url ) {
printf(
'%s',
esc_url( $url ),
esc_html( $label )
);
}
}
?>
get_sections();
$active_tab = $this->get_current_section();
echo '';
foreach ( $sections as $section ) {
printf(
'%s',
esc_attr( $active_tab ) === $section['id'] ? ' nav-tab-active' : '',
esc_attr( $section['id'] ),
esc_html( $section['title'] )
);
}
echo '
';
foreach ( $sections as $section ) {
if ( $section['title'] ) {
printf(
'%s
' . "\n",
esc_attr( $section['id'] ),
esc_html( $section['title'] )
);
}
if ( $section['callback'] ) {
call_user_func( $section['callback'], $section );
}
printf( '';
}
}
/**
* Fill in for the Settings API in the Network Admin
*/
public function update_network_options() {
/* Ensure the settings have been saved */
if ( empty( $_GET['update_site_option'] ) || empty( $_POST['code_snippets_settings'] ) ) {
return;
}
check_admin_referer( 'code-snippets-options' );
/* Retrieve the saved options and save them to the database */
$value = map_deep( wp_unslash( $_POST['code_snippets_settings'] ), 'sanitize_key' );
update_site_option( 'code_snippets_settings', $value );
/* Add an updated notice */
if ( ! count( get_settings_errors() ) ) {
add_settings_error( 'general', 'settings_updated', __( 'Settings saved.', 'code-snippets' ), 'updated' );
}
set_transient( 'settings_errors', get_settings_errors(), 30 );
/* Redirect back to the settings menu */
$redirect = add_query_arg( 'settings-updated', 'true', remove_query_arg( 'update_site_option', wp_get_referer() ) );
wp_redirect( esc_url_raw( $redirect ) );
exit;
}
/**
* Empty implementation for print_messages.
*
* @return void
*/
protected function print_messages() {
// none required.
}
}