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