| 1 |
<?php |
| 2 |
|
| 3 |
namespace DarkifyAppSero; |
| 4 |
|
| 5 |
// Abort if this file is accessed directly, outside of WordPress. |
| 6 |
if (! defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* Appsero Client |
| 12 |
* |
| 13 |
* This class is necessary to set project data |
| 14 |
*/ |
| 15 |
if (!class_exists('Client')) { |
| 16 |
class Client |
| 17 |
{ |
| 18 |
|
| 19 |
/** |
| 20 |
* The client version |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
public $version = '2.0.4'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Hash identifier of the plugin |
| 28 |
* |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
public $hash; |
| 32 |
|
| 33 |
/** |
| 34 |
* Name of the plugin |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $name; |
| 39 |
|
| 40 |
/** |
| 41 |
* The plugin/theme file path |
| 42 |
* @example .../wp-content/plugins/test-slug/test-slug.php |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
public $file; |
| 47 |
|
| 48 |
/** |
| 49 |
* Main plugin file |
| 50 |
* @example test-slug/test-slug.php |
| 51 |
* |
| 52 |
* @var string |
| 53 |
*/ |
| 54 |
public $basename; |
| 55 |
|
| 56 |
/** |
| 57 |
* Slug of the plugin |
| 58 |
* @example test-slug |
| 59 |
* |
| 60 |
* @var string |
| 61 |
*/ |
| 62 |
public $slug; |
| 63 |
|
| 64 |
/** |
| 65 |
* The project version |
| 66 |
* |
| 67 |
* @var string |
| 68 |
*/ |
| 69 |
public $project_version; |
| 70 |
|
| 71 |
/** |
| 72 |
* The project type |
| 73 |
* |
| 74 |
* @var string |
| 75 |
*/ |
| 76 |
public $type; |
| 77 |
|
| 78 |
/** |
| 79 |
* textdomain |
| 80 |
* |
| 81 |
* @var string |
| 82 |
*/ |
| 83 |
public $textdomain; |
| 84 |
|
| 85 |
/** |
| 86 |
* Initialize the class |
| 87 |
* |
| 88 |
* @param string $hash hash of the plugin |
| 89 |
* @param string $name readable name of the plugin |
| 90 |
* @param string $file main plugin file path |
| 91 |
*/ |
| 92 |
public function __construct($hash, $name, $file) |
| 93 |
{ |
| 94 |
$this->hash = $hash; |
| 95 |
$this->name = $name; |
| 96 |
$this->file = $file; |
| 97 |
|
| 98 |
$this->set_basename_and_slug(); |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Initialize insights class |
| 103 |
* |
| 104 |
* @return Appsero\Insights |
| 105 |
*/ |
| 106 |
public function insights() |
| 107 |
{ |
| 108 |
|
| 109 |
if (!class_exists(__NAMESPACE__ . '\Insights')) { |
| 110 |
require_once __DIR__ . '/Insights.php'; |
| 111 |
} |
| 112 |
|
| 113 |
return new Insights($this); |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* API Endpoint |
| 118 |
* |
| 119 |
* @return string |
| 120 |
*/ |
| 121 |
|
| 122 |
public function endpoint() |
| 123 |
{ |
| 124 |
$endpoint = apply_filters('appsero_endpoint', 'https://api.appsero.com'); |
| 125 |
|
| 126 |
return trailingslashit($endpoint); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Set project basename, slug and version |
| 131 |
* |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
protected function set_basename_and_slug() |
| 135 |
{ |
| 136 |
|
| 137 |
// Removed checking for Theme slug |
| 138 |
|
| 139 |
$this->basename = plugin_basename($this->file); |
| 140 |
|
| 141 |
list($this->slug, $mainfile) = explode('/', $this->basename); |
| 142 |
|
| 143 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 144 |
|
| 145 |
// $plugin_data = get_plugin_data($this->file); |
| 146 |
|
| 147 |
$this->project_version = DARKIFY_VERSION; |
| 148 |
$this->type = 'plugin'; |
| 149 |
$this->textdomain = $this->slug; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Send request to remote endpoint |
| 154 |
* |
| 155 |
* @param array $params |
| 156 |
* @param string $route |
| 157 |
* |
| 158 |
* @return array|WP_Error Array of results including HTTP headers or WP_Error if the request failed. |
| 159 |
*/ |
| 160 |
public function send_request($params, $route, $blocking = false) |
| 161 |
{ |
| 162 |
$url = $this->endpoint() . $route; |
| 163 |
|
| 164 |
$headers = array( |
| 165 |
'user-agent' => 'Appsero/' . md5(esc_url(home_url())) . ';', |
| 166 |
'Accept' => 'application/json', |
| 167 |
); |
| 168 |
|
| 169 |
$response = wp_remote_post($url, array( |
| 170 |
'method' => 'POST', |
| 171 |
'timeout' => 30, |
| 172 |
'redirection' => 5, |
| 173 |
'httpversion' => '1.0', |
| 174 |
'blocking' => $blocking, |
| 175 |
'headers' => $headers, |
| 176 |
'body' => array_merge($params, array('client' => $this->version)), |
| 177 |
'cookies' => array() |
| 178 |
)); |
| 179 |
|
| 180 |
return $response; |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Check if the current server is localhost |
| 185 |
* |
| 186 |
* @return boolean |
| 187 |
*/ |
| 188 |
public function is_local_server() |
| 189 |
{ |
| 190 |
return in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1')); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Translate function _e() |
| 195 |
*/ |
| 196 |
public function _etrans($text) |
| 197 |
{ |
| 198 |
call_user_func('_e', $text, $this->textdomain); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Translate function __() |
| 203 |
*/ |
| 204 |
public function darkify_trans($text) |
| 205 |
{ |
| 206 |
return call_user_func('__', $text, $this->textdomain); |
| 207 |
} |
| 208 |
} |
| 209 |
} |