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-transients-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-transients-endpoints.php
323 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 /**
8 * ADBC Transients Endpoints.
9 *
10 * This class provides the endpoints (controllers) for the transients routes.
11 */
12 class ADBC_Transients_Endpoints {
13
14 /**
15 * Get the transients list.
16 *
17 * @param WP_REST_Request $filters_request The request with the filters.
18 * @return WP_REST_Response The list of transients.
19 */
20 public static function get_transients_list( WP_REST_Request $filters_request ) {
21
22 try {
23
24 $filters = ADBC_Common_Validator::sanitize_filters( $filters_request );
25 $rest_response = ADBC_Transients::get_transients_list( $filters );
26 return $rest_response;
27
28 } catch (Throwable $e) {
29
30 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
31
32 }
33 }
34
35 /**
36 * Edit scan results of transients.
37 *
38 * @param WP_REST_Request $request_data The request with the transients to edit.
39 * @return WP_REST_Response The response.
40 */
41 public static function edit_scan_results_transients( WP_REST_Request $request_data ) {
42
43 try {
44
45 return ADBC_Scan_Utils::edit_scan_results( $request_data, 'edit_scan_results_transients', 'transients' );
46
47 } catch (Throwable $e) {
48
49 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
50
51 }
52 }
53
54 /**
55 * Set autoload to "yes" for transients.
56 *
57 * @param WP_REST_Request $request_data The request with the transients to set autoload to "yes".
58 * @return WP_REST_Response The response.
59 */
60 public static function set_autoload_to_yes_transients( WP_REST_Request $request_data ) {
61
62 try {
63
64 $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "set_autoload_to_yes_transients", "transients", $request_data );
65
66 // If $validation_answer is not an array, it means that the validation failed and we have an error message.
67 if ( ! is_array( $validation_answer ) )
68 return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST );
69
70 $cleaned_transients = $validation_answer;
71
72 if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) {
73
74 $cleaned_transients = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'transients', "wp" );
75
76 if ( ADBC_VERSION_TYPE === 'PREMIUM' )
77 $cleaned_transients = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_transients, 'transients' );
78
79 if ( empty( $cleaned_transients ) )
80 return ADBC_Rest::error(
81 __( 'The selected transients could not have their autoload edited because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
82 ADBC_Rest::BAD_REQUEST,
83 0,
84 [
85 "message_links" => [
86 [
87 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
88 "tab_id" => "settings",
89 "anchor_id" => "other_settings",
90 "setting_id" => "prevent_taking_action_on_wp_items"
91 ]
92 ]
93 ]
94 );
95 }
96
97 $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_transients );
98 $not_processed = ADBC_Transients::set_autoload_to_yes( $grouped );
99
100 if ( count( $cleaned_transients ) < count( $validation_answer ) )
101 return ADBC_Rest::success(
102 __( 'Some transients were updated successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
103 count( $not_processed ),
104 [
105 "message_links" => [
106 [
107 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
108 "tab_id" => "settings",
109 "anchor_id" => "other_settings",
110 "setting_id" => "prevent_taking_action_on_wp_items"
111 ]
112 ]
113 ]
114 );
115
116 return ADBC_Rest::success( "", count( $not_processed ) );
117
118 } catch (Throwable $e) {
119
120 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
121
122 }
123 }
124
125 /**
126 * Set autoload to "no" for transients.
127 *
128 * @param WP_REST_Request $request_data The request with the transients to set autoload to "no".
129 * @return WP_REST_Response The response.
130 */
131 public static function set_autoload_to_no_transients( WP_REST_Request $request_data ) {
132
133 try {
134
135 $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "set_autoload_to_no_transients", "transients", $request_data );
136
137 // If $validation_answer is not an array, it means that the validation failed and we have an error message.
138 if ( ! is_array( $validation_answer ) )
139 return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST );
140
141 $cleaned_transients = $validation_answer;
142
143 if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) {
144
145 $cleaned_transients = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'transients', "wp" );
146
147 if ( ADBC_VERSION_TYPE === 'PREMIUM' )
148 $cleaned_transients = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_transients, 'transients' );
149
150 if ( empty( $cleaned_transients ) )
151 return ADBC_Rest::error(
152 __( 'The selected transients could not have their autoload edited because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
153 ADBC_Rest::BAD_REQUEST,
154 0,
155 [
156 "message_links" => [
157 [
158 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
159 "tab_id" => "settings",
160 "anchor_id" => "other_settings",
161 "setting_id" => "prevent_taking_action_on_wp_items"
162 ]
163 ]
164 ]
165 );
166 }
167
168 $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_transients );
169 $not_processed = ADBC_Transients::set_autoload_to_no( $grouped );
170
171 if ( count( $cleaned_transients ) < count( $validation_answer ) ) {
172 return ADBC_Rest::success(
173 __( 'Some transients were updated successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
174 count( $not_processed ),
175 [
176 "message_links" => [
177 [
178 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
179 "tab_id" => "settings",
180 "anchor_id" => "other_settings",
181 "setting_id" => "prevent_taking_action_on_wp_items"
182 ]
183 ]
184 ]
185 );
186 }
187
188 return ADBC_Rest::success( "", count( $not_processed ) );
189
190 } catch (Throwable $e) {
191
192 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
193
194 }
195 }
196
197 /**
198 * Delete transients.
199 *
200 * @param WP_REST_Request $request_data The request with the transients to delete.
201 * @return WP_REST_Response The response.
202 */
203 public static function delete_transients( WP_REST_Request $request_data ) {
204
205 try {
206
207 // Verify if there is a scan in progress. If there is, return an error to prevent conflicts.
208 if ( ADBC_VERSION_TYPE === 'PREMIUM' && ADBC_Scan_Utils::is_scan_exists( 'transients' ) )
209 return ADBC_Rest::error( __( 'A scan is in progress. Please wait until it finishes before performing this action.', 'advanced-database-cleaner' ), ADBC_Rest::BAD_REQUEST );
210
211 $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "delete_transients", "transients", $request_data );
212
213 // If $validation_answer is not an array, it means that the validation failed and we have an error message.
214 if ( ! is_array( $validation_answer ) )
215 return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST );
216
217 $cleaned_transients = $validation_answer;
218
219 if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) {
220
221 $cleaned_transients = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'transients', "wp" );
222
223 if ( ADBC_VERSION_TYPE === 'PREMIUM' )
224 $cleaned_transients = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_transients, 'transients' );
225
226 if ( empty( $cleaned_transients ) )
227 return ADBC_Rest::error(
228 __( 'The selected transients could not be deleted because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
229 ADBC_Rest::BAD_REQUEST,
230 0,
231 [
232 "message_links" => [
233 [
234 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
235 "tab_id" => "settings",
236 "anchor_id" => "other_settings",
237 "setting_id" => "prevent_taking_action_on_wp_items"
238 ]
239 ]
240 ]
241 );
242 }
243
244 $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_transients );
245 $not_processed = ADBC_Transients::delete_transients( $grouped );
246
247 // Delete the transients from the scan results
248 if ( ADBC_VERSION_TYPE === 'PREMIUM' ) {
249 $transients_names = array_column( $cleaned_transients, 'name' ); // Create an array containing only the transients names.
250 ADBC_Scan_Utils::update_scan_results_file_after_deletion( 'transients', $transients_names, $not_processed );
251 }
252
253 if ( count( $cleaned_transients ) < count( $validation_answer ) ) {
254 return ADBC_Rest::success(
255 __( 'Some transients were deleted successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
256 count( $not_processed ),
257 [
258 "message_links" => [
259 [
260 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
261 "tab_id" => "settings",
262 "anchor_id" => "other_settings",
263 "setting_id" => "prevent_taking_action_on_wp_items"
264 ]
265 ]
266 ]
267 );
268 }
269
270 return ADBC_Rest::success( "", count( $not_processed ) );
271
272 } catch (Throwable $e) {
273
274 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
275
276 }
277 }
278
279 /**
280 * Count the total number of big transients.
281 *
282 * @return WP_REST_Response The response.
283 */
284 public static function count_big_transients() {
285 try {
286 return ADBC_Rest::success( "", ADBC_Transients::count_big_transients() );
287 } catch (Throwable $e) {
288 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
289 }
290 }
291
292 /**
293 * Count the total number of transients that are not scanned.
294 *
295 * @return WP_REST_Response The response.
296 */
297 public static function count_total_not_scanned_transients() {
298 try {
299 return ADBC_Rest::success( "", ADBC_Transients::count_total_not_scanned_transients() );
300 } catch (Throwable $e) {
301 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
302 }
303 }
304
305 /**
306 * Count the total number of expired transients.
307 *
308 * @return WP_REST_Response The response.
309 */
310 public static function count_expired_transients() {
311 try {
312 return ADBC_Rest::success( "", ADBC_Cleanup_Type_Registry::handler( 'expired_transients' )->count()['count'] );
313 } catch (Throwable $e) {
314 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
315 }
316 }
317
318 }
319
320
321
322
323