| 1 |
<?php |
| 2 |
/** |
| 3 |
* MainWP Connect Helper. |
| 4 |
* |
| 5 |
* MainWP Connect Helper functions. |
| 6 |
* |
| 7 |
* @package MainWP/Dashboard |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace MainWP\Dashboard; |
| 11 |
|
| 12 |
/** |
| 13 |
* Class MainWP_Connect_Helper |
| 14 |
* |
| 15 |
* @package MainWP\Dashboard |
| 16 |
*/ |
| 17 |
class MainWP_Connect_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR. |
| 18 |
|
| 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; |
| 27 |
|
| 28 |
|
| 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 === static::$instance ) { |
| 39 |
static::$instance = new self(); |
| 40 |
} |
| 41 |
return static::$instance; |
| 42 |
} |
| 43 |
|
| 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 |
} |
| 54 |
|
| 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 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Method ajax_prepare_renew_connections() |
| 96 |
*/ |
| 97 |
public function ajax_prepare_renew_connections() { // phpcs:ignore -- NOSONAR - complex. |
| 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 |
| 100 |
|
| 101 |
$data_fields = MainWP_System_Utility::get_default_map_site_fields(); |
| 102 |
|
| 103 |
$dbwebsites = array(); |
| 104 |
foreach ( $sites as $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 |
} |
| 112 |
|
| 113 |
if ( empty( $dbwebsites ) ) { |
| 114 |
wp_send_json( array( 'error' => esc_html__( 'Site not found. Please try again.' ) ) ); |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 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 |
} |
| 136 |
|
| 137 |
|
| 138 |
/** |
| 139 |
* Method ajax_renew_connections() |
| 140 |
*/ |
| 141 |
public function ajax_renew_connections() { |
| 142 |
|
| 143 |
MainWP_Post_Handler::instance()->secure_request( 'mainwp_renew_connections' ); |
| 144 |
|
| 145 |
$site_id = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 146 |
$website = false; |
| 147 |
|
| 148 |
if ( ! empty( $site_id ) ) { |
| 149 |
$website = MainWP_DB::instance()->get_website_by_id( $site_id ); |
| 150 |
} |
| 151 |
|
| 152 |
if ( empty( $website ) ) { |
| 153 |
wp_send_json( array( 'error' => esc_html__( 'Site not found.', 'mainwp' ) ) ); |
| 154 |
} |
| 155 |
|
| 156 |
if ( ! MainWP_System_Utility::can_edit_website( $website ) ) { |
| 157 |
wp_send_json( array( 'error' => 'You can not edit this website.' ) ); |
| 158 |
} |
| 159 |
|
| 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 |
} |
| 179 |
|
| 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 |
} |
| 191 |
} |
| 192 |
|