PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.2.2
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.2.2, at class/class-mainwp-db-common.php

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