PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
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 / pages / page-mainwp-extensions-handler.php

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

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