class-groups.php
1 year ago
class-onboarding.php
1 year ago
class-page-quick-edit.php
1 year ago
class-placements.php
1 year ago
class-quick-edit.php
1 year ago
class-utilities.php
1 year ago
class-quick-edit.php
95 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Rest 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 Advanced_Ads_Privacy; |
| 13 | use AdvancedAds\Constants; |
| 14 | use AdvancedAds\Utilities\Conditional; |
| 15 | use AdvancedAds\Framework\Interfaces\Routes_Interface; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Rest Quick Edit. |
| 21 | */ |
| 22 | class Quick_Edit implements Routes_Interface { |
| 23 | |
| 24 | /** |
| 25 | * Registers routes with WordPress. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function register_routes(): void { |
| 30 | register_rest_route( |
| 31 | Constants::REST_BASE, |
| 32 | '/quick_edit_data', |
| 33 | [ |
| 34 | 'methods' => 'POST', |
| 35 | 'callback' => [ $this, 'format_ad_data' ], |
| 36 | 'permission_callback' => function () { |
| 37 | return Conditional::user_can( 'advanced_ads_edit_ads' ); |
| 38 | }, |
| 39 | ] |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Return ad data for quick edit |
| 45 | * |
| 46 | * @param \WP_REST_Request $request the request. |
| 47 | * |
| 48 | * @return [] |
| 49 | */ |
| 50 | public function format_ad_data( $request ) { |
| 51 | $id = $request->get_param( 'id' ); |
| 52 | $ad = wp_advads_get_ad( $id ); |
| 53 | |
| 54 | if ( ! $ad ) { |
| 55 | return [ 'error' => __( 'Ad not found', 'advanced-ads' ) ]; |
| 56 | } |
| 57 | |
| 58 | $expiry = $ad->get_expiry_date(); |
| 59 | |
| 60 | if ( $expiry ) { |
| 61 | $expiry_date = array_combine( |
| 62 | [ 'year', 'month', 'day', 'hour', 'minutes' ], |
| 63 | explode( '-', wp_date( 'Y-m-d-H-i', $expiry ) ) |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | $ad_data = [ |
| 68 | 'debug_mode' => $ad->is_debug_mode(), |
| 69 | 'expiry' => $expiry |
| 70 | ? [ |
| 71 | 'expires' => true, |
| 72 | 'expiry_date' => $expiry_date, |
| 73 | ] |
| 74 | : [ |
| 75 | 'expires' => false, |
| 76 | ], |
| 77 | 'ad_label' => $ad->get_prop( 'ad_label' ), |
| 78 | ]; |
| 79 | |
| 80 | if ( isset( Advanced_Ads_Privacy::get_instance()->options()['enabled'] ) ) { |
| 81 | $ad_data['ignore_privacy'] = isset( $ad->get_data()['privacy']['ignore-consent'] ); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Allow add-ons to add more ad data fields. |
| 86 | * |
| 87 | * @param array $ad_data the fields to be sent back to the browser. |
| 88 | * @param $ad Ad the ad being currently edited. |
| 89 | */ |
| 90 | $ad_data = apply_filters( 'advanced-ads-quick-edit-ad-data', $ad_data, $ad ); |
| 91 | |
| 92 | return $ad_data; |
| 93 | } |
| 94 | } |
| 95 |