| 1 |
<?php |
| 2 |
namespace SureCartQuickWebP\Licensing; |
| 3 |
|
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 5 |
|
| 6 |
/** |
| 7 |
* SureCartQuickWebP Client |
| 8 |
* |
| 9 |
* This class is necessary to set project data |
| 10 |
*/ |
| 11 |
class Client { |
| 12 |
/** |
| 13 |
* The client version |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public $version = '1.0.2'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Name of the plugin |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $name; |
| 25 |
|
| 26 |
/** |
| 27 |
* The plugin/theme file path |
| 28 |
* |
| 29 |
* @example .../wp-content/plugins/test-slug/test-slug.php |
| 30 |
* |
| 31 |
* @var string |
| 32 |
*/ |
| 33 |
public $file; |
| 34 |
|
| 35 |
/** |
| 36 |
* The public token for the store. |
| 37 |
* |
| 38 |
* @example pt_jzieNYQdE5LMAxksscgU6H4 |
| 39 |
* |
| 40 |
* @var string |
| 41 |
*/ |
| 42 |
public $public_token; |
| 43 |
|
| 44 |
/** |
| 45 |
* Main plugin file |
| 46 |
* |
| 47 |
* @example test-slug/test-slug.php |
| 48 |
* |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public $basename; |
| 52 |
|
| 53 |
/** |
| 54 |
* Slug of the plugin |
| 55 |
* |
| 56 |
* @example test-slug |
| 57 |
* |
| 58 |
* @var string |
| 59 |
*/ |
| 60 |
public $slug; |
| 61 |
|
| 62 |
/** |
| 63 |
* The project version |
| 64 |
* |
| 65 |
* @var string |
| 66 |
*/ |
| 67 |
public $project_version; |
| 68 |
|
| 69 |
/** |
| 70 |
* The project type |
| 71 |
* |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
public $type; |
| 75 |
|
| 76 |
/** |
| 77 |
* Textdomain |
| 78 |
* |
| 79 |
* @var string |
| 80 |
*/ |
| 81 |
public $textdomain; |
| 82 |
|
| 83 |
/** |
| 84 |
* The Object of Updater Class |
| 85 |
* |
| 86 |
* @var object |
| 87 |
*/ |
| 88 |
private $updater; |
| 89 |
|
| 90 |
/** |
| 91 |
* The Object of License Class |
| 92 |
* |
| 93 |
* @var object |
| 94 |
*/ |
| 95 |
private $license; |
| 96 |
|
| 97 |
/** |
| 98 |
* The Object of Activation Class |
| 99 |
* |
| 100 |
* @var object |
| 101 |
*/ |
| 102 |
private $activation; |
| 103 |
|
| 104 |
/** |
| 105 |
* The Object of Settings Class |
| 106 |
* |
| 107 |
* @var object |
| 108 |
*/ |
| 109 |
private $settings; |
| 110 |
|
| 111 |
/** |
| 112 |
* Initialize the class |
| 113 |
* |
| 114 |
* @param string $name Readable name of the plugin. |
| 115 |
* @param string $public_token The public token for the store. |
| 116 |
* @param string $file Main plugin file path. |
| 117 |
*/ |
| 118 |
public function __construct( $name, $public_token, $file = '' ) { |
| 119 |
$this->name = $name; |
| 120 |
|
| 121 |
// handle backwards compatibility. |
| 122 |
if ( ! empty( $file ) ) { |
| 123 |
$this->file = $file; |
| 124 |
$this->public_token = $public_token; |
| 125 |
} else { |
| 126 |
$this->file = $public_token; |
| 127 |
} |
| 128 |
|
| 129 |
$this->set_basename_and_slug(); |
| 130 |
|
| 131 |
$this->license(); |
| 132 |
$this->activation(); |
| 133 |
$this->updater(); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Initialize plugin/theme updater |
| 138 |
* |
| 139 |
* @return SureCartQuickWebP\Updater |
| 140 |
*/ |
| 141 |
public function updater() { |
| 142 |
if ( ! class_exists( __NAMESPACE__ . '\Updater' ) ) { |
| 143 |
require_once __DIR__ . '/Updater.php'; |
| 144 |
} |
| 145 |
|
| 146 |
// if already instantiated, return the cached one. |
| 147 |
$this->updater = $this->updater ? $this->updater : new Updater( $this ); |
| 148 |
|
| 149 |
return $this->updater; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Initialize license model |
| 154 |
* |
| 155 |
* @return SureCartQuickWebP\Licensing |
| 156 |
*/ |
| 157 |
public function license() { |
| 158 |
if ( ! class_exists( __NAMESPACE__ . '\License' ) ) { |
| 159 |
require_once __DIR__ . '/License.php'; |
| 160 |
} |
| 161 |
|
| 162 |
// if already instantiated, return the cached one. |
| 163 |
$this->license = $this->license ? $this->license : new License( $this ); |
| 164 |
|
| 165 |
return $this->license; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Initialize activation model |
| 170 |
* |
| 171 |
* @return SureCartQuickWebP\Licensing |
| 172 |
*/ |
| 173 |
public function activation() { |
| 174 |
if ( ! class_exists( __NAMESPACE__ . '\Activation' ) ) { |
| 175 |
require_once __DIR__ . '/Activation.php'; |
| 176 |
} |
| 177 |
|
| 178 |
// if already instantiated, return the cached one. |
| 179 |
$this->activation = $this->activation ? $this->activation : new Activation( $this ); |
| 180 |
|
| 181 |
return $this->activation; |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Initialize settings page |
| 186 |
* |
| 187 |
* @return SureCartQuickWebP\Licensing |
| 188 |
*/ |
| 189 |
public function settings() { |
| 190 |
if ( ! class_exists( __NAMESPACE__ . '\Settings' ) ) { |
| 191 |
require_once __DIR__ . '/Settings.php'; |
| 192 |
} |
| 193 |
|
| 194 |
// if already instantiated, return the cached one. |
| 195 |
$this->settings = $this->settings ? $this->settings : new Settings( $this ); |
| 196 |
|
| 197 |
return $this->settings; |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* API Endpoint |
| 202 |
* |
| 203 |
* @return string |
| 204 |
*/ |
| 205 |
public function endpoint() { |
| 206 |
// allow a constant to be set. |
| 207 |
if ( defined( 'SURECART_LICENSING_ENDPOINT' ) ) { |
| 208 |
return trailingslashit( SURECART_LICENSING_ENDPOINT ); |
| 209 |
} |
| 210 |
|
| 211 |
// filterable endpoint. |
| 212 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 213 |
return trailingslashit( apply_filters( 'surecart_licensing_endpoint', 'https://api.surecart.com' ) ); |
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Set project basename, slug and version |
| 218 |
* |
| 219 |
* @return void |
| 220 |
*/ |
| 221 |
protected function set_basename_and_slug() { |
| 222 |
// it's a plugin. |
| 223 |
if ( strpos( $this->file, WP_CONTENT_DIR . '/themes/' ) === false ) { |
| 224 |
$this->basename = plugin_basename( $this->file ); |
| 225 |
|
| 226 |
list( $this->slug ) = explode( '/', $this->basename ); |
| 227 |
|
| 228 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 229 |
|
| 230 |
$plugin_data = get_plugin_data( $this->file ); |
| 231 |
|
| 232 |
if ( empty( $plugin_data['Version'] ) ) { |
| 233 |
add_action( |
| 234 |
'admin_notices', |
| 235 |
function() { |
| 236 |
printf( '<div class="notice notice-error"><p>' . esc_html( $this->name ) . ' Licensing Configuration Error: The <code>__FILE__</code> must point to the main file of your plugin.</p></div>' ); |
| 237 |
} |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
$this->project_version = $plugin_data['Version']; |
| 242 |
$this->type = 'plugin'; |
| 243 |
|
| 244 |
// it's a theme. |
| 245 |
} else { |
| 246 |
$this->basename = str_replace( WP_CONTENT_DIR . '/themes/', '', $this->file ); |
| 247 |
|
| 248 |
list( $this->slug ) = explode( '/', $this->basename ); |
| 249 |
|
| 250 |
$theme = wp_get_theme( $this->slug ); |
| 251 |
|
| 252 |
$this->project_version = $theme->version; |
| 253 |
|
| 254 |
if ( empty( $theme->version ) ) { |
| 255 |
add_action( |
| 256 |
'admin_notices', |
| 257 |
function() { |
| 258 |
printf( '<div class="notice notice-error"><p>' . esc_html( $this->name ) . ' Licensing Configuration Error: The <code>__FILE__</code> must point to the main file of your theme.</p></div>' ); |
| 259 |
} |
| 260 |
); |
| 261 |
} |
| 262 |
|
| 263 |
$this->type = 'theme'; |
| 264 |
} |
| 265 |
|
| 266 |
$this->textdomain = $this->slug; |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Send request to remote endpoint |
| 271 |
* |
| 272 |
* @param array $method The method for the request. |
| 273 |
* @param string $route The route. |
| 274 |
* @param array $body The body to send. |
| 275 |
* @param bool $blocking Is this a blocking request. |
| 276 |
* |
| 277 |
* @return array|WP_Error Array of results including HTTP headers or WP_Error if the request failed. |
| 278 |
*/ |
| 279 |
public function send_request( $method = 'POST', $route = '', $body = null, $blocking = true ) { |
| 280 |
$headers = array( |
| 281 |
'X-SURECART-WP-LICENSING-SDK-VERSION' => $this->version, |
| 282 |
'Accept' => 'application/json', |
| 283 |
); |
| 284 |
|
| 285 |
if ( ! empty( $this->public_token ) ) { |
| 286 |
$headers['Authorization'] = "Bearer $this->public_token"; |
| 287 |
} |
| 288 |
|
| 289 |
$response = wp_remote_request( |
| 290 |
$this->endpoint() . $route, |
| 291 |
array( |
| 292 |
'headers' => $headers, |
| 293 |
'method' => $method, |
| 294 |
'timeout' => 30, |
| 295 |
'blocking' => $blocking, |
| 296 |
'body' => $body, |
| 297 |
) |
| 298 |
); |
| 299 |
|
| 300 |
if ( is_wp_error( $response ) ) { |
| 301 |
return $response; |
| 302 |
} |
| 303 |
|
| 304 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 305 |
$response_body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 306 |
|
| 307 |
if ( ! in_array( $response_code, array( 200, 201 ), true ) ) { |
| 308 |
if ( 404 === $response_code ) { |
| 309 |
return new \WP_Error( 'not_found', __( 'Not found', 'quickwebp' ) ); |
| 310 |
} |
| 311 |
|
| 312 |
if ( ! empty( $response_body->code ) && ! empty( $response_body->message ) ) { |
| 313 |
return new \WP_Error( $response_body->code, esc_html( $response_body->message ) ); |
| 314 |
} |
| 315 |
return new \WP_Error( 'error', __( 'Unknown error occurred, Please try again.', 'quickwebp' ) ); |
| 316 |
} |
| 317 |
|
| 318 |
return $response_body; |
| 319 |
} |
| 320 |
|
| 321 |
/** |
| 322 |
* Check if the current server is localhost |
| 323 |
* |
| 324 |
* @return boolean |
| 325 |
*/ |
| 326 |
public function is_local_server() { |
| 327 |
$remote_addr = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) ); |
| 328 |
$is_local = in_array( $remote_addr, array( '127.0.0.1', '::1' ), true ); |
| 329 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound |
| 330 |
return apply_filters( 'surecart_licensing_is_local', $is_local ); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Translate function __() |
| 335 |
* |
| 336 |
* @param string $text The text string. |
| 337 |
*/ |
| 338 |
public function __( $text ) { |
| 339 |
return call_user_func( '__', $text, $this->textdomain ); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Set project textdomain. |
| 344 |
* |
| 345 |
* @param string $textdomain The textdomain for translations. |
| 346 |
*/ |
| 347 |
public function set_textdomain( $textdomain ) { |
| 348 |
$this->textdomain = $textdomain; |
| 349 |
} |
| 350 |
} |
| 351 |
|