| 1 |
<?php |
| 2 |
/** |
| 3 |
* The file that defines the core plugin class |
| 4 |
* |
| 5 |
* @link https://posimyth.com/ |
| 6 |
* @since 2.0.0 |
| 7 |
* |
| 8 |
* @package Wdesignkit |
| 9 |
* @subpackage Wdesignkit/includes |
| 10 |
*/ |
| 11 |
|
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
/** |
| 17 |
* Wdkit_Code_Snippet class |
| 18 |
* |
| 19 |
* @since 2.0.0 |
| 20 |
*/ |
| 21 |
if ( ! class_exists( 'Wdkit_Code_Snippet' ) ) { |
| 22 |
|
| 23 |
/** |
| 24 |
* Wdkit_Code_Snippet class |
| 25 |
* |
| 26 |
* @since 2.0.0 |
| 27 |
*/ |
| 28 |
class Wdkit_Code_Snippet { |
| 29 |
|
| 30 |
/** |
| 31 |
* Member Variable |
| 32 |
* |
| 33 |
* @var instance |
| 34 |
*/ |
| 35 |
private static $instance; |
| 36 |
|
| 37 |
/** |
| 38 |
* Member Variable |
| 39 |
* |
| 40 |
* @var staring $wdkit_api |
| 41 |
*/ |
| 42 |
public $wdkit_api = WDKIT_SERVER_API_URL . 'api/wp/'; |
| 43 |
|
| 44 |
/** |
| 45 |
* Get instance |
| 46 |
* |
| 47 |
* @return object |
| 48 |
*/ |
| 49 |
public static function get_instance() { |
| 50 |
if ( null === self::$instance ) { |
| 51 |
self::$instance = new self(); |
| 52 |
} |
| 53 |
return self::$instance; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Constructor |
| 58 |
*/ |
| 59 |
public function __construct() { |
| 60 |
add_action( 'wp_ajax_wdkit_code_snippet', array( $this, 'wdkit_code_snippet' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Main API Call for Code Snippet |
| 65 |
*/ |
| 66 |
public function wdkit_code_snippet() { |
| 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 |
$type = isset( $_POST['type'] ) ? strtolower( sanitize_text_field( wp_unslash( $_POST['type'] ) ) ) : false; |
| 73 |
|
| 74 |
if ( ! $type ) { |
| 75 |
$data = array( |
| 76 |
'success' => false, |
| 77 |
'message' => __( 'Something went wrong.', 'wdesignkit' ), |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
switch ( $type ) { |
| 82 |
case 'code_snippet_filter': |
| 83 |
$data = $this->wdkit_code_snippet_filter(); |
| 84 |
break; |
| 85 |
|
| 86 |
case 'filter_opt': |
| 87 |
$data = $this->wdkit_code_snippet_filter_opt(); |
| 88 |
break; |
| 89 |
|
| 90 |
case 'existing_snippet': |
| 91 |
$data = $this->wdkit_code_snippet_existing_snippet(); |
| 92 |
break; |
| 93 |
|
| 94 |
case 'favorite_unfavorite': |
| 95 |
$data = $this->wdkit_snippet_favorite_unfavorite(); |
| 96 |
break; |
| 97 |
|
| 98 |
case 'delete_code_snippet': |
| 99 |
$data = $this->wdkit_delete_code_snippet(); |
| 100 |
break; |
| 101 |
|
| 102 |
case 'save_code_snippet': |
| 103 |
$data = $this->wkit_save_code_snippet(); |
| 104 |
break; |
| 105 |
|
| 106 |
case 'get_user_snippets': |
| 107 |
$data = $this->wkit_get_user_snippets(); |
| 108 |
break; |
| 109 |
|
| 110 |
case 'get_user_snippets_favorite': |
| 111 |
$data = $this->wkit_get_user_snippets_favorite(); |
| 112 |
break; |
| 113 |
|
| 114 |
case 'download_code_snippet': |
| 115 |
$data = $this->wkit_download_code_snippet(); |
| 116 |
break; |
| 117 |
|
| 118 |
case 'get_snippet_info': |
| 119 |
$data = $this->wdkit_get_snippet_info(); |
| 120 |
break; |
| 121 |
|
| 122 |
case 'get_snippet_kit': |
| 123 |
$data = $this->wdkit_get_snippet_kit(); |
| 124 |
break; |
| 125 |
|
| 126 |
case 'get_user_roles': |
| 127 |
$data = $this->wdkit_get_user_roles(); |
| 128 |
break; |
| 129 |
|
| 130 |
default: |
| 131 |
break; |
| 132 |
} |
| 133 |
|
| 134 |
wp_send_json( $data ); |
| 135 |
wp_die(); |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Code snippet filter |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function wdkit_code_snippet_filter() { |
| 144 |
|
| 145 |
$current_page = isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1; |
| 146 |
$par_page = isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 1; |
| 147 |
$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; |
| 148 |
$free_pro = isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : ''; |
| 149 |
$category = isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : ''; |
| 150 |
$status = isset( $_POST['status'] ) ? sanitize_text_field( wp_unslash( $_POST['status'] ) ) : ''; |
| 151 |
$plugins = isset( $_POST['plugins'] ) ? sanitize_text_field( wp_unslash( $_POST['plugins'] ) ) : ''; |
| 152 |
$plugin_id_exclude = isset( $_POST['plugin_id_exclude'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin_id_exclude'] ) ) : ''; |
| 153 |
$tags = isset( $_POST['tags'] ) ? sanitize_text_field( wp_unslash( $_POST['tags'] ) ) : ''; |
| 154 |
$snippet_type = isset( $_POST['snippet_type'] ) ? sanitize_text_field( wp_unslash( $_POST['snippet_type'] ) ) : ''; |
| 155 |
|
| 156 |
$data = array( |
| 157 |
'CurrentPage' => $current_page, |
| 158 |
'ParPage' => $par_page, |
| 159 |
); |
| 160 |
|
| 161 |
if ( ! empty( $search ) ) { |
| 162 |
$data['search'] = $search; |
| 163 |
} |
| 164 |
|
| 165 |
if ( ! empty( $category ) ) { |
| 166 |
$data['terms_id'] = $category; |
| 167 |
} |
| 168 |
|
| 169 |
if ( ! empty( $free_pro ) ) { |
| 170 |
$data['free_pro'] = $free_pro; |
| 171 |
} |
| 172 |
|
| 173 |
if ( ! empty( $status ) ) { |
| 174 |
$data['status'] = $status; |
| 175 |
} |
| 176 |
|
| 177 |
if ( ! empty( $plugins ) ) { |
| 178 |
$data['plugin_id'] = $plugins; |
| 179 |
} |
| 180 |
|
| 181 |
if ( ! empty( $plugin_id_exclude ) ) { |
| 182 |
$data['plugin_id_exclude'] = $plugin_id_exclude; |
| 183 |
} |
| 184 |
|
| 185 |
if ( ! empty( $tags ) ) { |
| 186 |
$data['tags'] = $tags; |
| 187 |
} |
| 188 |
|
| 189 |
if ( ! empty( $snippet_type ) && 'all' !== $snippet_type ) { |
| 190 |
$data['type'] = $snippet_type; |
| 191 |
} |
| 192 |
|
| 193 |
// snippet/browse/list. |
| 194 |
$response = $this->wkit_api_call( $data, 'snippet/browse/list' ); |
| 195 |
|
| 196 |
if ( $response['success'] ) { |
| 197 |
$data = array( |
| 198 |
'success' => true, |
| 199 |
'data' => $response, |
| 200 |
); |
| 201 |
} else { |
| 202 |
$data = array( |
| 203 |
'success' => false, |
| 204 |
'message' => __( 'Failed to fetch snippet.', 'wdesignkit' ), |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
return $data; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Snippet Dynamic Options |
| 213 |
*/ |
| 214 |
public function wdkit_code_snippet_filter_opt() { |
| 215 |
|
| 216 |
$data = array( |
| 217 |
'type' => isset( $_POST['opt_type'] ) ? sanitize_text_field( wp_unslash( $_POST['opt_type'] ) ) : 'terms,plugin,tag', |
| 218 |
'search_tag' => isset( $_POST['search_tag'] ) ? sanitize_text_field( wp_unslash( $_POST['search_tag'] ) ) : '', |
| 219 |
'url_tag_id' => isset( $_POST['tags'] ) ? sanitize_text_field( wp_unslash( $_POST['tags'] ) ) : '', |
| 220 |
); |
| 221 |
|
| 222 |
// snippet/browse/filter. |
| 223 |
$response = $this->wkit_api_call( $data, 'snippet/browse/filter' ); |
| 224 |
|
| 225 |
if ( $response['success'] ) { |
| 226 |
$data = array( |
| 227 |
'success' => true, |
| 228 |
'data' => $response, |
| 229 |
); |
| 230 |
} else { |
| 231 |
$data = array( |
| 232 |
'success' => false, |
| 233 |
'message' => __( 'Failed to fetch filter options.', 'wdesignkit' ), |
| 234 |
); |
| 235 |
} |
| 236 |
|
| 237 |
return $data; |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Code snippet existing snippet |
| 242 |
* |
| 243 |
* @return array |
| 244 |
*/ |
| 245 |
public function wdkit_code_snippet_existing_snippet() { |
| 246 |
$data = array( |
| 247 |
'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '', |
| 248 |
'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '', |
| 249 |
'CurrentPage' => isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1, |
| 250 |
'ParPage' => isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 1, |
| 251 |
); |
| 252 |
$response = $this->wkit_api_call( $data, 'snippet/save/get_exist' ); |
| 253 |
|
| 254 |
if ( $response['success'] ) { |
| 255 |
$data = array( |
| 256 |
'success' => true, |
| 257 |
'data' => $response, |
| 258 |
); |
| 259 |
} else { |
| 260 |
$data = array( |
| 261 |
'success' => false, |
| 262 |
'message' => __( 'Failed to fetch existing snippet.', 'wdesignkit' ), |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
return $data; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Get post title by ID |
| 271 |
* |
| 272 |
* @return array |
| 273 |
*/ |
| 274 |
public function wdkit_get_snippet_info() { |
| 275 |
$post_id = isset( $_POST['post_id'] ) ? intval( sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) ) : 0; |
| 276 |
|
| 277 |
if ( $post_id ) { |
| 278 |
$post = get_post( $post_id ); |
| 279 |
|
| 280 |
if ( $post ) { |
| 281 |
$data = array( |
| 282 |
'success' => true, |
| 283 |
'data' => array( |
| 284 |
'title' => $post->post_title, |
| 285 |
'description' => get_post_meta( $post_id, 'nxt-code-note', true ), |
| 286 |
'id' => $post->ID, |
| 287 |
), |
| 288 |
); |
| 289 |
} else { |
| 290 |
$data = array( |
| 291 |
'success' => false, |
| 292 |
'message' => __( 'Post not found.', 'wdesignkit' ), |
| 293 |
); |
| 294 |
} |
| 295 |
} else { |
| 296 |
$data = array( |
| 297 |
'success' => false, |
| 298 |
'message' => __( 'Invalid post ID.', 'wdesignkit' ), |
| 299 |
); |
| 300 |
} |
| 301 |
|
| 302 |
return $data; |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Snippet Favorite/Unfavorite. |
| 307 |
* |
| 308 |
* @return array |
| 309 |
*/ |
| 310 |
public function wdkit_snippet_favorite_unfavorite() { |
| 311 |
$data = array( |
| 312 |
'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '', |
| 313 |
'snippet_id' => isset( $_POST['snippet_id'] ) ? sanitize_text_field( wp_unslash( $_POST['snippet_id'] ) ) : '', |
| 314 |
'type' => isset( $_POST['favorite_type'] ) ? sanitize_text_field( wp_unslash( $_POST['favorite_type'] ) ) : '', |
| 315 |
); |
| 316 |
|
| 317 |
$response = $this->wkit_api_call( $data, 'snippet/favorite' ); |
| 318 |
|
| 319 |
if ( $response['success'] ) { |
| 320 |
$data = $response; |
| 321 |
} else { |
| 322 |
$data = array( |
| 323 |
'success' => false, |
| 324 |
'message' => __( 'Failed to favorite/unfavorite snippet.', 'wdesignkit' ), |
| 325 |
); |
| 326 |
} |
| 327 |
|
| 328 |
return $data; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Snippet Delete |
| 333 |
*/ |
| 334 |
public function wdkit_delete_code_snippet() { |
| 335 |
$data = array( |
| 336 |
'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '', |
| 337 |
'id' => isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '', |
| 338 |
); |
| 339 |
$response = $this->wkit_api_call( $data, 'snippet/delete' ); |
| 340 |
|
| 341 |
if ( $response['success'] ) { |
| 342 |
$data = $response; |
| 343 |
} else { |
| 344 |
$data = array( |
| 345 |
'success' => false, |
| 346 |
'message' => __( 'Failed to delete snippet.', 'wdesignkit' ), |
| 347 |
); |
| 348 |
} |
| 349 |
|
| 350 |
return $data; |
| 351 |
} |
| 352 |
|
| 353 |
/** |
| 354 |
* Snippet Save |
| 355 |
*/ |
| 356 |
public function wkit_save_code_snippet() { |
| 357 |
$post_id = isset( $_POST['post_id'] ) ? intval( sanitize_text_field( wp_unslash( $_POST['post_id'] ) ) ) : 0; |
| 358 |
$description = isset( $_POST['description'] ) ? sanitize_text_field( wp_unslash( $_POST['description'] ) ) : ''; |
| 359 |
|
| 360 |
if ( empty( $post_id ) ) { |
| 361 |
$response = array( |
| 362 |
'success' => false, |
| 363 |
'message' => esc_html__( 'Data Not Found', 'wdesignkit' ), |
| 364 |
'description' => esc_html__( 'Post ID Not Found', 'wdesignkit' ), |
| 365 |
); |
| 366 |
|
| 367 |
return $response; |
| 368 |
} |
| 369 |
|
| 370 |
$type = get_post_meta( $post_id, 'nxt-code-type', true ); |
| 371 |
$get_data = array( |
| 372 |
'id' => $post_id, |
| 373 |
'name' => get_the_title( $post_id ), |
| 374 |
'description' => get_post_meta( $post_id, 'nxt-code-note', true ), |
| 375 |
'type' => $type, |
| 376 |
'post_type' => get_post_type( $post_id ), |
| 377 |
'tags' => get_post_meta( $post_id, 'nxt-code-tags', true ), |
| 378 |
'codeExecute' => get_post_meta( $post_id, 'nxt-code-execute', true ), |
| 379 |
'status' => get_post_meta( $post_id, 'nxt-code-status', true ), |
| 380 |
'langCode' => get_post_meta( $post_id, 'nxt-' . $type . '-code', true ), |
| 381 |
'htmlHooks' => get_post_meta( $post_id, 'nxt-code-html-hooks', true ), |
| 382 |
'hooksPriority' => get_post_meta( $post_id, 'nxt-code-hooks-priority', true ), |
| 383 |
'include_data' => get_post_meta( $post_id, 'nxt-add-display-rule', true ), |
| 384 |
'exclude_data' => get_post_meta( $post_id, 'nxt-exclude-display-rule', true ), |
| 385 |
'in_sub_data' => get_post_meta( $post_id, 'nxt-in-sub-rule', true ), |
| 386 |
'ex_sub_data' => get_post_meta( $post_id, 'nxt-ex-sub-rule', true ), |
| 387 |
// Word-based insertion settings. |
| 388 |
'word_count' => get_post_meta( $post_id, 'nxt-insert-word-count', true ), |
| 389 |
'word_interval' => get_post_meta( $post_id, 'nxt-insert-word-interval', true ), |
| 390 |
'post_number' => get_post_meta( $post_id, 'nxt-post-number', true ), |
| 391 |
// CSS Selector settings. |
| 392 |
'css_selector' => get_post_meta( $post_id, 'nxt-css-selector', true ), |
| 393 |
'element_index' => get_post_meta( $post_id, 'nxt-element-index', true ), |
| 394 |
// Missing fields that should be exported. |
| 395 |
'insertion' => get_post_meta( $post_id, 'nxt-code-insertion', true ), |
| 396 |
'location' => get_post_meta( $post_id, 'nxt-code-location', true ), |
| 397 |
'customname' => get_post_meta( $post_id, 'nxt-code-customname', true ), |
| 398 |
'compresscode' => get_post_meta( $post_id, 'nxt-code-compresscode', true ), |
| 399 |
'startDate' => get_post_meta( $post_id, 'nxt-code-startdate', true ), |
| 400 |
'endDate' => get_post_meta( $post_id, 'nxt-code-enddate', true ), |
| 401 |
'shortcodeattr' => get_post_meta( $post_id, 'nxt-code-shortcodeattr', true ), |
| 402 |
'smart_conditional_logic' => get_post_meta( $post_id, 'nxt-smart-conditional-logic', true ), |
| 403 |
'php_hidden_execute' => get_post_meta( $post_id, 'nxt-code-php-hidden-execute', true ), |
| 404 |
); |
| 405 |
$array_data = array( |
| 406 |
'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '', |
| 407 |
'type' => isset( $_POST['stype'] ) ? sanitize_text_field( wp_unslash( $_POST['stype'] ) ) : '', |
| 408 |
'title' => isset( $_POST['title'] ) ? sanitize_text_field( $_POST['title'] ) : '', |
| 409 |
'plugin_id' => isset( $_POST['plugin'] ) ? sanitize_text_field( wp_unslash( $_POST['plugin'] ) ) : '', |
| 410 |
'terms_id' => isset( $_POST['category'] ) ? sanitize_text_field( wp_unslash( $_POST['category'] ) ) : '', |
| 411 |
'data' => wp_json_encode( |
| 412 |
array( |
| 413 |
'generator' => 'WDesignKit Export v' . WDKIT_VERSION, |
| 414 |
'date_created' => current_time( 'Y-m-d H:i' ), |
| 415 |
'snippets' => array( $get_data ), |
| 416 |
) |
| 417 |
), |
| 418 |
); |
| 419 |
|
| 420 |
$stype = isset( $_POST['stype'] ) ? sanitize_text_field( wp_unslash( $_POST['stype'] ) ) : 'new'; |
| 421 |
$save_type = ( ! empty( $stype ) && 'existing' === $stype ) ? 'exist' : 'new'; |
| 422 |
if ( isset( $_POST['snippet_id'] ) && ! empty( $_POST['snippet_id'] ) && 'exist' === $save_type ) { |
| 423 |
$array_data['id'] = sanitize_text_field( wp_unslash( $_POST['snippet_id'] ) ); |
| 424 |
} |
| 425 |
|
| 426 |
if ( 'new' === $save_type ) { |
| 427 |
$array_data['w_unique'] = isset( $_POST['w_unique'] ) ? sanitize_text_field( wp_unslash( $_POST['w_unique'] ) ) : ''; |
| 428 |
$array_data['code_type'] = $type; |
| 429 |
$array_data['post_content'] = $description; |
| 430 |
} |
| 431 |
|
| 432 |
$response = $this->wkit_api_call( $array_data, 'snippet/save/' . $save_type ); |
| 433 |
|
| 434 |
if ( $response['success'] ) { |
| 435 |
$data = array( |
| 436 |
'success' => true, |
| 437 |
'data' => $response, |
| 438 |
); |
| 439 |
} else { |
| 440 |
$data = array( |
| 441 |
'success' => false, |
| 442 |
'message' => __( 'Failed to save snippet.', 'wdesignkit' ), |
| 443 |
); |
| 444 |
} |
| 445 |
|
| 446 |
return $data; |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* Get Snippet Kit |
| 451 |
*/ |
| 452 |
public function wdkit_get_snippet_kit() { |
| 453 |
$data = array(); |
| 454 |
|
| 455 |
$kit_id = isset( $_POST['kit_id'] ) ? sanitize_text_field( wp_unslash( $_POST['kit_id'] ) ) : ''; |
| 456 |
if ( empty( $kit_id ) ) { |
| 457 |
$data = array( |
| 458 |
'success' => false, |
| 459 |
'message' => __( 'Kit ID is required.', 'wdesignkit' ), |
| 460 |
); |
| 461 |
return $data; |
| 462 |
} |
| 463 |
|
| 464 |
$response = $this->wkit_api_call( $data, 'snippet/kit/' . $kit_id ); |
| 465 |
|
| 466 |
if ( $response['success'] ) { |
| 467 |
$data = array( |
| 468 |
'success' => true, |
| 469 |
'data' => $response, |
| 470 |
); |
| 471 |
} else { |
| 472 |
$data = array( |
| 473 |
'success' => false, |
| 474 |
'message' => __( 'Failed to fetch snippet kit.', 'wdesignkit' ), |
| 475 |
); |
| 476 |
} |
| 477 |
|
| 478 |
return $data; |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Get Users Snippets |
| 483 |
*/ |
| 484 |
public function wkit_get_user_snippets() { |
| 485 |
$array_data = array( |
| 486 |
'CurrentPage' => isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1, |
| 487 |
'ParPage' => isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : '', |
| 488 |
'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '', |
| 489 |
'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '', |
| 490 |
); |
| 491 |
|
| 492 |
$response = $this->wkit_api_call( $array_data, 'snippet/mysnippets' ); |
| 493 |
|
| 494 |
if ( $response['success'] ) { |
| 495 |
$data = array( |
| 496 |
'success' => true, |
| 497 |
'data' => $response, |
| 498 |
); |
| 499 |
} else { |
| 500 |
$data = array( |
| 501 |
'success' => false, |
| 502 |
'message' => __( 'Failed to fetch snippet.', 'wdesignkit' ), |
| 503 |
); |
| 504 |
} |
| 505 |
|
| 506 |
return $data; |
| 507 |
} |
| 508 |
|
| 509 |
/** |
| 510 |
* Get User Snippets Favorite |
| 511 |
*/ |
| 512 |
public function wkit_get_user_snippets_favorite() { |
| 513 |
$array_data = array( |
| 514 |
'CurrentPage' => isset( $_POST['CurrentPage'] ) ? sanitize_text_field( wp_unslash( $_POST['CurrentPage'] ) ) : 1, |
| 515 |
'ParPage' => isset( $_POST['ParPage'] ) ? sanitize_text_field( wp_unslash( $_POST['ParPage'] ) ) : 24, |
| 516 |
'token' => isset( $_POST['token'] ) ? sanitize_text_field( wp_unslash( $_POST['token'] ) ) : '', |
| 517 |
'search' => isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '', |
| 518 |
); |
| 519 |
$response = $this->wkit_api_call( $array_data, 'snippet/favorite/get' ); |
| 520 |
if ( $response['success'] ) { |
| 521 |
$data = array( |
| 522 |
'success' => true, |
| 523 |
'data' => $response, |
| 524 |
); |
| 525 |
} else { |
| 526 |
$data = array( |
| 527 |
'success' => false, |
| 528 |
'message' => __( 'Failed to fetch snippet favorite.', 'wdesignkit' ), |
| 529 |
); |
| 530 |
} |
| 531 |
return $data; |
| 532 |
} |
| 533 |
|
| 534 |
/** |
| 535 |
* Snippet Download |
| 536 |
*/ |
| 537 |
public function wkit_download_code_snippet() { |
| 538 |
$array_data = array( |
| 539 |
'id' => isset( $_POST['id'] ) ? sanitize_text_field( wp_unslash( $_POST['id'] ) ) : '', |
| 540 |
); |
| 541 |
|
| 542 |
$w_unique = isset( $_POST['w_unique'] ) ? sanitize_text_field( wp_unslash( $_POST['w_unique'] ) ) : ''; |
| 543 |
$free_pro = isset( $_POST['free_pro'] ) ? sanitize_text_field( wp_unslash( $_POST['free_pro'] ) ) : ''; |
| 544 |
|
| 545 |
$response = array(); |
| 546 |
if ( isset( $_POST['token'] ) && ! empty( $_POST['token'] ) ) { |
| 547 |
$array_data['token'] = sanitize_text_field( wp_unslash( $_POST['token'] ) ); |
| 548 |
// Check if Nexter Pro extension is activated and has valid license. |
| 549 |
if ( defined( 'NXT_PRO_EXT' ) && class_exists( 'Nexter_Pro_Ext_Activate' ) && 'pro' === $free_pro ) { |
| 550 |
$array_data['pro_active'] = 'yes'; |
| 551 |
} |
| 552 |
|
| 553 |
$response = $this->wkit_api_call( $array_data, 'snippet/import' ); |
| 554 |
} else { |
| 555 |
$response = $this->wkit_api_call( $array_data, 'snippet/import/free' ); |
| 556 |
} |
| 557 |
|
| 558 |
// Check if API call was successful. |
| 559 |
if ( ! $response || ! isset( $response['success'] ) || ! $response['success'] ) { |
| 560 |
$data = array( |
| 561 |
'success' => false, |
| 562 |
'data' => array( |
| 563 |
'message' => __( 'Failed to download snippet from server.', 'wdesignkit' ), |
| 564 |
), |
| 565 |
); |
| 566 |
} |
| 567 |
|
| 568 |
if ( $response && isset( $response['data'] ) && 'error' === $response['data'] ) { |
| 569 |
$data = array( |
| 570 |
'success' => false, |
| 571 |
'data' => array( |
| 572 |
'message' => $response['message'] ?? __( 'Failed to download snippet from server.', 'wdesignkit' ), |
| 573 |
), |
| 574 |
); |
| 575 |
} |
| 576 |
|
| 577 |
if ( $response && $response['content'] ) { |
| 578 |
$json = json_decode( $response['content'], true ); |
| 579 |
|
| 580 |
if ( ! $json || empty( $json['snippets'] ) || ! is_array( $json['snippets'] ) ) { |
| 581 |
$data = array( |
| 582 |
'success' => false, |
| 583 |
'data' => array( |
| 584 |
'message' => __( 'Invalid snippet file.', 'wdesignkit' ), |
| 585 |
), |
| 586 |
); |
| 587 |
} |
| 588 |
|
| 589 |
$snippet = $json['snippets'][0]; |
| 590 |
|
| 591 |
if ( empty( $snippet['post_type'] ) || 'nxt-code-snippet' !== $snippet['post_type'] ) { |
| 592 |
$data = array( |
| 593 |
'success' => false, |
| 594 |
'data' => array( |
| 595 |
'message' => __( 'Invalid snippet type.', 'wdesignkit' ), |
| 596 |
), |
| 597 |
); |
| 598 |
} |
| 599 |
$post_id = wp_insert_post( |
| 600 |
array( |
| 601 |
'post_title' => sanitize_text_field( html_entity_decode( wp_unslash( $snippet['name'] ) ) ), |
| 602 |
'post_type' => 'nxt-code-snippet', |
| 603 |
'post_status' => 'publish', |
| 604 |
) |
| 605 |
); |
| 606 |
|
| 607 |
if ( is_wp_error( $post_id ) ) { |
| 608 |
$data = array( |
| 609 |
'success' => false, |
| 610 |
'data' => array( |
| 611 |
'message' => __( 'Failed to insert snippet.', 'wdesignkit' ), |
| 612 |
), |
| 613 |
); |
| 614 |
} |
| 615 |
|
| 616 |
$tags = ( isset( $snippet['tags'] ) && ! empty( $snippet['tags'] ) ) |
| 617 |
? array_map( 'sanitize_text_field', is_array( $snippet['tags'] ) ? $snippet['tags'] : explode( ',', $snippet['tags'] ) ) : array(); |
| 618 |
|
| 619 |
// Save meta fields. |
| 620 |
update_post_meta( $post_id, 'nxt-code-type', sanitize_text_field( wp_unslash( $snippet['type'] ?? '' ) ) ); |
| 621 |
update_post_meta( $post_id, 'nxt-code-note', sanitize_text_field( wp_unslash( $snippet['description'] ?? '' ) ) ); |
| 622 |
update_post_meta( $post_id, 'nxt-code-tags', $tags ); |
| 623 |
update_post_meta( $post_id, 'nxt-code-execute', sanitize_text_field( wp_unslash( $snippet['codeExecute'] ?? '' ) ) ); |
| 624 |
update_post_meta( $post_id, 'nxt-code-status', intval( $snippet['status'] ?? 0 ) ); |
| 625 |
|
| 626 |
update_post_meta( $post_id, 'nxt-' . $snippet['type'] . '-code', wp_unslash( $snippet['langCode'] ?? '' ) ); |
| 627 |
update_post_meta( $post_id, 'nxt-code-html-hooks', wp_unslash( $snippet['htmlHooks'] ?? '' ) ); |
| 628 |
update_post_meta( $post_id, 'nxt-code-hooks-priority', sanitize_text_field( wp_unslash( $snippet['hooksPriority'] ?? '' ) ) ); |
| 629 |
update_post_meta( $post_id, 'nxt-add-display-rule', sanitize_text_field( wp_unslash( $snippet['include_data'] ?? '' ) ) ); |
| 630 |
update_post_meta( $post_id, 'nxt-exclude-display-rule', sanitize_text_field( wp_unslash( $snippet['exclude_data'] ?? '' ) ) ); |
| 631 |
update_post_meta( $post_id, 'nxt-in-sub-rule', sanitize_text_field( wp_unslash( $snippet['in_sub_data'] ?? '' ) ) ); |
| 632 |
update_post_meta( $post_id, 'nxt-ex-sub-rule', sanitize_text_field( wp_unslash( $snippet['ex_sub_data'] ?? '' ) ) ); |
| 633 |
|
| 634 |
// Word-based insertion settings. |
| 635 |
update_post_meta( $post_id, 'nxt-insert-word-count', sanitize_text_field( wp_unslash( $snippet['word_count'] ?? 100 ) ) ); |
| 636 |
update_post_meta( $post_id, 'nxt-insert-word-interval', sanitize_text_field( wp_unslash( $snippet['word_interval'] ?? 200 ) ) ); |
| 637 |
update_post_meta( $post_id, 'nxt-post-number', sanitize_text_field( wp_unslash( $snippet['post_number'] ?? 1 ) ) ); |
| 638 |
|
| 639 |
// CSS Selector settings. |
| 640 |
update_post_meta( $post_id, 'nxt-css-selector', wp_unslash( $snippet['css_selector'] ?? '' ) ); |
| 641 |
update_post_meta( $post_id, 'nxt-element-index', sanitize_text_field( wp_unslash( $snippet['element_index'] ?? 0 ) ) ); |
| 642 |
|
| 643 |
// Additional save options. |
| 644 |
update_post_meta( $post_id, 'nxt-code-insertion', sanitize_text_field( wp_unslash( $snippet['insertion'] ?? '' ) ) ); |
| 645 |
update_post_meta( $post_id, 'nxt-code-location', sanitize_text_field( wp_unslash( $snippet['location'] ?? '' ) ) ); |
| 646 |
update_post_meta( $post_id, 'nxt-code-customname', sanitize_text_field( wp_unslash( $snippet['customname'] ?? '' ) ) ); |
| 647 |
update_post_meta( $post_id, 'nxt-code-compresscode', sanitize_text_field( wp_unslash( $snippet['compresscode'] ?? '' ) ) ); |
| 648 |
update_post_meta( $post_id, 'nxt-code-startdate', sanitize_text_field( wp_unslash( $snippet['startDate'] ?? '' ) ) ); |
| 649 |
update_post_meta( $post_id, 'nxt-code-enddate', sanitize_text_field( wp_unslash( $snippet['endDate'] ?? '' ) ) ); |
| 650 |
update_post_meta( $post_id, 'nxt-code-shortcodeattr', wp_unslash( $snippet['shortcodeattr'] ?? '' ) ); |
| 651 |
update_post_meta( $post_id, 'nxt-smart-conditional-logic', wp_unslash( $snippet['smart_conditional_logic'] ?? '' ) ); |
| 652 |
update_post_meta( $post_id, 'nxt-code-php-hidden-execute', sanitize_text_field( wp_unslash( $snippet['php_hidden_execute'] ?? '' ) ) ); |
| 653 |
update_post_meta( $post_id, 'nxt-wdkit-unique-sid', $w_unique ); |
| 654 |
|
| 655 |
$data = array( |
| 656 |
'success' => true, |
| 657 |
'data' => array( |
| 658 |
'message' => __( 'Snippet imported.', 'wdesignkit' ), |
| 659 |
'post_id' => $post_id, |
| 660 |
), |
| 661 |
); |
| 662 |
} else { |
| 663 |
$data = array( |
| 664 |
'success' => false, |
| 665 |
'data' => array( |
| 666 |
'message' => __( 'No snippet content received from server.', 'wdesignkit' ), |
| 667 |
), |
| 668 |
); |
| 669 |
} |
| 670 |
|
| 671 |
return $data; |
| 672 |
} |
| 673 |
|
| 674 |
/** |
| 675 |
* Get user role |
| 676 |
*/ |
| 677 |
public function wdkit_get_user_roles() { |
| 678 |
$data = array( |
| 679 |
'success' => true, |
| 680 |
'data' => array( |
| 681 |
'wp_user_can_manage_options' => current_user_can( 'manage_options' ), |
| 682 |
'wp_user_roles' => wp_get_current_user()->roles, |
| 683 |
), |
| 684 |
); |
| 685 |
|
| 686 |
return $data; |
| 687 |
} |
| 688 |
|
| 689 |
/** |
| 690 |
* |
| 691 |
* This Function is used for API call |
| 692 |
* |
| 693 |
* @since 1.2.4 |
| 694 |
* |
| 695 |
* @param array $data give array. |
| 696 |
* @param array $name store data. |
| 697 |
*/ |
| 698 |
public function wkit_api_call( $data, $name ) { |
| 699 |
$u_r_l = $this->wdkit_api; |
| 700 |
|
| 701 |
if ( empty( $u_r_l ) ) { |
| 702 |
return array( |
| 703 |
'massage' => esc_html__( 'API Not Found', 'wdesignkit' ), |
| 704 |
'success' => false, |
| 705 |
); |
| 706 |
} |
| 707 |
|
| 708 |
$args = array( |
| 709 |
'method' => 'POST', |
| 710 |
'body' => $data, |
| 711 |
'timeout' => 100, |
| 712 |
); |
| 713 |
$response = wp_remote_post( $u_r_l . $name, $args ); |
| 714 |
|
| 715 |
if ( is_wp_error( $response ) ) { |
| 716 |
$error_message = $response->get_error_message(); |
| 717 |
|
| 718 |
/* Translators: %s is a placeholder for the error message */ |
| 719 |
$error_message = printf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) ); |
| 720 |
|
| 721 |
return array( |
| 722 |
'massage' => $error_message, |
| 723 |
'success' => false, |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 728 |
if ( 200 === $status_code ) { |
| 729 |
return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 730 |
} |
| 731 |
|
| 732 |
$error_message = printf( 'Server error: %d', esc_html( $status_code ) ); |
| 733 |
|
| 734 |
if ( isset( $error_data->message ) ) { |
| 735 |
$error_message .= ' (' . $error_data->message . ')'; |
| 736 |
} |
| 737 |
|
| 738 |
return array( |
| 739 |
'massage' => $error_message, |
| 740 |
'status' => $status_code, |
| 741 |
'success' => false, |
| 742 |
); |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
Wdkit_Code_Snippet::get_instance(); |
| 747 |
} |
| 748 |
|