| 1 |
<?php |
| 2 |
namespace Appsero; |
| 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.9'; |
| 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 |
* @example .../wp-content/plugins/test-slug/test-slug.php |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $file; |
| 39 |
|
| 40 |
/** |
| 41 |
* Main plugin file |
| 42 |
* @example test-slug/test-slug.php |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
public $basename; |
| 47 |
|
| 48 |
/** |
| 49 |
* Slug of the plugin |
| 50 |
* @example test-slug |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
public $slug; |
| 55 |
|
| 56 |
/** |
| 57 |
* The project version |
| 58 |
* |
| 59 |
* @var string |
| 60 |
*/ |
| 61 |
public $project_version; |
| 62 |
|
| 63 |
/** |
| 64 |
* The project type |
| 65 |
* |
| 66 |
* @var string |
| 67 |
*/ |
| 68 |
public $type; |
| 69 |
|
| 70 |
/** |
| 71 |
* textdomain |
| 72 |
* |
| 73 |
* @var string |
| 74 |
*/ |
| 75 |
public $textdomain; |
| 76 |
|
| 77 |
/** |
| 78 |
* Initialize the class |
| 79 |
* |
| 80 |
* @param string $hash hash of the plugin |
| 81 |
* @param string $name readable name of the plugin |
| 82 |
* @param string $file main plugin file path |
| 83 |
*/ |
| 84 |
public function __construct( $hash, $name, $file ) { |
| 85 |
$this->hash = $hash; |
| 86 |
$this->name = $name; |
| 87 |
$this->file = $file; |
| 88 |
|
| 89 |
$this->set_basename_and_slug(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Initialize insights class |
| 94 |
* |
| 95 |
* @return Appsero\Insights |
| 96 |
*/ |
| 97 |
public function insights() { |
| 98 |
|
| 99 |
if ( ! class_exists( __NAMESPACE__ . '\Insights') ) { |
| 100 |
require_once __DIR__ . '/Insights.php'; |
| 101 |
} |
| 102 |
|
| 103 |
return new Insights( $this ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Initialize plugin/theme updater |
| 108 |
* |
| 109 |
* @return Appsero\Updater |
| 110 |
*/ |
| 111 |
public function updater() { |
| 112 |
|
| 113 |
if ( ! class_exists( __NAMESPACE__ . '\Updater') ) { |
| 114 |
require_once __DIR__ . '/Updater.php'; |
| 115 |
} |
| 116 |
|
| 117 |
return new Updater( $this ); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Initialize license checker |
| 122 |
* |
| 123 |
* @return Appsero\License |
| 124 |
*/ |
| 125 |
public function license() { |
| 126 |
|
| 127 |
if ( ! class_exists( __NAMESPACE__ . '\License') ) { |
| 128 |
require_once __DIR__ . '/License.php'; |
| 129 |
} |
| 130 |
|
| 131 |
return new License( $this ); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* API Endpoint |
| 136 |
* |
| 137 |
* @return string |
| 138 |
*/ |
| 139 |
public function endpoint() { |
| 140 |
$endpoint = apply_filters( 'appsero_endpoint', 'https://api.appsero.com' ); |
| 141 |
|
| 142 |
return trailingslashit( $endpoint ); |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Set project basename, slug and version |
| 147 |
* |
| 148 |
* @return void |
| 149 |
*/ |
| 150 |
protected function set_basename_and_slug() { |
| 151 |
|
| 152 |
if ( strpos( $this->file, WP_CONTENT_DIR . '/themes/' ) === false ) { |
| 153 |
|
| 154 |
$this->basename = plugin_basename( $this->file ); |
| 155 |
|
| 156 |
list( $this->slug, $mainfile) = explode( '/', $this->basename ); |
| 157 |
|
| 158 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 159 |
|
| 160 |
$plugin_data = get_plugin_data( $this->file ); |
| 161 |
|
| 162 |
$this->project_version = $plugin_data['Version']; |
| 163 |
$this->type = 'plugin'; |
| 164 |
$this->textdomain = $this->slug; |
| 165 |
|
| 166 |
} else { |
| 167 |
|
| 168 |
$this->basename = str_replace( WP_CONTENT_DIR . '/themes/', '', $this->file ); |
| 169 |
|
| 170 |
list( $this->slug, $mainfile) = explode( '/', $this->basename ); |
| 171 |
|
| 172 |
$theme = wp_get_theme( $this->slug ); |
| 173 |
|
| 174 |
$this->project_version = $theme->version; |
| 175 |
$this->type = 'theme'; |
| 176 |
|
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Send request to remote endpoint |
| 182 |
* |
| 183 |
* @param array $params |
| 184 |
* @param string $route |
| 185 |
* |
| 186 |
* @return array|WP_Error Array of results including HTTP headers or WP_Error if the request failed. |
| 187 |
*/ |
| 188 |
public function send_request( $params, $route, $blocking = false ) { |
| 189 |
$url = $this->endpoint() . $route; |
| 190 |
|
| 191 |
$headers = array( |
| 192 |
'user-agent' => 'Appsero/' . md5( esc_url( home_url() ) ) . ';', |
| 193 |
'Accept' => 'application/json', |
| 194 |
); |
| 195 |
|
| 196 |
$response = wp_remote_post( $url, array( |
| 197 |
'method' => 'POST', |
| 198 |
'timeout' => 30, |
| 199 |
'redirection' => 5, |
| 200 |
'httpversion' => '1.0', |
| 201 |
'blocking' => $blocking, |
| 202 |
'headers' => $headers, |
| 203 |
'body' => array_merge( $params, array( 'client' => $this->version ) ), |
| 204 |
'cookies' => array() |
| 205 |
) ); |
| 206 |
|
| 207 |
return $response; |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|