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

1,030 lines 33.3 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 mixed $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 * @param int $paged paged.
381 * @param int $order order.
382 * @param array $params params.
383 *
384 * @return string sql query.
385 */
386 public function get_sql_log( $paged = 0, $order = '', $params = array() ) {
387
388 $count_only = ! empty( $params['count'] ) ? true : false;
389 $limit = ! empty( $params['limit'] ) ? intval( $params['limit'] ) : 500;
390
391 $last_hours = ! empty( $params['hour'] ) ? intval( $params['hour'] ) : 0;
392
393 $order = strtoupper( $order );
394
395 $order = 'DESC' === $order || 'ASC' === $order ? $order : 'DESC';
396
397 $start = ! empty( $paged ) ? absint( $paged * $limit ) : 0;
398
399 if ( $count_only ) {
400 return 'SELECT count(*)
401 FROM ' . $this->table_name( 'action_log' ) . ' log
402 WHERE 1 ';
403 }
404
405 if ( ! empty( $last_hours ) ) {
406 return 'SELECT log.*
407 FROM ' . $this->table_name( 'action_log' ) . ' log
408 WHERE ' . $this->wpdb->prepare( ' log_timestamp > %d ', time() - $last_hours * HOUR_IN_SECONDS ) .
409 ' ORDER BY log_timestamp ' . $this->escape( $order );
410 }
411
412 return 'SELECT log.*
413 FROM ' . $this->table_name( 'action_log' ) . ' log
414 WHERE 1 ORDER BY ' .
415 $this->wpdb->prepare( 'log_timestamp ' . $this->escape( $order ) . ' LIMIT %d, %d', $start, $limit );
416 }
417
418 /**
419 * Method insert_action_log()
420 *
421 * Insert action log.
422 *
423 * @param array $data log data.
424 *
425 * @return void
426 */
427 public function insert_action_log( $data ) {
428 $this->wpdb->insert( $this->table_name( 'action_log' ), $data );
429 }
430
431 /**
432 * Method delete_action_log()
433 *
434 * Delete action log.
435 *
436 * @param int $days number days.
437 *
438 * @return void
439 */
440 public function delete_action_log( $days = false ) {
441 $where = '';
442 if ( ! empty( $days ) ) {
443 $where .= ' AND log_timestamp < ' . ( time() - $days * DAY_IN_SECONDS );
444 }
445 $this->wpdb->query( 'DELETE FROM `' . $this->table_name( 'action_log' ) . '` WHERE 1 ' . $where );
446 }
447
448 /**
449 * Method insert_or_update_request_log()
450 *
451 * Insert or update request log.
452 *
453 * @param mixed $wpid WordPress ID.
454 * @param mixed $ip IP address.
455 * @param mixed $start Start time.
456 * @param mixed $stop Stop Time.
457 *
458 * @return void
459 */
460 public function insert_or_update_request_log( $wpid, $ip, $start, $stop ) {
461 $updateValues = array();
462 if ( ! empty( $ip ) ) {
463 $updateValues['ip'] = $ip;
464 }
465 if ( ! empty( $start ) ) {
466 $updateValues['micro_timestamp_start'] = $start;
467 }
468 if ( ! empty( $stop ) ) {
469 $updateValues['micro_timestamp_stop'] = $stop;
470 }
471
472 $var = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT id FROM ' . $this->table_name( 'request_log' ) . ' WHERE wpid = %d ', $wpid ) );
473 if ( null !== $var ) {
474 $this->wpdb->update( $this->table_name( 'request_log' ), $updateValues, array( 'wpid' => $wpid ) );
475 } else {
476 $updateValues['wpid'] = $wpid;
477 $this->wpdb->insert( $this->table_name( 'request_log' ), $updateValues );
478 }
479 }
480
481 /**
482 * Method close_open_requests()
483 *
484 * Close open request.
485 *
486 * @return void
487 */
488 public function close_open_requests() {
489 // Close requests open longer then 7 seconds.. something is wrong here.
490 $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' );
491 }
492
493 /**
494 * Method get_nrof_open_requests()
495 *
496 * Get number of requests.
497 *
498 * @param null $ip IP Address.
499 *
500 * @return (string|null) Database query result for number of requests or null on failure.
501 */
502 public function get_nrof_open_requests( $ip = null ) {
503 if ( null === $ip ) {
504 return $this->wpdb->get_var( 'select count(id) from ' . $this->table_name( 'request_log' ) . ' where micro_timestamp_stop < micro_timestamp_start' );
505 }
506
507 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 ) . '"' );
508 }
509
510 /**
511 * Method get_last_request_timestamp()
512 *
513 * Get timestamp of last request sent.
514 *
515 * @param null $ip Child Site IP address, default: null.
516 *
517 * @return (int|null) Database query result for timestamp of last request sent or null on failure.
518 */
519 public function get_last_request_timestamp( $ip = null ) {
520 if ( null === $ip ) {
521 return $this->wpdb->get_var( 'select micro_timestamp_start from ' . $this->table_name( 'request_log' ) . ' order by micro_timestamp_start desc limit 1' );
522 }
523
524 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 ) ) );
525 }
526
527 /**
528 * Method update_group_site()
529 *
530 * @param mixed $groupId Group ID.
531 * @param mixed $websiteId Child Site ID.
532 *
533 * @return void
534 */
535 public function update_group_site( $groupId, $websiteId ) {
536 $this->wpdb->insert(
537 $this->table_name( 'wp_group' ),
538 array(
539 'wpid' => $websiteId,
540 'groupid' => $groupId,
541 )
542 );
543 }
544
545 /**
546 * Method clear_group()
547 *
548 * Clear sites in group.
549 *
550 * @param mixed $groupId ID of group.
551 */
552 public function clear_group( $groupId ) {
553 $this->wpdb->query( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE groupid=' . $groupId );
554 }
555
556
557 /**
558 * Method add_group()
559 *
560 * Add group.
561 *
562 * @param mixed $userid Current User ID.
563 * @param mixed $name Name of group to add.
564 * @param mixed $color Color of group to add.
565 *
566 * @return boolean true
567 *
568 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
569 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
570 */
571 public function add_group( $userid, $name, $color = '' ) {
572 if ( MainWP_Utility::ctype_digit( $userid ) && $this->wpdb->insert(
573 $this->table_name( 'group' ),
574 array(
575 'userid' => $userid,
576 'name' => $this->escape( $name ),
577 'color' => $this->escape( $color ),
578 )
579 ) ) {
580
581 $groupId = $this->wpdb->insert_id;
582
583 $group = $this->get_group_by_id( $groupId );
584
585 /**
586 * Fires after a new sites tag has been created.
587 *
588 * @param object $group group created.
589 * @param string group action.
590 */
591 do_action( 'mainwp_site_tag_action', $group, 'created' );
592
593 return $groupId;
594 }
595
596 return false;
597 }
598
599 /**
600 * Method add_tag()
601 *
602 * Add Group.
603 *
604 * @param array $params params data.
605 */
606 public function add_tag( $params = array() ) {
607 /**
608 * Current user global.
609 *
610 * @global string
611 */
612 global $current_user;
613 //phpcs:disable WordPress.Security.NonceVerification.Missing
614 $groupId = isset( $params['id'] ) ? intval( $params['id'] ) : 0;
615 $newName = isset( $params['name'] ) ? sanitize_text_field( wp_unslash( $params['name'] ) ) : '';
616 $newColor = null;
617
618 if ( isset( $params['color'] ) ) {
619 $newColor = sanitize_hex_color( wp_unslash( $params['color'] ) );
620 }
621 //phpcs:enable WordPress.Security.NonceVerification.Missing
622
623 if ( ! empty( $groupId ) ) {
624 $color_update = '';
625 if ( null !== $newColor ) {
626 $color_update = ", color='" . $this->escape( $newColor ) . "' ";
627 }
628 // update groupname.
629 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'group' ) . ' SET name=%s ' . $color_update . ' WHERE id=%d', $this->escape( $newName ), $groupId ) );
630 return $this->get_group_by_id( $groupId );
631 } elseif ( ! empty( $newName ) ) {
632 $groupId = $this->add_group( $current_user->ID, MainWP_Manage_Groups::check_group_name( $newName ), $newColor );
633
634 /**
635 * New Group Added
636 *
637 * Fires after a new sites group has been created.
638 *
639 * @param int $groupId Group ID.
640 */
641 do_action( 'mainwp_added_new_group', $groupId );
642 return $this->get_group_by_id( $groupId );
643 }
644 return false;
645 }
646
647 /**
648 * Method remove_group()
649 *
650 * Remove group.
651 *
652 * @param mixed $groupid Group ID.
653 *
654 * @return int|boolean Group that was deleted or false on failure.
655 *
656 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
657 */
658 public function remove_group( $groupid ) {
659 if ( MainWP_Utility::ctype_digit( $groupid ) ) {
660 $group = $this->get_group_by_id( $groupid );
661 $nr = $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'group' ) . ' WHERE id=%d', $groupid ) );
662 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE groupid=%d', $groupid ) );
663 if ( $nr ) {
664 /**
665 * Fires after a tag has been deleted.
666 *
667 * @param object $group group created.
668 * @param string group action.
669 */
670 do_action( 'mainwp_site_tag_action', $group, 'deleted' );
671 }
672 return $nr;
673 }
674
675 return false;
676 }
677
678 /**
679 * Method update_note()
680 *
681 * Update Note.
682 *
683 * @param mixed $websiteid Child Site ID.
684 * @param mixed $note Note data.
685 *
686 * @return void
687 */
688 public function update_note( $websiteid, $note ) {
689 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET note= %s WHERE id=%d', $this->escape( $note ), $websiteid ) );
690 }
691
692 /**
693 * Method update_group()
694 *
695 * Update group.
696 *
697 * @param mixed $groupid Group ID.
698 * @param mixed $groupname Group Name.
699 * @param string $groupcolor Group Color.
700 *
701 * @return boolean true|false.
702 *
703 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
704 */
705 public function update_group( $groupid, $groupname, $groupcolor ) {
706 if ( MainWP_Utility::ctype_digit( $groupid ) ) {
707 // update groupname.
708 $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 ) );
709
710 return true;
711 }
712
713 return false;
714 }
715
716 /**
717 * Method get_user_notification_email()
718 *
719 * Get user notification email.
720 *
721 * @param mixed $userid Current user ID.
722 *
723 * @return string $user_email User email address.
724 *
725 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
726 */
727 public function get_user_notification_email( $userid = 0 ) {
728 $theUserId = $userid;
729 if ( MainWP_System::instance()->is_single_user() ) {
730 $theUserId = 0;
731 }
732 $user_email = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT user_email FROM ' . $this->table_name( 'users' ) . ' WHERE userid = %d', $theUserId ) );
733
734 if ( null === $user_email || empty( $user_email ) ) {
735 $user_email = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT user_email FROM ' . $this->wpdb->prefix . 'users WHERE id = %d', $userid ) );
736 }
737
738 return $user_email;
739 }
740
741 /**
742 * Method get_user_extension()
743 *
744 * Get user extension.
745 *
746 * @return boolean|int false|get_user_extension_by_user_id()
747 *
748 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
749 */
750 public function get_user_extension() {
751
752 /**
753 * Current user global.
754 *
755 * @global string
756 */
757 global $current_user;
758
759 if ( empty( $current_user ) ) {
760 if ( MainWP_System::instance()->is_single_user() ) {
761 $userid = 0;
762 } else {
763 return false;
764 }
765 } else {
766 $userid = $current_user->ID;
767 }
768
769 return $this->get_user_extension_by_user_id( $userid );
770 }
771
772 /**
773 * Method get_user_extension_by_user_id()
774 *
775 * Get user extension by user id.
776 *
777 * @param mixed $userid Current user ID.
778 *
779 * @return object $row User extension.
780 *
781 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
782 */
783 public function get_user_extension_by_user_id( $userid = 0 ) {
784 if ( MainWP_System::instance()->is_single_user() ) {
785 $userid = 0;
786 }
787
788 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
789 if ( null === $row ) {
790 $this->create_user_extension( $userid );
791 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
792 }
793
794 return $row;
795 }
796
797 /**
798 * Method create_user_extension()
799 *
800 * Create user extension
801 *
802 * @param mixed $userId Current user ID.
803 *
804 * @return void
805 */
806 protected function create_user_extension( $userId ) {
807 $fields = array(
808 'userid' => $userId,
809 'user_email' => '',
810 'ignored_plugins' => '',
811 'trusted_plugins' => '',
812 'trusted_plugins_notes' => '',
813 'ignored_themes' => '',
814 'trusted_themes' => '',
815 'trusted_themes_notes' => '',
816 'pluginDir' => '',
817 'ignored_wp_upgrades' => '',
818 );
819
820 $this->wpdb->insert( $this->table_name( 'users' ), $fields );
821 }
822
823 /**
824 * Method update_user_extension()
825 *
826 * Update user extension.
827 *
828 * @param mixed $userExtension User extention to update.
829 *
830 * @return object $row User extension.
831 *
832 * @uses \MainWP\Dashboard\MainWP_System::is_single_user()
833 */
834 public function update_user_extension( $userExtension ) {
835
836 if ( is_object( $userExtension ) ) {
837 $userid = $userExtension->userid;
838 } elseif ( is_array( $userExtension ) ) {
839 $userid = $userExtension['userid'];
840 } else {
841 $userid = null;
842 }
843
844 if ( null === $userid ) {
845 if ( MainWP_System::instance()->is_single_user() ) {
846 $userid = '0';
847 } else {
848
849 /**
850 * Current user global.
851 *
852 * @global string
853 */
854 global $current_user;
855
856 $userid = $current_user->ID;
857 }
858 }
859 $row = $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
860 if ( null === $row ) {
861 $this->create_user_extension( $userid );
862 }
863
864 $fields = array();
865 foreach ( $userExtension as $field => $value ) {
866 if ( $value != $row->$field ) { //phpcs:ignore -- to valid.
867 $fields[ $field ] = $value;
868 }
869 }
870
871 if ( ! empty( $fields ) ) {
872 $this->wpdb->update( $this->table_name( 'users' ), $fields, array( 'userid' => $userid ) );
873 }
874
875 return $this->wpdb->get_row( 'SELECT * FROM ' . $this->table_name( 'users' ) . ' WHERE userid= ' . $userid, OBJECT );
876 }
877
878
879 /**
880 * Method rest_api_update_website().
881 *
882 * Rest API update website.
883 *
884 * @param int $websiteid website ID.
885 * @param array $data Update fields array.
886 * 'http_user'.
887 * 'http_pass'.
888 * 'name'.
889 * 'admin'.
890 * 'sslversion'.
891 * 'uniqueid'.
892 * 'verify'.
893 * 'protocol'.
894 * 'checkinterval'.
895 * 'disablehealthchecking'.
896 * 'healththreshold'.
897 * 'groupids'.
898 * 'automatic_update'.
899 * 'backup_before_upgrade'.
900 * 'force_use_ipv4'.
901 * 'ignore_core_updates'.
902 * 'ignore_plugin_updates'.
903 * 'ignore_theme_updates'.
904 * 'monitoring_emails'.
905 *
906 * @return mixed array|true|false.
907 */
908 public function rest_api_update_website( $websiteid, $data ) { // phpcs:ignore -- NOSONAR - complex function.
909
910 $website = MainWP_DB::instance()->get_website_by_id( $websiteid );
911 if ( empty( $website ) ) {
912 return false;
913 }
914 $success = false;
915
916 $map_fields = array(
917 'http_user' => 'http_user',
918 'http_pass' => 'http_pass',
919 'name' => 'name',
920 'adminname' => 'admin',
921 'ssl_version' => 'sslversion',
922 'uniqueId' => 'uniqueid',
923 );
924
925 $sql_set = '';
926
927 foreach ( $map_fields as $field => $name ) {
928 if ( isset( $data[ $name ] ) && empty( ! $data[ $name ] ) ) {
929 $sql_set .= ' `' . $this->escape( $field ) . '` = "' . $this->escape( $data[ $name ] ) . '",';
930 }
931 }
932
933 if ( isset( $data['verify'] ) ) {
934 $verify = intval( $data['verify'] );
935 $sql_set .= ' verify_certificate = "' . $this->escape( $verify ) . '",';
936 }
937
938 if ( isset( $data['protocol'] ) && ( 'http' === $data['protocol'] || 'https' === $data['protocol'] ) ) {
939 $url = $data['protocol'] . '://' . MainWP_Utility::remove_http_prefix( $website->url, true );
940 $sql_set .= ' url = "' . $this->escape( $url ) . '",';
941 }
942
943 if ( isset( $data['disablehealthchecking'] ) ) {
944 $sql_set .= ' disable_health_check = "' . ( $data['disablehealthchecking'] ? 1 : 0 ) . '",';
945 }
946
947 if ( isset( $data['healththreshold'] ) ) {
948 $sql_set .= ' health_threshold = "' . intval( $data['healththreshold'] ) . '",';
949 }
950
951 if ( isset( $data['suspended'] ) ) {
952 $sql_set .= ' suspended = "' . ( 1 === intval( $data['suspended'] ) ? 1 : 0 ) . '",';
953 }
954
955 if ( ! empty( $sql_set ) ) {
956 $sql_set = rtrim( $sql_set, ',' );
957 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET ' . $sql_set . ' WHERE id=%d', $websiteid ) );
958 $success = true;
959 }
960
961 $groupids = array();
962 if ( isset( $data['groupids'] ) && ! empty( $data['groupids'] ) ) {
963 $groupids = explode( ',', sanitize_text_field( wp_unslash( $data['groupids'] ) ) );
964 }
965
966 if ( ! empty( $groupids ) ) {
967 // remove groups.
968 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE wpid=%d', $websiteid ) );
969
970 // update groups.
971 foreach ( $groupids as $groupid ) {
972 $this->wpdb->insert(
973 $this->table_name( 'wp_group' ),
974 array(
975 'wpid' => $websiteid,
976 'groupid' => $groupid,
977 )
978 );
979 }
980 $success = true;
981 }
982
983 $newValues = array();
984
985 if ( isset( $data['automatic_update'] ) ) {
986 $newValues['automatic_update'] = $data['automatic_update'] ? 1 : 0;
987 }
988
989 if ( isset( $data['backup_before_upgrade'] ) ) {
990 $newValues['backup_before_upgrade'] = $data['backup_before_upgrade'] ? 1 : 0;
991 }
992 if ( isset( $data['force_use_ipv4'] ) ) {
993 $forceuseipv4 = intval( $data['force_use_ipv4'] );
994 if ( 2 < $forceuseipv4 ) {
995 $forceuseipv4 = 0;
996 }
997 $newValues['force_use_ipv4'] = $forceuseipv4;
998 }
999
1000 if ( isset( $data['ignore_core_updates'] ) ) {
1001 $newValues['is_ignoreCoreUpdates'] = $data['ignore_core_updates'] ? 1 : 0;
1002 }
1003
1004 if ( isset( $data['ignore_plugin_updates'] ) ) {
1005 $newValues['is_ignorePluginUpdates'] = $data['ignore_plugin_updates'] ? 1 : 0;
1006 }
1007
1008 if ( isset( $data['ignore_theme_updates'] ) ) {
1009 $newValues['is_ignoreThemeUpdates'] = $data['ignore_theme_updates'] ? 1 : 0;
1010 }
1011
1012 if ( ! empty( $newValues ) ) {
1013 MainWP_DB::instance()->update_website_values( $website->id, $newValues );
1014 $success = true;
1015 }
1016
1017 if ( isset( $data['monitoring_emails'] ) ) {
1018 $monitoring_emails = MainWP_Utility::valid_input_emails( $data['monitoring_emails'] );
1019 MainWP_DB::instance()->update_website_option( $website, 'monitoring_notification_emails', ( $monitoring_emails ) );
1020
1021 }
1022
1023 return array(
1024 'message' => 'Site updated successfully.',
1025 'site' => $website->url,
1026 'success' => $success,
1027 );
1028 }
1029 }
1030