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

1,064 lines 37.1 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 { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
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 = static::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 = static::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 = static::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( static::$extensions ) || $forced ) {
157 static::$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 static::$extensions[] = $extension;
164 }
165 } else {
166 static::$extensions[] = $extension;
167 }
168 }
169 }
170 return static::$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 ) { // phpcs:ignore -- NOSONAR - complex.
182 if ( ! isset( static::$extensions_disabled ) || $compatible_api_response ) {
183 static::$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 static::$extensions_disabled = $exts_disabled;
234 }
235 }
236 return static::$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 ) { // phpcs:ignore -- NOSONAR - complex.
281 if ( ! is_array( $args ) ) {
282 $args = array();
283 }
284 $extensions = static::get_extensions();
285 $return = array();
286 foreach ( $extensions as $extension ) {
287 if ( ( isset( $args['activated'] ) && ! empty( $args['activated'] ) && isset( $extension['apiManager'] ) && $extension['apiManager'] ) && ( ! isset( $extension['activated_key'] ) || 'Activated' !== $extension['activated_key'] ) ) {
288 continue;
289 }
290 $apiManager = isset( $extension['apiManager'] ) && $extension['apiManager'] ? true : false;
291 $ext = array();
292 $ext['version'] = $extension['version'];
293 $ext['apiManager'] = $apiManager;
294 $ext['name'] = $extension['name'];
295 $ext['page'] = $extension['page'];
296 $ext['mainwp_version'] = isset( $extension['mainwp_version'] ) ? $extension['mainwp_version'] : '';
297
298 if ( isset( $extension['activated_key'] ) && 'Activated' === $extension['activated_key'] ) {
299 $ext['activated_key'] = 'Activated';
300 }
301
302 if ( null !== $deactivated_license && ( ( $deactivated_license && $apiManager && isset( $ext['activated_key'] ) && 'Activated' === $ext['activated_key'] ) || ( ! $deactivated_license && ( ! isset( $ext['activated_key'] ) || 'Activated' !== $ext['activated_key'] ) ) ) ) {
303 continue; // get deactivated license, skip activated license.
304 }
305
306 $return[ $extension['slug'] ] = $ext;
307 }
308 return $return;
309 }
310
311 /**
312 * Generate API Password.
313 *
314 * @param integer $length Lenght of password.
315 * @param bool $special_chars true|false, allow special characters.
316 * @param bool $extra_special_chars true|false, allow extra special characters.
317 *
318 * @return MainWP_Api_Manager_Password_Management::generate_password()
319 */
320 public static function gen_api_password( $length = 12, $special_chars = true, $extra_special_chars = false ) {
321 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
322 return MainWP_Api_Manager_Password_Management::generate_password( $length, $special_chars, $extra_special_chars );
323 }
324
325
326 /**
327 * Add Extension Menu.
328 *
329 * @param mixed $slug Extension slug.
330 *
331 * @return boolean true|false.
332 *
333 * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
334 */
335 public static function add_extension_menu( $slug ) {
336 $snMenuExtensions = get_option( 'mainwp_extmenu' );
337 if ( ! is_array( $snMenuExtensions ) ) {
338 $snMenuExtensions = array();
339 }
340
341 $snMenuExtensions[] = $slug;
342
343 MainWP_Utility::update_option( 'mainwp_extmenu', $snMenuExtensions );
344
345 /**
346 * Adds Extension to the navigation menu
347 *
348 * Adds Extension instance to the Extensions located in the main MainWP navigation menu.
349 *
350 * @param string $slug Extension slug.
351 *
352 * @since 4.0
353 */
354 do_action( 'mainwp_added_extension_menu', $slug );
355
356 return true;
357 }
358
359 /**
360 * HTTP Request Reject Unsafe Urls.
361 *
362 * @param bool $args args.
363 *
364 * @return mixed args.
365 */
366 public static function http_request_reject_unsafe_urls( $args ) {
367 $args['reject_unsafe_urls'] = false;
368
369 return $args;
370 }
371
372 /**
373 * No SSL Filter Function.
374 *
375 * @param bool $args args.
376 *
377 * @return mixed args.
378 */
379 public static function no_ssl_filter_function( $args ) {
380 $args['sslverify'] = false;
381 return $args;
382 }
383
384 /**
385 * No SSL Filter Extention Upgrade.
386 *
387 * @param bool $r Results.
388 * @param mixed $url Upgrade Extension URL.
389 *
390 * @return mixed false|$r.
391 */
392 public static function no_ssl_filter_extension_upgrade( $r, $url ) {
393 if ( ( false !== strpos( $url, 'am_download_file=' ) ) && ( false !== strpos( $url, 'am_email=' ) ) ) {
394 $r['sslverify'] = false;
395 }
396
397 return $r;
398 }
399
400 /**
401 * Install MainWP Extension.
402 *
403 * @param mixed $url MainWP Extension update URL.
404 * @param bool $activatePlugin true|false Whether or not to activate extension.
405 *
406 * @return mixed $return
407 *
408 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
409 */
410 public static function install_plugin( $url, $activatePlugin = false ) { //phpcs:ignore -- NOSONAR - complex.
411
412 MainWP_System_Utility::get_wp_file_system();
413
414 /**
415 * WordPress files system object.
416 *
417 * @global object
418 */
419 global $wp_filesystem;
420
421 if ( file_exists( ABSPATH . '/wp-admin/includes/screen.php' ) ) {
422 include_once ABSPATH . '/wp-admin/includes/screen.php'; // NOSONAR - WP compatible.
423 }
424
425 include_once ABSPATH . '/wp-admin/includes/template.php'; // NOSONAR - WP compatible.
426 include_once ABSPATH . '/wp-admin/includes/misc.php'; // NOSONAR - WP compatible.
427 include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; // NOSONAR - WP compatible.
428 include_once ABSPATH . '/wp-admin/includes/plugin.php'; // NOSONAR - WP compatible.
429
430 $installer = new \WP_Upgrader();
431 $ssl_verifyhost = get_option( 'mainwp_sslVerifyCertificate' );
432 $ssl_api_verifyhost = ( ( false === get_option( 'mainwp_api_sslVerifyCertificate' ) ) || ( 1 === (int) get_option( 'mainwp_api_sslVerifyCertificate' ) ) ) ? 1 : 0;
433
434 if ( empty( $ssl_api_verifyhost ) ) {
435 add_filter( 'http_request_args', array( static::get_class_name(), 'no_ssl_filter_function' ), 99, 2 );
436 }
437
438 add_filter( 'http_request_args', array( static::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
439
440 $result = $installer->run(
441 array(
442 'package' => $url,
443 'destination' => WP_PLUGIN_DIR,
444 'clear_destination' => false,
445 'clear_working' => true,
446 'hook_extra' => array(),
447 )
448 );
449
450 remove_filter( 'http_request_args', array( static::get_class_name(), 'http_request_reject_unsafe_urls' ), 99, 2 );
451
452 if ( '0' === $ssl_verifyhost ) {
453 remove_filter( 'http_request_args', array( static::get_class_name(), 'no_ssl_filter_function' ), 99 );
454 }
455
456 $error = null;
457 $output = null;
458 $plugin_slug = null;
459
460 if ( is_wp_error( $result ) ) {
461 $error_code = $result->get_error_code();
462 if ( $result->get_error_data() && is_string( $result->get_error_data() ) ) {
463 $error = $error_code . ' - ' . $result->get_error_data();
464 } else {
465 $error = $error_code;
466 }
467 } else {
468 $path = $result['destination'];
469
470 foreach ( $result['source_files'] as $srcFile ) {
471
472 if ( 'readme.txt' === $srcFile ) {
473 continue;
474 }
475
476 $thePlugin = get_plugin_data( $path . $srcFile );
477
478 if ( null !== $thePlugin && '' !== $thePlugin && '' !== $thePlugin['Name'] ) {
479 $the_name = static::polish_string_name( $thePlugin['Name'] );
480 $output .= esc_html( $the_name ) . ' ' . esc_html__( 'installed successfully. Do not forget to activate the extension API license.', 'mainwp' );
481 $plugin_slug = $result['destination_name'] . '/' . $srcFile;
482
483 if ( $activatePlugin ) {
484 activate_plugin( $path . $srcFile, '', false, true );
485
486 /**
487 * Extension API activation
488 *
489 * Activates the extension API license upon the extension installation.
490 *
491 * @since Unknown
492 * @ignore
493 */
494 do_action( 'mainwp_api_extension_activated', $path . $srcFile );
495 }
496
497 break;
498 }
499 }
500 }
501
502 if ( ! empty( $error ) ) {
503 $return['error'] = $error;
504 } else {
505 $return['result'] = 'SUCCESS';
506 $return['output'] = $output;
507 $return['slug'] = esc_html( $plugin_slug );
508 }
509
510 return $return;
511 }
512
513 /**
514 * Check if MainWP Extension is available.
515 *
516 * @param mixed $pAPI MainWP Extension API Key.
517 *
518 * @return boolean true|false.
519 */
520 public static function is_extension_available( $pAPI ) {
521
522 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
523
524 $extensions = static::get_extensions();
525 if ( isset( $extensions ) && is_array( $extensions ) ) {
526 foreach ( $extensions as $extension ) {
527 $slug = dirname( $extension['slug'] );
528 if ( $slug === $pAPI ) {
529 return true;
530 }
531 }
532 }
533 return false;
534 }
535
536 /**
537 * Check if MainWP Extension is enabled.
538 *
539 * @param mixed $pluginFile MainWP Extension to bo verified.
540 *
541 * @return array 'key' => md5( $pluginFile . '-SNNonceAdder').
542 */
543 public static function is_extension_enabled( $pluginFile ) {
544 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
545 return array( 'key' => md5( $pluginFile . '-SNNonceAdder' ) ); // NOSONAR - safe for sig file name.
546 }
547
548 /**
549 * Create Menu Extension Array.
550 *
551 * @param mixed $slug menu slug.
552 *
553 * @return array Menu Array.
554 */
555 public static function added_on_menu( $slug ) {
556 $snMenuExtensions = get_option( 'mainwp_extmenu' );
557 if ( ! is_array( $snMenuExtensions ) ) {
558 $snMenuExtensions = array();
559 }
560 return in_array( $slug, $snMenuExtensions );
561 }
562
563 /**
564 * Check if MainWP Extension is activated or not.
565 *
566 * @param mixed $plugin_slug MainWP Extension slug.
567 *
568 * @return boolean true|false.
569 */
570 public static function is_extension_activated( $plugin_slug ) {
571 $extensions = static::get_indexed_extensions_infor( array( 'activated' => true ) );
572 return isset( $extensions[ $plugin_slug ] ) ? true : false;
573 }
574
575 /**
576 * Verify MainWP Extension.
577 *
578 * @param mixed $pluginFile MainWP Extensoin to verify.
579 * @param mixed $key Child Site Key.
580 *
581 * @return mixed md5( $pluginFile . '-SNNonceAdder' ) === $key
582 */
583 public static function hook_verify( $pluginFile, $key ) {
584 return md5( $pluginFile . '-SNNonceAdder' ) === $key; // NOSONAR - safe for sig file name.
585 }
586
587 /**
588 * Get sql websites for current user.
589 *
590 * @param mixed $pluginFile Extension plugin file to verify.
591 * @param mixed $key PThe child-key.
592 *
593 * @return mixed null|sql query.
594 *
595 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_current_wpid()
596 */
597 public static function hook_get_dashboard_sites( $pluginFile, $key ) {
598 if ( ! static::hook_verify( $pluginFile, $key ) ) {
599 return null;
600 }
601
602 $current_wpid = MainWP_System_Utility::get_current_wpid();
603
604 if ( $current_wpid ) {
605 $sql = MainWP_DB::instance()->get_sql_website_by_id( $current_wpid );
606 } else {
607 $sql = MainWP_DB::instance()->get_sql_websites_for_current_user();
608 }
609
610 return MainWP_DB::instance()->query( $sql );
611 }
612
613 /**
614 * Fetch Authorized URLS.
615 *
616 * @param mixed $pluginFile Extension plugin file to verify.
617 * @param string $key The child key.
618 * @param object $dbwebsites The websites.
619 * @param string $what Action to perorm.
620 * @param mixed $params Request parameters.
621 * @param mixed $handle Request handle.
622 * @param mixed $output Request output.
623 *
624 * @uses MainWP_Connect::fetch_urls_authed()
625 *
626 * @return mixed false|MainWP_Connect::fetch_urls_authed()
627 */
628 public static function hook_fetch_urls_authed( $pluginFile, $key, $dbwebsites, $what, $params, $handle, $output ) {
629 if ( ! static::hook_verify( $pluginFile, $key ) ) {
630 return false;
631 }
632
633 return MainWP_Connect::fetch_urls_authed( $dbwebsites, $what, $params, $handle, $output );
634 }
635
636 /**
637 * Fetch Authorized URL.
638 *
639 * @throws MainWP_Exception On incorrect website.
640 * @param mixed $pluginFile Extension plugin file to verify.
641 * @param mixed $key The child-key.
642 * @param mixed $websiteId Child Site ID.
643 * @param mixed $what What.
644 * @param mixed $params Parameters.
645 * @param null $rawResponse Raw responce.
646 *
647 * @return mixed false|throw|error
648 *
649 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
650 */
651 public static function hook_fetch_url_authed( $pluginFile, $key, $websiteId, $what, $params, $rawResponse = null ) {
652 if ( ! static::hook_verify( $pluginFile, $key ) ) {
653 return false;
654 }
655 return static::fetch_url_authed( $websiteId, $what, $params, $rawResponse );
656 }
657
658 /**
659 * Fetch Authorized URL.
660 *
661 * @throws MainWP_Exception On incorrect website.
662 * @param mixed $websiteId Child Site ID.
663 * @param mixed $what What.
664 * @param mixed $params Parameters.
665 * @param null $rawResponse Raw responce.
666 *
667 * @return mixed false|throw|error
668 *
669 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
670 */
671 public static function fetch_url_authed( $websiteId, $what, $params, $rawResponse = null ) {
672 try {
673 $website = MainWP_DB::instance()->get_website_by_id( $websiteId );
674 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
675 throw new MainWP_Exception( 'You can not edit this website.' );
676 }
677
678 return MainWP_Connect::fetch_url_authed( $website, $what, $params, false, false, true, $rawResponse );
679 } catch ( MainWP_Exception $e ) {
680 return array(
681 'error' => MainWP_Error_Helper::get_error_message( $e ),
682 'errorCode' => 'excep_fetch_url_authed',
683 );
684 }
685 }
686
687 /**
688 * Get DB Sites.
689 *
690 * @param mixed $pluginFile Extension plugin file to verify.
691 * @param mixed $key The child-key.
692 * @param mixed $sites Child Sites.
693 * @param string $groups Groups.
694 * @param bool $options Options.
695 *
696 * @return array $dbwebsites.
697 *
698 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
699 * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
700 */
701 public static function hook_get_db_sites( $pluginFile, $key, $sites, $groups = '', $options = false ) {
702 if ( ! static::hook_verify( $pluginFile, $key ) ) {
703 return false;
704 }
705
706 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
707 $params = array(
708 'fields' => $options,
709 'sites' => $sites,
710 'groups' => $groups,
711 );
712 return MainWP_DB::instance()->get_db_sites( $params );
713 }
714
715 /**
716 * Get DB Sites.
717 *
718 * @since 4.4.2
719 *
720 * @param mixed $pluginFile Extension plugin file to verify.
721 * @param mixed $key The child-key.
722 * @param mixed $params params.
723 *
724 * @return array $dbwebsites.
725 */
726 public static function hook_get_db_websites( $pluginFile, $key, $params = array() ) {
727 if ( ! static::hook_verify( $pluginFile, $key ) ) {
728 return false;
729 }
730
731 if ( empty( $params ) || ! is_array( $params ) ) {
732 return false;
733 }
734 return MainWP_DB::instance()->get_db_sites( $params );
735 }
736
737
738 /**
739 * Get Sites.
740 *
741 * @param string $pluginFile Extension plugin file to verify.
742 * @param string $key The child-key.
743 * @param int $websiteid The id of the child site you wish to retrieve.
744 * @param bool $for_manager Check Team Control.
745 * @param array $others Array of others.
746 *
747 * @return array $output Array of content to output.
748 *
749 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
750 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
751 */
752 public static function hook_get_sites( $pluginFile, $key, $websiteid = null, $for_manager = false, $others = array() ) { // phpcs:ignore -- NOSONAR - not quite complex function.
753 if ( ! static::hook_verify( $pluginFile, $key ) ) {
754 return false;
755 }
756
757 if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! mainwp_current_user_have_right( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
758 return false;
759 }
760 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
761
762 return MainWP_DB::instance()->get_sites( $websiteid, $for_manager, $others );
763 }
764
765 /**
766 * Method hook_get_groups()
767 *
768 * Get Child Sites within groups & store them in an array.
769 *
770 * @param string $pluginFile Extension plugin file to verify.
771 * @param string $key The child-key.
772 * @param int $groupid The id of the group you wish to retrieve.
773 * @param bool $for_manager Check Team Control.
774 *
775 * @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.
776 */
777 public static function hook_get_groups( $pluginFile, $key, $groupid, $for_manager = false ) {
778 if ( ! static::hook_verify( $pluginFile, $key ) ) {
779 return false;
780 }
781
782 if ( $for_manager && ( ! defined( 'MWP_TEAMCONTROL_PLUGIN_SLUG' ) || ! mainwp_current_user_have_right( 'extension', dirname( MWP_TEAMCONTROL_PLUGIN_SLUG ) ) ) ) {
783 return false;
784 }
785
786 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
787
788 if ( isset( $groupid ) ) {
789 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
790 if ( empty( $group ) ) {
791 return false;
792 }
793
794 $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
795 $websitesOut = array();
796 foreach ( $websites as $website ) {
797 $websitesOut[] = $website->id;
798 }
799
800 return array(
801 array(
802 'id' => $groupid,
803 'name' => $group->name,
804 'websites' => $websitesOut,
805 ),
806 );
807 }
808
809 $groups = MainWP_DB_Common::instance()->get_groups_and_count( null, $for_manager );
810 $output = array();
811 foreach ( $groups as $group ) {
812 $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
813 $websitesOut = array();
814 foreach ( $websites as $website ) {
815 if ( in_array( $website->id, $websitesOut ) ) {
816 continue;
817 }
818 $websitesOut[] = $website->id;
819 }
820 $output[] = array(
821 'id' => $group->id,
822 'name' => $group->name,
823 'websites' => $websitesOut,
824 );
825 }
826
827 return $output;
828 }
829
830
831 /**
832 * Get all loaded extensions.
833 *
834 * @return mainwp_extensions value.
835 */
836 public static function hook_get_all_extensions() {
837 MainWP_Deprecated_Hooks::maybe_handle_deprecated_hook();
838 return get_option( 'mainwp_extensions' );
839 }
840
841 /**
842 * Clone Site.
843 *
844 * @param mixed $pluginFile Extension plugin file to verify.
845 * @param mixed $key The child-key.
846 * @param mixed $websiteid Child Site ID.
847 * @param mixed $cloneID Clone ID.
848 * @param mixed $clone_url URL to CLone to.
849 * @param bool $force_update true|false, force an update.
850 *
851 * @return mixed false|$ret
852 *
853 * @uses \MainWP\Dashboard\MainWP_Sync::sync_site()
854 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
855 * @uses \MainWP\Dashboard\MainWP_Utility::remove_http_www_prefix()
856 */
857 public static function hook_clone_site( $pluginFile, $key, $websiteid, $cloneID, $clone_url, $force_update = false ) { //phpcs:ignore -- NOSONAR - complex.
858 if ( ! static::hook_verify( $pluginFile, $key ) ) {
859 return false;
860 }
861
862 if ( ! empty( $websiteid ) && ! empty( $cloneID ) ) {
863
864 $sql = MainWP_DB::instance()->get_sql_website_by_id( $websiteid );
865 $websites = MainWP_DB::instance()->query( $sql );
866 $website = MainWP_DB::fetch_object( $websites );
867
868 if ( empty( $website ) ) {
869 return array( 'error' => esc_html__( 'Website not found.', 'mainwp' ) );
870 }
871
872 $ret = array();
873
874 if ( '/' !== substr( $clone_url, - 1 ) ) {
875 $clone_url .= '/';
876 }
877
878 $tmp1 = MainWP_Utility::remove_http_www_prefix( $website->url );
879 $tmp2 = MainWP_Utility::remove_http_www_prefix( $clone_url );
880
881 if ( false === strpos( $tmp2, $tmp1 ) ) {
882 return false;
883 }
884
885 $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
886 if ( $clone_sites ) {
887 $clone_site = current( $clone_sites );
888 if ( $clone_site && $clone_site->is_staging ) {
889 if ( $force_update ) {
890 MainWP_DB::instance()->update_website_values(
891 $clone_site->id,
892 array(
893 'adminname' => $website->adminname,
894 'pubkey' => $website->pubkey,
895 'privkey' => $website->privkey,
896 'verify_certificate' => $website->verify_certificate,
897 'uniqueId' => ( null !== $website->uniqueId ? $website->uniqueId : '' ),
898 'http_user' => $website->http_user,
899 'http_pass' => $website->http_pass,
900 'ssl_version' => $website->ssl_version,
901 )
902 );
903 }
904 $ret['siteid'] = $clone_site->id;
905 $ret['response'] = esc_html__( 'Site updated.', 'mainwp' );
906 }
907 return $ret;
908 }
909 $clone_name = $website->name . ' - ' . $cloneID;
910
911 /**
912 * Current user global.
913 *
914 * @global string
915 */
916 global $current_user;
917
918 $others = array(
919 'groupids' => array(),
920 'groupnames' => array(),
921 'verifyCertificate' => $website->verify_certificate,
922 'addUniqueId' => ( null !== $website->uniqueId ? $website->uniqueId : '' ),
923 'http_user' => $website->http_user,
924 'http_pass' => $website->http_pass,
925 'sslVersion' => $website->ssl_version,
926 'wpe' => $website->wpe,
927 'isStaging' => 1,
928 );
929
930 $id = MainWP_DB::instance()->add_website( $current_user->ID, $clone_name, $clone_url, $website->adminname, $website->pubkey, $website->privkey, $others );
931
932 /** This action is documented in class\class-mainwp-manage-sites-view.php */
933 do_action( 'mainwp_added_new_site', $id, $website );
934
935 if ( $id ) {
936 $group_id = get_option( 'mainwp_stagingsites_group_id' );
937 if ( $group_id ) {
938 $website = MainWP_DB::instance()->get_website_by_id( $id );
939 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
940 MainWP_Sync::sync_site( $website, false, false );
941 $group = MainWP_DB_Common::instance()->get_group_by_id( $group_id );
942 if ( ! empty( $group ) ) {
943 MainWP_DB_Common::instance()->update_group_site( $group->id, $id );
944 }
945 }
946 }
947 $ret['response'] = esc_html__( 'Site successfully added.', 'mainwp' );
948 $ret['siteid'] = $id;
949 }
950 return $ret;
951 }
952
953 return false;
954 }
955
956 /**
957 * Delete Clones Site.
958 *
959 * @param mixed $pluginFile Extension plugin file to verify.
960 * @param mixed $key The child-key.
961 * @param mixed $clone_url URL to Clone to.
962 * @param bool $clone_site_id Cloned Site ID.
963 *
964 * @return mixed false|array Array => "Success".
965 *
966 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_wp_file_system()
967 * @uses \MainWP\Dashboard\MainWP_System_Utility::get_icons_dir()
968 */
969 public static function hook_delete_clone_site( $pluginFile, $key, $clone_url = '', $clone_site_id = false ) { //phpcs:ignore -- NOSONAR - complex.
970 if ( ! static::hook_verify( $pluginFile, $key ) ) {
971 return false;
972 }
973
974 if ( empty( $clone_url ) && empty( $clone_site_id ) ) {
975 return false;
976 }
977
978 $clone_site = null;
979 if ( ! empty( $clone_url ) ) {
980 if ( '/' !== substr( $clone_url, - 1 ) ) {
981 $clone_url .= '/';
982 }
983 $clone_sites = MainWP_DB::instance()->get_websites_by_url( $clone_url );
984 if ( ! empty( $clone_sites ) ) {
985 $clone_site = current( $clone_sites );
986
987 }
988 } elseif ( ! empty( $clone_site_id ) ) {
989 $sql = MainWP_DB::instance()->get_sql_website_by_id( $clone_site_id );
990 $websites = MainWP_DB::instance()->query( $sql );
991 $clone_site = MainWP_DB::fetch_object( $websites );
992 }
993
994 if ( empty( $clone_site ) ) {
995 return array( 'error' => esc_html__( 'Not found the clone website', 'mainwp' ) );
996 }
997
998 if ( $clone_site ) {
999 if ( empty( $clone_site->is_staging ) ) {
1000 return false;
1001 }
1002
1003 MainWP_System_Utility::get_wp_file_system();
1004
1005 /**
1006 * WordPress files system object.
1007 *
1008 * @global object
1009 */
1010 global $wp_filesystem;
1011
1012 $favi = MainWP_DB::instance()->get_website_option( $clone_site, 'favi_icon', '' );
1013 if ( ! empty( $favi ) && ( false !== strpos( $favi, 'favi-' . $clone_site->id . '-' ) ) ) {
1014 $dirs = MainWP_System_Utility::get_icons_dir();
1015 if ( $wp_filesystem->exists( $dirs[0] . $favi ) ) {
1016 $wp_filesystem->delete( $dirs[0] . $favi );
1017 }
1018 }
1019
1020 MainWP_DB::instance()->remove_website( $clone_site->id );
1021
1022 /** This action is documented in pages\page-mainwp-manage-sites-handler.php */
1023 do_action( 'mainwp_delete_site', $clone_site );
1024 return array( 'result' => 'SUCCESS' );
1025 }
1026
1027 return false;
1028 }
1029
1030 /**
1031 * Add Groups.
1032 *
1033 * @param mixed $pluginFile Extension plugin file to verify.
1034 * @param mixed $key The child-key.
1035 * @param mixed $newName Name that you want to give the group.
1036 *
1037 * @return mixed false|$groupId
1038 *
1039 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::check_group_name()
1040 */
1041 public static function hook_add_group( $pluginFile, $key, $newName ) {
1042
1043 if ( ! static::hook_verify( $pluginFile, $key ) ) {
1044 return false;
1045 }
1046
1047 /**
1048 * Current user global.
1049 *
1050 * @global string
1051 */
1052 global $current_user;
1053
1054 if ( ! empty( $newName ) ) {
1055 $groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, MainWP_Manage_Groups::check_group_name( $newName ) );
1056
1057 /** This action is documented in pages\page-mainwp-manage-groups.php */
1058 do_action( 'mainwp_added_new_group', $groupId );
1059 return $groupId;
1060 }
1061 return false;
1062 }
1063 }
1064