| 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 |
if ( ! class_exists( 'Wdkit_Empty_Search_Handling' ) ) { |
| 17 |
|
| 18 |
/** |
| 19 |
* Wdkit_Empty_Search_Handling class |
| 20 |
* |
| 21 |
* @since 2.0.0 |
| 22 |
*/ |
| 23 |
class Wdkit_Empty_Search_Handling { |
| 24 |
|
| 25 |
/** |
| 26 |
* Singleton instance variable. |
| 27 |
* |
| 28 |
* @var instance|null The single instance of the class. |
| 29 |
*/ |
| 30 |
private static $instance; |
| 31 |
|
| 32 |
/** |
| 33 |
* Member Variable |
| 34 |
* |
| 35 |
* @var staring $wdkit_api |
| 36 |
*/ |
| 37 |
public $wdkit_api = WDKIT_SERVER_API_URL . 'api/v2/'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Singleton instance getter method. |
| 41 |
* |
| 42 |
* @since 2.0.0 |
| 43 |
* @return self The single instance of the class. |
| 44 |
*/ |
| 45 |
public static function get_instance() { |
| 46 |
if ( ! isset( self::$instance ) ) { |
| 47 |
self::$instance = new self(); |
| 48 |
} |
| 49 |
return self::$instance; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor for the core functionality of the plugin. |
| 54 |
* |
| 55 |
* @since 2.0.0 |
| 56 |
*/ |
| 57 |
public function __construct() { |
| 58 |
add_action( 'wp_ajax_wdkit_empty_search_handling', array( $this, 'wdkit_empty_search_handling' ) ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Handle empty search handling. |
| 63 |
* |
| 64 |
* @since 2.0.0 |
| 65 |
*/ |
| 66 |
public function wdkit_empty_search_handling() { |
| 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 |
$search_type = isset( $_POST['search_type'] ) ? sanitize_text_field( wp_unslash( $_POST['search_type'] ) ) : 'plugin'; // plugin or server. |
| 74 |
$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : ''; // search keyword. |
| 75 |
$platform = isset( $_POST['platform'] ) ? sanitize_text_field( wp_unslash( $_POST['platform'] ) ) : ''; // platform name. |
| 76 |
$user_id = isset( $_POST['user_id'] ) ? sanitize_text_field( wp_unslash( $_POST['user_id'] ) ) : ''; // user id. |
| 77 |
$filters = isset( $_POST['filters'] ) ? sanitize_textarea_field( wp_unslash( $_POST['filters'] ) ) : ''; // filters. |
| 78 |
|
| 79 |
if ( empty( $search ) ) { |
| 80 |
$data = array( |
| 81 |
'success' => false, |
| 82 |
'message' => __( 'Search is required.', 'wdesignkit' ), |
| 83 |
); |
| 84 |
|
| 85 |
wp_send_json_error( $data ); |
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
$data = array( |
| 90 |
'search_type' => $search_type, |
| 91 |
'search' => $search, |
| 92 |
'platform' => $platform, |
| 93 |
'user_id' => $user_id, |
| 94 |
'filters' => $filters, |
| 95 |
); |
| 96 |
$response = $this->wkit_api_call( $data, 'front/analytics/search' ); |
| 97 |
|
| 98 |
wp_send_json_success( |
| 99 |
array( |
| 100 |
'success' => true, |
| 101 |
'message' => __( 'Empty search handling sent successfully.', 'wdesignkit' ), |
| 102 |
) |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* |
| 108 |
* This Function is used for API call |
| 109 |
* |
| 110 |
* @since 1.2.4 |
| 111 |
* |
| 112 |
* @param array $data give array. |
| 113 |
* @param array $name store data. |
| 114 |
*/ |
| 115 |
public function wkit_api_call( $data, $name ) { |
| 116 |
$u_r_l = $this->wdkit_api; |
| 117 |
|
| 118 |
if ( empty( $u_r_l ) ) { |
| 119 |
return array( |
| 120 |
'message' => esc_html__( 'API Not Found', 'wdesignkit' ), |
| 121 |
'success' => false, |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
$args = array( |
| 126 |
'method' => 'POST', |
| 127 |
'body' => $data, |
| 128 |
'timeout' => 100, |
| 129 |
); |
| 130 |
$response = wp_remote_post( $u_r_l . $name, $args ); |
| 131 |
|
| 132 |
if ( is_wp_error( $response ) ) { |
| 133 |
$error_message = $response->get_error_message(); |
| 134 |
|
| 135 |
/* Translators: %s is a placeholder for the error message */ |
| 136 |
$error_message = sprintf( esc_html__( 'API request error: %s', 'wdesignkit' ), esc_html( $error_message ) ); |
| 137 |
|
| 138 |
return array( |
| 139 |
'message' => $error_message, |
| 140 |
'success' => false, |
| 141 |
); |
| 142 |
} |
| 143 |
|
| 144 |
$status_code = wp_remote_retrieve_response_code( $response ); |
| 145 |
if ( 200 === $status_code ) { |
| 146 |
return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 147 |
} |
| 148 |
|
| 149 |
$error_message = sprintf( 'Server error: %d', esc_html( $status_code ) ); |
| 150 |
|
| 151 |
if ( isset( $error_data->message ) ) { |
| 152 |
$error_message .= ' (' . $error_data->message . ')'; |
| 153 |
} |
| 154 |
|
| 155 |
return array( |
| 156 |
'message' => $error_message, |
| 157 |
'status' => $status_code, |
| 158 |
'success' => false, |
| 159 |
); |
| 160 |
} |
| 161 |
} |
| 162 |
Wdkit_Empty_Search_Handling::get_instance(); |
| 163 |
} |
| 164 |
|