-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmainwp-client-reports-extension.php
555 lines (465 loc) · 16.6 KB
/
mainwp-client-reports-extension.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
<?php
/** MainWP Client Reports Extension */
/*
Plugin Name: MainWP Client Reports Extension
Plugin URI: https://mainwp.com
Description: MainWP Client Reports Extension allows you to generate activity reports for your clients sites. Requires MainWP Dashboard.
Version: 4.0.14
Author: MainWP
Author URI: https://mainwp.com
Documentation URI: https://mainwp.com/help/docs/category/mainwp-extensions/client-reports/
*/
if ( ! defined( 'MAINWP_CLIENT_REPORTS_PLUGIN_FILE' ) ) {
/**
* Define MAINWP_CLIENT_REPORTS_PLUGIN_FILE with MainWP client reports extension.
*/
define( 'MAINWP_CLIENT_REPORTS_PLUGIN_FILE', __FILE__ );
}
if ( ! defined( 'MAINWP_CLIENT_REPORTS_LOG_PRIORITY_NUMBER' ) ) {
/**
* Define MAINWP_CLIENT_REPORTS_LOG_PRIORITY_NUMBER with MainWP client reports extension.
*/
define( 'MAINWP_CLIENT_REPORTS_LOG_PRIORITY_NUMBER', 55 );
}
/**
* Class MainWP_CReport_Extension
*/
class MainWP_CReport_Extension {
/**
* Method instance()
*
* Create a public static instance.
*
* @return mixed Class instance.
*/
public static $instance = null;
/** @var string Hold the extension handle. */
public $plugin_handle = 'mainwp-wpcreport-extension';
/** @var string Hold the entension URL. */
public static $plugin_url;
/** @var string Holds the extension's slug. */
public $plugin_slug;
/** @var string Hold the extension's directory location. */
public $plugin_dir;
/** @var string Hold extension option. */
protected $option;
/** @var string Holds option handle. */
protected $option_handle = 'mainwp_wpcreport_extension';
/** @var string Extension version. */
public $version = '1.6';
/**
* Create a public static instance.
*
* @return mixed Class instance.
*
* @uses MainWP_CReport_Extension()
*/
static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new MainWP_CReport_Extension();
}
return self::$instance;
}
/**
* MainWP_CReport_Extension constructor.
*
* Run each time the class is called.
*
* @uses MainWP_CReport::hook_generate_report()
* @uses MainWP_CReport::hook_get_site_tokens()
* @uses MainWP_CReport::hook_generate_content()
* @uses MainWP_CReport_DB::install()
*/
public function __construct() {
$this->plugin_dir = plugin_dir_path( __FILE__ );
self::$plugin_url = plugin_dir_url( __FILE__ );
$this->plugin_slug = plugin_basename( __FILE__ );
$this->option = get_option( $this->option_handle );
add_action( 'init', array( &$this, 'localization' ) );
add_action( 'init', array( &$this, 'init' ) );
add_filter( 'plugin_row_meta', array( &$this, 'plugin_row_meta' ), 10, 2 );
add_action( 'admin_init', array( &$this, 'admin_init' ) );
add_filter( 'mainwp_sync_extensions_options', array( &$this, 'mainwp_sync_extensions_options' ), 10, 1 );
add_filter( 'mainwp_sync_others_data', array( $this, 'sync_others_data' ), 10, 2 );
add_action( 'mainwp_site_synced', array( &$this, 'site_synced' ), 10, 2 );
add_action( 'mainwp_delete_site', array( &$this, 'on_delete_site' ), 10, 1 );
add_filter( 'mainwp_log_specific_actions', array( $this, 'hook_log_specific_actions' ), 10, 2 );
// to fix action for wp cli.
add_action( 'mainwp_sucuri_scan_done', array( &$this, 'sucuri_scan_done' ), 10, 3 );
/**
* This hook allows you to generate report content via the 'mainwp_client_report_generate' filter.
*
* @see \MainWP_CReport::hook_generate_report();
*/
add_filter( 'mainwp_client_report_generate', array( 'MainWP_CReport', 'hook_generate_report' ), 10, 5 );
add_filter( 'mainwp_client_report_get_site_tokens', array( 'MainWP_CReport', 'hook_get_site_tokens' ), 10, 2 );
add_filter( 'mainwp_client_report_generate_content', array( 'MainWP_CReport', 'hook_generate_content' ), 10, 5 );
if ( isset( $_GET['page'] ) && ( 'Extensions-Mainwp-Client-Reports-Extension' == $_GET['page'] ) && isset( $_GET['tab'] ) && $_GET['tab'] == 'report' ) {
require_once 'includes/functions.php';
add_action( 'admin_print_footer_scripts', 'mainwp_creport_admin_print_footer_scripts' );
}
MainWP_CReport_DB::get_instance()->install();
add_filter( 'cron_schedules', array( $this, 'getCronSchedules' ) );
}
/**
* Initiate extension localization.
*/
public function localization() {
load_plugin_textdomain( 'mainwp-client-reports-extension', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Initiate MainWP Client reports instance.
*
* @uses MainWP_CReport()
*/
public function init() {
$mwp_creport = new MainWP_CReport();
$mwp_creport->init_cron();
}
/**
* Hook hook_log_specific_actions.
*
* Hook sites table sortable columns.
*
* @return mixed $sortable_columns.
*/
public function hook_log_specific_actions( $inputs ) {
$inputs[ MAINWP_CLIENT_REPORTS_LOG_PRIORITY_NUMBER ] = __( 'Client Reports', 'mainwp-client-reports-extension' );
return $inputs;
}
/**
* Plugin meta row.
*
* @param array $plugin_meta Plugin Meta data.
* @param string $plugin_file Plugin file.
*
* @return array Return plugin meta array.
*/
public function plugin_row_meta( $plugin_meta, $plugin_file ) {
if ( $this->plugin_slug != $plugin_file ) {
return $plugin_meta;
}
$slug = basename( $plugin_file, '.php' );
$api_data = get_option( $slug . '_APIManAdder' );
if ( ! is_array( $api_data ) || ! isset( $api_data['activated_key'] ) || $api_data['activated_key'] != 'Activated' || ! isset( $api_data['api_key'] ) || empty( $api_data['api_key'] ) ) {
return $plugin_meta;
}
$plugin_meta[] = '<a href="?do=checkUpgrade" title="Check for updates.">Check for updates now</a>';
return $plugin_meta;
}
/**
* Sync client reports data.
*
* @param array $data Client reports data.
* @param array $pWebsite Child Site data.
*
* @return array Return client reports data.
*/
public function sync_others_data( $data, $pWebsite = null ) {
if ( ! is_array( $data ) ) {
$data = array();
}
$data['syncClientReportData'] = 1;
return $data;
}
/**
* Sync Client reports data.
*
* @param array $website Child site data.
* @param array $information Information data.
*
* @uses MainWP_CReport_Stream::get_option()
* @uses MainWP_CReport_Stream::set_option()
*/
public function site_synced( $website, $information = array() ) {
$website_id = $website->id;
if ( is_array( $information ) && isset( $information['syncClientReportData'] ) && is_array( $information['syncClientReportData'] ) ) {
$data = $information['syncClientReportData'];
if ( isset( $data['firsttime_activated'] ) ) {
$creportSettings = MainWP_CReport_Stream::get_instance()->get_option( 'settings' );
if ( ! is_array( $creportSettings ) ) {
$creportSettings = array();
}
$creportSettings[ $website_id ]['first_time'] = $data['firsttime_activated'];
MainWP_CReport_Stream::get_instance()->set_option( 'settings', $creportSettings );
}
}
}
/**
* On delete site.
*
* @param array $website Child site array.
*
* @uses MainWP_CReport_DB::delete_group_report_content()
*/
public function on_delete_site( $website ) {
if ( $website ) {
MainWP_CReport_DB::get_instance()->delete_group_report_content( 0, $website->id );
}
}
/**
* Sucuri scan done.
*
* @param string $website_id Child Site ID.
* @param string $scan_status Scan status.
* @param array $data Return scan data.
*/
public function sucuri_scan_done( $website_id, $scan_status, $data ) {
$scan_result = array();
if ( is_array( $data ) ) {
$blacklisted = isset( $data['BLACKLIST']['WARN'] ) ? true : false;
$malware_exists = isset( $data['MALWARE']['WARN'] ) ? true : false;
$status = array();
if ( $blacklisted ) {
$status[] = __( 'Site Blacklisted', 'mainwp-client-reports-extension' ); }
if ( $malware_exists ) {
$status[] = __( 'Site With Warnings', 'mainwp-client-reports-extension' ); }
$scan_result['status'] = count( $status ) > 0 ? implode( ', ', $status ) : __( 'Verified Clear', 'mainwp-client-reports-extension' );
$scan_result['webtrust'] = $blacklisted ? __( 'Site Blacklisted', 'mainwp-client-reports-extension' ) : __( 'Trusted', 'mainwp-client-reports-extension' );
}
$scan_data = array(
'blacklisted' => $blacklisted,
'malware_exists' => $malware_exists,
);
// save results to child site stream.
$post_data = array(
'mwp_action' => 'save_sucuri_stream',
'result' => base64_encode( wp_json_encode( $scan_result ) ),
'scan_status' => $scan_status,
'scan_data' => base64_encode( wp_json_encode( $scan_data ) ),
);
/** @global object $mainWPCReportExtensionActivator Instance of MainWP CReports Extension Activator. */
global $mainWPCReportExtensionActivator;
apply_filters( 'mainwp_fetchurlauthed', $mainWPCReportExtensionActivator->get_child_file(), $mainWPCReportExtensionActivator->get_child_key(), $website_id, 'client_report', $post_data );
}
/**
* Initiate Admin page.
*
* @uses MainWP_CReport()
* @uses MainWP_CReport_Stream()
*/
public function admin_init() {
if ( isset( $_GET['page'] ) && ( 'Extensions-Mainwp-Client-Reports-Extension' == $_GET['page'] ) &&
isset( $_GET['action'] ) && ( 'savepdf' == $_GET['action'] )
&& isset( $_GET['_noncesave'] ) && wp_verify_nonce( $_REQUEST['_noncesave'], '_noncesave' )
&& isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) {
require_once $this->plugin_dir . '/libs/save-as-pdf.php';
exit();
}
wp_enqueue_style( 'mainwp-creport-extension', self::$plugin_url . 'css/mainwp-reporting.css', array(), $this->version );
wp_enqueue_script( 'mainwp-creport-extension', self::$plugin_url . 'js/mainwp-reporting.js', array(), $this->version );
wp_localize_script(
'mainwp-creport-extension',
'mainwp_clientreport_loc',
array(
'nonce' => wp_create_nonce( '_wpnonce_creport' ),
)
);
MainWP_CReport::get_instance();
$mwp_creport = new MainWP_CReport();
$mwp_creport->admin_init();
$mwp_creport_stream = new MainWP_CReport_Stream();
$mwp_creport_stream->admin_init();
}
/**
* Set cron schedules to once every 5 minuets.
*
* @param array $schedules Holds the Cron job schedules.
* @return array Return array of cron jobs schedules.
*/
public static function getCronSchedules( $schedules ) {
$schedules['5minutely'] = array(
'interval' => 5 * 60, // 5 minute in seconds
'display' => __( 'Once every 5 minutes', 'mainwp' ),
);
return $schedules;
}
/**
* Sync extensions options.
*
* @param array $values Option values array.
* @return array Return option values array.
*/
function mainwp_sync_extensions_options( $values = array() ) {
$values['mainwp-client-reports-extension'] = array(
'plugin_name' => 'MainWP Child Reports',
'plugin_slug' => 'mainwp-child-reports/mainwp-child-reports.php',
'no_setting' => true,
);
return $values;
}
/**
* Get option value by given key.
*
* @param string $key Option array key.
* @param string $default Default option key.
* @return string Return option string.
*/
public function get_option( $key, $default = '' ) {
if ( isset( $this->option[ $key ] ) ) {
return $this->option[ $key ];
}
return $default;
}
/**
* Set options by given key.
*
* @param string $key Option array key.
* @param string $value New option value.
* @return bool True if the value was updated, false otherwise.
*/
public function set_option( $key, $value ) {
$this->option[ $key ] = $value;
return update_option( $this->option_handle, $this->option );
}
}
/**
* Class MainWP_CReport_Extension_Activator.
*/
class MainWP_CReport_Extension_Activator {
/** @var bool Whether MainWP is active or not. */
protected $mainwpMainActivated = false;
/** @var bool Whether MainWP Child Plugin is activated. */
protected $childEnabled = false;
/** @var bool Whether or not there is a Child Key. */
protected $childKey = false;
/** @var string Holds the Child File. */
protected $childFile;
/** @var string Plugin handle. */
protected $plugin_handle = 'mainwp-client-reports-extension';
/** @var string MainWP extension name. */
protected $product_id = 'MainWP Client Reports Extension';
/** @var string MainWP extension version. */
protected $software_version = '4.0.13';
/**
* MainWP_CReport_Extension_Activator constructor.
*
* Run each time the class is called.
*/
public function __construct() {
$this->childFile = __FILE__;
spl_autoload_register( array( $this, 'autoload' ) );
register_activation_hook( __FILE__, array( $this, 'activate' ) );
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
add_filter( 'mainwp_getextensions', array( &$this, 'get_this_extension' ) );
$this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', false );
if ( $this->mainwpMainActivated !== false ) {
$this->activate_this_plugin();
} else {
add_action( 'mainwp_activated', array( &$this, 'activate_this_plugin' ) );
}
add_action( 'admin_notices', array( &$this, 'mainwp_error_notice' ) );
add_action( 'mainwp_cronload_action', array( $this, 'load_cron_actions' ) );
}
/**
* Class Autoloader.
*
* @param string $class_name Class name to load.
*/
function autoload( $class_name ) {
$allowedLoadingTypes = array( 'class' );
$class_name = str_replace( '_', '-', strtolower( $class_name ) );
foreach ( $allowedLoadingTypes as $allowedLoadingType ) {
$class_file = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . str_replace( basename( __FILE__ ), '', plugin_basename( __FILE__ ) ) . $allowedLoadingType . DIRECTORY_SEPARATOR . $class_name . '.' . $allowedLoadingType . '.php';
if ( file_exists( $class_file ) ) {
require_once $class_file;
}
}
}
/**
* Load cron actions.
*
* @uses MainWP_CReport::managesite_schedule_backup()
*/
function load_cron_actions() {
add_action( 'mainwp_managesite_schedule_backup', array( 'MainWP_CReport', 'managesite_schedule_backup' ), 10, 3 );
}
/**
* Get this extension array.
*
* @param array $pArray Hold the extension array.
*
* @return array Return this extension array.
*/
function get_this_extension( $pArray ) {
$pArray[] = array(
'plugin' => __FILE__,
'api' => $this->plugin_handle,
'mainwp' => true,
'callback' => array( &$this, 'settings' ),
'apiManager' => true,
);
return $pArray;
}
/**
* MainWP Client Report settings.
*
* @uses MainWP_CReport::render()
*/
function settings() {
do_action( 'mainwp_pageheader_extensions', __FILE__ );
MainWP_CReport::render();
do_action( 'mainwp_pagefooter_extensions', __FILE__ );
}
/**
* Activate MainWP Client Reports Plugin.
*
* @uses MainWP_CReport::renderClientReportsSiteTokens()
* @uses MainWP_CReport_Extension()
*/
function activate_this_plugin() {
$this->mainwpMainActivated = apply_filters( 'mainwp_activated_check', $this->mainwpMainActivated );
$this->childEnabled = apply_filters( 'mainwp_extension_enabled_check', __FILE__ );
$this->childKey = $this->childEnabled['key'];
if ( function_exists( 'mainwp_current_user_can' ) && ! mainwp_current_user_can( 'extension', 'mainwp-client-reports-extension' ) ) {
return;
}
add_action( 'mainwp_extension_sites_edit_tablerow', array( 'MainWP_CReport', 'renderClientReportsSiteTokens' ), 10, 1 ); // to do change to: mainwp-manage-sites-edit
new MainWP_CReport_Extension();
}
/**
* Get Child key.
*
* @return string|bool Return Child Key or FALSE on failure.
*/
public function get_child_key() {
return $this->childKey;
}
/**
* Get child file.
*
* @return string Return child file.
*/
public function get_child_file() {
return $this->childFile;
}
/**
* MainWP error notices.
*/
function mainwp_error_notice() {
/** @global string $current_screen Current page. */
global $current_screen;
if ( $current_screen->parent_base == 'plugins' && $this->mainwpMainActivated == false ) {
echo '<div class="error"><p>MainWP Client Reports Extension ' . __( 'requires <a href="http://mainwp.com/" target="_blank">MainWP Dashboard Plugin</a> to be activated in order to work. Please install and activate <a href="http://mainwp.com/" target="_blank">MainWP Dashboard Plugin</a> first.' ) . '</p></div>';
}
}
/**
* Activate MainWP Extension.
*/
public function activate() {
$options = array(
'product_id' => $this->product_id,
'software_version' => $this->software_version,
);
do_action( 'mainwp_activate_extention', $this->plugin_handle, $options );
}
/**
* Deactivate MainWP Extension.
*/
public function deactivate() {
do_action( 'mainwp_deactivate_extention', $this->plugin_handle );
}
}
/** @global object $mainWPCReportExtensionActivator Extension activator class instance. */
global $mainWPCReportExtensionActivator;
$mainWPCReportExtensionActivator = new MainWP_CReport_Extension_Activator();