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-posts-meta-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-posts-meta-endpoints.php
196 lines
1 <?php
2
3 // Exit if accessed directly
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 /**
8 * ADBC Posts Meta Endpoints.
9 *
10 * This class provides the endpoints (controllers) for the posts meta routes.
11 */
12 class ADBC_Posts_Meta_Endpoints {
13
14 /**
15 * Get the posts meta list.
16 *
17 * @param WP_REST_Request $filters_request The request with the filters.
18 * @return WP_REST_Response The list of posts meta.
19 */
20 public static function get_posts_meta_list( WP_REST_Request $filters_request ) {
21
22 try {
23
24 $filters = ADBC_Common_Validator::sanitize_filters( $filters_request );
25 $rest_response = ADBC_Posts_Meta::get_posts_meta_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 posts meta.
37 *
38 * @param WP_REST_Request $request_data The request with the posts meta to edit.
39 * @return WP_REST_Response The response.
40 */
41 public static function edit_scan_results_posts_meta( WP_REST_Request $request_data ) {
42
43 try {
44
45 return ADBC_Scan_Utils::edit_scan_results( $request_data, 'edit_scan_results_posts_meta', 'posts_meta' );
46
47 } catch (Throwable $e) {
48
49 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
50
51 }
52 }
53
54 /**
55 * Delete posts meta.
56 *
57 * @param WP_REST_Request $request_data The request with the posts meta to delete.
58 * @return WP_REST_Response The response.
59 */
60 public static function delete_posts_meta( WP_REST_Request $request_data ) {
61
62 try {
63
64 // Verify if there is a scan in progress. If there is, return an error to prevent conflicts.
65 if ( ADBC_VERSION_TYPE === 'PREMIUM' && ADBC_Scan_Utils::is_scan_exists( 'posts_meta' ) )
66 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 );
67
68 $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "delete_posts_meta", "posts_meta", $request_data );
69
70 // If $validation_answer is not an array, it means that the validation failed and we have an error message.
71 if ( ! is_array( $validation_answer ) )
72 return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST );
73
74 $cleaned_posts_meta = $validation_answer;
75
76 if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) {
77
78 $cleaned_posts_meta = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'posts_meta', "wp" );
79
80 if ( ADBC_VERSION_TYPE === 'PREMIUM' )
81 $cleaned_posts_meta = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_posts_meta, 'posts_meta' );
82
83 if ( empty( $cleaned_posts_meta ) )
84 return ADBC_Rest::error(
85 __( 'The selected post meta could not be deleted because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
86 ADBC_Rest::BAD_REQUEST,
87 0,
88 [
89 "message_links" => [
90 [
91 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
92 "tab_id" => "settings",
93 "anchor_id" => "other_settings",
94 "setting_id" => "prevent_taking_action_on_wp_items"
95 ]
96 ]
97 ]
98 );
99 }
100
101 $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_posts_meta );
102 $not_processed = ADBC_Posts_Meta::delete_posts_meta( $grouped );
103
104 // Delete the posts meta from the scan results
105 if ( ADBC_VERSION_TYPE === 'PREMIUM' ) {
106 $posts_meta_names = array_column( $cleaned_posts_meta, 'name' ); // Create an array containing only the posts meta names.
107 ADBC_Scan_Utils::update_scan_results_file_after_deletion( 'posts_meta', $posts_meta_names, $not_processed );
108 }
109
110 if ( count( $cleaned_posts_meta ) < count( $validation_answer ) ) {
111 return ADBC_Rest::success(
112 __( 'Some post meta was deleted successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ),
113 count( $not_processed ),
114 [
115 "message_links" => [
116 [
117 "text" => __( 'Check setting', 'advanced-database-cleaner' ),
118 "tab_id" => "settings",
119 "anchor_id" => "other_settings",
120 "setting_id" => "prevent_taking_action_on_wp_items"
121 ]
122 ]
123 ]
124 );
125 }
126
127 return ADBC_Rest::success( "", count( $not_processed ) );
128
129 } catch (Throwable $e) {
130
131 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
132
133 }
134 }
135
136 /**
137 * Count the total number of big posts meta in all sites.
138 *
139 * @return WP_REST_Response The response.
140 */
141 public static function count_big_posts_meta() {
142
143 try {
144 return ADBC_Rest::success( "", ADBC_Posts_Meta::count_big_posts_meta() );
145 } catch (Throwable $e) {
146 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
147 }
148
149 }
150
151 /**
152 * Count the total number of posts meta that are not scanned.
153 *
154 * @return WP_REST_Response The response.
155 */
156 public static function count_total_not_scanned_posts_meta() {
157
158 try {
159 return ADBC_Rest::success( "", ADBC_Posts_Meta::count_total_not_scanned_posts_meta() );
160 } catch (Throwable $e) {
161 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
162 }
163
164 }
165
166 /**
167 * Count the total number of duplicated posts meta.
168 *
169 * @return WP_REST_Response The response.
170 */
171 public static function count_duplicated_posts_meta() {
172
173 try {
174 return ADBC_Rest::success( "", ADBC_Posts_Meta::count_duplicated_posts_meta() );
175 } catch (Throwable $e) {
176 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
177 }
178
179 }
180
181 /**
182 * Count the total number of unused posts meta.
183 *
184 * @return WP_REST_Response The response.
185 */
186 public static function count_unused_posts_meta() {
187
188 try {
189 return ADBC_Rest::success( "", ADBC_Posts_Meta::count_unused_posts_meta() );
190 } catch (Throwable $e) {
191 return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e );
192 }
193
194 }
195
196 }