class-ajax.php
3 months ago
class-bulk-edit.php
3 months ago
class-create-modal.php
3 months ago
class-edit-modal.php
2 months ago
class-list-table.php
1 day ago
class-quick-edit.php
2 months ago
class-ajax.php
101 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Ajax functions. |
| 4 | * |
| 5 | * @since 2.0.14 |
| 6 | * @package AdvancedAds |
| 7 | * @author Advanced Ads <info@wpadvancedads.com> |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin\Placements; |
| 11 | |
| 12 | use AdvancedAds\Utilities\Conditional; |
| 13 | use AdvancedAds\Framework\Utilities\Params; |
| 14 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 15 | |
| 16 | defined( 'ABSPATH' ) || exit; |
| 17 | |
| 18 | /** |
| 19 | * Ajax functions for placements. |
| 20 | */ |
| 21 | class Ajax implements Integration_Interface { |
| 22 | |
| 23 | /** |
| 24 | * Hook into WordPress. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function hooks(): void { |
| 29 | add_action( 'wp_ajax_advads_placements_allowed_ads', [ $this, 'get_allowed_items' ] ); |
| 30 | add_action( 'wp_ajax_advads_placement_update_item', [ $this, 'placement_update_item' ] ); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Get allowed ads per placement. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public function get_allowed_items(): void { |
| 39 | check_ajax_referer( 'advads-create-new-placement', 'security' ); |
| 40 | $type = sanitize_text_field( Params::post( 'placement_type' ) ); |
| 41 | |
| 42 | wp_send_json_success( |
| 43 | [ |
| 44 | 'items' => wp_advads_get_placement_type( $type )->get_allowed_items(), |
| 45 | ] |
| 46 | ); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Update the item for the placement. |
| 51 | * |
| 52 | * @return void |
| 53 | */ |
| 54 | public function placement_update_item(): void { |
| 55 | check_ajax_referer( 'placement-update-item', 'security' ); |
| 56 | |
| 57 | if ( ! Conditional::user_can( 'advanced_ads_manage_placements' ) ) { |
| 58 | wp_send_json_error( |
| 59 | [ |
| 60 | 'message' => __( 'User don\'t have permission to update the placement.', 'advanced-ads' ), |
| 61 | ], |
| 62 | 400 |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | $placement = wp_advads_get_placement( Params::post( 'placement_id', false, FILTER_VALIDATE_INT ) ); |
| 67 | $new_item = sanitize_text_field( Params::post( 'item_id' ) ); |
| 68 | $new_item_type = 0 === strpos( $new_item, 'ad' ) ? 'ad_' : 'group_'; |
| 69 | |
| 70 | try { |
| 71 | if ( empty( $new_item ) ) { |
| 72 | $placement->remove_item(); |
| 73 | wp_send_json_success( |
| 74 | [ |
| 75 | 'edit_href' => '#', |
| 76 | 'placement_id' => $placement->get_id(), |
| 77 | 'item_id' => '', |
| 78 | ] |
| 79 | ); |
| 80 | } |
| 81 | |
| 82 | $new_item = $placement->update_item( $new_item ); |
| 83 | wp_send_json_success( |
| 84 | [ |
| 85 | 'edit_href' => $new_item->get_edit_link(), |
| 86 | 'placement_id' => $placement->get_id(), |
| 87 | 'item_id' => $new_item_type . $new_item->get_id(), |
| 88 | ] |
| 89 | ); |
| 90 | } catch ( \RuntimeException $e ) { |
| 91 | wp_send_json_error( |
| 92 | [ |
| 93 | 'message' => $e->getMessage(), |
| 94 | 'item_id' => $placement->get_item_object() ? $placement->get_item_object()->get_id() : 0, |
| 95 | ], |
| 96 | 400 |
| 97 | ); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 |