PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.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
← All changes | class/class-mainwp-post-handler.php +758 -97 5.1 → 6.2.1 View file →
@@ -6,8 +6,18 @@
6 6 */
7 7
8 8 namespace MainWP\Dashboard;
9 9
10 +use MainWP\Dashboard\Module\Log\Log_DB_Helper;
11 +use MainWP\Dashboard\Module\Log\Log_Changes_Logs_Helper;
12 +
13 +// Exit if accessed directly.
14 +if ( ! defined( 'ABSPATH' ) ) {
15 + exit;
16 +}
17 +
18 +// phpcs:disable Generic.Metrics.CyclomaticComplexity -- complexity.
19 +
10 20 /**
11 21 * Class MainWP_Post_Handler
12 22 *
13 23 * @package MainWP\Dashboard
@@ -13,12 +23,10 @@
13 23 * @package MainWP\Dashboard
14 24 *
15 25 * @uses \MainWP\Dashboard\MainWP_Post_Base_Handler
16 26 */
17 -class MainWP_Post_Handler extends MainWP_Post_Base_Handler { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
27 +class MainWP_Post_Handler extends MainWP_Post_Base_Handler { // phpcs:ignore -- NOSONAR - Complex.
18 28
19 - // phpcs:disable Generic.Metrics.CyclomaticComplexity -- This is the only way to achieve desired results, pull request solutions appreciated.
20 -
21 29 /**
22 30 * Private static variable to hold the single instance of the class.
23 31 *
24 32 * @static
@@ -57,9 +65,9 @@
57 65
58 66 // Widget: RightNow.
59 67 $this->add_action( 'mainwp_syncerrors_dismiss', array( &$this, 'mainwp_syncerrors_dismiss' ) );
60 68
61 - if ( mainwp_current_user_have_right( 'dashboard', 'manage_security_issues' ) ) {
69 + if ( \mainwp_current_user_can( 'dashboard', 'manage_security_issues' ) ) {
62 70 // Page: SecurityIssues.
63 71 $this->add_action( 'mainwp_security_issues_request', array( &$this, 'mainwp_security_issues_request' ) );
64 72 $this->add_action( 'mainwp_security_issues_fix', array( &$this, 'mainwp_security_issues_fix' ) );
65 73 $this->add_action( 'mainwp_security_issues_unfix', array( &$this, 'mainwp_security_issues_unfix' ) );
@@ -71,11 +79,13 @@
71 79 $this->add_action( 'mainwp_leftmenu_filter_group', array( &$this, 'mainwp_leftmenu_filter_group' ) );
72 80 $this->add_action( 'mainwp_widgets_order', array( &$this, 'ajax_widgets_order' ) );
73 81 $this->add_action( 'mainwp_save_settings', array( &$this, 'ajax_mainwp_save_settings' ) );
74 82 $this->add_action( 'mainwp_guided_tours_option_update', array( &$this, 'ajax_guided_tours_option_update' ) );
83 + $this->add_action( 'mainwp_help_modal_content_update', array( &$this, 'ajax_mainwp_help_modal_content_update' ) );
84 + $this->add_action( 'mainwp_widgets_connections_status_details_display_rows', array( MainWP_Connection_Status::instance(), 'ajax_display_rows' ) );
75 85
76 86 // Page: Recent Posts.
77 - if ( mainwp_current_user_have_right( 'dashboard', 'manage_posts' ) ) {
87 + if ( \mainwp_current_user_can( 'dashboard', 'manage_posts' ) ) {
78 88 $this->add_action( 'mainwp_post_unpublish', array( &$this, 'mainwp_post_unpublish' ) );
79 89 $this->add_action( 'mainwp_post_publish', array( &$this, 'mainwp_post_publish' ) );
80 90 $this->add_action( 'mainwp_post_trash', array( &$this, 'mainwp_post_trash' ) );
81 91 $this->add_action( 'mainwp_post_delete', array( &$this, 'mainwp_post_delete' ) );
@@ -83,9 +93,9 @@
83 93 $this->add_action( 'mainwp_post_approve', array( &$this, 'mainwp_post_approve' ) );
84 94 }
85 95 $this->add_action( 'mainwp_post_addmeta', array( MainWP_Post_Page_Handler::get_class_name(), 'ajax_add_meta' ) );
86 96 // Page: Pages.
87 - if ( mainwp_current_user_have_right( 'dashboard', 'manage_pages' ) ) {
97 + if ( \mainwp_current_user_can( 'dashboard', 'manage_pages' ) ) {
88 98 $this->add_action( 'mainwp_page_unpublish', array( &$this, 'mainwp_page_unpublish' ) );
89 99 $this->add_action( 'mainwp_page_publish', array( &$this, 'mainwp_page_publish' ) );
90 100 $this->add_action( 'mainwp_page_trash', array( &$this, 'mainwp_page_trash' ) );
91 101 $this->add_action( 'mainwp_page_delete', array( &$this, 'mainwp_page_delete' ) );
@@ -128,8 +138,9 @@
128 138 $this->add_action( 'mainwp_clients_delete_client', array( &$this, 'mainwp_clients_delete_client' ) );
129 139
130 140 $this->add_action( 'mainwp_clients_save_field', array( &$this, 'mainwp_clients_save_field' ) );
131 141 $this->add_action( 'mainwp_clients_delete_general_field', array( &$this, 'mainwp_clients_delete_general_field' ) );
142 + $this->add_action( 'mainwp_clients_bulk_delete_fields', array( &$this, 'mainwp_clients_bulk_delete_fields' ) );
132 143 $this->add_action( 'mainwp_clients_delete_field', array( &$this, 'mainwp_clients_delete_field' ) );
133 144 $this->add_action( 'mainwp_clients_notes_save', array( &$this, 'mainwp_clients_notes_save' ) );
134 145 $this->add_action( 'mainwp_clients_suspend_client', array( &$this, 'mainwp_clients_suspend_client' ) );
135 146 $this->add_action( 'mainwp_refresh_icon', array( &$this, 'ajax_refresh_icon' ) );
@@ -134,14 +145,28 @@
134 145 $this->add_action( 'mainwp_clients_suspend_client', array( &$this, 'mainwp_clients_suspend_client' ) );
135 146 $this->add_action( 'mainwp_refresh_icon', array( &$this, 'ajax_refresh_icon' ) );
136 147 $this->add_action( 'mainwp_upload_custom_icon', array( &$this, 'ajax_upload_custom_icon' ) );
137 148 $this->add_action( 'mainwp_select_custom_theme', array( &$this, 'ajax_select_custom_theme' ) );
138 - $this->add_action( 'mainwp_site_actions_dismiss', array( &$this, 'ajax_site_actions_dismiss' ) );
139 - $this->add_action( 'mainwp_delete_non_mainwp_actions', array( &$this, 'ajax_delete_non_mainwp_actions' ) );
140 149 $this->add_action( 'mainwp_import_demo_data', array( &$this, 'ajax_import_demo_data' ) );
141 150 $this->add_action( 'mainwp_delete_demo_data', array( &$this, 'ajax_delete_demo_data' ) );
142 151 $this->add_action( 'mainwp_prepare_renew_connections', array( MainWP_Connect_Helper::instance(), 'ajax_prepare_renew_connections' ) );
143 152 $this->add_action( 'mainwp_renew_connections', array( MainWP_Connect_Helper::instance(), 'ajax_renew_connections' ) );
153 +
154 + $this->add_action( 'mainwp_clients_check_client', array( &$this, 'mainwp_clients_check_client' ) );
155 + $this->add_action( 'mainwp_clients_import_client', array( &$this, 'mainwp_clients_import_client' ) );
156 +
157 + // Page: managesites.
158 + $this->add_action( 'mainwp_save_temp_import_website', array( &$this, 'ajax_save_temp_import_website' ) );
159 + $this->add_action( 'mainwp_delete_temp_import_website', array( &$this, 'ajax_delete_temp_import_website' ) );
160 + $this->add_action( 'mainwp_import_website_add_client', array( &$this, 'ajax_import_website_add_client' ) );
161 + $this->add_action( 'mainwp_import_website_add_client_no_site', array( &$this, 'ajax_import_website_add_client_no_site' ) );
162 +
163 + // Page:: mainwp-setup.
164 + $this->add_action( 'mainwp_clients_add_multi_client', array( &$this, 'ajax_clients_add_multi_client' ) );
165 + $this->add_action( 'mainwp_increase_connection_security', array( &$this, 'ajax_increase_connection_security' ) );
166 + $this->add_action( 'mainwp_qsw_ui_mode_detected', array( &$this, 'ajax_ui_mode_detected' ) );
167 +
168 + $this->add_action( 'mainwp_changes_logs_get_item_changes', array( &$this, 'ajax_get_item_changes_logs' ) );
144 169 }
145 170
146 171 /**
147 172 * Method add_post_action()
@@ -180,9 +205,9 @@
180 205 $this->secure_request( 'mainwp_users_search' );
181 206 MainWP_Cache::init_session();
182 207
183 208 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
184 - $role = isset( $_POST['role'] ) ? sanitize_text_field( wp_unslash( $_POST['role'] ) ) : '';
209 + $role = isset( $_POST['urole'] ) ? sanitize_text_field( wp_unslash( $_POST['urole'] ) ) : '';
185 210 $groups = isset( $_POST['groups'] ) && is_array( $_POST['groups'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['groups'] ) ) : '';
186 211 $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : '';
187 212 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : '';
188 213 $search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
@@ -187,8 +212,9 @@
187 212 $clients = isset( $_POST['clients'] ) && is_array( $_POST['clients'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['clients'] ) ) : '';
188 213 $search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';
189 214 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
190 215 MainWP_User::render_table( false, $role, $groups, $sites, $search, $clients );
216 + session_write_close();
191 217 die();
192 218 }
193 219
194 220 /**
@@ -513,13 +539,45 @@
513 539 $this->secure_request( 'mainwp_notice_status_update' );
514 540
515 541 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
516 542 $no_id = isset( $_POST['notice_id'] ) ? sanitize_text_field( wp_unslash( $_POST['notice_id'] ) ) : false;
543 +
544 + if ( ! empty( $_POST['short_term'] ) ) {
545 + if ( empty( $no_id ) ) {
546 + die( 'invalid' );
547 + }
548 +
549 + $keys = explode( ';', $no_id );
550 + foreach ( $keys as $key ) {
551 + MainWP_Utility::dismiss_short_term_notice( $key );
552 + }
553 + die( 'dismissed' );
554 + }
555 +
517 556 if ( 'mail_failed' === $no_id ) {
518 557 MainWP_Utility::update_option( 'mainwp_notice_wp_mail_failed', 'hide' );
519 558 die( 'ok' );
520 559 }
521 560
561 + if ( 'phpver_8_0' === $no_id ) {
562 + MainWP_Utility::update_user_option( 'lasttime_hidden_phpver_8_0', time() );
563 + }
564 +
565 + $time_set = isset( $_POST['time_set'] ) && 1 === intval( $_POST['time_set'] ) ? true : false;
566 + $this->hide_dashboard_notice_status( $no_id, $time_set );
567 + // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
568 + die( 1 );
569 + }
570 +
571 + /**
572 + * Method hide dashboard notice status.
573 + *
574 + * @param mixed $no_id notice id.
575 + * @param mixed $time_set time.
576 + * @param bool $hide_noti hide notice.
577 + * @return void
578 + */
579 + public function hide_dashboard_notice_status( $no_id, $time_set = false, $hide_noti = true ) { //phpcs:ignore -- NOSONAR - complexity.
522 580 /**
523 581 * Current user global.
524 582 *
525 583 * @global string
@@ -531,25 +589,29 @@
531 589 if ( ! is_array( $status ) ) {
532 590 $status = array();
533 591 }
534 592 if ( ! empty( $no_id ) ) {
535 - $time_set = isset( $_POST['time_set'] ) && 1 === intval( $_POST['time_set'] ) ? true : false;
536 - if ( $time_set ) {
537 - $status[ $no_id ] = time();
538 - } else {
539 - $status[ $no_id ] = 1;
593 + if ( 'mainwp_secure_priv_key_notice' === $no_id ) {
594 + MainWP_Utility::update_option( 'mainwp_not_start_encrypt_keys', 1 ); // to show the button.
540 595 }
596 + if ( $hide_noti ) {
597 + if ( $time_set ) {
598 + $status[ $no_id ] = time();
599 + } else {
600 + $status[ $no_id ] = 1;
601 + }
602 + } elseif ( ! empty( $status[ $no_id ] ) ) {
603 + unset( $status[ $no_id ] );
604 + }
541 605 update_user_option( $user_id, 'mainwp_notice_saved_status', $status );
542 606 }
543 607 }
544 - // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
545 - die( 1 );
546 608 }
547 609
548 610 /**
549 611 * Method mainwp_status_saving()
550 612 *
551 - * Save last_sync_sites time() or mainwp_status_saved_values.
613 + * Save last_sync_sites time().
552 614 */
553 615 public function mainwp_status_saving() { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR - complexity.
554 616 $this->secure_request( 'mainwp_status_saving' );
555 617 $values = get_option( 'mainwp_status_saved_values' );
@@ -562,24 +624,8 @@
562 624 if ( ! isset( $_POST['status'] ) ) {
563 625 die( -1 );
564 626 }
565 627
566 - if ( 'save_indicator_values' === $_POST['status'] ) {
567 -
568 - if ( ! empty( $_POST['name'] ) ) {
569 - $current_values = is_array( $values ) && isset( $values['save_indicator_values'] ) && ! empty( $values['save_indicator_values'] ) ? json_decode( $values['save_indicator_values'], true ) : array();
570 - if ( ! is_array( $current_values ) ) {
571 - $current_values = array();
572 - }
573 - $current_values[ sanitize_text_field( wp_unslash( $_POST['name'] ) ) ] = isset( $_POST['value'] ) ? intval( wp_unslash( $_POST['value'] ) ) : 0;
574 -
575 - $values['save_indicator_values'] = wp_json_encode( $current_values );
576 - update_option( 'mainwp_status_saved_values', $values );
577 - die( 'ok' );
578 - }
579 - die( 'failed' );
580 - }
581 -
582 628 if ( 'last_sync_sites' === $_POST['status'] ) {
583 629 if ( isset( $_POST['isGlobalSync'] ) && ! empty( $_POST['isGlobalSync'] ) ) {
584 630 update_option( 'mainwp_last_synced_all_sites', time() );
585 631
@@ -623,11 +669,12 @@
623 669 $this->secure_request( 'mainwp_widgets_order' );
624 670 $user = wp_get_current_user();
625 671 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
626 672 if ( $user && ! empty( $_POST['page'] ) ) {
627 - $page = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
628 - $order = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
629 - $wgids = isset( $_POST['wgids'] ) ? sanitize_text_field( wp_unslash( $_POST['wgids'] ) ) : '';
673 + $page = isset( $_POST['page'] ) ? sanitize_text_field( wp_unslash( $_POST['page'] ) ) : '';
674 + $order = isset( $_POST['order'] ) ? sanitize_text_field( wp_unslash( $_POST['order'] ) ) : '';
675 + $wgids = isset( $_POST['wgids'] ) ? sanitize_text_field( wp_unslash( $_POST['wgids'] ) ) : '';
676 + $page_widget = isset( $_POST['page_widget'] ) ? sanitize_text_field( wp_unslash( $_POST['page_widget'] ) ) : '';
630 677
631 678 $wgs_orders = array();
632 679
633 680 if ( ! empty( $wgids ) ) {
@@ -635,9 +682,9 @@
635 682 $order = json_decode( $order, true );
636 683 if ( is_array( $wgids ) && is_array( $order ) ) {
637 684 foreach ( $wgids as $idx => $wgid ) {
638 685 if ( isset( $order[ $idx ] ) ) {
639 - $pre = 'widget-';
686 + $pre = 'widget-'; // #compatible-widgetid.
640 687 if ( 0 === strpos( $wgid, $pre ) ) {
641 688 $wgid = substr( $wgid, strlen( $pre ) );
642 689 }
643 690 $wgs_orders[ $wgid ] = $order[ $idx ];
@@ -659,8 +706,9 @@
659 706 update_user_option( $user->ID, 'mainwp_widgets_sorted_' . $page, wp_json_encode( $sorted_array ), true );
660 707 } else {
661 708 update_user_option( $user->ID, 'mainwp_widgets_sorted_' . $page, wp_json_encode( $wgs_orders ), true );
662 709 }
710 + MainWP_Cache_Warm_Helper::invalidate_manage_pages( array( $page_widget ) );
663 711 die( 'ok' );
664 712 }
665 713 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
666 714 die( -1 );
@@ -681,9 +729,9 @@
681 729 $option_name = 'mainwp_' . $name;
682 730 $val = isset( $_POST['value'] ) ? sanitize_text_field( wp_unslash( $_POST['value'] ) ) : '';
683 731 MainWP_Utility::update_option( $option_name, $val );
684 732 }
685 - // phpcs:enable
733 + // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
686 734 die( 'ok' );
687 735 }
688 736
689 737 /**
@@ -702,8 +750,23 @@
702 750 die( 'ok ' . intval( $enable ) );
703 751 }
704 752
705 753 /**
754 + * Method mainwp_help_modal_content_update()
755 + *
756 + * Update saved MainWP Settings.
757 + *
758 + * @uses \MainWP\Dashboard\MainWP_Utility::update_option()
759 + */
760 + public function ajax_mainwp_help_modal_content_update() {
761 + $this->secure_request( 'mainwp_help_modal_content_update' );
762 + MainWP_Utility::update_option( 'mainwp_enable_guided_tours', ( int)$_POST['enable_tour'] ); //phpcs:ignore -- NOSONAR verify.
763 + MainWP_Utility::update_option( 'mainwp_enable_guided_video', (int)$_POST['enable_video'] ); //phpcs:ignore -- NOSONAR verify.
764 + MainWP_Utility::update_option( 'mainwp_enable_guided_chatbase', (int)$_POST['enable_chatbase'] ); //phpcs:ignore -- NOSONAR verify.
765 + die( 'ok' );
766 + }
767 +
768 + /**
706 769 * Method mainwp_leftmenu_filter_group()
707 770 *
708 771 * MainWP left menu filter by group.
709 772 *
@@ -961,8 +1024,9 @@
961 1024 if ( $client_id ) {
962 1025 MainWP_DB_Client::instance()->delete_client( $client_id );
963 1026 $ret['success'] = 'SUCCESS';
964 1027 $ret['result'] = esc_html__( 'Client removed successfully.', 'mainwp' );
1028 + MainWP_Client::invalidate_warm_cache();
965 1029 } else {
966 1030 $ret['result'] = esc_html__( 'Client ID empty.', 'mainwp' );
967 1031 }
968 1032
@@ -976,8 +1040,9 @@
976 1040 * Save client custom fields.
977 1041 */
978 1042 public function mainwp_clients_save_field() {
979 1043 $this->check_security( 'mainwp_clients_save_field' );
1044 + MainWP_Client::invalidate_warm_cache();
980 1045 MainWP_Client::save_client_field();
981 1046 die();
982 1047 }
983 1048
@@ -995,19 +1060,56 @@
995 1060 $client_id = 0; // 0 general fields.
996 1061 if ( MainWP_DB_Client::instance()->delete_client_field_by( 'field_id', $field_id, $client_id ) ) {
997 1062 $ret['success'] = true;
998 1063 }
1064 + MainWP_Client::invalidate_warm_cache();
999 1065 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1000 1066 echo wp_json_encode( $ret );
1001 1067 exit;
1002 1068 }
1003 1069
1070 + /**
1071 + * Method mainwp_clients_bulk_delete_fields()
1072 + *
1073 + * Bulk delete client general fields.
1074 + */
1075 + public function mainwp_clients_bulk_delete_fields() {
1076 + $this->check_security( 'mainwp_clients_bulk_delete_fields' );
1077 + $ret = array( 'success' => false );
1078 + // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1079 + $field_ids = isset( $_POST['field_ids'] ) && is_array( $_POST['field_ids'] ) ? array_map( 'intval', $_POST['field_ids'] ) : array();
1080 + // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1004 1081
1005 - /**
1006 - * Method mainwp_clients_delete_field()
1007 - *
1008 - * Delete client custom fields.
1009 - */
1082 + if ( empty( $field_ids ) ) {
1083 + $ret['data'] = array( 'message' => __( 'No fields selected.', 'mainwp' ) );
1084 + echo wp_json_encode( $ret );
1085 + exit;
1086 + }
1087 +
1088 + $client_id = 0; // 0 general fields.
1089 + $deleted_count = 0;
1090 + foreach ( $field_ids as $field_id ) {
1091 + if ( MainWP_DB_Client::instance()->delete_client_field_by( 'field_id', $field_id, $client_id ) ) {
1092 + ++$deleted_count;
1093 + }
1094 + }
1095 +
1096 + if ( $deleted_count > 0 ) {
1097 + $ret['success'] = true;
1098 + MainWP_Client::invalidate_warm_cache();
1099 + } else {
1100 + $ret['data'] = array( 'message' => __( 'Failed to delete fields.', 'mainwp' ) );
1101 + }
1102 +
1103 + echo wp_json_encode( $ret );
1104 + exit;
1105 + }
1106 +
1107 + /**
1108 + * Method mainwp_clients_delete_field()
1109 + *
1110 + * Delete client custom fields.
1111 + */
1010 1112 public function mainwp_clients_delete_field() {
1011 1113 $this->check_security( 'mainwp_clients_delete_field' );
1012 1114 $ret = array( 'success' => false );
1013 1115 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
@@ -1090,8 +1192,114 @@
1090 1192 wp_die( 'success' );
1091 1193 }
1092 1194
1093 1195 /**
1196 + * Method mainwp_clients_check_client()
1197 + *
1198 + * The client check has been created in the system yet.
1199 + *
1200 + * @uses \MainWP_DB_Client::instance()->get_wp_client_by()
1201 + */
1202 + public function mainwp_clients_check_client() {
1203 + $this->secure_request( 'mainwp_clients_check_client' );
1204 + // phpcs:disable WordPress.Security.NonceVerification
1205 + $client_email = isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '';
1206 + // phpcs:enable WordPress.Security.NonceVerification
1207 + $client_existed = MainWP_DB_Client::instance()->get_wp_client_by( 'client_email', $client_email, ARRAY_A );
1208 + if ( is_array( $client_existed ) && isset( $client_existed['client_id'] ) ) {
1209 + return wp_send_json_error( array( 'error' => esc_html__( 'Client email exists. Please try again.', 'mainwp' ) ) );
1210 + }
1211 + return wp_send_json_success( array( 'result' => esc_html__( 'Client email not exists.', 'mainwp' ) ) );
1212 + }
1213 +
1214 + /**
1215 + * Method mainwp_clients_import_client()
1216 + *
1217 + * Import new client
1218 + *
1219 + * @uses \MainWP_DB::instance()->get_websites_by_url()
1220 + * @uses \MainWP_Client_Handler::get_default_client_fields()
1221 + * @uses \MainWP_DB_Client::instance()->update_client()
1222 + * @uses \MainWP_DB_Client::instance()->update_selected_sites_for_client()
1223 + */
1224 + public function mainwp_clients_import_client() { // phpcs:ignore -- NOSONAR
1225 + $this->secure_request( 'mainwp_clients_import_client' );
1226 + // phpcs:disable
1227 + // Set value from POST data.
1228 + $default_values = array(
1229 + 'client.name' => ! empty( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '',
1230 + 'client.email' => ! empty( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '',
1231 + 'client.contact.address.1' => ! empty( $_POST['address_1'] ) ? sanitize_text_field( wp_unslash( $_POST['address_1'] ) ) : '',
1232 + 'client.contact.address.2' => ! empty( $_POST['address_2'] ) ? sanitize_text_field( wp_unslash( $_POST['address_2'] ) ) : '',
1233 + 'client.city' => ! empty( $_POST['city'] ) ? sanitize_text_field( wp_unslash( $_POST['city'] ) ) : '',
1234 + 'client.state' => ! empty( $_POST['state'] ) ? sanitize_text_field( wp_unslash( $_POST['state'] ) ) : '',
1235 + 'client.zip' => ! empty( $_POST['zip'] ) ? sanitize_text_field( wp_unslash( $_POST['zip'] ) ) : '',
1236 + 'client.country' => ! empty( $_POST['country'] ) ? sanitize_text_field( wp_unslash( $_POST['country'] ) ) : '',
1237 + 'client.suspended' => ! empty( $_POST['suspended'] ) ? sanitize_text_field( wp_unslash( $_POST['suspended'] ) ) : 0,
1238 + );
1239 + // Get Site id.
1240 + $selected_sites = array();
1241 + if ( ! empty( $_POST['urls'] ) ) {
1242 + $urls = array_map( 'sanitize_text_field', wp_unslash( $_POST['urls'] ) );
1243 + $selected_sites = array_filter(
1244 + array_map(
1245 + function ( $v_url ) {
1246 + $url = esc_url( $v_url );
1247 + $website = MainWP_DB::instance()->get_websites_by_url( $url );
1248 + return ! empty( $website ) ? current( $website )->id : null;
1249 + },
1250 + $urls
1251 + )
1252 + );
1253 + }
1254 + // phpcs:enable
1255 + $client_to_add = array();
1256 + $default_client_fields = MainWP_Client_Handler::get_default_client_fields(); // Get client field database.
1257 + foreach ( $default_values as $key_client => $val_client ) {
1258 + if ( isset( $default_client_fields[ $key_client ] ) && ! empty( $default_client_fields[ $key_client ]['db_field'] ) && ! empty( $val_client ) ) {
1259 + $client_to_add[ $default_client_fields[ $key_client ]['db_field'] ] = $val_client; // Assign data to customers according to DB Field.
1260 + }
1261 + }
1262 +
1263 + if ( ! empty( $client_to_add ) ) {
1264 + // Set default values.
1265 + $client_to_add['created'] = time();
1266 + $client_to_add['primary_contact_id'] = 0;
1267 + // Inset client.
1268 + $inserted = MainWP_DB_Client::instance()->update_client( $client_to_add, true );
1269 +
1270 + if ( ! empty( $inserted ) && is_object( $inserted ) ) {
1271 + if ( ! empty( $selected_sites ) ) {
1272 + MainWP_DB_Client::instance()->update_selected_sites_for_client( $inserted->client_id, $selected_sites ); // Set client to selected sites.
1273 + }
1274 + // Set default color and icon .
1275 + $update = array(
1276 + 'client_id' => $inserted->client_id,
1277 + 'selected_icon_info' => 'selected:wordpress;color:#34424d',
1278 + );
1279 + MainWP_DB_Client::instance()->update_client( $update ); // Update Client.
1280 +
1281 + return wp_send_json_success(
1282 + array(
1283 + 'message' => esc_html__( 'successful client.', 'mainwp' ),
1284 + 'client' => $inserted,
1285 + )
1286 + );
1287 + }
1288 + return wp_send_json_error(
1289 + array(
1290 + 'message' => esc_html__( 'unsuccessful client.', 'mainwp' ),
1291 + )
1292 + );
1293 + }
1294 + return wp_send_json_error(
1295 + array(
1296 + 'message' => esc_html__( 'Error, please try again later.', 'mainwp' ),
1297 + )
1298 + );
1299 + }
1300 +
1301 + /**
1094 1302 * Method mainwp_syncerrors_dismiss()
1095 1303 *
1096 1304 * Dismiss Sync errors for,
1097 1305 * Widget: RightNow.
@@ -1104,9 +1312,9 @@
1104 1312
1105 1313 try {
1106 1314 die( wp_json_encode( array( 'result' => MainWP_Updates_Overview::dismiss_sync_errors() ) ) );
1107 1315 } catch ( \Exception $e ) {
1108 - die( wp_json_encode( array( 'error' => $e->getMessage() ) ) );
1316 + die( wp_json_encode( array( 'error' => sanitize_text_field( $e->getMessage() ) ) ) );
1109 1317 }
1110 1318 }
1111 1319
1112 1320 /**
@@ -1212,9 +1420,14 @@
1212 1420 // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1213 1421
1214 1422 $result = MainWP_Monitoring_Handler::handle_check_website( $website );
1215 1423 $http_code = ( is_array( $result ) && isset( $result['httpCode'] ) ) ? $result['httpCode'] : 0;
1216 - $check_result = MainWP_Connect::check_ignored_http_code( $http_code );
1424 + $check_result = MainWP_Connect::check_ignored_http_code( $http_code, $website );
1425 +
1426 + if ( is_array( $result ) && isset( $result['new_uptime_status'] ) ) {
1427 + $check_result = $result['new_uptime_status'];
1428 + }
1429 +
1217 1430 die(
1218 1431 wp_json_encode(
1219 1432 array(
1220 1433 'httpcode' => esc_html( $http_code ),
@@ -1331,9 +1544,9 @@
1331 1544
1332 1545 /**
1333 1546 * Method ajax_upload_custom_icon()
1334 1547 */
1335 - public function ajax_upload_custom_icon() {
1548 + public function ajax_upload_custom_icon() { //phpcs:ignore -- NOSONAR -complex.
1336 1549 $this->secure_request( 'mainwp_upload_custom_icon' );
1337 1550 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1338 1551 $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : '';
1339 1552 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
@@ -1339,9 +1552,9 @@
1339 1552 $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : '';
1340 1553 $delete = isset( $_POST['delete'] ) ? intval( $_POST['delete'] ) : 0;
1341 1554
1342 1555 if ( empty( $slug ) || ( 'plugin' !== $type && 'theme' !== $type ) ) {
1343 - wp_die( 'invalid data!' );
1556 + wp_die( 'Invalid data! Type or empty slug' );
1344 1557 }
1345 1558
1346 1559 $sub_folder = '';
1347 1560
@@ -1369,13 +1582,55 @@
1369 1582 if ( 'NOTCHANGE' !== $uploaded_icon ) {
1370 1583 MainWP_System_Utility::update_cached_icons( $uploaded_icon, $slug, $type, true );
1371 1584 wp_die( wp_json_encode( array( 'result' => 'success' ) ) );
1372 1585 } else {
1373 - wp_die( wp_json_encode( array( 'result' => 'failed' ) ) );
1586 + $result = array(
1587 + 'result' => 'failed',
1588 + );
1589 + $error = static::get_upload_icon_error( $output );
1590 + if ( ! empty( $error ) ) {
1591 + $result['error'] = esc_html( $error );
1592 + }
1593 + wp_die( wp_json_encode( $result ) );
1374 1594 }
1375 1595 }
1376 1596
1377 1597 /**
1598 + * Method get_upload_icon_error().
1599 + *
1600 + * @param mixed $results Upload results.
1601 + *
1602 + * @return string
1603 + */
1604 + public static function get_upload_icon_error( $results ) {
1605 + $error = '';
1606 + if ( is_array( $results ) && ! empty( $results['error'] ) && is_array( $results['error'] ) ) {
1607 + $error_code = $results['error'][0];
1608 + switch ( $error_code ) {
1609 + case 3:
1610 + $error = __( 'File size is too big. Please try a smaller file.', 'mainwp' );
1611 + break;
1612 + case 4:
1613 + $error = __( 'File type is not allowed. Please use a different file type.', 'mainwp' );
1614 + break;
1615 + case 5:
1616 + $error = __( 'File extension is not allowed. Please use a different file type.', 'mainwp' );
1617 + break;
1618 + case 6:
1619 + $error = __( 'Cannot copy file. Please try again.', 'mainwp' );
1620 + break;
1621 + case 9:
1622 + $error = __( 'Failed to crop file. Please try again.', 'mainwp' );
1623 + break;
1624 + default:
1625 + $error = __( 'Undefined error. Please try again.', 'mainwp' );
1626 + break;
1627 + }
1628 + }
1629 + return $error;
1630 + }
1631 +
1632 + /**
1378 1633 * Method ajax_select_custom_theme()
1379 1634 */
1380 1635 public function ajax_select_custom_theme() {
1381 1636 $this->secure_request( 'mainwp_select_custom_theme' );
@@ -1394,81 +1649,487 @@
1394 1649 update_user_option( $user->ID, 'mainwp_selected_theme', $theme );
1395 1650 wp_die( 'success' );
1396 1651 }
1397 1652
1653 + /**
1654 + * Method ajax_import_demo_data().
1655 + */
1656 + public function ajax_import_demo_data() {
1657 + $this->secure_request( 'mainwp_import_demo_data' );
1658 + $data = MainWP_Demo_Handle::get_instance()->import_data_demo();
1659 + MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 1 );
1660 + wp_die( wp_json_encode( $data ) );
1661 + }
1398 1662
1399 1663 /**
1400 - * Method ajax_site_actions_dismiss()
1664 + * Method ajax_delete_demo_data().
1401 1665 */
1402 - public function ajax_site_actions_dismiss() {
1403 - $this->secure_request( 'mainwp_site_actions_dismiss' );
1404 - // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1405 - $action_id = isset( $_POST['action_id'] ) ? intval( $_POST['action_id'] ) : 0;
1406 - // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1666 + public function ajax_delete_demo_data() {
1667 + $this->secure_request( 'mainwp_delete_demo_data' );
1668 + $data = MainWP_Demo_Handle::get_instance()->delete_data_demo();
1669 + MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 0 );
1670 + wp_die( wp_json_encode( $data ) );
1671 + }
1407 1672
1408 - if ( empty( $action_id ) ) {
1409 - wp_die( 'failed' );
1673 + /**
1674 + * Method ajax_save_temp_import_website()
1675 + *
1676 + * Save temp import website.
1677 + *
1678 + * @uses MainWP_DB::instance()->get_general_option()
1679 + * @uses MainWP_DB::instance()->update_general_option(()
1680 + */
1681 + public function ajax_save_temp_import_website() {
1682 + $this->secure_request( 'mainwp_save_temp_import_website' ); // Check secure.
1683 + $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1684 + // Get previously saved temporary data (if any).
1685 + $values = MainWP_DB::instance()->get_general_option( 'temp_import_sites', 'array' );
1686 + $status = $this->mainwp_get_sanitized_post( 'status' );
1687 + $index = $this->mainwp_get_sanitized_post( 'row_index', 'intval' ) ?? 1;
1688 + if ( empty( $status ) || '' === $index ) {
1689 + wp_die( wp_send_json_error( $error_msg ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1410 1690 }
1411 - $update = array(
1412 - 'dismiss' => 1,
1413 - );
1414 - MainWP_DB_Site_Actions::instance()->update_action_by_id( $action_id, $update );
1415 - wp_die( 'success' );
1691 +
1692 + $is_update = false;
1693 + if ( 'delete_temp' === $status ) { // Handling remove temp data.
1694 + $is_update = $this->mainwp_handle_delete_temp_import_website( $values, $index );
1695 + } elseif ( 'save_temp' === $status ) { // Handling save row data.
1696 + // Update row data into array.
1697 + $row_item = array(
1698 + 'site_url' => $this->mainwp_get_sanitized_post( 'site_url' ),
1699 + 'admin_name' => $this->mainwp_get_sanitized_post( 'admin_name' ),
1700 + 'admin_password' => $this->mainwp_get_sanitized_post( 'admin_password' ),
1701 + 'site_name' => $this->mainwp_get_sanitized_post( 'site_name' ),
1702 + 'tag' => $this->mainwp_get_sanitized_post( 'tag' ),
1703 + 'security_id' => $this->mainwp_get_sanitized_post( 'security_id' ),
1704 + 'http_username' => $this->mainwp_get_sanitized_post( 'http_username' ),
1705 + 'http_password' => $this->mainwp_get_sanitized_post( 'http_password' ),
1706 + 'verify_certificate' => $this->mainwp_get_sanitized_post( 'verify_certificate', 'intval' ) ?? 1,
1707 + 'ssl_version' => $this->mainwp_get_sanitized_post( 'ssl_version' ) ?? 'auto',
1708 + );
1709 +
1710 + $is_update = $this->mainwp_handle_save_temp_import_website( $values, $index, $row_item );
1711 + }
1712 + // Save data to options table.
1713 + if ( $is_update ) {
1714 + MainWP_DB::instance()->update_general_option( 'temp_import_sites', $values, 'array' );
1715 + wp_die( wp_send_json_success( esc_html( __( 'Row data saved successfully.', 'mainwp' ) ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1716 + }
1717 +
1718 + // In case of no updates.
1719 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1416 1720 }
1417 1721
1418 1722 /**
1419 - * Method ajax_site_actions_dismiss()
1723 + * Method mainwp_handle_delete_temp_import_website()
1724 + *
1725 + * Handling remove temp data.
1726 + *
1727 + * @param mixed $values temp import sites.
1728 + * @param int $index row key.
1729 + *
1730 + * @return bool true|false.
1420 1731 */
1421 - public function ajax_delete_non_mainwp_actions() {
1422 - $this->secure_request( 'mainwp_delete_non_mainwp_actions' );
1423 - // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1424 - $siteid = isset( $_POST['wp_id'] ) ? intval( $_POST['wp_id'] ) : 0;
1425 - if ( empty( $siteid ) ) {
1426 - wp_die( wp_json_encode( array( 'error' => 'Empty site ID' ) ) );
1732 + private function mainwp_handle_delete_temp_import_website( &$values, $index ) {
1733 + // Check if row data stream exists.
1734 + if ( ! empty( $values ) && is_array( $values ) && array_key_exists( $index, $values ) ) {
1735 + unset( $values[ $index ] ); // Remove row data.
1736 + return true;
1427 1737 }
1428 - // phpcs:enable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1429 - $website = MainWP_DB::instance()->get_website_by_id( $siteid );
1430 - $success = false;
1431 - $error = '';
1738 + return false;
1739 + }
1740 +
1741 + /**
1742 + * Method mainwp_handle_save_temp_import_website()
1743 + *
1744 + * Create value if empty or array key exists.
1745 + *
1746 + * @param mixed $values temp import sites.
1747 + * @param int $index row key.
1748 + * @param array $row_item new row.
1749 + *
1750 + * @return bool true.
1751 + */
1752 + private function mainwp_handle_save_temp_import_website( &$values, $index, $row_item ) {
1753 + if ( empty( $values ) || ! array_key_exists( $index, $values ) ) {
1754 + $index = 0 !== $index ? $index : 0;
1755 + $values[ $index ] = $row_item;
1756 + } else {
1757 + $values[ $index ] = $row_item;
1758 + }
1759 + return true;
1760 + }
1761 +
1762 + /**
1763 + * Method ajax_delete_temp_import_website().
1764 + *
1765 + * Delete data temp if has been added website.
1766 + *
1767 + * @uses MainWP_DB::instance()->get_general_option().
1768 + * @uses MainWP_DB::instance()->update_general_option().
1769 + */
1770 + public function ajax_delete_temp_import_website() {
1771 + $this->secure_request( 'mainwp_delete_temp_import_website' ); // Check secure.
1772 + $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1773 + // Get option temp import sites data.
1774 + $temp_sites = MainWP_DB::instance()->get_general_option( 'temp_import_sites', 'array' );
1775 + if ( empty( $temp_sites ) || ! is_array( $temp_sites ) ) { // Check if temp_sites data exists and is an array.
1776 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1777 + }
1778 +
1779 + // Get wpurl, wpadmin from POST.
1780 + $wp_url = $this->mainwp_get_sanitized_post( 'managesites_add_wpurl' );
1781 + $wp_admin = $this->mainwp_get_sanitized_post( 'managesites_add_wpadmin' );
1782 + if ( empty( $wp_url ) || empty( $wp_admin ) ) { // Check wpurl, wpadmin has data or empty.
1783 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1784 + }
1785 + $is_delete = false;
1786 +
1787 + if ( ! empty( $temp_sites ) && is_array( $temp_sites ) ) {
1788 + foreach ( $temp_sites as $k_site => $val_site ) { // Loop through data to remove added website.
1789 + // Check if row data stream exists.
1790 + if ( rtrim( $wp_url, '/' ) === $val_site['site_url'] && $wp_admin === $val_site['admin_name'] ) {
1791 + // Remove row data.
1792 + unset( $temp_sites[ $k_site ] );
1793 + $is_delete = true;
1794 + break;
1795 + }
1796 + }
1797 + }
1798 +
1799 + // Save data to options table.
1800 + if ( $is_delete ) {
1801 + MainWP_DB::instance()->update_general_option( 'temp_import_sites', $temp_sites, 'array' );
1802 + wp_die( wp_send_json_success( esc_html__( 'Delete import row successfully.', 'mainwp' ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1803 + }
1804 +
1805 + // In case of no delete.
1806 + wp_die( wp_send_json_error( $error_msg ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1807 + }
1808 +
1809 + /**
1810 + * Method ajax_import_website_add_client()
1811 + *
1812 + * Create client.
1813 + */
1814 + public function ajax_import_website_add_client() {
1815 + $this->secure_request( 'mainwp_import_website_add_client' ); // Check secure.
1816 + $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1432 1817 try {
1433 - $response = MainWP_Connect::fetch_url_authed( $website, 'delete_actions', array( 'del' => 'act' ) );
1434 - if ( is_array( $response ) ) {
1435 - if ( isset( $response['success'] ) ) {
1436 - $success = true;
1437 - } elseif ( isset( $response['error'] ) ) {
1438 - $error = $response['error'];
1818 + // Retrieve client data.
1819 + $data = $this->mainwp_get_sanitized_post( 'client' );
1820 + $site_id = $this->mainwp_get_sanitized_post( 'site_id' );
1821 +
1822 + if ( empty( $data ) ) {
1823 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1824 + }
1825 +
1826 + $client_data = json_decode( $data, true );
1827 + // Create client.
1828 + $client = $this->mainwp_handle_create_client( $client_data );
1829 + if ( $client ) {
1830 + // Add groups website and client.
1831 + if ( ! empty( $site_id ) ) {
1832 + $this->mainwp_handle_selected_sites_for_client( $client, $site_id );
1439 1833 }
1834 +
1835 + wp_die( wp_send_json_success( esc_html__( 'Created client successfully.', 'mainwp' ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1440 1836 }
1441 1837 } catch ( \Exception $e ) {
1442 - // ok!
1838 + wp_die( wp_send_json_error( sanitize_text_field( $e->getMessage() ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1443 1839 }
1444 - if ( $success ) {
1445 - MainWP_DB_Site_Actions::instance()->delete_action_by( 'wpid', $siteid );
1446 - wp_die( wp_json_encode( array( 'success' => 'ok' ) ) );
1840 + // In case of no created.
1841 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1842 + }
1843 +
1844 + /**
1845 + * Method ajax_import_website_add_client_no_site()
1846 + *
1847 + * Create client dont has a website.
1848 + */
1849 + public function ajax_import_website_add_client_no_site() {
1850 + $this->secure_request( 'mainwp_import_website_add_client_no_site' ); // Check secure.
1851 + $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1852 + try {
1853 + $data = ! empty( $_POST['client'] ) ? rest_sanitize_array( $_POST['client'] ) : array(); //phpcs:ignore -- NonceVerification.
1854 +
1855 + if ( empty( $data ) || ! is_array( $data ) ) {
1856 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1857 + }
1858 +
1859 + foreach ( $data as $val_data ) {
1860 + $client_data = json_decode( stripslashes( $val_data ), true ); // Decode client data.
1861 + $this->mainwp_handle_create_client( $client_data ); // Update or instert new client.
1862 + }
1863 + } catch ( \Exception $e ) {
1864 + wp_die( wp_send_json_error( sanitize_text_field( $e->getMessage() ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1447 1865 }
1866 + // In case of no created.
1867 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1868 + }
1448 1869
1449 - if ( empty( $error ) ) {
1450 - $error = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1870 + /**
1871 + * Method ajax_clients_add_multi_client
1872 + *
1873 + * Create Multi Client form QSW.
1874 + */
1875 + public function ajax_clients_add_multi_client() {
1876 + $error_msg = esc_html__( 'Undefined error. Please try again.', 'mainwp' );
1877 + $this->check_security( 'mainwp_clients_add_multi_client' );
1878 + $dataes = isset( $_POST['data'] ) ? $_POST['data'] : array(); //phpcs:ignore -- NonceVerification.
1879 + try {
1880 + if ( ! empty( $dataes ) ) {
1881 + foreach ( $dataes as $val_data ) {
1882 + $client_data = array(
1883 + 'image' => '',
1884 + 'name' => $val_data['client_name'] ?? '',
1885 + 'address_1' => '',
1886 + 'address_2' => '',
1887 + 'city' => '',
1888 + 'zip' => '',
1889 + 'state' => '',
1890 + 'country' => '',
1891 + 'note' => '',
1892 + 'selected_icon_info' => 'selected:wordpress;color:#34424d',
1893 + 'client_email' => $val_data['client_email'] ?? '',
1894 + 'client_phone' => '',
1895 + 'client_facebook' => '',
1896 + 'client_twitter' => '',
1897 + 'client_instagram' => '',
1898 + 'client_linkedin' => '',
1899 + 'suspended' => 0,
1900 + 'primary_contact_id' => 0,
1901 + );
1902 +
1903 + // Create client.
1904 + $client = $this->mainwp_handle_create_client( $client_data );
1905 +
1906 + // Create client successfy.
1907 + if ( ! empty( $client ) && ! empty( $val_data['website_id'] ) ) {
1908 + // Add groups website and client.
1909 + $this->mainwp_handle_selected_sites_for_client( $client, $val_data['website_id'] );
1910 + }
1911 + // Create client contact.
1912 + if ( ! empty( $val_data['contacts_field'] ) ) {
1913 + $this->mainwp_handle_create_contact_for_client( $client, $val_data['contacts_field'] );
1914 + }
1915 + }
1916 + wp_die( wp_send_json_success( esc_html__( 'Created client successfully.', 'mainwp' ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1917 + }
1918 + } catch ( \Exception $e ) {
1919 + wp_die( wp_send_json_error( sanitize_text_field( $e->getMessage() ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1451 1920 }
1452 - wp_die( wp_json_encode( array( 'error' => $error ) ) );
1921 +
1922 + wp_die( wp_send_json_error( esc_html( $error_msg ) ) ); //phpcs:ignore WordPress.Security.EscapeOutput
1453 1923 }
1454 1924
1455 1925 /**
1456 - * Method ajax_import_demo_data().
1926 + * Method ajax_increase_connection_security
1457 1927 */
1458 - public function ajax_import_demo_data() {
1459 - $this->secure_request( 'mainwp_import_demo_data' );
1460 - $data = MainWP_Demo_Handle::get_instance()->import_data_demo();
1461 - MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 1 );
1462 - wp_die( wp_json_encode( $data ) );
1928 + public function ajax_increase_connection_security() {
1929 + $this->check_security( 'mainwp_increase_connection_security' );
1930 + $websites = MainWP_DB::instance()->query( MainWP_DB::instance()->get_sql_websites_for_current_user( false, null, 'wp.url', false, false, null, true ) );
1931 + while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
1932 + // try to decrypt priv key.
1933 + $de_privkey = MainWP_Encrypt_Data_Lib::instance()->decrypt_privkey( base64_decode( $website->privkey ), $website->id ); // phpcs:ignore -- NOSONAR - base64_encode trust.
1934 + if ( empty( $de_privkey ) ) { // key is not encrypted.
1935 + $en_privkey = MainWP_Encrypt_Data_Lib::instance()->encrypt_privkey( base64_decode( $website->privkey ), $website->id, true ); //phpcs:ignore -- NOSONAR - trust - create encrypt priv key for the site.
1936 + if ( ! empty( $en_privkey['en_data'] ) ) {
1937 + MainWP_DB::instance()->update_website_values(
1938 + $website->id,
1939 + array(
1940 + 'privkey' => base64_encode( $en_privkey['en_data'] ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions -- NOSONAR - base64_encode trust.
1941 + )
1942 + );
1943 + }
1944 + }
1945 + }
1946 + MainWP_DB::free_result( $websites );
1947 + $this->hide_dashboard_notice_status( 'mainwp_secure_priv_key_notice' );
1948 + delete_option( 'mainwp_not_start_encrypt_keys' );
1949 + wp_die( wp_json_encode( array( 'success' => 1 ) ) );
1463 1950 }
1464 1951
1465 1952 /**
1466 - * Method ajax_delete_demo_data().
1953 + * Method ajax_ui_mode_detected
1467 1954 */
1468 - public function ajax_delete_demo_data() {
1469 - $this->secure_request( 'mainwp_delete_demo_data' );
1470 - $data = MainWP_Demo_Handle::get_instance()->delete_data_demo();
1471 - MainWP_Utility::update_option( 'mainwp_enable_guided_tours', 0 );
1472 - wp_die( wp_json_encode( $data ) );
1955 + public function ajax_ui_mode_detected() {
1956 + $this->check_security( 'mainwp_qsw_ui_mode_detected' );
1957 +
1958 + $detected_ui = isset( $_POST['ui_mode'] ) ? sanitize_text_field( wp_unslash( $_POST['ui_mode'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1959 +
1960 + $current_user_theme = get_user_option( 'mainwp_selected_theme' );
1961 + if ( empty( $current_user_theme ) ) {
1962 + if ( 'dark' === $detected_ui ) {
1963 + MainWP_Utility::update_user_option( 'mainwp_selected_theme', 'default-dark' );
1964 + } else {
1965 + MainWP_Utility::update_user_option( 'mainwp_selected_theme', 'default' );
1966 + }
1967 + }
1968 +
1969 + wp_die( wp_json_encode( array( 'success' => 1 ) ) );
1970 + }
1971 +
1972 +
1973 +
1974 + /**
1975 + * Handle ajax get changes logs for plugins/themes.
1976 + */
1977 + public function ajax_get_item_changes_logs() { //phpcs:ignore -- NOSONAR -complex.
1978 + $this->check_security( 'mainwp_changes_logs_get_item_changes' );
1979 +
1980 + $siteId = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1981 + $type = isset( $_POST['type'] ) ? sanitize_text_field( wp_unslash( $_POST['type'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1982 + $slug = isset( $_POST['slug'] ) ? sanitize_text_field( wp_unslash( $_POST['slug'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1983 + $name = isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( urldecode( $_POST['name'] ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1984 +
1985 + if ( 'plugin' === $type && ! empty( $slug ) ) {
1986 + if ( false !== strpos( $slug, '/' ) ) {
1987 + $parts = explode( '/', $slug );
1988 + $slug = $parts[0];
1989 + } else {
1990 + $slug = basename( $slug, '.php' );
1991 + }
1992 + } elseif ( 'theme' === $type && ! empty( $slug ) ) {
1993 + $slug = strtolower( $slug );
1994 + }
1995 +
1996 + // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
1997 + $target_dt = ! empty( $_POST['target_date'] ) ? sanitize_text_field( wp_unslash( $_POST['target_date'] ) ) : '';
1998 +
1999 + if ( ! in_array( $type, array( 'plugin', 'theme' ) ) || ( empty( $target_dt ) && ( empty( $siteId ) || ( empty( $slug ) && empty( $name ) ) ) ) ) {
2000 + wp_send_json( array( 'error' => esc_html__( 'Invalid request data. Please try again.', 'mainwp' ) ) );
2001 + }
2002 +
2003 + // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2004 + $from_date = isset( $_POST['from_date'] ) ? sanitize_text_field( wp_unslash( $_POST['from_date'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
2005 +
2006 + $results = Log_Changes_Logs_Helper::instance()->get_history_changes(
2007 + array(
2008 + 'wpid' => $siteId,
2009 + 'from_date' => $from_date,
2010 + 'days_number' => 10,
2011 + 'slug' => $slug,
2012 + 'type' => $type,
2013 + 'name' => $name,
2014 + 'target_date' => $target_dt,
2015 + )
2016 + );
2017 +
2018 + if ( ! is_array( $results ) ) {
2019 + $results = array();
2020 + }
2021 +
2022 + wp_send_json( $results );
2023 + }
2024 +
2025 + /**
2026 + * Handle self_connect action.
2027 + */
2028 + public function ajax_self_connect() {
2029 +
2030 + $mainwp_run = isset( $_POST['mainwp_run'] ) ? sanitize_text_field( wp_unslash( $_POST['mainwp_run'] ) ) : ''; // phpcs:ignore -- NOSONAR -ok.
2031 +
2032 + if ( 'self_connect' !== $mainwp_run ) {
2033 + status_header( 400 );
2034 + exit;
2035 + }
2036 +
2037 + MainWP_System_Utility::instance()->handle_self_connect();
2038 +
2039 + exit;
2040 + }
2041 +
2042 +
2043 + /**
2044 + * Method mainwp_get_sanitized_post()
2045 + *
2046 + * Sanitized post field.
2047 + *
2048 + * @param string $key key to get from POST.
2049 + * @param string $callback cleaning method.
2050 + *
2051 + * @return mixed data value.
2052 + */
2053 + public function mainwp_get_sanitized_post( $key, $callback = 'sanitize_text_field' ) {
2054 + return isset( $_POST[ $key ] ) ? $callback( wp_unslash( $_POST[ $key ] ) ) : ''; //phpcs:ignore -- NonceVerification.
2055 + }
2056 +
2057 + /**
2058 + * Method mainwp_handle_create_client()
2059 + *
2060 + * Create new client or return client data.
2061 + *
2062 + * @uses MainWP_DB_Client::instance()->get_wp_client_by()
2063 + * @uses MainWP_DB_Client::instance()->update_client()
2064 + *
2065 + * @param array $client_data new client creation data.
2066 + *
2067 + * @return object client data.
2068 + */
2069 + public function mainwp_handle_create_client( $client_data ) {
2070 + // Check if client does not exist then create new one.
2071 + $client = MainWP_DB_Client::instance()->get_wp_client_by( 'client_email', $client_data['client_email'], OBJECT );
2072 + if ( empty( $client ) ) {
2073 + $client = MainWP_DB_Client::instance()->update_client( $client_data ); // Create client.
2074 + }
2075 +
2076 + return $client;
2077 + }
2078 +
2079 + /**
2080 + * Method mainwp_handle_selected_sites_for_client()
2081 + *
2082 + * Update selected sites for client.
2083 + *
2084 + * @uses MainWP_DB_Client::instance()->get_websites_by_client_ids()
2085 + * @uses MainWP_DB_Client::instance()->update_selected_sites_for_client()
2086 + *
2087 + * @param object $client client data.
2088 + * @param int $website_id id.
2089 + */
2090 + public function mainwp_handle_selected_sites_for_client( $client, $website_id ) {
2091 + $sites_ids = MainWP_DB_Client::instance()->get_websites_by_client_ids( $client->client_id );
2092 + $ids = isset( $sites_ids ) && ! empty( $sites_ids ) ? array_column( $sites_ids, 'id' ) : array();
2093 + $ids[] = $website_id;
2094 + MainWP_DB_Client::instance()->update_selected_sites_for_client( $client->client_id, $ids );
2095 + }
2096 +
2097 + /**
2098 + * Method mainwp_handle_create_contact_for_client()
2099 + *
2100 + * Handle create contact for client.
2101 + *
2102 + * @uses MainWP_DB_Client::instance()->update_client_contact()
2103 + * @uses MMainWP_DB_Client::instance()->update_client()
2104 + *
2105 + * @param object $client client data.
2106 + * @param array $contacts_data contacts Data.
2107 + */
2108 + public function mainwp_handle_create_contact_for_client( $client, $contacts_data ) {
2109 + $contacts = array_filter( $contacts_data );
2110 + // Check required fields.
2111 + if ( ( isset( $contacts['contact_name'] ) && '' !== $contacts['contact_name'] ) && ( isset( $contacts['contact_email'] ) && $contacts['contact_email'] ) ) {
2112 + $contact_data = array(
2113 + 'contact_name' => $contacts['contact_name'] ?? '',
2114 + 'contact_email' => $contacts['contact_email'] ?? '',
2115 + 'contact_role' => $contacts['contact_role'] ?? '',
2116 + 'contact_client_id' => $client->client_id,
2117 + 'contact_phone' => '',
2118 + 'facebook' => '',
2119 + 'twitter' => '',
2120 + 'instagram' => '',
2121 + 'linkedin' => '',
2122 + 'contact_icon_info' => '',
2123 + );
2124 + $inserted = MainWP_DB_Client::instance()->update_client_contact( $contact_data ); // Create or update contact of client.
2125 + // If the update is successful, the client will be updated again.
2126 + if ( $inserted ) {
2127 + $update = array(
2128 + 'client_id' => $client->client_id,
2129 + 'primary_contact_id' => $inserted->contact_id,
2130 + );
2131 + MainWP_DB_Client::instance()->update_client( $update );
2132 + }
2133 + }
1473 2134 }
1474 2135 }