| 1 |
<?php |
| 2 |
/** |
| 3 |
* The file that defines the core plugin class |
| 4 |
* |
| 5 |
* @link https://posimyth.com/ |
| 6 |
* @since 2.0.12 |
| 7 |
* |
| 8 |
* @package Wdesignkit |
| 9 |
* @subpackage Wdesignkit/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Wdkit_Theme_Builder class |
| 18 |
* |
| 19 |
* @since 2.0.12 |
| 20 |
*/ |
| 21 |
if ( ! class_exists( 'Wdkit_Theme_Builder' ) ) { |
| 22 |
|
| 23 |
/** |
| 24 |
* Wdkit_Theme_Builder class |
| 25 |
* |
| 26 |
* @since 2.0.12 |
| 27 |
*/ |
| 28 |
class Wdkit_Theme_Builder { |
| 29 |
/** |
| 30 |
* Member Variable |
| 31 |
* |
| 32 |
* @var instance |
| 33 |
*/ |
| 34 |
private static $instance; |
| 35 |
|
| 36 |
/** |
| 37 |
* Member Variable |
| 38 |
* |
| 39 |
* @var staring $wdkit_api |
| 40 |
*/ |
| 41 |
public $wdkit_api = WDKIT_SERVER_API_URL . 'api/v2/wp/'; |
| 42 |
|
| 43 |
/** |
| 44 |
* Get instance |
| 45 |
* |
| 46 |
* @return object |
| 47 |
*/ |
| 48 |
public static function get_instance() { |
| 49 |
if ( null === self::$instance ) { |
| 50 |
self::$instance = new self(); |
| 51 |
} |
| 52 |
return self::$instance; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Constructor |
| 57 |
*/ |
| 58 |
public function __construct() { |
| 59 |
add_action( 'wp_ajax_wdkit_theme_builder', array( $this, 'wdkit_theme_builder' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Main API Call for Theme Builder |
| 64 |
*/ |
| 65 |
public function wdkit_theme_builder() { |
| 66 |
check_ajax_referer( 'wdkit_nonce', 'kit_nonce' ); |
| 67 |
|
| 68 |
if ( ! is_user_logged_in() || ! current_user_can( 'manage_options' ) ) { |
| 69 |
wp_send_json_error( array( 'content' => __( 'Insufficient permissions.', 'wdesignkit' ) ) ); |
| 70 |
} |
| 71 |
$type = isset( $_POST['type'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['type'] ) ) ) : false; |
| 72 |
|
| 73 |
$data = array( |
| 74 |
'success' => false, |
| 75 |
'message' => __( 'Something went wrong.', 'wdesignkit' ), |
| 76 |
); |
| 77 |
|
| 78 |
if ( $type ) { |
| 79 |
switch ( $type ) { |
| 80 |
case 'browse_template': |
| 81 |
$data = $this->wdkit_theme_builder_browse(); |
| 82 |
break; |
| 83 |
default: |
| 84 |
$data = array( |
| 85 |
'success' => false, |
| 86 |
'message' => __( 'Invalid request type.', 'wdesignkit' ), |
| 87 |
); |
| 88 |
break; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
wp_send_json( $data ); |
| 93 |
wp_die(); |
| 94 |
} |
| 95 |
|
| 96 |
/* Theme Builder Get Data */ |
| 97 |
public function wdkit_theme_builder_browse() { |
| 98 |
|
| 99 |
$par_page = isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 10; |
| 100 |
$current_page = isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1; |
| 101 |
$free_pro = isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : ''; |
| 102 |
$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; |
| 103 |
$category = isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : ''; |
| 104 |
$builder = isset( $_POST['builder'] ) ? sanitize_text_field( wp_unslash( $_POST['builder'] ) ) : ''; |
| 105 |
|
| 106 |
$data = array( |
| 107 |
'page' => $current_page, |
| 108 |
'perpage' => $par_page, |
| 109 |
); |
| 110 |
|
| 111 |
if ( ! empty( $builder ) ) { |
| 112 |
$data['builder'] = $builder; |
| 113 |
} |
| 114 |
|
| 115 |
if ( ! empty( $search ) ) { |
| 116 |
$data['search'] = $search; |
| 117 |
} |
| 118 |
|
| 119 |
if ( ! empty( $category ) ) { |
| 120 |
$data['category'] = $category; |
| 121 |
} |
| 122 |
|
| 123 |
if ( ! empty( $free_pro ) ) { |
| 124 |
$data['free_pro'] = $free_pro; |
| 125 |
} |
| 126 |
|
| 127 |
$response = $this->wkit_api_call( $data, 'theme/builder/browse' ); |
| 128 |
|
| 129 |
if ( $response['success'] ) { |
| 130 |
// Add manage_licence data to check for pro plugin activation |
| 131 |
$manage_licence = array(); |
| 132 |
$theplus_active_check = is_plugin_active( 'the-plus-addons-for-elementor-page-builder/theplus_elementor_addon.php' ); |
| 133 |
$manage_licence['theplus_elementor_addon'] = !empty ( defined( 'THEPLUS_VERSION' ) ) ? true : false; |
| 134 |
$manage_licence['the-plus-addons-for-elementor-page-builder'] = !empty ( $theplus_active_check ) ? true : false; |
| 135 |
$manage_licence['tpag'] = !empty ( defined( 'TPGBP_VERSION' ) ) ? true : false; |
| 136 |
$manage_licence['elementor-pro'] = !empty ( defined( 'ELEMENTOR_PRO_VERSION' ) ) ? true : false; |
| 137 |
|
| 138 |
// Add manage_licence to response (will be nested in data.data.manage_licence in final response) |
| 139 |
$response['manage_licence'] = $manage_licence; |
| 140 |
|
| 141 |
$data = array( |
| 142 |
'success' => true, |
| 143 |
'data' => $response, |
| 144 |
); |
| 145 |
} else { |
| 146 |
$data = array( |
| 147 |
'success' => false, |
| 148 |
'message' => __( 'Failed to fetch theme builder data.', 'wdesignkit' ), |
| 149 |
); |
| 150 |
} |
| 151 |
|
| 152 |
return $data; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* |
| 157 |
* This Function is used for API call |
| 158 |
* |
| 159 |
* @since 1.2.4 |
| 160 |
* |
| 161 |
* @param array $data give array. |
| 162 |
* @param array $name store data. |
| 163 |
*/ |
| 164 |
public function wkit_api_call( $data, $name ) { |
| 165 |
$u_r_l = $this->wdkit_api; |
| 166 |
|
| 167 |
if ( empty( $u_r_l ) ) { |
| 168 |
return array( |
| 169 |
'massage' => esc_html__( 'API Not Found', 'wdesignkit' ), |
| 170 |
'success' => false, |
| 171 |
); |
| 172 |
} |
| 173 |
|
| 174 |
$args = array( |
| 175 |
'method' => 'POST', |
| 176 |
'body' => $data, |
| 177 |
'timeout' => 100, |
| 178 |
); |
| 179 |
|
| 180 |
$response = wp_remote_post( $u_r_l . $name, $args ); |
| 181 |
|
| 182 |
if ( is_wp_error( $response ) ) { |
| 183 |
$error_message = $response->get_error_message(); |
| 184 |
|
| 185 |
/* Translators: %s is a placeholder for the error message */ |
| 186 |
$error_message = sprintf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) ); |
| 187 |
|
| 188 |
return array( |
| 189 |
'massage' => $error_message, |
| 190 |
'success' => false, |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 195 |
if ( 200 === $status_code ) { |
| 196 |
return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 197 |
} |
| 198 |
|
| 199 |
$error_message = sprintf( 'Server error: %d', esc_html( $status_code ) ); |
| 200 |
|
| 201 |
if ( isset( $error_data->message ) ) { |
| 202 |
$error_message .= ' (' . $error_data->message . ')'; |
| 203 |
} |
| 204 |
|
| 205 |
return array( |
| 206 |
'massage' => $error_message, |
| 207 |
'status' => $status_code, |
| 208 |
'success' => false, |
| 209 |
); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
Wdkit_Theme_Builder::get_instance(); |
| 214 |
} |