| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly! |
| 4 |
} |
| 5 |
|
| 6 |
if ( ! class_exists( 'WPSC_Option_Controller' ) ) : |
| 7 |
|
| 8 |
final class WPSC_Option_Controller { |
| 9 |
|
| 10 |
/** |
| 11 |
* Initialize this class |
| 12 |
*/ |
| 13 |
public static function init() { |
| 14 |
|
| 15 |
add_action( 'wp_ajax_wpsc_add_new_option', array( __CLASS__, 'add_new' ) ); |
| 16 |
add_action( 'wp_ajax_wpsc_set_edit_option', array( __CLASS__, 'update_option' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Add new option |
| 21 |
* |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public static function add_new() { |
| 25 |
|
| 26 |
if ( check_ajax_referer( 'wpsc_add_new_option', '_ajax_nonce', false ) !== 1 ) { |
| 27 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 28 |
} |
| 29 |
|
| 30 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 31 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 32 |
} |
| 33 |
|
| 34 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 35 |
if ( ! $name ) { |
| 36 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 37 |
} |
| 38 |
|
| 39 |
$data = array( |
| 40 |
'name' => $name, |
| 41 |
'date_created' => ( new DateTime( 'now' ) )->format( 'Y-m-d H:m:s' ), |
| 42 |
); |
| 43 |
$option = WPSC_Option::insert( $data ); |
| 44 |
|
| 45 |
$response = array( |
| 46 |
'id' => $option->id, |
| 47 |
'name' => $option->name, |
| 48 |
); |
| 49 |
|
| 50 |
wp_send_json( $response ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Add new option |
| 55 |
* |
| 56 |
* @return void |
| 57 |
*/ |
| 58 |
public static function update_option() { |
| 59 |
|
| 60 |
if ( check_ajax_referer( 'wpsc_set_edit_option', '_ajax_nonce', false ) !== 1 ) { |
| 61 |
wp_send_json_error( 'Unauthorized request!', 401 ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( ! WPSC_Functions::is_site_admin() ) { |
| 65 |
wp_send_json_error( __( 'Unauthorized access!', 'supportcandy' ), 401 ); |
| 66 |
} |
| 67 |
|
| 68 |
$id = isset( $_POST['id'] ) ? intval( $_POST['id'] ) : 0; |
| 69 |
if ( ! $id ) { |
| 70 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 71 |
} |
| 72 |
|
| 73 |
$name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : ''; |
| 74 |
if ( ! $name ) { |
| 75 |
wp_send_json_error( __( 'Bad request!', 'supportcandy' ), 400 ); |
| 76 |
} |
| 77 |
|
| 78 |
$option = new WPSC_Option( $id ); |
| 79 |
$option->name = $name; |
| 80 |
$option->save(); |
| 81 |
|
| 82 |
$response = array( |
| 83 |
'id' => $option->id, |
| 84 |
'name' => $option->name, |
| 85 |
); |
| 86 |
|
| 87 |
wp_send_json( $response ); |
| 88 |
} |
| 89 |
} |
| 90 |
endif; |
| 91 |
|
| 92 |
WPSC_Option_Controller::init(); |
| 93 |
|