| 1 |
<?php |
| 2 |
/** |
| 3 |
* ctp_register_settings |
| 4 |
* CTP Register Settings |
| 5 |
*/ |
| 6 |
if ( ! function_exists( 'ctp_register_settings' ) ) { |
| 7 |
function ctp_register_settings() { |
| 8 |
// register_setting( $option_group, $option_name, $sanitize_callback ) |
| 9 |
register_setting( |
| 10 |
'ctp-group', |
| 11 |
'ctp_options', |
| 12 |
array() |
| 13 |
); |
| 14 |
} |
| 15 |
} |
| 16 |
add_action( 'admin_init', 'ctp_register_settings' ); |
| 17 |
|
| 18 |
if ( ! function_exists( 'ctp_get_options' ) ) { |
| 19 |
/** |
| 20 |
* Returns the options array for ctp_get options |
| 21 |
* |
| 22 |
* @since 1.3 |
| 23 |
*/ |
| 24 |
function ctp_get_options() { |
| 25 |
$defaults = ctp_default_options(); |
| 26 |
$options = get_option( 'ctp_options', $defaults ); |
| 27 |
|
| 28 |
return wp_parse_args( $options, $defaults ); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! function_exists( 'ctp_default_options' ) ) { |
| 33 |
/** |
| 34 |
* Return array of default options |
| 35 |
* |
| 36 |
* @since 1.3 |
| 37 |
* @return array default options. |
| 38 |
*/ |
| 39 |
function ctp_default_options( $option = null ) { |
| 40 |
$default_options['theme_plugin_tabs'] = 1; |
| 41 |
if ( null == $option ) { |
| 42 |
return apply_filters( 'ctp_options', $default_options ); |
| 43 |
} else { |
| 44 |
return $default_options[ $option ]; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
if ( ! function_exists( 'ctp_switch' ) ) { |
| 50 |
/** |
| 51 |
* Return $string |
| 52 |
* |
| 53 |
* @since 1.3 |
| 54 |
* @return $string 1 or 2. |
| 55 |
*/ |
| 56 |
function ctp_switch() { |
| 57 |
if ( ! wp_verify_nonce( $_POST['security'], 'ew_switch_tabs_nonce' ) ) { |
| 58 |
wp_die( esc_html__( 'Unauthorized access!', 'essential-widgets' ) ); |
| 59 |
} else { |
| 60 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 61 |
wp_die( esc_html__( 'Permission denied!', 'essential-widgets' ) ); |
| 62 |
} |
| 63 |
$value = ( 'true' == $_POST['value'] ) ? 1 : 0; |
| 64 |
|
| 65 |
$option_name = $_POST['option_name']; |
| 66 |
|
| 67 |
$option_value = ctp_get_options(); |
| 68 |
|
| 69 |
$option_value[ $option_name ] = $value; |
| 70 |
|
| 71 |
if ( update_option( 'ctp_options', $option_value ) ) { |
| 72 |
echo $value; |
| 73 |
} else { |
| 74 |
esc_html_e( 'Connection Error. Please try again.', 'essential-widgets' ); |
| 75 |
} |
| 76 |
} |
| 77 |
wp_die(); // this is required to terminate immediately and return a proper response |
| 78 |
} |
| 79 |
} |
| 80 |
add_action( 'wp_ajax_ctp_switch', 'ctp_switch' ); |
| 81 |
|