| 1 |
<?php |
| 2 |
|
| 3 |
namespace Sellkit\Admin\Settings; |
| 4 |
|
| 5 |
defined( 'ABSPATH' ) || die(); |
| 6 |
|
| 7 |
/** |
| 8 |
* Steps class. |
| 9 |
* |
| 10 |
* @since 1.1.0 |
| 11 |
*/ |
| 12 |
class Sellkit_Admin_Settings { |
| 13 |
|
| 14 |
/** |
| 15 |
* Class instance. |
| 16 |
* |
| 17 |
* @since 1.1.0 |
| 18 |
* @var Sellkit_Admin_Settings |
| 19 |
*/ |
| 20 |
private static $instance = null; |
| 21 |
|
| 22 |
/** |
| 23 |
* Sellkit_Admin_Settings constructor. |
| 24 |
* |
| 25 |
* @since 1.1.0 |
| 26 |
*/ |
| 27 |
public function __construct() { |
| 28 |
add_action( 'wp_ajax_sellkit_save_settings', [ $this, 'save_settings' ] ); |
| 29 |
add_action( 'wp_ajax_sellkit_get_settings', [ $this, 'get_settings' ] ); |
| 30 |
|
| 31 |
add_action( 'wp_ajax_sellkit_remove_content_box', [ $this, 'remove_content_box' ] ); |
| 32 |
|
| 33 |
add_action( 'wp_ajax_sellkit_settings_get_templates', [ $this, 'get_templates' ] ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get the class instance. |
| 38 |
* |
| 39 |
* @since 1.1.0 |
| 40 |
*/ |
| 41 |
public static function get_instance() { |
| 42 |
if ( null === self::$instance ) { |
| 43 |
self::$instance = new self(); |
| 44 |
} |
| 45 |
|
| 46 |
return self::$instance; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Saving Settings. |
| 51 |
* |
| 52 |
* @since 1.1.0 |
| 53 |
*/ |
| 54 |
public function save_settings() { |
| 55 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 56 |
|
| 57 |
$fields = sellkit_post( 'fields' ); |
| 58 |
|
| 59 |
if ( ! is_array( $fields ) ) { |
| 60 |
wp_send_json_error( __( 'Somethings went wrong.', 'sellkit' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
foreach ( $fields as $option => $value ) { |
| 64 |
$safe_value = ! is_array( $value ) ? sanitize_text_field( $value ) : $value; |
| 65 |
$this->sellkit_update_option( $option, $safe_value ); |
| 66 |
|
| 67 |
if ( 'delete_data' === $option ) { |
| 68 |
update_site_option( 'delete_data', $safe_value ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
wp_send_json_success( __( 'The update process has been completed.', 'sellkit' ) ); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Gets sellkit Settings. |
| 77 |
* |
| 78 |
* @since 1.1.0 |
| 79 |
*/ |
| 80 |
public function get_settings() { |
| 81 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 82 |
|
| 83 |
$options = get_option( 'sellkit', [] ); |
| 84 |
|
| 85 |
wp_send_json_success( $options ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Update Sellkit options. |
| 90 |
* |
| 91 |
* @since 1.1.0 |
| 92 |
* @return bool |
| 93 |
* @param string $option Option. |
| 94 |
* @param string $value String. |
| 95 |
*/ |
| 96 |
public function sellkit_update_option( $option, $value ) { |
| 97 |
$options = get_option( 'sellkit', [] ); |
| 98 |
|
| 99 |
// No need to update the same value. |
| 100 |
if ( isset( $options[ $option ] ) && $value === $options[ $option ] ) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
// Update the option. |
| 105 |
$options[ $option ] = $value; |
| 106 |
|
| 107 |
update_option( 'sellkit', $options ); |
| 108 |
|
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Removes content box. |
| 114 |
* |
| 115 |
* @since 1.1.0 |
| 116 |
*/ |
| 117 |
public function remove_content_box() { |
| 118 |
$new_content_box_id = sellkit_htmlspecialchars( INPUT_POST, 'content_box_id' ); |
| 119 |
$removed_content_box_data = sellkit_get_option( 'removed_content_box_data' ); |
| 120 |
$removed_content_box_data = empty( $removed_content_box_data ) ? [] : $removed_content_box_data; |
| 121 |
$new_removed_content_box_data = array_merge( $removed_content_box_data, [ $new_content_box_id ] ); |
| 122 |
|
| 123 |
sellkit_update_option( 'removed_content_box_data', array_unique( $new_removed_content_box_data ) ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Gets removed content box. |
| 128 |
* |
| 129 |
* @since 1.1.0 |
| 130 |
* @return array|bool|mixed |
| 131 |
*/ |
| 132 |
public static function get_removed_content_box() { |
| 133 |
$removed_content_boxes = sellkit_get_option( 'removed_content_box_data' ); |
| 134 |
|
| 135 |
return ! empty( $removed_content_boxes ) ? $removed_content_boxes : []; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Gets Elementor templates for sellkit settings to set empty cart template. |
| 140 |
* |
| 141 |
* @since 1.1.0 |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function get_templates() { |
| 145 |
check_ajax_referer( 'sellkit', 'nonce' ); |
| 146 |
|
| 147 |
$input = filter_input( INPUT_GET, 'input_value', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); |
| 148 |
|
| 149 |
if ( empty( $input ) ) { |
| 150 |
wp_send_json_error(); |
| 151 |
} |
| 152 |
|
| 153 |
$filtered_templates = []; |
| 154 |
$args = [ |
| 155 |
'post_type' => 'elementor_library', |
| 156 |
'post_status' => 'publish', |
| 157 |
's' => $input, |
| 158 |
'posts_per_page' => 20, |
| 159 |
]; |
| 160 |
|
| 161 |
$query = new \WP_Query( $args ); |
| 162 |
|
| 163 |
if ( $query->have_posts() ) { |
| 164 |
while ( $query->have_posts() ) { |
| 165 |
$query->the_post(); |
| 166 |
$filtered_templates[] = [ |
| 167 |
'label' => html_entity_decode( get_the_title() ), |
| 168 |
'value' => get_the_ID(), |
| 169 |
]; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
wp_send_json_success( $filtered_templates ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
Sellkit_Admin_Settings::get_instance(); |
| 178 |
|