PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 4.6.0.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v4.6.0.1
6.2.2 6.2.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 All 155 releases
mainwp / pages / page-mainwp-extensions-handler.php

page-mainwp-extensions-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 4.6.0.1, at pages/page-mainwp-extensions-handler.php

1,218 lines 34.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Extensions Page Handler
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Extensions_Handler
12 */
13 class MainWP_Extensions_Handler {
14
15 // phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
16
17 /**
18 * Method get_class_name()
19 *
20 * Get Class Name.
21 *
22 * @return object
23 */
24 public static function get_class_name() {
25 return __CLASS__;
26 }
27
28 /**
29 * All extensions.
30 *
31 * @var array $extensions
32 */
33 public static $extensions;
34
35 /**
36 * All disabled extensions.
37 *
38 * @var array $extensions
39 */
40 public static $extensions_disabled;
41
42
43 /**
44 * Possible options.
45 *
46 * @var array $possible_options
47 */
48 private static $possible_options = array(
49 'plugin_upgrades',
50 'theme_upgrades',
51 'premium_upgrades',
52 'plugins',
53 'themes',
54 'dtsSync',
55 'version',
56 'sync_errors',
57 'ignored_plugins',
58 'wp_upgrades',
59 'site_info',
60 'client',
61 'signature_algo',
62 'verify_method',
63 'pubkey',
64 );
65
66
67 /**
68 * Get Extension Slug.
69 *
70 * @param mixed $slug Extension Slug.
71 *
72 * @return string Extensions Slug.
73 */
74 public static function get_extension_slug( $slug ) {
75 $currentExtensions = self::get_extensions();
76 if ( ! is_array( $currentExtensions ) || empty( $currentExtensions ) ) {
77 return $slug;
78 }
79
80 foreach ( $currentExtensions as $extension ) {
81 if ( isset( $extension['api'] ) && ( $extension['api'] === $slug ) ) {
82 return $extension['slug'];
83 }
84 }
85
86 return $slug;
87 }
88
89 /**
90 * Get all extension slugs.
91 *
92 * @return array am_slugs|slugs.
93 */
94 public static function get_slugs() {
95 $currentExtensions = self::get_extensions();
96
97 if ( empty( $currentExtensions ) ) {
98 return array(
99 'slugs' => '',
100 'am_slugs' => '',
101 );
102 }
103
104 $out = '';
105 $am_out = '';
106 foreach ( $currentExtensions as $extension ) {
107 if ( ! isset( $extension['api'] ) || '' === $extension['api'] ) {
108 continue;
109 }
110
111 if ( isset( $extension['apiManager'] ) && ! empty( $extension['apiManager'] ) && 'Activated' === $extension['activated_key'] ) {
112 if ( '' !== $am_out ) {
113 $am_out .= ',';
114 }
115 $am_out .= $extension['api'];
116 } else {
117 if ( '' !== $out ) {
118 $out .= ',';
119 }
120 $out .= $extension['api'];
121 }
122 }
123
124 return array(
125 'slugs' => $out,
126 'am_slugs' => $am_out,
127 );
128 }
129
130 /**
131 * Clean up MainWP Extention names.
132 *
133 * @param array $extension Array of MainWP Extentsions.
134 * @param array $forced forced polish.
135 *
136 * @return string $menu_name Final Menu Name.
137 */
138 public static function polish_ext_name( $extension, $forced = false ) {
139 if ( $forced || ( isset( $extension['mainwp'] ) && $extension['mainwp'] ) ) {
140 $menu_name = self::polish_string_name( $extension['name'] );
141 } else {
142 $menu_name = $extension['name'];
143 }
144 return $menu_name;
145 }
146
147 /**
148 * Clean up MainWP Extention names.
149 *
150 * @param string $name Extention name string.
151 *
152 * @return string $menu_name Final Name.
153 */
154 public static function polish_string_name( $name ) {
155 if ( false !== stripos( $name, 'for Mainwp' ) ) {
156 return $name; // skip.
157 }
158 $new_name = str_replace(
159 array(
160 'Extensions',
161 'Mainwp',
162 'Extension',
163 'MainWP',
164 ),
165 '',
166 $name
167 );
168 $new_name = trim( $new_name );
169 return $new_name;
170 }
171
172
173 /**
174 * Load MainWP Extensions.
175 *
176 * @param bool $forced Forced reload value.
177 *
178 * @return array Array of loaded Extensions.
179 */
180 public static function get_extensions( $forced = false ) {
181 if ( ! isset( self::$extensions ) || $forced ) {
182 self::$extensions = array();
183 $extensions = get_option( 'mainwp_extensions', array() );
184 foreach ( $extensions as $extension ) {
185 $slug = $extension['slug'];
186 if ( function_exists( 'mainwp_current_user_have_right' ) ) { // to fix later init, it's ok to check user have right.
187 if ( mainwp_current_user_have_right( 'extension', dirname( $slug ) ) ) {
188 self::$extensions[] = $extension;
189 }
190 } else {
191 self::$extensions[] = $extension;
192 }
193 }
194 }
195 return self::$extensions;
196 }
197
198
199 /**
200 * Get disabled MainWP Extensions.
201 *
202 * @param bool $compatible_api_response To get compatible api response values.
203 *
204 * @return array Array of disabled Extensions.
205 */
206 public static function get_extensions_disabled( $compatible_api_response = false ) {
207 if ( ! isset( self::$extensions_disabled ) || $compatible_api_response ) {
208 self::$extensions_disabled = array();
209 $all_available_extensions = MainWP_Extensions_View::get_available_extensions( 'all' );
210 $all_plugins = get_plugins();
211
212 $exts_disabled = array();
213
214 if ( $all_plugins ) {
215 foreach ( $all_plugins as $plugin => $plugin_data ) {
216 if ( is_plugin_active( $plugin ) ) {
217 continue;
218 }
219 $slug = dirname( $plugin );
220 if ( isset( $all_available_extensions[ $slug ] ) ) {
221
222 $ext = $all_available_extensions[ $slug ];
223
224 $extension = array();
225
226 $extension['name'] = $plugin_data['Name'];
227 $extension['slug'] = $plugin;
228 $extension['version'] = $plugin_data['Version'];
229 $extension['description'] = $plugin_data['Description'];
230 $extension['author'] = $plugin_data['Author'];
231 $extension['img'] = isset( $ext['img'] ) ? $ext['img'] : '';
232 $extension['iconURI'] = isset( $plugin_data['IconURI'] ) ? $plugin_data['IconURI'] : '';
233 $extension['SupportForumURI'] = '';
234 $extension['DocumentationURI'] = '';
235 $extension['page'] = 'Extensions-' . str_replace( ' ', '-', ucwords( str_replace( '-', ' ', dirname( $slug ) ) ) );
236
237 $extension['api_key'] = '';
238 $extension['activated_key'] = 'Deactivated';
239 $extension['deactivate_checkbox'] = 'off';
240 $extension['product_id'] = $ext['product_id'];
241 $extension['instance_id'] = '';
242 $extension['software_version'] = '';
243 $extension['product_item_id'] = '';
244 $extension['type'] = $ext['type'];
245
246 if ( $compatible_api_response ) {
247 $exts_disabled[ $ext['product_id'] ] = $extension;
248 } else {
249 $exts_disabled[] = $extension;
250 }
251 }
252 }
253 }
254
255 if ( $compatible_api_response ) {
256 return $exts_disabled;
257 } else {
258 self::$extensions_disabled = $exts_disabled;
259 }
260 }
261 return self::$extensions_disabled;
262 }
263
264 /**
265 * Get MainWP Extensions infor array.
266 *
267 * @param array $args Empty Array.
268 *
269 * @return array Array of Extensions.
270 */
271 public static function get_indexed_extensions_infor( $args = array() ) {
272 if ( ! is_array( $args ) ) {
273 $args = array();
274 }
275 $extensions = self::get_extensions();
276 $return = array();
277 foreach ( $extensions as $extension ) {
278 if ( isset( $args['activated'] ) && ! empty( $args['activated'] ) ) {
279 if ( isset( $extension['apiManager'] ) && $extension['apiManager'] ) {
280 if ( ! isset( $extension['activated_key'] ) || 'Activated' !== $extension['activated_key'] ) {
281 continue;
282 }
283 }
284 }
285 $ext = array();
286 $ext['version'] = $extension['version'];
287 $ext['name'] = $extension['name'];
288 $ext['page'] = $extension['page'];
289 if ( isset( $extension['activated_key'] ) && 'Activated' === $extension['activated_key'] ) {
290 $ext['activated_key'] = 'Activated';
291 }
292 $return[ $extension['slug'] ] = $ext;
293 }
294 return $return;
295 }
296
297 /**
298 * Generate API Password.
299 *
300 * @param integer $length Lenght of password.
301 * @param bool $special_chars true|false, allow special characters.
302 * @param bool $extra_special_chars true|false, allow extra special characters.
303 *
304 * @return MainWP_Api_Manager_Password_Management::generate_password()
305 */
306 public static function gen_api_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
307 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
308 return MainWP_Api_Manager_Password_Management::generate_password( $length, $special_chars, $extra_special_chars );
309 }
310
311
312 /**
313 * Add Extension Menu.
314 *
315 * @param mixed $slug Extension slug.
316 *
317 * @return boolean true|false.
318 *
319 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
320 */
321 public static function add_extension_menu( $slug ) {
322 $snMenuExtensions = get_option( 'mainwp_extmenu' );
323 if ( ! is_array( $snMenuExtensions ) ) {
324 $snMenuExtensions = array();
325 }
326
327 $snMenuExtensions[] = $slug;
328
329 MainWP_Utility::update_option( 'mainwp_extmenu', $snMenuExtensions );
330
331 /**
332 * Adds Extension to the navigation menu
333 *
334 * Adds Extension instance to the Extensions located in the main MainWP navigation menu.
335 *
336 * @param string $slug Extension slug.
337 *
338 * @since 4.0
339 */
340 do_action( 'mainwp_added_extension_menu', $slug );
341
342 return true;
343 }
344
345 /**
346 * HTTP Request Reject Unsafe Urls.
347 *
348 * @param bool $args args.
349 *
350 * @return mixed args.
351 */
352 public static function http_request_reject_unsafe_urls( $args ) {
353 $args['reject_unsafe_urls'] = false;
354
355 return $args;
356 }
357
358 /**
359 * No SSL Filter Function.
360 *
361 * @param bool $args args.
362 *
363 * @return mixed args.
364 */
365 public static function no_ssl_filter_function( $args ) {
366 $args['sslverify'] = false;
367 return $args;
368 }
369
370 /**
371 * No SSL Filter Extention Upgrade.
372 *
373 * @param bool $r Results.
374 * @param mixed $url Upgrade Extension URL.
375 *
376 * @return mixed false|$r.
377 */
378 public static function no_ssl_filter_extension_upgrade( $r, $url ) {
379 if ( ( false !== strpos( $url, 'am_download_file=' ) ) && ( false !== strpos( $url, 'am_email=' ) ) ) {
380 $r['sslverify'] = false;
381 }
382
383 return $r;
384 }
385
386 /**
387 * Install MainWP Extension.
388 *
389 * @param mixed $url MainWP Extension update URL.
390 * @param bool $activatePlugin true|false Whether or not to activate extension.
391 *
392 * @return mixed $return
393 *
394 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
395 */
396 public static function install_plugin( $url, $activatePlugin = false ) {
397
398 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
399
400 /**
401 * WordPress files system object.
402 *
403 * @global object
404 */
405 global $wp_filesystem;
406
407 if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
408 include_once ABSPATH . '/wp-admin/includes/screen.php';
409 }
410
411 include_once ABSPATH . '/wp-admin/includes/template.php';
412 include_once ABSPATH . '/wp-admin/includes/misc.php';
413 include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
414 include_once ABSPATH . '/wp-admin/includes/plugin.php';
415
416 $installer = new \WP_Upgrader();
417 $ssl_verifyhost = get_option( 'mainwp_sslVerifyCertificate' );
418 $ssl_api_verifyhost = ( ( false === get_option( 'mainwp_api_sslVerifyCertificate' ) ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
419
420 if ( empty( $ssl_api_verifyhost ) ) {
421 add_filter( 'http_request_args', array( self::get_class_name(), 'no_ssl_filter_function' ), 99, 2 );
422 }
423
424 add_filter( 'http_request_args', array( self::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
425
426 $result = $installer->run(
427 array(
428 'package' => $url,
429 'destination' => WP_PLUGIN_DIR,
430 'clear_destination' => false,
431 'clear_working' => true,
432 'hook_extra' => array(),
433 )
434 );
435
436 remove_filter( 'http_request_args', array( self::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
437
438 if ( '0' === $ssl_verifyhost ) {
439 remove_filter( 'http_request_args', array( self::get_class_name(), 'no_ssl_filter_function' ), 99 );
440 }
441
442 $error = null;
443 $output = null;
444 $plugin_slug = null;
445
446 if ( is_wp_error( $result ) ) {
447 $error_code = $result->get_error_code();
448 if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) {
449 $error = $error_code . ' - ' . $result->get_error_data();
450 } else {
451 $error = $error_code;
452 }
453 } else {
454 $path = $result['destination'];
455
456 foreach ( $result['source_files'] as $srcFile ) {
457
458 if ( 'readme.txt' === $srcFile ) {
459 continue;
460 }
461
462 $thePlugin = get_plugin_data( $path . $srcFile );
463
464 if ( null !== $thePlugin && '' !== $thePlugin && '' !== $thePlugin['Name'] ) {
465 $the_name = self::polish_string_name( $thePlugin['Name'] );
466 $output .= esc_html( $the_name ) . ' ' . esc_html__( 'installed successfully. Do not forget to activate the extension API license.', 'mainwp' );
467 $plugin_slug = $result['destination_name'] . '/' . $srcFile;
468
469 if ( $activatePlugin ) {
470 activate_plugin( $path . $srcFile, '', false, true );
471
472 /**
473 * Extension API activation
474 *
475 * Activates the extension API license upon the extension installation.
476 *
477 * @since Unknown
478 * @ignore
479 */
480 do_action( 'mainwp_api_extension_activated', $path . $srcFile );
481 }
482
483 break;
484 }
485 }
486 }
487
488 if ( ! empty( $error ) ) {
489 $return['error'] = $error;
490 } else {
491 $return['result'] = 'SUCCESS';
492 $return['output'] = $output;
493 $return['slug'] = esc_html( $plugin_slug );
494 }
495
496 return $return;
497 }
498
499 /**
500 * Check if MainWP Extension is available.
501 *
502 * @param mixed $pAPI MainWP Extension API Key.
503 *
504 * @return boolean true|false.
505 */
506 public static function is_extension_available( $pAPI ) {
507
508 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
509
510 $extensions = self::get_extensions();
511 if ( isset( $extensions ) && is_array( $extensions ) ) {
512 foreach ( $extensions as $extension ) {
513 $slug = dirname( $extension['slug'] );
514 if ( $slug === $pAPI ) {
515 return true;
516 }
517 }
518 }
519 return false;
520 }
521
522 /**
523 * Check if MainWP Extension is enabled.
524 *
525 * @param mixed $pluginFile MainWP Extension to bo verified.
526 *
527 * @return array 'key' => md5( $pluginFile . '-SNNonceAdder').
528 */
529 public static function is_extension_enabled( $pluginFile ) {
530 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
531 return array( 'key' => md5( $pluginFile . '-SNNonceAdder' ) );
532 }
533
534 /**
535 * Create Menu Extension Array.
536 *
537 * @param mixed $slug menu slug.
538 *
539 * @return array Menu Array.
540 */
541 public static function added_on_menu( $slug ) {
542 $snMenuExtensions = get_option( 'mainwp_extmenu' );
543 if ( ! is_array( $snMenuExtensions ) ) {
544 $snMenuExtensions = array();
545 }
546 return in_array( $slug, $snMenuExtensions );
547 }
548
549 /**
550 * Check if MainWP Extension is activated or not.
551 *
552 * @param mixed $plugin_slug MainWP Extension slug.
553 *
554 * @return boolean true|false.
555 */
556 public static function is_extension_activated( $plugin_slug ) {
557 $extensions = self::get_indexed_extensions_infor( array( 'activated' => true ) );
558 return isset( $extensions[ $plugin_slug ] ) ? true : false;
559 }
560
561 /**
562 * Verify MainWP Extension.
563 *
564 * @param mixed $pluginFile MainWP Extensoin to verify.
565 * @param mixed $key Child Site Key.
566 *
567 * @return mixed md5( $pluginFile . '-SNNonceAdder' ) === $key
568 */
569 public static function hook_verify( $pluginFile, $key ) {
570 return ( md5( $pluginFile . '-SNNonceAdder' ) === $key );
571 }
572
573 /**
574 * Get sql websites for current user.
575 *
576 * @param mixed $pluginFile Extension plugin file to verify.
577 * @param mixed $key PThe child-key.
578 *
579 * @return mixed null|sql query.
580 *
581 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid()
582 */
583 public static function hook_get_dashboard_sites( $pluginFile, $key ) {
584 if ( ! self::hook_verify( $pluginFile, $key ) ) {
585 return null;
586 }
587
588 $current_wpid = MainWP_System_Utility::get_current_wpid();
589
590 if ( $current_wpid ) {
591 $sql = MainWP_DB::instance()->get_sql_website_by_id( $current_wpid );
592 } else {
593 $sql = MainWP_DB::instance()->get_sql_websites_for_current_user();
594 }
595
596 return MainWP_DB::instance()->query( $sql );
597 }
598
599 /**
600 * Fetch Authorized URLS.
601 *
602 * @param mixed $pluginFile Extension plugin file to verify.
603 * @param string $key The child key.
604 * @param object $dbwebsites The websites.
605 * @param string $what Action to perorm.
606 * @param mixed $params Request parameters.
607 * @param mixed $handle Request handle.
608 * @param mixed $output Request output.
609 *
610 * @uses MainWP_Connect::fetch_urls_authed()
611 *
612 * @return mixed false|MainWP_Connect::fetch_urls_authed()
613 */
614 public static function hook_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output ) {
615 if ( ! self::hook_verify( $pluginFile, $key ) ) {
616 return false;
617 }
618
619 return MainWP_Connect::fetch_urls_authed( $dbwebsites, $what, $params, $handle, $output );
620 }
621
622 /**
623 * Fetch Authorized URL.
624 *
625 * @throws MainWP_Exception On incorrect website.
626 * @param mixed $pluginFile Extension plugin file to verify.
627 * @param mixed $key The child-key.
628 * @param mixed $websiteId Child Site ID.
629 * @param mixed $what What.
630 * @param mixed $params Parameters.
631 * @param null $rawResponse Raw responce.
632 *
633 * @return mixed false|throw|error
634 *
635 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
636 */
637 public static function hook_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $rawResponse = null ) {
638 if ( ! self::hook_verify( $pluginFile, $key ) ) {
639 return false;
640 }
641
642 try {
643 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
644 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
645 throw new MainWP_Exception( 'You can not edit this website.' );
646 }
647
648 return MainWP_Connect::fetch_url_authed( $website, $what, $params, $checkConstraints = false, $pForceFetch = false, $pRetryFailed = true, $rawResponse );
649 } catch ( MainWP_Exception $e ) {
650 return array(
651 'error' => MainWP_Error_Helper::get_error_message( $e ),
652 'errorCode' => 'excep_fetch_url_authed',
653 );
654 }
655 }
656
657 /**
658 * Get DB Sites.
659 *
660 * @param mixed $pluginFile Extension plugin file to verify.
661 * @param mixed $key The child-key.
662 * @param mixed $sites Child Sites.
663 * @param string $groups Groups.
664 * @param bool $options Options.
665 *
666 * @return array $dbwebsites.
667 *
668 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
669 * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
670 */
671 public static function hook_get_db_sites( $pluginFile, $key, $sites, $groups = '', $options = false ) {
672 if ( ! self::hook_verify( $pluginFile, $key ) ) {
673 return false;
674 }
675
676 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
677
678 $dbwebsites = array();
679
680 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
681 $data_fields[] = 'verify_certificate';
682
683 if ( is_array( $options ) ) {
684 foreach ( $options as $option_name => $value ) {
685 if ( ! empty( $value ) && in_array( $option_name, self::$possible_options ) ) {
686 if ( ! in_array( $option_name, $data_fields ) ) {
687 $data_fields[] = $option_name;
688 }
689 }
690 }
691 }
692
693 if ( '' !== $sites ) {
694 foreach ( $sites as $k => $v ) {
695 if ( MainWP_Utility::ctype_digit( $v ) ) {
696 $website = MainWP_DB::instance()->get_website_by_id( $v );
697 if ( empty( $website ) ) {
698 continue;
699 }
700 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
701 }
702 }
703 }
704
705 if ( is_array( $groups ) ) {
706 foreach ( $groups as $k => $v ) {
707 if ( MainWP_Utility::ctype_digit( $v ) ) {
708 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
709 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
710 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
711 }
712 MainWP_DB::free_result( $websites );
713 }
714 }
715 }
716
717 return $dbwebsites;
718 }
719
720
721 /**
722 * Get DB Sites.
723 *
724 * @since 4.4.2
725 *
726 * @param mixed $pluginFile Extension plugin file to verify.
727 * @param mixed $key The child-key.
728 * @param mixed $params params.
729 *
730 * @return array $dbwebsites.
731 */
732 public static function hook_get_db_websites( $pluginFile, $key, $params = array() ) {
733 if ( ! self::hook_verify( $pluginFile, $key ) ) {
734 return false;
735 }
736
737 if ( empty( $params ) || ! is_array( $params ) ) {
738 return false;
739 }
740
741 $dbwebsites = array();
742
743 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
744 $data_fields[] = 'verify_certificate';
745
746 $fields = isset( $params['fields'] ) && is_array( $params['fields'] ) ? $params['fields'] : array();
747 $sites = isset( $params['sites'] ) && is_array( $params['sites'] ) ? $params['sites'] : array();
748 $groups = isset( $params['groups'] ) && is_array( $params['groups'] ) ? $params['groups'] : array();
749 $clients = isset( $params['clients'] ) && is_array( $params['clients'] ) ? $params['clients'] : array();
750
751 if ( is_array( $fields ) ) {
752 foreach ( $fields as $field ) {
753 if ( in_array( $field, self::$possible_options ) ) {
754 if ( ! in_array( $field, $data_fields ) ) {
755 $data_fields[] = $field;
756 }
757 }
758 }
759 }
760
761 if ( ! empty( $sites ) ) {
762 foreach ( $sites as $k => $v ) {
763 if ( MainWP_Utility::ctype_digit( $v ) ) {
764 $website = MainWP_DB::instance()->get_website_by_id( $v );
765 if ( empty( $website ) ) {
766 continue;
767 }
768 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
769 }
770 }
771 }
772
773 if ( ! empty( $groups ) ) {
774 foreach ( $groups as $k => $v ) {
775 if ( MainWP_Utility::ctype_digit( $v ) ) {
776 $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_by_group_id( $v ) );
777 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
778 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
779 }
780 MainWP_DB::free_result( $websites );
781 }
782 }
783 }
784
785 $client_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $clients );
786 if ( $client_sites ) {
787 foreach ( $client_sites as $website ) {
788 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
789 }
790 }
791
792 return $dbwebsites;
793 }
794
795
796 /**
797 * Get Sites.
798 *
799 * @param string $pluginFile Extension plugin file to verify.
800 * @param string $key The child-key.
801 * @param int $websiteid The id of the child site you wish to retrieve.
802 * @param bool $for_manager Check Team Control.
803 * @param array $others Array of others.
804 *
805 * @return array $output Array of content to output.
806 *
807 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
808 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
809 */
810 public static function hook_get_sites( $pluginFile, $key, $websiteid = null, $for_manager = false, $others = array() ) { // phpcs:ignore -- not quite complex function.
811 if ( ! self::hook_verify( $pluginFile, $key ) ) {
812 return false;
813 }
814
815 if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! mainwp_current_user_have_right( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
816 return false;
817 }
818
819 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
820
821 if ( ! is_array( $others ) ) {
822 $others = array();
823 }
824
825 $search_site = null;
826 $orderBy = 'wp.url';
827 $offset = false;
828 $rowcount = false;
829 $extraWhere = null;
830
831 if ( isset( $websiteid ) && ( null !== $websiteid ) ) {
832 $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
833
834 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
835 return false;
836 }
837
838 if ( ! mainwp_current_user_have_right( 'site', $websiteid ) ) {
839 return false;
840 }
841
842 return array(
843 array(
844 'id' => $websiteid,
845 'url' => MainWP_Utility::get_nice_url( $website->url, true ),
846 'name' => $website->name,
847 'totalsize' => $website->totalsize,
848 'sync_errors' => $website->sync_errors,
849 ),
850 );
851 } else {
852 if ( isset( $others['orderby'] ) ) {
853 if ( ( 'site' === $others['orderby'] ) ) {
854 $orderBy = 'wp.name ' . ( 'asc' === $others['order'] ? 'asc' : 'desc' );
855 } elseif ( ( 'url' === $others['orderby'] ) ) {
856 $orderBy = 'wp.url ' . ( 'asc' === $others['order'] ? 'asc' : 'desc' );
857 }
858 }
859 if ( isset( $others['search'] ) ) {
860 $search_site = trim( $others['search'] );
861 }
862
863 if ( is_array( $others ) ) {
864 if ( isset( $others['plugins_slug'] ) ) {
865
866 $slugs = explode( ',', $others['plugins_slug'] );
867 $extraWhere = '';
868 foreach ( $slugs as $slug ) {
869 $slug = wp_json_encode( $slug );
870 $slug = trim( $slug, '"' );
871 $slug = str_replace( '\\', '.', $slug );
872 $extraWhere .= ' wp.plugins REGEXP "' . $slug . '" OR';
873 }
874 $extraWhere = trim( rtrim( $extraWhere, 'OR' ) );
875
876 if ( '' === $extraWhere ) {
877 $extraWhere = null;
878 } else {
879 $extraWhere = '(' . $extraWhere . ')';
880 }
881 }
882 }
883 }
884
885 $totalRecords = '';
886
887 if ( isset( $others['per_page'] ) && ! empty( $others['per_page'] ) ) {
888 $sql = MainWP_DB::instance()->get_sql_websites_for_current_user( false, $search_site, $orderBy, false, false, $extraWhere, $for_manager );
889 $websites_total = MainWP_DB::instance()->query( $sql );
890 $totalRecords = ( $websites_total ? MainWP_DB::num_rows( $websites_total ) : 0 );
891
892 if ( $websites_total ) {
893 MainWP_DB::free_result( $websites_total );
894 }
895
896 $rowcount = absint( $others['per_page'] );
897 $pagenum = isset( $others['paged'] ) ? absint( $others['paged'] ) : 0;
898 if ( $pagenum > $totalRecords ) {
899 $pagenum = $totalRecords;
900 }
901 $pagenum = max( 1, $pagenum );
902 $offset = ( $pagenum - 1 ) * $rowcount;
903
904 }
905
906 $sql = MainWP_DB::instance()->get_sql_websites_for_current_user( false, $search_site, $orderBy, $offset, $rowcount, $extraWhere, $for_manager );
907 $websites = MainWP_DB::instance()->query( $sql );
908
909 $output = array();
910 while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
911 $re = array(
912 'id' => $website->id,
913 'url' => MainWP_Utility::get_nice_url( $website->url, true ),
914 'name' => $website->name,
915 'totalsize' => $website->totalsize,
916 'sync_errors' => $website->sync_errors,
917 );
918
919 if ( 0 < $totalRecords ) {
920 $re['totalRecords'] = $totalRecords;
921 $totalRecords = 0;
922 }
923
924 $output[] = $re;
925 }
926 MainWP_DB::free_result( $websites );
927
928 return $output;
929 }
930
931 /**
932 * Method hook_get_groups()
933 *
934 * Get Child Sites within groups & store them in an array.
935 *
936 * @param string $pluginFile Extension plugin file to verify.
937 * @param string $key The child-key.
938 * @param int $groupid The id of the group you wish to retrieve.
939 * @param bool $for_manager Check Team Control.
940 *
941 * @return array|bool $output|false An array of arrays, the inner-array contains the id/name/array of site ids for the supplied groupid/all groups. False when something goes wrong.
942 */
943 public static function hook_get_groups( $pluginFile, $key, $groupid, $for_manager = false ) {
944 if ( ! self::hook_verify( $pluginFile, $key ) ) {
945 return false;
946 }
947
948 if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! mainwp_current_user_have_right( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
949 return false;
950 }
951
952 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
953
954 if ( isset( $groupid ) ) {
955 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
956 if ( empty( $group ) ) {
957 return false;
958 }
959
960 $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
961 $websitesOut = array();
962 foreach ( $websites as $website ) {
963 $websitesOut[] = $website->id;
964 }
965
966 return array(
967 array(
968 'id' => $groupid,
969 'name' => $group->name,
970 'websites' => $websitesOut,
971 ),
972 );
973 }
974
975 $groups = MainWP_DB_Common::instance()->get_groups_and_count( null, $for_manager );
976 $output = array();
977 foreach ( $groups as $group ) {
978 $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
979 $websitesOut = array();
980 foreach ( $websites as $website ) {
981 if ( in_array( $website->id, $websitesOut ) ) {
982 continue;
983 }
984 $websitesOut[] = $website->id;
985 }
986 $output[] = array(
987 'id' => $group->id,
988 'name' => $group->name,
989 'websites' => $websitesOut,
990 );
991 }
992
993 return $output;
994 }
995
996
997 /**
998 * Get all loaded extensions.
999 *
1000 * @return mainwp_extensions value.
1001 */
1002 public static function hook_get_all_extensions() {
1003 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
1004 return get_option( 'mainwp_extensions' );
1005 }
1006
1007 /**
1008 * Clone Site.
1009 *
1010 * @param mixed $pluginFile Extension plugin file to verify.
1011 * @param mixed $key The child-key.
1012 * @param mixed $websiteid Child Site ID.
1013 * @param mixed $cloneID Clone ID.
1014 * @param mixed $clone_url URL to CLone to.
1015 * @param bool $force_update true|false, force an update.
1016 *
1017 * @return mixed false|$ret
1018 *
1019 * @uses \MainWP\Dashboard\MainWP_Sync::sync_site()
1020 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
1021 * @uses \MainWP\Dashboard\MainWP_Utility::remove_http_www_prefix()
1022 */
1023 public static function hook_clone_site( $pluginFile, $key, $websiteid, $cloneID, $clone_url, $force_update = false ) {
1024 if ( ! self::hook_verify( $pluginFile, $key ) ) {
1025 return false;
1026 }
1027
1028 if ( ! empty( $websiteid ) && ! empty( $cloneID ) ) {
1029
1030 $sql = MainWP_DB::instance()->get_sql_website_by_id( $websiteid );
1031 $websites = MainWP_DB::instance()->query( $sql );
1032 $website = MainWP_DB::fetch_object( $websites );
1033
1034 if ( empty( $website ) ) {
1035 return array( 'error' => esc_html__( 'Website not found.', 'mainwp' ) );
1036 }
1037
1038 $ret = array();
1039
1040 if ( '/' !== substr( $clone_url, - 1 ) ) {
1041 $clone_url .= '/';
1042 }
1043
1044 $tmp1 = MainWP_Utility::remove_http_www_prefix( $website->url );
1045 $tmp2 = MainWP_Utility::remove_http_www_prefix( $clone_url );
1046
1047 if ( false === strpos( $tmp2, $tmp1 ) ) {
1048 return false;
1049 }
1050
1051 $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
1052 if ( $clone_sites ) {
1053 $clone_site = current( $clone_sites );
1054 if ( $clone_site && $clone_site->is_staging ) {
1055 if ( $force_update ) {
1056 MainWP_DB::instance()->update_website_values(
1057 $clone_site->id,
1058 array(
1059 'adminname' => $website->adminname,
1060 'pubkey' => $website->pubkey,
1061 'privkey' => $website->privkey,
1062 'verify_certificate' => $website->verify_certificate,
1063 'uniqueId' => ( null !== $website->uniqueId ? $website->uniqueId : '' ),
1064 'http_user' => $website->http_user,
1065 'http_pass' => $website->http_pass,
1066 'ssl_version' => $website->ssl_version,
1067 )
1068 );
1069 }
1070 $ret['siteid'] = $clone_site->id;
1071 $ret['response'] = esc_html__( 'Site updated.', 'mainwp' );
1072 }
1073 return $ret;
1074 }
1075 $clone_name = $website->name . ' - ' . $cloneID;
1076
1077 /**
1078 * Current user global.
1079 *
1080 * @global string
1081 */
1082 global $current_user;
1083
1084 $id = MainWP_DB::instance()->add_website( $current_user->ID, $clone_name, $clone_url, $website->adminname, $website->pubkey, $website->privkey, array(), array(), $website->verify_certificate, ( null !== $website->uniqueId ? $website->uniqueId : '' ), $website->http_user, $website->http_pass, $website->ssl_version, $website->wpe, $isStaging = 1 );
1085
1086 /** This action is documented in class\class-mainwp-manage-sites-view.php */
1087 do_action( 'mainwp_added_new_site', $id, $website );
1088
1089 if ( $id ) {
1090 $group_id = get_option( 'mainwp_stagingsites_group_id' );
1091 if ( $group_id ) {
1092 $website = MainWP_DB::instance()->get_website_by_id( $id );
1093 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
1094 MainWP_Sync::sync_site( $website, false, false );
1095 $group = MainWP_DB_Common::instance()->get_group_by_id( $group_id );
1096 if ( ! empty( $group ) ) {
1097 MainWP_DB_Common::instance()->update_group_site( $group->id, $id );
1098 }
1099 }
1100 }
1101 $ret['response'] = esc_html__( 'Site successfully added.', 'mainwp' );
1102 $ret['siteid'] = $id;
1103 }
1104 return $ret;
1105 }
1106
1107 return false;
1108 }
1109
1110 /**
1111 * Delete Clones Site.
1112 *
1113 * @param mixed $pluginFile Extension plugin file to verify.
1114 * @param mixed $key The child-key.
1115 * @param mixed $clone_url URL to Clone to.
1116 * @param bool $clone_site_id Cloned Site ID.
1117 *
1118 * @return mixed false|array Array => "Success".
1119 *
1120 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
1121 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_icons_dir()
1122 */
1123 public static function hook_delete_clone_site( $pluginFile, $key, $clone_url = '', $clone_site_id = false ) {
1124 if ( ! self::hook_verify( $pluginFile, $key ) ) {
1125 return false;
1126 }
1127
1128 if ( ( empty( $clone_url ) && empty( $clone_site_id ) ) ) {
1129 return false;
1130 }
1131
1132 $clone_site = null;
1133 if ( ! empty( $clone_url ) ) {
1134 if ( '/' !== substr( $clone_url, - 1 ) ) {
1135 $clone_url .= '/';
1136 }
1137 $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
1138 if ( ! empty( $clone_sites ) ) {
1139 $clone_site = current( $clone_sites );
1140
1141 }
1142 } elseif ( ! empty( $clone_site_id ) ) {
1143 $sql = MainWP_DB::instance()->get_sql_website_by_id( $clone_site_id );
1144 $websites = MainWP_DB::instance()->query( $sql );
1145 $clone_site = MainWP_DB::fetch_object( $websites );
1146 }
1147
1148 if ( empty( $clone_site ) ) {
1149 return array( 'error' => esc_html__( 'Not found the clone website', 'mainwp' ) );
1150 }
1151
1152 if ( $clone_site ) {
1153 if ( empty( $clone_site->is_staging ) ) {
1154 return false;
1155 }
1156
1157 $hasWPFileSystem = MainWP_System_Utility::get_wp_file_system();
1158
1159 /**
1160 * WordPress files system object.
1161 *
1162 * @global object
1163 */
1164 global $wp_filesystem;
1165
1166 $favi = MainWP_DB::instance()->get_website_option( $clone_site, 'favi_icon', '' );
1167 if ( ! empty( $favi ) && ( false !== strpos( $favi, 'favi-' . $clone_site->id . '-' ) ) ) {
1168 $dirs = MainWP_System_Utility::get_icons_dir();
1169 if ( $wp_filesystem->exists( $dirs[0] . $favi ) ) {
1170 $wp_filesystem->delete( $dirs[0] . $favi );
1171 }
1172 }
1173
1174 MainWP_DB::instance()->remove_website( $clone_site->id );
1175
1176 /** This action is documented in pages\page-mainwp-manage-sites-handler.php */
1177 do_action( 'mainwp_delete_site', $clone_site );
1178 return array( 'result' => 'SUCCESS' );
1179 }
1180
1181 return false;
1182 }
1183
1184 /**
1185 * Add Groups.
1186 *
1187 * @param mixed $pluginFile Extension plugin file to verify.
1188 * @param mixed $key The child-key.
1189 * @param mixed $newName Name that you want to give the group.
1190 *
1191 * @return mixed false|$groupId
1192 *
1193 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::check_group_name()
1194 */
1195 public static function hook_add_group( $pluginFile, $key, $newName ) {
1196
1197 if ( ! self::hook_verify( $pluginFile, $key ) ) {
1198 return false;
1199 }
1200
1201 /**
1202 * Current user global.
1203 *
1204 * @global string
1205 */
1206 global $current_user;
1207
1208 if ( ! empty( $newName ) ) {
1209 $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, MainWP_Manage_Groups::check_group_name( $newName ) );
1210
1211 /** This action is documented in pages\page-mainwp-manage-groups.php */
1212 do_action( 'mainwp_added_new_group', $groupId );
1213 return $groupId;
1214 }
1215 return false;
1216 }
1217 }
1218