| 1 |
<?php |
| 2 |
/** |
| 3 |
* The file that defines the core plugin class |
| 4 |
* |
| 5 |
* @link https://posimyth.com/ |
| 6 |
* @since 1.1.3 |
| 7 |
* |
| 8 |
* @package Wdesignkit |
| 9 |
* @subpackage Wdesignkit/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Exit if accessed directly. |
| 14 |
* */ |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
if ( ! class_exists( 'Wdkit_Preset_Ajax' ) ) { |
| 20 |
|
| 21 |
/** |
| 22 |
* It is wdesignkit Main Class |
| 23 |
* |
| 24 |
* @since 1.1.7 |
| 25 |
*/ |
| 26 |
class Wdkit_Preset_Ajax { |
| 27 |
|
| 28 |
/** |
| 29 |
* Member Variable |
| 30 |
* |
| 31 |
* @var instance |
| 32 |
*/ |
| 33 |
private static $instance; |
| 34 |
|
| 35 |
/** |
| 36 |
* Member Variable |
| 37 |
* |
| 38 |
* @var staring $wdkit_api |
| 39 |
*/ |
| 40 |
public $wdkit_api = WDKIT_SERVER_API_URL . 'api/wp/'; |
| 41 |
|
| 42 |
/** |
| 43 |
* Initiator |
| 44 |
*/ |
| 45 |
public static function get_instance() { |
| 46 |
if ( ! isset( self::$instance ) ) { |
| 47 |
self::$instance = new self(); |
| 48 |
} |
| 49 |
|
| 50 |
return self::$instance; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Define the core functionality of the plugin. |
| 55 |
*/ |
| 56 |
public function __construct() { |
| 57 |
add_filter( 'wp_wdkit_preset_ajax', array( $this, 'wp_wdkit_preset_ajax_call' ) ); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get Wdkit Api Call Ajax. |
| 62 |
* |
| 63 |
* @since 1.1.1 |
| 64 |
*/ |
| 65 |
public function wp_wdkit_preset_ajax_call( $type ) { |
| 66 |
|
| 67 |
check_ajax_referer( 'wdkit_nonce', 'kit_nonce' ); |
| 68 |
|
| 69 |
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { |
| 70 |
wp_send_json_error( array( 'content' => __( 'Insufficient permissions.', 'wdesignkit' ) ) ); |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! $type ) { |
| 74 |
$response = $this->tpae_set_response( false, 'Invalid Permission.', 'Something went wrong.' ); |
| 75 |
|
| 76 |
wp_send_json( $response ); |
| 77 |
wp_die(); |
| 78 |
} |
| 79 |
|
| 80 |
switch ( $type ) { |
| 81 |
case 'wdkit_preset_template': |
| 82 |
$data = $this->wdkit_preset_template(); |
| 83 |
break; |
| 84 |
case 'wdkit_preset_dwnld_template': |
| 85 |
$data = $this->wdkit_preset_dwnld_template(); |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
wp_send_json( $data ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* It is used for calling Preset API |
| 94 |
* |
| 95 |
* @since 1.1.7 |
| 96 |
*/ |
| 97 |
public function wdkit_preset_template() { |
| 98 |
$array_data = array( |
| 99 |
'BuilderType' => isset( $_POST['buildertype'] ) ? wp_unslash( $_POST['buildertype'] ) : '', |
| 100 |
'perpage' => isset( $_POST['perpage'] ) ? (int) $_POST['perpage'] : 8, |
| 101 |
'page' => isset( $_POST['page'] ) ? (int) $_POST['page'] : 1, |
| 102 |
'free_pro' => isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : '', |
| 103 |
'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '', |
| 104 |
'plugin' => isset( $_POST['plugin'] ) ? wp_unslash($_POST['plugin']) : json_encode(array(1014)), |
| 105 |
'key_words' => isset( $_POST['key_words'] ) ? wp_unslash($_POST['key_words']) : [], |
| 106 |
); |
| 107 |
|
| 108 |
$temp_id = isset( $_POST['template_id'] ) ? $_POST['template_id'] : ''; |
| 109 |
|
| 110 |
if ( empty( $temp_id ) ) { |
| 111 |
$response = array( |
| 112 |
'success' => false, |
| 113 |
'message' => esc_html__( 'Data Not Found', 'wdesignkit' ), |
| 114 |
'description' => esc_html__( 'Preset ID Not Found', 'wdesignkit' ), |
| 115 |
); |
| 116 |
|
| 117 |
wp_send_json( $response ); |
| 118 |
wp_die(); |
| 119 |
} |
| 120 |
|
| 121 |
$response = $this->wkit_api_call( $array_data, 'preset/templates/' . $temp_id ); |
| 122 |
|
| 123 |
$manage_licence = array(); |
| 124 |
$theplus_active_check = is_plugin_active( 'the-plus-addons-for-elementor-page-builder/theplus_elementor_addon.php' ); |
| 125 |
$nexter_active_check = is_plugin_active( 'the-plus-addons-for-block-editor/the-plus-addons-for-block-editor.php' ); |
| 126 |
$manage_licence['theplus_elementor_addon'] = !empty ( defined( 'THEPLUS_VERSION' ) ) ? true : false; |
| 127 |
$manage_licence['the-plus-addons-for-elementor-page-builder'] = !empty ( $theplus_active_check ) ? true : false; |
| 128 |
$manage_licence['tpag'] = !empty ( defined( 'TPGBP_VERSION' ) ) ? true : false; |
| 129 |
$manage_licence['elementor-pro'] = !empty ( defined( 'ELEMENTOR_PRO_VERSION' ) ) ? true : false; |
| 130 |
|
| 131 |
$success = ! empty( $response['success'] ) ? $response['success'] : false; |
| 132 |
|
| 133 |
if ( empty( $success ) ) { |
| 134 |
$response = array( |
| 135 |
'success' => false, |
| 136 |
'message' => esc_html__( 'Data Not Found', 'wdesignkit' ), |
| 137 |
'description' => esc_html__( 'Preset List Not Found', 'wdesignkit' ), |
| 138 |
); |
| 139 |
|
| 140 |
wp_send_json( $response ); |
| 141 |
wp_die(); |
| 142 |
} |
| 143 |
|
| 144 |
$response = json_decode( wp_json_encode( $response['data'] ), true ); |
| 145 |
$response['manage_licence'] = $manage_licence; |
| 146 |
|
| 147 |
wp_send_json( $response ); |
| 148 |
wp_die(); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* It is used for downloading Preset Template |
| 153 |
* |
| 154 |
* @since 1.1.7 |
| 155 |
*/ |
| 156 |
public function wdkit_preset_dwnld_template() { |
| 157 |
|
| 158 |
$builder = isset( $_POST['builder'] ) ? sanitize_text_field( wp_unslash( $_POST['builder'] ) ) : ''; |
| 159 |
$sheCheck = isset( $_POST['sheCheck'] ) ? sanitize_text_field( wp_unslash( $_POST['sheCheck'] ) ) : false; |
| 160 |
$ele_mega_menu = get_option('elementor_experiment-mega-menu', false); |
| 161 |
|
| 162 |
if ( ('elementor' == $builder) && $sheCheck ){ |
| 163 |
if ( isset( $ele_mega_menu ) ){ |
| 164 |
update_option('elementor_experiment-mega-menu', 'active'); |
| 165 |
} else { |
| 166 |
add_option('elementor_experiment-mega-menu', 'active'); |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
$array_data = array( |
| 171 |
'id' => isset( $_POST['id'] ) ? (int) $_POST['id'] : '', |
| 172 |
'builder' => $builder, |
| 173 |
'free_pro' => isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : '', |
| 174 |
'product_name' => isset( $_POST['product_name'] ) ? sanitize_text_field( wp_unslash( $_POST['product_name'] ) ) : '', |
| 175 |
); |
| 176 |
|
| 177 |
if ( $array_data['free_pro'] == 'pro' ) { |
| 178 |
if ( $array_data['builder'] === 'elementor' ) { |
| 179 |
if ( empty ( defined( 'THEPLUS_VERSION' )) && empty( $sheCheck )) { |
| 180 |
$response = $this->tpae_set_response( false, 'Invalid Permission.', 'Something went wrong.' ); |
| 181 |
|
| 182 |
wp_send_json( $response ); |
| 183 |
wp_die(); |
| 184 |
} |
| 185 |
|
| 186 |
$array_data['license'] = 'activate'; |
| 187 |
$array_data['license_key'] = $theplus_licence['license_key']; |
| 188 |
|
| 189 |
} else if ( $array_data['builder'] === 'gutenberg' ) { |
| 190 |
if ( empty ( defined( 'TPGBP_VERSION' ) ) ) { |
| 191 |
$response = $this->tpae_set_response( false, 'Invalid Permission.', 'Something went wrong.' ); |
| 192 |
|
| 193 |
wp_send_json( $response ); |
| 194 |
wp_die(); |
| 195 |
} |
| 196 |
|
| 197 |
$array_data['license'] = 'activate'; |
| 198 |
$array_data['license_key'] = $nexter_licence['tpgb_activate_key']; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
if ( isset( $_POST['id'] ) && ! empty( $_POST['id'] ) ) { |
| 203 |
$api_preset_download_path = 'preset/templates/download'; |
| 204 |
} else { |
| 205 |
$response = array( |
| 206 |
'success' => false, |
| 207 |
'message' => esc_html__( 'Data Not Found', 'wdesignkit' ), |
| 208 |
'description' => esc_html__( 'Sorry , Invalid ID', 'wdesignkit' ), |
| 209 |
); |
| 210 |
|
| 211 |
wp_send_json( $response ); |
| 212 |
wp_die(); |
| 213 |
} |
| 214 |
|
| 215 |
$response = WDesignKit_Data_Query::get_data( $api_preset_download_path, $array_data ); |
| 216 |
/* Normalised before any subscript: a failed request returns WP_Error, and in |
| 217 |
* PHP 8 even empty() on it is a fatal "Cannot use object of type WP_Error as |
| 218 |
* array". */ |
| 219 |
$response = is_array( $response ) ? $response : array(); |
| 220 |
$success = ! empty( $response['success'] ) ? $response['success'] : false; |
| 221 |
|
| 222 |
if ( empty( $success ) ) { |
| 223 |
$response = array( |
| 224 |
'success' => false, |
| 225 |
'message' => esc_html__( 'Data Not Found', 'wdesignkit' ), |
| 226 |
'description' => esc_html__( 'Sorry , Cannot Download', 'wdesignkit' ), |
| 227 |
); |
| 228 |
|
| 229 |
wp_send_json( $response ); |
| 230 |
wp_die(); |
| 231 |
} |
| 232 |
|
| 233 |
$custom_meta = isset( $_POST['custom_meta'] ) ? sanitize_text_field( wp_unslash( $_POST['custom_meta'] ) ) : false; |
| 234 |
|
| 235 |
/** Custom meta Field */ |
| 236 |
if ( ! empty( $custom_meta ) && 'true' === $custom_meta && ! empty( $response ) && ! empty( $response['content'] ) ) { |
| 237 |
|
| 238 |
$res_content = json_decode( $response['content'], true ); |
| 239 |
if ( isset( $res_content['custom_meta'] ) && ! empty( $res_content['custom_meta'] ) ) { |
| 240 |
$meta_data = $res_content['custom_meta']; |
| 241 |
|
| 242 |
if ( ! empty( $meta_data ) ) { |
| 243 |
foreach ( $meta_data as $meta_key => $meta_val ) { |
| 244 |
if ( ! empty( $meta_val[0] ) && is_serialized( $meta_val[0] ) ) { |
| 245 |
$meta_val[0] = unserialize( $meta_val[0], array( 'allowed_classes' => false ) ); |
| 246 |
} |
| 247 |
|
| 248 |
if ( get_post_meta( get_the_ID(), $meta_key, true ) === '' ) { |
| 249 |
add_post_meta( get_the_ID(), $meta_key, $meta_val[0] ); |
| 250 |
} else { |
| 251 |
update_post_meta( get_the_ID(), $meta_key, $meta_val[0] ); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
wp_send_json( $response ); |
| 259 |
wp_die(); |
| 260 |
} |
| 261 |
|
| 262 |
/* All below functions are helper functions for this file */ |
| 263 |
|
| 264 |
/** |
| 265 |
* |
| 266 |
* This Function is used for API call |
| 267 |
* |
| 268 |
* @since 1.0.0 |
| 269 |
* |
| 270 |
* @param array $data give array. |
| 271 |
* @param array $name store data. |
| 272 |
*/ |
| 273 |
protected function wkit_api_call( $data, $name, $timeout = 100 ) { |
| 274 |
$u_r_l = $this->wdkit_api; |
| 275 |
|
| 276 |
if ( empty( $u_r_l ) ) { |
| 277 |
return array( |
| 278 |
'massage' => esc_html__( 'API Not Found', 'wdesignkit' ), |
| 279 |
'success' => false, |
| 280 |
); |
| 281 |
} |
| 282 |
|
| 283 |
$args = array( |
| 284 |
'method' => 'POST', |
| 285 |
'body' => $data, |
| 286 |
'timeout' => $timeout, |
| 287 |
); |
| 288 |
$response = wp_remote_post( $u_r_l . $name, $args ); |
| 289 |
|
| 290 |
if ( is_wp_error( $response ) ) { |
| 291 |
$error_message = $response->get_error_message(); |
| 292 |
|
| 293 |
/* Translators: %s is a placeholder for the error message */ |
| 294 |
$error_message = sprintf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) ); |
| 295 |
|
| 296 |
return array( |
| 297 |
'massage' => $error_message, |
| 298 |
'success' => false, |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 303 |
if ( 200 === $status_code ) { |
| 304 |
|
| 305 |
return array( |
| 306 |
'data' => json_decode( wp_remote_retrieve_body( $response ) ), |
| 307 |
'massage' => esc_html__( 'Success', 'wdesignkit' ), |
| 308 |
'status' => $status_code, |
| 309 |
'success' => true, |
| 310 |
); |
| 311 |
} |
| 312 |
|
| 313 |
$error_message = sprintf( 'Server error: %d', esc_html( $status_code ) ); |
| 314 |
|
| 315 |
if ( isset( $error_data->message ) ) { |
| 316 |
$error_message .= ' (' . $error_data->message . ')'; |
| 317 |
} |
| 318 |
|
| 319 |
return array( |
| 320 |
'massage' => $error_message, |
| 321 |
'status' => $status_code, |
| 322 |
'success' => false, |
| 323 |
); |
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Set the response data. |
| 328 |
* |
| 329 |
* @since 6.0.0 |
| 330 |
* |
| 331 |
* @param bool $success Indicates whether the operation was successful. Default is false. |
| 332 |
* @param string $message The main message to include in the response. Default is an empty string. |
| 333 |
* @param string $description A more detailed description of the message or error. Default is an empty string. |
| 334 |
*/ |
| 335 |
public function tpae_set_response( $success = false, $message = '', $description = '' ) { |
| 336 |
|
| 337 |
$response = array( |
| 338 |
'success' => $success, |
| 339 |
'message' => esc_html( $message ), |
| 340 |
'description' => esc_html( $description ), |
| 341 |
); |
| 342 |
|
| 343 |
return $response; |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
Wdkit_Preset_Ajax::get_instance(); |
| 348 |
} |