PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 5.1
MainWP Dashboard: Self-hosted WordPress Management for Agencies v5.1
6.2 6.1.8 6.1.7 6.1.6 6.1.5 6.1.4 6.1.3 6.1.2 6.1.1 6.1 6.0.12 6.0.11 4.6.0.1 5.0 5.0.1 5.0.2 5.0.3 5.0.3.1 5.0.3.2 5.1 5.1.1 5.2 5.2.1 5.2.2 5.3 All 153 releases
mainwp / modules / logs / classes / class-log-query.php

class-log-query.php in MainWP Dashboard: Self-hosted WordPress Management for Agencies 5.1, at modules/logs/classes/class-log-query.php

203 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
15 * Class - Log_Query
16 */
17 class Log_Query {
18 /**
19 * Hold the number of records found
20 *
21 * @var int
22 */
23 public $found_records = 0;
24
25 /**
26 * Query records
27 *
28 * @param array $args Arguments to filter the records by.
29 *
30 * @return array Logs Records
31 */
32 public function query( $args ) { //phpcs:ignore -- NOSONAR - complex method.
33 global $wpdb;
34
35 $join = '';
36 $where = '';
37
38 if ( ! empty( $args['search'] ) ) {
39 $field = ! empty( $args['search_field'] ) ? $args['search_field'] : 'item';
40
41 // Sanitize field.
42 $allowed_fields = array( 'log_id', 'site_id', 'user_id', 'created', 'item', 'connector', 'context', 'action' );
43 if ( in_array( $field, $allowed_fields, true ) ) {
44 $where .= $wpdb->prepare( " AND lg.{$field} LIKE %s", "%{$args['search']}%" ); // @codingStandardsIgnoreLine can't prepare column name
45 }
46 }
47
48 $array_groups_ids = array();
49 $array_clients_ids = array();
50 $array_users_ids = array();
51
52 if ( ! empty( $args['groups_ids'] ) && is_array( $args['groups_ids'] ) ) {
53 $array_groups_ids = MainWP_Utility::array_numeric_filter( $args['groups_ids'] );
54 if ( ! empty( $array_groups_ids ) ) {
55 $groups_sites = MainWP_DB_Client::instance()->get_websites_by_group_ids( $array_groups_ids );
56 $array_website_ids = array();
57 if ( $groups_sites ) {
58 foreach ( $groups_sites as $website ) {
59 $array_website_ids[] = $website->id;
60 }
61 }
62 unset( $groups_sites );
63 if ( ! empty( $array_website_ids ) ) {
64 $where .= " AND lg.site_id IN ('" . implode( "','", $array_website_ids ) . "') ";
65 } else {
66 $where .= ' AND false ';
67 }
68 }
69 }
70
71 if ( ! empty( $args['client_ids'] ) && is_array( $args['client_ids'] ) ) {
72 $array_clients_ids = MainWP_Utility::array_numeric_filter( $args['client_ids'] );
73 if ( ! empty( $array_clients_ids ) ) {
74 $client_sites = MainWP_DB_Client::instance()->get_websites_by_client_ids( $array_clients_ids );
75 $array_website_ids = array();
76 if ( $client_sites ) {
77 foreach ( $client_sites as $website ) {
78 $array_website_ids[] = $website->id;
79 }
80 }
81 unset( $client_sites );
82 if ( ! empty( $array_website_ids ) ) {
83 $where .= " AND lg.site_id IN ('" . implode("','",$array_website_ids) . "') "; // phpcs:ignore -- ok.
84 } else {
85 $where .= ' AND false ';
86 }
87 }
88 }
89
90 if ( ! empty( $args['user_ids'] ) && is_array( $args['user_ids'] ) ) {
91 $array_users_ids = MainWP_Utility::array_numeric_filter( $args['user_ids'] );
92 if ( ! empty( $array_users_ids ) ) {
93 $where .= " AND lg.user_id IN ('" . implode("','",$array_users_ids) . "') "; // phpcs:ignore -- ok.
94 }
95 }
96
97 if ( ! empty( $args['timestart'] ) && ! empty( $args['timestop'] ) ) {
98 $where .= $wpdb->prepare( ' AND `lg`.`created` >= %d AND `lg`.`created` <= %d', $args['timestart'], $args['timestop'] );
99 }
100
101 /**
102 * PARSE PAGINATION PARAMS
103 */
104 $limits = '';
105 $start = absint( $args['start'] );
106 $per_page = absint( $args['records_per_page'] );
107
108 if ( $per_page >= 0 ) {
109 $limits = "LIMIT {$start}, {$per_page}";
110 }
111
112 $limits_recent_count = '';
113
114 // list recent events.
115 if ( ! empty( $args['recent_number'] ) ) {
116 $limits_recent_count = ' LIMIT ' . intval( $args['recent_number'] );
117 }
118
119 /**
120 * PARSE ORDER PARAMS
121 */
122 $orderable = array( 'site_id', 'name', 'url', 'user_id', 'item', 'created', 'connector', 'context', 'action', 'duration', 'state' );
123
124 // Default to sorting by record ID.
125 $orderby = 'lg.log_id';
126
127 if ( in_array( $args['orderby'], $orderable, true ) ) {
128 if ( in_array( $args['orderby'], array( 'name', 'url' ) ) ) {
129 $orderby = sprintf( '%s.%s', 'meta_view', $args['orderby'] );
130 } else {
131 $orderby = sprintf( '%s.%s', 'lg', $args['orderby'] );
132 }
133 }
134
135 // Show the recent records first by default.
136 $order = 'DESC';
137 if ( 'ASC' === strtoupper( $args['order'] ) ) {
138 $order = 'ASC';
139 }
140
141 $orderby = sprintf( 'ORDER BY %s %s', $orderby, $order );
142
143 /**
144 * PARSE FIELDS PARAMETER
145 */
146 $selects = array();
147 $selects[] = 'lg.*';
148 $selects[] = 'meta_view.*';
149 $select = implode( ', ', $selects );
150
151 $join = ' LEFT JOIN ' . $this->get_log_meta_view() . ' meta_view ON lg.log_id = meta_view.view_log_id ';
152
153 /**
154 * BUILD THE FINAL QUERY
155 */
156 $query = "SELECT {$select}
157 FROM $wpdb->mainwp_tbl_logs as lg
158 {$join}
159 WHERE `lg`.`connector` != 'compact' {$where}
160 {$orderby}
161 {$limits}";
162
163 // Build result count query.
164 $count_query = "SELECT COUNT(*) as found
165 FROM $wpdb->mainwp_tbl_logs as lg
166 {$join}
167 WHERE `lg`.`connector` != 'compact' {$where}
168 {$limits_recent_count}";
169
170 //phpcs:ignore Squiz.PHP.CommentedOutCode.Found
171 // error_log( print_r( $args, true ) );//.
172
173 //phpcs:ignore Squiz.PHP.CommentedOutCode.Found
174 // error_log( $query );//.
175
176 //phpcs:ignore Squiz.PHP.CommentedOutCode.Found
177 // error_log( $count_query );//.
178
179 /**
180 * QUERY THE DATABASE FOR RESULTS
181 */
182 return array(
183 'items' => $wpdb->get_results( $query ), // phpcs:ignore -- ok.
184 'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
185 );
186 }
187
188 /**
189 * Get logs meta database table view.
190 *
191 * @return string logs meta view.
192 */
193 public function get_log_meta_view() {
194 global $wpdb;
195 $view = '(SELECT intlog.log_id AS view_log_id ';
196 $view .= ',(SELECT site_name.meta_value FROM ' . $wpdb->mainwp_tbl_logs_meta . ' site_name WHERE site_name.meta_log_id = intlog.log_id AND site_name.meta_key = "site_name" LIMIT 1) AS log_site_name,
197 (SELECT siteurl.meta_value FROM ' . $wpdb->mainwp_tbl_logs_meta . ' siteurl WHERE siteurl.meta_log_id = intlog.log_id AND siteurl.meta_key = "siteurl" LIMIT 1) AS url,';
198 $view .= '(SELECT extra_info.meta_value FROM ' . $wpdb->mainwp_tbl_logs_meta . ' extra_info WHERE extra_info.meta_log_id = intlog.log_id AND extra_info.meta_key = "extra_info" LIMIT 1) AS extra_info ';
199 $view .= ' FROM ' . $wpdb->mainwp_tbl_logs . ' intlog)';
200 return $view;
201 }
202 }
203