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