| @@ -8,184 +8,189 @@ | ||
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | 10 | namespace MainWP\Dashboard; |
| 11 | 11 | |
| 12 | +// Exit if accessed directly. | |
| 13 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 14 | + exit; | |
| 15 | +} | |
| 16 | + | |
| 12 | 17 | /** |
| 13 | 18 | * Class MainWP_Connect_Helper |
| 14 | 19 | * |
| 15 | 20 | * @package MainWP\Dashboard |
| 16 | 21 | */ |
| 17 | -class MainWP_Connect_Helper { | |
| 22 | +class MainWP_Connect_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. | |
| 18 | 23 | |
| 19 | - /** | |
| 20 | - * Private static variable to hold the single instance of the class. | |
| 21 | - * | |
| 22 | - * @static | |
| 23 | - * | |
| 24 | - * @var mixed Default null | |
| 25 | - */ | |
| 26 | - private static $instance = null; | |
| 24 | + /** | |
| 25 | + * Private static variable to hold the single instance of the class. | |
| 26 | + * | |
| 27 | + * @static | |
| 28 | + * | |
| 29 | + * @var mixed Default null | |
| 30 | + */ | |
| 31 | + private static $instance = null; | |
| 27 | 32 | |
| 28 | 33 | |
| 29 | - /** | |
| 30 | - * Method instance() | |
| 31 | - * | |
| 32 | - * Create a public static instance. | |
| 33 | - * | |
| 34 | - * @static | |
| 35 | - * @return MainWP_Connect_Helper | |
| 36 | - */ | |
| 37 | - public static function instance() { | |
| 38 | - if ( null === self::$instance ) { | |
| 39 | - self::$instance = new self(); | |
| 40 | - } | |
| 41 | - return self::$instance; | |
| 42 | - } | |
| 34 | + /** | |
| 35 | + * Method instance() | |
| 36 | + * | |
| 37 | + * Create a public static instance. | |
| 38 | + * | |
| 39 | + * @static | |
| 40 | + * @return MainWP_Connect_Helper | |
| 41 | + */ | |
| 42 | + public static function instance() { | |
| 43 | + if ( null === static::$instance ) { | |
| 44 | + static::$instance = new self(); | |
| 45 | + } | |
| 46 | + return static::$instance; | |
| 47 | + } | |
| 43 | 48 | |
| 44 | - /** | |
| 45 | - * Method get_class_name() | |
| 46 | - * | |
| 47 | - * Get Class Name. | |
| 48 | - * | |
| 49 | - * @return object Class name. | |
| 50 | - */ | |
| 51 | - public static function get_class_name() { | |
| 52 | - return __CLASS__; | |
| 53 | - } | |
| 49 | + /** | |
| 50 | + * Method get_class_name() | |
| 51 | + * | |
| 52 | + * Get Class Name. | |
| 53 | + * | |
| 54 | + * @return object Class name. | |
| 55 | + */ | |
| 56 | + public static function get_class_name() { | |
| 57 | + return __CLASS__; | |
| 58 | + } | |
| 54 | 59 | |
| 55 | - /** | |
| 56 | - * Render renew connections modal box. | |
| 57 | - */ | |
| 58 | - public static function render_renew_connections_modal() { | |
| 59 | - ?> | |
| 60 | - <div class="ui small modal" id="mainwp-tool-renew-connect-modal"> | |
| 61 | - <i class="close icon mainwp-modal-close"></i> | |
| 62 | - <div class="header"><?php esc_html_e( 'Force your MainWP Dashboard to set a new pair of OpenSSL Keys', 'mainwp' ); ?></div> | |
| 63 | - <div class="ui green progress mainwp-modal-progress" style="display:none;"> | |
| 64 | - <div class="bar"><div class="progress"></div></div> | |
| 65 | - <div class="label"></div> | |
| 66 | - </div> | |
| 67 | - <div class="scrolling content mainwp-modal-content"> | |
| 68 | - <div class="ui message" id="mainwp-message-zone-modal" style="display:none;"></div> | |
| 69 | - <div class="ui middle aligned divided selection list" id="mainwp-renew-connections-list"></div> | |
| 70 | - <div class="mainwp-select-sites-wrapper"> | |
| 71 | - <div class="mainwp-select-sites ui fluid accordion mainwp-sidebar-accordion"> | |
| 72 | - <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Sites', 'mainwp' ); ?></div> | |
| 73 | - <div class="content active"> | |
| 74 | - <?php | |
| 75 | - $sel_params = array( | |
| 76 | - 'show_group' => false, | |
| 77 | - 'enable_offline_sites' => true, | |
| 78 | - 'show_select_all_disconnect' => true, | |
| 79 | - 'show_create_tag' => false, | |
| 80 | - ); | |
| 81 | - MainWP_UI_Select_Sites::select_sites_box( $sel_params ); | |
| 82 | - ?> | |
| 83 | - </div> | |
| 84 | - </div> | |
| 85 | - </div> | |
| 86 | - </div> | |
| 87 | - <div class="actions mainwp-modal-actions"> | |
| 88 | - <button class="ui green left floated button" onclick="mainwp_tool_prepare_renew_connections(this); return false;"><?php esc_html_e( 'Reset OpenSSL Key Pair', 'mainwp' ); ?></button> | |
| 89 | - </div> | |
| 90 | - </div> | |
| 91 | - <?php | |
| 92 | - } | |
| 60 | + /** | |
| 61 | + * Render renew connections modal box. | |
| 62 | + */ | |
| 63 | + public static function render_renew_connections_modal() { | |
| 64 | + ?> | |
| 65 | + <div class="ui small modal" id="mainwp-tool-renew-connect-modal"> | |
| 66 | + <i class="close icon mainwp-modal-close"></i> | |
| 67 | + <div class="header"><?php esc_html_e( 'Force your MainWP Dashboard to set a new pair of OpenSSL Keys', 'mainwp' ); ?></div> | |
| 68 | + <div class="ui green progress mainwp-modal-progress" style="display:none;"> | |
| 69 | + <div class="bar"><div class="progress"></div></div> | |
| 70 | + <div class="label"></div> | |
| 71 | + </div> | |
| 72 | + <div class="scrolling content mainwp-modal-content"> | |
| 73 | + <div class="ui message" id="mainwp-message-zone-modal" style="display:none;"></div> | |
| 74 | + <div class="ui middle aligned divided list" id="mainwp-renew-connections-list"></div> | |
| 75 | + <div class="mainwp-select-sites-wrapper"> | |
| 76 | + <div class="mainwp-select-sites ui fluid accordion mainwp-sidebar-accordion"> | |
| 77 | + <div class="title active"><i class="dropdown icon"></i> <?php esc_html_e( 'Select Sites', 'mainwp' ); ?></div> | |
| 78 | + <div class="content active"> | |
| 79 | + <?php | |
| 80 | + $sel_params = array( | |
| 81 | + 'show_group' => false, | |
| 82 | + 'enable_offline_sites' => true, | |
| 83 | + 'show_select_all_disconnect' => true, | |
| 84 | + 'show_create_tag' => false, | |
| 85 | + ); | |
| 86 | + MainWP_UI_Select_Sites::select_sites_box( $sel_params ); | |
| 87 | + ?> | |
| 88 | + </div> | |
| 89 | + </div> | |
| 90 | + </div> | |
| 91 | + </div> | |
| 92 | + <div class="actions mainwp-modal-actions"> | |
| 93 | + <button class="ui green left floated button" onclick="mainwp_tool_prepare_renew_connections(this); return false;"><?php esc_html_e( 'Reset OpenSSL Key Pair', 'mainwp' ); ?></button> | |
| 94 | + </div> | |
| 95 | + </div> | |
| 96 | + <?php | |
| 97 | + } | |
| 93 | 98 | |
| 94 | - /** | |
| 95 | - * Method ajax_prepare_renew_connections() | |
| 96 | - */ | |
| 97 | - public function ajax_prepare_renew_connections() { | |
| 98 | - MainWP_Post_Handler::instance()->secure_request( 'mainwp_prepare_renew_connections' ); | |
| 99 | - $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 99 | + /** | |
| 100 | + * Method ajax_prepare_renew_connections() | |
| 101 | + */ | |
| 102 | + public function ajax_prepare_renew_connections() { // phpcs:ignore -- NOSONAR - complex. | |
| 103 | + MainWP_Post_Handler::instance()->secure_request( 'mainwp_prepare_renew_connections' ); | |
| 104 | + $sites = isset( $_POST['sites'] ) && is_array( $_POST['sites'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['sites'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 100 | 105 | |
| 101 | - $data_fields = MainWP_System_Utility::get_default_map_site_fields(); | |
| 106 | + $data_fields = MainWP_System_Utility::get_default_map_site_fields(); | |
| 102 | 107 | |
| 103 | - $dbwebsites = array(); | |
| 104 | - foreach ( $sites as $k => $v ) { | |
| 105 | - if ( MainWP_Utility::ctype_digit( $v ) ) { | |
| 106 | - $website = MainWP_DB::instance()->get_website_by_id( $v ); | |
| 107 | - if ( $website ) { | |
| 108 | - $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields ); | |
| 109 | - } | |
| 110 | - } | |
| 111 | - } | |
| 108 | + $dbwebsites = array(); | |
| 109 | + foreach ( $sites as $v ) { | |
| 110 | + if ( MainWP_Utility::ctype_digit( $v ) ) { | |
| 111 | + $website = MainWP_DB::instance()->get_website_by_id( $v ); | |
| 112 | + if ( $website ) { | |
| 113 | + $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields ); | |
| 114 | + } | |
| 115 | + } | |
| 116 | + } | |
| 112 | 117 | |
| 113 | - if ( empty( $dbwebsites ) ) { | |
| 114 | - wp_send_json( array( 'error' => esc_html__( 'Site not found. Please try again.' ) ) ); | |
| 115 | - return; | |
| 116 | - } | |
| 118 | + if ( empty( $dbwebsites ) ) { | |
| 119 | + wp_send_json( array( 'error' => esc_html__( 'Site not found. Please try again.', 'mainwp' ) ) ); | |
| 120 | + return; | |
| 121 | + } | |
| 117 | 122 | |
| 118 | - ob_start(); | |
| 119 | - foreach ( $dbwebsites as $site ) { | |
| 120 | - $site_name = $site->name; | |
| 121 | - $is_sync_err = ( '' !== $site->sync_errors ) ? true : false; | |
| 122 | - ?> | |
| 123 | - <div class="item <?php echo $is_sync_err ? 'disconnected-site' : ''; ?>" status="queue"> | |
| 124 | - <div class="right floated content"> | |
| 125 | - <div class="renew-site-status" niceurl="<?php echo esc_html( $site_name ); ?>" siteid="<?php echo intval( $site->id ); ?>"><span data-position="left center" data-inverted="" data-tooltip="<?php echo $is_sync_err ? esc_html__( 'Site disconnected', 'mainwp' ) : esc_html__( 'Pending', 'mainwp' ); ?>"><i class="<?php echo $is_sync_err ? 'exclamation red icon' : 'clock outline icon'; ?>"></i></span></div> | |
| 126 | - </div> | |
| 127 | - <div class="content"> | |
| 128 | - <?php echo esc_html( $site_name ); ?> | |
| 129 | - </div> | |
| 130 | - </div> | |
| 131 | - <?php | |
| 132 | - } | |
| 133 | - $output = ob_get_clean(); | |
| 134 | - wp_send_json( array( 'result' => $output ) ); | |
| 135 | - } | |
| 123 | + ob_start(); | |
| 124 | + foreach ( $dbwebsites as $site ) { | |
| 125 | + $site_name = $site->name; | |
| 126 | + $is_sync_err = ( '' !== $site->sync_errors ) ? true : false; | |
| 127 | + ?> | |
| 128 | + <div class="item <?php echo $is_sync_err ? 'disconnected-site' : ''; ?>" status="queue"> | |
| 129 | + <div class="right floated content"> | |
| 130 | + <div class="renew-site-status" niceurl="<?php echo esc_html( $site_name ); ?>" siteid="<?php echo intval( $site->id ); ?>"><span data-position="left center" data-inverted="" data-tooltip="<?php echo $is_sync_err ? esc_html__( 'Site disconnected', 'mainwp' ) : esc_html__( 'Pending', 'mainwp' ); ?>"><i class="<?php echo $is_sync_err ? 'exclamation red icon' : 'clock outline icon'; ?>"></i></span></div> | |
| 131 | + </div> | |
| 132 | + <div class="content"> | |
| 133 | + <?php echo esc_html( $site_name ); ?> | |
| 134 | + </div> | |
| 135 | + </div> | |
| 136 | + <?php | |
| 137 | + } | |
| 138 | + $output = ob_get_clean(); | |
| 139 | + wp_send_json( array( 'result' => $output ) ); | |
| 140 | + } | |
| 136 | 141 | |
| 137 | 142 | |
| 138 | - /** | |
| 139 | - * Method ajax_renew_connections() | |
| 140 | - */ | |
| 141 | - public function ajax_renew_connections() { | |
| 143 | + /** | |
| 144 | + * Method ajax_renew_connections() | |
| 145 | + */ | |
| 146 | + public function ajax_renew_connections() { | |
| 142 | 147 | |
| 143 | - MainWP_Post_Handler::instance()->secure_request( 'mainwp_renew_connections' ); | |
| 148 | + MainWP_Post_Handler::instance()->secure_request( 'mainwp_renew_connections' ); | |
| 144 | 149 | |
| 145 | - $site_id = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 146 | - $website = false; | |
| 150 | + $site_id = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 151 | + $website = false; | |
| 147 | 152 | |
| 148 | - if ( ! empty( $site_id ) ) { | |
| 149 | - $website = MainWP_DB::instance()->get_website_by_id( $site_id ); | |
| 150 | - } | |
| 153 | + if ( ! empty( $site_id ) ) { | |
| 154 | + $website = MainWP_DB::instance()->get_website_by_id( $site_id ); | |
| 155 | + } | |
| 151 | 156 | |
| 152 | - if ( empty( $website ) ) { | |
| 153 | - wp_send_json( array( 'error' => esc_html__( 'Site not found.', 'mainwp' ) ) ); | |
| 154 | - } | |
| 157 | + if ( empty( $website ) ) { | |
| 158 | + wp_send_json( array( 'error' => esc_html__( 'Site not found.', 'mainwp' ) ) ); | |
| 159 | + } | |
| 155 | 160 | |
| 156 | - if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { | |
| 157 | - wp_send_json( array( 'error' => 'You can not edit this website.' ) ); | |
| 158 | - } | |
| 161 | + if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { | |
| 162 | + wp_send_json( array( 'error' => 'You can not edit this website.' ) ); | |
| 163 | + } | |
| 159 | 164 | |
| 160 | - try { | |
| 161 | - // if disconnected, try to reconnect. | |
| 162 | - if ( '' !== $website->sync_errors ) { | |
| 163 | - // try reconnect, if failed. | |
| 164 | - MainWP_Sync::sync_site( $website, true ); | |
| 165 | - } | |
| 166 | - $data = MainWP_Connect::fetch_url_authed( $website, 'renew' ); // to disconnect, | |
| 167 | - // disconnect success. | |
| 168 | - if ( is_array( $data ) && isset( $data['result'] ) && 'success' === $data['result'] ) { | |
| 169 | - // reconnect immediately, to renew. | |
| 170 | - if ( MainWP_Manage_Sites_View::m_reconnect_site( $website, false ) ) { | |
| 171 | - MainWP_Logger::instance()->info_for_website( $website, 'renew', 'Renew connection successfully.' ); | |
| 172 | - wp_send_json( array( 'result' => 'success' ) ); | |
| 173 | - } else { | |
| 174 | - wp_send_json( array( 'error' => esc_html__( 'Try to reconnect site failed. Please try again.', 'mainwp' ) ) ); | |
| 175 | - } | |
| 176 | - } else { | |
| 177 | - MainWP_Logger::instance()->info_for_website( $website, 'renew', 'Try to disconnect site failed.' ); | |
| 178 | - } | |
| 165 | + try { | |
| 166 | + // if disconnected, try to reconnect. | |
| 167 | + if ( '' !== $website->sync_errors ) { | |
| 168 | + // try reconnect, if failed. | |
| 169 | + MainWP_Sync::sync_site( $website, true ); | |
| 170 | + } | |
| 171 | + $data = MainWP_Connect::fetch_url_authed( $website, 'renew' ); // to disconnect, | |
| 172 | + // disconnect success. | |
| 173 | + if ( is_array( $data ) && isset( $data['result'] ) && 'success' === $data['result'] ) { | |
| 174 | + // reconnect immediately, to renew. | |
| 175 | + if ( MainWP_Manage_Sites_View::m_reconnect_site( $website, false ) ) { | |
| 176 | + MainWP_Logger::instance()->info_for_website( $website, 'renew', 'Renew connection successfully.' ); | |
| 177 | + wp_send_json( array( 'result' => 'success' ) ); | |
| 178 | + } else { | |
| 179 | + wp_send_json( array( 'error' => esc_html__( 'Try to reconnect site failed. Please try again.', 'mainwp' ) ) ); | |
| 180 | + } | |
| 181 | + } else { | |
| 182 | + MainWP_Logger::instance()->info_for_website( $website, 'renew', 'Try to disconnect site failed.' ); | |
| 183 | + } | |
| 179 | 184 | |
| 180 | - if ( is_array( $data ) && isset( $data['error'] ) ) { | |
| 181 | - wp_send_json( array( 'error' => esc_html( wp_strip_all_tags( $data['error'] ) ) ) ); | |
| 182 | - } else { | |
| 183 | - wp_send_json( array( 'error' => esc_html__( 'Try to disconnect site failed. Please try again.', 'mainwp' ) ) ); | |
| 184 | - } | |
| 185 | - } catch ( MainWP_Exception $e ) { | |
| 186 | - $error = MainWP_Error_Helper::get_error_message( $e ); | |
| 187 | - wp_send_json( array( 'error' => esc_html( wp_strip_all_tags( $error ) ) ) ); | |
| 188 | - } | |
| 189 | - wp_send_json( array( 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ) ) ); | |
| 190 | - } | |
| 185 | + if ( is_array( $data ) && isset( $data['error'] ) ) { | |
| 186 | + wp_send_json( array( 'error' => esc_html( wp_strip_all_tags( $data['error'] ) ) ) ); | |
| 187 | + } else { | |
| 188 | + wp_send_json( array( 'error' => esc_html__( 'Try to disconnect site failed. Please try again.', 'mainwp' ) ) ); | |
| 189 | + } | |
| 190 | + } catch ( MainWP_Exception $e ) { | |
| 191 | + $error = MainWP_Error_Helper::get_error_message( $e ); | |
| 192 | + wp_send_json( array( 'error' => esc_html( wp_strip_all_tags( $error ) ) ) ); | |
| 193 | + } | |
| 194 | + wp_send_json( array( 'error' => esc_html__( 'Undefined error. Please try again.', 'mainwp' ) ) ); | |
| 195 | + } | |
| 191 | 196 | } |