class-groups.php
5 days ago
class-licenses.php
1 day ago
class-onboarding.php
1 year ago
class-page-quick-edit.php
1 day ago
class-placements.php
1 year ago
class-utilities.php
1 day ago
class-page-quick-edit.php
96 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rest Page Quick Edit. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Rest; |
| 11 | |
| 12 | use AdvancedAds\Constants; |
| 13 | use AdvancedAds\Utilities\Conditional; |
| 14 | use AdvancedAds\Framework\Interfaces\Routes_Interface; |
| 15 | use WP_REST_Request; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Rest Page Quick Edit. |
| 21 | */ |
| 22 | class Page_Quick_Edit implements Routes_Interface { |
| 23 | /** |
| 24 | * Register rest route for disabled ads status |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function register_routes(): void { |
| 29 | register_rest_route( |
| 30 | Constants::REST_BASE, |
| 31 | '/page_quick_edit', |
| 32 | [ |
| 33 | 'methods' => 'GET', |
| 34 | 'callback' => [ $this, 'get_disable_ads' ], |
| 35 | 'permission_callback' => [ $this, 'can_read_quick_edit_settings' ], |
| 36 | 'args' => [ |
| 37 | 'id' => [ |
| 38 | 'required' => true, |
| 39 | 'type' => 'integer', |
| 40 | 'sanitize_callback' => 'absint', |
| 41 | ], |
| 42 | 'nonce' => [ |
| 43 | 'required' => true, |
| 44 | 'type' => 'string', |
| 45 | 'sanitize_callback' => 'sanitize_text_field', |
| 46 | ], |
| 47 | ], |
| 48 | ] |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Whether the current user may read quick-edit settings for the requested post. |
| 54 | * |
| 55 | * @param WP_REST_Request $request REST request. |
| 56 | * |
| 57 | * @return bool |
| 58 | */ |
| 59 | public function can_read_quick_edit_settings( WP_REST_Request $request ): bool { |
| 60 | if ( ! Conditional::user_can( 'edit_posts' ) ) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | $post_id = absint( $request->get_param( 'id' ) ); |
| 65 | if ( $post_id <= 0 || ! current_user_can( 'edit_post', $post_id ) ) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | $post = get_post( $post_id ); |
| 70 | if ( ! $post || ! in_array( $post->post_type, [ 'post', 'page' ], true ) ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Endpoint callback |
| 79 | * |
| 80 | * @param WP_REST_Request $request the request. |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public function get_disable_ads( WP_REST_Request $request ) { |
| 85 | $nonce = sanitize_text_field( $request->get_param( 'nonce' ) ); |
| 86 | |
| 87 | if ( ! wp_verify_nonce( $nonce, 'advads-post-quick-edit' ) ) { |
| 88 | return []; |
| 89 | } |
| 90 | |
| 91 | $id = absint( $request->get_param( 'id' ) ); |
| 92 | |
| 93 | return (array) get_post_meta( $id, '_advads_ad_settings', true ); |
| 94 | } |
| 95 | } |
| 96 |