PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.4.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.4.1
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-handler.php

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

1,045 lines 37.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * System Handler
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_System_Handler
12 *
13 * @package MainWP\Dashboard
14 */
15 class MainWP_System_Handler {
16
17 // phpcs:disable WordPress.WP.AlternativeFunctions -- use system functions
18
19 /**
20 * Private static variable to hold the single instance of the class.
21 *
22 * @static
23 *
24 * @var mixed Default null
25 */
26 private static $instance = null;
27
28 /**
29 * Private variable to hold the upgrade version info.
30 *
31 * @var null Version info.
32 */
33 private $upgradeVersionInfo = null;
34
35 /**
36 * Method instance()
37 *
38 * Create public static instance.
39 *
40 * @static
41 * @return MainWP_System
42 */
43 public static function instance() {
44 if ( null == self::$instance ) {
45 self::$instance = new self();
46 }
47
48 return self::$instance;
49 }
50
51 /**
52 * MainWP_System_Handler constructor.
53 *
54 * Run each time the class is called.
55 *
56 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_class_name()
57 */
58 public function __construct() {
59 add_filter( 'mainwp-extension-enabled-check', array( MainWP_Extensions_Handler::get_class_name(), 'is_extension_enabled' ) ); // @deprecated Use 'mainwp_extension_enabled_check' instead.
60 add_filter( 'mainwp_extension_enabled_check', array( MainWP_Extensions_Handler::get_class_name(), 'is_extension_enabled' ) );
61
62 /**
63 * This hook allows you to get a list of sites via the 'mainwp-getsites' filter.
64 *
65 * @link http://codex.mainwp.com/#mainwp-getsites
66 *
67 * @see \MainWP_Extensions::hook_get_sites
68 */
69 add_filter( 'mainwp-getsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_sites' ), 10, 5 ); // @deprecated Use 'mainwp_getsites' instead.
70 add_filter( 'mainwp-getdbsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_db_sites' ), 10, 5 ); // @deprecated Use 'mainwp_getdbsites' instead.
71
72 add_filter( 'mainwp_getsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_sites' ), 10, 5 );
73 add_filter( 'mainwp_getdbsites', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_db_sites' ), 10, 5 );
74
75 /**
76 * This hook allows you to get a information about groups via the 'mainwp-getgroups' filter.
77 *
78 * @link http://codex.mainwp.com/#mainwp-getgroups
79 *
80 * @see \MainWP_Extensions::hook_get_groups
81 */
82 add_filter( 'mainwp-getgroups', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_groups' ), 10, 4 ); // @deprecated Use 'mainwp_getgroups' instead.
83 add_filter( 'mainwp_getgroups', array( MainWP_Extensions_Handler::get_class_name(), 'hook_get_groups' ), 10, 4 );
84 add_action( 'mainwp_fetchurlsauthed', array( &$this, 'filter_fetch_urls_authed' ), 10, 7 );
85 add_filter( 'mainwp_fetchurlauthed', array( &$this, 'filter_fetch_url_authed' ), 10, 6 );
86 add_filter( 'mainwp_getsqlwebsites_for_current_user', array( self::class, 'hook_get_sql_websites_for_current_user' ), 10, 4 );
87
88 add_filter(
89 'mainwp_getdashboardsites',
90 array(
91 MainWP_Extensions_Handler::get_class_name(),
92 'hook_get_dashboard_sites',
93 ),
94 10,
95 7
96 );
97
98 // @deprecated Use 'mainwp_manager_getextensions' instead.
99 add_filter(
100 'mainwp-manager-getextensions',
101 array(
102 MainWP_Extensions_Handler::get_class_name(),
103 'hook_get_all_extensions',
104 )
105 );
106
107 add_filter(
108 'mainwp_manager_getextensions',
109 array(
110 MainWP_Extensions_Handler::get_class_name(),
111 'hook_get_all_extensions',
112 )
113 );
114
115 add_action( 'admin_init', array( &$this, 'admin_init' ) );
116
117 if ( '' != get_option( 'mainwp_upgradeVersionInfo' ) ) {
118 $this->upgradeVersionInfo = get_option( 'mainwp_upgradeVersionInfo' );
119 if ( ! is_object( $this->upgradeVersionInfo ) ) {
120 $this->upgradeVersionInfo = new \stdClass();
121 }
122 }
123 }
124
125 /**
126 * Method filter_fetch_urls_authed()
127 *
128 * Filter fetch authorized urls.
129 *
130 * @param mixed $pluginFile MainWP extention.
131 * @param string $key MainWP Licence Key.
132 * @param object $dbwebsites Child Sites.
133 * @param string $what Function to perform.
134 * @param array $params Function paramerters.
135 * @param mixed $handle Function handle.
136 * @param mixed $output Function output.
137 *
138 * @return mixed MainWP_Extensions_Handler::hook_fetch_urls_authed() Hook fetch authorized URLs.
139 *
140 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::hook_fetch_urls_authed()
141 */
142 public function filter_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output ) {
143 return MainWP_Extensions_Handler::hook_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output, $is_external_hook = true );
144 }
145
146 /**
147 * Method filter_fetch_url_authed()
148 *
149 * Filter fetch Authorized URL.
150 *
151 * @param mixed $pluginFile MainWP extention.
152 * @param string $key MainWP licence key.
153 * @param int $websiteId Website ID.
154 * @param string $what Function to perform.
155 * @param array $params Function paramerters.
156 * @param null $raw_response Raw response.
157 *
158 * @return mixed MainWP_Extensions_Handler::hook_fetch_url_authed() Hook fetch authorized URL.
159 *
160 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::hook_fetch_url_authed()
161 */
162 public function filter_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $raw_response = null ) {
163 return MainWP_Extensions_Handler::hook_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $raw_response );
164 }
165
166 /**
167 * Method apply_filter()
168 *
169 * Apply filter
170 *
171 * @param string $filter The filter.
172 * @param array $value Input value.
173 *
174 * @return array $output Output array.
175 *
176 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::hook_verify()
177 */
178 public function apply_filters( $filter, $value = array() ) {
179
180 if ( 'mainwp-getmetaboxes' === $filter ) {
181 $output = apply_filters_deprecated( 'mainwp-getmetaboxes', array( $value ), '4.0.7.2', 'mainwp_getmetaboxes' ); // @deprecated Use 'mainwp_getmetaboxes' instead.
182 } else {
183 $output = apply_filters( $filter, $value );
184 }
185
186 if ( ! is_array( $output ) ) {
187 return array();
188 }
189 $count = count( $output );
190 for ( $i = 0; $i < $count; $i ++ ) {
191
192 if ( 'mainwp_getmetaboxes' === $filter ) {
193 // pass custom widget.
194 if ( isset( $output[ $i ]['custom'] ) && $output[ $i ]['custom'] && isset( $output[ $i ]['plugin'] ) ) {
195 continue;
196 }
197 }
198
199 if ( ! isset( $output[ $i ]['plugin'] ) || ! isset( $output[ $i ]['key'] ) ) {
200 unset( $output[ $i ] );
201 continue;
202 }
203
204 if ( ! MainWP_Extensions_Handler::hook_verify( $output[ $i ]['plugin'], $output[ $i ]['key'] ) ) {
205 unset( $output[ $i ] );
206 continue;
207 }
208 }
209
210 return $output;
211 }
212
213 /**
214 * Method admin_init()
215 *
216 * Do nothing if current user is not an Admin.
217 */
218 public function admin_init() { // phpcs:ignore -- complex function.
219 if ( ! MainWP_System_Utility::is_admin() ) {
220 return;
221 }
222
223 global $pagenow;
224
225 if ( 'plugins.php' === $pagenow && isset( $_GET['do'] ) && 'checkUpgrade' === $_GET['do'] && ( ( time() - $this->upgradeVersionInfo->updated ) > 30 ) ) {
226 $this->check_upgrade();
227 delete_site_transient( 'update_plugins' ); // to forced refresh 'update_plugins' transient.
228 wp_safe_redirect( admin_url( 'plugins.php' ) );
229 exit;
230 }
231 }
232
233
234
235 /**
236 * Method hook_get_sql_websites_for_current_user()
237 *
238 * Get sql websites for current user.
239 *
240 * @param mixed $false First input filter value.
241 * @param string $pluginFile Extension plugin file to verify.
242 * @param string $key The child-key.
243 * @param mixed $params Input params data.
244 *
245 * @return string sql.
246 */
247 public static function hook_get_sql_websites_for_current_user( $false, $pluginFile, $key, $params ) {
248 if ( ! MainWP_Extensions_Handler::hook_verify( $pluginFile, $key ) ) {
249 return false;
250 }
251 return MainWP_DB::instance()->get_sql_wp_for_current_user( $params );
252 }
253 /**
254 * Method handle_manage_sites_screen_settings()
255 *
256 * Handle manage sites screen settings
257 */
258 public function handle_manage_sites_screen_settings() { // phpcs:ignore -- required to achieve desired results, pull request solutions appreciated.
259 if ( isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'ManageSitesScrOptions' ) ) {
260 $show_cols = array();
261 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST ) ) as $key => $val ) {
262 if ( false !== strpos( $key, 'mainwp_show_column_' ) ) {
263 $col = str_replace( 'mainwp_show_column_', '', $key );
264 $show_cols[ $col ] = 1;
265 }
266 }
267 if ( isset( $_POST['show_columns_name'] ) ) {
268 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['show_columns_name'] ) ) as $col ) {
269 if ( ! isset( $show_cols[ $col ] ) ) {
270 $show_cols[ $col ] = 0; // uncheck, hide columns.
271 }
272 }
273 }
274 MainWP_Utility::update_option( 'mainwp_use_favicon', ( ! isset( $_POST['mainwp_use_favicon'] ) ? 0 : 1 ) );
275 MainWP_Utility::update_option( 'mainwp_optimize', ( ! isset( $_POST['mainwp_optimize'] ) ? 0 : 1 ) );
276 $user = wp_get_current_user();
277 if ( $user ) {
278 $val = ( isset( $_POST['mainwp_sitesviewmode'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_sitesviewmode'] ) ) : 'grid' );
279 if ( 'grid' !== $val && 'table' !== $val ) {
280 $val = 'grid';
281 }
282 update_user_option( $user->ID, 'mainwp_sitesviewmode', $val, true );
283 update_user_option( $user->ID, 'mainwp_settings_show_manage_sites_columns', $show_cols, true );
284 update_option( 'mainwp_default_sites_per_page', ( isset( $_POST['mainwp_default_sites_per_page'] ) ? intval( $_POST['mainwp_default_sites_per_page'] ) : 25 ) );
285 }
286 } elseif ( isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'SreenshotsSitesScrOptions' ) ) {
287 $user = wp_get_current_user();
288 if ( $user ) {
289 $val = ( isset( $_POST['mainwp_sitesviewmode'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_sitesviewmode'] ) ) : 'grid' );
290 if ( 'grid' !== $val && 'table' !== $val ) {
291 $val = 'grid';
292 }
293 update_user_option( $user->ID, 'mainwp_sitesviewmode', $val, true );
294 }
295 }
296 }
297
298 /**
299 * Method handle_monitoring_sites_screen_settings()
300 *
301 * Handle monitoring sites screen settings
302 */
303 public function handle_monitoring_sites_screen_settings() {
304 if ( isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'MonitoringSitesScrOptions' ) ) {
305 $show_cols = array();
306 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST ) ) as $key => $val ) {
307 if ( false !== strpos( $key, 'mainwp_show_column_' ) ) {
308 $col = str_replace( 'mainwp_show_column_', '', $key );
309 $show_cols[ $col ] = 1;
310 }
311 }
312 if ( isset( $_POST['show_columns_name'] ) ) {
313 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['show_columns_name'] ) ) as $col ) {
314 if ( ! isset( $show_cols[ $col ] ) ) {
315 $show_cols[ $col ] = 0; // uncheck, hide columns.
316 }
317 }
318 }
319
320 $user = wp_get_current_user();
321 if ( $user ) {
322 update_user_option( $user->ID, 'mainwp_settings_show_monitoring_sites_columns', $show_cols, true );
323 update_option( 'mainwp_default_monitoring_sites_per_page', ( isset( $_POST['mainwp_default_monitoring_sites_per_page'] ) ? intval( $_POST['mainwp_default_monitoring_sites_per_page'] ) : 25 ) );
324 }
325
326 update_option( 'mainwp_disableSitesChecking', ( ! isset( $_POST['mainwp_disableSitesChecking'] ) ? 1 : 0 ) );
327
328 $val = isset( $_POST['mainwp_frequency_sitesChecking'] ) ? intval( $_POST['mainwp_frequency_sitesChecking'] ) : 1440;
329 update_option( 'mainwp_frequencySitesChecking', $val );
330
331 update_option( 'mainwp_disableSitesHealthMonitoring', ( ! isset( $_POST['mainwp_disable_sitesHealthMonitoring'] ) ? 1 : 0 ) );
332
333 $val = isset( $_POST['mainwp_site_healthThreshold'] ) ? intval( $_POST['mainwp_site_healthThreshold'] ) : 80;
334 update_option( 'mainwp_sitehealthThreshold', $val );
335 }
336 }
337
338 /**
339 * Method handle_clients_screen_settings()
340 *
341 * Handle manage clients screen settings
342 */
343 public function handle_clients_screen_settings() {
344 if ( isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'ManageClientsScrOptions' ) ) {
345 $show_cols = array();
346 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST ) ) as $key => $val ) {
347 if ( false !== strpos( $key, 'mainwp_show_column_' ) ) {
348 $col = str_replace( 'mainwp_show_column_', '', $key );
349 $show_cols[ $col ] = 1;
350 }
351 }
352 if ( isset( $_POST['show_columns_name'] ) ) {
353 foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['show_columns_name'] ) ) as $col ) {
354 if ( ! isset( $show_cols[ $col ] ) ) {
355 $show_cols[ $col ] = 0; // uncheck, hide columns.
356 }
357 }
358 }
359
360 $user = wp_get_current_user();
361 if ( $user ) {
362 update_user_option( $user->ID, 'mainwp_settings_show_manage_clients_columns', $show_cols, true );
363 update_option( 'mainwp_default_manage_clients_per_page', ( isset( $_POST['mainwp_default_manage_clients_per_page'] ) ? intval( $_POST['mainwp_default_manage_clients_per_page'] ) : 25 ) );
364 }
365 }
366 }
367
368 /**
369 * Method handle_clients_screen_settings()
370 *
371 * Handle manage clients screen settings
372 */
373 public function handle_updates_screen_settings() {
374 if ( isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'UpdatesScrOptions' ) ) {
375 $val = ( ! isset( $_POST['mainwp_pluginAutomaticDailyUpdate'] ) ? 0 : intval( $_POST['mainwp_pluginAutomaticDailyUpdate'] ) );
376 MainWP_Utility::update_option( 'mainwp_pluginAutomaticDailyUpdate', $val );
377 $val = ( ! isset( $_POST['mainwp_themeAutomaticDailyUpdate'] ) ? 0 : intval( $_POST['mainwp_themeAutomaticDailyUpdate'] ) );
378 MainWP_Utility::update_option( 'mainwp_themeAutomaticDailyUpdate', $val );
379 $val = ( ! isset( $_POST['mainwp_automaticDailyUpdate'] ) ? 0 : intval( $_POST['mainwp_automaticDailyUpdate'] ) );
380 MainWP_Utility::update_option( 'mainwp_automaticDailyUpdate', $val );
381 $val = ( isset( $_POST['mainwp_delay_autoupdate'] ) ? intval( $_POST['mainwp_delay_autoupdate'] ) : 1 );
382 MainWP_Utility::update_option( 'mainwp_delay_autoupdate', $val );
383 $val = ( ! isset( $_POST['mainwp_show_language_updates'] ) ? 0 : 1 );
384 MainWP_Utility::update_option( 'mainwp_show_language_updates', $val );
385 $val = ( ! isset( $_POST['mainwp_disable_update_confirmations'] ) ? 0 : intval( $_POST['mainwp_disable_update_confirmations'] ) );
386 MainWP_Utility::update_option( 'mainwp_disable_update_confirmations', $val );
387 }
388 }
389
390
391 /**
392 * Method handle_mainwp_tools_settings()
393 *
394 * Handle mainwp tools settings.
395 *
396 * @uses \MainWP\Dashboard\MainWP_Twitter::clear_all_twitter_messages()
397 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
398 */
399 public function handle_mainwp_tools_settings() { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
400
401 $user = wp_get_current_user();
402
403 $update_selected_mainwp_themes = false;
404
405 if ( isset( $_GET['page'] ) && 'MainWPTools' === $_GET['page'] ) {
406 if ( isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'MainWPTools' ) ) {
407 if ( isset( $_POST['mainwp_restore_info_messages'] ) && ! empty( $_POST['mainwp_restore_info_messages'] ) ) {
408 delete_user_option( $user->ID, 'mainwp_notice_saved_status' );
409 } else {
410 MainWP_Utility::update_option( 'mainwp_enable_managed_cr_for_wc', ( ! isset( $_POST['enable_managed_cr_for_wc'] ) ? 0 : 1 ) );
411 $enabled_twit = ! isset( $_POST['mainwp_hide_twitters_message'] ) ? 0 : 1;
412 MainWP_Utility::update_option( 'mainwp_hide_twitters_message', $enabled_twit );
413 if ( ! $enabled_twit ) {
414 MainWP_Twitter::clear_all_twitter_messages();
415 }
416 }
417 $enabled_tours = ! isset( $_POST['mainwp-guided-tours-option'] ) ? 0 : 1;
418 MainWP_Utility::update_option( 'mainwp_enable_guided_tours', $enabled_tours );
419
420 if ( isset( $_POST['mainwp_settings_custom_theme'] ) ) {
421 $update_selected_mainwp_themes = true;
422 }
423 }
424 }
425
426 if ( isset( $_POST['wp_scr_options_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_scr_options_nonce'] ), 'MainWPSelectThemes' ) ) {
427 $update_selected_mainwp_themes = true;
428 }
429
430 if ( $update_selected_mainwp_themes ) {
431 if ( isset( $_POST['mainwp_settings_custom_theme'] ) ) {
432 $custom_theme = sanitize_text_field( wp_unslash( $_POST['mainwp_settings_custom_theme'] ) );
433 update_user_option( $user->ID, 'mainwp_selected_theme', $custom_theme );
434 }
435 }
436
437 $update_screen_options = false;
438 if ( isset( $_POST['wp_nonce'] ) && ( wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) || wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'MainWPScrOptions' ) ) ) {
439 $update_screen_options = true;
440 }
441
442 if ( $update_screen_options ) {
443 $show_wids = array();
444 if ( isset( $_POST['mainwp_show_widgets'] ) && is_array( $_POST['mainwp_show_widgets'] ) ) {
445 $selected_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_show_widgets'] ) );
446 foreach ( $selected_wids as $name ) {
447 $show_wids[ $name ] = 1;
448 }
449 }
450
451 if ( isset( $_POST['mainwp_widgets_name'] ) && is_array( $_POST['mainwp_widgets_name'] ) ) {
452 $name_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_widgets_name'] ) );
453 foreach ( $name_wids as $name ) {
454 if ( ! isset( $show_wids[ $name ] ) ) {
455 $show_wids[ $name ] = 0;
456 }
457 }
458 }
459
460 $show_cols = array();
461 if ( isset( $_POST['mainwp_show_columns'] ) && is_array( $_POST['mainwp_show_columns'] ) ) {
462 $selected_cols = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_show_columns'] ) );
463 foreach ( $selected_cols as $name ) {
464 $show_cols[ $name ] = 1;
465 }
466 }
467
468 if ( isset( $_POST['mainwp_columns_name'] ) && is_array( $_POST['mainwp_columns_name'] ) ) {
469 $name_cols = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_columns_name'] ) );
470 foreach ( $name_cols as $name ) {
471 if ( ! isset( $show_cols[ $name ] ) ) {
472 $show_cols[ $name ] = 0;
473 }
474 }
475 }
476
477 $val = ( isset( $_POST['mainwp_sidebarPosition'] ) ? intval( $_POST['mainwp_sidebarPosition'] ) : 1 );
478 if ( $user ) {
479 update_user_option( $user->ID, 'mainwp_settings_show_widgets', $show_wids, true );
480 update_user_option( $user->ID, 'mainwp_sidebarPosition', $val, true );
481
482 if ( isset( $_POST['mainwp_manageposts_show_columns_settings'] ) ) {
483 update_user_option( $user->ID, 'mainwp_manageposts_show_columns', $show_cols, true );
484 }
485
486 if ( isset( $_POST['mainwp_managepages_show_columns_settings'] ) ) {
487 update_user_option( $user->ID, 'mainwp_managepages_show_columns', $show_cols, true );
488 }
489
490 if ( isset( $_POST['mainwp_manageusers_show_columns_settings'] ) ) {
491 update_user_option( $user->ID, 'mainwp_manageusers_show_columns', $show_cols, true );
492 }
493 }
494
495 MainWP_Utility::update_option( 'mainwp_hide_update_everything', ( ! isset( $_POST['hide_update_everything'] ) ? 0 : 1 ) );
496 MainWP_Utility::update_option( 'mainwp_number_overview_columns', ( isset( $_POST['number_overview_columns'] ) ? intval( $_POST['number_overview_columns'] ) : 2 ) );
497
498 if ( isset( $_POST['reset_overview_settings'] ) && ! empty( $_POST['reset_overview_settings'] ) && isset( $_POST['reset_overview_which_settings'] ) && 'overview_settings' == $_POST['reset_overview_which_settings'] ) {
499 update_user_option( $user->ID, 'mainwp_widgets_sorted_toplevel_page_mainwp_tab', false, true );
500 update_user_option( $user->ID, 'mainwp_widgets_sorted_mainwp_page_managesites', false, true );
501 }
502 }
503
504 $update_clients_screen_options = false;
505 if ( isset( $_POST['wp_scr_options_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_scr_options_nonce'] ), 'MainWPClientsScrOptions' ) ) {
506 $update_clients_screen_options = true;
507 }
508 if ( $update_clients_screen_options ) {
509
510 $show_wids = array();
511 if ( isset( $_POST['mainwp_show_widgets'] ) && is_array( $_POST['mainwp_show_widgets'] ) ) {
512 $selected_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_show_widgets'] ) );
513 foreach ( $selected_wids as $name ) {
514 $show_wids[ $name ] = 1;
515 }
516 }
517
518 if ( isset( $_POST['mainwp_widgets_name'] ) && is_array( $_POST['mainwp_widgets_name'] ) ) {
519 $name_wids = array_map( 'sanitize_text_field', wp_unslash( $_POST['mainwp_widgets_name'] ) );
520 foreach ( $name_wids as $name ) {
521 if ( ! isset( $show_wids[ $name ] ) ) {
522 $show_wids[ $name ] = 0;
523 }
524 }
525 }
526
527 if ( $user ) {
528 update_user_option( $user->ID, 'mainwp_clients_show_widgets', $show_wids, true );
529 }
530
531 MainWP_Utility::update_option( 'mainwp_number_clients_overview_columns', ( isset( $_POST['number_overview_columns'] ) ? intval( $_POST['number_overview_columns'] ) : 2 ) );
532
533 if ( isset( $_POST['reset_client_overview_settings'] ) && ! empty( $_POST['reset_client_overview_settings'] ) ) {
534 update_user_option( $user->ID, 'mainwp_widgets_sorted_mainwp_page_manageclients', false, true );
535 }
536 }
537 }
538
539
540 /**
541 * Method handle_rest_api_settings()
542 *
543 * Handle rest api settings
544 */
545 public function handle_rest_api_settings() {
546 if ( isset( $_POST['submit'] ) && isset( $_GET['page'] ) && 'RESTAPI' === $_GET['page'] ) {
547 if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'RESTAPI' ) ) {
548 MainWP_Utility::update_option( 'mainwp_enable_rest_api', ( ! isset( $_POST['mainwp_enable_rest_api'] ) ? 0 : 1 ) );
549
550 }
551 }
552 }
553
554 /**
555 * Method handle_settings_post()
556 *
557 * Handle saving settings page.
558 *
559 * @uses \MainWP\Dashboard\MainWP_Backup_Handler::handle_settings_post()
560 * @uses \MainWP\Dashboard\MainWP_DB_Common::get_user_extension()
561 * @uses \MainWP\Dashboard\MainWP_DB_Common::update_user_extension()
562 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::handle_settings_post()
563 * @uses \MainWP\Dashboard\MainWP_Settings::handle_settings_post()
564 */
565 public function handle_settings_post() {
566
567 if ( isset( $_GET['page'] ) && ( isset( $_POST['wp_nonce'] ) || isset( $_POST['wp_scr_options_nonce'] ) ) ) {
568 $this->include_pluggable();
569 $this->handle_mainwp_tools_settings();
570 $this->handle_rest_api_settings();
571 $this->handle_manage_sites_screen_settings();
572 $this->handle_monitoring_sites_screen_settings();
573 $this->handle_clients_screen_settings();
574 $this->handle_updates_screen_settings();
575 }
576
577 if ( isset( $_POST['select_mainwp_options_siteview'] ) ) {
578 $this->include_pluggable();
579 if ( check_admin_referer( 'mainwp-admin-nonce' ) ) {
580 $userExtension = MainWP_DB_Common::instance()->get_user_extension();
581 $userExtension->site_view = ( empty( $_POST['select_mainwp_options_siteview'] ) ? MAINWP_VIEW_PER_PLUGIN_THEME : intval( $_POST['select_mainwp_options_siteview'] ) );
582 MainWP_DB_Common::instance()->update_user_extension( $userExtension );
583 }
584 }
585
586 if ( isset( $_POST['submit'] ) && isset( $_POST['wp_nonce'] ) ) {
587 $this->include_pluggable();
588 if ( wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'Settings' ) ) {
589 $updated = MainWP_Settings::handle_settings_post();
590 $updated |= MainWP_Backup_Handler::handle_settings_post();
591 $updated |= MainWP_Monitoring_Handler::handle_settings_post();
592 $msg = '';
593 if ( $updated ) {
594 $msg = '&message=saved';
595 }
596 wp_safe_redirect( admin_url( 'admin.php?page=Settings' . $msg ) );
597 exit();
598 }
599 }
600
601 if ( isset( $_POST['mainwp_sidebar_position'] ) && isset( $_POST['wp_nonce'] ) && wp_verify_nonce( sanitize_key( $_POST['wp_nonce'] ), 'onchange_sidebarposition' ) ) {
602 $val = isset( $_POST['mainwp_sidebar_position'] ) && $_POST['mainwp_sidebar_position'] ? 1 : 0;
603 $user = wp_get_current_user();
604 if ( $user ) {
605 update_user_option( $user->ID, 'mainwp_sidebarPosition', $val, true );
606 }
607 return true;
608 }
609
610 if ( isset( $_GET['viewmode'] ) && isset( $_GET['modenonce'] ) ) {
611 $viewmode = sanitize_text_field( wp_unslash( $_GET['viewmode'] ) );
612 $nonce = sanitize_key( $_GET['modenonce'] );
613 if ( ( 'table' === $viewmode || 'grid' === $viewmode ) && wp_verify_nonce( sanitize_key( $nonce ), 'viewmode' ) ) {
614 $user = wp_get_current_user();
615 if ( $user ) {
616 update_user_option( $user->ID, 'mainwp_sitesviewmode', $viewmode, true );
617 wp_safe_redirect( admin_url( 'admin.php?page=managesites' ) );
618 exit;
619 }
620 }
621 }
622 }
623
624 /**
625 * Method include_pluggable()
626 *
627 * Include pluggable functions.
628 */
629 public function include_pluggable() {
630 // may causing of conflict with Post S m t p plugin.
631 if ( ! function_exists( 'wp_create_nonce' ) ) {
632 include_once ABSPATH . WPINC . '/pluggable.php';
633 }
634 }
635
636 /**
637 * Method plugins_api_extension_info()
638 *
639 * Get MainWP Extension api information.
640 *
641 * @param mixed $false Return value.
642 * @param mixed $action Action being performed.
643 * @param mixed $arg Action arguments. Should be the plugin slug.
644 *
645 * @return mixed $info|$false
646 *
647 * @uses \MainWP\Dashboard\MainWP_API_Handler::get_update_information()
648 * @uses \MainWP\Dashboard\MainWP_Extensions_View::get_available_extensions()
649 * @uses \MainWP\Dashboard\MainWP_System::get_plugin_slug()
650 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_slugs()
651 */
652 public function plugins_api_extension_info( $false, $action, $arg ) {
653 if ( 'plugin_information' !== $action ) {
654 return $false;
655 }
656
657 if ( is_array( $arg ) ) {
658 $arg = (object) $arg;
659 }
660
661 if ( ! isset( $arg->slug ) || ( '' === $arg->slug ) ) {
662 return $false;
663 }
664
665 if ( dirname( MainWP_System::instance()->get_plugin_slug() ) === $arg->slug ) {
666 return $false;
667 }
668
669 $result = MainWP_Extensions_Handler::get_slugs();
670 $am_slugs = $result['am_slugs'];
671
672 if ( '' !== $am_slugs ) {
673 $am_slugs = explode( ',', $am_slugs );
674 if ( false !== strpos( $arg->slug, '/' ) ) { // to fix.
675 $dir_slug = dirname( $arg->slug );
676 } else {
677 $dir_slug = $arg->slug;
678 }
679 if ( in_array( $dir_slug, $am_slugs ) ) {
680 $info = MainWP_API_Handler::get_update_information( $dir_slug );
681 if ( is_object( $info ) && property_exists( $info, 'sections' ) ) {
682 if ( ! is_array( $info->sections ) || ! isset( $info->sections['changelog'] ) || empty( $info->sections['changelog'] ) ) {
683 $exts_data = MainWP_Extensions_View::get_available_extensions();
684 if ( isset( $exts_data[ $arg->slug ] ) ) {
685 $ext_info = $exts_data[ $arg->slug ];
686 $changelog_link = rtrim( $ext_info['link'], '/' );
687 $info->sections['changelog'] = '<a href="' . $changelog_link . '#tab-changelog" target="_blank">' . $changelog_link . '#tab-changelog</a>';
688 }
689 }
690 return $info;
691 }
692 return $info;
693 }
694 }
695
696 return $false;
697 }
698
699
700 /**
701 * Method plugins_api_wp_plugin_info()
702 *
703 * Get plugins api information.
704 *
705 * @param mixed $false Return value.
706 * @param mixed $action Action being performed.
707 * @param mixed $arg Action arguments. Should be the plugin slug.
708 *
709 * @return mixed $info|$false
710 */
711 public function plugins_api_wp_plugin_info( $false, $action, $arg ) {
712 if ( 'plugin_information' !== $action ) {
713 return $false;
714 }
715
716 if ( ! isset( $_GET['wpplugin'] ) || ! is_numeric( $_GET['wpplugin'] ) || empty( $_GET['wpplugin'] ) ) {
717 return $false;
718 }
719
720 if ( is_array( $arg ) ) {
721 $arg = (object) $arg;
722 }
723
724 if ( ! isset( $arg->slug ) || ( '' === $arg->slug ) ) {
725 return $false;
726 }
727
728 $site_id = intval( $_GET['wpplugin'] );
729
730 if ( $site_id ) {
731 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
732 if ( $website && ! empty( $website->plugin_upgrades ) ) {
733 $plugin_upgrades = json_decode( $website->plugin_upgrades, true );
734 if ( is_array( $plugin_upgrades ) ) {
735 foreach ( $plugin_upgrades as $plugin_slug => $info ) {
736 if ( false !== strpos( $plugin_slug, $arg->slug . '.php' ) && isset( $info['update'] ) ) {
737 if ( isset( $info['update']['slug'] ) && $arg->slug == $info['update']['slug'] && isset( $info['update']['new_version'] ) && ! empty( $info['update']['new_version'] ) && isset( $info['update']['sections'] ) && ! empty( $info['update']['sections'] ) ) {
738 $info_update = (object) $info['update'];
739 return $info_update;
740 }
741 break;
742 }
743 }
744 }
745 }
746 }
747
748 return $false;
749 }
750
751
752 /**
753 * Method check_update_custom()
754 *
755 * Check MainWP Extensions for updates.
756 *
757 * @param object $transient Transient information.
758 *
759 * @return object $transient Transient information.
760 *
761 * @uses \MainWP\Dashboard\MainWP_API_Handler::get_upgrade_information()
762 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_indexed_extensions_infor()
763 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_extension_slug()
764 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
765 */
766 public function check_update_custom( $transient ) { // phpcs:ignore -- Current complexity is the only way to achieve desired results, pull request solutions appreciated.
767 if ( isset( $_POST['action'] ) && ( ( 'update-plugin' === $_POST['action'] ) || ( 'update-selected' === $_POST['action'] ) ) && is_object( $transient ) && property_exists( $transient, 'response' ) ) {
768 $extensions = MainWP_Extensions_Handler::get_indexed_extensions_infor( array( 'activated' => true ) );
769 if ( defined( 'DOING_AJAX' ) && isset( $_POST['plugin'] ) && 'update-plugin' == $_POST['action'] ) {
770
771 if ( empty( $transient->response ) ) {
772 $transient->response = array();
773 }
774
775 $plugin_slug = sanitize_text_field( wp_unslash( $_POST['plugin'] ) );
776 if ( isset( $extensions[ $plugin_slug ] ) ) {
777
778 if ( ! isset( $transient->response[ $plugin_slug ] ) || ! is_object( $transient->response[ $plugin_slug ] ) || ! property_exists( $transient->response[ $plugin_slug ], 'new_version' ) ) {
779 return $transient;
780 }
781
782 if ( isset( $transient->response[ $plugin_slug ] ) && version_compare( $transient->response[ $plugin_slug ]->new_version, $extensions[ $plugin_slug ]['version'], '=' ) ) {
783 return $transient;
784 }
785
786 $api_slug = dirname( $plugin_slug );
787 $rslt = MainWP_API_Handler::get_upgrade_information( $api_slug );
788
789 if ( ! empty( $rslt ) && is_object( $rslt ) && property_exists( $rslt, 'new_version' ) && ! empty( $rslt->new_version ) && version_compare( $rslt->new_version, $extensions[ $plugin_slug ]['version'], '>' ) ) {
790 $transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt );
791 }
792
793 return $transient;
794 }
795 } elseif ( 'update-selected' === $_POST['action'] && isset( $_POST['checked'] ) && is_array( $_POST['checked'] ) ) {
796
797 if ( empty( $transient->response ) ) {
798 $transient->response = array();
799 }
800
801 $updated = false;
802 foreach ( wp_unslash( $_POST['checked'] ) as $plugin_slug ) {
803 if ( isset( $extensions[ $plugin_slug ] ) ) {
804
805 if ( ! isset( $transient->response[ $plugin_slug ] ) ) {
806 $transient->response[ $plugin_slug ] = new \stdClass();
807 }
808
809 if ( isset( $transient->response[ $plugin_slug ] ) && version_compare( $transient->response[ $plugin_slug ]->new_version, $extensions[ $plugin_slug ]['version'], '=' ) ) {
810 continue;
811 }
812 $api_slug = dirname( $plugin_slug );
813 $rslt = MainWP_API_Handler::get_upgrade_information( $api_slug );
814 if ( ! empty( $rslt ) && is_object( $rslt ) && property_exists( $rslt, 'new_version' ) && ! empty( $rslt->new_version ) && version_compare( $rslt->new_version, $extensions[ $plugin_slug ]['version'], '>' ) ) {
815 $this->upgradeVersionInfo->result[ $api_slug ] = $rslt;
816 $transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt );
817 $updated = true;
818 }
819 }
820 }
821 if ( $updated ) {
822 MainWP_Utility::update_option( 'mainwp_upgradeVersionInfo', $this->upgradeVersionInfo );
823 }
824
825 return $transient;
826 }
827 }
828
829 if ( empty( $transient->checked ) ) {
830 return $transient;
831 }
832
833 if ( null != $this->upgradeVersionInfo && property_exists( $this->upgradeVersionInfo, 'result' ) && is_array( $this->upgradeVersionInfo->result ) ) {
834 foreach ( $this->upgradeVersionInfo->result as $rslt ) {
835 if ( ! isset( $rslt->slug ) ) {
836 continue;
837 }
838
839 $plugin_slug = MainWP_Extensions_Handler::get_extension_slug( $rslt->slug );
840 if ( isset( $transient->checked[ $plugin_slug ] ) && property_exists( $rslt, 'new_version' ) && ! empty( $rslt->new_version ) && version_compare( $rslt->new_version, $transient->checked[ $plugin_slug ], '>' ) ) {
841 $transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt );
842 }
843 }
844 }
845
846 return $transient;
847 }
848
849
850 /**
851 * Method map_rslt_obj()
852 *
853 * Map resulting object.
854 *
855 * @param object $result Resulting information.
856 *
857 * @return object $obj Mapped resulting object.
858 */
859 public static function map_rslt_obj( $result ) {
860 $obj = new \stdClass();
861 $obj->slug = $result->slug;
862 $obj->new_version = $result->new_version;
863 $obj->url = 'https://mainwp.com/';
864 $obj->package = $result->package;
865
866 return $obj;
867 }
868
869 /**
870 * Method check_upgrade()
871 *
872 * Check if Extension has an update.
873 *
874 * @uses \MainWP\Dashboard\MainWP_API_Handler::check_exts_upgrade()
875 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
876 */
877 private function check_upgrade() {
878 $result = MainWP_API_Handler::check_exts_upgrade();
879 if ( null == $this->upgradeVersionInfo ) {
880 $this->upgradeVersionInfo = new \stdClass();
881 }
882 $this->upgradeVersionInfo->updated = time();
883 if ( ! empty( $result ) ) {
884 $this->upgradeVersionInfo->result = $result;
885 }
886 MainWP_Utility::update_option( 'mainwp_upgradeVersionInfo', $this->upgradeVersionInfo );
887 }
888
889 /**
890 * Method pre_check_update_custom()
891 *
892 * Pre-check for extension updates.
893 *
894 * @param object $transient Transient information.
895 *
896 * @return object $transient Transient information.
897 *
898 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_indexed_extensions_infor()
899 * @uses \MainWP\Dashboard\MainWP_Extensions_Handler::get_extension_slug()
900 */
901 public function pre_check_update_custom( $transient ) {
902 if ( ! isset( $transient->checked ) ) {
903 return $transient;
904 }
905
906 if ( ( null == $this->upgradeVersionInfo ) || ! property_exists( $this->upgradeVersionInfo, 'updated' ) || ( ( time() - $this->upgradeVersionInfo->updated ) > 60 ) ) {
907 $this->check_upgrade();
908 }
909
910 if ( empty( $transient->response ) ) {
911 $transient->response = array();
912 }
913
914 if ( null != $this->upgradeVersionInfo && property_exists( $this->upgradeVersionInfo, 'result' ) && is_array( $this->upgradeVersionInfo->result ) ) {
915 $extensions = MainWP_Extensions_Handler::get_indexed_extensions_infor( array( 'activated' => true ) );
916 foreach ( $this->upgradeVersionInfo->result as $rslt ) {
917 $plugin_slug = MainWP_Extensions_Handler::get_extension_slug( $rslt->slug );
918 if ( isset( $extensions[ $plugin_slug ] ) && property_exists( $rslt, 'new_version' ) && ! empty( $rslt->new_version ) && version_compare( $rslt->new_version, $extensions[ $plugin_slug ]['version'], '>' ) ) {
919 $transient->response[ $plugin_slug ] = self::map_rslt_obj( $rslt );
920 }
921 }
922 }
923
924 return $transient;
925 }
926
927 /**
928 * Method upload_file()
929 *
930 * Upload a file.
931 *
932 * @param mixed $file File to upload.
933 *
934 * @return void
935 *
936 * @uses \MainWP\Dashboard\MainWP_Utility::ends_with()
937 */
938 public function upload_file( $file ) {
939 header( 'Content-Description: File Transfer' );
940 if ( MainWP_Utility::ends_with( $file, '.tar.gz' ) ) {
941 header( 'Content-Type: application/x-gzip' );
942 header( 'Content-Encoding: gzip' );
943 } else {
944 header( 'Content-Type: application/octet-stream' );
945 }
946 header( 'Content-Disposition: attachment; filename="' . basename( $file ) . '"' );
947 header( 'Expires: 0' );
948 header( 'Cache-Control: must-revalidate' );
949 header( 'Pragma: public' );
950 header( 'Content-Length: ' . filesize( $file ) );
951 while ( ob_get_level() ) {
952 ob_end_clean();
953 }
954 $this->readfile_chunked( $file );
955 exit();
956 }
957
958 /**
959 * Method readfile_chunked()
960 *
961 * Read Chunked File.
962 *
963 * @param mixed $filename Name of file.
964 *
965 * @return mixed echo $buffer|false|$handle.
966 */
967 public function readfile_chunked( $filename ) {
968 $chunksize = 1024;
969 $handle = fopen( $filename, 'rb' );
970 if ( false === $handle ) {
971 return false;
972 }
973
974 while ( ! feof( $handle ) ) {
975 $buffer = fread( $handle, $chunksize );
976 echo $buffer;
977 if ( ob_get_length() ) {
978 ob_flush();
979 flush();
980 }
981 $buffer = null;
982 }
983
984 return fclose( $handle );
985 }
986
987 /**
988 * Method activate_redirection()
989 *
990 * Redirect after activating MainWP Extension.
991 *
992 * @param mixed $location Location to redirect to.
993 *
994 * @return $location Admin URL + the page to redirect to.
995 */
996 public function activate_redirect( $location ) {
997 $location = admin_url( 'admin.php?page=Extensions' );
998 return $location;
999 }
1000
1001 /**
1002 * Method activate_extension()
1003 *
1004 * Activate MainWP Extension.
1005 *
1006 * @param mixed $ext_key Extension API Key.
1007 * @param array $info Extension Info.
1008 *
1009 * @uses \MainWP\Dashboard\MainWP_Api_Manager::set_activation_info()
1010 * @uses \MainWP\Dashboard\MainWP_Api_Manager_Password_Management::generate_password()
1011 */
1012 public function activate_extension( $ext_key, $info = array() ) {
1013
1014 add_filter( 'wp_redirect', array( $this, 'activate_redirect' ) );
1015
1016 if ( is_array( $info ) && isset( $info['product_id'] ) && isset( $info['software_version'] ) ) {
1017 $act_info = array(
1018 'product_id' => $info['product_id'],
1019 'software_version' => $info['software_version'],
1020 'activated_key' => 'Deactivated',
1021 'instance_id' => MainWP_Api_Manager_Password_Management::generate_password( 12, false ),
1022 );
1023 MainWP_Api_Manager::instance()->set_activation_info( $ext_key, $act_info );
1024 }
1025 }
1026
1027 /**
1028 * Method deactivate_extension()
1029 *
1030 * Deactivate MaiNWP Extension.
1031 *
1032 * @param mixed $ext_key Exnension API Key.
1033 *
1034 * @uses \MainWP\Dashboard\MainWP_Api_Manager::set_activation_info()
1035 */
1036 public function deactivate_extension( $ext_key ) {
1037 // try to deactivate license.
1038 $mainwp_api_key = MainWP_Api_Manager_Key::instance()->get_decrypt_master_api_key();
1039 $result = MainWP_Api_Manager::instance()->license_key_deactivation( $ext_key, $mainwp_api_key );
1040
1041 MainWP_Api_Manager::instance()->remove_activation_info( $ext_key );
1042 }
1043
1044 }
1045