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-post-types-endpoints.php
188 lines
| 1 | <?php |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) |
| 5 | exit; |
| 6 | |
| 7 | /** |
| 8 | * ADBC Post Types Endpoints. |
| 9 | * |
| 10 | * This class provides the endpoints (controllers) for the post types routes. |
| 11 | */ |
| 12 | class ADBC_Post_Types_Endpoints { |
| 13 | |
| 14 | /** |
| 15 | * Get the post types list. |
| 16 | * |
| 17 | * @param WP_REST_Request $filters_request The request with the filters. |
| 18 | * @return WP_REST_Response The list of post types. |
| 19 | */ |
| 20 | public static function get_post_types_list( WP_REST_Request $filters_request ) { |
| 21 | |
| 22 | try { |
| 23 | |
| 24 | $filters = ADBC_Common_Validator::sanitize_filters( $filters_request ); |
| 25 | $rest_response = ADBC_Post_Types::get_post_types_list( $filters ); |
| 26 | |
| 27 | return $rest_response; |
| 28 | |
| 29 | } catch (Throwable $e) { |
| 30 | |
| 31 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 32 | |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * List the posts of a given post type in a given site (paginated). |
| 38 | * |
| 39 | * @param WP_REST_Request $request_data The request with the post type name, site ID, and pagination filters. |
| 40 | * @return WP_REST_Response The paginated list of posts. |
| 41 | */ |
| 42 | public static function list_posts_by_post_type( WP_REST_Request $request_data ) { |
| 43 | |
| 44 | try { |
| 45 | |
| 46 | $filters = ADBC_Common_Validator::sanitize_filters( $request_data ); |
| 47 | |
| 48 | $post_type = sanitize_text_field( $request_data->get_param( 'postTypeName' ) ); |
| 49 | $site_id = absint( $request_data->get_param( 'siteId' ) ); |
| 50 | $site_prefix = ADBC_Sites::instance()->get_prefix_from_site_id( $site_id ); |
| 51 | |
| 52 | if ( empty( $post_type ) || $site_prefix === null ) |
| 53 | return ADBC_Rest::error( 'Invalid params.', ADBC_Rest::BAD_REQUEST ); |
| 54 | |
| 55 | $data = ADBC_Post_Types::get_posts_rows( $site_id, $post_type, $filters ); |
| 56 | |
| 57 | return ADBC_Rest::success( "", $data ); |
| 58 | |
| 59 | } catch (Throwable $e) { |
| 60 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Delete all posts of the selected post types. |
| 67 | * |
| 68 | * @param WP_REST_Request $request_data The request with the post types to purge. |
| 69 | * @return WP_REST_Response The response. |
| 70 | */ |
| 71 | public static function delete_posts_by_post_type( WP_REST_Request $request_data ) { |
| 72 | |
| 73 | try { |
| 74 | |
| 75 | if ( ADBC_VERSION_TYPE === 'PREMIUM' && ADBC_Scan_Utils::is_scan_exists( 'post_types' ) ) |
| 76 | 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 ); |
| 77 | |
| 78 | $validation_answer = ADBC_Common_Validator::validate_endpoint_action_data( "delete_post_types", "post_types", $request_data ); |
| 79 | |
| 80 | if ( ! is_array( $validation_answer ) ) |
| 81 | return ADBC_Rest::error( $validation_answer, ADBC_Rest::BAD_REQUEST ); |
| 82 | |
| 83 | $cleaned_post_types = $validation_answer; |
| 84 | |
| 85 | if ( ADBC_Settings::instance()->get_setting( 'prevent_taking_action_on_wp_items' ) === '1' ) { |
| 86 | |
| 87 | $cleaned_post_types = ADBC_Hardcoded_Items::instance()->exclude_hardcoded_items_from_selected_items( $validation_answer, 'post_types', "wp" ); |
| 88 | |
| 89 | if ( ADBC_VERSION_TYPE === 'PREMIUM' ) |
| 90 | $cleaned_post_types = ADBC_Scan_Utils::exclude_r_wp_items_from_selected_items( $cleaned_post_types, 'post_types' ); |
| 91 | |
| 92 | if ( empty( $cleaned_post_types ) ) |
| 93 | return ADBC_Rest::error( |
| 94 | __( 'The selected post types could not be purged because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 95 | ADBC_Rest::BAD_REQUEST, |
| 96 | 0, |
| 97 | [ |
| 98 | "message_links" => [ |
| 99 | [ |
| 100 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 101 | "tab_id" => "settings", |
| 102 | "anchor_id" => "other_settings", |
| 103 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 104 | ] |
| 105 | ] |
| 106 | ] |
| 107 | ); |
| 108 | } |
| 109 | |
| 110 | $grouped = ADBC_Selected_Items_Validator::group_selected_items_by_site_id( $cleaned_post_types ); |
| 111 | $not_processed = ADBC_Post_Types::delete_posts( $grouped ); |
| 112 | |
| 113 | if ( count( $cleaned_post_types ) < count( $validation_answer ) ) { |
| 114 | return ADBC_Rest::success( |
| 115 | __( 'Some post types were purged successfully; others were skipped because they belong to WordPress core and are protected.', 'advanced-database-cleaner' ), |
| 116 | count( $not_processed ), |
| 117 | [ |
| 118 | "message_links" => [ |
| 119 | [ |
| 120 | "text" => __( 'Check setting', 'advanced-database-cleaner' ), |
| 121 | "tab_id" => "settings", |
| 122 | "anchor_id" => "other_settings", |
| 123 | "setting_id" => "prevent_taking_action_on_wp_items" |
| 124 | ] |
| 125 | ] |
| 126 | ] |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | return ADBC_Rest::success( "", count( $not_processed ) ); |
| 131 | |
| 132 | } catch (Throwable $e) { |
| 133 | |
| 134 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 135 | |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Edit scan results of post types. |
| 141 | * |
| 142 | * @param WP_REST_Request $request_data The request with the post types to edit. |
| 143 | * @return WP_REST_Response The response. |
| 144 | */ |
| 145 | public static function edit_scan_results_post_types( WP_REST_Request $request_data ) { |
| 146 | |
| 147 | try { |
| 148 | |
| 149 | return ADBC_Scan_Utils::edit_scan_results( $request_data, 'edit_scan_results_post_types', 'post_types' ); |
| 150 | |
| 151 | } catch (Throwable $e) { |
| 152 | |
| 153 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 154 | |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Count the total number of post types that are not scanned. |
| 160 | * |
| 161 | * @return WP_REST_Response The response. |
| 162 | */ |
| 163 | public static function count_total_not_scanned_post_types() { |
| 164 | |
| 165 | try { |
| 166 | return ADBC_Rest::success( "", ADBC_Post_Types::count_total_not_scanned_post_types() ); |
| 167 | } catch (Throwable $e) { |
| 168 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Count the total number of non-public post types with a large number of posts. |
| 174 | * |
| 175 | * @return WP_REST_Response The response. |
| 176 | */ |
| 177 | public static function count_total_large_non_public_post_types() { |
| 178 | |
| 179 | try { |
| 180 | return ADBC_Rest::success( "", ADBC_Post_Types::count_total_large_non_public_post_types() ); |
| 181 | } catch (Throwable $e) { |
| 182 | return ADBC_Rest::error_for_uncaught_exception( __METHOD__, $e ); |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | } |
| 188 |