| 1 |
<?php |
| 2 |
|
| 3 |
namespace Appsero; |
| 4 |
|
| 5 |
/** |
| 6 |
* Appsero Client |
| 7 |
* |
| 8 |
* This class is necessary to set project data |
| 9 |
*/ |
| 10 |
class Client { |
| 11 |
|
| 12 |
/** |
| 13 |
* The client version |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
public $version = '2.0.4'; |
| 18 |
|
| 19 |
/** |
| 20 |
* Hash identifier of the plugin |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $hash; |
| 25 |
|
| 26 |
/** |
| 27 |
* Name of the plugin |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $name; |
| 32 |
|
| 33 |
/** |
| 34 |
* The plugin/theme file path |
| 35 |
* |
| 36 |
* @example .../wp-content/plugins/test-slug/test-slug.php |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public $file; |
| 41 |
|
| 42 |
/** |
| 43 |
* Main plugin file |
| 44 |
* |
| 45 |
* @example test-slug/test-slug.php |
| 46 |
* |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
public $basename; |
| 50 |
|
| 51 |
/** |
| 52 |
* Slug of the plugin |
| 53 |
* |
| 54 |
* @example test-slug |
| 55 |
* |
| 56 |
* @var string |
| 57 |
*/ |
| 58 |
public $slug; |
| 59 |
|
| 60 |
/** |
| 61 |
* The project version |
| 62 |
* |
| 63 |
* @var string |
| 64 |
*/ |
| 65 |
public $project_version; |
| 66 |
|
| 67 |
/** |
| 68 |
* The project type |
| 69 |
* |
| 70 |
* @var string |
| 71 |
*/ |
| 72 |
public $type; |
| 73 |
|
| 74 |
/** |
| 75 |
* Textdomain |
| 76 |
* |
| 77 |
* @var string |
| 78 |
*/ |
| 79 |
public $textdomain; |
| 80 |
|
| 81 |
/** |
| 82 |
* The Object of Insights Class |
| 83 |
* |
| 84 |
* @var object |
| 85 |
*/ |
| 86 |
private $insights; |
| 87 |
|
| 88 |
/** |
| 89 |
* The Object of License Class |
| 90 |
* |
| 91 |
* @var object |
| 92 |
*/ |
| 93 |
private $license; |
| 94 |
|
| 95 |
/** |
| 96 |
* Initialize the class |
| 97 |
* |
| 98 |
* @param string $hash hash of the plugin |
| 99 |
* @param string $name readable name of the plugin |
| 100 |
* @param string $file main plugin file path |
| 101 |
*/ |
| 102 |
public function __construct( $hash, $name, $file ) { |
| 103 |
$this->hash = $hash; |
| 104 |
$this->name = $name; |
| 105 |
$this->file = $file; |
| 106 |
|
| 107 |
$this->set_basename_and_slug(); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Initialize insights class |
| 112 |
* |
| 113 |
* @return Appsero\Insights |
| 114 |
*/ |
| 115 |
public function insights() { |
| 116 |
if ( ! class_exists( __NAMESPACE__ . '\Insights' ) ) { |
| 117 |
require_once __DIR__ . '/Insights.php'; |
| 118 |
} |
| 119 |
|
| 120 |
// if already instantiated, return the cached one |
| 121 |
if ( $this->insights ) { |
| 122 |
return $this->insights; |
| 123 |
} |
| 124 |
|
| 125 |
$this->insights = new Insights( $this ); |
| 126 |
|
| 127 |
return $this->insights; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Initialize plugin/theme updater |
| 132 |
* |
| 133 |
* @return void |
| 134 |
*/ |
| 135 |
public function updater() { |
| 136 |
// do not show update notice on ajax request and rest api request |
| 137 |
if ( wp_doing_ajax() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
// show deprecated notice |
| 142 |
_deprecated_function( __CLASS__ . '::updater', '2.0', '\Appsero\Updater::init($client);, for more details please visit: https://appsero.com/docs/appsero-developers-guide/appsero-client/appsero-sdk-updater-changes/' ); |
| 143 |
|
| 144 |
// initialize the new updater |
| 145 |
if ( method_exists( '\Appsero\Updater', 'init' ) ) { |
| 146 |
\Appsero\Updater::init( $this ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Initialize license checker |
| 152 |
* |
| 153 |
* @return Appsero\License |
| 154 |
*/ |
| 155 |
public function license() { |
| 156 |
if ( ! class_exists( __NAMESPACE__ . '\License' ) ) { |
| 157 |
require_once __DIR__ . '/License.php'; |
| 158 |
} |
| 159 |
|
| 160 |
// if already instantiated, return the cached one |
| 161 |
if ( $this->license ) { |
| 162 |
return $this->license; |
| 163 |
} |
| 164 |
|
| 165 |
$this->license = new License( $this ); |
| 166 |
|
| 167 |
return $this->license; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* API Endpoint |
| 172 |
* |
| 173 |
* @return string |
| 174 |
*/ |
| 175 |
public function endpoint() { |
| 176 |
$endpoint = apply_filters( 'appsero_endpoint', 'https://api.appsero.com' ); |
| 177 |
|
| 178 |
return trailingslashit( $endpoint ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Set project basename, slug and version |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
protected function set_basename_and_slug() { |
| 187 |
if ( strpos( $this->file, WP_CONTENT_DIR . '/themes/' ) === false ) { |
| 188 |
$this->basename = plugin_basename( $this->file ); |
| 189 |
|
| 190 |
list( $this->slug, $mainfile ) = explode( '/', $this->basename ); |
| 191 |
|
| 192 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 193 |
|
| 194 |
$plugin_data = get_plugin_data( $this->file, false, false ); |
| 195 |
|
| 196 |
$this->project_version = $plugin_data['Version']; |
| 197 |
$this->type = 'plugin'; |
| 198 |
} else { |
| 199 |
$this->basename = str_replace( WP_CONTENT_DIR . '/themes/', '', $this->file ); |
| 200 |
|
| 201 |
list( $this->slug, $mainfile ) = explode( '/', $this->basename ); |
| 202 |
|
| 203 |
$theme = wp_get_theme( $this->slug ); |
| 204 |
|
| 205 |
$this->project_version = $theme->version; |
| 206 |
$this->type = 'theme'; |
| 207 |
} |
| 208 |
|
| 209 |
$this->textdomain = $this->slug; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Send request to remote endpoint |
| 214 |
* |
| 215 |
* @param array $params |
| 216 |
* @param string $route |
| 217 |
* |
| 218 |
* @return array|WP_Error array of results including HTTP headers or WP_Error if the request failed |
| 219 |
*/ |
| 220 |
public function send_request( $params, $route, $blocking = false ) { |
| 221 |
$url = $this->endpoint() . $route; |
| 222 |
|
| 223 |
$headers = [ |
| 224 |
'user-agent' => 'Appsero/' . md5( esc_url( home_url() ) ) . ';', |
| 225 |
'Accept' => 'application/json', |
| 226 |
]; |
| 227 |
|
| 228 |
$response = wp_remote_post( |
| 229 |
$url, |
| 230 |
[ |
| 231 |
'method' => 'POST', |
| 232 |
'timeout' => 30, |
| 233 |
'redirection' => 5, |
| 234 |
'httpversion' => '1.0', |
| 235 |
'blocking' => $blocking, |
| 236 |
'headers' => $headers, |
| 237 |
'body' => array_merge( $params, [ 'client' => $this->version ] ), |
| 238 |
'cookies' => [], |
| 239 |
] |
| 240 |
); |
| 241 |
|
| 242 |
return $response; |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Check if the current server is localhost |
| 247 |
* |
| 248 |
* @return bool |
| 249 |
*/ |
| 250 |
public function is_local_server() { |
| 251 |
$is_local = isset( $_SERVER['REMOTE_ADDR'] ) && in_array( $_SERVER['REMOTE_ADDR'], [ '127.0.0.1', '::1' ], true ); |
| 252 |
|
| 253 |
return apply_filters( 'appsero_is_local', $is_local ); |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* Translate function _e() |
| 258 |
*/ |
| 259 |
// phpcs:ignore |
| 260 |
public function _etrans( $text ) { |
| 261 |
call_user_func( '_e', $text, $this->textdomain ); |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Translate function __() |
| 266 |
*/ |
| 267 |
// phpcs:ignore |
| 268 |
public function __trans( $text ) { |
| 269 |
return call_user_func( '__', $text, $this->textdomain ); |
| 270 |
} |
| 271 |
|
| 272 |
/** |
| 273 |
* Set project textdomain |
| 274 |
*/ |
| 275 |
public function set_textdomain( $textdomain ) { |
| 276 |
$this->textdomain = $textdomain; |
| 277 |
} |
| 278 |
} |
| 279 |
|