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-db-common.php

class-mainwp-db-common.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.0, at class/class-mainwp-db-common.php

881 lines 23.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Controller
4 *
5 * This file handles all interactions with the DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 /**
13 * Class MainWP_DB_Common
14 *
15 * @package MainWP\Dashboard
16 */
17 class MainWP_DB_Common extends MainWP_DB {
18
19 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
20
21 /**
22 * Private static variable to hold the single instance of the class.
23 *
24 * @static
25 *
26 * @var mixed Default null
27 */
28 private static $instance = null;
29
30 /**
31 * Method instance()
32 *
33 * Create public static instance.
34 *
35 * @static
36 * @return MainWP_DB_Common
37 */
38 public static function instance() {
39 if ( null === self::$instance ) {
40 self::$instance = new self();
41 }
42 return self::$instance;
43 }
44
45 /**
46 * Method get_last_sync_status()
47 *
48 * Get last sync status.
49 *
50 * @param string|null $userId User ID.
51 *
52 * @return string $return all_synced|not_synced|last_sync
53 */
54 public function get_last_sync_status( $userId = null ) {
55 $sql = $this->get_sql_websites_for_current_user();
56 $websites = $this->query( $sql );
57
58 $return = array(
59 'sync_status' => false,
60 'last_sync' => 0,
61 );
62
63 if ( ! $websites ) {
64 $return['sync_status'] = 'all_synced';
65 return $return;
66 }
67
68 $total_sites = 0;
69 $synced_sites = 0;
70 $last_sync = 0;
71 self::data_seek( $websites, 0 );
72 while ( $websites && ( $website = self::fetch_object( $websites ) ) ) {
73 if ( empty( $website ) || '' !== $website->sync_errors ) {
74 continue;
75 }
76 ++$total_sites;
77 if ( 60 * 60 * 24 > time() - $website->dtsSync ) {
78 ++$synced_sites;
79 }
80 if ( $last_sync < $website->dtsSync ) {
81 $last_sync = $website->dtsSync;
82 }
83 }
84
85 if ( $total_sites === $synced_sites ) {
86 $return['sync_status'] = 'all_synced';
87 } elseif ( 0 === $synced_sites ) {
88 $return['sync_status'] = 'not_synced';
89 }
90 $return['last_sync'] = $last_sync;
91 return $return;
92 }
93
94 /**
95 * Method get_group_by_name()
96 *
97 * Get group by name.
98 *
99 * @param mixed $name Group name.
100 * @param null $userid user ID.
101 *
102 * @return object|null Database query result for chosen group name or null on failure
103 *
104 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
105 */
106 public function get_group_by_name( $name, $userid = null ) {
107 if ( ( null === $userid ) && MainWP_System::instance()->is_multi_user() ) {
108
109 /**
110 * Current user global.
111 *
112 * @global string
113 */
114 global $current_user;
115
116 $userid = $current_user->ID;
117 }
118 $where = ( null !== $userid ) ? ' AND userid=' . intval( $userid ) : '';
119 $where .= $this->get_sql_where_allow_groups();
120
121 return $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'group' ) . ' WHERE 1 ' . $where . ' AND name= %s', $this->escape( $name ) ) );
122 }
123
124 /**
125 * Method get_group_by_id()
126 *
127 * Get group by ID.
128 *
129 * @param mixed $id Group ID.
130 *
131 * @return object|null Database query result for chosen Group ID or null on failure.
132 *
133 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
134 */
135 public function get_group_by_id( $id ) {
136 if ( MainWP_Utility::ctype_digit( $id ) ) {
137 $group = $this->wpdb->get_row( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'group' ) . ' WHERE id= %d', $id ) );
138 return $group;
139 }
140
141 return null;
142 }
143
144 /**
145 * Method get_groups_for_manage_sites()
146 *
147 * Get groups for mananged sites.
148 *
149 * @return object|null Database query result for Managed Sites Groups or null on failure.
150 *
151 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
152 */
153 public function get_groups_for_manage_sites() {
154 $where = ' 1 ';
155 if ( MainWP_System::instance()->is_multi_user() ) {
156
157 /**
158 * Current user global.
159 *
160 * @global string
161 */
162 global $current_user;
163
164 $where = ' userid = ' . $current_user->ID . ' ';
165 }
166 $with_staging = 'yes';
167 $staging_enabled = is_plugin_active( 'mainwp-staging-extension/mainwp-staging-extension.php' ) || is_plugin_active( 'mainwp-timecapsule-extension/mainwp-timecapsule-extension.php' );
168
169 if ( ! $staging_enabled ) {
170 $with_staging = 'no';
171 }
172
173 $where .= $this->get_sql_where_allow_groups( '', $with_staging );
174
175 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'group' ) . ' WHERE ' . $where . ' ORDER BY name', OBJECT_K );
176 }
177
178 /**
179 * Method get_groups_for_current_user()
180 *
181 * Get groups for current user.
182 *
183 * @return object|null Database query result for Current User Groups or null on failure.
184 *
185 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
186 */
187 public function get_groups_for_current_user() {
188 $where = ' 1 ';
189 if ( MainWP_System::instance()->is_multi_user() ) {
190
191 /**
192 * Current user global.
193 *
194 * @global string
195 */
196 global $current_user;
197
198 $where = ' userid = ' . $current_user->ID . ' ';
199 }
200 $where .= $this->get_sql_where_allow_groups();
201
202 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'group' ) . ' WHERE ' . $where . ' ORDER BY name', OBJECT_K );
203 }
204
205 /**
206 * Method get_groups_by_website_id()
207 *
208 * Get groups by website ID.
209 *
210 * @param mixed $websiteid Child Site ID.
211 *
212 * @return object|null Database query result for groups by website ID or null on failure.
213 *
214 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
215 */
216 public function get_groups_by_website_id( $websiteid ) {
217 if ( MainWP_Utility::ctype_digit( $websiteid ) ) {
218 return $this->wpdb->get_results(
219 $this->wpdb->prepare(
220 'SELECT * FROM ' . $this->table_name( 'group' ) . ' gr JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON gr.id = wpgr.groupid WHERE wpgr.wpid = %d ORDER BY name',
221 $websiteid
222 ),
223 OBJECT_K
224 );
225 }
226
227 return null;
228 }
229
230 /**
231 * Medthod get_groups_and_count()
232 *
233 * Get groups and count.
234 *
235 * @param null $userid Current user ID.
236 * @param bool $for_manager Default: false.
237 *
238 * @return object|null Database query result for groups and count or null on failure.
239 *
240 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
241 */
242 public function get_groups_and_count( $userid = null, $for_manager = false ) {
243 if ( ( null === $userid ) && MainWP_System::instance()->is_multi_user() ) {
244
245 /**
246 * Current user global.
247 *
248 * @global string
249 */
250 global $current_user;
251
252 $userid = $current_user->ID;
253 }
254
255 $where = '';
256
257 if ( ! empty( $userid ) ) {
258 $where = ' AND gr.userid = ' . intval( $userid );
259 }
260
261 if ( ! $for_manager ) {
262 $where .= $this->get_sql_where_allow_groups( 'gr' );
263 }
264
265 return $this->wpdb->get_results( 'SELECT gr.*, COUNT(DISTINCT(wpgr.wpid)) as nrsites FROM ' . $this->table_name( 'group' ) . ' gr LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON gr.id = wpgr.groupid WHERE 1 ' . $where . ' GROUP BY gr.id ORDER BY gr.name', OBJECT_K );
266 }
267
268 /**
269 * Method get_not_empty_groups()
270 *
271 * Get non-empty groups.
272 *
273 * @param null $userid Current user ID.
274 * @param bool $enableOfflineSites Include offline sites? Default: true.
275 *
276 * @return object|null Database query result for non-empty groups or null on failure.
277 *
278 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
279 */
280 public function get_not_empty_groups( $userid = null, $enableOfflineSites = true ) {
281 if ( ( null === $userid ) && MainWP_System::instance()->is_multi_user() ) {
282
283 /**
284 * Current user global.
285 *
286 * @global string
287 */
288 global $current_user;
289
290 $userid = $current_user->ID;
291 }
292
293 $where = ' WHERE 1 ';
294 $where .= $this->get_sql_where_allow_groups( 'g' );
295
296 if ( null !== $userid ) {
297 $where .= ' AND g.userid = ' . intval( $userid );
298 }
299 if ( ! $enableOfflineSites ) {
300 $where .= ' AND wp_sync.sync_errors = ""';
301 }
302
303 return $this->wpdb->get_results( 'SELECT DISTINCT(g.id), g.name, count(wp.wpid) FROM ' . $this->table_name( 'group' ) . ' g JOIN ' . $this->table_name( 'wp_group' ) . ' wp ON g.id = wp.groupid JOIN ' . $this->table_name( 'wp' ) . ' wpsite ON wp.wpid = wpsite.id JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.wpid = wp_sync.wpid ' . $where . ' GROUP BY g.id HAVING count(wp.wpid) > 0 ORDER BY g.name', OBJECT_K );
304 }
305
306 /**
307 * Method get_sql_log()
308 *
309 * Get sql log.
310 *
311 * @return string sql query.
312 */
313 public function get_sql_log() {
314 return 'SELECT log.*
315 FROM ' . $this->table_name( 'action_log' ) . ' log
316 WHERE 1 LIMIT 0, 300 ';
317 }
318
319 /**
320 * Method insert_action_log()
321 *
322 * Insert action log.
323 *
324 * @param array $data log data.
325 *
326 * @return void
327 */
328 public function insert_action_log( $data ) {
329 $this->wpdb->insert( $this->table_name( 'action_log' ), $data );
330 }
331
332 /**
333 * Method delete_action_log()
334 *
335 * Delete action log.
336 *
337 * @param int $days number days.
338 *
339 * @return void
340 */
341 public function delete_action_log( $days = false ) {
342 $where = '';
343 if ( ! empty( $days ) ) {
344 $where .= ' AND log_timestamp < ' . ( time() - $days * DAY_IN_SECONDS );
345 }
346 $this->wpdb->query( 'DELETE FROM `' . $this->table_name( 'action_log' ) . '` WHERE 1 ' . $where );
347 }
348
349 /**
350 * Method insert_or_update_request_log()
351 *
352 * Insert or update request log.
353 *
354 * @param mixed $wpid WordPress ID.
355 * @param mixed $ip IP address.
356 * @param mixed $start Start time.
357 * @param mixed $stop Stop Time.
358 *
359 * @return void
360 */
361 public function insert_or_update_request_log( $wpid, $ip, $start, $stop ) {
362 $updateValues = array();
363 if ( ! empty( $ip ) ) {
364 $updateValues['ip'] = $ip;
365 }
366 if ( ! empty( $start ) ) {
367 $updateValues['micro_timestamp_start'] = $start;
368 }
369 if ( ! empty( $stop ) ) {
370 $updateValues['micro_timestamp_stop'] = $stop;
371 }
372
373 $var = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT id FROM ' . $this->table_name( 'request_log' ) . ' WHERE wpid = %d ', $wpid ) );
374 if ( null !== $var ) {
375 $this->wpdb->update( $this->table_name( 'request_log' ), $updateValues, array( 'wpid' => $wpid ) );
376 } else {
377 $updateValues['wpid'] = $wpid;
378 $this->wpdb->insert( $this->table_name( 'request_log' ), $updateValues );
379 }
380 }
381
382 /**
383 * Method close_open_requests()
384 *
385 * Close open request.
386 *
387 * @return void
388 */
389 public function close_open_requests() {
390 // Close requests open longer then 7 seconds.. something is wrong here.
391 $this->wpdb->query( 'UPDATE ' . $this->table_name( 'request_log' ) . ' SET micro_timestamp_stop = micro_timestamp_start WHERE micro_timestamp_stop < micro_timestamp_start and ' . microtime( true ) . ' - micro_timestamp_start > 7' );
392 }
393
394 /**
395 * Method get_nrof_open_requests()
396 *
397 * Get number of requests.
398 *
399 * @param null $ip IP Address.
400 *
401 * @return (string|null) Database query result for number of requests or null on failure.
402 */
403 public function get_nrof_open_requests( $ip = null ) {
404 if ( null === $ip ) {
405 return $this->wpdb->get_var( 'select count(id) from ' . $this->table_name( 'request_log' ) . ' where micro_timestamp_stop < micro_timestamp_start' );
406 }
407
408 return $this->wpdb->get_var( 'select count(id) from ' . $this->table_name( 'request_log' ) . ' where micro_timestamp_stop < micro_timestamp_start and ip = "' . esc_sql( $ip ) . '"' );
409 }
410
411 /**
412 * Method get_last_request_timestamp()
413 *
414 * Get timestamp of last request sent.
415 *
416 * @param null $ip Child Site IP address, default: null.
417 *
418 * @return (int|null) Database query result for timestamp of last request sent or null on failure.
419 */
420 public function get_last_request_timestamp( $ip = null ) {
421 if ( null === $ip ) {
422 return $this->wpdb->get_var( 'select micro_timestamp_start from ' . $this->table_name( 'request_log' ) . ' order by micro_timestamp_start desc limit 1' );
423 }
424
425 return $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT micro_timestamp_start FROM ' . $this->table_name( 'request_log' ) . ' WHERE ip = %s order by micro_timestamp_start desc limit 1', esc_sql( $ip ) ) );
426 }
427
428 /**
429 * Method update_group_site()
430 *
431 * @param mixed $groupId Group ID.
432 * @param mixed $websiteId Child Site ID.
433 *
434 * @return void
435 */
436 public function update_group_site( $groupId, $websiteId ) {
437 $this->wpdb->insert(
438 $this->table_name( 'wp_group' ),
439 array(
440 'wpid' => $websiteId,
441 'groupid' => $groupId,
442 )
443 );
444 }
445
446 /**
447 * Method clear_group()
448 *
449 * Clear sites in group.
450 *
451 * @param mixed $groupId ID of group.
452 */
453 public function clear_group( $groupId ) {
454 $this->wpdb->query( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE groupid=' . $groupId );
455 }
456
457
458 /**
459 * Method add_group()
460 *
461 * Add group.
462 *
463 * @param mixed $userid Current User ID.
464 * @param mixed $name Name of group to add.
465 * @param mixed $color Color of group to add.
466 *
467 * @return boolean true
468 *
469 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
470 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
471 */
472 public function add_group( $userid, $name, $color = '' ) {
473 if ( MainWP_Utility::ctype_digit( $userid ) ) {
474 if ( $this->wpdb->insert(
475 $this->table_name( 'group' ),
476 array(
477 'userid' => $userid,
478 'name' => $this->escape( $name ),
479 'color' => $this->escape( $color ),
480 )
481 )
482 ) {
483
484 $groupId = $this->wpdb->insert_id;
485
486 $group = $this->get_group_by_id( $groupId );
487
488 /**
489 * Fires after a new sites tag has been created.
490 *
491 * @param object $group group created.
492 * @param string group action.
493 */
494 do_action( 'mainwp_site_tag_action', $group, 'created' );
495
496 return $groupId;
497 }
498 }
499
500 return false;
501 }
502
503 /**
504 * Method remove_group()
505 *
506 * Remove group.
507 *
508 * @param mixed $groupid Group ID.
509 *
510 * @return int|boolean Group that was deleted or false on failure.
511 *
512 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
513 */
514 public function remove_group( $groupid ) {
515 if ( MainWP_Utility::ctype_digit( $groupid ) ) {
516 $nr = $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'group' ) . ' WHERE id=%d', $groupid ) );
517 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE groupid=%d', $groupid ) );
518
519 return $nr;
520 }
521
522 return false;
523 }
524
525 /**
526 * Method update_note()
527 *
528 * Update Note.
529 *
530 * @param mixed $websiteid Child Site ID.
531 * @param mixed $note Note data.
532 *
533 * @return void
534 */
535 public function update_note( $websiteid, $note ) {
536 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET note= %s WHERE id=%d', $this->escape( $note ), $websiteid ) );
537 }
538
539 /**
540 * Method update_group()
541 *
542 * Update group.
543 *
544 * @param mixed $groupid Group ID.
545 * @param mixed $groupname Group Name.
546 * @param string $groupcolor Group Color.
547 *
548 * @return boolean true|false.
549 *
550 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
551 */
552 public function update_group( $groupid, $groupname, $groupcolor ) {
553 if ( MainWP_Utility::ctype_digit( $groupid ) ) {
554 // update groupname.
555 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'group' ) . ' SET name=%s, color=%s WHERE id=%d', $this->escape( $groupname ), $this->escape( $groupcolor ), $groupid ) );
556
557 return true;
558 }
559
560 return false;
561 }
562
563 /**
564 * Method get_user_notification_email()
565 *
566 * Get user notification email.
567 *
568 * @param mixed $userid Current user ID.
569 *
570 * @return string $user_email User email address.
571 *
572 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
573 */
574 public function get_user_notification_email( $userid = 0 ) {
575 $theUserId = $userid;
576 if ( MainWP_System::instance()->is_single_user() ) {
577 $theUserId = 0;
578 }
579 $user_email = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT user_email FROM ' . $this->table_name( 'users' ) . ' WHERE userid = %d', $theUserId ) );
580
581 if ( null === $user_email || empty( $user_email ) ) {
582 $user_email = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT user_email FROM ' . $this->wpdb->prefix . 'users WHERE id = %d', $userid ) );
583 }
584
585 return $user_email;
586 }
587
588 /**
589 * Method get_user_extension()
590 *
591 * Get user extension.
592 *
593 * @return boolean|int false|get_user_extension_by_user_id()
594 *
595 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
596 */
597 public function get_user_extension() {
598
599 /**
600 * Current user global.
601 *
602 * @global string
603 */
604 global $current_user;
605
606 if ( empty( $current_user ) ) {
607 if ( MainWP_System::instance()->is_single_user() ) {
608 $userid = 0;
609 } else {
610 return false;
611 }
612 } else {
613 $userid = $current_user->ID;
614 }
615
616 return $this->get_user_extension_by_user_id( $userid );
617 }
618
619 /**
620 * Method get_user_extension_by_user_id()
621 *
622 * Get user extension by user id.
623 *
624 * @param mixed $userid Current user ID.
625 *
626 * @return object $row User extension.
627 *
628 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
629 */
630 public function get_user_extension_by_user_id( $userid ) {
631 if ( MainWP_System::instance()->is_single_user() ) {
632 $userid = 0;
633 }
634
635 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
636 if ( null === $row ) {
637 $this->create_user_extension( $userid );
638 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
639 }
640
641 return $row;
642 }
643
644 /**
645 * Method create_user_extension()
646 *
647 * Create user extension
648 *
649 * @param mixed $userId Current user ID.
650 *
651 * @return void
652 */
653 protected function create_user_extension( $userId ) {
654 $fields = array(
655 'userid' => $userId,
656 'user_email' => '',
657 'ignored_plugins' => '',
658 'trusted_plugins' => '',
659 'trusted_plugins_notes' => '',
660 'ignored_themes' => '',
661 'trusted_themes' => '',
662 'trusted_themes_notes' => '',
663 'pluginDir' => '',
664 );
665
666 $this->wpdb->insert( $this->table_name( 'users' ), $fields );
667 }
668
669 /**
670 * Method update_user_extension()
671 *
672 * Update user extension.
673 *
674 * @param mixed $userExtension User extention to update.
675 *
676 * @return object $row User extension.
677 *
678 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
679 */
680 public function update_user_extension( $userExtension ) {
681
682 if ( is_object( $userExtension ) ) {
683 $userid = $userExtension->userid;
684 } elseif ( is_array( $userExtension ) ) {
685 $userid = $userExtension['userid'];
686 } else {
687 $userid = null;
688 }
689
690 if ( null === $userid ) {
691 if ( MainWP_System::instance()->is_single_user() ) {
692 $userid = '0';
693 } else {
694
695 /**
696 * Current user global.
697 *
698 * @global string
699 */
700 global $current_user;
701
702 $userid = $current_user->ID;
703 }
704 }
705 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
706 if ( null === $row ) {
707 $this->create_user_extension( $userid );
708 }
709
710 $fields = array();
711 foreach ( $userExtension as $field => $value ) {
712 if ( $value != $row->$field ) { //phpcs:ignore -- to valid.
713 $fields[ $field ] = $value;
714 }
715 }
716
717 if ( 0 < count( $fields ) ) {
718 $this->wpdb->update( $this->table_name( 'users' ), $fields, array( 'userid' => $userid ) );
719 }
720
721 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
722
723 return $row;
724 }
725
726
727 /**
728 * Method rest_api_update_website().
729 *
730 * Rest API update website.
731 *
732 * @param int $websiteid website ID.
733 * @param array $data Update fields array.
734 * 'http_user'.
735 * 'http_pass'.
736 * 'name'.
737 * 'admin'.
738 * 'sslversion'.
739 * 'uniqueid'.
740 * 'verify'.
741 * 'protocol'.
742 * 'disablechecking'.
743 * 'checkinterval'.
744 * 'disablehealthchecking'.
745 * 'healththreshold'.
746 * 'groupids'.
747 * 'automatic_update'.
748 * 'backup_before_upgrade'.
749 * 'force_use_ipv4'.
750 * 'ignore_core_updates'.
751 * 'ignore_plugin_updates'.
752 * 'ignore_theme_updates'.
753 * 'monitoring_emails'.
754 *
755 * @return bool true|false.
756 */
757 public function rest_api_update_website( $websiteid, $data ) { // phpcs:ignore -- complex function.
758
759 $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
760 if ( empty( $website ) ) {
761 return false;
762 }
763
764 $map_fields = array(
765 'http_user' => 'http_user',
766 'http_pass' => 'http_pass',
767 'name' => 'name',
768 'adminname' => 'admin',
769 'ssl_version' => 'sslversion',
770 'uniqueId' => 'uniqueid',
771 );
772
773 $sql_set = '';
774
775 foreach ( $map_fields as $field => $name ) {
776 if ( isset( $data[ $name ] ) && empty( ! $data[ $name ] ) ) {
777 $sql_set .= ' `' . $this->escape( $field ) . '` = "' . $this->escape( $data[ $name ] ) . '",';
778 }
779 }
780
781 if ( isset( $data['verify'] ) ) {
782 $verify = intval( $data['verify'] );
783 $sql_set .= ' verify_certificate = "' . $this->escape( $verify ) . '",';
784 }
785
786 if ( isset( $data['protocol'] ) && ( 'http' === $data['protocol'] || 'https' === $data['protocol'] ) ) {
787 $url = $data['protocol'] . '://' . MainWP_Utility::remove_http_prefix( $website->url, true );
788 $sql_set .= ' url = "' . $this->escape( $url ) . '",';
789 }
790
791 if ( isset( $data['disablechecking'] ) ) {
792 $sql_set .= ' disable_status_check= "' . ( $data['disablechecking'] ? 1 : 0 ) . '",';
793 }
794
795 if ( isset( $data['checkinterval'] ) ) {
796 $sql_set .= ' status_check_interval= "' . intval( $data['checkinterval'] ) . '",';
797 }
798
799 if ( isset( $data['disablehealthchecking'] ) ) {
800 $sql_set .= ' disable_health_check = "' . ( $data['disablehealthchecking'] ? 1 : 0 ) . '",';
801 }
802
803 if ( isset( $data['healththreshold'] ) ) {
804 $sql_set .= ' health_threshold = "' . intval( $data['healththreshold'] ) . '",';
805 }
806
807 if ( isset( $data['suspended'] ) ) {
808 $sql_set .= ' suspended = "' . ( 1 === intval( $data['suspended'] ) ? 1 : 0 ) . '",';
809 }
810
811 if ( ! empty( $sql_set ) ) {
812 $sql_set = rtrim( $sql_set, ',' );
813 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET ' . $sql_set . ' WHERE id=%d', $websiteid ) );
814 }
815
816 $groupids = array();
817 if ( isset( $data['groupids'] ) && ! empty( $data['groupids'] ) ) {
818 $groupids = explode( ',', sanitize_text_field( wp_unslash( $data['groupids'] ) ) );
819 }
820
821 if ( ! empty( $groupids ) ) {
822 // remove groups.
823 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE wpid=%d', $websiteid ) );
824
825 // update groups.
826 foreach ( $groupids as $groupid ) {
827 $this->wpdb->insert(
828 $this->table_name( 'wp_group' ),
829 array(
830 'wpid' => $websiteid,
831 'groupid' => $groupid,
832 )
833 );
834 }
835 }
836
837 $newValues = array();
838
839 if ( isset( $data['automatic_update'] ) ) {
840 $newValues['automatic_update'] = $data['automatic_update'] ? 1 : 0;
841 }
842
843 if ( isset( $data['backup_before_upgrade'] ) ) {
844 $newValues['backup_before_upgrade'] = $data['backup_before_upgrade'] ? 1 : 0;
845 }
846 if ( isset( $data['force_use_ipv4'] ) ) {
847 $forceuseipv4 = intval( $data['force_use_ipv4'] );
848 if ( 2 < $forceuseipv4 ) {
849 $forceuseipv4 = 0;
850 }
851 $newValues['force_use_ipv4'] = $forceuseipv4;
852 }
853
854 if ( isset( $data['ignore_core_updates'] ) ) {
855 $newValues['is_ignoreCoreUpdates'] = $data['ignore_core_updates'] ? 1 : 0;
856 }
857
858 if ( isset( $data['ignore_plugin_updates'] ) ) {
859 $newValues['is_ignorePluginUpdates'] = $data['ignore_plugin_updates'] ? 1 : 0;
860 }
861
862 if ( isset( $data['ignore_theme_updates'] ) ) {
863 $newValues['is_ignoreThemeUpdates'] = $data['ignore_theme_updates'] ? 1 : 0;
864 }
865
866 if ( ! empty( $newValues ) ) {
867 MainWP_DB::instance()->update_website_values( $website->id, $newValues );
868 }
869
870 if ( isset( $data['monitoring_emails'] ) ) {
871 $monitoring_emails = MainWP_Utility::valid_input_emails( $data['monitoring_emails'] );
872 MainWP_DB::instance()->update_website_option( $website, 'monitoring_notification_emails', $monitoring_emails );
873 }
874
875 return array(
876 'message' => 'Site updated successfully.',
877 'site' => $website->url,
878 );
879 }
880 }
881