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

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

2,453 lines 81.1 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
14 *
15 * @package MainWP\Dashboard
16 *
17 * @uses \MainWP\Dashboard\MainWP_DB_Base
18 */
19 class MainWP_DB extends MainWP_DB_Base {
20
21 // 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.
22
23 /**
24 * Private static variable to hold the single instance of the class.
25 *
26 * @static
27 *
28 * @var mixed Default null
29 */
30 private static $instance = null;
31
32 /**
33 * Private static variable to hold the single instance.
34 *
35 * @static
36 *
37 * @var mixed Default null
38 */
39 private static $general_options = null;
40
41 /**
42 * Possible options.
43 *
44 * @var array $possible_options
45 */
46 private static $possible_options = array(
47 'plugin_upgrades',
48 'theme_upgrades',
49 'premium_upgrades',
50 'plugins',
51 'themes',
52 'dtsSync',
53 'version',
54 'sync_errors',
55 'ignored_plugins',
56 'wp_upgrades',
57 'site_info',
58 'client',
59 'signature_algo',
60 'verify_method',
61 'pubkey',
62 );
63
64 /**
65 * Create public static instance.
66 *
67 * @static
68 *
69 * @return MainWP_DB
70 */
71 public static function instance() {
72 if ( null === self::$instance ) {
73 self::$instance = new self();
74 }
75
76 self::$instance->test_connection();
77
78 return self::$instance;
79 }
80
81 /**
82 * Get wp_options database table view.
83 *
84 * @param array $fields Extra option fields.
85 * @param bool $default_value Whether or not to get default option fields.
86 *
87 * @return array wp_options view.
88 */
89 public function get_option_view( $fields = array(), $default_value = true ) {
90
91 if ( ! is_array( $fields ) ) {
92 $fields = array();
93 }
94
95 $view = '(SELECT intwp.id AS wpid ';
96
97 if ( empty( $fields ) || $default_value ) {
98 $view .= ',(SELECT recent_comments.value FROM ' . $this->table_name( 'wp_options' ) . ' recent_comments WHERE recent_comments.wpid = intwp.id AND recent_comments.name = "recent_comments" LIMIT 1) AS recent_comments,
99 (SELECT recent_posts.value FROM ' . $this->table_name( 'wp_options' ) . ' recent_posts WHERE recent_posts.wpid = intwp.id AND recent_posts.name = "recent_posts" LIMIT 1) AS recent_posts,
100 (SELECT recent_pages.value FROM ' . $this->table_name( 'wp_options' ) . ' recent_pages WHERE recent_pages.wpid = intwp.id AND recent_pages.name = "recent_pages" LIMIT 1) AS recent_pages,
101 (SELECT phpversion.value FROM ' . $this->table_name( 'wp_options' ) . ' phpversion WHERE phpversion.wpid = intwp.id AND phpversion.name = "phpversion" LIMIT 1) AS phpversion,
102 (SELECT added_timestamp.value FROM ' . $this->table_name( 'wp_options' ) . ' added_timestamp WHERE added_timestamp.wpid = intwp.id AND added_timestamp.name = "added_timestamp" LIMIT 1) AS added_timestamp,
103 (SELECT wp_upgrades.value FROM ' . $this->table_name( 'wp_options' ) . ' wp_upgrades WHERE wp_upgrades.wpid = intwp.id AND wp_upgrades.name = "wp_upgrades" LIMIT 1) AS wp_upgrades ';
104 }
105
106 if ( ! in_array( 'signature_algo', $fields ) ) {
107 $fields[] = 'signature_algo';
108 }
109
110 if ( ! in_array( 'verify_method', $fields ) ) {
111 $fields[] = 'verify_method';
112 }
113
114 if ( is_array( $fields ) ) {
115 foreach ( $fields as $field ) {
116 if ( empty( $field ) ) {
117 continue;
118 }
119 $view .= ', ';
120 $view .= '(SELECT ' . $this->escape( $field ) . '.value FROM ' . $this->table_name( 'wp_options' ) . ' ' . $this->escape( $field ) . ' WHERE ' . $this->escape( $field ) . '.wpid = intwp.id AND ' . $this->escape( $field ) . '.name = "' . $this->escape( $field ) . '" LIMIT 1) AS ' . $this->escape( $field );
121 }
122 }
123
124 $view .= ' FROM ' . $this->table_name( 'wp' ) . ' intwp)';
125
126 return $view;
127 }
128
129 /**
130 * Get connected child sites.
131 *
132 * @param array $sites_ids Websites ids - option field.
133 *
134 * @return array $connected_sites Array of connected sites.
135 */
136 public function get_connected_websites( $sites_ids = false ) {
137 $where = $this->get_sql_where_allow_access_sites( 'wp' );
138
139 $sql = 'SELECT wp.*,wp_sync.*
140 FROM ' . $this->table_name( 'wp' ) . ' wp
141 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync
142 ON wp.id = wp_sync.wpid
143 WHERE (wp_sync.sync_errors IS NOT NULL) AND (wp_sync.sync_errors = "") ' .
144 $where;
145
146 $websites = $this->wpdb->get_results( $sql );
147 $connected_sites = array();
148 if ( $websites ) {
149 foreach ( $websites as $website ) {
150
151 if ( ! empty( $sites_ids ) ) {
152 // filter sites.
153 if ( ! in_array( $website->id, $sites_ids ) ) {
154 continue;
155 }
156 }
157
158 $connected_sites[] = array(
159 'id' => $website->id,
160 'name' => $website->name,
161 'url' => $website->url,
162 );
163 }
164 }
165 return $connected_sites;
166 }
167
168 /**
169 * Get disconnected child sites.
170 *
171 * @param array $sites_ids Websites ids - option field.
172 *
173 * @return array $disc_sites Array of disonnected sites.
174 */
175 public function get_disconnected_websites( $sites_ids = false ) {
176 $where = $this->get_sql_where_allow_access_sites( 'wp' );
177
178 $sql = 'SELECT wp.*,wp_sync.*
179 FROM ' . $this->table_name( 'wp' ) . ' wp
180 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync
181 ON wp.id = wp_sync.wpid
182 WHERE (wp_sync.sync_errors IS NOT NULL) AND (wp_sync.sync_errors <> "") ' .
183 $where;
184
185 $websites = $this->wpdb->get_results( $sql );
186 $disc_sites = array();
187 if ( $websites ) {
188 foreach ( $websites as $website ) {
189
190 if ( ! empty( $sites_ids ) ) {
191 // filter sites.
192 if ( ! in_array( $website->id, $sites_ids ) ) {
193 continue;
194 }
195 }
196
197 $disc_sites[] = array(
198 'id' => $website->id,
199 'name' => $website->name,
200 'url' => $website->url,
201 );
202 }
203 }
204 return $disc_sites;
205 }
206
207 /**
208 * Get child site count.
209 *
210 * @param null $userId Current user ID.
211 * @param bool $all_access Check if user has access to all sites.
212 *
213 * @return int Child site count.
214 *
215 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
216 */
217 public function get_websites_count( $userId = null, $all_access = false ) {
218 if ( ( null === $userId ) && MainWP_System::instance()->is_multi_user() ) {
219
220 /**
221 * Current user global.
222 *
223 * @global string
224 */
225 global $current_user;
226
227 $userId = $current_user->ID;
228 }
229 $where = ( null === $userId ? '' : ' wp.userid = ' . $userId );
230 if ( ! $all_access ) {
231 $where .= $this->get_sql_where_allow_access_sites( 'wp' );
232 }
233 $qry = 'SELECT COUNT(wp.id) FROM ' . $this->table_name( 'wp' ) . ' wp WHERE 1 ' . $where;
234
235 return $this->wpdb->get_var( $qry );
236 }
237
238
239 /**
240 * Get child sites stats count.
241 *
242 * @param array $params Params.
243 */
244 public function get_websites_stats_count( $params = array() ) {
245 if ( ! is_array( $params ) ) {
246 $params = array();
247 }
248
249 if ( isset( $params['all_access'] ) ) {
250 $all_access = ! empty( $params['all_access'] ) ? true : false;
251 } else {
252 $all_access = true;
253 }
254
255 $where = '';
256 if ( ! $all_access ) {
257 $where .= $this->get_sql_where_allow_access_sites( 'wp' );
258 }
259
260 $select_stats = ' ( SELECT COUNT(wp.id) as count_all ';
261 if ( ! empty( $params['count_disconnected'] ) ) {
262 $select_stats .= ',( SELECT COUNT(wp_disconnected.id) FROM ' . $this->table_name( 'wp' ) . ' wp_disconnected LEFT JOIN ' . $this->table_name( 'wp_sync' ) . ' as wp_sync ';
263 $select_stats .= ' ON wp_disconnected.id = wp_sync.wpid WHERE wp_sync.sync_errors != "" ) as count_disconnected ';
264 }
265 if ( ! empty( $params['count_suspended'] ) ) {
266 $select_stats .= ',( SELECT COUNT(wp_suspended.id) FROM ' . $this->table_name( 'wp' ) . ' wp_suspended WHERE wp_suspended.suspended = 1 ) as count_suspended ';
267 }
268 $qry = 'SELECT * FROM ' . $select_stats;
269 $qry .= ' FROM ' . $this->table_name( 'wp' ) . ' wp ' . $where . ' ) as wp_stats ';
270
271 return $this->wpdb->get_row( $qry, ARRAY_A ); //phpcs:ignore -- ok.
272 }
273
274 /**
275 * Get Child site wp_options database table.
276 *
277 * @param array $website Child Site array.
278 * @param mixed $option Child Site wp_options table name.
279 * @param mixed $default_value default value.
280 *
281 * @return string|null Database query result (as string), or null on failure.
282 */
283 public function get_website_option( $website, $option, $default_value = null ) {
284
285 if ( is_array( $website ) ) {
286 if ( isset( $website[ $option ] ) ) {
287 return $website[ $option ];
288 }
289 $site_id = $website['id'];
290 } elseif ( is_object( $website ) ) {
291 if ( property_exists( $website, $option ) ) {
292 return $website->{$option};
293 }
294 $site_id = $website->id;
295 } elseif ( is_numeric( $website ) ) { // to support $site_id = 0, for global options.
296 $site_id = $website;
297 } else {
298 return false;
299 }
300
301 $var = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT value FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid = %d AND name = "' . $this->escape( $option ) . '"', $site_id ) );
302
303 if ( null === $var && null !== $default_value ) {
304 $var = $default_value;
305 }
306 return $var;
307 }
308
309 /**
310 * Get child site options.
311 *
312 * @param array $website Child site.
313 * @param mixed $options Child site options name.
314 *
315 * @return string|null Database query result (as string), or null on failure.
316 */
317 public function get_website_options_array( &$website, $options ) {
318
319 if ( ! is_array( $options ) || empty( $options ) ) {
320 return array();
321 }
322
323 if ( is_array( $website ) ) {
324 $site_id = $website['id'];
325 } elseif ( is_object( $website ) ) {
326 $site_id = $website->id;
327 } elseif ( is_numeric( $website ) ) { // to support $site_id = 0 for global options.
328 $site_id = $website;
329 } else {
330 return array();
331 }
332
333 $arr_options = array();
334 $get_options = array();
335
336 foreach ( $options as $option ) {
337 if ( is_array( $website ) ) {
338 if ( isset( $website[ $option ] ) ) {
339 $arr_options[ $option ] = $website[ $option ];
340 } else {
341 $get_options[] = $option;
342 }
343 } elseif ( is_object( $website ) ) {
344 if ( property_exists( $website, $option ) ) {
345 $arr_options[ $option ] = $website->{$option};
346 } else {
347 $get_options[] = $option;
348 }
349 } else {
350 $get_options[] = $option;
351 }
352 }
353
354 if ( empty( $get_options ) ) {
355 return $arr_options; // all options.
356 }
357
358 $options_name = implode( "','", $get_options );
359 $options_name = "'" . $options_name . "'";
360
361 $options_db = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT name, value FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid = %d AND name IN (' . $options_name . ')', $site_id ) );
362
363 $fill_options = array(
364 'primary_lasttime_backup',
365 );
366
367 foreach ( (array) $options_db as $o ) {
368 $arr_options[ $o->name ] = $o->value;
369 if ( in_array( $o->name, $fill_options ) ) {
370 if ( is_array( $website ) ) {
371 if ( ! isset( $website[ $o->name ] ) ) {
372 $website[ $o->name ] = $o->value;
373 }
374 } elseif ( is_object( $website ) ) {
375 if ( ! property_exists( $website, $o->name ) ) {
376 $website->{$o->name} = $o->value;
377 }
378 }
379 }
380 }
381 return $arr_options;
382 }
383
384 /**
385 * Update child site options.
386 *
387 * @param object $website Child site object.
388 * @param mixed $option Option to update.
389 * @param mixed $value Value to update with.
390 */
391 public function update_website_option( $website, $option, $value ) {
392 $rslt = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT name FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid = %d AND name = "' . $this->escape( $option ) . '"', $website->id ) );
393 if ( 0 === count( $rslt ) ) {
394 $this->wpdb->insert(
395 $this->table_name( 'wp_options' ),
396 array(
397 'wpid' => $website->id,
398 'name' => $option,
399 'value' => $value,
400 )
401 );
402 } else {
403 $this->wpdb->update(
404 $this->table_name( 'wp_options' ),
405 array( 'value' => $value ),
406 array(
407 'wpid' => $website->id,
408 'name' => $option,
409 )
410 );
411 }
412 }
413
414
415 /**
416 * Get general Child site option.
417 *
418 * @param mixed $option Child Site option name.
419 *
420 * @return string|null Database query result (as string), or null on failure.
421 */
422 private function get_general_website_option( $option ) {
423
424 if ( null !== self::$general_options ) {
425 if ( isset( self::$general_options[ $option ] ) ) {
426 return self::$general_options[ $option ];
427 }
428 } else {
429 self::$general_options[] = array();
430 }
431
432 $val = $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT value FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid = %d AND name = "' . $this->escape( $option ) . '"', 0 ) );
433
434 self::$general_options[ $option ] = $val;
435 return $val;
436 }
437
438 /**
439 * Get child site options.
440 *
441 * @param mixed $options Child site options name.
442 *
443 * @return string|null Database query result (as string), or null on failure.
444 */
445 public function get_general_options_array( $options ) {
446
447 if ( ! is_array( $options ) || empty( $options ) ) {
448 return array();
449 }
450
451 $return_options = array();
452 if ( null !== self::$general_options ) {
453 foreach ( self::$general_options as $opt => $val ) {
454 if ( in_array( $opt, $options ) ) {
455 $return_options[ $opt ] = $val;
456 }
457 }
458 } else {
459 self::$general_options[] = array();
460 }
461
462 $diff_options = array();
463 foreach ( $options as $opt ) {
464 if ( ! isset( $return_options[ $opt ] ) ) {
465 $diff_options[] = $opt;
466 }
467 }
468
469 $options_name = implode( "','", $diff_options );
470 $options_name = "'" . $options_name . "'";
471
472 $options_db = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT name, value FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid = %d AND name IN (' . $options_name . ')', 0 ) );
473
474 foreach ( (array) $options_db as $o ) {
475 $return_options[ $o->name ] = $o->value;
476 self::$general_options[ $o->name ] = $o->value;
477 }
478 return $return_options;
479 }
480
481 /**
482 * Update general site options.
483 *
484 * @param mixed $option Option to update.
485 * @param mixed $value Value to update with.
486 * @param string $type_value Type values: single|array.
487 */
488 public function update_general_option( $option, $value, $type_value = 'single' ) {
489
490 if ( 'array' === $type_value ) {
491 if ( empty( $value ) ) {
492 $value = array();
493 } elseif ( ! is_array( $value ) ) {
494 return false;
495 }
496 $value = wp_json_encode( $value );
497 }
498
499 if ( null === self::$general_options ) {
500 self::$general_options[] = array();
501 }
502 self::$general_options[ $option ] = $value;
503
504 $rslt = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT name FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid = %d AND name = "' . $this->escape( $option ) . '"', 0 ) );
505
506 if ( 0 === count( $rslt ) ) {
507 $this->wpdb->insert(
508 $this->table_name( 'wp_options' ),
509 array(
510 'wpid' => 0,
511 'name' => $option,
512 'value' => $value,
513 )
514 );
515 } else {
516 $this->wpdb->update(
517 $this->table_name( 'wp_options' ),
518 array( 'value' => $value ),
519 array(
520 'wpid' => 0,
521 'name' => $option,
522 )
523 );
524 }
525 return true;
526 }
527
528 /**
529 * Get general Child site option.
530 *
531 * @param mixed $opt Child Site option name.
532 * @param string $type_value Type values: single|array.
533 *
534 * @return string|null Database query result (as string), or null on failure.
535 */
536 public function get_general_option( $opt, $type_value = 'single' ) {
537 if ( 'single' === $type_value ) {
538 return $this->get_general_website_option( $opt );
539 } elseif ( 'array' === $type_value ) {
540 $json_value = $this->get_general_website_option( $opt );
541 if ( empty( $json_value ) ) {
542 return array();
543 }
544 return json_decode( $json_value, true );
545 }
546 return false;
547 }
548
549 /**
550 * Get child sites by user ID.
551 *
552 * @param int $userid User ID.
553 * @param bool $selectgroups Selected groups.
554 * @param null $search_site Site search field value.
555 * @param string $orderBy Order list by. Default: URL.
556 *
557 * @return array|object|null Database query results or null on failer.
558 */
559 public function get_websites_by_user_id( $userid, $selectgroups = false, $search_site = null, $orderBy = 'wp.url' ) {
560 return $this->get_results_result( $this->get_sql_websites_by_user_id( $userid, $selectgroups, $search_site, $orderBy ) );
561 }
562
563 /**
564 * Get child sites.
565 *
566 * @return string SQL string.
567 */
568 public function get_sql_websites() {
569 $where = $this->get_sql_where_allow_access_sites( 'wp' );
570
571 return 'SELECT wp.*,wp_sync.*,wp_optionview.*
572 FROM ' . $this->table_name( 'wp' ) . ' wp
573 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
574 JOIN ' . $this->get_option_view() . ' wp_optionview ON wp.id = wp_optionview.wpid
575 WHERE 1 ' . $where;
576 }
577
578 /**
579 * Get child sites to run the status check process.
580 *
581 * @param int $last_check Time of the last check.
582 * @param int $count Number of websites.
583 *
584 * @return string SQL string.
585 */
586 public function get_sql_websites_to_check_status( $last_check, $count = 20 ) {
587 $where = $this->get_sql_where_allow_access_sites( 'wp' );
588 $sql = 'SELECT wp.*
589 FROM ' . $this->table_name( 'wp' ) . ' wp
590 WHERE wp.disable_status_check <> 1 AND ( wp.status_check_interval = 0 AND wp.offline_checks_last < ' . intval( $last_check ) . ' )' .
591 $where . '
592 LIMIT ' . intval( $count );
593 return $sql;
594 }
595
596
597 /**
598 * Get child sites to run the status individual check process.
599 *
600 * @param int $count Number of websites.
601 *
602 * @return string SQL string.
603 */
604 public function get_sql_websites_to_check_individual_status( $count = 20 ) {
605 $where = $this->get_sql_where_allow_access_sites( 'wp' );
606 $sql = 'SELECT wp.*
607 FROM ' . $this->table_name( 'wp' ) . ' wp
608 WHERE wp.disable_status_check <> 1 AND ( wp.status_check_interval <> 0 AND ( wp.offline_checks_last + wp.status_check_interval * 60 < UNIX_TIMESTAMP() ) )' .
609 $where . '
610 LIMIT ' . intval( $count );
611 return $sql;
612 }
613
614 /**
615 * Get child sites by user id via SQL.
616 *
617 * @param int $userid Given user ID.
618 * @param bool $selectgroups Selected groups. Default: false.
619 * @param null $search_site Site search field value. Default: null.
620 * @param string $orderBy Order list by. Default: URL.
621 * @param bool $offset Query offset. Default: false.
622 * @param bool $rowcount Row count. Default: falese.
623 *
624 * @return object|null Return database query or null on failure.
625 *
626 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
627 */
628 public function get_sql_websites_by_user_id( $userid, $selectgroups = false, $search_site = null, $orderBy = 'wp.url', $offset = false, $rowcount = false ) {
629 if ( MainWP_Utility::ctype_digit( $userid ) ) {
630 $where = '';
631 if ( null !== $search_site ) {
632 $search_site = trim( $search_site );
633 $where = ' AND (wp.name LIKE "%' . $search_site . '%" OR wp.url LIKE "%' . $search_site . '%") ';
634 }
635
636 $where .= $this->get_sql_where_allow_access_sites( 'wp' );
637
638 if ( $selectgroups ) {
639 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*, GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ",") as wpgroups, GROUP_CONCAT(gr.id ORDER BY gr.name SEPARATOR ",") as wpgroupids
640 FROM ' . $this->table_name( 'wp' ) . ' wp
641 LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid
642 LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id
643 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
644 JOIN ' . $this->get_option_view() . ' wp_optionview ON wp.id = wp_optionview.wpid
645 WHERE wp.userid = ' . $userid . "
646 $where
647 GROUP BY wp.id, wp_sync.sync_id
648 ORDER BY " . $orderBy;
649 } else {
650 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*
651 FROM ' . $this->table_name( 'wp' ) . ' wp
652 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
653 JOIN ' . $this->get_option_view() . ' wp_optionview ON wp.id = wp_optionview.wpid
654 WHERE wp.userid = ' . $userid . "
655 $where
656 ORDER BY " . $orderBy;
657 }
658
659 if ( ( false !== $offset ) && ( false !== $rowcount ) ) {
660 $qry .= ' LIMIT ' . $offset . ', ' . $rowcount;
661 } elseif ( false !== $rowcount ) {
662 $qry .= ' LIMIT ' . $rowcount;
663 }
664
665 return $qry;
666 }
667
668 return null;
669 }
670
671 /**
672 * Get SQL to get child sites for current user.
673 *
674 * @param bool $selectgroups Selected groups. Default: false.
675 * @param null $search_site Site search field value. Default: null.
676 * @param string $orderBy Order list by. Default: URL.
677 * @param bool $offset Query offset. Default: false.
678 * @param bool $rowcount Row count. Default: false.
679 * @param null $extraWhere Extra WHERE. Default: null.
680 * @param bool $for_manager For role manager. Default: false.
681 * @param mixed $extra_view Extra view. Default favi_icon.
682 * @param string $is_staging yes|no Is child site a staging site.
683 * @param array $params other params.
684 *
685 * @return object|null Database query results or null on failure.
686 *
687 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
688 */
689 public function get_sql_websites_for_current_user(
690 $selectgroups = false,
691 $search_site = null,
692 $orderBy = 'wp.url',
693 $offset = false,
694 $rowcount = false,
695 $extraWhere = null,
696 $for_manager = false,
697 $extra_view = array( 'favi_icon' ),
698 $is_staging = 'no',
699 $params = array()
700 ) {
701
702 $where = '';
703 if ( MainWP_System::instance()->is_multi_user() ) {
704
705 /**
706 * Current user global.
707 *
708 * @global string
709 */
710 global $current_user;
711
712 $where .= ' AND wp.userid = ' . $current_user->ID . ' ';
713 }
714
715 if ( null !== $search_site ) {
716 $search_site = trim( $search_site );
717 $where .= ' AND (wp.name LIKE "%' . $search_site . '%" OR wp.url LIKE "%' . $search_site . '%") ';
718 }
719
720 if ( null !== $extraWhere ) {
721 $where .= ' AND ' . $extraWhere;
722 }
723
724 if ( ! $for_manager ) {
725 $where .= $this->get_sql_where_allow_access_sites( 'wp', $is_staging );
726 }
727
728 $connected_sql = '';
729 if ( is_array( $params ) ) {
730 if ( isset( $params['connected'] ) ) {
731 if ( 'yes' === $params['connected'] ) {
732 $connected_sql = ' AND wp_sync.sync_errors = "" ';
733 } elseif ( 'no' === $params['connected'] ) {
734 $connected_sql = ' AND wp_sync.sync_errors <> "" ';
735 }
736 }
737 }
738
739 if ( 'wp.url' === $orderBy ) {
740 $orderBy = "replace(replace(replace(replace(replace(wp.url, 'https://www.',''), 'http://www.',''), 'https://', ''), 'http://', ''), 'www.', '')";
741 }
742
743 // wpgroups to fix issue for mysql 8.0, as groups will generate error syntax.
744 if ( $selectgroups ) {
745 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*, GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ",") as wpgroups, GROUP_CONCAT(gr.id ORDER BY gr.name SEPARATOR ",") as wpgroupids,
746 wpclient.name as client_name
747 FROM ' . $this->table_name( 'wp' ) . ' wp
748 LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid
749 LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id
750 LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id
751 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
752 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
753 WHERE 1 ' . $where . $connected_sql . '
754 GROUP BY wp.id, wp_sync.sync_id
755 ORDER BY ' . $orderBy;
756 } else {
757 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*, wpclient.name as client_name
758 FROM ' . $this->table_name( 'wp' ) . ' wp
759 LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id
760 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
761 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
762 WHERE 1 ' . $where . $connected_sql . '
763 GROUP BY wp.id, wp_sync.sync_id
764 ORDER BY ' . $orderBy;
765 }
766
767 if ( ( false !== $offset ) && ( false !== $rowcount ) ) {
768 $qry .= ' LIMIT ' . $offset . ', ' . $rowcount;
769 } elseif ( false !== $rowcount ) {
770 $qry .= ' LIMIT ' . $rowcount;
771 }
772
773 return $qry;
774 }
775
776 /**
777 * Get SQL to get wp child sites for current user.
778 *
779 * @since 4.3
780 *
781 * @param array $params params .
782 *
783 * @return object|null Database query results or null on failure.
784 */
785 public function get_sql_wp_for_current_user( $params = array() ) {
786 if ( ! is_array( $params ) ) {
787 $params = array();
788 }
789
790 $selectgroups = ! empty( $params['select_groups'] ) ? true : false;
791 $search_site = isset( $params['search_site'] ) && ! empty( $params['search_site'] ) ? $params['search_site'] : null;
792 $orderBy = isset( $params['order_by'] ) && ! empty( $params['order_by'] ) ? $params['order_by'] : 'wp.url';
793 $offset = isset( $params['offset'] ) ? $params['offset'] : false;
794 $rowcount = isset( $params['row_count'] ) ? $params['row_count'] : false;
795 $for_manager = isset( $params['for_manager'] ) ? $params['for_manager'] : false;
796 $extraWhere = isset( $params['extra_where'] ) && ! empty( $params['extra_where'] ) ? $params['extra_where'] : null;
797 $extra_view = isset( $params['extra_view'] ) && is_array( $params['extra_view'] ) && ! empty( $params['extra_view'] ) ? $params['extra_view'] : array( 'favi_icon' );
798 $extra_join = isset( $params['extra_join'] ) ? $params['extra_join'] : '';
799
800 $extra_select_wp_fields = isset( $params['extra_select_wp_fields'] ) && is_array( $params['extra_select_wp_fields'] ) && ! empty( $params['extra_select_wp_fields'] ) ? $params['extra_select_wp_fields'] : array();
801 $extra_select_sql_fields = isset( $params['extra_select_sql_fields'] ) && ! empty( $params['extra_select_sql_fields'] ) ? $params['extra_select_sql_fields'] : '';
802
803 $is_staging = isset( $params['is_staging'] ) && 'yes' === $params['is_staging'] ? 'yes' : 'no';
804 $count_only = isset( $params['count_only'] ) && $params['count_only'] ? true : false;
805
806 $where = '';
807
808 if ( null !== $search_site ) {
809 $search_site = trim( $search_site );
810 $where .= ' AND (wp.name LIKE "%' . $this->escape( $search_site ) . '%" OR wp.url LIKE "%' . $this->escape( $search_site ) . '%") ';
811 }
812
813 if ( null !== $extraWhere ) {
814 $where .= ' AND ' . $extraWhere;
815 }
816
817 if ( ! $for_manager ) {
818 $where .= $this->get_sql_where_allow_access_sites( 'wp', $is_staging );
819 }
820
821 if ( 'wp.url' === $orderBy ) {
822 $orderBy = "replace(replace(replace(replace(replace(wp.url, 'https://www.',''), 'http://www.',''), 'https://', ''), 'http://', ''), 'www.', '')";
823 }
824
825 $select_wp_fields = $this->get_sql_select_wp_valid_fields( $extra_select_wp_fields );
826
827 if ( ! empty( $extra_select_sql_fields ) ) {
828 $extra_select_sql_fields = ',' . $extra_select_sql_fields;
829 }
830
831 // wpgroups to fix issue for mysql 8.0, as groups will generate error syntax.
832 if ( $selectgroups ) {
833 if ( $count_only ) {
834 $select = ' COUNT(DISTINCT(wp.id)) ';
835 } else {
836 $select = $select_wp_fields . '
837 ' . $extra_select_sql_fields . '
838 ,wp_sync.sync_errors,wp_optionview.*, GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ",") as wpgroups, GROUP_CONCAT(gr.id ORDER BY gr.name SEPARATOR ",") as wpgroupids, wpclient.name as client_name ';
839 }
840 $qry = 'SELECT ' . $select . '
841 FROM ' . $this->table_name( 'wp' ) . ' wp
842 LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid
843 LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id
844 LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id
845 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
846 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid ' .
847 $extra_join . '
848 WHERE 1 ' . $where;
849 if ( ! $count_only ) {
850 $qry .= ' GROUP BY wp.id, wp_sync.sync_id';
851 }
852 $qry .= ' ORDER BY ' . $orderBy;
853 } else {
854 if ( $count_only ) {
855 $select = ' COUNT(DISTINCT(wp.id)) ';
856 } else {
857 $select = $select_wp_fields . '
858 ' . $extra_select_sql_fields . '
859 ,wp_sync.sync_errors,wp_optionview.*, wpclient.name as client_name ';
860 }
861 $qry = 'SELECT ' . $select . '
862 FROM ' . $this->table_name( 'wp' ) . ' wp
863 LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id
864 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
865 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid ' .
866 $extra_join . '
867 WHERE 1 ' . $where;
868 if ( ! $count_only ) {
869 $qry .= ' GROUP BY wp.id, wp_sync.sync_id';
870 }
871 $qry .= ' ORDER BY ' . $orderBy;
872 }
873
874 if ( ! $count_only ) {
875 if ( ( false !== $offset ) && ( false !== $rowcount ) ) {
876 $qry .= ' LIMIT ' . intval( $offset ) . ', ' . intval( $rowcount );
877 } elseif ( false !== $rowcount ) {
878 $qry .= ' LIMIT ' . intval( $rowcount );
879 }
880 }
881 return $qry;
882 }
883 /**
884 * Get SQL select websites fields.
885 *
886 * @since 4.3
887 *
888 * @param array $other_fields extra select wp fields .
889 *
890 * @return string sql string.
891 */
892 public function get_sql_select_wp_valid_fields( $other_fields = array() ) {
893
894 $allow_other_fields = array(
895 'offline_checks_last',
896 'offline_check_result',
897 'http_response_code',
898 'disable_status_check',
899 'disable_health_check',
900 'status_check_interval',
901 'health_threshold',
902 'note',
903 'statsUpdate',
904 'directories',
905 'plugin_upgrades',
906 'theme_upgrades',
907 'translation_upgrades',
908 'premium_upgrades',
909 'securityIssues',
910 'themes',
911 'ignored_themes',
912 'plugins',
913 'ignored_plugins',
914 'users',
915 'categories',
916 'pluginDir',
917 'automatic_update',
918 'backup_before_upgrade',
919 'mainwpdir',
920 'is_ignoreCoreUpdates',
921 'is_ignorePluginUpdates',
922 'is_ignoreThemeUpdates',
923 'verify_certificate',
924 'force_use_ipv4',
925 'ssl_version',
926 'http_user',
927 'http_pass',
928 'wpe',
929 'is_staging',
930 'client_id',
931 );
932
933 $default_fields = array( 'id', 'url', 'name', 'adminname', 'verify_certificate', 'ssl_version', 'http_user', 'http_pass', 'suspended' );
934
935 $select = ' ';
936
937 foreach ( $default_fields as $field ) {
938 $select .= 'wp.' . $this->escape( $field ) . ',';
939 }
940 foreach ( $other_fields as $field ) {
941 if ( ! in_array( $field, $allow_other_fields ) ) {
942 continue;
943 }
944 if ( in_array( $field, $default_fields ) ) {
945 continue;
946 }
947 $select .= 'wp.' . $this->escape( $field ) . ',';
948 }
949 $select = rtrim( $select, ',' );
950 return $select;
951 }
952
953 /**
954 * Get child sites for current user.
955 *
956 * @param array $params to get sites. Default: array().
957 *
958 * @return array Results or null on failure.
959 *
960 * @uses \MainWP\Dashboard\MainWP_Utility::map_site()
961 */
962 public function get_websites_for_current_user( $params = array() ) {
963 if ( ! is_array( $params ) ) {
964 $params = array();
965 }
966
967 $selectgroups = isset( $params['selectgroups'] ) ? $params['selectgroups'] : false;
968 $search_site = isset( $params['search_site'] ) ? $params['search_site'] : null;
969 $orderBy = isset( $params['order_by'] ) ? $params['order_by'] : 'wp.url';
970 $offset = isset( $params['offset'] ) ? $params['offset'] : false;
971 $rowcount = isset( $params['rowcount'] ) ? $params['rowcount'] : false;
972 $extraWhere = isset( $params['where'] ) ? $params['where'] : null;
973 $extra_view = isset( $params['extra_view'] ) && is_array( $params['extra_view'] ) ? $params['extra_view'] : array( 'favi_icon' );
974 $is_staging = isset( $params['is_staging'] ) ? $params['is_staging'] : 'no';
975 $full_data = isset( $params['full_data'] ) && $params['full_data'] && ( 'no' !== $params['full_data'] ) ? true : false;
976 $select_data = isset( $params['select_data'] ) && is_array( $params['select_data'] ) ? $params['select_data'] : false;
977 $format = isset( $params['format'] ) ? $params['format'] : '';
978 $clients = isset( $params['client'] ) ? $params['client'] : '';
979
980 $for_manager = false;
981
982 $urlsWhere = '';
983
984 if ( isset( $params['urls'] ) && ! empty( $params['urls'] ) ) {
985 $urls = explode( ';', $params['urls'] );
986 foreach ( $urls as $url ) {
987 $url = str_replace( array( 'https://www.', 'http://www.', 'https://', 'http://', 'www.' ), array( '', '', '', '', '' ), $url );
988 if ( '/' !== substr( $url, - 1 ) ) {
989 $url .= '/';
990 }
991 $urlsWhere .= '"' . $this->escape( $url ) . '", ';
992 }
993 $urlsWhere = rtrim( $urlsWhere, ', ' );
994 }
995
996 if ( ! empty( $urlsWhere ) ) {
997 $urlsWhere = " ( replace(replace(replace(replace(replace(wp.url, 'https://www.',''), 'http://www.',''), 'https://', ''), 'http://', ''), 'www.', '') IN ( " . $urlsWhere . ') ) ';
998
999 if ( empty( $extraWhere ) ) {
1000 $extraWhere = $urlsWhere;
1001 } else {
1002 $extraWhere = $extraWhere . ' AND ' . $urlsWhere;
1003 }
1004 }
1005
1006 $clientWhere = '';
1007 if ( ! empty( $clients ) ) {
1008 $clients = explode( ';', $clients );
1009 foreach ( $clients as $client ) {
1010 if ( is_numeric( $client ) ) {
1011 $clientWhere .= intval( $client ) . ', ';
1012 }
1013 }
1014 $clientWhere = rtrim( $clientWhere, ', ' );
1015 }
1016
1017 if ( ! empty( $clientWhere ) ) {
1018 $clientWhere = ' ( wp.client_id IN ( ' . $clientWhere . ') ) ';
1019 if ( empty( $extraWhere ) ) {
1020 $extraWhere = $clientWhere;
1021 } else {
1022 $extraWhere = $extraWhere . ' AND ' . $clientWhere;
1023 }
1024 }
1025
1026 $data = array( 'id', 'url', 'name', 'client_id' );
1027
1028 if ( $full_data ) {
1029 $data = array(
1030 'id',
1031 'url',
1032 'name',
1033 'offline_checks_last',
1034 'offline_check_result',
1035 'http_response_code',
1036 'disable_status_check',
1037 'disable_health_check',
1038 'status_check_interval',
1039 'health_threshold',
1040 'note',
1041 'plugin_upgrades',
1042 'theme_upgrades',
1043 'translation_upgrades',
1044 'securityIssues',
1045 'themes',
1046 'plugins',
1047 'automatic_update',
1048 'sync_errors',
1049 'dtsAutomaticSync',
1050 'dtsAutomaticSyncStart',
1051 'dtsSync',
1052 'dtsSyncStart',
1053 'last_post_gmt',
1054 'health_value',
1055 'phpversion',
1056 'wp_upgrades',
1057 'security_stats',
1058 'client_id',
1059 'adminname',
1060 'privkey',
1061 'http_user',
1062 'http_pass',
1063 'ssl_version',
1064 'signature_algo',
1065 'verify_method',
1066 'verify_certificate',
1067 );
1068
1069 if ( ! in_array( 'security_stats', $extra_view ) ) {
1070 $extra_view[] = 'security_stats';
1071 }
1072 }
1073
1074 if ( ! empty( $select_data ) && is_array( $select_data ) ) {
1075 $data = $select_data;
1076 }
1077
1078 if ( $selectgroups ) {
1079 $data[] = 'wpgroups';
1080 }
1081
1082 $dbwebsites = array();
1083 $websites = $this->query( $this->get_sql_websites_for_current_user( $selectgroups, $search_site, $orderBy, $offset, $rowcount, $extraWhere, $for_manager, $extra_view, $is_staging ) );
1084 while ( $websites && ( $website = self::fetch_object( $websites ) ) ) {
1085 $obj_data = MainWP_Utility::map_site( $website, $data );
1086
1087 if ( $full_data ) {
1088 $sum_upgrades = 0;
1089 if ( '' !== $obj_data->plugin_upgrades ) {
1090 $plugin_upgrades = json_decode( $obj_data->plugin_upgrades, true );
1091 if ( is_array( $plugin_upgrades ) ) {
1092 $sum_upgrades += count( $plugin_upgrades );
1093 }
1094 }
1095
1096 if ( '' !== $obj_data->theme_upgrades ) {
1097 $theme_upgrades = json_decode( $obj_data->theme_upgrades, true );
1098 if ( is_array( $theme_upgrades ) ) {
1099 $sum_upgrades += count( $theme_upgrades );
1100 }
1101 }
1102
1103 if ( '' !== $obj_data->wp_upgrades ) {
1104 $wp_upgrades = json_decode( $obj_data->wp_upgrades, true );
1105 if ( is_array( $wp_upgrades ) ) {
1106 $sum_upgrades += count( $wp_upgrades );
1107 }
1108 }
1109 $obj_data->sum_of_upgrades = $sum_upgrades;
1110 }
1111
1112 if ( 'array' === $format ) {
1113 $dbwebsites[] = $obj_data;
1114 } else {
1115 $dbwebsites[ $website->id ] = $obj_data;
1116 }
1117 }
1118 self::free_result( $websites );
1119 return $dbwebsites;
1120 }
1121
1122 /**
1123 * Get the child sites the current user has searched for.
1124 *
1125 * @param array $params Query parameters.
1126 *
1127 * @return boolean|null $qry Database query results or null on failure.
1128 *
1129 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
1130 */
1131 public function get_sql_search_websites_for_current_user( $params ) {
1132
1133 if ( ! is_array( $params ) ) {
1134 $params = array();
1135 }
1136
1137 $selectgroups = isset( $params['selectgroups'] ) && $params['selectgroups'] ? true : false;
1138 $search_site = isset( $params['search'] ) ? $this->escape( trim( $params['search'] ) ) : null;
1139 $orderBy = isset( $params['orderby'] ) ? $params['orderby'] : 'wp.url';
1140 $offset = isset( $params['offset'] ) ? intval( $params['offset'] ) : false;
1141 $rowcount = isset( $params['rowcount'] ) ? intval( $params['rowcount'] ) : false;
1142 $extraWhere = isset( $params['extra_where'] ) ? $params['extra_where'] : null;
1143 $for_manager = isset( $params['for_manager'] ) && $params['for_manager'] ? true : false;
1144 $extra_view = isset( $params['extra_view'] ) ? $params['extra_view'] : array( 'favi_icon' );
1145 $is_staging = isset( $params['is_staging'] ) && 'yes' === $params['is_staging'] ? 'yes' : 'no';
1146 $is_count = isset( $params['count_only'] ) && $params['count_only'] ? true : false;
1147 $group_ids = isset( $params['group_id'] ) && ! empty( $params['group_id'] ) ? $params['group_id'] : array();
1148 $client_ids = isset( $params['client_id'] ) && ! empty( $params['client_id'] ) ? $params['client_id'] : array();
1149 $is_not = isset( $params['isnot'] ) && ! empty( $params['isnot'] ) ? true : false;
1150 $selected_sites = isset( $params['selected_sites'] ) ? $params['selected_sites'] : array();
1151
1152 if ( ! is_array( $group_ids ) ) {
1153 $group_ids = array();
1154 }
1155
1156 // valid group ids.
1157 $group_ids = array_filter(
1158 $group_ids,
1159 function ( $e ) {
1160 if ( 'nogroups' === $e ) {
1161 return true;
1162 }
1163 return ( is_numeric( $e ) && 0 < $e ) ? true : false;
1164 }
1165 );
1166
1167 if ( ! is_array( $client_ids ) ) {
1168 $client_ids = array();
1169 }
1170
1171 // valid group ids.
1172 $client_ids = array_filter(
1173 $client_ids,
1174 function ( $e ) {
1175 if ( 'noclients' === $e ) {
1176 return true;
1177 }
1178 return is_numeric( $e ) && ! empty( $e ) ? true : false; // to valid client ids.
1179 }
1180 );
1181
1182 if ( $selectgroups ) {
1183 $staging_group = get_option( 'mainwp_stagingsites_group_id' );
1184 if ( $staging_group ) {
1185 if ( in_array( $staging_group, $group_ids ) ) {
1186 if ( 0 === count( $group_ids ) ) {
1187 $is_staging = 'yes';
1188 } else {
1189 $is_staging = 'nocheckstaging';
1190 }
1191 }
1192 }
1193 }
1194
1195 if ( ! is_array( $selected_sites ) ) {
1196 $selected_sites = array();
1197 }
1198 $selected_sites = MainWP_Utility::array_numeric_filter( $selected_sites );
1199
1200 $where = '';
1201 if ( MainWP_System::instance()->is_multi_user() ) {
1202
1203 /**
1204 * Current user global.
1205 *
1206 * @global string
1207 */
1208 global $current_user;
1209
1210 $where .= ' AND wp.userid = ' . $current_user->ID . ' ';
1211 }
1212
1213 if ( ! empty( $selected_sites ) ) {
1214 $where .= ' AND wp.id IN (' . implode( ',', $selected_sites ) . ') ';
1215 }
1216
1217 // for searching.
1218 if ( null !== $search_site && '' !== $search_site ) {
1219 $where .= ' AND (wp.name LIKE "%' . $search_site . '%" OR wp.url LIKE "%' . $search_site . '%") ';
1220 }
1221
1222 if ( null !== $extraWhere ) {
1223 $where .= ' AND ' . $extraWhere;
1224 }
1225
1226 if ( ! $for_manager ) {
1227 $where .= $this->get_sql_where_allow_access_sites( 'wp', $is_staging );
1228 }
1229
1230 if ( $is_count ) {
1231 $orderBy = '';
1232 } elseif ( 'wp.url' === $orderBy ) {
1233 $orderBy = "replace(replace(replace(replace(replace(wp.url, 'https://www.',''), 'http://www.',''), 'https://', ''), 'http://', ''), 'www.', '')";
1234 }
1235
1236 if ( ! empty( $orderBy ) ) {
1237 $orderBy = ' ORDER BY ' . $orderBy;
1238 }
1239
1240 $join_group = '';
1241 $where_group = '';
1242
1243 if ( in_array( 'nogroups', $group_ids ) ) {
1244 $join_group = ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid ';
1245 $group_ids = array_filter(
1246 $group_ids,
1247 function ( $e ) {
1248 return 'nogroups' !== $e;
1249 }
1250 );
1251 if ( 0 < count( $group_ids ) ) {
1252 $groups = implode( ',', $group_ids );
1253 if ( $is_not ) {
1254 $where_group = ' AND wpgroup.groupid IS NOT NULL AND wpgroup.groupid NOT IN (' . $groups . ') ';
1255 // to fix.
1256 $sub_select_is_not = ' SELECT wp.id FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid WHERE wpgroup.groupid IN (' . $groups . ') ';
1257 $where_group .= ' AND wp.id NOT IN ( ' . $sub_select_is_not . ' ) ';
1258 } else {
1259 $where_group = ' AND ( wpgroup.groupid IS NULL OR wpgroup.groupid IN (' . $groups . ') ) ';
1260 }
1261 } elseif ( $is_not ) {
1262 $where_group = ' AND wpgroup.groupid IS NOT NULL ';
1263 } else {
1264 $where_group = ' AND wpgroup.groupid IS NULL ';
1265 }
1266 } elseif ( $group_ids && 0 < count( $group_ids ) ) {
1267 $groups = implode( ',', $group_ids );
1268 if ( $is_not ) {
1269 $join_group = ' LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid ';
1270 $where_group = ' AND ( wpgroup.groupid NOT IN (' . $groups . ') OR wpgroup.groupid IS NULL ) ';
1271 // to fix.
1272 $sub_select_is_not = ' SELECT wp.id FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid WHERE wpgroup.groupid IN (' . $groups . ') ';
1273 $where_group .= ' AND wp.id NOT IN ( ' . $sub_select_is_not . ' ) ';
1274 } else {
1275 $join_group = ' JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid ';
1276 $where_group = ' AND wpgroup.groupid IN (' . $groups . ') ';
1277 }
1278 }
1279
1280 $join_client = '';
1281 $where_client = '';
1282
1283 if ( in_array( 'noclients', $client_ids ) ) {
1284 $join_client = ' LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id ';
1285 $client_ids = array_filter(
1286 $client_ids,
1287 function ( $e ) {
1288 return 'noclients' !== $e;
1289 }
1290 );
1291 if ( 0 < count( $client_ids ) ) {
1292 $clients = implode( ',', $client_ids );
1293 if ( $is_not ) {
1294 $where_client = ' AND wpclient.client_id IS NOT NULL AND wp.client_id NOT IN (' . $clients . ') ';
1295 } else {
1296 $where_client = ' AND wpclient.client_id IN (' . $clients . ') ';
1297 }
1298 } elseif ( $is_not ) {
1299 $where_client = ' AND wpclient.client_id IS NOT NULL ';
1300 } else {
1301 $where_client = ' AND wpclient.client_id IS NULL ';
1302 }
1303 } elseif ( $client_ids && 0 < count( $client_ids ) ) {
1304 $clients = implode( ',', $client_ids );
1305 if ( $is_not ) {
1306 $join_client = ' LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id ';
1307 $where_client = ' AND ( wpclient.client_id NOT IN (' . $clients . ') OR wpclient.client_id IS NULL ) ';
1308 } else {
1309 $join_client = ' JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id ';
1310 $where_client = ' AND wpclient.client_id IN (' . $clients . ') ';
1311 }
1312 }
1313
1314 if ( '' === $join_client ) {
1315 $join_client = ' LEFT JOIN ' . $this->table_name( 'wp_clients' ) . ' wpclient ON wp.client_id = wpclient.client_id ';
1316 }
1317 // wpgroups to fix issue for mysql 8.0, as groups will generate error syntax.
1318 if ( $selectgroups ) {
1319 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*, GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ",") as wpgroups, GROUP_CONCAT(gr.id ORDER BY gr.name SEPARATOR ",") as wpgroupids, wpclient.name as client_name
1320 FROM ' . $this->table_name( 'wp' ) . ' wp ' .
1321 $join_group . ' ' .
1322 $join_client . '
1323 LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid
1324 LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id
1325
1326 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1327 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
1328 WHERE 1 ' . $where . $where_group . $where_client . '
1329 GROUP BY wp.id, wp_sync.sync_id ' .
1330 $orderBy;
1331 } else {
1332 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*, wpclient.name as client_name
1333 FROM ' . $this->table_name( 'wp' ) . ' wp ' .
1334 $join_group . ' ' .
1335 $join_client . '
1336 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1337 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
1338 WHERE 1 ' . $where . $where_group . $where_client . '
1339 GROUP BY wp.id, wp_sync.sync_id ' .
1340 $orderBy;
1341 }
1342
1343 if ( ( false !== $offset ) && ( false !== $rowcount ) ) {
1344 $qry .= ' LIMIT ' . $offset . ', ' . $rowcount;
1345 } elseif ( false !== $rowcount ) {
1346 $qry .= ' LIMIT ' . $rowcount;
1347 }
1348 return $qry;
1349 }
1350
1351 /**
1352 * Get child sites where allowed access via SQL.
1353 *
1354 * @param string $site_table_alias Child site table alias.
1355 * @param string $is_staging yes|no Is child site a staging site.
1356 *
1357 * @return boolean|null $_where Database query results or null on failure.
1358 */
1359 public function get_sql_where_allow_access_sites( $site_table_alias = '', $is_staging = 'no' ) {
1360
1361 if ( empty( $site_table_alias ) ) {
1362 $site_table_alias = $this->table_name( 'wp' );
1363 }
1364
1365 // check to filter the staging sites.
1366 $where_staging = ' AND ' . $site_table_alias . '.is_staging = 0 ';
1367 if ( 'no' === $is_staging ) {
1368 $where_staging = ' AND ' . $site_table_alias . '.is_staging = 0 ';
1369 } elseif ( 'yes' === $is_staging ) {
1370 $where_staging = ' AND ' . $site_table_alias . '.is_staging = 1 ';
1371 } elseif ( 'nocheckstaging' === $is_staging ) {
1372 $where_staging = '';
1373 }
1374 // end staging filter.
1375
1376 $_where = $where_staging;
1377 // To fix bug run from cron job.
1378 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
1379 return $_where;
1380 }
1381
1382 // To fix bug run from wp cli.
1383 if ( defined( 'WP_CLI' ) && WP_CLI ) {
1384 return $_where;
1385 }
1386
1387 // Run from Rest Api.
1388 if ( defined( 'MAINWP_REST_API_DOING' ) && MAINWP_REST_API_DOING ) {
1389 return $_where;
1390 }
1391
1392 /**
1393 * Filter: mainwp_currentuserallowedaccesssites
1394 *
1395 * Filters allowed sites for the current user.
1396 *
1397 * @since Unknown
1398 */
1399 $allowed_sites = apply_filters( 'mainwp_currentuserallowedaccesssites', 'all' );
1400
1401 if ( 'all' === $allowed_sites ) {
1402 return $_where;
1403 }
1404
1405 if ( is_array( $allowed_sites ) && 0 < count( $allowed_sites ) ) {
1406 // valid group ids.
1407 $allowed_sites = array_filter(
1408 $allowed_sites,
1409 function ( $e ) {
1410 return is_numeric( $e ) ? true : false;
1411 }
1412 );
1413 $_where .= ' AND ' . $site_table_alias . '.id IN (' . implode( ',', $allowed_sites ) . ') ';
1414 } else {
1415 $_where .= ' AND 0 ';
1416 }
1417
1418 return $_where;
1419 }
1420
1421 /**
1422 * Get groupd where allowed access via SQL.
1423 *
1424 * @param string $group_table_alias Child site table alias.
1425 * @param string $with_staging yes|no Is child site a staging site.
1426 *
1427 * @return boolean|null $_where Database query results or null on failer.
1428 */
1429 public function get_sql_where_allow_groups( $group_table_alias = '', $with_staging = 'no' ) {
1430
1431 if ( empty( $group_table_alias ) ) {
1432 $group_table_alias = $this->table_name( 'group' );
1433 }
1434
1435 // check to filter the staging group.
1436 $where_staging_group = '';
1437 $staging_group = get_option( 'mainwp_stagingsites_group_id' );
1438 if ( $staging_group ) {
1439 $where_staging_group = ' AND ' . $group_table_alias . '.id <> ' . $staging_group . ' ';
1440 if ( 'yes' === $with_staging ) {
1441 $where_staging_group = '';
1442 }
1443 }
1444
1445 // end staging filter.
1446 $_where = $where_staging_group;
1447
1448 // To fix bug run from cron job.
1449 if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
1450 return $_where;
1451 }
1452
1453 // Run from wp cli.
1454 if ( defined( 'WP_CLI' ) && WP_CLI ) {
1455 return $_where;
1456 }
1457
1458 // Run from Rest Api.
1459 if ( defined( 'MAINWP_REST_API_DOING' ) && MAINWP_REST_API_DOING ) {
1460 return $_where;
1461 }
1462
1463 /**
1464 * Filter: mainwp_currentuserallowedaccessgroups
1465 *
1466 * Filters allowed groups for the current user.
1467 *
1468 * @since Unknown
1469 */
1470 $allowed_groups = apply_filters( 'mainwp_currentuserallowedaccessgroups', 'all' );
1471
1472 if ( 'all' === $allowed_groups ) {
1473 return $_where;
1474 }
1475
1476 if ( is_array( $allowed_groups ) && 0 < count( $allowed_groups ) ) {
1477
1478 // valid group ids.
1479 $allowed_groups = array_filter(
1480 $allowed_groups,
1481 function ( $e ) {
1482 return is_numeric( $e ) ? true : false;
1483 }
1484 );
1485
1486 return ' AND ' . $group_table_alias . '.id IN (' . implode( ',', $allowed_groups ) . ') ' . $_where;
1487 } else {
1488 return ' AND 0 ';
1489 }
1490 }
1491
1492 /**
1493 * Get child site by id.
1494 *
1495 * @param int $id Child site ID.
1496 * @param array $selectGroups Select groups.
1497 * @param array $extra_view Get extra option fields.
1498 *
1499 * @return object|null Database query results or null on failure.
1500 */
1501 public function get_website_by_id( $id, $selectGroups = false, $extra_view = array() ) {
1502 return $this->get_row_result( $this->get_sql_website_by_id( $id, $selectGroups, $extra_view ) );
1503 }
1504
1505 /**
1506 * Get child site by id via SQL.
1507 *
1508 * @param int $id Child site ID.
1509 * @param bool $selectGroups Selected groups.
1510 * @param mixed $extra_view Extra view value.
1511 *
1512 * @return object|null Database query result or null on failure.
1513 *
1514 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1515 */
1516 public function get_sql_website_by_id( $id, $selectGroups = false, $extra_view = array() ) {
1517
1518 if ( ! is_array( $extra_view ) || empty( $extra_view ) ) {
1519 $extra_view = array( 'favi_icon', 'site_info' );
1520 }
1521
1522 if ( MainWP_Utility::ctype_digit( $id ) ) {
1523 $where = $this->get_sql_where_allow_access_sites( 'wp', 'nocheckstaging' );
1524 if ( $selectGroups ) {
1525 return 'SELECT wp.*,wp_sync.*,wp_optionview.*, GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ",") as wpgroups, GROUP_CONCAT(gr.id ORDER BY gr.name SEPARATOR ",") as wpgroupids
1526 FROM ' . $this->table_name( 'wp' ) . ' wp
1527 LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid
1528 LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id
1529 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1530 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
1531 WHERE wp.id = ' . $id . $where . '
1532 GROUP BY wp.id, wp_sync.sync_id';
1533 }
1534
1535 return 'SELECT wp.*,wp_sync.*,wp_optionview.*
1536 FROM ' . $this->table_name( 'wp' ) . ' wp
1537 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1538 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
1539 WHERE id = ' . $id . $where;
1540 }
1541
1542 return null;
1543 }
1544
1545 /**
1546 * Method get_websites_by_ids()
1547 *
1548 * Get child sites by child site IDs.
1549 *
1550 * @param array $ids Child site IDs.
1551 * @param int $userId User ID.
1552 *
1553 * @return object|null Database query result or null on failure.
1554 *
1555 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
1556 */
1557 public function get_websites_by_ids( $ids, $userId = null ) {
1558 if ( ( null === $userId ) && MainWP_System::instance()->is_multi_user() ) {
1559
1560 /**
1561 * Current user global.
1562 *
1563 * @global string
1564 */
1565 global $current_user;
1566
1567 $userId = $current_user->ID;
1568 }
1569
1570 // valid group ids.
1571 $ids = array_filter(
1572 $ids,
1573 function ( $e ) {
1574 return ( is_numeric( $e ) && 0 < $e ) ? true : false;
1575 }
1576 );
1577
1578 $where = $this->get_sql_where_allow_access_sites();
1579
1580 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp' ) . ' WHERE id IN (' . implode( ',', $ids ) . ')' . ( null !== $userId ? ' AND userid = ' . intval( $userId ) : '' ) . $where, OBJECT );
1581 }
1582
1583 /**
1584 * Get child sites by groups IDs.
1585 *
1586 * @param array $ids Groups IDs.
1587 * @param int $userId User ID.
1588 *
1589 * @return object|null Database query result or null on failure.
1590 *
1591 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
1592 */
1593 public function get_websites_by_group_ids( $ids, $userId = null ) {
1594 if ( empty( $ids ) ) {
1595 return array();
1596 }
1597 if ( ( null === $userId ) && MainWP_System::instance()->is_multi_user() ) {
1598
1599 /**
1600 * Current user global.
1601 *
1602 * @global string
1603 */
1604 global $current_user;
1605
1606 $userId = $current_user->ID;
1607 }
1608
1609 // valid group ids.
1610 $group_ids = array_filter(
1611 $ids,
1612 function ( $e ) {
1613 return is_numeric( $e ) ? true : false;
1614 }
1615 );
1616
1617 return $this->wpdb->get_results( 'SELECT * FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid WHERE wpgroup.groupid IN (' . implode( ',', $group_ids ) . ') ' . ( null !== $userId ? ' AND wp.userid = ' . intval( $userId ) : '' ), OBJECT );
1618 }
1619
1620 /**
1621 * Get child sites by group ID.
1622 *
1623 * @param int $id Group ID.
1624 *
1625 * @return object|null Database query result or null on failure.
1626 */
1627 public function get_websites_by_group_id( $id ) {
1628 return $this->get_results_result( $this->get_sql_websites_by_group_id( $id ) );
1629 }
1630
1631 /**
1632 * Get child sites by group id via SQL.
1633 *
1634 * @param int $id Group ID.
1635 * @param bool $selectgroups Selected groups. Default: false.
1636 * @param string $orderBy Order list by. Default: URL.
1637 * @param bool $offset Query offset. Default: false.
1638 * @param bool $rowcount Row count. Default: falese.
1639 * @param null $where SQL WHERE value.
1640 * @param null $search_site Site search field value. Default: null.
1641 *
1642 * @return object|null Return database query or null on failure.
1643 *
1644 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1645 */
1646 public function get_sql_websites_by_group_id(
1647 $id,
1648 $selectgroups = false,
1649 $orderBy = 'wp.url',
1650 $offset = false,
1651 $rowcount = false,
1652 $where = null,
1653 $search_site = null
1654 ) {
1655
1656 $is_staging = 'no';
1657 if ( $selectgroups ) {
1658 $staging_group = get_option( 'mainwp_stagingsites_group_id' );
1659 if ( $staging_group ) {
1660 if ( $id === $staging_group ) {
1661 $is_staging = 'yes';
1662 }
1663 }
1664 }
1665
1666 $where_search = '';
1667 if ( ! empty( $search_site ) ) {
1668 $search_site = trim( $search_site );
1669 $where_search .= ' AND (wp.name LIKE "%' . $this->escape( $search_site ) . '%" OR wp.url LIKE "%' . $this->escape( $search_site ) . '%") ';
1670 }
1671
1672 if ( MainWP_Utility::ctype_digit( $id ) ) {
1673 $where_allowed = $this->get_sql_where_allow_access_sites( 'wp', $is_staging );
1674 if ( $selectgroups ) {
1675 $qry = 'SELECT wp.*,wp_sync.*,wp_optionview.*, GROUP_CONCAT(gr.name ORDER BY gr.name SEPARATOR ",") as wpgroups, GROUP_CONCAT(gr.id ORDER BY gr.name SEPARATOR ",") as wpgroupids
1676 FROM ' . $this->table_name( 'wp' ) . ' wp
1677 JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid
1678 LEFT JOIN ' . $this->table_name( 'wp_group' ) . ' wpgr ON wp.id = wpgr.wpid
1679 LEFT JOIN ' . $this->table_name( 'group' ) . ' gr ON wpgr.groupid = gr.id
1680 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1681 JOIN ' . $this->get_option_view( array( 'site_info' ), true ) . ' wp_optionview ON wp.id = wp_optionview.wpid
1682 WHERE wpgroup.groupid = ' . $id . ' ' .
1683 ( empty( $where ) ? '' : ' AND ' . $where ) . $where_allowed . $where_search . '
1684 GROUP BY wp.id, wp_sync.sync_id
1685 ORDER BY ' . $orderBy;
1686 } else {
1687 $qry = 'SELECT wp.*,wp_optionview.*, wp_sync.* FROM ' . $this->table_name( 'wp' ) . ' wp
1688 JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid
1689 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1690 JOIN ' . $this->get_option_view( array( 'site_info' ), false ) . ' wp_optionview ON wp.id = wp_optionview.wpid
1691 WHERE wpgroup.groupid = ' . $id . ' ' . $where_allowed . $where_search .
1692 ( empty( $where ) ? '' : ' AND ' . $where ) . ' ORDER BY ' . $orderBy;
1693 }
1694 if ( ( false !== $offset ) && ( false !== $rowcount ) ) {
1695 $qry .= ' LIMIT ' . $offset . ', ' . $rowcount;
1696 } elseif ( false !== $rowcount ) {
1697 $qry .= ' LIMIT ' . $rowcount;
1698 }
1699
1700 return $qry;
1701 }
1702
1703 return null;
1704 }
1705
1706 /**
1707 * Get child sites by group name.
1708 *
1709 * @param int $userid Current user ID.
1710 * @param string $groupname Group name.
1711 *
1712 * @return object|null Database query result or null on failure.
1713 */
1714 public function get_websites_by_group_name( $userid, $groupname ) {
1715 return $this->get_results_result( $this->get_sql_websites_by_group_name( $groupname, $userid ) );
1716 }
1717
1718 /**
1719 * Get child sites by group name.
1720 *
1721 * @param string $groupname Group name.
1722 * @param int $userid Current user ID.
1723 *
1724 * @return object|null Database query result or null on failure.
1725 *
1726 * @uses \MainWP\Dashboard\MainWP_System::is_multi_user()
1727 */
1728 public function get_sql_websites_by_group_name( $groupname, $userid = null ) {
1729 if ( ( null === $userid ) && MainWP_System::instance()->is_multi_user() ) {
1730
1731 /**
1732 * Current user global.
1733 *
1734 * @global string
1735 */
1736 global $current_user;
1737
1738 $userid = $current_user->ID;
1739 }
1740
1741 $sql = 'SELECT wp.*,wp_sync.*,wp_optionview.* FROM ' . $this->table_name( 'wp' ) . ' wp
1742 INNER JOIN ' . $this->table_name( 'wp_group' ) . ' wpgroup ON wp.id = wpgroup.wpid
1743 JOIN ' . $this->table_name( 'group' ) . ' g ON wpgroup.groupid = g.id
1744 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
1745 JOIN ' . $this->get_option_view() . ' wp_optionview ON wp.id = wp_optionview.wpid
1746 WHERE g.name="' . $this->escape( $groupname ) . '"';
1747 if ( null !== $userid ) {
1748 $sql .= ' AND g.userid = "' . intval( $userid ) . '"';
1749 }
1750
1751 return $sql;
1752 }
1753
1754 /**
1755 * Get child site IP address.
1756 *
1757 * @param int $wpid Child site ID.
1758 *
1759 * @return string|null Child site IP address or null on failure.
1760 */
1761 public function get_wp_ip( $wpid ) {
1762 return $this->wpdb->get_var( $this->wpdb->prepare( 'SELECT ip FROM ' . $this->table_name( 'request_log' ) . ' WHERE wpid = %d', $wpid ) );
1763 }
1764
1765 /**
1766 * Add website to the MainWP Dashboard.
1767 *
1768 * @param int $userid Current user ID.
1769 * @param string $name Child site name.
1770 * @param string $url Child site URL.
1771 * @param string $admin Child site administrator username.
1772 * @param string $pubkey OpenSSL public key.
1773 * @param string $privkey OpenSSL private key.
1774 * @param array $groupids Group IDs.
1775 * @param array $groupnames Group names.
1776 * @param int $verifyCertificate Whether or not to verify SSL Certificate.
1777 * @param string $uniqueId Unique security ID.
1778 * @param string $http_user HTTP Basic Authentication username.
1779 * @param string $http_pass HTTP Basic Authentication password.
1780 * @param int $sslVersion SSL Version.
1781 * @param int $wpe Is it WP Engine hosted site.
1782 * @param int $isStaging Whether or not child site is staging site.
1783 *
1784 * @return int|false Child site ID or false on failure.
1785 *
1786 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1787 */
1788 public function add_website(
1789 $userid,
1790 $name,
1791 $url,
1792 $admin,
1793 $pubkey,
1794 $privkey,
1795 $groupids,
1796 $groupnames,
1797 $verifyCertificate = 1,
1798 $uniqueId = '',
1799 $http_user = null,
1800 $http_pass = null,
1801 $sslVersion = 0,
1802 $wpe = 0,
1803 $isStaging = 0
1804 ) {
1805
1806 if ( MainWP_Utility::ctype_digit( $userid ) ) {
1807 if ( '/' !== substr( $url, - 1 ) ) {
1808 $url .= '/';
1809 }
1810 $values = array(
1811 'userid' => $userid,
1812 'adminname' => $this->escape( $admin ),
1813 'name' => $this->escape( wp_strip_all_tags( $name ) ),
1814 'url' => $this->escape( $url ),
1815 'pubkey' => $this->escape( $pubkey ),
1816 'privkey' => $this->escape( $privkey ),
1817 'siteurl' => '',
1818 'ga_id' => '',
1819 'gas_id' => 0,
1820 'offline_checks_last' => 0,
1821 'offline_check_result' => 0,
1822 'note' => '',
1823 'statsUpdate' => 0,
1824 'directories' => '',
1825 'plugin_upgrades' => '',
1826 'theme_upgrades' => '',
1827 'translation_upgrades' => '',
1828 'securityIssues' => '',
1829 'themes' => '',
1830 'ignored_themes' => '',
1831 'plugins' => '',
1832 'ignored_plugins' => '',
1833 'users' => '',
1834 'categories' => '',
1835 'pluginDir' => '',
1836 'automatic_update' => 0,
1837 'backup_before_upgrade' => 2,
1838 'verify_certificate' => intval( $verifyCertificate ),
1839 'ssl_version' => $sslVersion,
1840 'uniqueId' => $uniqueId,
1841 'mainwpdir' => 0,
1842 'http_user' => $http_user,
1843 'http_pass' => $http_pass,
1844 'wpe' => $wpe,
1845 'is_staging' => $isStaging,
1846 );
1847
1848 $syncValues = array(
1849 'dtsSync' => 0,
1850 'dtsSyncStart' => 0,
1851 'dtsAutomaticSync' => 0,
1852 'dtsAutomaticSyncStart' => 0,
1853 'totalsize' => 0,
1854 'extauth' => '',
1855 'sync_errors' => '',
1856 );
1857 if ( $this->wpdb->insert( $this->table_name( 'wp' ), $values ) ) {
1858 $websiteid = $this->wpdb->insert_id;
1859 $syncValues['wpid'] = $websiteid;
1860 $this->wpdb->insert( $this->table_name( 'wp_sync' ), $syncValues );
1861 $this->wpdb->insert(
1862 $this->table_name( 'wp_settings_backup' ),
1863 array(
1864 'wpid' => $websiteid,
1865 'archiveFormat' => 'global',
1866 )
1867 );
1868
1869 foreach ( $groupnames as $groupname ) {
1870 if ( $this->wpdb->insert(
1871 $this->table_name( 'group' ),
1872 array(
1873 'userid' => $userid,
1874 'name' => $this->escape( htmlspecialchars( $groupname ) ),
1875 )
1876 )
1877 ) {
1878 $groupids[] = $this->wpdb->insert_id;
1879 }
1880 }
1881 // add groupids.
1882 foreach ( $groupids as $groupid ) {
1883 $this->wpdb->insert(
1884 $this->table_name( 'wp_group' ),
1885 array(
1886 'wpid' => $websiteid,
1887 'groupid' => $groupid,
1888 )
1889 );
1890 }
1891
1892 return $websiteid;
1893 }
1894 }
1895
1896 return false;
1897 }
1898
1899 /**
1900 * Remove child site from the MainWP Dashboard.
1901 *
1902 * @param int $websiteid Child site ID.
1903 *
1904 * @return int|boolean Return child site ID that was removed or false on failure.
1905 *
1906 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1907 */
1908 public function remove_website( $websiteid ) {
1909 if ( MainWP_Utility::ctype_digit( $websiteid ) ) {
1910 $nr = $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp' ) . ' WHERE id=%d', $websiteid ) );
1911 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE wpid=%d', $websiteid ) );
1912 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_sync' ) . ' WHERE wpid=%d', $websiteid ) );
1913 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_options' ) . ' WHERE wpid=%d', $websiteid ) );
1914 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_status' ) . ' WHERE wpid=%d', $websiteid ) );
1915
1916 return $nr;
1917 }
1918
1919 return false;
1920 }
1921
1922 /**
1923 * Update child site db values.
1924 *
1925 * @param int $websiteid Child site ID.
1926 * @param array $fields Database fields to update.
1927 *
1928 * @return int|boolean The number of rows updated, or false on error.
1929 */
1930 public function update_website_values( $websiteid, $fields ) {
1931 if ( 0 < count( $fields ) ) {
1932 return $this->wpdb->update( $this->table_name( 'wp' ), $fields, array( 'id' => $websiteid ) );
1933 }
1934
1935 return false;
1936 }
1937
1938 /**
1939 * Update child site sync values.
1940 *
1941 * @param int $websiteid Child site ID.
1942 * @param array $fields Database fields to update.
1943 *
1944 * @return int|boolean The number of rows updated, or false on error.
1945 */
1946 public function update_website_sync_values( $websiteid, $fields ) {
1947 if ( 0 < count( $fields ) ) {
1948 return $this->wpdb->update( $this->table_name( 'wp_sync' ), $fields, array( 'wpid' => $websiteid ) );
1949 }
1950
1951 return false;
1952 }
1953
1954 /**
1955 * Update child site.
1956 *
1957 * @param int $websiteid Website ID.
1958 * @param string $url Child site URL.
1959 * @param int $userid Current user ID.
1960 * @param string $name Child site name.
1961 * @param string $siteadmin Child site administrator username.
1962 * @param array $groupids Group IDs.
1963 * @param array $groupnames Group Names.
1964 * @param string $pluginDir Plugin directory.
1965 * @param mixed $maximumFileDescriptorsOverride Overwrite the Maximum File Descriptors option.
1966 * @param mixed $maximumFileDescriptorsAuto Auto set the Maximum File Descriptors option.
1967 * @param mixed $maximumFileDescriptors Set the Maximum File Descriptors option.
1968 * @param int $verifyCertificate Whether or not to verify SSL Certificate.
1969 * @param mixed $archiveFormat Backup archive formate.
1970 * @param string $uniqueId Unique security ID.
1971 * @param string $http_user HTTP Basic Authentication username.
1972 * @param string $http_pass HTTP Basic Authentication password.
1973 * @param int $sslVersion SSL Version.
1974 * @param int $disableChecking Wether or not disable sites status checking.
1975 * @param int $checkInterval Status checking interval.
1976 * @param bool $disableHealthChecking Disable Site health threshold.
1977 * @param int $healthThreshold Site health threshold.
1978 * @param int $wpe Is it WP Engine hosted site.
1979 *
1980 * @return boolean ture on success or false on failure.
1981 *
1982 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
1983 * @uses \MainWP\Dashboard\MainWP_Utility::ctype_digit()
1984 */
1985 public function update_website(
1986 $websiteid,
1987 $url,
1988 $userid,
1989 $name,
1990 $siteadmin,
1991 $groupids,
1992 $groupnames,
1993 $pluginDir,
1994 $maximumFileDescriptorsOverride,
1995 $maximumFileDescriptorsAuto,
1996 $maximumFileDescriptors,
1997 $verifyCertificate = 1,
1998 $archiveFormat = 'global',
1999 $uniqueId = '',
2000 $http_user = null,
2001 $http_pass = null,
2002 $sslVersion = 0,
2003 $disableChecking = 1,
2004 $checkInterval = 1440,
2005 $disableHealthChecking = 1,
2006 $healthThreshold = 80,
2007 $wpe = 0
2008 ) {
2009
2010 if ( MainWP_Utility::ctype_digit( $websiteid ) && MainWP_Utility::ctype_digit( $userid ) ) {
2011 $website = $this->get_website_by_id( $websiteid );
2012 if ( MainWP_System_Utility::can_edit_website( $website ) ) {
2013 // update admin.
2014 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET url="' . $this->escape( $url ) . '", name="' . $this->escape( wp_strip_all_tags( $name ) ) . '", adminname="' . $this->escape( $siteadmin ) . '",pluginDir="' . $this->escape( $pluginDir ) . '", verify_certificate="' . intval( $verifyCertificate ) . '", ssl_version="' . intval( $sslVersion ) . '", wpe="' . intval( $wpe ) . '", uniqueId="' . $this->escape( $uniqueId ) . '", http_user="' . $this->escape( $http_user ) . '", http_pass="' . $this->escape( $http_pass ) . '", disable_status_check="' . $this->escape( $disableChecking ) . '", status_check_interval="' . $this->escape( $checkInterval ) . '", disable_health_check="' . $this->escape( $disableHealthChecking ) . '", health_threshold="' . $this->escape( $healthThreshold ) . '" WHERE id=%d', $websiteid ) );
2015 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp_settings_backup' ) . ' SET archiveFormat = "' . $this->escape( $archiveFormat ) . '" WHERE wpid=%d', $websiteid ) );
2016
2017 if ( get_option( 'mainwp_enableLegacyBackupFeature' ) ) {
2018 $this->wpdb->query( $this->wpdb->prepare( 'UPDATE ' . $this->table_name( 'wp' ) . ' SET maximumFileDescriptorsOverride = ' . ( $maximumFileDescriptorsOverride ? 1 : 0 ) . ',maximumFileDescriptorsAuto= ' . ( $maximumFileDescriptorsAuto ? 1 : 0 ) . ',maximumFileDescriptors = ' . $maximumFileDescriptors . ' WHERE id=%d', $websiteid ) );
2019 }
2020
2021 // remove groups.
2022 $this->wpdb->query( $this->wpdb->prepare( 'DELETE FROM ' . $this->table_name( 'wp_group' ) . ' WHERE wpid=%d', $websiteid ) );
2023 // Remove GA stats.
2024 $showErrors = $this->wpdb->hide_errors();
2025
2026 /**
2027 * Action: mainwp_ga_delete_site
2028 *
2029 * Fires upon site removal process in order to delete Google Analytics data.
2030 *
2031 * @param int $websiteid Child site ID.
2032 *
2033 * @since Unknown
2034 */
2035 do_action( 'mainwp_ga_delete_site', $websiteid );
2036
2037 if ( $showErrors ) {
2038 $this->wpdb->show_errors();
2039 }
2040 // add groups with groupnames.
2041 foreach ( $groupnames as $groupname ) {
2042 if ( $this->wpdb->insert(
2043 $this->table_name( 'group' ),
2044 array(
2045 'userid' => $userid,
2046 'name' => $this->escape( $groupname ),
2047 )
2048 )
2049 ) {
2050 $groupids[] = $this->wpdb->insert_id;
2051 }
2052 }
2053 // add groupids.
2054 foreach ( $groupids as $groupid ) {
2055 $this->wpdb->insert(
2056 $this->table_name( 'wp_group' ),
2057 array(
2058 'wpid' => $websiteid,
2059 'groupid' => $groupid,
2060 )
2061 );
2062 }
2063
2064 return true;
2065 }
2066 }
2067
2068 return false;
2069 }
2070
2071 /**
2072 * Get websites check updates count.
2073 *
2074 * @param int $lasttime_start Lasttime start automatic update.
2075 *
2076 * @return int Child sites update count.
2077 */
2078 public function get_websites_check_updates_count( $lasttime_start ) {
2079 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2080
2081 return $this->wpdb->get_var( 'SELECT count(wp.id) FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid WHERE ( wp_sync.dtsAutomaticSyncStart = 0 OR wp_sync.dtsAutomaticSyncStart < ' . intval( $lasttime_start ) . ')' . $where );
2082 }
2083
2084 /**
2085 * Get child site count where date & time Session sync is smaller then start.
2086 *
2087 * @param int $lasttime_start Last time start automatic.
2088 *
2089 * @return int Returned child site count.
2090 */
2091 public function get_websites_count_where_dts_automatic_sync_smaller_then_start( $lasttime_start ) {
2092 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2093
2094 return $this->wpdb->get_var( 'SELECT count(wp.id) FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid WHERE (( wp_sync.dtsAutomaticSync < wp_sync.dtsAutomaticSyncStart AND wp_sync.dtsAutomaticSyncStart > ' . intval( $lasttime_start ) . ') OR (wp_sync.dtsAutomaticSyncStart = 0)) ' . $where );
2095 }
2096
2097 /**
2098 * Get child site last automatic sync date & time.
2099 *
2100 * @return string Date and time of last automatic sync.
2101 */
2102 public function get_websites_last_automatic_sync() {
2103 return $this->wpdb->get_var( 'SELECT MAX(wp_sync.dtsAutomaticSync) FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid' );
2104 }
2105
2106 /**
2107 * Get child sites check updates.
2108 *
2109 * @param int $limit Query limit.
2110 * @param int $lasttime_start Lasttime start automatic update.
2111 *
2112 * @return object|null Database query result or null on failure.
2113 */
2114 public function get_websites_check_updates( $limit, $lasttime_start ) {
2115 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2116
2117 return $this->wpdb->get_results( 'SELECT wp.*,wp_sync.*,wp_optionview.* FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid JOIN ' . $this->get_option_view() . ' wp_optionview ON wp.id = wp_optionview.wpid WHERE ( wp_sync.dtsAutomaticSync = 0 OR wp_sync.dtsAutomaticSyncStart = 0 OR wp_sync.dtsAutomaticSyncStart < ' . intval( $lasttime_start ) . ' ) ' . $where . ' ORDER BY wp_sync.dtsAutomaticSyncStart ASC LIMIT ' . $limit, OBJECT );
2118 }
2119
2120 /**
2121 * Get website update stats via SQL.
2122 *
2123 * @return object|null Database query result of null on failure.
2124 */
2125 public function get_websites_stats_update_sql() {
2126 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2127 return 'SELECT wp.*,wp_sync.sync_errors FROM ' . $this->table_name( 'wp' ) . ' wp JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid WHERE (wp.statsUpdate = 0 OR ' . time() . ' - wp.statsUpdate >= ' . ( 60 * 60 * 24 ) . ')' . $where . ' ORDER BY wp.statsUpdate ASC';
2128 }
2129
2130 /**
2131 * Update child site statistics.
2132 *
2133 * Update whether or not a child site has been updated.
2134 *
2135 * @param mixed $websiteid Child site ID.
2136 * @param mixed $statsUpdated Child site Update status.
2137 *
2138 * @return (int|boolean) Number of rows effected in update or false on failure.
2139 */
2140 public function update_website_stats( $websiteid, $statsUpdated ) {
2141 return $this->wpdb->update(
2142 $this->table_name( 'wp' ),
2143 array( 'statsUpdate' => $statsUpdated ),
2144 array( 'id' => $websiteid )
2145 );
2146 }
2147
2148 /**
2149 * Get child site by url.
2150 *
2151 * @param string $url Child site URL.
2152 *
2153 * @return object|null Database query result or null on failure.
2154 */
2155 public function get_websites_by_url( $url ) {
2156 if ( '/' !== substr( $url, - 1 ) ) {
2157 $url .= '/';
2158 }
2159 $results = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp' ) . ' WHERE url = %s ', $this->escape( $url ) ), OBJECT );
2160 if ( $results ) {
2161 return $results;
2162 }
2163
2164 if ( stristr( $url, '/www.' ) ) {
2165 // remove www if it's there!
2166 $url = str_replace( '/www.', '/', $url );
2167 } else {
2168 // add www if it's not there!
2169 $url = str_replace( 'https://', 'https://www.', $url );
2170 $url = str_replace( 'http://', 'http://www.', $url );
2171 }
2172
2173 $results = $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp' ) . ' WHERE url = %s ', $this->escape( $url ) ), OBJECT );
2174 if ( $results ) {
2175 return $results;
2176 }
2177
2178 $url = str_replace( array( 'https://www.', 'http://www.', 'https://', 'http://', 'www.' ), array( '', '', '', '', '' ), $url );
2179
2180 return $this->wpdb->get_results( $this->wpdb->prepare( 'SELECT * FROM ' . $this->table_name( 'wp' ) . " WHERE replace(replace(replace(replace(replace(url, 'https://www.',''), 'http://www.',''), 'https://', ''), 'http://', ''), 'www.', '') = %s ", $this->escape( $url ) ), OBJECT );
2181 }
2182
2183 /**
2184 * Get websites offline status.
2185 *
2186 * @return array Child site monitoring status.
2187 */
2188 public function get_websites_offline_status_to_send_notice() {
2189 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2190 $extra_view = array( 'monitoring_notification_emails', 'settings_notification_emails' );
2191
2192 return $this->wpdb->get_results(
2193 'SELECT wp.*,wp_sync.*,wp_optionview.* FROM ' . $this->table_name( 'wp' ) . ' wp
2194 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
2195 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
2196 WHERE wp.disable_status_check <> 1 AND wp.offline_check_result <> 1 AND wp.offline_check_result <> 0 AND wp.http_code_noticed = 0' . // http_code_noticed = 0: not noticed yet.
2197 $where,
2198 OBJECT
2199 );
2200 }
2201
2202 /**
2203 * Method get_websites_to_notice_health_threshold()
2204 *
2205 * Get websites to notice site health.
2206 *
2207 * @param int $globalThreshold Global site health threshold.
2208 * @param int $count Limit count.
2209 */
2210 public function get_websites_to_notice_health_threshold( $globalThreshold, $count = 10 ) {
2211
2212 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2213 $extra_view = array( 'monitoring_notification_emails', 'settings_notification_emails' );
2214
2215 if ( 80 >= $globalThreshold ) { // actual is 80.
2216 // should-be-improved site health.
2217 $where_global_threshold = '( wp.health_threshold = 0 AND wp_sync.health_value < 80 )';
2218 } else {
2219 // good site health.
2220 $where_global_threshold = '( wp.health_threshold = 0 AND wp_sync.health_value >= 80 )';
2221 }
2222
2223 $where_site_threshold = ' ( wp.health_threshold = 80 AND wp_sync.health_value < 80 ) '; // should-be-improved site health.
2224 $where_site_threshold .= ' OR ( wp.health_threshold = 100 AND wp_sync.health_value >= 80 ) '; // good site health.
2225
2226 return $this->wpdb->get_results(
2227 'SELECT wp.*,wp_sync.*,wp_optionview.* FROM ' . $this->table_name( 'wp' ) . ' wp
2228 JOIN ' . $this->table_name( 'wp_sync' ) . ' wp_sync ON wp.id = wp_sync.wpid
2229 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
2230 WHERE wp.disable_health_check <> 1 AND wp.offline_check_result = 1 AND ( ' . $where_global_threshold . ' OR' . $where_site_threshold . ' ) AND wp_sync.health_site_noticed = 0 ' .
2231 $where,
2232 OBJECT
2233 );
2234 }
2235
2236 /**
2237 * Get websites offline status.
2238 *
2239 * @return array Sites with offline status.
2240 */
2241 public function get_websites_offline_check_status() {
2242 $where = $this->get_sql_where_allow_access_sites( 'wp' );
2243 $extra_view = array( 'settings_notification_emails' );
2244
2245 return $this->wpdb->get_results(
2246 'SELECT wp.*,wp_optionview.* FROM ' . $this->table_name( 'wp' ) . ' wp
2247 JOIN ' . $this->get_option_view( $extra_view ) . ' wp_optionview ON wp.id = wp_optionview.wpid
2248 WHERE wp.disable_status_check <> 1 AND wp.offline_check_result = -1' . // offline checked status.
2249 $where,
2250 OBJECT
2251 );
2252 }
2253
2254 /**
2255 * Get DB Sites.
2256 *
2257 * @since 4.6
2258 *
2259 * @param mixed $params params.
2260 *
2261 * @return array $dbwebsites.
2262 */
2263 public static function get_db_sites( $params = array() ) {
2264
2265 $dbwebsites = array();
2266
2267 $data_fields = MainWP_System_Utility::get_default_map_site_fields();
2268 $data_fields[] = 'verify_certificate';
2269 $data_fields[] = 'client_id';
2270
2271 $fields = isset( $params['fields'] ) && is_array( $params['fields'] ) ? $params['fields'] : array();
2272 $sites = isset( $params['sites'] ) && is_array( $params['sites'] ) ? $params['sites'] : array();
2273 $groups = isset( $params['groups'] ) && is_array( $params['groups'] ) ? $params['groups'] : array();
2274 $clients = isset( $params['clients'] ) && is_array( $params['clients'] ) ? $params['clients'] : array();
2275
2276 if ( is_array( $fields ) ) {
2277 foreach ( $fields as $field_indx => $field_name ) {
2278
2279 $get_field = $field_name;
2280 if ( is_numeric( $get_field ) || is_bool( $get_field ) ) { // to compatible fix.
2281 $get_field = $field_indx;
2282 }
2283
2284 if ( in_array( $get_field, self::$possible_options ) ) {
2285 if ( ! in_array( $get_field, $data_fields ) ) {
2286 $data_fields[] = $get_field;
2287 }
2288 }
2289 }
2290 }
2291
2292 if ( ! empty( $sites ) ) {
2293 foreach ( $sites as $k => $v ) {
2294 if ( MainWP_Utility::ctype_digit( $v ) ) {
2295 $website = self::instance()->get_website_by_id( $v );
2296 if ( empty( $website ) ) {
2297 continue;
2298 }
2299 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
2300 }
2301 }
2302 }
2303
2304 if ( ! empty( $groups ) ) {
2305 foreach ( $groups as $k => $v ) {
2306 if ( MainWP_Utility::ctype_digit( $v ) ) {
2307 $websites = self::instance()->query( self::instance()->get_sql_websites_by_group_id( $v ) );
2308 while ( $websites && ( $website = self::fetch_object( $websites ) ) ) {
2309 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
2310 }
2311 self::free_result( $websites );
2312 }
2313 }
2314 }
2315
2316 $params = array(
2317 'full_data' => true,
2318 );
2319 $client_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $clients, $params );
2320 if ( $client_sites ) {
2321 foreach ( $client_sites as $website ) {
2322 $dbwebsites[ $website->id ] = MainWP_Utility::map_site( $website, $data_fields );
2323 }
2324 }
2325 return $dbwebsites;
2326 }
2327
2328 /**
2329 * Get Sites.
2330 *
2331 * @param int $websiteid The id of the child site you wish to retrieve.
2332 * @param bool $for_manager Check Team Control.
2333 * @param array $others Array of others.
2334 *
2335 * @return array $output Array of content to output.
2336 *
2337 * @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
2338 * @uses \MainWP\Dashboard\MainWP_Utility::get_nice_url()
2339 */
2340 public function get_sites( $websiteid = null, $for_manager = false, $others = array() ) { // phpcs:ignore -- not quite complex function.
2341
2342 if ( ! is_array( $others ) ) {
2343 $others = array();
2344 }
2345
2346 $search_site = null;
2347 $orderBy = 'wp.url';
2348 $offset = false;
2349 $rowcount = false;
2350 $extraWhere = null;
2351
2352 if ( isset( $websiteid ) && ( null !== $websiteid ) ) {
2353 $website = self::instance()->get_website_by_id( $websiteid );
2354
2355 if ( ! MainWP_System_Utility::can_edit_website( $website ) ) {
2356 return false;
2357 }
2358
2359 if ( ! mainwp_current_user_have_right( 'site', $websiteid ) ) {
2360 return false;
2361 }
2362
2363 return array(
2364 array(
2365 'id' => $websiteid,
2366 'url' => MainWP_Utility::get_nice_url( $website->url, true ),
2367 'name' => $website->name,
2368 'totalsize' => $website->totalsize,
2369 'sync_errors' => $website->sync_errors,
2370 ),
2371 );
2372 } else {
2373 if ( isset( $others['orderby'] ) ) {
2374 if ( ( 'site' === $others['orderby'] ) ) {
2375 $orderBy = 'wp.name ' . ( 'asc' === $others['order'] ? 'asc' : 'desc' );
2376 } elseif ( ( 'url' === $others['orderby'] ) ) {
2377 $orderBy = 'wp.url ' . ( 'asc' === $others['order'] ? 'asc' : 'desc' );
2378 }
2379 }
2380 if ( isset( $others['search'] ) ) {
2381 $search_site = trim( $others['search'] );
2382 }
2383
2384 if ( is_array( $others ) ) {
2385 if ( isset( $others['plugins_slug'] ) ) {
2386
2387 $slugs = explode( ',', $others['plugins_slug'] );
2388 $extraWhere = '';
2389 foreach ( $slugs as $slug ) {
2390 $slug = wp_json_encode( $slug );
2391 $slug = trim( $slug, '"' );
2392 $slug = str_replace( '\\', '.', $slug );
2393 $extraWhere .= ' wp.plugins REGEXP "' . $slug . '" OR';
2394 }
2395 $extraWhere = trim( rtrim( $extraWhere, 'OR' ) );
2396
2397 if ( '' === $extraWhere ) {
2398 $extraWhere = null;
2399 } else {
2400 $extraWhere = '(' . $extraWhere . ')';
2401 }
2402 }
2403 }
2404 }
2405
2406 $totalRecords = '';
2407
2408 if ( isset( $others['per_page'] ) && ! empty( $others['per_page'] ) ) {
2409 $sql = self::instance()->get_sql_websites_for_current_user( false, $search_site, $orderBy, false, false, $extraWhere, $for_manager );
2410 $websites_total = self::instance()->query( $sql );
2411 $totalRecords = ( $websites_total ? self::num_rows( $websites_total ) : 0 );
2412
2413 if ( $websites_total ) {
2414 self::free_result( $websites_total );
2415 }
2416
2417 $rowcount = absint( $others['per_page'] );
2418 $pagenum = isset( $others['paged'] ) ? absint( $others['paged'] ) : 0;
2419 if ( $pagenum > $totalRecords ) {
2420 $pagenum = $totalRecords;
2421 }
2422 $pagenum = max( 1, $pagenum );
2423 $offset = ( $pagenum - 1 ) * $rowcount;
2424
2425 }
2426
2427 $sql = self::instance()->get_sql_websites_for_current_user( false, $search_site, $orderBy, $offset, $rowcount, $extraWhere, $for_manager );
2428 $websites = self::instance()->query( $sql );
2429
2430 $output = array();
2431 while ( $websites && ( $website = self::fetch_object( $websites ) ) ) {
2432 $re = array(
2433 'id' => $website->id,
2434 'url' => MainWP_Utility::get_nice_url( $website->url, true ),
2435 'name' => $website->name,
2436 'totalsize' => $website->totalsize,
2437 'sync_errors' => $website->sync_errors,
2438 'client_id' => $website->client_id,
2439 );
2440
2441 if ( 0 < $totalRecords ) {
2442 $re['totalRecords'] = $totalRecords;
2443 $totalRecords = 0;
2444 }
2445
2446 $output[] = $re;
2447 }
2448 self::free_result( $websites );
2449
2450 return $output;
2451 }
2452 }
2453