clean_post_cache( $item_id ); return rest_ensure_response( $this->return_success( $generated ) ); } /** * Validate the item to be sent to generate CPCSS. * * @since 3.6 * * @param int $item_id ID for this item to be validated. * * @return true|WP_Error */ abstract protected function validate_item_for_generate( $item_id ); /** * Validate the item to be sent to Delete CPCSS. * * @since 3.6 * * @param int $item_id ID for this item to be validated. * * @return true|WP_Error */ abstract protected function validate_item_for_delete( $item_id ); /** * Get url for this item. * * @since 3.6 * * @param int $item_id ID for this item to get Url for. * * @return false|string */ abstract protected function get_url( $item_id ); /** * Get CPCSS file path to save CPCSS code into. * * @since 3.6 * * @param int $item_id ID for this item to get the path for. * @param bool $is_mobile Bool identifier for is_mobile CPCSS generation. * * @return string */ abstract protected function get_path( $item_id, $is_mobile = false ); /** * Delete Post ID CPCSS file. * * @since 3.6 * * @param WP_REST_Request $request the WP Rest Request object. * * @return WP_REST_Response */ public function delete( WP_REST_Request $request ) { $item_id = (int) $request->get_param( 'id' ); // validate item. $validated = $this->validate_item_for_delete( $item_id ); if ( is_wp_error( $validated ) ) { return rest_ensure_response( $this->return_error( $validated ) ); } if ( $this->options->get( 'async_css_mobile', 0 ) ) { $mobile_item_path = $this->get_path( $item_id, true ); $this->cpcss_service->process_delete( $mobile_item_path ); } $item_path = $this->get_path( $item_id ); $deleted = $this->cpcss_service->process_delete( $item_path ); if ( is_wp_error( $deleted ) ) { return rest_ensure_response( $this->return_error( $deleted ) ); } $this->clean_post_cache( $item_id ); return rest_ensure_response( $this->return_success( $deleted ) ); } /** * Returns the formatted array response * * @since 3.6 * * @param bool $success True for success, false otherwise. * @param string $code The code to use for the response. * @param string $message The message to send in the response. * @param int $status The status code to send for the response. * * @return array */ protected function return_array_response( $success = false, $code = '', $message = '', $status = 200 ) { return [ 'success' => $success, 'code' => $code, 'message' => $message, 'data' => [ 'status' => $status, ], ]; } /** * Convert WP_Error into array to be used in response. * * @since 3.6 * * @param WP_Error $error Error that will be converted to array. * * @return array */ protected function return_error( $error ) { $error_data = $error->get_error_data(); return $this->return_array_response( false, $error->get_error_code(), $error->get_error_message(), isset( $error_data['status'] ) ? $error_data['status'] : 400 ); } /** * Return success to be used in response. * * @since 3.6 * * @param array $data which has success parameters with two keys: code and message. * * @return array */ protected function return_success( $data ) { return $this->return_array_response( true, $data['code'], $data['message'], 200 ); } }