backup: estado antes de limpieza de defaults
This commit is contained in:
214
admin/theme-options/theme-options.php
Normal file
214
admin/theme-options/theme-options.php
Normal file
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
/**
|
||||
* Theme Options Admin Page
|
||||
*
|
||||
* @package Apus_Theme
|
||||
* @since 1.0.0
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add admin menu
|
||||
*/
|
||||
function apus_add_admin_menu() {
|
||||
add_theme_page(
|
||||
__('Apus Theme Options', 'apus-theme'), // Page title
|
||||
__('Theme Options', 'apus-theme'), // Menu title
|
||||
'manage_options', // Capability
|
||||
'apus-theme-options', // Menu slug
|
||||
'apus_render_options_page', // Callback function
|
||||
30 // Position
|
||||
);
|
||||
}
|
||||
add_action('admin_menu', 'apus_add_admin_menu');
|
||||
|
||||
/**
|
||||
* Render the options page
|
||||
*/
|
||||
function apus_render_options_page() {
|
||||
// Check user capabilities
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_die(__('You do not have sufficient permissions to access this page.', 'apus-theme'));
|
||||
}
|
||||
|
||||
// Load the template
|
||||
include get_template_directory() . '/admin/theme-options/options-page-template.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue admin scripts and styles
|
||||
*/
|
||||
function apus_enqueue_admin_scripts($hook) {
|
||||
// Only load on our theme options page
|
||||
if ($hook !== 'appearance_page_apus-theme-options') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Enqueue WordPress media uploader
|
||||
wp_enqueue_media();
|
||||
|
||||
// Enqueue admin styles
|
||||
wp_enqueue_style(
|
||||
'apus-admin-options',
|
||||
get_template_directory_uri() . '/admin/assets/css/theme-options.css',
|
||||
array(),
|
||||
APUS_VERSION
|
||||
);
|
||||
|
||||
// Enqueue admin scripts
|
||||
wp_enqueue_script(
|
||||
'apus-admin-options',
|
||||
get_template_directory_uri() . '/admin/assets/js/theme-options.js',
|
||||
array('jquery', 'wp-color-picker'),
|
||||
APUS_VERSION,
|
||||
true
|
||||
);
|
||||
|
||||
// Localize script
|
||||
wp_localize_script('apus-admin-options', 'apusAdminOptions', array(
|
||||
'ajaxUrl' => admin_url('admin-ajax.php'),
|
||||
'nonce' => wp_create_nonce('apus_admin_nonce'),
|
||||
'strings' => array(
|
||||
'selectImage' => __('Select Image', 'apus-theme'),
|
||||
'useImage' => __('Use Image', 'apus-theme'),
|
||||
'removeImage' => __('Remove Image', 'apus-theme'),
|
||||
'confirmReset' => __('Are you sure you want to reset all options to default values? This cannot be undone.', 'apus-theme'),
|
||||
'saved' => __('Settings saved successfully!', 'apus-theme'),
|
||||
'error' => __('An error occurred while saving settings.', 'apus-theme'),
|
||||
),
|
||||
));
|
||||
}
|
||||
add_action('admin_enqueue_scripts', 'apus_enqueue_admin_scripts');
|
||||
|
||||
/**
|
||||
* Add settings link to theme actions
|
||||
*/
|
||||
function apus_add_settings_link($links) {
|
||||
$settings_link = '<a href="' . admin_url('themes.php?page=apus-theme-options') . '">' . __('Settings', 'apus-theme') . '</a>';
|
||||
array_unshift($links, $settings_link);
|
||||
return $links;
|
||||
}
|
||||
add_filter('theme_action_links_' . get_template(), 'apus_add_settings_link');
|
||||
|
||||
/**
|
||||
* AJAX handler for resetting options
|
||||
*/
|
||||
function apus_reset_options_ajax() {
|
||||
check_ajax_referer('apus_admin_nonce', 'nonce');
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(array('message' => __('Insufficient permissions.', 'apus-theme')));
|
||||
}
|
||||
|
||||
// Delete options to reset to defaults
|
||||
delete_option('apus_theme_options');
|
||||
|
||||
wp_send_json_success(array('message' => __('Options reset to defaults successfully.', 'apus-theme')));
|
||||
}
|
||||
add_action('wp_ajax_apus_reset_options', 'apus_reset_options_ajax');
|
||||
|
||||
/**
|
||||
* AJAX handler for exporting options
|
||||
*/
|
||||
function apus_export_options_ajax() {
|
||||
check_ajax_referer('apus_admin_nonce', 'nonce');
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(array('message' => __('Insufficient permissions.', 'apus-theme')));
|
||||
}
|
||||
|
||||
$options = get_option('apus_theme_options', array());
|
||||
|
||||
wp_send_json_success(array(
|
||||
'data' => json_encode($options, JSON_PRETTY_PRINT),
|
||||
'filename' => 'apus-theme-options-' . date('Y-m-d') . '.json'
|
||||
));
|
||||
}
|
||||
add_action('wp_ajax_apus_export_options', 'apus_export_options_ajax');
|
||||
|
||||
/**
|
||||
* AJAX handler for importing options
|
||||
*/
|
||||
function apus_import_options_ajax() {
|
||||
check_ajax_referer('apus_admin_nonce', 'nonce');
|
||||
|
||||
if (!current_user_can('manage_options')) {
|
||||
wp_send_json_error(array('message' => __('Insufficient permissions.', 'apus-theme')));
|
||||
}
|
||||
|
||||
if (!isset($_POST['import_data'])) {
|
||||
wp_send_json_error(array('message' => __('No import data provided.', 'apus-theme')));
|
||||
}
|
||||
|
||||
$import_data = json_decode(stripslashes($_POST['import_data']), true);
|
||||
|
||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
||||
wp_send_json_error(array('message' => __('Invalid JSON data.', 'apus-theme')));
|
||||
}
|
||||
|
||||
// Sanitize imported data
|
||||
$sanitized_data = apus_sanitize_options($import_data);
|
||||
|
||||
// Update options
|
||||
update_option('apus_theme_options', $sanitized_data);
|
||||
|
||||
wp_send_json_success(array('message' => __('Options imported successfully.', 'apus-theme')));
|
||||
}
|
||||
add_action('wp_ajax_apus_import_options', 'apus_import_options_ajax');
|
||||
|
||||
/**
|
||||
* Add admin notices
|
||||
*/
|
||||
function apus_admin_notices() {
|
||||
$screen = get_current_screen();
|
||||
if ($screen->id !== 'appearance_page_apus-theme-options') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if settings were updated
|
||||
if (isset($_GET['settings-updated']) && $_GET['settings-updated'] === 'true') {
|
||||
?>
|
||||
<div class="notice notice-success is-dismissible">
|
||||
<p><?php _e('Settings saved successfully!', 'apus-theme'); ?></p>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
add_action('admin_notices', 'apus_admin_notices');
|
||||
|
||||
/**
|
||||
* Register theme options in Customizer as well (for preview)
|
||||
*/
|
||||
function apus_customize_register($wp_customize) {
|
||||
// Add a panel for theme options
|
||||
$wp_customize->add_panel('apus_theme_options', array(
|
||||
'title' => __('Apus Theme Options', 'apus-theme'),
|
||||
'description' => __('Configure theme options (Also available in Theme Options page)', 'apus-theme'),
|
||||
'priority' => 10,
|
||||
));
|
||||
|
||||
// General Section
|
||||
$wp_customize->add_section('apus_general', array(
|
||||
'title' => __('General Settings', 'apus-theme'),
|
||||
'panel' => 'apus_theme_options',
|
||||
'priority' => 10,
|
||||
));
|
||||
|
||||
// Enable breadcrumbs
|
||||
$wp_customize->add_setting('apus_theme_options[enable_breadcrumbs]', array(
|
||||
'default' => true,
|
||||
'type' => 'option',
|
||||
'sanitize_callback' => 'apus_sanitize_checkbox',
|
||||
));
|
||||
|
||||
$wp_customize->add_control('apus_theme_options[enable_breadcrumbs]', array(
|
||||
'label' => __('Enable Breadcrumbs', 'apus-theme'),
|
||||
'section' => 'apus_general',
|
||||
'type' => 'checkbox',
|
||||
));
|
||||
}
|
||||
add_action('customize_register', 'apus_customize_register');
|
||||
Reference in New Issue
Block a user