PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / trunk
MainWP Dashboard: Self-hosted WordPress Management for Agencies vtrunk
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 / class / class-mainwp-connect-helper.php

class-mainwp-connect-helper.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies trunk, at class/class-mainwp-connect-helper.php

197 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Class MainWP_Connect_Helper
19 *
20 * @package MainWP\Dashboard
21 */
22 class MainWP_Connect_Helper { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
23
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;
32
33
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 }
48
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 }
59
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 }
98
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
105
106 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
107
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 }
117
118 if ( empty( $dbwebsites ) ) {
119 wp_send_json( array( 'error' => esc_html__( 'Site not found. Please try again.', 'mainwp' ) ) );
120 return;
121 }
122
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 }
141
142
143 /**
144 * Method ajax_renew_connections()
145 */
146 public function ajax_renew_connections() {
147
148 MainWP_Post_Handler::instance()->secure_request( 'mainwp_renew_connections' );
149
150 $site_id = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : 0; //phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
151 $website = false;
152
153 if ( ! empty( $site_id ) ) {
154 $website = MainWP_DB::instance()->get_website_by_id( $site_id );
155 }
156
157 if ( empty( $website ) ) {
158 wp_send_json( array( 'error' => esc_html__( 'Site not found.', 'mainwp' ) ) );
159 }
160
161 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
162 wp_send_json( array( 'error' => 'You can not edit this website.' ) );
163 }
164
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 }
184
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 }
196 }
197