| 1 |
<?php |
| 2 |
/** |
| 3 |
* Queries the database for logs records. |
| 4 |
* |
| 5 |
* @package MainWP/Dashboard |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace MainWP\Dashboard\Module\Log; |
| 9 |
|
| 10 |
use MainWP\Dashboard\MainWP_DB; |
| 11 |
use MainWP\Dashboard\MainWP_DB_Client; |
| 12 |
use MainWP\Dashboard\MainWP_Utility; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class - Log_Query |
| 21 |
*/ |
| 22 |
class Log_Query { |
| 23 |
/** |
| 24 |
* Hold the number of records found |
| 25 |
* |
| 26 |
* @var int |
| 27 |
*/ |
| 28 |
public $found_records = 0; |
| 29 |
|
| 30 |
/** |
| 31 |
* Query records |
| 32 |
* |
| 33 |
* @param array $args Arguments to filter the records by. |
| 34 |
* |
| 35 |
* @return array Logs Records |
| 36 |
*/ |
| 37 |
public function query( $args ) { //phpcs:ignore -- NOSONAR - complex method. |
| 38 |
global $wpdb; |
| 39 |
|
| 40 |
// To support none mainwp actions. |
| 41 |
$log_id = isset( $args['log_id'] ) ? intval( $args['log_id'] ) : 0; |
| 42 |
$site_id = isset( $args['wpid'] ) ? $args['wpid'] : 0; // int or array of int site ids. |
| 43 |
$where_extra = ''; // compatible. |
| 44 |
$check_access = isset( $args['check_access'] ) ? $args['check_access'] : true; |
| 45 |
$view = isset( $args['view'] ) ? sanitize_text_field( $args['view'] ) : ''; |
| 46 |
$optimize_get_dt = isset( $args['optimize'] ) && ! empty( $args['optimize'] ) ? true : false; |
| 47 |
$opti_with_meta = isset( $args['optimize_with_meta'] ) && ! empty( $args['optimize_with_meta'] ) ? true : false; |
| 48 |
$with_logs_meta = ! empty( $args['with_all_logs_meta'] ); |
| 49 |
|
| 50 |
$join = ''; |
| 51 |
$join_meta = ''; |
| 52 |
$select_view = ''; |
| 53 |
|
| 54 |
$where = ''; |
| 55 |
$search_str = ''; |
| 56 |
|
| 57 |
$count_only = ! empty( $args['count_only'] ) ? true : false; |
| 58 |
$not_count = ! empty( $args['not_count'] ) ? true : false; |
| 59 |
if ( ! empty( $args['search'] ) ) { |
| 60 |
$search_str = MainWP_DB::instance()->escape( $args['search'] ); |
| 61 |
// for searching. |
| 62 |
if ( ! empty( $search_str ) ) { |
| 63 |
$search_str = trim( $search_str ); |
| 64 |
// prepare search value for searching. |
| 65 |
if ( ! empty( $search_str ) ) { |
| 66 |
$where_search = ' AND ( wp.name LIKE "%' . $search_str . '%" OR lg.action LIKE "%' . $search_str . '%" '; |
| 67 |
$where_search .= ' OR ( CASE '; |
| 68 |
$where_search .= " WHEN lg.action = 'sync' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Sync Data', 'mainwp' ) ) . "' "; |
| 69 |
$where_search .= " WHEN lg.action = 'activate' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Activated', 'mainwp' ) ) . "' "; |
| 70 |
$where_search .= " WHEN lg.action = 'deactivate' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Deactivated', 'mainwp' ) ) . "' "; |
| 71 |
$where_search .= " WHEN lg.action = 'install' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Installed', 'mainwp' ) ) . "' "; |
| 72 |
$where_search .= " WHEN lg.action = 'updated' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Updated', 'mainwp' ) ) . "' "; |
| 73 |
$where_search .= " WHEN lg.action = 'delete' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Deleted', 'mainwp' ) ) . "' "; |
| 74 |
$where_search .= " WHEN lg.action = 'suspend' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Suspended', 'mainwp' ) ) . "' "; |
| 75 |
$where_search .= ' ELSE lg.action '; |
| 76 |
$where_search .= ' END ) LIKE "%' . $search_str . '%" '; |
| 77 |
$where_search .= ' OR lg.log_id LIKE "%' . $search_str . '%" OR lg.user_id LIKE "%' . $search_str . '%" '; |
| 78 |
$where_search .= ' OR lg.item LIKE "%' . $search_str . '%" '; |
| 79 |
if ( 'events_list' === $view ) { |
| 80 |
$where_search .= ' OR ( CASE '; |
| 81 |
$where_search .= " WHEN lg.connector = 'non-mainwp-changes' THEN 'WP Admin' "; |
| 82 |
$where_search .= " ELSE 'Dashboard' "; |
| 83 |
$where_search .= ' END ) LIKE "%' . $search_str . '%" '; |
| 84 |
$where_search .= ' OR lg.user_login LIKE "%' . $search_str . '%" '; |
| 85 |
} |
| 86 |
$where_search .= ') '; |
| 87 |
$where .= $where_search; |
| 88 |
|
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
if ( isset( $args['dismiss'] ) ) { |
| 94 |
$where .= ' AND lg.dismiss = ' . ( ! empty( $args['dismiss'] ) ? 1 : 0 ) . ' '; |
| 95 |
} |
| 96 |
|
| 97 |
if ( ! empty( $args['groups_ids'] ) && is_array( $args['groups_ids'] ) ) { |
| 98 |
$array_groups_ids = MainWP_Utility::array_numeric_filter( $args['groups_ids'] ); |
| 99 |
if ( ! empty( $array_groups_ids ) ) { |
| 100 |
$groups_sites = MainWP_DB_Client::instance()->get_websites_by_group_ids( $array_groups_ids ); |
| 101 |
$array_website_ids = array(); |
| 102 |
if ( $groups_sites ) { |
| 103 |
foreach ( $groups_sites as $website ) { |
| 104 |
$array_website_ids[] = $website->id; |
| 105 |
} |
| 106 |
} |
| 107 |
unset( $groups_sites ); |
| 108 |
if ( ! empty( $array_website_ids ) ) { |
| 109 |
$where .= " AND lg.site_id IN ('" . implode( "','", $array_website_ids ) . "') "; |
| 110 |
} else { |
| 111 |
$where .= ' AND false '; |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if ( ! empty( $args['client_ids'] ) && is_array( $args['client_ids'] ) ) { |
| 117 |
$array_clients_ids = MainWP_Utility::array_numeric_filter( $args['client_ids'] ); |
| 118 |
if ( ! empty( $array_clients_ids ) ) { |
| 119 |
$client_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $array_clients_ids ); |
| 120 |
$array_website_ids = array(); |
| 121 |
if ( $client_sites ) { |
| 122 |
foreach ( $client_sites as $website ) { |
| 123 |
$array_website_ids[] = $website->id; |
| 124 |
} |
| 125 |
} |
| 126 |
unset( $client_sites ); |
| 127 |
if ( ! empty( $array_website_ids ) ) { |
| 128 |
$where .= " AND lg.site_id IN ('" . implode("','",$array_website_ids) . "') "; // phpcs:ignore -- ok. |
| 129 |
} else { |
| 130 |
$where .= ' AND false '; |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
$where_users_filter = ''; |
| 136 |
if ( ! empty( $args['usersfilter_sites_ids'] ) && is_array( $args['usersfilter_sites_ids'] ) ) { |
| 137 |
$usersfilter_sites_ids = $args['usersfilter_sites_ids']; |
| 138 |
$cond_users = array(); |
| 139 |
foreach ( $usersfilter_sites_ids as $user_filter ) { |
| 140 |
if ( false !== strpos( $user_filter, '-' ) ) { // new users filter. |
| 141 |
list( $uid, $sid, $is_dash_user ) = explode( '-', $user_filter ); |
| 142 |
if ( $is_dash_user ) { |
| 143 |
$cond_users[] = ' lg.user_id = ' . (int) $uid . ' AND lg.connector != "non-mainwp-changes" '; // dashboard user does not need to check site ids. |
| 144 |
} else { |
| 145 |
$cond_users[] = ' lg.user_id = ' . (int) $uid . ' AND lg.site_id = ' . (int) $sid . ' AND lg.connector = "non-mainwp-changes" '; // child site user need to check site ids. |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
if ( ! empty( $cond_users ) ) { |
| 150 |
$where_users_filter = ' AND ( ' . implode( ') OR (', $cond_users ) . ') '; |
| 151 |
} |
| 152 |
} elseif ( ! empty( $args['user_ids'] ) && is_array( $args['user_ids'] ) ) { // compatible. |
| 153 |
$array_users_ids = MainWP_Utility::array_numeric_filter( $args['user_ids'] ); |
| 154 |
if ( ! empty( $array_users_ids ) ) { |
| 155 |
$where .= " AND lg.user_id IN ('" . implode("','",$array_users_ids) . "') "; // phpcs:ignore -- ok. |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if ( ! empty( $args['timestart'] ) && ! empty( $args['timestop'] ) ) { |
| 160 |
$timestart_us = (int) $args['timestart'] * 1000000; |
| 161 |
$timestop_us = (int) $args['timestop'] * 1000000; |
| 162 |
$where .= $wpdb->prepare( ' AND `lg`.`created` >= %d AND `lg`.`created` <= %d', $timestart_us, $timestop_us ); |
| 163 |
} |
| 164 |
|
| 165 |
// available sources conds values: wp-admin-only|dashboard-only|empty. |
| 166 |
if ( ! empty( $args['sources_conds'] ) ) { |
| 167 |
if ( 'wp-admin-only' === $args['sources_conds'] ) { |
| 168 |
$where .= ' AND ( `lg`.`connector` = "non-mainwp-changes" ) '; |
| 169 |
} elseif ( 'dashboard-only' === $args['sources_conds'] ) { |
| 170 |
$where .= ' AND `lg`.`connector` != "non-mainwp-changes" '; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if ( ! empty( $args['contexts'] ) ) { |
| 175 |
$contexts_list = explode( ',', $args['contexts'] ); |
| 176 |
$contexts_list = array_map( |
| 177 |
function ( $value ) { |
| 178 |
return MainWP_DB::instance()->escape( $value ); |
| 179 |
}, |
| 180 |
(array) $contexts_list |
| 181 |
); |
| 182 |
$contexts_list = array_filter( $contexts_list ); |
| 183 |
if ( ! empty( $contexts_list ) ) { |
| 184 |
$where .= ' AND lg.context IN ( "' . implode( '","', $contexts_list ) . '" ) '; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
if ( ! empty( $args['sites_ids'] ) ) { |
| 189 |
$where_site_ids = implode( ',', array_filter( array_map( 'intval', (array) $args['sites_ids'] ) ) ); |
| 190 |
if ( ! empty( $where_site_ids ) ) { |
| 191 |
$where .= ' AND lg.site_id IN ( ' . $where_site_ids . ' ) '; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if ( ! empty( $args['events'] ) ) { |
| 196 |
$events_list = array_map( |
| 197 |
function ( $value ) { |
| 198 |
return MainWP_DB::instance()->escape( $value ); |
| 199 |
}, |
| 200 |
(array) $args['events'] |
| 201 |
); |
| 202 |
$events_list = array_filter( $events_list ); |
| 203 |
if ( ! empty( $events_list ) ) { |
| 204 |
$where .= ' AND lg.action IN ( "' . implode( '","', $events_list ) . '" ) '; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* PARSE PAGINATION PARAMS |
| 210 |
*/ |
| 211 |
$limits = ''; |
| 212 |
$start = absint( $args['start'] ); |
| 213 |
$per_page = absint( $args['records_per_page'] ); |
| 214 |
|
| 215 |
if ( $per_page > 0 ) { |
| 216 |
$limits = "LIMIT {$start}, {$per_page}"; |
| 217 |
} |
| 218 |
|
| 219 |
// Show the recent records first by default. |
| 220 |
$order = 'DESC'; |
| 221 |
if ( 'ASC' === strtoupper( $args['order'] ) ) { |
| 222 |
$order = 'ASC'; |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* PARSE ORDER PARAMS |
| 227 |
*/ |
| 228 |
$orderable = array( 'site_id', 'name', 'url', 'user_id', 'item', 'created', 'connector', 'context', 'action', 'event', 'duration', 'state' ); |
| 229 |
|
| 230 |
// Default to sorting by. |
| 231 |
$orderby = 'lg.created'; |
| 232 |
|
| 233 |
if ( in_array( $args['orderby'], $orderable, true ) ) { |
| 234 |
if ( in_array( $args['orderby'], array( 'name', 'url' ) ) ) { |
| 235 |
$orderby = sprintf( '%s', $args['orderby'] ); |
| 236 |
} elseif ( 'event' === $args['orderby'] || 'action' === $args['orderby'] ) { |
| 237 |
$orderby = sprintf( '%s.%s', 'lg', 'action' ); |
| 238 |
} else { |
| 239 |
$orderby = sprintf( '%s.%s', 'lg', $args['orderby'] ); |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if ( 'source' === $args['orderby'] ) { |
| 244 |
$orderby = " ORDER BY |
| 245 |
CASE |
| 246 |
WHEN connector = 'non-mainwp-changes' THEN 2 |
| 247 |
ELSE 1 |
| 248 |
END " . $order; |
| 249 |
} elseif ( 'log_object' === $args['orderby'] ) { |
| 250 |
$orderby = sprintf( 'ORDER BY %s %s', $orderby, $order ); |
| 251 |
} else { |
| 252 |
$orderby = sprintf( 'ORDER BY %s %s', $orderby, $order ); |
| 253 |
} |
| 254 |
|
| 255 |
$where_actions = ''; |
| 256 |
|
| 257 |
if ( ! empty( $log_id ) ) { |
| 258 |
$where_actions .= ' AND lg.log_id = ' . $log_id; |
| 259 |
} else { |
| 260 |
$sql_and = ''; |
| 261 |
if ( ! empty( $site_id ) ) { |
| 262 |
if ( is_array( $site_id ) ) { |
| 263 |
$site_ids = array_map( 'intval', $site_id ); |
| 264 |
$site_ids = array_filter( $site_ids ); |
| 265 |
if ( ! empty( $site_ids ) ) { |
| 266 |
$site_ids = implode( ',', $site_ids ); |
| 267 |
$sql_and = ' AND '; |
| 268 |
$where_actions .= $sql_and . ' lg.site_id IN ( ' . $site_ids . ' )'; |
| 269 |
} |
| 270 |
} elseif ( is_numeric( $site_id ) ) { |
| 271 |
$sql_and = ' AND '; |
| 272 |
$where_actions .= $sql_and . ' lg.site_id = ' . intval( $site_id ); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
$where .= $where_actions . $where_extra; |
| 278 |
|
| 279 |
/** |
| 280 |
* PARSE FIELDS PARAMETER |
| 281 |
*/ |
| 282 |
$selects = array(); |
| 283 |
$selects[] = 'lg.*'; |
| 284 |
|
| 285 |
if ( 'api-view' !== $view ) { |
| 286 |
$selects[] = 'wp.url as url'; |
| 287 |
$selects[] = 'wp.name as log_site_name'; |
| 288 |
} |
| 289 |
|
| 290 |
if ( 'api-view' !== $view ) { |
| 291 |
$join = ' INNER JOIN ' . $wpdb->mainwp_tbl_wp . ' wp ON lg.site_id = wp.id '; |
| 292 |
// Improve query. |
| 293 |
if ( $check_access && 'api-view' !== $view ) { |
| 294 |
$join .= MainWP_DB::instance()->get_sql_where_allow_access_sites( 'wp' ); // Example: AND wp.is_staging = 0 or empty. |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
$mt_params = array(); |
| 299 |
|
| 300 |
$optimize_get_meta = false; |
| 301 |
if ( ! $optimize_get_dt ) { |
| 302 |
$join_meta .= ' LEFT JOIN ' . $this->get_log_meta_view( $mt_params ) . ' meta_view ON lg.log_id = meta_view.log_id '; |
| 303 |
} elseif ( $opti_with_meta ) { |
| 304 |
$optimize_get_meta = true; |
| 305 |
} |
| 306 |
|
| 307 |
if ( 'events_list' === $view ) { |
| 308 |
$select_view .= $this->select_fields_view(); |
| 309 |
} |
| 310 |
|
| 311 |
$recent_where = ''; |
| 312 |
$recent_query = ''; |
| 313 |
// list recent events. |
| 314 |
if ( ! empty( $args['recent_number'] ) ) { |
| 315 |
$recent_limits = ' LIMIT ' . intval( $args['recent_number'] ); |
| 316 |
|
| 317 |
$recent_query = "SELECT MAX( lg.created ) |
| 318 |
FROM $wpdb->mainwp_tbl_logs as lg |
| 319 |
{$join} |
| 320 |
{$join_meta} |
| 321 |
WHERE `lg`.`connector` != 'compact' ORDER BY lg.created DESC {$recent_limits}"; |
| 322 |
|
| 323 |
$recent_created = $wpdb->get_var( $recent_query ); //phpcs:ignore -- NOSONAR - ok. |
| 324 |
|
| 325 |
$recent_where = ' AND lg.created <= ' . (int) $recent_created; |
| 326 |
} |
| 327 |
|
| 328 |
if ( ! empty( $recent_where ) ) { |
| 329 |
$orderby = ''; |
| 330 |
$limits = ''; |
| 331 |
} |
| 332 |
|
| 333 |
if ( ! empty( $join_meta ) ) { |
| 334 |
// Improve select. |
| 335 |
$selects[] = 'meta_view.meta_name'; |
| 336 |
$selects[] = 'meta_view.user_meta_json'; |
| 337 |
$selects[] = 'meta_view.usermeta'; |
| 338 |
$selects[] = 'meta_view.extra_info'; |
| 339 |
$selects[] = 'lg.log_id as view_log_id'; // deprecate compatible. |
| 340 |
} |
| 341 |
|
| 342 |
$select = implode( ', ', $selects ); |
| 343 |
|
| 344 |
/** |
| 345 |
* BUILD THE FINAL QUERY |
| 346 |
*/ |
| 347 |
$query = "SELECT {$select}{$select_view} |
| 348 |
FROM $wpdb->mainwp_tbl_logs as lg |
| 349 |
{$join} |
| 350 |
{$join_meta} |
| 351 |
WHERE `lg`.`connector` != 'compact' {$where} {$where_users_filter} {$recent_where} |
| 352 |
{$orderby} |
| 353 |
{$limits}"; |
| 354 |
|
| 355 |
// Build result count query. |
| 356 |
// Join meta, join sub for search conditionals if existed. |
| 357 |
$count_query = "SELECT COUNT(*) |
| 358 |
FROM $wpdb->mainwp_tbl_logs as lg |
| 359 |
{$join}"; |
| 360 |
if ( ! empty( $search_str ) ) { |
| 361 |
$count_query .= "{$join_meta}"; |
| 362 |
} |
| 363 |
$count_query .= " WHERE `lg`.`connector` != 'compact' {$where} {$where_users_filter} {$recent_where} "; |
| 364 |
|
| 365 |
if ( ! empty( $recent_query ) ) { |
| 366 |
MainWP_DB::instance()->log_system_query( $args, $recent_query, $this ); |
| 367 |
} |
| 368 |
|
| 369 |
if ( ! $count_only ) { |
| 370 |
MainWP_DB::instance()->log_system_query( $args, $query, $this ); |
| 371 |
} |
| 372 |
|
| 373 |
if ( $count_only || ! $not_count ) { |
| 374 |
MainWP_DB::instance()->log_system_query( $args, $count_query, $this ); |
| 375 |
} |
| 376 |
|
| 377 |
// Generate cache key for count query. |
| 378 |
$cache_key = 'mainwp_logs_count_' . md5( serialize( $args ) ); // NOSONAR - MD5 used for cache key generation only, not cryptographic purposes. |
| 379 |
|
| 380 |
if ( $count_only ) { |
| 381 |
$cached_count = wp_cache_get( $cache_key, 'mainwp_logs' ); |
| 382 |
if ( false !== $cached_count ) { |
| 383 |
return array( |
| 384 |
'count' => $cached_count, |
| 385 |
); |
| 386 |
} |
| 387 |
|
| 388 |
$count = absint( $wpdb->get_var( $count_query ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.NotPrepared -- Count query is built with properly escaped and validated SQL fragments (WHERE clauses use escape(), intval(), and sanitize_text_field(); JOIN and recent_where are static or int-cast). |
| 389 |
wp_cache_set( $cache_key, $count, 'mainwp_logs', HOUR_IN_SECONDS ); |
| 390 |
|
| 391 |
return array( |
| 392 |
'count' => $count, |
| 393 |
); |
| 394 |
} |
| 395 |
|
| 396 |
$items = $wpdb->get_results( $query ); // phpcs:ignore -- ok. |
| 397 |
|
| 398 |
if ( ( ( $optimize_get_dt && $optimize_get_meta ) || $with_logs_meta ) && $items ) { |
| 399 |
|
| 400 |
$ids = array_map( 'absint', wp_list_pluck( $items, 'log_id' ) ); |
| 401 |
|
| 402 |
$start_slice = 0; |
| 403 |
$max_slice = 100; |
| 404 |
$count = count( $ids ); |
| 405 |
|
| 406 |
while ( $start_slice <= $count ) { |
| 407 |
$slice_ids = array_slice( $ids, $start_slice, $max_slice ); |
| 408 |
$start_slice += $max_slice; |
| 409 |
|
| 410 |
if ( ! empty( $slice_ids ) ) { |
| 411 |
|
| 412 |
$sql_meta = sprintf( |
| 413 |
"SELECT * FROM $wpdb->mainwp_tbl_logs_meta WHERE meta_log_id IN ( %s )", |
| 414 |
implode( ',', $slice_ids ) |
| 415 |
); |
| 416 |
|
| 417 |
$meta_records = $wpdb->get_results( $sql_meta ); //phpcs:ignore -- ok. |
| 418 |
$ids_flip = array_flip( $ids ); |
| 419 |
|
| 420 |
if ( is_array( $meta_records ) ) { |
| 421 |
foreach ( $meta_records as $meta_record ) { |
| 422 |
if ( ! empty( $meta_record->meta_value ) ) { |
| 423 |
// compatible format. |
| 424 |
if ( in_array( $meta_record->meta_key, array( 'user_meta_json', 'user_login', 'extra_info' ) ) ) { |
| 425 |
$items[ $ids_flip[ $meta_record->meta_log_id ] ]->{$meta_record->meta_key} = $meta_record->meta_value; |
| 426 |
} else { |
| 427 |
if ( empty( $items[ $ids_flip[ $meta_record->meta_log_id ] ]->meta ) ) { |
| 428 |
$items[ $ids_flip[ $meta_record->meta_log_id ] ]->meta = array(); |
| 429 |
} |
| 430 |
$items[ $ids_flip[ $meta_record->meta_log_id ] ]->meta[ $meta_record->meta_key ] = $meta_record->meta_value; |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
$sites_opts = array(); |
| 440 |
// get sites meta data. |
| 441 |
if ( $items ) { |
| 442 |
$ids = array_map( 'absint', wp_list_pluck( $items, 'site_id' ) ); |
| 443 |
|
| 444 |
$start_slice = 0; |
| 445 |
$max_slice = 100; |
| 446 |
$count = count( $ids ); |
| 447 |
|
| 448 |
while ( $start_slice <= $count ) { |
| 449 |
$slice_ids = array_slice( $ids, $start_slice, $max_slice ); |
| 450 |
$start_slice += $max_slice; |
| 451 |
|
| 452 |
if ( ! empty( $slice_ids ) ) { |
| 453 |
|
| 454 |
$wp_opts = $with_logs_meta ? array( 'site_info', 'cust_site_icon_info', 'favi_icon' ) : array( 'site_info' ); |
| 455 |
|
| 456 |
$opts_records = Log_DB_Helper::instance()->get_sites_options( $slice_ids, $wp_opts ); |
| 457 |
|
| 458 |
if ( is_array( $opts_records ) ) { |
| 459 |
foreach ( $opts_records as $opt_record ) { |
| 460 |
if ( ! isset( $sites_opts[ $opt_record->wpid ] ) ) { |
| 461 |
$sites_opts[ $opt_record->wpid ] = array(); |
| 462 |
} |
| 463 |
if ( ! empty( $opt_record->value ) ) { |
| 464 |
$values = $opt_record->value; |
| 465 |
if ( 'site_info' === $opt_record->name ) { |
| 466 |
$values = json_decode( $values, true ); |
| 467 |
if ( ! is_array( $values ) ) { |
| 468 |
$values = array(); |
| 469 |
} |
| 470 |
} |
| 471 |
$sites_opts[ $opt_record->wpid ][ $opt_record->name ] = $values; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* QUERY THE DATABASE FOR RESULTS |
| 481 |
*/ |
| 482 |
$results = array( |
| 483 |
'items' => $items, |
| 484 |
'sites_opts' => $sites_opts, |
| 485 |
); |
| 486 |
|
| 487 |
if ( ! $not_count ) { |
| 488 |
$cached_count = wp_cache_get( $cache_key, 'mainwp_logs' ); |
| 489 |
if ( false !== $cached_count ) { |
| 490 |
$results['count'] = $cached_count; |
| 491 |
} else { |
| 492 |
$count = absint( $wpdb->get_var( $count_query ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,PluginCheck.Security.DirectDB.UnescapedDBParameter,WordPress.DB.PreparedSQL.NotPrepared -- NOSONAR Count query is built with properly escaped and validated SQL fragments (WHERE clauses use escape(), intval(), and sanitize_text_field(); JOIN and recent_where are static or int-cast). |
| 493 |
wp_cache_set( $cache_key, $count, 'mainwp_logs', HOUR_IN_SECONDS ); |
| 494 |
$results['count'] = $count; |
| 495 |
} |
| 496 |
} |
| 497 |
return $results; |
| 498 |
} |
| 499 |
|
| 500 |
/** |
| 501 |
* Get logs meta database table view. |
| 502 |
* |
| 503 |
* @return string logs meta view. |
| 504 |
*/ |
| 505 |
public function get_log_meta_view() { |
| 506 |
global $wpdb; |
| 507 |
// Improve select. |
| 508 |
return " ( SELECT |
| 509 |
meta_log_id AS log_id, |
| 510 |
MAX(CASE WHEN meta_key = 'name' THEN meta_value END) AS meta_name, |
| 511 |
MAX(CASE WHEN meta_key = 'user_meta_json' THEN meta_value END) AS user_meta_json, |
| 512 |
MAX(CASE WHEN meta_key = 'user_meta' THEN meta_value END) AS usermeta, |
| 513 |
MAX(CASE WHEN meta_key = 'extra_info' THEN meta_value END) AS extra_info |
| 514 |
FROM " . $wpdb->mainwp_tbl_logs_meta . " |
| 515 |
WHERE meta_key IN ('name', 'user_meta_json', 'user_meta', 'extra_info') |
| 516 |
GROUP BY meta_log_id ) "; |
| 517 |
} |
| 518 |
|
| 519 |
/** |
| 520 |
* Method get_sub_query to support seaching in events table. |
| 521 |
* deprecated @since 6.0. |
| 522 |
* |
| 523 |
* @return string sub query view. |
| 524 |
*/ |
| 525 |
public function get_sub_query_view() { |
| 526 |
global $wpdb; |
| 527 |
$view = ' (SELECT sub_tbl.log_id AS sub_log_id, '; |
| 528 |
$view .= ' CASE WHEN sub_tbl.connector = "non-mainwp-changes" THEN "WP Admin" '; |
| 529 |
$view .= ' ELSE "Dashboard" '; |
| 530 |
$view .= ' END AS source, '; |
| 531 |
// to support searching on events column. |
| 532 |
$view .= " CASE |
| 533 |
WHEN sub_tbl.action = 'sync' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Sync Data', 'mainwp' ) ) . "' |
| 534 |
WHEN sub_tbl.action = 'activate' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Activated', 'mainwp' ) ) . "' |
| 535 |
WHEN sub_tbl.action = 'deactivate' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Deactivated', 'mainwp' ) ) . "' |
| 536 |
WHEN sub_tbl.action = 'install' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Installed', 'mainwp' ) ) . "' |
| 537 |
WHEN sub_tbl.action = 'updated' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Updated', 'mainwp' ) ) . "' |
| 538 |
WHEN sub_tbl.action = 'delete' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Deleted', 'mainwp' ) ) . "' |
| 539 |
WHEN sub_tbl.action = 'suspend' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Suspended', 'mainwp' ) ) . "' |
| 540 |
ELSE sub_tbl.action |
| 541 |
END AS action_display "; |
| 542 |
$view .= ' FROM ' . $wpdb->mainwp_tbl_logs . ' sub_tbl) '; |
| 543 |
return $view; |
| 544 |
} |
| 545 |
|
| 546 |
/** |
| 547 |
* Method select_fields_view to support seaching in events table. |
| 548 |
* |
| 549 |
* @return string sub query view. |
| 550 |
*/ |
| 551 |
public function select_fields_view() { |
| 552 |
return ", CASE |
| 553 |
WHEN lg.connector = 'non-mainwp-changes' |
| 554 |
THEN 'WP Admin' |
| 555 |
ELSE 'Dashboard' |
| 556 |
END AS source, |
| 557 |
CASE |
| 558 |
WHEN lg.action = 'sync' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Sync Data', 'mainwp' ) ) . "' |
| 559 |
WHEN lg.action = 'activate' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Activated', 'mainwp' ) ) . "' |
| 560 |
WHEN lg.action = 'deactivate' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Deactivated', 'mainwp' ) ) . "' |
| 561 |
WHEN lg.action = 'install' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Installed', 'mainwp' ) ) . "' |
| 562 |
WHEN lg.action = 'updated' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Updated', 'mainwp' ) ) . "' |
| 563 |
WHEN lg.action = 'delete' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Deleted', 'mainwp' ) ) . "' |
| 564 |
WHEN lg.action = 'suspend' THEN '" . MainWP_DB::instance()->escape( esc_html__( 'Suspended', 'mainwp' ) ) . "' |
| 565 |
ELSE lg.action |
| 566 |
END AS action_display "; |
| 567 |
} |
| 568 |
} |
| 569 |
|