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