PluginProbe
MainWP Dashboard: Self-hosted WordPress Management for Agencies / 6.2.2
MainWP Dashboard: Self-hosted WordPress Management for Agencies v6.2.2
6.2.2 6.2.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 All 155 releases
mainwp / class / class-mainwp-db-site-actions.php

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

296 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * MainWP Database Site Actions
4 *
5 * This file handles all interactions with the Site Actions DB.
6 *
7 * @package MainWP/Dashboard
8 */
9
10 namespace MainWP\Dashboard;
11
12 use MainWP\Dashboard\Module\Log\Log_Query;
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Class MainWP_DB_Site_Actions
21 *
22 * @package MainWP\Dashboard
23 */
24 class MainWP_DB_Site_Actions extends MainWP_DB { // phpcs:ignore Generic.Classes.OpeningBraceSameLine.ContentAfterBrace -- NOSONAR.
25
26 // phpcs:disable WordPress.DB.RestrictedFunctions, WordPress.DB.PreparedSQL.NotPrepared -- unprepared SQL ok, accessing the database directly to custom database functions.
27
28 /**
29 * Private static variable to hold the single instance of the class.
30 *
31 * @static
32 *
33 * @var mixed Default null
34 */
35 private static $instance = null;
36
37 /**
38 * Method instance()
39 *
40 * Create public static instance.
41 *
42 * @static
43 * @return MainWP_DB_Site_Actions
44 */
45 public static function instance() {
46 if ( null === static::$instance ) {
47 static::$instance = new self();
48 }
49 return static::$instance;
50 }
51
52 /**
53 * Constructor.
54 *
55 * Run each time the class is called.
56 */
57 public function __construct() {
58 parent::__construct();
59 }
60
61 /**
62 * Method hook_db_install_tables()
63 *
64 * Get the query to install db tables.
65 *
66 * @param array $sql input filter.
67 * @param string $currentVersion Current db Version.
68 * @param string $charset_collate charset collate.
69 *
70 * @return array $sql queries.
71 */
72 public function hook_db_install_tables( $sql, $currentVersion, $charset_collate ) {
73 $tbl = 'CREATE TABLE ' . $this->table_name( 'wp_actions' ) . ' (
74 action_id int(11) NOT NULL auto_increment,
75 wpid int(11) NOT NULL,
76 object_id varchar(20) NOT NULL,
77 context varchar(20) NOT NULL,
78 action varchar(100) NOT NULL,
79 action_user text NOT NULL DEFAULT "",
80 created int(11) NOT NULL DEFAULT 0,
81 meta_data text NOT NULL DEFAULT "",
82 dismiss tinyint(1) NOT NULL DEFAULT 0,
83 summary varchar(255) NOT NULL default ""';
84 if ( empty( $currentVersion ) || version_compare( $currentVersion, '8.89', '<=' ) ) {
85 $tbl .= ',
86 PRIMARY KEY (action_id)';
87 }
88 $tbl .= ') ' . $charset_collate;
89 $sql[] = $tbl;
90
91 return $sql;
92 }
93
94
95 /**
96 * Method hook_delete_site()
97 *
98 * Installs the new DB.
99 *
100 * @param mixed $site site object.
101 *
102 * @return bool result.
103 */
104 public function hook_delete_site( $site ) {
105 if ( empty( $site ) ) {
106 return false;
107 }
108 return $this->delete_action_by( 'wpid', $site->id );
109 }
110
111 /**
112 * Method update_action_by_id.
113 *
114 * Create or update action.
115 *
116 * @param int $action_id action id.
117 * @param array $data action data.
118 *
119 * @return bool
120 */
121 public function update_action_by_id( $action_id, $data ) {
122 if ( empty( $action_id ) ) {
123 return false;
124 }
125 return $this->wpdb->update( $this->table_name( 'wp_actions' ), $data, array( 'action_id' => intval( $action_id ) ) );
126 }
127
128 /**
129 * Method delete_action_by.
130 *
131 * Delete action by.
132 *
133 * @param string $by By what.
134 * @param mixed $value By value.
135 * @return bool Results.
136 */
137 public function delete_action_by( $by = 'action_id', $value = null ) {
138 if ( empty( $value ) ) {
139 return false;
140 }
141 $table_actions = esc_sql( $this->table_name( 'wp_actions' ) );
142 if ( ! $this->wpdb->get_var( 'SHOW TABLES LIKE ' . $this->wpdb->prepare( '%s', $this->table_name( 'wp_actions' ) ) ) ) {
143 return false;
144 }
145 if ( 'action_id' === $by ) {
146 if ( $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$table_actions} WHERE action_id=%d ", $value ) ) ) {
147 return true;
148 }
149 } elseif ( 'wpid' === $by ) {
150 if ( $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$table_actions} WHERE wpid=%d ", $value ) ) ) {
151 return true;
152 }
153 }
154 return false;
155 }
156
157 /**
158 * Method delete_all_actions.
159 *
160 * Deletes all actions.
161 *
162 * @return bool Results.
163 */
164 public function delete_all_actions() {
165 $table_actions = esc_sql( $this->table_name( 'wp_actions' ) );
166 return $this->wpdb->query( $this->wpdb->prepare( "DELETE FROM {$table_actions}" ) );
167 }
168
169 /**
170 * Method get_wp_actions.
171 *
172 * Get wp actions.
173 *
174 * @param bool $params params.
175 * @param mixed $site Site data.
176 *
177 * @return mixed $result result.
178 */
179 public function get_wp_actions( $params = array(), $site = false ) { //phpcs:ignore -- NOSONAR - complex.
180 return $this->get_none_mainwp_actions_log_for_rest_api( $params, $site );
181 }
182
183 /**
184 * Method get_none_mainwp_actions_log_for_rest_api.
185 *
186 * Get wp actions.
187 *
188 * @param bool $legacy_params params.
189 * @param mixed $site Site data.
190 *
191 * @return mixed $result result.
192 */
193 public function get_none_mainwp_actions_log_for_rest_api( $legacy_params = array(), $site = false ) { //phpcs:ignore -- NOSONAR - complex.
194
195 $action_id = isset( $legacy_params['action_id'] ) ? intval( $legacy_params['action_id'] ) : 0;
196 $site_id = isset( $legacy_params['wpid'] ) ? $legacy_params['wpid'] : 0;
197 $where_extra = isset( $legacy_params['where_extra'] ) ? $legacy_params['where_extra'] : ''; // compatible.
198 $dism = ! empty( $legacy_params['dismiss'] ) ? 1 : 0;
199 $check_access = isset( $legacy_params['check_access'] ) ? $legacy_params['check_access'] : true;
200 $filter_events = isset( $legacy_params['actions'] ) ? sanitize_text_field( wp_unslash( $legacy_params['actions'] ) ) : '';
201 $filter_contexts = isset( $legacy_params['contexts'] ) ? sanitize_text_field( wp_unslash( $legacy_params['contexts'] ) ) : '';
202
203 $order_by = isset( $legacy_params['order_by'] ) && ! empty( $legacy_params['order_by'] ) ? $legacy_params['order_by'] : 'created ';
204 $offset = ! empty( $legacy_params['offset'] ) ? (int) ( $legacy_params['offset'] ) : 0;
205 $per_page = ! empty( $legacy_params['rowcount'] ) ? (int) ( $legacy_params['rowcount'] ) : 0;
206 $total_count = isset( $legacy_params['total_count'] ) && $legacy_params['total_count'] ? true : false;
207
208 $limit = ! empty( $legacy_params['limit'] ) ? intval( $legacy_params['limit'] ) : false;
209
210 $order = 'DESC';
211 if ( isset( $legacy_params['order'] ) && 'ASC' === strtoupper( $legacy_params['order'] ) ) {
212 $order = 'ASC';
213 }
214
215 $compatible_args = array(
216 'start' => $offset,
217 'records_per_page' => $per_page,
218 'order' => $order,
219 'orderby' => $order_by,
220 'count_only' => $total_count ? true : false,
221 'limit' => $limit,
222 'where_extra' => $where_extra,
223 'log_id' => $action_id,
224 'wpid' => $site_id,
225 'dismiss' => $dism,
226 'check_access' => $check_access,
227 'not_count' => true,
228 );
229
230 if ( isset( $legacy_params['optimize_view'] ) && $legacy_params['optimize_view'] ) {
231 $compatible_args['optimize'] = 1;
232 }
233
234 if ( isset( $legacy_params['optimize_with_meta'] ) && $legacy_params['optimize_with_meta'] ) {
235 $compatible_args['optimize_with_meta'] = 1;
236 }
237
238 // available source values: wpadmin|dashboard|all, default value `wpadmin`.
239 if ( ! empty( $legacy_params['source'] ) ) {
240 $sources_conds = '';
241 if ( 'wpadmin' === $legacy_params['source'] ) {
242 $sources_conds = 'wp-admin-only';
243 } elseif ( 'dashboard' === $legacy_params['source'] ) {
244 $sources_conds = 'dashboard-only';
245 } elseif ( 'all' === $legacy_params['source'] ) {
246 $sources_conds = '';
247 } else {
248 $compatible_args['wpid'] = -1; // will return empty.
249 }
250
251 $compatible_args['sources_conds'] = $sources_conds;
252 } else {
253 $compatible_args['sources_conds'] = 'wp-admin-only';
254 }
255
256 if ( ! empty( $filter_events ) ) {
257 $array_events_list = explode( ',', $filter_events ); // convert to array.
258 if ( in_array( 'allevents', $array_events_list, true ) ) {
259 $filter_events = '';
260 $array_events_list = false;
261 }
262 if ( ! empty( $array_events_list ) ) {
263 $compatible_args['events'] = $array_events_list;
264 }
265 }
266
267 if ( ! empty( $filter_contexts ) ) {
268 $compatible_args['contexts'] = $filter_contexts;
269 }
270
271 $compatible_args['view'] = 'api-view';
272
273 $query_log = new Log_Query();
274
275 $results = $query_log->query( $compatible_args );
276
277 if ( is_array( $results ) && isset( $results['items'] ) ) {
278 $items = array();
279 foreach ( $results['items'] as $item ) {
280 $item->log_site_name = $site->name;
281 $item->url = $site->url;
282 $item->created = (int) ( $item->created / 1000000 ); // compatible secords value.
283 $items[] = $item;
284 }
285 return $items;
286 } elseif ( $total_count ) {
287 if ( isset( $results['count'] ) ) {
288 return $results['count'];
289 } else {
290 return 0;
291 }
292 }
293 return array();
294 }
295 }
296