PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.1.8
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.1.8
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / class / class-mainwp-system.php

class-mainwp-system.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 6.1.8, at class/class-mainwp-system.php

1,591 lines 66.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP System.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 use MainWP\Dashboard\Module\Log\Log_Manage_Insights_Events_Page;
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
18 // phpcs:disable plugin_updater_detected -- not a self-updater; injects update data exclusively for licensed MainWP premium extensions hosted on mainwp.com.
19
20 const MAINWP_VIEW_PER_SITE = 1;
21 const MAINWP_VIEW_PER_PLUGIN_THEME = 0;
22 const MAINWP_VIEW_PER_GROUP = 2;
23
24 // phpcs:disable WordPress.WP.AlternativeFunctions -- for custom read/write file.
25
26 /**
27 * Class MainWP_System
28 *
29 * @package MainWP\Dashboard
30 */
31 class MainWP_System { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
32
33 /**
34 * Public static variable to hold the current plugin version.
35 *
36 * @var string Current plugin version.
37 */
38 public static $version = '6.1.8'; // NOSONAR.
39
40 /**
41 * Private static variable to hold the single instance of the class.
42 *
43 * @var mixed Default null
44 */
45 private static $instance = null;
46
47 /**
48 * Public variable to hold the Metaboxes instance.
49 *
50 * @var object Metaboxes.
51 */
52 public $metaboxes;
53
54 /**
55 * Private variable to hold the current version.
56 *
57 * @var string The plugin current version.
58 */
59 private $current_version = null;
60
61
62 /**
63 * Private variable to hold the check update version number.
64 *
65 * @var string The plugin current update version.
66 */
67 private $check_ver_update = '0.0.5';
68
69 /**
70 * Private variable to hold the plugin slug (mainwp/mainwp.php)
71 *
72 * @var string Plugin slug.
73 */
74 private $plugin_slug;
75
76
77 /**
78 * Private variable to hold the defer js handle.
79 *
80 * @var array Defer js handle.
81 */
82 private static $defer_js_handle = array();
83
84
85 /**
86 * Method instance()
87 *
88 * Create a public static instance.
89 *
90 * @static
91 * @return MainWP_System
92 */
93 public static function instance() {
94 return static::$instance;
95 }
96
97 /**
98 * MainWP_System constructor.
99 *
100 * Runs any time class is called.
101 *
102 * @param string $mainwp_plugin_file Plugn slug.
103 *
104 * @uses \MainWP\Dashboard\MainWP_Bulk_Post
105 * @uses \MainWP\Dashboard\MainWP_Hooks
106 * @uses \MainWP\Dashboard\MainWP_Menu::get_class_name()
107 * @uses \MainWP\Dashboard\MainWP_Menu
108 * @uses \MainWP\Dashboard\MainWP_Meta_Boxes
109 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::init_cron_jobs()
110 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs
111 * @uses \MainWP\Dashboard\MainWP_System_Handler
112 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_class_name()
113 * @uses \MainWP\Dashboard\MainWP_System_View::get_class_name()
114 * @uses \MainWP\Dashboard\MainWP_UI::get_class_name()
115 * @uses \MainWP\Dashboard\MainWP_WP_CLI_Command::init()
116 * @uses \MainWP\Dashboard\MainWP_Updates_Overview::init()
117 * @uses \MainWP\Dashboard\MainWP_Extensions::init()
118 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_class_name()
119 * @uses \MainWP\Dashboard\MainWP_Install_Bulk::init()
120 * @uses \MainWP\Dashboard\MainWP_Manage_Backups::init()
121 * @uses \MainWP\Dashboard\MainWP_Manage_Sites::init()
122 * @uses \MainWP\Dashboard\MainWP_Overview::get()
123 * @uses \MainWP\Dashboard\MainWP_Page::init()
124 * @uses \MainWP\Dashboard\MainWP_Plugins::init()
125 * @uses \MainWP\Dashboard\MainWP_Post::init()
126 * @uses \MainWP\Dashboard\MainWP_Settings::init()
127 * @uses \MainWP\Dashboard\MainWP_Themes::init()
128 * @uses \MainWP\Dashboard\MainWP_Updates::init()
129 * @uses \MainWP\Dashboard\MainWP_User::init()
130 * @uses \MainWP\Dashboard\MainWP_Updates::init()
131 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
132 */
133 public function __construct( $mainwp_plugin_file ) { //phpcs:ignore -- NOSONAR - complex.
134 static::$instance = $this;
135
136 MainWP_Execution_Helper::instance()->init_exec_time();
137
138 $includer = new MainWP_Includes();
139 $includer->includes();
140
141 MainWP_Keys_Manager::auto_load_files();
142 MainWP_Keys_Manager::register_migration_hooks();
143
144 $this->load_all_options();
145
146 $this->update_install();
147 $this->plugin_slug = plugin_basename( $mainwp_plugin_file );
148
149 add_action( 'shutdown', array( $this, 'hook_wp_shutdown' ) );
150
151 do_action( 'mainwp_system_init' );
152
153 if ( is_admin() ) {
154 include_once ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'plugin.php'; // NOSONAR - WP compatible.
155 $pluginData = get_plugin_data( $mainwp_plugin_file, true, false ); // to fix issue of user language setting.
156 $this->current_version = $pluginData['Version'];
157 $currentVersion = get_option( 'mainwp_plugin_version' );
158
159 if ( ! empty( $currentVersion ) && version_compare( $currentVersion, '4.0', '<' ) && version_compare( $this->current_version, '4.0', '>=' ) ) {
160 add_action( 'mainwp_after_header', array( MainWP_System_View::get_class_name(), 'mainwp_4_update_notice' ) );
161 }
162
163 if ( ! empty( $currentVersion ) && version_compare( $currentVersion, '5.0', '<' ) && version_compare( $this->current_version, '5.0', '>=' ) ) {
164 add_action( 'mainwp_after_header', array( MainWP_System_View::get_class_name(), 'mainwp_ver5_update_notice' ) );
165 }
166
167 if ( ! empty( $currentVersion ) && version_compare( $currentVersion, '5.0.2', '<' ) && version_compare( $this->current_version, '5.0.2', '>=' ) ) {
168 add_action( 'mainwp_after_header', array( MainWP_System_View::get_class_name(), 'mainwp_ver502_update_notice' ) );
169 }
170
171 // Use 6.0.12 as the lower bound so 6.1-er.x builds trigger this notice.
172 if ( ! empty( $currentVersion ) && version_compare( $currentVersion, '6.1', '<' ) && version_compare( $currentVersion, '6.0.12', '>' ) ) {
173 add_action( 'mainwp_after_header', array( MainWP_System_View::get_class_name(), 'mainwp_ver61_update_notice' ) );
174 }
175
176 MainWP_Utility::update_option( 'mainwp_plugin_version', $this->current_version );
177 }
178
179 if ( ! defined( 'MAINWP_VERSION' ) ) {
180
181 /**
182 * Defines MainWP Version.
183 *
184 * @const ( string )
185 * @source https://code-reference.mainwp.com/classes/MainWP.Dashboard.MainWP_System.html
186 */
187 define( 'MAINWP_VERSION', $this->current_version );
188 }
189
190 if ( get_option( 'mainwp_setting_demo_mode_enabled' ) && ! defined( 'MAINWP_DEMO_MODE' ) ) {
191 define( 'MAINWP_DEMO_MODE', true );
192 }
193
194 $ssl_api_verifyhost = ( ( get_option( 'mainwp_api_sslVerifyCertificate' ) === false ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
195 if ( empty( $ssl_api_verifyhost ) ) {
196 add_filter(
197 'http_request_args',
198 array(
199 MainWP_Extensions_Handler::get_class_name(),
200 'no_ssl_filter_extension_upgrade',
201 ),
202 99,
203 2
204 );
205 }
206
207 MainWP_Extensions::init();
208 MainWP_Extensions_Groups::init();
209
210 $systemHandler = MainWP_System_Handler::instance();
211
212 add_action( 'init', array( &$this, 'localization' ) );
213 add_filter( 'site_transient_update_plugins', array( $systemHandler, 'check_update_custom' ) ); // phpcs:ignore PluginCheck.CodeAnalysis.Sniffs.UpdatingPluginTransientFound -- not a self-updater; injects update data exclusively for licensed MainWP premium extensions (hosted on mainwp.com), not for the Dashboard plugin itself.
214 add_filter( 'pre_set_site_transient_update_plugins', array( $systemHandler, 'pre_check_update_custom' ) ); // phpcs:ignore PluginCheck.CodeAnalysis.Sniffs.UpdatingPluginTransientFound -- same as above; pre-populates update info for MainWP premium extensions before WordPress saves the transient.
215 add_filter( 'plugins_api', array( $systemHandler, 'plugins_api_extension_info' ), 10, 3 );
216 add_filter( 'plugins_api_result', array( $systemHandler, 'plugins_api_wp_plugins_api_result' ), 10, 3 );
217 add_filter( 'upgrader_pre_download', array( $systemHandler, 'hook_refresh_trusted_extension_package_url' ), 10, 4 );
218
219 $this->metaboxes = new MainWP_Meta_Boxes();
220
221 MainWP_Overview::get();
222 MainWP_Client_Overview::instance();
223
224 MainWP_Manage_Sites::init();
225 MainWP_Uptime_Monitoring_Handle::instance();
226
227 MainWP_Hooks::get_instance();
228 MainWP_Menu::get_instance();
229
230 add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ), 9 );
231 add_action( 'admin_menu', array( MainWP_Menu::get_class_name(), 'init_mainwp_menus' ) );
232 add_filter( 'admin_footer', array( &$this, 'admin_footer' ), 15 );
233 add_action( 'admin_head', array( MainWP_System_View::get_class_name(), 'admin_head' ) );
234 add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_styles' ) );
235 add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts' ) );
236 add_action( 'admin_enqueue_scripts', array( &$this, 'admin_enqueue_scripts_fix_conflict' ), 9 );
237 add_action( 'admin_body_class', array( MainWP_System_View::get_class_name(), 'admin_body_class' ) );
238
239 MainWP_Bulk_Post::get_instance();
240
241 add_action( 'admin_init', array( &$this, 'admin_init' ), 20 );
242 add_action( 'admin_init', array( $this, 'hook_admin_update_check' ) );
243 add_action( 'after_setup_theme', array( &$this, 'after_setup_theme' ) );
244
245 add_action( 'init', array( &$this, 'parse_init' ) );
246 add_action( 'init', array( &$this, 'init_jobs' ) );
247 add_action( 'init', array( &$this, 'init' ), 9999 );
248
249 add_action( 'admin_init', array( $this, 'admin_redirects' ) );
250 add_action( 'current_screen', array( &$this, 'current_screen_redirects' ), 15 );
251 add_filter( 'plugin_action_links', array( $this, 'hook_plugin_action_links' ), 10, 4 );
252 add_action( 'admin_menu', array( &$this, 'admin_menu' ) );
253 add_action( 'admin_print_styles', array( MainWP_System_View::get_class_name(), 'admin_print_styles' ) );
254
255 add_action( 'wp_logout', array( &$this, 'clear_sessions' ) );
256
257 MainWP_Install_Bulk::init();
258
259 add_action( 'mainwp_after_header', array( MainWP_System_View::get_class_name(), 'admin_notices' ) );
260 add_action( 'admin_notices', array( MainWP_System_View::get_class_name(), 'wp_admin_notices' ) );
261 add_action( 'wp_mail_failed', array( &$this, 'wp_mail_failed' ) );
262
263 add_action( 'after_plugin_row', array( MainWP_System_View::get_class_name(), 'after_extensions_plugin_row' ), 10, 3 );
264
265 add_filter( 'mainwp-activated-check', array( &$this, 'activated_check' ) ); // @deprecated Use 'mainwp_activated_check' instead.
266 add_filter( 'mainwp_activated_check', array( &$this, 'activated_check' ) );
267
268 do_action_deprecated( 'mainwp-activated', array(), '4.0.7.2', 'mainwp_activated' ); // @deprecated Use 'mainwp_activated' instead. NOSONAR - not IP.
269
270 /**
271 * Action: mainwp_activated
272 *
273 * Fires upon MainWP plugin activation.
274 *
275 * @since Unknown
276 */
277 do_action( 'mainwp_activated' );
278
279 MainWP_Updates::init();
280 MainWP_Post::init();
281 MainWP_Settings::init();
282 if ( get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
283 MainWP_Manage_Backups::init();
284 }
285 MainWP_Manage_Groups::init();
286 MainWP_User::init();
287 MainWP_Password_Policy_Settings::init();
288 MainWP_Page::init();
289 MainWP_Themes::init();
290 MainWP_Plugins::init();
291 MainWP_Updates_Overview::init();
292 MainWP_Client::init();
293 MainWP_Rest_Api_Page::init();
294 MainWP_Logger::instance();
295
296 if ( class_exists( '\MainWP\Dashboard\Module\Log\Log_Manage_Insights_Events_Page' ) ) {
297 Log_Manage_Insights_Events_Page::instance();
298 }
299
300 MainWP_Uptime_Monitoring_Schedule::instance();
301
302 if ( defined( 'WP_CLI' ) && WP_CLI ) {
303 MainWP_WP_CLI_Command::init();
304 }
305 MainWP_Unhooks_Helper::instance();
306 MainWP_Cache_Warm_Helper::instance();
307
308 add_action(
309 'wp_ajax_nopriv_mainwp_self_connect',
310 array( MainWP_Post_Handler::instance(), 'ajax_self_connect' )
311 );
312 }
313
314 /**
315 * Method get_dashboard_version()
316 *
317 * Get dashboard version.
318 *
319 * @return string $version Version.
320 */
321 public function get_dashboard_version() {
322 return static::$version;
323 }
324
325 /**
326 * Method get_mainwp_version()
327 *
328 * Get dashboard version.
329 *
330 * @return string $version Version.
331 */
332 public static function get_mainwp_version() { //phpcs:ignore -- NOSONAR same function body.
333 return static::$version;
334 }
335
336 /**
337 * Method load_all_options()
338 *
339 * Load all wp_options data.
340 *
341 * @return array $alloptions Array of all options.
342 */
343 public function load_all_options() { //phpcs:ignore -- NOSONAR - complex.
344
345 /**
346 * WordPress Database instance.
347 *
348 * @global object $wpdb
349 */
350 global $wpdb;
351
352 if ( ! defined( 'WP_INSTALLING' ) || ! is_multisite() ) {
353 $alloptions = wp_cache_get( 'alloptions', 'options' );
354 } else {
355 $alloptions = false;
356 }
357
358 if ( ! defined( 'WP_INSTALLING' ) || ! is_multisite() ) {
359 $notoptions = wp_cache_get( 'notoptions', 'options' );
360 } else {
361 $notoptions = false;
362 }
363
364 if ( ! isset( $alloptions['mainwp_db_version'] ) ) {
365 $suppress = $wpdb->suppress_errors();
366 $options = array(
367 'mainwp_db_version',
368 'mainwp_plugin_version',
369 'mainwp_upgradeVersionInfo',
370 'mainwp_extensions',
371 'mainwp_activated',
372 'mainwp_api_sslVerifyCertificate',
373 'mainwp_automaticDailyUpdate',
374 'mainwp_pluginAutomaticDailyUpdate',
375 'mainwp_themeAutomaticDailyUpdate',
376 'mainwp_backup_before_upgrade',
377 'mainwp_enableLegacyBackupFeature',
378 'mainwp_maximumInstallUpdateRequests',
379 'mainwp_maximumSyncRequests',
380 'mainwp_primaryBackup',
381 'mainwp_use_favicon',
382 'mainwp_wp_cron',
383 'mainwp_timeDailyUpdate',
384 'mainwp_frequencyDailyUpdate',
385 'mainwp_delay_autoupdate',
386 'mainwp_wpcreport_extension',
387 'mainwp_daily_digest_plain_text',
388 'mainwp_hide_update_everything',
389 'mainwp_disable_update_confirmations',
390 'mainwp_settings_show_widgets',
391 'mainwp_clients_show_widgets',
392 'mainwp_settings_show_manage_sites_columns',
393 'mainwp_individual_uptime_monitoring_schedule_enabled',
394 'mainwp_disableSitesHealthMonitoring',
395 'mainwp_sitehealthThreshold',
396 'mainwp_check_http_response',
397 'mainwp_extmenu',
398 'mainwp_opensslLibLocation',
399 'mainwp_notice_wp_mail_failed',
400 'mainwp_show_language_updates',
401 'mainwp_logger_check_daily',
402 'mainwp_site_actions_notification_enable',
403 'mainwp_update_check_version',
404 'mainwp_setting_demo_mode_enabled',
405 'mainwp_log_wait_lasttime',
406 'mainwp_cron_license_deactivated_alert_lasttime',
407 'mainwp_updatescheck_is_running',
408 'mainwp_automatic_updates_is_running',
409 'mainwp_frequency_AutoUpdate',
410 'mainwp_batch_updates_is_running',
411 'mainwp_batch_individual_updates_is_running',
412 'mainwp_maximum_uptime_monitoring_requests',
413 'mainwp_actionlogs',
414 'mainwp_process_uptime_notification_run_status',
415 'mainwp_warm_cache_pages_ttl',
416 'mainwp_module_log_settings_logs_selection_data',
417 );
418
419 $options = apply_filters( 'mainwp_init_load_all_options', $options );
420
421 $query = "SELECT option_name, option_value FROM $wpdb->options WHERE option_name in (";
422 foreach ( $options as $option ) {
423 $query .= "'" . $option . "', ";
424 }
425 $query = substr( $query, 0, strlen( $query ) - 2 );
426 $query .= ")"; // phpcs:ignore -- ignore double quotes auto-correction.
427 $alloptions_db = $wpdb->get_results( $query ); // phpcs:ignore -- unprepared SQL ok.
428 $wpdb->suppress_errors( $suppress );
429 if ( ! is_array( $alloptions ) ) {
430 $alloptions = array();
431 }
432 if ( is_array( $alloptions_db ) ) {
433 foreach ( (array) $alloptions_db as $o ) {
434 $alloptions[ $o->option_name ] = $o->option_value;
435 unset( $options[ array_search( $o->option_name, $options ) ] );
436 }
437 if ( ! is_array( $notoptions ) ) {
438 $notoptions = array();
439 }
440 foreach ( $options as $option ) {
441 $notoptions[ $option ] = true;
442 }
443 if ( ! defined( 'WP_INSTALLING' ) || ! is_multisite() ) {
444 wp_cache_set( 'alloptions', $alloptions, 'options' );
445 wp_cache_set( 'notoptions', $notoptions, 'options' );
446 }
447 }
448 }
449
450 return $alloptions;
451 }
452
453 /**
454 * Method localization()
455 *
456 * Loads plugin language files.
457 */
458 public function localization() {
459 $load = apply_filters( 'mainwp_load_text_domain', true );
460 if ( $load ) {
461 load_plugin_textdomain( 'mainwp', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' ); // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound -- kept for older WP versions (<4.6) and to support local/custom translation files loaded from the plugin's languages directory.
462 }
463 }
464
465
466 /**
467 * Method wp_mail_failed()
468 *
469 * Check if there has been a wp mail failer.
470 *
471 * @param string $error Array of error messages.
472 *
473 * @uses \MainWP\Dashboard\MainWP_Logger::debug()
474 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
475 */
476 public function wp_mail_failed( $error ) {
477 $mail_failed = get_option( 'mainwp_notice_wp_mail_failed' );
478 if ( is_object( $error ) && empty( $mail_failed ) ) {
479 MainWP_Utility::update_option( 'mainwp_notice_wp_mail_failed', 'yes' );
480 $er = $error->get_error_message();
481 if ( ! empty( $er ) ) {
482 MainWP_Logger::instance()->debug( 'Error :: wp_mail :: [error=' . $er . ']' );
483 }
484 }
485 }
486
487 /**
488 * Method get_version()
489 *
490 * Get current plugin version.
491 *
492 * @return string Current plugin version.
493 */
494 public function get_version() {
495 return $this->current_version;
496 }
497
498 /**
499 * Method mainwp_cronpingchilds_action()
500 *
501 * Run cron ping child's action.
502 *
503 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::cron_ping_childs()
504 */
505 public function mainwp_cronpingchilds_action() {
506 MainWP_System_Cron_Jobs::instance()->cron_ping_childs();
507 }
508
509 /**
510 * Method mainwp_cronbackups_continue_action()
511 *
512 * Run cron backups continue action.
513 *
514 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::cron_backups_continue()
515 */
516 public function mainwp_cronbackups_continue_action() {
517 MainWP_System_Cron_Jobs::instance()->cron_backups_continue();
518 }
519
520 /**
521 * Method mainwp_cronbackups_action()
522 *
523 * Run cron backups action.
524 *
525 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::cron_backups()
526 */
527 public function mainwp_cronbackups_action() {
528 MainWP_System_Cron_Jobs::instance()->cron_backups();
529 }
530
531 /**
532 * Method mainwp_cronreconnect_action()
533 *
534 * Run cron stats action.
535 *
536 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::cron_reconnect()
537 */
538 public function mainwp_cronreconnect_action() {
539 MainWP_System_Cron_Jobs::instance()->cron_reconnect();
540 }
541
542 /**
543 * Method mainwp_cronupdatescheck_action()
544 *
545 * Run cron updates check action.
546 *
547 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::cron_updates_check()
548 */
549 public function mainwp_cronupdatescheck_action() {
550 MainWP_System_Cron_Jobs::instance()->cron_updates_check();
551 }
552
553 /**
554 * Method mainwp_cron_uptime_monitoring_check_action()
555 *
556 * Run cron uptime monitoring check action.
557 */
558 public function mainwp_cron_uptime_monitoring_check_action() {
559 MainWP_Uptime_Monitoring_Schedule::instance()->cron_uptime_check();
560 }
561
562 /**
563 * Method mainwp_cron_perform_general_schedules_action()
564 *
565 * Run cron uptime monitoring check action.
566 */
567 public function mainwp_cron_perform_general_schedules_action() {
568 MainWP_System_Cron_Jobs::instance()->cron_perform_general_process();
569 }
570
571
572 /**
573 * Method mainwp_crondeactivatedlicensesalert_action()
574 *
575 * Run cron activated licenses alert action.
576 */
577 public function mainwp_crondeactivatedlicensesalert_action() {
578 MainWP_System_Cron_Jobs::instance()->cron_deactivated_licenses_alert();
579 }
580
581 /**
582 * Method mainwp_cronchecksitehealth_action()
583 *
584 * Run cron check sites health action.
585 *
586 * @uses \MainWP\Dashboard\MainWP_System_Cron_Jobs::cron_check_websites_health()
587 */
588 public function mainwp_cronchecksitehealth_action() {
589 MainWP_System_Cron_Jobs::instance()->cron_check_websites_health();
590 }
591
592 /**
593 * Method is_mainwp_pages()
594 *
595 * Get the current page and check it for "mainwp_".
596 *
597 * @return boolean ture|false.
598 */
599 public static function is_mainwp_pages() {
600 $screen = get_current_screen();
601 if ( $screen && strpos( $screen->base, 'mainwp_' ) !== false && strpos( $screen->base, 'mainwp_child_tab' ) === false ) {
602 return true;
603 }
604
605 return false;
606 }
607
608 /**
609 * Method is_mainwp_site_page()
610 *
611 * Checks if the current page is under the site mode.
612 *
613 * @return boolean ture|false.
614 */
615 public static function is_mainwp_site_page() {
616 $is_page = false;
617 //phpcs:disable WordPress.Security.NonceVerification.Recommended
618 if ( isset( $_GET['page'] ) && 'CostTrackerAdd' !== $_GET['page'] && ( ( ( isset( $_GET['id'] ) && ! empty( $_GET['id'] ) ) || ( isset( $_GET['dashboard'] ) && ! empty( $_GET['dashboard'] ) ) || ( isset( $_GET['updateid'] ) && ! empty( $_GET['updateid'] ) ) || ( isset( $_GET['monitor_wpid'] ) && ! empty( $_GET['monitor_wpid'] ) ) || ( isset( $_GET['emailsettingsid'] ) && ! empty( $_GET['emailsettingsid'] ) ) || ( isset( $_GET['scanid'] ) && ! empty( $_GET['scanid'] ) ) ) || ( 'ServerInformation' === $_GET['page'] || 'ServerInformationCron' === $_GET['page'] || 'ErrorLog' === $_GET['page'] || 'ActionLogs' === $_GET['page'] || 'PluginPrivacy' === $_GET['page'] || 'Settings' === $_GET['page'] || 'SettingsAdvanced' === $_GET['page'] || 'SettingsEmail' === $_GET['page'] || 'MainWPTools' === $_GET['page'] || 'SettingsInsights' === $_GET['page'] || 'SettingsApiBackups' === $_GET['page'] || 'MonitoringSettings' === $_GET['page'] ) ) ) {
619 $is_page = true;
620 }
621 //phpcs:enable
622
623 /**
624 * Hook mainwp_is_mainwp_site_page.
625 *
626 * @since 5.4.1
627 */
628 return apply_filters( 'mainwp_is_mainwp_site_page', $is_page );
629 }
630
631
632 /**
633 * Method plugins_loaded()
634 *
635 * Load plugin text domain.
636 */
637 public function plugins_loaded() { // phpcs:ignore -- NOSONAR - complex.
638 /**
639 * Action: mainwp_system_plugins_loaded
640 *
641 * Fires after all plugins are loaded.
642 *
643 * @since 6.1.0
644 */
645 do_action( 'mainwp_system_plugins_loaded' );
646
647 if ( ! function_exists( 'MainWP\Dashboard\mainwp_current_user_have_right' ) ) {
648
649 /**
650 * Method \mainwp_current_user_can()
651 *
652 * Check permission level by hook mainwp_currentusercan of Team Control extension.
653 *
654 * @param string $cap_type group or type of capabilities.
655 * @param string $cap capabilities for current user.
656 *
657 * @return bool true|false
658 *
659 * @uses \MainWP\Dashboard\MainWP_System_Handler::handle_settings_post()
660 */
661 function mainwp_current_user_have_right( $cap_type = '', $cap = '' ) {
662
663 /**
664 * Current user global.
665 *
666 * @global string
667 */
668 global $current_user;
669
670 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
671 return true;
672 }
673
674 if ( defined( 'WP_CLI' ) && WP_CLI ) {
675 return true;
676 }
677
678 if ( defined( 'MAINWP_REST_API_DOING' ) && MAINWP_REST_API_DOING ) {
679 return true;
680 }
681
682 if ( empty( $current_user ) && ! function_exists( 'wp_get_current_user' ) ) {
683 require_once ABSPATH . WPINC . '/pluggable.php'; // NOSONAR - WP compatible.
684 }
685
686 return apply_filters( 'mainwp_currentusercan', true, $cap_type, $cap );
687 }
688 }
689 }
690
691 /**
692 * Method init()
693 *
694 * Instantiate Plugin.
695 */
696 public function init() { //phpcs:ignore -- NOSONAR - complex.
697
698 /**
699 * MainWP disabled menu items array.
700 *
701 * @global object $_mainwp_disable_menus_items
702 */
703 global $_mainwp_disable_menus_items;
704
705 $_mainwp_disable_menus_items = apply_filters( 'mainwp_disablemenuitems', $_mainwp_disable_menus_items );
706
707 /**
708 * Filter: mainwp_main_menu_disable_menu_items
709 *
710 * Filters disabled MainWP navigation items.
711 *
712 * @since Unknown
713 */
714 $_mainwp_disable_menus_items = apply_filters( 'mainwp_main_menu_disable_menu_items', $_mainwp_disable_menus_items );
715
716 MainWP_System_Handler::instance()->handle_settings_post();
717 }
718
719 /**
720 * Method parse_init()
721 *
722 * Initiate plugin installation & then run the Quick Setup Wizard.
723 *
724 * @uses \MainWP\Dashboard\MainWP_System_Handler::upload_file()
725 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_mainwp_dir()
726 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
727 * @uses \MainWP\Dashboard\MainWP_System_Utility::is_admin()
728 * @uses \MainWP\Dashboard\MainWP_Setup_Wizard()
729 */
730 public function parse_init() {
731 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
732 if ( isset( $_GET['mwpdl'] ) && isset( $_GET['sig'] ) ) {
733 $mwpDir = MainWP_System_Utility::get_mainwp_dir();
734 $mwpDir = $mwpDir[0];
735 $mwpdl = isset( $_REQUEST['mwpdl'] ) ? wp_unslash( $_REQUEST['mwpdl'] ) : '';
736 $file = trailingslashit( $mwpDir ) . rawurldecode( $mwpdl );
737
738 if ( stristr( rawurldecode( $mwpdl ), '..' ) ) {
739 return;
740 }
741
742 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
743
744 /**
745 * WordPress files system object.
746 *
747 * @global object
748 */
749 global $wp_filesystem;
750 $sig_value = wp_unslash( $_GET['sig'] );
751 $valid_sig = MainWP_System_Utility::valid_download_sig( $file, $sig_value );
752
753 if ( ! $valid_sig ) {
754 MainWP_Logger::instance()->debug( ' :: download :: invalid sig :: ' . base64_decode( $sig_value ) ); // phpcs:ignore -- for debug logging.
755 }
756
757 if ( $hasWPFileSystem && $wp_filesystem->exists( $file ) && $valid_sig ) {
758 MainWP_System_Handler::instance()->upload_file( $file );
759 exit();
760 }
761 } elseif ( isset( $_GET['page'] ) ) {
762 if ( MainWP_System_Utility::is_admin() && 'mainwp-setup' === $_GET['page'] ) {
763 MainWP_Setup_Wizard::get_instance();
764 }
765 }
766 // phpcs:enable
767 }
768
769 /**
770 * Method init_jobs()
771 *
772 * Initiate mainwp schedule jobs.
773 */
774 public function init_jobs() {
775 MainWP_System_Cron_Jobs::instance()->init_cron_jobs();
776 }
777
778
779 /**
780 * Method after_setup_theme()
781 *
782 * After setup theme hook, to support post thumbnails.
783 */
784 public function after_setup_theme() {
785 add_theme_support( 'post-thumbnails' );
786 }
787
788
789 /**
790 * Method hook_admin_update_check()
791 */
792 public function hook_admin_update_check() {
793 $current_ver = $this->check_ver_update;
794 $saved_ver = get_option( 'mainwp_update_check_version', false );
795
796 if ( false === $saved_ver ) {
797 return;
798 }
799
800 if ( version_compare( $saved_ver, $current_ver, '=' ) ) {
801 return;
802 }
803
804 if ( version_compare( $saved_ver, '0.0.4', '<' ) ) {
805 $sched = wp_next_scheduled( 'mainwp_cronstats_action' );
806 if ( ! empty( $sched ) ) {
807 wp_unschedule_event( $sched, 'mainwp_cronstats_action' );
808 }
809 global $wpdb;
810 $wpdb->query( 'DELETE FROM ' . $wpdb->usermeta . ' WHERE meta_key = "mainwp_widgets_sorted_toplevel_page_mainwp_tab" OR meta_key="mainwp_settings_show_widgets"' );//phpcs:ignore -- safe.
811 }
812
813 if ( version_compare( $saved_ver, '0.0.5', '<' ) ) {
814 $all_ext = MainWP_Extensions_View::get_available_extensions();
815 foreach ( $all_ext as $slug => $info ) {
816 $data = MainWP_Api_Manager::instance()->get_activation_info( $slug );
817 if ( is_array( $data ) && isset( $data['api_key'] ) ) {
818 $data['api_key'] = '';
819 $data['activated_key'] = 'Deactivated';
820 MainWP_Api_Manager::instance()->set_activation_info( $slug, $data );
821 }
822 }
823 update_option( 'mainwp_extensions_all_activation_cached', '' );
824 }
825
826 MainWP_Utility::update_option( 'mainwp_update_check_version', $current_ver );
827 }
828
829 /**
830 * Method admin_init()
831 *
832 * Do nothing if current user is not an Admin else display the page.
833 *
834 * @uses \MainWP\Dashboard\MainWP_Post_Backup_Handler::init()
835 * @uses \MainWP\Dashboard\MainWP_Post_Extension_Handler::init()
836 * @uses \MainWP\Dashboard\MainWP_Post_Handler::init()
837 * @uses \MainWP\Dashboard\MainWP_Post_Plugin_Theme_Handler::init()
838 * @uses \MainWP\Dashboard\MainWP_Post_Site_Handler::init()
839 * @uses \MainWP\Dashboard\MainWP_System_Handler::activate_extension()
840 * @uses \MainWP\Dashboard\MainWP_System_Utility::is_admin()
841 * @uses \MainWP\Dashboard\MainWP_System_View::get_class_name()
842 * @uses \MainWP\Dashboard\MainWP_System_View::get_mainwp_translations()
843 */
844 public function admin_init() { // phpcs:ignore -- NOSONAR - complex function.
845
846 if ( ! MainWP_System_Utility::is_admin() ) {
847 return;
848 }
849
850 add_action( 'mainwp_activate_extention', array( MainWP_System_Handler::instance(), 'activate_extension' ), 10, 2 ); // @deprecated Use 'mainwp_activate_extension' instead.
851 add_action( 'mainwp_deactivate_extention', array( MainWP_System_Handler::instance(), 'deactivate_extension' ), 10, 3 ); // @deprecated Use 'mainwp_deactivate_extension' instead.
852
853 add_action( 'mainwp_activate_extension', array( MainWP_System_Handler::instance(), 'activate_extension' ), 10, 2 );
854 add_action( 'mainwp_deactivate_extension', array( MainWP_System_Handler::instance(), 'deactivate_extension' ), 10, 3 );
855
856 /**
857 * MainWP use external primary backup method.
858 *
859 * @global string
860 */
861 global $mainwpUseExternalPrimaryBackupsMethod;
862
863 if ( null === $mainwpUseExternalPrimaryBackupsMethod ) {
864 $return = '';
865
866 /*
867 * @deprecated Use 'mainwp_getprimarybackup_activated' instead.
868 *
869 */
870 $return = apply_filters_deprecated( 'mainwp-getprimarybackup-activated', array( $return ), '4.0.7.2', 'mainwp_getprimarybackup_activated' ); // NOSONAR - not IP.
871 $mainwpUseExternalPrimaryBackupsMethod = apply_filters( 'mainwp_getprimarybackup_activated', $return );
872 }
873
874 add_action( 'mainwp_after_header', array( MainWP_System_View::get_class_name(), 'mainwp_warning_notice' ) );
875
876 MainWP_Post_Handler::instance()->init();
877 MainWP_Post_Site_Handler::instance()->init();
878 MainWP_Post_Plugin_Theme_Handler::instance()->init();
879 MainWP_Post_Extension_Handler::instance()->init();
880 MainWP_Post_Backup_Handler::instance()->init();
881 MainWP_Manage_Sites_Filter_Segment::get_instance()->admin_init();
882 MainWP_Ui_Manage_Widgets_Layout::get_instance()->admin_init();
883
884 /**
885 * Filter: mainwp_ui_use_wp_calendar
886 *
887 * Filters whether default jQuery datepicker should be used to avoid potential problems with Senatic UI Calendar library.
888 *
889 * @since 4.0.5
890 */
891 $use_wp_datepicker = apply_filters( 'mainwp_ui_use_wp_calendar', false );
892 if ( $use_wp_datepicker ) {
893 wp_enqueue_script( 'jquery-ui-datepicker' );
894 }
895 wp_enqueue_script( 'jquery-ui-dialog' );
896 $en_params = array( 'jquery-ui-dialog' );
897 if ( $use_wp_datepicker ) {
898 $en_params[] = 'jquery-ui-datepicker';
899 }
900
901 static::$defer_js_handle = array_merge( static::$defer_js_handle, array( 'mainwp' ) );
902
903 wp_enqueue_script( 'mainwp', MAINWP_PLUGIN_URL . 'assets/js/mainwp.js', $en_params, $this->current_version, true );
904 wp_enqueue_script( 'mainwp-cache-warm', MAINWP_PLUGIN_URL . 'assets/js/mainwp-cache-warm.js', array(), $this->current_version, true );
905
906 $disable_backup_checking = true; // removed option.
907 $mainwpParams = array(
908 'image_url' => MAINWP_PLUGIN_URL . 'assets/images/',
909 'backup_before_upgrade' => 1 === (int) get_option( 'mainwp_backup_before_upgrade' ),
910 'disable_checkBackupBeforeUpgrade' => $disable_backup_checking,
911 'admin_url' => admin_url(),
912 'use_wp_datepicker' => $use_wp_datepicker ? 1 : 0,
913 'date_format' => get_option( 'date_format' ),
914 'time_format' => get_option( 'time_format' ),
915 'installedBulkSettingsManager' => is_plugin_active( 'mainwp-bulk-settings-manager/mainwp-bulk-settings-manager.php' ) ? 1 : 0,
916 'maximumSyncRequests' => ( get_option( 'mainwp_maximumSyncRequests' ) === false ) ? 8 : get_option( 'mainwp_maximumSyncRequests' ),
917 'maximumInstallUpdateRequests' => ( get_option( 'mainwp_maximumInstallUpdateRequests' ) === false ) ? 3 : get_option( 'mainwp_maximumInstallUpdateRequests' ),
918 'maximumUptimeMonitoringRequests' => (int) get_option( 'mainwp_maximum_uptime_monitoring_requests', 10 ),
919 '_wpnonce' => wp_create_nonce( 'mainwp-admin-nonce' ),
920 'quickThemeChangeNonce' => wp_create_nonce( 'mainwp_quick_theme_change' ),
921 'demoMode' => MainWP_Demo_Handle::is_demo_mode() ? 1 : 0,
922 'roll_ui_icon' => MainWP_Updates_Helper::get_roll_icon( '', true ),
923 'admin_url_base' => admin_url(),
924 'mainwpVersion' => static::$version,
925 );
926 wp_localize_script( 'mainwp', 'mainwpParams', $mainwpParams );
927
928 $mainwpTranslations = MainWP_System_View::get_mainwp_translations();
929
930 wp_localize_script( 'mainwp', 'mainwpTranslations', $mainwpTranslations );
931
932 $security_nonces = MainWP_Post_Handler::instance()->create_security_nonces(); // to fix conflict with Post S M T P plugin.
933 $nonces_filter = apply_filters( 'mainwp_security_nonces', array() );
934 if ( is_array( $nonces_filter ) && ! empty( $nonces_filter ) ) {
935 $security_nonces = array_merge( $security_nonces, $nonces_filter );
936 }
937 wp_localize_script( 'mainwp', 'security_nonces', $security_nonces );
938
939 wp_enqueue_script( 'thickbox' );
940 wp_enqueue_script( 'user-profile' );
941 wp_enqueue_style( 'thickbox' );
942
943 $load_gridstack = false;
944
945 // phpcs:disable WordPress.Security.NonceVerification
946 if ( ( isset( $_GET['page'] ) && ( 'mainwp_tab' === $_GET['page'] || ( 'managesites' === $_GET['page'] && isset( $_GET['dashboard'] ) ) ) ) || ( isset( $_GET['page'] ) && 'ManageClients' === $_GET['page'] && isset( $_GET['client_id'] ) ) || ( isset( $_GET['page'] ) && 0 === strpos( wp_unslash( $_GET['page'] ), 'ManageSites' ) ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- individual page.
947 $load_gridstack = true;
948 }
949
950 // compatible filter.
951 $load_gridstack = apply_filters( 'mainwp_enqueue_script_gridster', $load_gridstack );
952
953 if ( $load_gridstack ) {
954 static::$defer_js_handle[] = 'mainwp_gridstack';
955 wp_enqueue_script( 'mainwp_gridstack', MAINWP_PLUGIN_URL . 'assets/js/gridstack/gridstack-all.js', array( 'jquery' ), $this->current_version, true );
956 wp_enqueue_style( 'mainwp_gridstack', MAINWP_PLUGIN_URL . 'assets/js/gridstack/gridstack.min.css', array(), $this->current_version );
957 }
958
959 if ( isset( $_GET['page'] ) && ( 'managesites' === $_GET['page'] || 'MonitoringSites' === $_GET['page'] || 'ManageGroups' === $_GET['page'] ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
960 wp_enqueue_script( 'preview', MAINWP_PLUGIN_URL . 'assets/js/preview.js', array(), $this->current_version, true );
961 wp_enqueue_style( 'preview', MAINWP_PLUGIN_URL . 'assets/css/preview.css', array(), $this->current_version );
962 }
963 // phpcs:enable
964
965 wp_enqueue_style( 'wp-color-picker' );
966 wp_enqueue_script( 'wp-color-picker' );
967
968 $this->init_session();
969
970 if ( ! current_user_can( 'update_core' ) ) {
971 remove_action( 'admin_notices', 'update_nag', 3 );
972 }
973 }
974
975 /**
976 * Method hook_wp_shutdown()
977 */
978 public function hook_wp_shutdown() {
979 if ( MainWP_Logger::DISABLED !== (int) get_option( 'mainwp_actionlogs' ) ) {
980 $error = error_get_last();
981 if ( is_array( $error ) && isset( $error['type'] ) && ( E_ERROR === $error['type'] || E_CORE_ERROR === $error['type'] || E_COMPILE_ERROR === $error['type'] || E_PARSE === $error['type'] ) ) {
982 MainWP_Logger::instance()->log_action( '[Fatal ERROR detected=' . print_r( $error, true ) . ']', false, MainWP_Logger::WARNING_COLOR, true ); //phpcs:ignore -- NOSONAR.
983 }
984 }
985 MainWP_Cache_Helper::log_metrics();
986
987 /**
988 * MainWP shutdown.
989 *
990 * @since 5.5.
991 */
992 do_action( 'mainwp_shutdown' );
993
994 $exctime = MainWP_Execution_Helper::get_run_time();
995 MainWP_Logger::instance()->log_events( 'execution-time', 'Shutdown :: ' . $exctime );
996 MainWP_Logger::instance()->log_execution_sync( 'end' );
997 }
998
999 /**
1000 * Method hook_plugin_action_links()
1001 *
1002 * @param string[] $actions An array of plugin action links. By default this can include
1003 * 'activate', 'deactivate', and 'delete'. With Multisite active
1004 * this can also include 'network_active' and 'network_only' items.
1005 * @param string $plugin_file Path to the plugin file relative to the plugins directory.
1006 * @param array $plugin_data An array of plugin data. See get_plugin_data()
1007 * and the {@see 'plugin_row_meta'} filter for the list
1008 * of possible values.
1009 */
1010 public function hook_plugin_action_links( $actions, $plugin_file, $plugin_data ) {
1011 unset( $plugin_file ); // not use, compatible.
1012 if ( is_array( $plugin_data ) && ! empty( $plugin_data['slug'] ) && ! empty( $plugin_data['plugin'] ) && 'mainwp' === $plugin_data['slug'] && 'mainwp/mainwp.php' === $plugin_data['plugin'] ) {
1013 $tmp = array(
1014 'mainwp-setup' => '<a href="admin.php?page=mainwp-setup" id="mainwp-setup" aria-label="' . esc_html__( 'MainWP Dashboard Setup Wizard', 'mainwp' ) . '">' . esc_html__( 'Setup Wizard', 'mainwp' ) . '</a>',
1015 );
1016 if ( is_array( $actions ) ) {
1017 foreach ( $actions as $key => $val ) {
1018 $tmp[ $key ] = $val;
1019
1020 }
1021 }
1022 return $tmp;
1023 }
1024 return $actions;
1025 }
1026
1027 /**
1028 * Method current_screen_redirects()
1029 *
1030 * MainWP current screen redirects.
1031 */
1032 public function current_screen_redirects() {
1033
1034 if ( ( defined( 'DOING_CRON' ) && DOING_CRON ) || defined( 'DOING_AJAX' ) ) {
1035 return;
1036 }
1037 if ( static::is_mainwp_pages() ) {
1038 $quick_setup = get_option( 'mainwp_run_quick_setup', false );
1039 if ( 'yes' === $quick_setup ) {
1040 delete_option( 'mainwp_run_quick_setup' );
1041 wp_safe_redirect( admin_url( 'admin.php?page=mainwp-setup' ) );
1042 exit;
1043 }
1044 }
1045
1046 if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'mainwp-setup' ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1047 return;
1048 }
1049
1050 if ( static::is_mainwp_pages() && 'yes' === get_option( 'mainwp_activated' ) ) {
1051 delete_option( 'mainwp_activated' );
1052 wp_cache_delete( 'mainwp_activated', 'options' );
1053 wp_cache_delete( 'alloptions', 'options' );
1054 wp_safe_redirect( admin_url( 'admin.php?page=mainwp_tab' ) );
1055 exit;
1056 }
1057 }
1058
1059 /**
1060 * Method admin_redirects()
1061 *
1062 * MainWP admin redirects.
1063 */
1064 public function admin_redirects() {
1065 if ( ( defined( 'DOING_CRON' ) && DOING_CRON ) || defined( 'DOING_AJAX' ) ) {
1066 return;
1067 }
1068
1069 if ( ! empty( $_GET['page'] ) && in_array( $_GET['page'], array( 'mainwp-setup' ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1070 return;
1071 }
1072
1073 $quick_setup = get_option( 'mainwp_run_quick_setup', false );
1074 if ( 'yes' === $quick_setup ) {
1075 wp_safe_redirect( admin_url( 'admin.php?page=mainwp-setup' ) );
1076 exit;
1077 }
1078
1079 $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification
1080 $_pos = strlen( $request_uri ) - strlen( '/wp-admin/' );
1081 if ( ! empty( $request_uri ) && strpos( $request_uri, '/wp-admin/' ) !== false && strpos( $request_uri, '/wp-admin/' ) === $_pos ) {
1082 $referer = wp_get_referer();
1083 if ( ! empty( $referer ) && strpos( $referer, 'wp-login.php?redirect_to' ) !== false && strpos( $referer, '&reauth=1' ) !== false && \mainwp_current_user_can( 'dashboard', 'access_global_dashboard' ) ) {
1084 wp_safe_redirect( admin_url( 'admin.php?page=mainwp_tab' ) );
1085 die();
1086 }
1087 }
1088
1089 MainWP_Logger::instance()->check_log_daily();
1090 }
1091
1092 /**
1093 * Method init_session()
1094 *
1095 * Check current page & initiate a session.
1096 *
1097 * @uses \MainWP\Dashboard\MainWP_Cache::init_session()
1098 */
1099 public function init_session() {
1100 $page = isset( $_GET['page'] ) ? wp_unslash( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1101 if ( ! empty( $page ) && in_array(
1102 $page,
1103 array(
1104 'PostBulkManage',
1105 'PageBulkManage',
1106 'PluginsManage',
1107 'PluginsAutoUpdate',
1108 'ThemesManage',
1109 'ThemesAutoUpdate',
1110 'UserBulkManage',
1111 )
1112 )
1113 ) {
1114 MainWP_Cache::init_session();
1115 }
1116 }
1117
1118 /**
1119 * Method clear_sessions()
1120 *
1121 * When logout clear the sessions to reset.
1122 */
1123 public function clear_sessions() {
1124 MainWP_Cache::init_session();
1125 $clear_cached = array(
1126 'Post',
1127 'Page',
1128 );
1129 foreach ( $clear_cached as $ca ) {
1130 MainWP_Cache::init_cache( $ca ); // to re-set cache.
1131 }
1132 }
1133
1134
1135 /**
1136 * Method admin_enqueue_scripts()
1137 *
1138 * Enqueue all Mainwp Admin Scripts.
1139 */
1140 public function admin_enqueue_scripts() { // phpcs:ignore -- NOSONAR - complex function.
1141
1142 $load_cust_scripts = false;
1143
1144 /**
1145 * Current pagenow.
1146 *
1147 * @global string
1148 */
1149 global $pagenow;
1150
1151 if ( is_plugin_active( 'mainwp-custom-post-types/mainwp-custom-post-types.php' ) && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) {
1152 $load_cust_scripts = true;
1153 }
1154
1155 if ( static::is_mainwp_pages() ) {
1156 wp_enqueue_script( 'jquery-migrate' );
1157 wp_enqueue_script( 'mainwp-updates', MAINWP_PLUGIN_URL . 'assets/js/mainwp-updates.js', array(), $this->current_version, true );
1158 wp_enqueue_script( 'mainwp-managesites-action', MAINWP_PLUGIN_URL . 'assets/js/mainwp-managesites-action.js', array(), $this->current_version, true );
1159 wp_enqueue_script( 'mainwp-managesites-update', MAINWP_PLUGIN_URL . 'assets/js/mainwp-managesites-update.js', array(), $this->current_version, true );
1160 wp_enqueue_script( 'mainwp-managesites-import', MAINWP_PLUGIN_URL . 'assets/js/mainwp-managesites-import.js', array(), $this->current_version, true );
1161 wp_enqueue_script( 'mainwp-plugins-themes', MAINWP_PLUGIN_URL . 'assets/js/mainwp-plugins-themes.js', array(), $this->current_version, true );
1162 wp_enqueue_script( 'mainwp-backups', MAINWP_PLUGIN_URL . 'assets/js/mainwp-backups.js', array(), $this->current_version, true );
1163 wp_enqueue_script( 'mainwp-posts', MAINWP_PLUGIN_URL . 'assets/js/mainwp-posts.js', array(), $this->current_version, true );
1164 wp_enqueue_script( 'mainwp-users', MAINWP_PLUGIN_URL . 'assets/js/mainwp-users.js', array(), $this->current_version, true );
1165 wp_enqueue_script( 'mainwp-clients', MAINWP_PLUGIN_URL . 'assets/js/mainwp-clients.js', array(), $this->current_version, true );
1166 wp_enqueue_script( 'mainwp-extensions', MAINWP_PLUGIN_URL . 'assets/js/mainwp-extensions.js', array(), $this->current_version, true );
1167 wp_enqueue_script( 'moment' );
1168 wp_enqueue_script( 'fomantic-ui', MAINWP_PLUGIN_URL . 'assets/js/fomantic-ui/fomantic-ui.js', array( 'jquery' ), $this->current_version, false );
1169
1170 wp_enqueue_script( 'datatables', MAINWP_PLUGIN_URL . 'assets/js/datatables/datatables.min.js', array( 'jquery' ), $this->current_version, false );
1171 wp_enqueue_script( 'datatables-semanticui', MAINWP_PLUGIN_URL . 'assets/js/datatables/dataTables.semanticui.min.js', array( 'datatables' ), $this->current_version, false );
1172 wp_enqueue_script( 'datatables-select', MAINWP_PLUGIN_URL . 'assets/js/datatables/dataTables.select.min.js', array( 'datatables' ), $this->current_version, false );
1173 wp_enqueue_script( 'datatables-add-ons', MAINWP_PLUGIN_URL . 'assets/js/datatables/add-ons.datatables.min.js', array( 'datatables' ), $this->current_version, false );
1174
1175 wp_enqueue_script( 'datatables-natural-sorting', MAINWP_PLUGIN_URL . 'assets/js/sorting/natural.min.js', array( 'jquery', 'datatables' ), $this->current_version, true );
1176
1177 wp_enqueue_script( 'clipboard' );
1178 wp_enqueue_script( 'mainwp-rest-api', MAINWP_PLUGIN_URL . 'assets/js/mainwp-rest-api.js', array(), time(), true );
1179
1180 if ( isset( $_GET['page'] ) && 'ManageGroups' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1181 wp_enqueue_script( 'mainwp-groups', MAINWP_PLUGIN_URL . 'assets/js/mainwp-groups.js', array(), $this->current_version, true );
1182 }
1183 $enqueue_scripts = apply_filters( 'mainwp_admin_enqueue_scripts', array() );
1184 if ( is_array( $enqueue_scripts ) && ! empty( $enqueue_scripts['apexcharts'] ) ) {
1185 wp_enqueue_script(
1186 'mainwp-apexcharts',
1187 MAINWP_PLUGIN_URL . 'assets/js/apexcharts/apexcharts.min.js',
1188 array(
1189 'jquery',
1190 'mainwp',
1191 ),
1192 $this->current_version,
1193 true
1194 );
1195 static::$defer_js_handle[] = 'mainwp-apexcharts';
1196 }
1197 wp_enqueue_script( 'mainwp-dropzone', MAINWP_PLUGIN_URL . 'assets/js/dropzone/dropzone.min.js', array(), $this->current_version, true );
1198 wp_enqueue_script( 'mainwp-uptime', MAINWP_PLUGIN_URL . 'assets/js/mainwp-uptime.js', array( 'jquery', 'fomantic-ui' ), $this->current_version, true );
1199 static::$defer_js_handle = array_merge( static::$defer_js_handle, array( 'mainwp-updates', 'mainwp-managesites-action', 'mainwp-managesites-update', 'mainwp-managesites-import', 'mainwp-plugins-themes', 'mainwp-managesites-import', 'mainwp-plugins-themes', 'mainwp-backups', 'mainwp-posts', 'mainwp-users', 'mainwp-clients', 'fomantic-ui', 'datatables', 'datatables-semanticui', 'datatables-select', 'datatables-add-ons', 'mainwp-dropzone', 'mainwp-uptime' ) );
1200
1201 }
1202
1203 if ( $load_cust_scripts ) {
1204 static::$defer_js_handle[] = 'fomantic-ui';
1205 wp_enqueue_script( 'fomantic-ui', MAINWP_PLUGIN_URL . 'assets/js/fomantic-ui/fomantic-ui.js', array( 'jquery' ), $this->current_version, true );
1206 }
1207
1208 wp_enqueue_script( 'mainwp-ui', MAINWP_PLUGIN_URL . 'assets/js/mainwp-ui.js', array(), $this->current_version, true );
1209 wp_localize_script(
1210 'mainwp-ui',
1211 'mainwpWidgetLayout',
1212 array(
1213 'openNonce' => wp_create_nonce( 'mainwp-admin-nonce' ),
1214 )
1215 );
1216 wp_enqueue_script( 'mainwp-js-popup', MAINWP_PLUGIN_URL . 'assets/js/mainwp-popup.js', array(), $this->current_version, true );
1217 // to support extension uploader.
1218 wp_enqueue_script( 'mainwp-fileuploader', MAINWP_PLUGIN_URL . 'assets/js/fileuploader.js', array(), $this->current_version ); // phpcs:ignore -- fileuploader scripts need to load at header.
1219 wp_enqueue_script( 'mainwp-filesaver', MAINWP_PLUGIN_URL . 'assets/js/FileSaver.js', array(), $this->current_version, true );
1220
1221 static::$defer_js_handle[] = 'mainwp-ui';
1222
1223 if ( is_array( static::$defer_js_handle ) ) {
1224 $defer_handle = array_filter( array_unique( static::$defer_js_handle ) );
1225 foreach ( $defer_handle as $h ) {
1226 if ( wp_script_is( $h, 'enqueued' ) || wp_script_is( $h, 'registered' ) ) {
1227 wp_script_add_data( $h, 'strategy', 'defer' ); // adds defer to <script> tag.
1228 }
1229 }
1230 }
1231 }
1232
1233 /**
1234 * Method admin_enqueue_scripts_fix_conflict()
1235 *
1236 * Enqueue Admin Scripts fix conflict.
1237 */
1238 public function admin_enqueue_scripts_fix_conflict() {
1239 if ( static::is_mainwp_pages() ) {
1240 // to fix conflict with the SVG Support plugin.
1241 remove_action( 'admin_enqueue_scripts', 'bodhi_svgs_admin_multiselect' );
1242 }
1243 }
1244
1245 /**
1246 * Method admin_enqueue_styles()
1247 *
1248 * Enqueue all Mainwp Admin Styles.
1249 */
1250 public function admin_enqueue_styles() { //phpcs:ignore -- NOSONAR - complex.
1251
1252 wp_enqueue_style( 'mainwp', MAINWP_PLUGIN_URL . 'assets/css/mainwp.css', array(), $this->current_version );
1253 wp_enqueue_style( 'mainwp-responsive-layouts', MAINWP_PLUGIN_URL . 'assets/css/mainwp-responsive-layouts.css', array(), $this->current_version );
1254
1255 if ( isset( $_GET['hideall'] ) && 1 === (int) $_GET['hideall'] ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1256 remove_action( 'admin_footer', 'wp_admin_bar_render', 1000 );
1257 }
1258
1259 /**
1260 * Current pagenow.
1261 *
1262 * @global string
1263 */
1264 global $pagenow;
1265
1266 $load_cust_scripts = false;
1267 if ( is_plugin_active( 'mainwp-custom-post-types/mainwp-custom-post-types.php' ) && ( 'post-new.php' === $pagenow || 'post.php' === $pagenow ) ) {
1268 $load_cust_scripts = true;
1269 }
1270
1271 if ( static::is_mainwp_pages() ) {
1272 wp_enqueue_style( 'mainwp-fonts', MAINWP_PLUGIN_URL . 'assets/css/mainwp-fonts.css', array(), $this->current_version );
1273 wp_enqueue_style( 'fomantic-ui', MAINWP_PLUGIN_URL . 'assets/js/fomantic-ui/fomantic-ui.css', array(), $this->current_version );
1274 wp_enqueue_style( 'mainwp-fomantic', MAINWP_PLUGIN_URL . 'assets/css/mainwp-fomantic.css', array(), $this->current_version );
1275
1276 wp_enqueue_style( 'datatables', MAINWP_PLUGIN_URL . 'assets/js/datatables/dataTables.dataTables.min.css', array(), $this->current_version );
1277 wp_enqueue_style( 'datatables-semanticui', MAINWP_PLUGIN_URL . 'assets/js/datatables/dataTables.semanticui.min.css', array(), $this->current_version );
1278 wp_enqueue_style( 'datatables-select', MAINWP_PLUGIN_URL . 'assets/js/datatables/select.semanticui.min.css', array(), $this->current_version );
1279 wp_enqueue_style( 'datatables-add-ons', MAINWP_PLUGIN_URL . 'assets/js/datatables/add-ons.datatables.min.css', array(), $this->current_version );
1280 // to fix conflict layout.
1281 wp_enqueue_style( 'jquery-ui-style', MAINWP_PLUGIN_URL . 'assets/css/1.11.1/jquery-ui.min.css', array(), '1.11.1' );
1282
1283 // load custom MainWP theme.
1284 $selected_theme = MainWP_Settings::get_instance()->get_selected_theme();
1285 if ( ! empty( $selected_theme ) ) {
1286 if ( 'dark' === $selected_theme ) {
1287 wp_enqueue_style( 'mainwp-custom-dashboard-extension-dark-theme', MAINWP_PLUGIN_URL . 'assets/css/themes/mainwp-dark-theme.css', array(), $this->current_version );
1288 } elseif ( 'wpadmin' === $selected_theme ) {
1289 wp_enqueue_style( 'mainwp-custom-dashboard-extension-wp-admin-theme', MAINWP_PLUGIN_URL . 'assets/css/themes/mainwp-wpadmin-theme.css', array(), $this->current_version );
1290 } elseif ( 'minimalistic' === $selected_theme ) {
1291 wp_enqueue_style( 'mainwp-custom-dashboard-extension-minimalistic-theme', MAINWP_PLUGIN_URL . 'assets/css/themes/mainwp-minimalistic-theme.css', array(), $this->current_version );
1292 } elseif ( 'default-2024' === $selected_theme ) {
1293 wp_enqueue_style( 'mainwp-custom-dashboard-extension-default-2024-theme', MAINWP_PLUGIN_URL . 'assets/css/themes/mainwp-default-2024-theme.css', array(), $this->current_version );
1294 } elseif ( 'default' === $selected_theme ) {
1295 wp_enqueue_style( 'mainwp-custom-dashboard-extension-default-theme', MAINWP_PLUGIN_URL . 'assets/css/themes/mainwp-default-theme.css', array(), $this->current_version );
1296 } elseif ( 'default-dark' === $selected_theme ) {
1297 wp_enqueue_style( 'mainwp-custom-dashboard-extension-default-dark-theme', MAINWP_PLUGIN_URL . 'assets/css/themes/mainwp-default-dark-theme.css', array(), $this->current_version );
1298 } else {
1299 $dirs = MainWP_Settings::get_instance()->get_custom_theme_folder();
1300 $custom_theme_url = $dirs[1];
1301 wp_enqueue_style( 'mainwp-custom-dashboard-theme', $custom_theme_url . $selected_theme, array(), $this->current_version );
1302 }
1303 }
1304 }
1305
1306 if ( $load_cust_scripts ) {
1307 wp_enqueue_style( 'mainwp-fonts', MAINWP_PLUGIN_URL . 'assets/css/mainwp-fonts.css', array(), $this->current_version );
1308 wp_enqueue_style( 'fomantic-ui', MAINWP_PLUGIN_URL . 'assets/js/fomantic-ui/fomantic-ui.css', array(), $this->current_version );
1309 }
1310 }
1311
1312 /**
1313 * Method admin_menu()
1314 *
1315 * Add Bulk Post/Pages menue.
1316 */
1317 public function admin_menu() {
1318
1319 /**
1320 * Admin menu array.
1321 *
1322 * @global object
1323 */
1324 global $menu;
1325
1326 foreach ( $menu as $k => $item ) {
1327 if ( 'edit.php?post_type=bulkpost' === $item[2] || 'edit.php?post_type=bulkpage' === $item[2] ) {
1328 unset( $menu[ $k ] );
1329 }
1330 }
1331 }
1332
1333 /**
1334 * Method enqueue_postbox_scripts()
1335 *
1336 * Enqueue postbox scripts.
1337 */
1338 public static function enqueue_postbox_scripts() {
1339 wp_enqueue_script( 'common' );
1340 wp_enqueue_script( 'wp-lists' );
1341 wp_enqueue_script( 'postbox' );
1342 }
1343
1344 /**
1345 * Method admin_footer()
1346 *
1347 * Create MainWP admin footer.
1348 *
1349 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
1350 * @uses \MainWP\Dashboard\MainWP_DB::query()
1351 * @uses \MainWP\Dashboard\MainWP_DB::get_sql_websites_for_current_user()
1352 * @uses \MainWP\Dashboard\MainWP_DB::free_result()
1353 * @uses \MainWP\Dashboard\MainWP_Menu::init_subpages_menu()
1354 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid()
1355 * @uses \MainWP\Dashboard\MainWP_System_View::render_footer_content()
1356 * @uses \MainWP\Dashboard\MainWP_System_View::admin_footer()
1357 */
1358 public function admin_footer() { //phpcs:ignore -- NOSONAR - complex.
1359 if ( ! static::is_mainwp_pages() ) {
1360 $sites_count = MainWP_DB::instance()->get_websites_count();
1361 if ( empty( $sites_count ) ) {
1362 ?>
1363 <script type="text/javascript">
1364 jQuery( function($){
1365 $( 'li#toplevel_page_mainwp_tab .wp-submenu-wrap .wp-submenu-head' ).after('<li style="background: #FFD300 !important;" class="wp-first-item"><a href="admin.php?page=mainwp-setup" style="color: #000 !important;" class="wp-first-item"><?php esc_html_e( 'Quick Setup', 'mainwp' ); ?></a></li>');
1366 });
1367 </script>
1368 <?php
1369 }
1370 return;
1371 }
1372
1373 ?>
1374 <div class="ui large modal" id="mainwp-response-data-modal">
1375 <i class="close icon"></i>
1376 <div class="header"><?php esc_html_e( 'Child Site Response', 'mainwp' ); ?></div>
1377 <div class="content">
1378 <div class="ui info message"><?php esc_html_e( 'To see the response in a more readable way, you can copy it and paste it into some HTML render tool, such as Codepen.io.', 'mainwp' ); ?>
1379 </div>
1380 </div>
1381 <div class="scrolling content content-response" contenteditable="true"></div>
1382 <div class="actions">
1383 <button class="ui green button mainwp-response-copy-button"><?php esc_html_e( 'Copy Response', 'mainwp' ); ?></button>
1384 </div>
1385 </div>
1386 <div id="mainwp-response-data-container" resp-data=""></div>
1387 <?php
1388
1389 if ( isset( $_GET['hideall'] ) && 1 === (int) $_GET['hideall'] ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1390 return;
1391 }
1392 $current_wpid = MainWP_System_Utility::get_current_wpid();
1393 if ( $current_wpid ) {
1394 $website = MainWP_DB::instance()->get_website_by_id( $current_wpid );
1395 $websites = array( $website );
1396 } else {
1397 $is_staging = 'no';
1398 if ( isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1399 if ( ( 'managesites' === $_GET['page'] ) && ! isset( $_GET['id'] ) && ! isset( $_GET['do'] ) && ! isset( $_GET['dashboard'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1400 $group_ids = get_user_option( 'mainwp_managesites_filter_group' );
1401 if ( ! empty( $group_ids ) ) {
1402 $group_ids = explode( ',', $group_ids ); // convert to array.
1403 }
1404 } elseif ( 'UpdatesManage' === $_GET['page'] || 'mainwp_tab' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1405 $staging_enabled = is_plugin_active( 'mainwp-staging-extension/mainwp-staging-extension.php' ) ? true : false;
1406 if ( $staging_enabled ) {
1407 $staging_view = MainWP_System_Utility::get_select_staging_view_sites();
1408 if ( 'staging' === $staging_view ) {
1409 $is_staging = 'yes';
1410 }
1411 }
1412 }
1413 }
1414 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp_sync.dtsSync DESC, wp.url ASC', false, false, null, false, array(), $is_staging ) );
1415 }
1416
1417 MainWP_System_View::render_footer_content( $websites, $current_wpid );
1418 if ( empty( $current_wpid ) ) {
1419 MainWP_DB::free_result( $websites );
1420 }
1421
1422 MainWP_System_View::admin_footer();
1423 MainWP_System_View::render_plugins_install_check();
1424
1425 /**
1426 * MainWP disabled menu items array.
1427 *
1428 * @global object
1429 */
1430 global $_mainwp_disable_menus_items;
1431
1432 $_mainwp_disable_menus_items = apply_filters( 'mainwp_all_disablemenuitems', $_mainwp_disable_menus_items );
1433 }
1434
1435 /**
1436 * Method activated_check()
1437 *
1438 * Activated check.
1439 *
1440 * @uses \MainWP\Dashboard\MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook()
1441 */
1442 public function activated_check() {
1443 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
1444 return $this->get_version();
1445 }
1446
1447 /**
1448 * Method activation()
1449 *
1450 * Activate MainWP.
1451 *
1452 * @uses \MainWP\Dashboard\MainWP_Install::install()
1453 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
1454 */
1455 public function activation() {
1456 $this->update_install();
1457 MainWP_Utility::update_option( 'mainwp_activated', 'yes' );
1458 }
1459
1460 /**
1461 * Method deactivation()
1462 *
1463 * Deactivate MainWP.
1464 */
1465 public function deactivation() {
1466 update_option( 'mainwp_extensions_all_activation_cached', '' );
1467 $useWPCron = ( get_option( 'mainwp_wp_cron' ) === false ) || ( (int) get_option( 'mainwp_wp_cron' ) === 1 );
1468 if ( $useWPCron ) {
1469 $jobs = MainWP_System_Cron_Jobs::instance()->get_cron_jobs();
1470 if ( $jobs ) {
1471 foreach ( $jobs as $job => $recu ) {
1472 wp_clear_scheduled_hook( $job );
1473 }
1474 }
1475 }
1476 }
1477
1478 /**
1479 * Method update_install()
1480 *
1481 * Update MainWP.
1482 *
1483 * @uses \MainWP\Dashboard\MainWP_Install::install()
1484 */
1485 public function update_install() {
1486 MainWP_DB_Client::instance();
1487 MainWP_DB_Site_Actions::instance();
1488 MainWP_Install::instance()->install();
1489 $this->check_to_updates();
1490 }
1491
1492
1493 /**
1494 * Method check_to_updates()
1495 *
1496 * To check updates options.
1497 */
1498 public function check_to_updates() {
1499 $update_ver = get_option( 'mainwp_update_check_version', false );
1500 $current_ver = $this->check_ver_update;
1501
1502 if ( false === $update_ver && version_compare( $current_ver, '0.0.1', '=' ) ) {
1503 // to update new saving format.
1504 MainWP_Api_Manager_Key::instance()->get_decrypt_master_api_key();
1505 MainWP_Utility::update_option( 'mainwp_update_check_version', $current_ver );
1506 }
1507 }
1508
1509 /**
1510 * Method is_single_user()
1511 *
1512 * Check if single user environment.
1513 *
1514 * @return boolean true|false.
1515 */
1516 public function is_single_user() {
1517 return true;
1518 }
1519
1520 /**
1521 * Method is_multi_user()
1522 *
1523 * Check if multi user environment.
1524 *
1525 * @return boolean true|false.
1526 */
1527 public function is_multi_user() {
1528 return ! $this->is_single_user();
1529 }
1530
1531 /**
1532 * Method get_plugin_slug()
1533 *
1534 * Get MainWP Plugin Slug.
1535 */
1536 public function get_plugin_slug() {
1537 return $this->plugin_slug;
1538 }
1539
1540 /**
1541 * Method get_selected_sites_with_allowed_sites.
1542 *
1543 * @param array $selected_ids Selected sites IDs.
1544 * @param array $current_sites Current sites IDs.
1545 * @return array Not selected but in not allowed sites in current sites ids, and selected sites ids.
1546 */
1547 public static function get_selected_sites_with_allowed_sites( $selected_ids, $current_sites_ids = array() ) {
1548
1549 $allowed_sites = apply_filters( 'mainwp_currentuserallowedaccesssites', 'all' );
1550 if ( 'all' === $allowed_sites ) {
1551 return $selected_ids;
1552 }
1553
1554 if ( ! is_array( $selected_ids ) ) {
1555 $selected_ids = array();
1556 }
1557
1558 if ( ! is_array( $current_sites_ids ) ) {
1559 $current_sites_ids = array();
1560 }
1561
1562 $allowed_sites = MainWP_DB::instance()->get_websites_for_current_user();
1563 if ( empty( $allowed_sites ) ) {
1564 return array();
1565 }
1566
1567 $allowed_ids = array_values(
1568 array_map(
1569 function ( $item ) {
1570 return $item->id;
1571 },
1572 $allowed_sites
1573 )
1574 );
1575
1576 $client_site_ids = array();
1577 foreach ( $selected_ids as $id ) {
1578 if ( in_array( $id, $allowed_ids ) ) {
1579 $client_site_ids[] = $id;
1580 }
1581 }
1582
1583 foreach ( $current_sites_ids as $id ) {
1584 if ( ! in_array( $id, $selected_ids ) && ! in_array( $id, $allowed_ids ) ) { // If it’s not in the allowed site IDs, the user is not permitted to change it.
1585 $client_site_ids[] = $id; // keep this site ids.
1586 }
1587 }
1588 return $client_site_ids;
1589 }
1590 }
1591