-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathar-wplugin.php
361 lines (251 loc) · 10.5 KB
/
ar-wplugin.php
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<?php
/*
ArvanCloud CDN Wordpress Plugin
* Plugin Name: ArvanCloud CDN Wordpress Plugin
* Plugin URI: https://github.com/arvancloud/ar-wplugin
* Github URI: https://github.com/zedium/ar-wplugin
* Description: WordPress Plugin for ArvanCloud CDN
* Author: Arvan Cloud
* Author URI: https://github.com/zedium/
* Version: 1.0.0
* Text Domain: arvancloud-theme-textdomain
* License: GPLv3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.txt
* Provides: ArvanCloud
*
* @package ArvanCloudPlugin
* @author Arvan Cloud
* @license GNU General Public License, version 3
* @copyright 2012-2020 Arvan Cloud
*/
// Block direct access
defined( 'ABSPATH' ) || exit;
class ArPluginCore{
private $opt_name = 'arvan';
public static $instance = null;
/**
* Constructor initialize and load default values
*/
public function __construct(){
// Loads the optionpanel/framework.php
$this->include_redux_core();
//optionpanel/config.php
$this->inlcude_redux_config();
$this->get_domain_list();
/**
* This code loads the cache status from ArvanCloud
* And set the default values in plugin option
*/
$this->call_cache_status();
/**
* Called when options in plugin options saved
*/
add_action('redux/options/arvan/saved', array($this, 'redux_after_saved'));
/**
* Include javascripts
*/
add_action('admin_enqueue_scripts', array($this, 'arvan_wp_enqueue_scripts'));
/**
* Will be called when Total Purge button clicked in plugin option
*/
add_action("wp_ajax_nopriv_arvan_total_purge", array($this,"arvan_total_purge_ajax_nopriv"));
add_action("wp_ajax_arvan_total_purge", array($this,"arvan_total_purge_ajax"));
/**
* To purge specific post url cache
*/
add_action("publish_post", array($this,"arvan_save_post_action"),1,3);
}
public static function getInstance(){
if(self::$instance == null)
self::$instance = new self();
return self::$instance;
}
private function get_domain_list(){
$domains = array();
$arvan_url = Redux::get_option( $this->opt_name, 'arvan-cache-endpoint-url');
$arvan_api_key = Redux::get_option( $this->opt_name, 'arvan-api-key');
$response = wp_remote_get( $arvan_url, array(
'headers' => array('Content-Type' => 'application/json; charset=utf-8','Authorization' => $arvan_api_key),
)
);
$domains = null;
if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$headers = $response['headers']; // array of http header lines
$body = $response['body']; // use the content
$responseBody = wp_remote_retrieve_body( $response );
$decoded_data = json_decode($responseBody)->data;
$domains = array();
foreach($decoded_data as $data){
$domains[] = $data->domain;
}
}
return $domains;
}
/**
* Call cache status from remote Arvan Settings
* And set it to plugin options
*/
public function call_cache_status(){
/**
* Load Cache EndPoint URL from plugin options to make a request
*/
$arvan_url = Redux::get_option( $this->opt_name, 'arvan-cache-endpoint-url');
$domain = Redux::get_option( $this->opt_name, 'arvan-domain');
$full_url = $arvan_url . $domain . '/caching';
/**
* Load API-Key from plugin options
*/
$arvan_api_key = Redux::get_option( $this->opt_name, 'arvan-api-key');
/**
* Make a request to server
*/
$response = wp_remote_get( $full_url , array(
'headers' => array('Authorization' => $arvan_api_key),
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
} else {
if(!isset($response['body']))
return;
/**
* Get cache status from JSON response
*/
$response_object = json_decode($response['body']);
$cache_status = $response_object->data->cache_status;
/**
* Save the remote cache status to plugin options
*/
Redux::set_option( $this->opt_name, 'arvan-cache-status', $cache_status);
}
}
/**
* Include plugin options core file
*/
public function include_redux_core(){
if (!class_exists('ReduxFramework') && file_exists(plugin_dir_path(__FILE__) . '/optionpanel/framework.php'))
{
require_once ('optionpanel/framework.php');
}
}
/**
* Include plugin options config file
*/
public function inlcude_redux_config(){
if (!isset($redux_demo) && file_exists(plugin_dir_path(__FILE__) . '/optionpanel/config.php'))
{
require_once ('optionpanel/config.php');
}
}
/**
* Import javascript files to admin header
*/
public function arvan_wp_enqueue_scripts(){
wp_register_script('arvan-plugin-custom-script', plugin_dir_url(__FILE__) . '/js/custom.js', array('jquery'));
wp_localize_script( 'arvan-plugin-custom-script', 'WPData', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
wp_enqueue_script( 'arvan-plugin-custom-script');
}
function arvan_total_purge_ajax_nopriv()
{
wp_die('Oops, you don\'t have permissions.');
}
/**
* These function does a request to ArvanCloud to make Total Purge
* It will be called when Total Purge button plugin option clicked
* It will be called via ajax request
*/
function arvan_total_purge_ajax(){
if ( current_user_can( 'manage_options' ) ) {
/**
* Making Purge request URL
*/
$arvan_url = Redux::get_option( $this->opt_name, 'arvan-cache-endpoint-url');
$domain = Redux::get_option( $this->opt_name, 'arvan-domain');
$full_url = $arvan_url . $domain . '/caching';
if(empty($arvan_url)){
wp_send_json_error(array("message"=>"Endpoint url is empty"));
return;
}
$full_url .= '?purge=all';
$arvan_api_key = Redux::get_option( $this->opt_name, 'arvan-api-key');
if(empty($arvan_api_key)){
wp_send_json_error(array("message"=>"API Key is empty"));
return;
}
$response = wp_remote_get( $full_url, array(
'method' => 'DELETE',
'blocking' => true,
'headers' => array('Authorization' => $arvan_api_key),
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}else{
if(!isset($response['body']))
return;
$response_object = json_decode($response['body']);
/**
* Send a successful response to client JS
*/
wp_send_json_success($response_object);
}
}
}
/**
* This function called when plugin option got saved
* Mainly used for set the cache status on ArvanCloud
*/
public function redux_after_saved($value){
$arvan_api_key = Redux::get_option( $this->opt_name, 'arvan-api-key');
$arvan_url = Redux::get_option( $this->opt_name, 'arvan-cache-endpoint-url');
$domain = Redux::get_option( $this->opt_name, 'arvan-domain');
$full_url = $arvan_url . $domain . '/caching';
$body = '{"cache_status":"'. $value['arvan-cache-status'] .'"}';
$response = wp_remote_post( $full_url, array(
'method' => 'PATCH',
'blocking' => true,
'headers' => array('Content-Type' => 'application/json; charset=utf-8','Authorization' => $arvan_api_key),
'body' => $body,
'data_format' => 'body'
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}
}
/**
* Function will be used for clear a webpage cache after new post or change
* When a new post created it makes a request to ArvanCloud to purge the cached url
*/
public function arvan_save_post_action($post_id ){
$purge_after_save_status = Redux::get_option( $this->opt_name, 'arvan-save-post-status');
/**
* Do nothin if option turned off
*/
if(isset($purge_after_save_status) && ($purge_after_save_status == 'off'))
return;
$url = get_permalink($post_id);
$arvan_url = Redux::get_option( $this->opt_name, 'arvan-cache-endpoint-url');
$domain = Redux::get_option( $this->opt_name, 'arvan-domain');
$full_url = $arvan_url . $domain . '/caching';
$full_url .= '?purge=individual&purge_urls='. $url;
$arvan_api_key = Redux::get_option( $this->opt_name, 'arvan-api-key');
$response = wp_remote_get( $full_url, array(
'method' => 'DELETE',
'blocking' => true,
'headers' => array('Authorization' => $arvan_api_key),
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
return "Something went wrong: $error_message";
}
}
}
/**
* Everything starts from here
*/
$arPluginCore = ArPluginCore::getInstance();