| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* ctp_register_settings |
| 9 |
* CTP Register Settings |
| 10 |
*/ |
| 11 |
if ( ! function_exists( 'ctp_register_settings' ) ) { |
| 12 |
function ctp_register_settings() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ctp_ prefix kept for cross-plugin compatibility; wrapped in function_exists() guard. |
| 13 |
// register_setting( $option_group, $option_name, $sanitize_callback ) |
| 14 |
register_setting( |
| 15 |
'ctp-group', |
| 16 |
'ctp_options', |
| 17 |
array() |
| 18 |
); |
| 19 |
} |
| 20 |
} |
| 21 |
add_action( 'admin_init', 'ctp_register_settings' ); |
| 22 |
|
| 23 |
if ( ! function_exists( 'ctp_get_options' ) ) { |
| 24 |
/** |
| 25 |
* Returns the options array for ctp_get options |
| 26 |
* |
| 27 |
* @since 1.3 |
| 28 |
*/ |
| 29 |
function ctp_get_options() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ctp_ prefix kept for cross-plugin compatibility; wrapped in function_exists() guard. |
| 30 |
$defaults = ctp_default_options(); |
| 31 |
$options = get_option( 'ctp_options', $defaults ); |
| 32 |
|
| 33 |
return wp_parse_args( $options, $defaults ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
if ( ! function_exists( 'ctp_default_options' ) ) { |
| 38 |
/** |
| 39 |
* Return array of default options |
| 40 |
* |
| 41 |
* @since 1.3 |
| 42 |
* @return array default options. |
| 43 |
*/ |
| 44 |
function ctp_default_options( $option = null ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ctp_ prefix kept for cross-plugin compatibility; wrapped in function_exists() guard. |
| 45 |
$default_options['theme_plugin_tabs'] = 1; |
| 46 |
if ( null === $option ) { |
| 47 |
return apply_filters( 'ctp_options', $default_options ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Hook name kept for cross-plugin compatibility with other Catch plugins. |
| 48 |
} else { |
| 49 |
return $default_options[ $option ]; |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if ( ! function_exists( 'ctp_switch' ) ) { |
| 55 |
/** |
| 56 |
* AJAX switch handler |
| 57 |
* |
| 58 |
* @since 1.3 |
| 59 |
*/ |
| 60 |
function ctp_switch() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- ctp_ prefix kept for cross-plugin compatibility; wrapped in function_exists() guard. |
| 61 |
|
| 62 |
// Required fields check |
| 63 |
if ( ! isset( $_POST['security'], $_POST['value'], $_POST['option_name'] ) ) { |
| 64 |
wp_die( esc_html__( 'Invalid request.', 'essential-widgets' ) ); |
| 65 |
} |
| 66 |
|
| 67 |
// Sanitize & verify nonce |
| 68 |
$nonce = sanitize_text_field( wp_unslash( $_POST['security'] ) ); |
| 69 |
if ( ! wp_verify_nonce( $nonce, 'ew_switch_tabs_nonce' ) ) { |
| 70 |
wp_die( esc_html__( 'Unauthorized access!', 'essential-widgets' ) ); |
| 71 |
} |
| 72 |
|
| 73 |
// Capability check |
| 74 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 75 |
wp_die( esc_html__( 'Permission denied!', 'essential-widgets' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
// Sanitize remaining inputs |
| 79 |
$raw_value = sanitize_text_field( wp_unslash( $_POST['value'] ) ); |
| 80 |
$value = ( 'true' === $raw_value ) ? 1 : 0; |
| 81 |
|
| 82 |
$option_name = sanitize_key( wp_unslash( $_POST['option_name'] ) ); |
| 83 |
|
| 84 |
// Update option safely |
| 85 |
$option_value = ctp_get_options(); |
| 86 |
$option_value[ $option_name ] = $value; |
| 87 |
|
| 88 |
if ( update_option( 'ctp_options', $option_value ) ) { |
| 89 |
echo esc_html( (string) $value ); |
| 90 |
} else { |
| 91 |
esc_html_e( 'Connection Error. Please try again.', 'essential-widgets' ); |
| 92 |
} |
| 93 |
|
| 94 |
wp_die(); // Required for AJAX |
| 95 |
} |
| 96 |
} |
| 97 |
add_action( 'wp_ajax_ctp_switch', 'ctp_switch' ); |
| 98 |
|