1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* The file that defines the core plugin class |
5
|
|
|
* |
6
|
|
|
* A class definition that includes attributes and functions used across both the |
7
|
|
|
* public-facing side of the site and the admin area. |
8
|
|
|
* |
9
|
|
|
* @link http://www.thinkovi.com |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @package Xcloner |
13
|
|
|
* @subpackage Xcloner/includes |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The core plugin class. |
18
|
|
|
* |
19
|
|
|
* This is used to define internationalization, admin-specific hooks, and |
20
|
|
|
* public-facing site hooks. |
21
|
|
|
* |
22
|
|
|
* Also maintains the unique identifier of this plugin as well as the current |
23
|
|
|
* version of the plugin. |
24
|
|
|
* |
25
|
|
|
* @since 1.0.0 |
26
|
|
|
* @package Xcloner |
27
|
|
|
* @subpackage Xcloner/includes |
28
|
|
|
* @author Liuta Ovidiu <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class Xcloner { |
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The loader that's responsible for maintaining and registering all hooks that power |
34
|
|
|
* the plugin. |
35
|
|
|
* |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
* @access protected |
38
|
|
|
* @var Xcloner_Loader $loader Maintains and registers all hooks for the plugin. |
39
|
|
|
*/ |
40
|
|
|
protected $loader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The unique identifier of this plugin. |
44
|
|
|
* |
45
|
|
|
* @since 1.0.0 |
46
|
|
|
* @access protected |
47
|
|
|
* @var string $plugin_name The string used to uniquely identify this plugin. |
48
|
|
|
*/ |
49
|
|
|
protected $plugin_name; |
50
|
|
|
|
51
|
|
|
protected $plugin_admin; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The current version of the plugin. |
55
|
|
|
* |
56
|
|
|
* @since 1.0.0 |
57
|
|
|
* @access protected |
58
|
|
|
* @var string $version The current version of the plugin. |
59
|
|
|
*/ |
60
|
|
|
protected $version; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Define the core functionality of the plugin. |
64
|
|
|
* |
65
|
|
|
* Set the plugin name and the plugin version that can be used throughout the plugin. |
66
|
|
|
* Load the dependencies, define the locale, and set the hooks for the admin area and |
67
|
|
|
* the public-facing side of the site. |
68
|
|
|
* |
69
|
|
|
* @since 1.0.0 |
70
|
|
|
*/ |
71
|
|
|
public function init() |
72
|
|
|
{ |
73
|
|
|
register_shutdown_function(array($this, 'exception_handler')); |
74
|
|
|
|
75
|
|
|
$this->plugin_name = 'xcloner'; |
76
|
|
|
$this->version = '4.0.1'; |
77
|
|
|
|
78
|
|
|
$this->load_dependencies(); |
79
|
|
|
$this->set_locale(); |
80
|
|
|
$this->define_admin_hooks(); |
81
|
|
|
$this->define_public_hooks(); |
82
|
|
|
|
83
|
|
|
$this->define_admin_menu(); |
84
|
|
|
$this->define_plugin_settings(); |
85
|
|
|
|
86
|
|
|
$this->define_ajax_hooks(); |
87
|
|
|
$this->define_cron_hooks(); |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function check_dependencies(){ |
92
|
|
|
|
93
|
|
|
$backup_storage_path = realpath(__DIR__.DS."..".DS."..".DS."..").DS."backups".DS; |
94
|
|
|
|
95
|
|
|
define("XCLONER_STORAGE_PATH", realpath($backup_storage_path)); |
96
|
|
|
|
97
|
|
View Code Duplication |
if(!is_dir($backup_storage_path)) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
if(!@mkdir($backup_storage_path)) |
100
|
|
|
{ |
101
|
|
|
$status = "error"; |
102
|
|
|
$message = sprintf(__("Unable to create the Backup Storage Location Folder %s . Please fix this before starting the backup process."), $backup_storage_path); |
103
|
|
|
$this->trigger_message($message, $status, $backup_storage_path); |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
View Code Duplication |
if(!is_writable($backup_storage_path)) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
$status = "error"; |
110
|
|
|
$message = sprintf(__("Unable to write to the Backup Storage Location Folder %s . Please fix this before starting the backup process."), $backup_storage_path); |
111
|
|
|
$this->trigger_message($message, $status, $backup_storage_path); |
112
|
|
|
|
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function trigger_message($message, $status = "error", $message_param1 = "", $message_param2 = "", $message_param3 = "") |
119
|
|
|
{ |
120
|
|
|
$message = sprintf(__($message), $message_param1, $message_param2, $message_param3); |
121
|
|
|
add_action( 'xcloner_admin_notices', array($this,"trigger_message_notice"), 10, 2); |
122
|
|
|
do_action( 'xcloner_admin_notices', $message, $status); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function trigger_message_notice($message, $status = "success") |
126
|
|
|
{ |
127
|
|
|
?> |
128
|
|
|
<div class="notice notice-<?php echo $status?> is-dismissible"> |
129
|
|
|
<p><?php _e( $message, 'xcloner-backup-and-restore' ); ?></p> |
130
|
|
|
</div> |
131
|
|
|
<?php |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Load the required dependencies for this plugin. |
136
|
|
|
* |
137
|
|
|
* Include the following files that make up the plugin: |
138
|
|
|
* |
139
|
|
|
* - Xcloner_Loader. Orchestrates the hooks of the plugin. |
140
|
|
|
* - Xcloner_i18n. Defines internationalization functionality. |
141
|
|
|
* - Xcloner_Admin. Defines all hooks for the admin area. |
142
|
|
|
* - Xcloner_Public. Defines all hooks for the public side of the site. |
143
|
|
|
* |
144
|
|
|
* Create an instance of the loader which will be used to register the hooks |
145
|
|
|
* with WordPress. |
146
|
|
|
* |
147
|
|
|
* @since 1.0.0 |
148
|
|
|
* @access private |
149
|
|
|
*/ |
150
|
|
|
private function load_dependencies() { |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* The class responsible for orchestrating the actions and filters of the |
154
|
|
|
* core plugin. |
155
|
|
|
*/ |
156
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-loader.php'; |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* The class responsible for defining internationalization functionality |
160
|
|
|
* of the plugin. |
161
|
|
|
*/ |
162
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-i18n.php'; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* The class responsible for defining all actions that occur in the admin area. |
166
|
|
|
*/ |
167
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-xcloner-admin.php'; |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* The class responsible for debugging XCloner. |
171
|
|
|
*/ |
172
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-logger.php'; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* The class responsible for defining the admin settings area. |
176
|
|
|
*/ |
177
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-settings.php'; |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* The class responsible for defining the Remote Storage settings area. |
181
|
|
|
*/ |
182
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-remote-storage.php'; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* The class responsible for implementing the database backup methods. |
186
|
|
|
*/ |
187
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-database.php'; |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* The class responsible for sanitization of users input. |
191
|
|
|
*/ |
192
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-sanitization.php'; |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* The class responsible for XCloner system requirements validation. |
196
|
|
|
*/ |
197
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-requirements.php'; |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* The class responsible for XCloner backup archive creation. |
201
|
|
|
*/ |
202
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-archive.php'; |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* The class responsible for XCloner API requests. |
206
|
|
|
*/ |
207
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-api.php'; |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* The class responsible for the XCloner File System methods. |
211
|
|
|
*/ |
212
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-file-system.php'; |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* The class responsible for the XCloner File Transfer methods. |
216
|
|
|
*/ |
217
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-file-transfer.php'; |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* The class responsible for the XCloner Scheduler methods. |
221
|
|
|
*/ |
222
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-xcloner-scheduler.php'; |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* The class responsible for defining all actions that occur in the public-facing |
226
|
|
|
* side of the site. |
227
|
|
|
*/ |
228
|
|
|
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-xcloner-public.php'; |
229
|
|
|
|
230
|
|
|
$this->loader = new Xcloner_Loader(); |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Define the locale for this plugin for internationalization. |
236
|
|
|
* |
237
|
|
|
* Uses the Xcloner_i18n class in order to set the domain and to register the hook |
238
|
|
|
* with WordPress. |
239
|
|
|
* |
240
|
|
|
* @since 1.0.0 |
241
|
|
|
* @access private |
242
|
|
|
*/ |
243
|
|
|
private function set_locale() { |
244
|
|
|
|
245
|
|
|
$plugin_i18n = new Xcloner_i18n(); |
246
|
|
|
|
247
|
|
|
$this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); |
248
|
|
|
|
249
|
|
|
//wp_localize_script( 'ajax-script', 'my_ajax_object', |
|
|
|
|
250
|
|
|
// array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) ); |
|
|
|
|
251
|
|
|
|
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Register all of the hooks related to the admin area functionality |
256
|
|
|
* of the plugin. |
257
|
|
|
* |
258
|
|
|
* @since 1.0.0 |
259
|
|
|
* @access private |
260
|
|
|
*/ |
261
|
|
|
private function define_admin_hooks() { |
262
|
|
|
|
263
|
|
|
$plugin_admin = new Xcloner_Admin( $this->get_plugin_name(), $this->get_version() ); |
264
|
|
|
$this->plugin_admin = $plugin_admin; |
265
|
|
|
|
266
|
|
|
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); |
267
|
|
|
$this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); |
268
|
|
|
|
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Register the Admin Sidebar menu |
273
|
|
|
* |
274
|
|
|
* @access private |
275
|
|
|
*/ |
276
|
|
|
private function define_admin_menu(){ |
277
|
|
|
|
278
|
|
|
add_action('admin_menu', array($this->loader, 'xcloner_backup_add_admin_menu')); |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
private function define_plugin_settings(){ |
283
|
|
|
/** |
284
|
|
|
* register wporg_settings_init to the admin_init action hook |
285
|
|
|
*/ |
286
|
|
|
$settings = new Xcloner_Settings(); |
287
|
|
|
add_action('admin_init', array($settings, 'settings_init')); |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Register all of the hooks related to the public-facing functionality |
292
|
|
|
* of the plugin. |
293
|
|
|
* |
294
|
|
|
* @since 1.0.0 |
295
|
|
|
* @access private |
296
|
|
|
*/ |
297
|
|
|
private function define_public_hooks() { |
298
|
|
|
|
299
|
|
|
$plugin_public = new Xcloner_Public( $this->get_plugin_name(), $this->get_version() ); |
300
|
|
|
|
301
|
|
|
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); |
302
|
|
|
$this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); |
303
|
|
|
|
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function exception_handler() { |
307
|
|
|
|
308
|
|
|
$logger = new XCloner_Logger("php_system"); |
309
|
|
|
$error = error_get_last(); |
310
|
|
|
|
311
|
|
|
if($error['type'] and $logger) |
|
|
|
|
312
|
|
|
{ |
313
|
|
|
$logger->info($this->friendly_error_type ($error['type']).": ".var_export($error, true)); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
function friendly_error_type($type) { |
|
|
|
|
319
|
|
|
static $levels=null; |
320
|
|
|
if ($levels===null) { |
321
|
|
|
$levels=[]; |
322
|
|
|
foreach (get_defined_constants() as $key=>$value) { |
323
|
|
|
if (strpos($key,'E_')!==0) {continue;} |
324
|
|
|
$levels[$value]= $key; //substr($key,2); |
|
|
|
|
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
return (isset($levels[$type]) ? $levels[$type] : "Error #{$type}"); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
private function define_ajax_hooks() |
331
|
|
|
{ |
332
|
|
|
//$plugin_public = new Xcloner_Public( $this->get_plugin_name(), $this->get_version() ); |
|
|
|
|
333
|
|
|
//$this->loader->add_action( 'wp_ajax_get_database_tables_action', $plugin_public, array('Xcloner_Api','get_database_tables_action') ); |
|
|
|
|
334
|
|
|
|
335
|
|
|
if(is_admin()) |
336
|
|
|
{ |
337
|
|
|
$xcloner_api = new Xcloner_Api(); |
338
|
|
|
|
339
|
|
|
add_action( 'wp_ajax_get_database_tables_action' , array($xcloner_api,'get_database_tables_action') ); |
340
|
|
|
add_action( 'wp_ajax_get_file_system_action' , array($xcloner_api,'get_file_system_action') ); |
341
|
|
|
add_action( 'wp_ajax_scan_filesystem' , array($xcloner_api,'scan_filesystem') ); |
342
|
|
|
add_action( 'wp_ajax_backup_database' , array($xcloner_api,'backup_database') ); |
343
|
|
|
add_action( 'wp_ajax_backup_files' , array($xcloner_api,'backup_files') ); |
344
|
|
|
add_action( 'wp_ajax_save_schedule' , array($xcloner_api,'save_schedule') ); |
345
|
|
|
add_action( 'wp_ajax_get_schedule_by_id' , array($xcloner_api,'get_schedule_by_id') ); |
346
|
|
|
add_action( 'wp_ajax_get_scheduler_list' , array($xcloner_api,'get_scheduler_list') ); |
347
|
|
|
add_action( 'wp_ajax_delete_schedule_by_id' , array($xcloner_api,'delete_schedule_by_id') ); |
348
|
|
|
add_action( 'wp_ajax_delete_backup_by_name' , array($xcloner_api,'delete_backup_by_name') ); |
349
|
|
|
add_action( 'wp_ajax_download_backup_by_name' , array($xcloner_api,'download_backup_by_name') ); |
350
|
|
|
add_action( 'wp_ajax_remote_storage_save_status' , array($xcloner_api,'remote_storage_save_status') ); |
351
|
|
|
add_action( 'wp_ajax_upload_backup_to_remote' , array($xcloner_api,'upload_backup_to_remote') ); |
352
|
|
|
add_action( 'wp_ajax_list_backup_files' , array($xcloner_api,'list_backup_files') ); |
353
|
|
|
add_action( 'wp_ajax_restore_upload_backup' , array($xcloner_api,'restore_upload_backup') ); |
354
|
|
|
add_action( 'wp_ajax_download_restore_script' , array($xcloner_api,'download_restore_script') ); |
355
|
|
|
add_action( 'admin_notices', array($this, 'xcloner_error_admin_notices' )); |
356
|
|
|
|
357
|
|
|
//if (is_admin()) { |
|
|
|
|
358
|
|
|
add_filter('plugin_action_links', array($this, 'add_plugin_action_links'), 10, 2); |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
function add_plugin_action_links($links, $file) { |
|
|
|
|
364
|
|
|
if ($file == plugin_basename(dirname(dirname(__FILE__)) . '/xcloner.php')) |
365
|
|
|
{ |
366
|
|
|
$links[] = '<a href="admin.php?page=xcloner_settings_page">'.__('Settings', 'xcloner-backup-and-restore').'</a>'; |
367
|
|
|
$links[] = '<a href="admin.php?page=xcloner_generate_backups_page">'.__('Generate Backup', 'xcloner-backup-and-restore').'</a>'; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
return $links; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
public function xcloner_error_admin_notices() { |
374
|
|
|
settings_errors( 'xcloner_error_message' ); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function define_cron_hooks() |
378
|
|
|
{ |
379
|
|
|
//registering new schedule intervals |
380
|
|
|
add_filter( 'cron_schedules', array($this, 'add_new_intervals')); |
381
|
|
|
|
382
|
|
|
|
383
|
|
|
$xcloner_scheduler = new Xcloner_Scheduler(); |
384
|
|
|
$xcloner_scheduler->update_wp_cron_hooks(); |
385
|
|
|
|
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/*public function xcloner_scheduler_callback($schedule_id) |
|
|
|
|
389
|
|
|
{ |
390
|
|
|
$cron = new Xcloner_Scheduler; |
391
|
|
|
|
392
|
|
|
$cron->run_schedule($schedule_id); |
393
|
|
|
}*/ |
394
|
|
|
|
395
|
|
|
function add_new_intervals($schedules) |
|
|
|
|
396
|
|
|
{ |
397
|
|
|
// add weekly and monthly intervals |
398
|
|
|
$schedules['weekly'] = array( |
399
|
|
|
'interval' => 604800, |
400
|
|
|
'display' => __('Once Weekly') |
401
|
|
|
); |
402
|
|
|
|
403
|
|
|
$schedules['monthly'] = array( |
404
|
|
|
'interval' => 2635200, |
405
|
|
|
'display' => __('Once a month') |
406
|
|
|
); |
407
|
|
|
|
408
|
|
|
return $schedules; |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Run the loader to execute all of the hooks with WordPress. |
414
|
|
|
* |
415
|
|
|
* @since 1.0.0 |
416
|
|
|
*/ |
417
|
|
|
public function run() { |
418
|
|
|
$this->loader->run(); |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* The name of the plugin used to uniquely identify it within the context of |
423
|
|
|
* WordPress and to define internationalization functionality. |
424
|
|
|
* |
425
|
|
|
* @since 1.0.0 |
426
|
|
|
* @return string The name of the plugin. |
427
|
|
|
*/ |
428
|
|
|
public function get_plugin_name() { |
429
|
|
|
return $this->plugin_name; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* The reference to the class that orchestrates the hooks with the plugin. |
434
|
|
|
* |
435
|
|
|
* @since 1.0.0 |
436
|
|
|
* @return Xcloner_Loader Orchestrates the hooks of the plugin. |
437
|
|
|
*/ |
438
|
|
|
public function get_loader() { |
439
|
|
|
return $this->loader; |
440
|
|
|
} |
441
|
|
|
|
442
|
|
|
/** |
443
|
|
|
* Retrieve the version number of the plugin. |
444
|
|
|
* |
445
|
|
|
* @since 1.0.0 |
446
|
|
|
* @return string The version number of the plugin. |
447
|
|
|
*/ |
448
|
|
|
public function get_version() { |
449
|
|
|
return $this->version; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function display($page) |
453
|
|
|
{ |
454
|
|
|
$plugin_admin = new Xcloner_Admin( $this->get_plugin_name(), $this->get_version() ); |
455
|
|
|
$this->plugin_admin = $plugin_admin; |
456
|
|
|
|
457
|
|
|
call_user_func_array(array($this->plugin_admin, $page), array()); |
458
|
|
|
} |
459
|
|
|
} |
460
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.