PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.0
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.0
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-post-site-handler.php

class-mainwp-post-site-handler.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0, at class/class-mainwp-post-site-handler.php

472 lines 15.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Post Site Handler.
4 *
5 * @package MainWP/Dashboard
6 */
7
8 namespace MainWP\Dashboard;
9
10 /**
11 * Class MainWP_Post_Site_Handler
12 *
13 * @package MainWP\Dashboard
14 *
15 * @uses \MainWP\Dashboard\MainWP_Post_Base_Handler
16 */
17 class MainWP_Post_Site_Handler extends MainWP_Post_Base_Handler {
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 * Method instance()
30 *
31 * Create MainWP Post Site Handler instance.
32 *
33 * @static
34 * @return self $instance MainWP_Post_Site_Handler.
35 */
36 public static function instance() {
37 if ( null === self::$instance ) {
38 self::$instance = new self();
39 }
40 return self::$instance;
41 }
42
43 /**
44 * Init site actions
45 */
46 public function init() {
47 // Page: ManageSites.
48 $this->add_action( 'mainwp_checkwp', array( &$this, 'mainwp_checkwp' ) );
49 $this->add_action( 'mainwp_addwp', array( &$this, 'mainwp_addwp' ) );
50 $this->add_action( 'mainwp_get_site_icon', array( &$this, 'get_site_icon' ) );
51 $this->add_action( 'mainwp_check_abandoned', array( &$this, 'check_abandoned' ) );
52
53 if ( mainwp_current_user_have_right( 'dashboard', 'test_connection' ) ) {
54 $this->add_action( 'mainwp_testwp', array( &$this, 'mainwp_testwp' ) );
55 }
56
57 $this->add_action( 'mainwp_removesite', array( &$this, 'mainwp_removesite' ) );
58 $this->add_action( 'mainwp_reconnectwp', array( &$this, 'mainwp_reconnectwp' ) );
59 $this->add_action( 'mainwp_updatechildsite_value', array( &$this, 'mainwp_updatechildsite_value' ) );
60
61 // Page: ManageGroups.
62 $this->add_action( 'mainwp_group_rename', array( &$this, 'mainwp_group_rename' ) );
63 $this->add_action( 'mainwp_group_delete', array( &$this, 'mainwp_group_delete' ) );
64 $this->add_action( 'mainwp_group_add', array( &$this, 'mainwp_group_add' ) );
65
66 $this->add_action( 'mainwp_group_getsites', array( &$this, 'mainwp_group_getsites' ) );
67 $this->add_action( 'mainwp_group_updategroup', array( &$this, 'mainwp_group_updategroup' ) );
68
69 // Widget: RightNow.
70 $this->add_action( 'mainwp_syncsites', array( &$this, 'mainwp_syncsites' ) );
71
72 $this->add_action( 'mainwp_checksites', array( &$this, 'mainwp_checksites' ) );
73 $this->add_action( 'mainwp_manage_sites_suspend_site', array( &$this, 'manage_suspend_site' ) );
74 $this->add_action( 'mainwp_group_sites_add', array( &$this, 'ajax_group_sites_add' ) );
75 }
76
77 /**
78 * Method mainwp_group_rename()
79 *
80 * Rename Group.
81 *
82 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::rename_group()
83 */
84 public function mainwp_group_rename() {
85 $this->secure_request( 'mainwp_group_rename' );
86
87 MainWP_Manage_Groups::rename_group();
88 }
89
90 /**
91 * Method mainwp_group_delete()
92 *
93 * Delete Group.
94 *
95 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::delete_group()
96 */
97 public function mainwp_group_delete() {
98 $this->secure_request( 'mainwp_group_delete' );
99
100 MainWP_Manage_Groups::delete_group();
101 }
102
103 /**
104 * Method mainwp_group_add()
105 *
106 * Add Group.
107 *
108 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::add_group()
109 */
110 public function mainwp_group_add() {
111 $this->secure_request( 'mainwp_group_add' );
112 MainWP_Manage_Groups::add_group();
113 }
114
115 /**
116 * Method ajax_group_sites_add()
117 *
118 * Add Group in modal.
119 */
120 public function ajax_group_sites_add() {
121 $this->secure_request( 'mainwp_group_sites_add' );
122 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
123 $newName = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
124 $newColor = isset( $_POST['newColor'] ) ? sanitize_hex_color( wp_unslash( $_POST['newColor'] ) ) : '';
125 if ( empty( $newColor ) && ! empty( $tmp_color ) ) {
126 $newColor = '#cce2ff'; // default.
127 }
128 $selected_sites = isset( $_POST['selected_sites'] ) && is_array( $_POST['selected_sites'] ) ? array_map( 'intval', wp_unslash( $_POST['selected_sites'] ) ) : array();
129 $selected_sites = array_filter( $selected_sites );
130 // phpcs:enable
131 $success = false;
132 if ( ! empty( $newName ) ) {
133 $success = MainWP_Manage_Groups::add_group_sites( $newName, $selected_sites, $newColor );
134 }
135 if ( ! $success ) {
136 wp_die( wp_json_encode( array( 'error' => esc_html__( 'Unexpected error occurred. Please try again.', 'mainwp' ) ) ) );
137 } else {
138 wp_die( wp_json_encode( array( 'success' => 1 ) ) );
139 }
140 }
141
142
143 /**
144 * Method mainwp_group_getsites()
145 *
146 * Get Child Sites in group.
147 */
148 public function mainwp_group_getsites() {
149
150 $this->secure_request( 'mainwp_group_getsites' );
151 //phpcs:disable WordPress.Security.NonceVerification.Missing
152 $groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
153 //phpcs:enable
154
155 if ( $groupid ) {
156 $group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
157 if ( ! empty( $group ) ) {
158 $websites = MainWP_DB::instance()->get_websites_by_group_id( $group->id );
159 $websiteIds = array();
160 if ( ! empty( $websites ) ) {
161 foreach ( $websites as $website ) {
162 $websiteIds[] = $website->id;
163 }
164 }
165
166 die( wp_json_encode( $websiteIds ) ); // phpcs:ignore WordPress.Security.EscapeOutput
167 }
168 }
169 die( 'ERROR' );
170 }
171
172 /**
173 * Method mainwp_group_updategroup()
174 *
175 * Update Group.
176 *
177 * @uses \MainWP\Dashboard\MainWP_Manage_Groups::update_group()
178 */
179 public function mainwp_group_updategroup() {
180 $this->secure_request( 'mainwp_group_updategroup' );
181
182 MainWP_Manage_Groups::update_group();
183 }
184
185 /**
186 * Method mainwp_checkwp()
187 *
188 * Check if WP can be added.
189 *
190 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::check_site()
191 */
192 public function mainwp_checkwp() {
193 $this->check_security( 'mainwp_checkwp', 'security' );
194 MainWP_Manage_Sites_Handler::check_site();
195 }
196
197 /**
198 * Method mainwp_addwp()
199 *
200 * Add WP to the database.
201 *
202 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::add_site()
203 */
204 public function mainwp_addwp() {
205 $this->check_security( 'mainwp_addwp', 'security' );
206 MainWP_Manage_Sites_Handler::add_site();
207 }
208
209 /**
210 * Method get_site_icon()
211 *
212 * Get Child Site Favicon.
213 *
214 * @uses \MainWP\Dashboard\MainWP_Sync::get_wp_icon()
215 */
216 public function get_site_icon() {
217 $this->check_security( 'mainwp_get_site_icon', 'security' );
218 $siteId = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
219 $result = MainWP_Sync::get_wp_icon( $siteId );
220 wp_send_json( $result );
221 }
222
223 /**
224 * Method check_abandoned()
225 *
226 * Check abandoned plugins or themes.
227 */
228 public function check_abandoned() {
229 $this->check_security( 'mainwp_check_abandoned', 'security' );
230 $siteId = isset( $_POST['siteId'] ) ? intval( $_POST['siteId'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
231 $which = isset( $_POST['which'] ) ? sanitize_text_field( wp_unslash( $_POST['which'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
232 $result = MainWP_Utility::check_abandoned( $siteId, $which );
233 wp_send_json( $result );
234 }
235
236 /**
237 * Method mainwp_testwp()
238 *
239 * Test if Child Site can be reached.
240 *
241 * @uses \MainWP\Dashboard\MainWP_Connect::try_visit()
242 * @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
243 * @uses \MainWP\Dashboard\MainWP_Utility::remove_http_prefix()
244 */
245 public function mainwp_testwp() { // phpcs:ignore -- complex function. Current complexity is the only way to achieve desired results, pull request solutions appreciated.
246 $this->secure_request( 'mainwp_testwp' );
247
248 $url = null;
249 $name = null;
250 $http_user = null;
251 $http_pass = null;
252 $verifyCertificate = 1;
253 $sslVersion = 0;
254
255 // phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
256 if ( isset( $_POST['url'] ) ) {
257 $url = sanitize_text_field( wp_unslash( $_POST['url'] ) );
258 $url = urldecode( $url );
259
260 $invalid = false;
261 $info = wp_parse_url( $url );
262
263 $def_not_allow = array( 21, 22 ); // not allow ports 21, 22.
264 $not_allow_ports = apply_filters( 'mainwp_connect_sites_not_allow_ports', $def_not_allow, $url );
265
266 if ( ! is_array( $not_allow_ports ) ) {
267 $not_allow_ports = $def_not_allow;
268 }
269
270 if ( is_array( $info ) && ! empty( $info['port'] ) && ( in_array( intval( $info['port'] ), $not_allow_ports, true ) ) ) {
271 $invalid = true;
272 }
273
274 $temp_url = MainWP_Utility::remove_http_prefix( $url, true );
275
276 if ( $invalid || false !== strpos( $url, '?=' ) ) {
277 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid URL.', 'mainwp' ) ) ) );
278 }
279
280 if ( strpos( $temp_url, ':' ) ) {
281 $invalid = true;
282 $allow_ports = apply_filters( 'mainwp_connect_sites_allow_ports', array(), $url );
283 if ( ! empty( $allow_ports ) && is_array( $allow_ports ) ) {
284 if ( is_array( $info ) && ! empty( $info['port'] ) && ( in_array( intval( $info['port'] ), $allow_ports, true ) ) ) {
285 $invalid = false;
286 }
287 }
288 if ( $invalid ) {
289 die( wp_json_encode( array( 'error' => esc_html__( 'Invalid URL.', 'mainwp' ) ) ) );
290 }
291 }
292
293 $verifyCertificate = isset( $_POST['test_verify_cert'] ) ? intval( $_POST['test_verify_cert'] ) : 1;
294 $forceUseIPv4 = apply_filters( 'mainwp_manage_sites_force_use_ipv4', false, $url );
295 $sslVersion = isset( $_POST['ssl_version'] ) ? intval( $_POST['ssl_version'] ) : 0;
296 $http_user = isset( $_POST['http_user'] ) ? sanitize_text_field( wp_unslash( $_POST['http_user'] ) ) : '';
297 $http_pass = isset( $_POST['http_pass'] ) ? wp_unslash( $_POST['http_pass'] ) : '';
298
299 } elseif ( isset( $_POST['siteid'] ) ) {
300 $website = MainWP_DB::instance()->get_website_by_id( intval( $_POST['siteid'] ) );
301 if ( $website ) {
302 $url = $website->url;
303 $name = $website->name;
304 $verifyCertificate = (int) $website->verify_certificate;
305 $forceUseIPv4 = $website->force_use_ipv4;
306 $sslVersion = $website->ssl_version;
307 $http_user = $website->http_user;
308 $http_pass = $website->http_pass;
309 }
310 }
311 // phpcs:enable
312
313 $ssl_verifyhost = false;
314
315 if ( 1 === $verifyCertificate ) {
316 $ssl_verifyhost = true;
317 } elseif ( 2 === $verifyCertificate ) {
318 if ( ( ( false === get_option( 'mainwp_sslVerifyCertificate' ) ) || ( 1 === (int) get_option( 'mainwp_sslVerifyCertificate' ) ) ) ) {
319 $ssl_verifyhost = true;
320 }
321 }
322
323 $rslt = MainWP_Connect::try_visit( $url, $ssl_verifyhost, $http_user, $http_pass, $sslVersion, $forceUseIPv4 );
324
325 if ( isset( $rslt['error'] ) && ( '' !== $rslt['error'] ) && ( 'wp-admin/' !== substr( $url, - 9 ) ) ) {
326 if ( substr( $url, - 1 ) !== '/' ) {
327 $url .= '/';
328 }
329 $url .= 'wp-admin/';
330 $newrslt = MainWP_Connect::try_visit( $url, $ssl_verifyhost, $http_user, $http_pass, $sslVersion, $forceUseIPv4 );
331 if ( isset( $newrslt['error'] ) && ( '' !== $rslt['error'] ) ) {
332 $rslt = $newrslt;
333 }
334 }
335
336 if ( null !== $name ) {
337 $rslt['sitename'] = esc_html( $name );
338 }
339
340 wp_send_json( $rslt );
341 }
342
343 /**
344 * Method mainwp_removesite()
345 *
346 * Remove a website from MainWP.
347 *
348 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::remove_site()
349 */
350 public function mainwp_removesite() {
351 if ( ! mainwp_current_user_have_right( 'dashboard', 'delete_sites' ) ) {
352 die( wp_json_encode( array( 'error' => mainwp_do_not_have_permissions( esc_html__( 'delete sites', 'mainwp' ), false ) ) ) );
353 }
354
355 $this->secure_request( 'mainwp_removesite' );
356
357 MainWP_Manage_Sites_Handler::remove_site();
358 }
359
360 /**
361 * Method mainwp_reconnectwp()
362 *
363 * Reconnect to Child Site.
364 *
365 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::reconnect_site()
366 */
367 public function mainwp_reconnectwp() {
368 $this->secure_request( 'mainwp_reconnectwp' );
369
370 MainWP_Manage_Sites_Handler::reconnect_site();
371 }
372
373 /**
374 * Method mainwp_updatechildsite_value()
375 *
376 * Update Child Site value.
377 *
378 * @uses \MainWP\Dashboard\MainWP_Manage_Sites_Handler::update_child_site_value()
379 */
380 public function mainwp_updatechildsite_value() {
381 $this->secure_request( 'mainwp_updatechildsite_value' );
382
383 MainWP_Manage_Sites_Handler::update_child_site_value();
384 }
385
386 /**
387 * Method mainwp_syncsites()
388 *
389 * Sync Child Sites.
390 *
391 * @uses \MainWP\Dashboard\MainWP_Updates_Overview::dismiss_sync_errors()
392 */
393 public function mainwp_syncsites() {
394 $this->secure_request( 'mainwp_syncsites' );
395 MainWP_Updates_Overview::dismiss_sync_errors( false );
396
397 $website = null;
398 $wp_id = isset( $_POST['wp_id'] ) ? intval( $_POST['wp_id'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
399 if ( $wp_id ) {
400 $website = MainWP_DB::instance()->get_website_by_id( $wp_id );
401 }
402
403 if ( null === $website ) {
404 die( wp_json_encode( array( 'error' => esc_html__( 'Site ID not found. Please reload the page and try again.', 'mainwp' ) ) ) );
405 }
406
407 if ( MainWP_Sync::sync_website( $website ) ) {
408 $website = MainWP_DB::instance()->get_website_by_id( $website->id ); // reload.
409 /**
410 * Fires immediately after website synced successfully.
411 *
412 * @since 4.6
413 *
414 * @param object $website website data.
415 */
416 do_action( 'mainwp_after_sync_site_success', $website );
417 die( wp_json_encode( array( 'result' => 'SUCCESS' ) ) );
418 }
419
420 $website = MainWP_DB::instance()->get_website_by_id( $website->id );
421
422 die( wp_json_encode( array( 'error' => esc_html( wp_strip_all_tags( $website->sync_errors ) ) ) ) );
423 }
424
425 /**
426 * Method mainwp_checksites()
427 *
428 * Check Child Sites.
429 *
430 * @uses \MainWP\Dashboard\MainWP_Monitoring_Handler::ajax_check_status_site()
431 */
432 public function mainwp_checksites() {
433 $this->secure_request( 'mainwp_checksites' );
434 MainWP_Monitoring_Handler::ajax_check_status_site();
435 }
436
437 /**
438 * Method mainwp_manage_sites_suspend_site()
439 *
440 * Check Child Sites.
441 *
442 * @uses \MainWP\Dashboard\MainWP_Sync::get_wp_icon()
443 */
444 public function manage_suspend_site() {
445 $this->secure_request( 'mainwp_manage_sites_suspend_site' );
446 $siteId = isset( $_POST['siteid'] ) ? intval( $_POST['siteid'] ) : null; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
447 $suspended = isset( $_POST['suspended'] ) && '1' === $_POST['suspended'] ? 1 : 0; // phpcs:ignore WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
448 $newValues = array(
449 'suspended' => $suspended,
450 );
451
452 if ( $siteId ) {
453 $website = MainWP_DB::instance()->get_website_by_id( $siteId );
454 if ( $website && $website->suspended != $suspended ) { //phpcs:ignore -- to valid.
455 MainWP_DB::instance()->update_website_values( $siteId, $newValues );
456 /**
457 * Fires immediately after website suspended/unsuspend.
458 *
459 * @since 4.5.1.1
460 *
461 * @param object $website website data.
462 * @param int $suspended The new suspended value.
463 */
464 do_action( 'mainwp_site_suspended', $website, $suspended );
465 }
466 wp_send_json( array( 'result' => 'success' ) );
467 } else {
468 wp_send_json( array( 'error' => 'Error: site id empty' ) );
469 }
470 }
471 }
472