| 1 |
<?php |
| 2 |
/** |
| 3 |
* WebAppick Services Client |
| 4 |
* @version 1.0.2 |
| 5 |
* @package CTXFeed |
| 6 |
* @subpackage AppServices |
| 7 |
* This Package is based on AppSero project by weDevs |
| 8 |
* @see https://github.com/WebAppick/client |
| 9 |
* @license MIT |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Disco\Libs\WebAppick\AppServices; |
| 13 |
|
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
die(); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Class Client |
| 22 |
*/ |
| 23 |
class Client { |
| 24 |
|
| 25 |
/** |
| 26 |
* The client version. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
protected $clientVersion = '1.0.3'; |
| 31 |
|
| 32 |
/** |
| 33 |
* API EndPoint. |
| 34 |
* |
| 35 |
* @var string |
| 36 |
*/ |
| 37 |
protected $API_EndPoint = 'https://track.webappick.com/api/'; |
| 38 |
|
| 39 |
/** |
| 40 |
* API Version. |
| 41 |
* |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
protected $apiVersion = 'v1'; |
| 45 |
|
| 46 |
/** |
| 47 |
* Hash identifier of the Plugin/Theme. |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
protected $hash; |
| 52 |
|
| 53 |
/** |
| 54 |
* Name of the Plugin/Theme. |
| 55 |
* |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
protected $name; |
| 59 |
|
| 60 |
/** |
| 61 |
* The Plugin/Theme file path. |
| 62 |
* Example .../wp-content/Plugin/test-slug/test-slug.php. |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
protected $file; |
| 67 |
|
| 68 |
/** |
| 69 |
* Main Plugin/Theme file. |
| 70 |
* Example: test-slug/test-slug.php. |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
protected $basename; |
| 75 |
|
| 76 |
/** |
| 77 |
* Slug of the Plugin/Theme. |
| 78 |
* Example: test-slug. |
| 79 |
* |
| 80 |
* @var string |
| 81 |
*/ |
| 82 |
protected $slug; |
| 83 |
|
| 84 |
/** |
| 85 |
* The project version. |
| 86 |
* |
| 87 |
* @var string |
| 88 |
*/ |
| 89 |
protected $project_version; |
| 90 |
|
| 91 |
/** |
| 92 |
* The project type. |
| 93 |
* |
| 94 |
* @var string |
| 95 |
*/ |
| 96 |
protected $type; |
| 97 |
|
| 98 |
/** |
| 99 |
* Store Product (unique) id for current Product |
| 100 |
* Required by WooCommerce API Manager > 2.0. |
| 101 |
* |
| 102 |
* @var bool|int |
| 103 |
*/ |
| 104 |
protected $product_id; |
| 105 |
|
| 106 |
/** |
| 107 |
* Instance of Insights class. |
| 108 |
* |
| 109 |
* @since 1.0.3 |
| 110 |
* @var Insights |
| 111 |
*/ |
| 112 |
private $insights; |
| 113 |
|
| 114 |
/** |
| 115 |
* Instance of Promotions class. |
| 116 |
* |
| 117 |
* @since 1.0.3 |
| 118 |
* @var Promotions |
| 119 |
*/ |
| 120 |
private $promotions; |
| 121 |
|
| 122 |
/** |
| 123 |
* Instance of License class. |
| 124 |
* |
| 125 |
* @since 1.0.3 |
| 126 |
* @var License |
| 127 |
*/ |
| 128 |
private $license; |
| 129 |
|
| 130 |
/** |
| 131 |
* Instance of Updater class. |
| 132 |
* |
| 133 |
* @since 1.0.3 |
| 134 |
* @var Updater |
| 135 |
*/ |
| 136 |
private $updater; |
| 137 |
|
| 138 |
/** |
| 139 |
* Initialize the class. |
| 140 |
* |
| 141 |
* @param string $hash hash of the Plugin/Theme. |
| 142 |
* @param string $name readable name of the Plugin/Theme. |
| 143 |
* @param string $file main Plugin/Theme file path. |
| 144 |
* @param string $product_id Store Product id for pro product. |
| 145 |
* If null license page will show field for product id input. |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
public function __construct( $hash, $name, $file, $product_id = null ) { |
| 149 |
$this->hash = $hash; |
| 150 |
$this->name = $name; |
| 151 |
$this->file = $file; |
| 152 |
$this->product_id = ! empty( $product_id ) ? (int) $product_id : false; |
| 153 |
$this->set_basename_and_slug(); |
| 154 |
} |
| 155 |
|
| 156 |
/** |
| 157 |
* Initialize insights class. |
| 158 |
* |
| 159 |
* @return Insights |
| 160 |
*/ |
| 161 |
public function insights() { |
| 162 |
if ( ! is_null( $this->insights ) ) { |
| 163 |
return $this->insights; |
| 164 |
} |
| 165 |
|
| 166 |
if ( ! class_exists( __NAMESPACE__ . '\Insights' ) ) { |
| 167 |
require_once __DIR__ . '/Insights.php'; |
| 168 |
} |
| 169 |
$this->insights = new Insights( $this ); |
| 170 |
|
| 171 |
return $this->insights; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Initialize Promotions class. |
| 176 |
* |
| 177 |
* @return Promotions |
| 178 |
*/ |
| 179 |
public function promotions() { |
| 180 |
if ( ! is_null( $this->promotions ) ) { |
| 181 |
return $this->promotions; |
| 182 |
} |
| 183 |
if ( ! class_exists( __NAMESPACE__ . '\Promotions' ) ) { |
| 184 |
require_once __DIR__ . '/Promotions.php'; |
| 185 |
} |
| 186 |
$this->promotions = new Promotions( $this ); |
| 187 |
|
| 188 |
return $this->promotions; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* API Endpoint. |
| 193 |
* |
| 194 |
* @param string $route route to send the request. |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
private function endpoint( $route = '' ) { |
| 199 |
/** |
| 200 |
* Filter Request Route string |
| 201 |
* @param string $route |
| 202 |
* @param array $params |
| 203 |
*/ |
| 204 |
$route = apply_filters( $this->slug . '_request_route', $route ); |
| 205 |
/** |
| 206 |
* API EndPoint |
| 207 |
* @since 1.0.0 |
| 208 |
* @param string $url |
| 209 |
* |
| 210 |
*/ |
| 211 |
$this->API_EndPoint = apply_filters( $this->slug . '_WebAppick_API_EndPoint', $this->API_EndPoint, $this->apiVersion, $this->clientVersion ); |
| 212 |
$this->API_EndPoint = untrailingslashit( $this->API_EndPoint ); |
| 213 |
$route = rtrim( $route, '/\\' ); |
| 214 |
$route = ltrim( $route, '/\\' ); |
| 215 |
$URL = "{$this->API_EndPoint}/{$this->apiVersion}/$route"; |
| 216 |
/** |
| 217 |
* Filter Final API URL for request |
| 218 |
* @since 1.0.0 |
| 219 |
* |
| 220 |
* @param string $URL |
| 221 |
* @param string $API_EndPoint |
| 222 |
* @param string $route |
| 223 |
* @param string $apiVersion |
| 224 |
* @param string $clientVersion |
| 225 |
*/ |
| 226 |
$URL = apply_filters( $this->slug . '_WebAppick_API_URL', $URL, $this->API_EndPoint, $route, $this->apiVersion, $this->clientVersion ); |
| 227 |
return untrailingslashit( $URL ); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Set project basename, slug and version. |
| 232 |
* |
| 233 |
* @return void |
| 234 |
*/ |
| 235 |
protected function set_basename_and_slug() { |
| 236 |
|
| 237 |
if ( false === strpos( $this->file, WP_CONTENT_DIR . '/themes/' ) ) { |
| 238 |
$this->basename = plugin_basename( $this->file ); |
| 239 |
list( $this->slug, $mainfile ) = explode( '/', $this->basename ); |
| 240 |
if ( ! function_exists( 'get_plugin_data' ) ) require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 241 |
//$plugin_data = get_plugin_data( $this->file ); |
| 242 |
if( is_plugin_active( 'disco-pro/disco-pro.php' ) ) { |
| 243 |
$plugin_data = get_plugin_data( DISCO_PRO_PLUGIN_FILE, false, false ); |
| 244 |
} else { |
| 245 |
$plugin_data = get_plugin_data( DISCO_PLUGIN_FILE, false, false ); |
| 246 |
} |
| 247 |
|
| 248 |
$this->project_version = $plugin_data['Version']; |
| 249 |
$this->type = 'plugin'; |
| 250 |
} else { |
| 251 |
$this->basename = str_replace( WP_CONTENT_DIR . '/themes/', '', $this->file ); |
| 252 |
|
| 253 |
list( $this->slug, $main_file ) = explode( '/', $this->basename ); |
| 254 |
$theme = wp_get_theme( $this->slug ); |
| 255 |
$this->project_version = $theme->version; |
| 256 |
$this->type = 'theme'; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Client UserAgent String. |
| 262 |
* |
| 263 |
* @return string |
| 264 |
*/ |
| 265 |
private function __user_agent() { |
| 266 |
return 'WebAppick/' . md5( esc_url( home_url() ) ) . ';'; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Send request to remote endpoint. |
| 271 |
* |
| 272 |
* @param array $params Parameters/Data that being sent. |
| 273 |
* @param string $route Route to send the request to. |
| 274 |
* @param bool $blocking Block Execution Until the server response back or timeout. |
| 275 |
* |
| 276 |
* @return array|WP_Error Array of results including HTTP headers or WP_Error if the request failed. |
| 277 |
*/ |
| 278 |
public function send_request( $params, $route = '', $blocking = false ) { |
| 279 |
$url = $this->endpoint( $route ); |
| 280 |
$headers = array( |
| 281 |
'user-agent' => $this->__user_agent(), |
| 282 |
'Accept' => 'application/json', |
| 283 |
); |
| 284 |
|
| 285 |
/** |
| 286 |
* before request to api server. |
| 287 |
* |
| 288 |
* @since 1.0.2 |
| 289 |
* @param array $params |
| 290 |
* @param string $route |
| 291 |
* @param array $headers |
| 292 |
* @param string $clientVersion |
| 293 |
* @param string $url |
| 294 |
*/ |
| 295 |
do_action( $this->getSlug() . '_before_request', $params, $route, $headers, $this->clientVersion, $url ); |
| 296 |
if ( ! empty( $route ) ) { |
| 297 |
/** |
| 298 |
* before request to api server to route. |
| 299 |
* @since 1.0.2 |
| 300 |
* @param array $params |
| 301 |
* @param string $route |
| 302 |
* @param array $headers |
| 303 |
* @param string $clientVersion |
| 304 |
* @param string $url |
| 305 |
*/ |
| 306 |
do_action( $this->getSlug() . '_before_request_' . $route, $params, $route, $headers, $url ); |
| 307 |
} |
| 308 |
/** |
| 309 |
* Request Blocking mode. |
| 310 |
* Set it to true for debugging the response with after request action. |
| 311 |
* |
| 312 |
* @since 1.0.2 |
| 313 |
* @param bool $blocking |
| 314 |
*/ |
| 315 |
$blocking = (bool) apply_filters( $this->getSlug() . '_request_blocking_mode', $blocking ); |
| 316 |
$response = wp_safe_remote_post( |
| 317 |
esc_url( $url ), |
| 318 |
[ |
| 319 |
'method' => 'POST', |
| 320 |
'timeout' => 45, // phpcs:ignore |
| 321 |
'redirection' => 5, |
| 322 |
'httpversion' => '1.0', |
| 323 |
'blocking' => $blocking, |
| 324 |
'headers' => $headers, |
| 325 |
'body' => array_merge( $params, [ 'client' => $this->clientVersion ] ), |
| 326 |
'cookies' => [], |
| 327 |
] |
| 328 |
); |
| 329 |
/** |
| 330 |
* After request to api server. |
| 331 |
* |
| 332 |
* @since 1.0.2 |
| 333 |
* @param array $response |
| 334 |
* @param string $route |
| 335 |
*/ |
| 336 |
do_action( $this->getSlug() . '_after_request', $response, $route ); |
| 337 |
if ( ! empty( $route ) ) { |
| 338 |
/** |
| 339 |
* After request to api server to route. |
| 340 |
* |
| 341 |
* @since 1.0.2 |
| 342 |
* @param array $response |
| 343 |
* @param string $route |
| 344 |
*/ |
| 345 |
do_action( $this->getSlug() . '_after_request_' . $route, $response, $route ); |
| 346 |
} |
| 347 |
return $response; |
| 348 |
} |
| 349 |
|
| 350 |
//===> Getters. |
| 351 |
|
| 352 |
/** |
| 353 |
* Get Version of this client. |
| 354 |
* |
| 355 |
* @return string |
| 356 |
*/ |
| 357 |
public function getClientVersion() { |
| 358 |
return $this->clientVersion; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Get API URI. |
| 363 |
* |
| 364 |
* @return string |
| 365 |
*/ |
| 366 |
public function getApi() { |
| 367 |
return $this->API_EndPoint; |
| 368 |
} |
| 369 |
|
| 370 |
/** |
| 371 |
* Get API Version using by this client. |
| 372 |
* |
| 373 |
* @return string |
| 374 |
*/ |
| 375 |
public function getApiVersion() { |
| 376 |
return $this->apiVersion; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Get Hash of current Plugin/Theme. |
| 381 |
* |
| 382 |
* @return string |
| 383 |
*/ |
| 384 |
public function getHash() { |
| 385 |
return $this->hash; |
| 386 |
} |
| 387 |
|
| 388 |
/** |
| 389 |
* Get Plugin/Theme Name. |
| 390 |
* |
| 391 |
* @return string |
| 392 |
*/ |
| 393 |
public function getName() { |
| 394 |
return $this->name; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Store Product ID. |
| 399 |
* |
| 400 |
* @return bool|int |
| 401 |
*/ |
| 402 |
public function getProductId() { |
| 403 |
return $this->product_id; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Get Plugin/Theme file. |
| 408 |
* |
| 409 |
* @return string |
| 410 |
*/ |
| 411 |
public function getFile() { |
| 412 |
return $this->file; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Get Plugin/Theme base name. |
| 417 |
* |
| 418 |
* @return string |
| 419 |
*/ |
| 420 |
public function getBasename() { |
| 421 |
return $this->basename; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Get Plugin/Theme Slug. |
| 426 |
* |
| 427 |
* @return string |
| 428 |
*/ |
| 429 |
public function getSlug() { |
| 430 |
return $this->slug; |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Get Plugin/Theme Project Version. |
| 435 |
* |
| 436 |
* @return string |
| 437 |
*/ |
| 438 |
public function getProjectVersion() { |
| 439 |
return $this->project_version; |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Get Project Type Plugin/Theme. |
| 444 |
* |
| 445 |
* @return string |
| 446 |
*/ |
| 447 |
public function getType() { |
| 448 |
return $this->type; |
| 449 |
} |
| 450 |
} |
| 451 |
// End of file Client.php. |
| 452 |
|