PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.0.11
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.0.11
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.0.11, at class/class-mainwp-system.php

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