PluginProbe ʕ •ᴥ•ʔ
Advanced Database Cleaner – Optimize & Clean Database to Speed Up Site Performance / 4.2.0
Advanced Database Cleaner – Optimize & Clean Database to Speed Up Site Performance v4.2.0
4.2.0 trunk 1.0.0 1.1.0 1.1.1 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.5 1.3.6 1.3.7 2.0.0 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.1.0 4.1.1
advanced-database-cleaner / includes / endpoints / class-adbc-general-cleanup-endpoints.php
advanced-database-cleaner / includes / endpoints Last commit date
class-adbc-automation-endpoints.php 3 months ago class-adbc-common-endpoints.php 4 months ago class-adbc-cron-jobs-endpoints.php 3 months ago class-adbc-general-cleanup-endpoints.php 3 months ago class-adbc-info-endpoints.php 3 months ago class-adbc-logs-endpoints.php 3 months ago class-adbc-options-endpoints.php 3 months ago class-adbc-post-types-endpoints.php 3 months ago class-adbc-posts-meta-endpoints.php 3 months ago class-adbc-settings-endpoints.php 3 months ago class-adbc-tables-endpoints.php 3 months ago class-adbc-transients-endpoints.php 3 months ago class-adbc-users-meta-endpoints.php 3 months ago
class-adbc-general-cleanup-endpoints.php
310 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 /**
8 * General Cleanup Endpoints
9 *
10 * This class provides the endpoints for general cleanup operations in WordPress.
11 */
12 class ADBC_General_Cleanup_Endpoints {
13
14 /**
15 * Retrieves general cleanup data based on the items type if provided, otherwise returns all general cleanup data.
16 *
17 * Accepts:
18 * - Empty array [] to get all items
19 * - Array of item types ['revisions', 'auto_drafts'] to get only those items
20 * - String item type 'revisions' to get a single item (for backward compatibility)
21 *
22 * @param WP_REST_Request $request The request object containing the items type if specified.
23 *
24 * @return WP_REST_Response The response containing the general cleanup data.
25 */
26 public static function get_general_data( WP_REST_Request $request ) {
27
28 try {
29
30 $items_type_param = $request->get_param( 'itemsType' );
31
32 // Handle empty array - return all items
33 if ( is_array( $items_type_param ) && empty( $items_type_param ) ) {
34 return ADBC_Rest::success( '', ADBC_General_Cleanup::get_general_data() );
35 }
36
37 // Handle array of item types
38 if ( is_array( $items_type_param ) && ! empty( $items_type_param ) ) {
39 $validated_items_types = ADBC_Common_Validator::sanitize_items_types( $items_type_param );
40
41 if ( empty( $validated_items_types ) ) {
42 return ADBC_Rest::error( 'invalid items types.', ADBC_Rest::BAD_REQUEST );
43 }
44
45 return ADBC_Rest::success( '', ADBC_General_Cleanup::get_general_data( $validated_items_types ) );
46 }
47
48 // Handle string item type (backward compatibility and refresh after purge)
49 $items_type = ADBC_Common_Validator::sanitize_items_type( $items_type_param );
50
51 if ( $items_type === '' ) {
52 return ADBC_Rest::success( '', ADBC_General_Cleanup::get_general_data() );
53 }
54
55 return ADBC_Rest::success( '', ADBC_General_Cleanup::get_general_data( $items_type ) );
56
57 } catch (Throwable $e) {
58 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
59 }
60
61 }
62
63 /**
64 * Retrieves a list of items based on the provided filters.
65 *
66 * @param WP_REST_Request $request The request object containing the filters.
67 *
68 * @return WP_REST_Response The response containing the list of items.
69 */
70 public static function get_items( WP_REST_Request $request ) {
71
72 try {
73
74 $args = ADBC_Common_Validator::sanitize_filters( $request );
75
76 if ( $args['items_type'] === '' ) {
77 return ADBC_Rest::error( 'invalid items type.', ADBC_Rest::BAD_REQUEST );
78 }
79
80 $list = ADBC_General_Cleanup::get_items( $args['items_type'], $args );
81
82 return ADBC_Rest::success( '', $list );
83
84 } catch (Throwable $e) {
85 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
86 }
87
88 }
89
90 /**
91 * Deletes items based on the provided items type and selected items.
92 *
93 * @param WP_REST_Request $request The request object containing the items type and selected items.
94 *
95 * @return WP_REST_Response The response indicating the success or failure of the deletion.
96 */
97 public static function delete_items( WP_REST_Request $request ) {
98
99 try {
100
101 $items_type = ADBC_Common_Validator::sanitize_items_type( $request->get_param( 'itemsType' ) );
102
103 if ( $items_type === '' ) {
104 return ADBC_Rest::error( 'invalid items type.', ADBC_Rest::BAD_REQUEST );
105 }
106
107 $action_type = "delete_$items_type";
108
109 $validated_selected_items = ADBC_Common_Validator::validate_endpoint_action_data( $action_type, $items_type, $request );
110
111 if ( ! is_array( $validated_selected_items ) )
112 return ADBC_Rest::error( $validated_selected_items, ADBC_Rest::BAD_REQUEST );
113
114 $deleted = ADBC_General_Cleanup::delete_items( $items_type, $validated_selected_items );
115
116 return ADBC_Rest::success( '', [ 'deleted' => $deleted ] );
117
118 } catch (Throwable $e) {
119 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
120 }
121
122 }
123
124 /**
125 * Purges items based on the provided items type.
126 *
127 * @param WP_REST_Request $request The request object containing the items type.
128 *
129 * @return WP_REST_Response The response indicating the success or failure of the purge operation.
130 */
131 public static function purge_items( WP_REST_Request $request ) {
132
133 try {
134
135 $items_type = ADBC_Common_Validator::sanitize_items_type( $request->get_param( 'itemsType' ) );
136
137 if ( $items_type === '' ) {
138 return ADBC_Rest::error( 'invalid items type.', ADBC_Rest::BAD_REQUEST );
139 }
140
141 $purged = ADBC_General_Cleanup::purge_items( $items_type );
142
143 return ADBC_Rest::success( '', [ 'purged' => $purged ] );
144
145 } catch (Throwable $e) {
146 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
147 }
148
149 }
150
151 /**
152 * Sets the "keep last" configuration for general cleanup.
153 *
154 * @param WP_REST_Request $request The request object containing the keep last configuration.
155 *
156 * @return WP_REST_Response The response indicating the success or failure of the operation.
157 */
158 public static function set_keep_last( WP_REST_Request $request ) {
159
160 try {
161
162 $keep_last = $request->get_param( 'keepLast' );
163
164 if ( ADBC_VERSION_TYPE === 'FREE' && is_array( $keep_last ) ) {
165 foreach ( $keep_last as $value ) {
166 if ( ( $value['type'] ?? null ) === 'items' ) {
167 return ADBC_Rest::error(
168 __( 'Cannot use retention by items in free version, please upgrade to premium.' ),
169 ADBC_Rest::BAD_REQUEST,
170 0,
171 [
172 "message_links" => [
173 [
174 "text" => __( 'Upgrade Now', 'advanced-database-cleaner' ),
175 "url" => "https://sigmaplugin.com/downloads/wordpress-advanced-database-cleaner/?utm_source=adbc_notification_msg",
176 "target" => "_blank",
177 ]
178 ]
179 ]
180 );
181 }
182 }
183 }
184
185 if ( ! is_array( $keep_last ) || ! ADBC_Settings_Validator::is_keep_last_valid( 'keep_last', $keep_last ) ) {
186 return ADBC_Rest::error( 'invalid keep last.', ADBC_Rest::BAD_REQUEST );
187 }
188
189 $updated = ADBC_General_Cleanup::set_keep_last( $keep_last );
190
191 return ADBC_Rest::success( '', $updated );
192
193 } catch (Throwable $e) {
194 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
195 }
196
197 }
198
199 /**
200 * Retrieves the "keep last" configuration for general cleanup.
201 *
202 * @return WP_REST_Response The response containing the keep last configuration.
203 */
204 public static function get_keep_last() {
205
206 try {
207
208 $keep_last = ADBC_General_Cleanup::get_keep_last();
209
210 return ADBC_Rest::success( '', $keep_last );
211
212 } catch (Throwable $e) {
213 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
214 }
215
216 }
217
218 /**
219 * Deletes the keep_last configuration for the specified items types.
220 *
221 * @param WP_REST_Request $request The request object containing the items types.
222 *
223 * @return WP_REST_Response The response indicating the success or failure of the deletion operation.
224 */
225 public static function delete_keep_last( WP_REST_Request $request ) {
226
227 try {
228
229 $items_types = $request->get_param( 'itemsTypes' );
230
231 if ( ! is_array( $items_types ) || empty( $items_types ) ) {
232 return ADBC_Rest::error( 'invalid items types.', ADBC_Rest::BAD_REQUEST );
233 }
234
235 foreach ( $items_types as $items_type ) {
236 if ( ADBC_Common_Validator::sanitize_items_type( $items_type ) === '' ) {
237 // If any of the items types is invalid, return an error.
238 return ADBC_Rest::error( 'invalid items type.', ADBC_Rest::BAD_REQUEST );
239 }
240 }
241
242 $deleted = ADBC_General_Cleanup::delete_keep_last( $items_types );
243
244 return ADBC_Rest::success( '', $deleted );
245
246 } catch (Throwable $e) {
247 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
248 }
249
250 }
251
252 /**
253 * Activate auto count for specified items types.
254 *
255 * @param WP_REST_Request $request The request object containing the items types.
256 *
257 * @return WP_REST_Response The response indicating the success or failure of the activation operation.
258 */
259 public static function activate_auto_count( WP_REST_Request $request ) {
260
261 try {
262
263 $items_types = $request->get_param( 'itemsTypes' );
264
265 $validated_items_types = ADBC_Common_Validator::sanitize_items_types( $items_types );
266
267 if ( empty( $validated_items_types ) ) {
268 return ADBC_Rest::error( 'invalid items types.', ADBC_Rest::BAD_REQUEST );
269 }
270
271 $activated = ADBC_General_Cleanup::activate_auto_count( $validated_items_types );
272
273 return ADBC_Rest::success( '', $activated );
274
275 } catch (Throwable $e) {
276 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
277 }
278
279 }
280
281 /**
282 * Deactivate auto count for specified items types.
283 *
284 * @param WP_REST_Request $request The request object containing the items types.
285 *
286 * @return WP_REST_Response The response indicating the success or failure of the deactivation operation.
287 */
288 public static function deactivate_auto_count( WP_REST_Request $request ) {
289
290 try {
291
292 $items_types = $request->get_param( 'itemsTypes' );
293
294 $validated_items_types = ADBC_Common_Validator::sanitize_items_types( $items_types );
295
296 if ( empty( $validated_items_types ) ) {
297 return ADBC_Rest::error( 'invalid items types.', ADBC_Rest::BAD_REQUEST );
298 }
299
300 $deactivated = ADBC_General_Cleanup::deactivate_auto_count( $validated_items_types );
301
302 return ADBC_Rest::success( '', $deactivated );
303
304 } catch (Throwable $e) {
305 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
306 }
307
308 }
309
310 }