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