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 week ago
class-quick-edit.php
2 months ago
class-bulk-edit.php
109 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Bulk edit for placement |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin\Placements; |
| 11 | |
| 12 | use AdvancedAds\Constants; |
| 13 | use AdvancedAds\Abstracts\Placement; |
| 14 | use AdvancedAds\Framework\Utilities\Params; |
| 15 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Placement Bulk Edit. |
| 21 | */ |
| 22 | class Bulk_Edit implements Integration_Interface { |
| 23 | |
| 24 | /** |
| 25 | * Hook into WordPress. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function hooks(): void { |
| 30 | add_action( 'bulk_edit_custom_box', [ $this, 'add_bulk_edit_fields' ], 10, 2 ); |
| 31 | add_action( 'save_post', [ $this, 'save_bulk_edit' ], 100 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Add the bulk edit inputs |
| 36 | * |
| 37 | * @param string $column_name the current column. |
| 38 | * @param string $post_type the current post type. |
| 39 | * |
| 40 | * @return void |
| 41 | */ |
| 42 | public function add_bulk_edit_fields( $column_name, $post_type ) { |
| 43 | // Early bail!! |
| 44 | if ( Constants::POST_TYPE_PLACEMENT !== $post_type || 'type' !== $column_name ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | include ADVADS_ABSPATH . 'views/admin/placements/bulk-edit.php'; |
| 49 | |
| 50 | /** |
| 51 | * Allow add-ons to add more fields. |
| 52 | */ |
| 53 | do_action( 'advanced-ads-placement-bulk-edit-fields' ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Save changes made during bulk edit |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | public function save_bulk_edit() { |
| 62 | // not placement or not enough permissions. |
| 63 | if ( Constants::POST_TYPE_PLACEMENT !== sanitize_key( Params::get( 'post_type' ) ) || ! current_user_can( 'advanced_ads_edit_ads' ) |
| 64 | ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | check_admin_referer( 'bulk-posts' ); |
| 69 | |
| 70 | $ad_label = Params::get( 'ad_label' ); |
| 71 | |
| 72 | $has_change = ! empty( $ad_label ); |
| 73 | |
| 74 | /** |
| 75 | * Filter to determine if there are changes to be saved during bulk edit. |
| 76 | * |
| 77 | * @param bool $has_change Indicates if there are changes to be saved. |
| 78 | */ |
| 79 | $has_change = apply_filters( 'advanced-ads-placement-bulk-edit-has-change', $has_change ); |
| 80 | |
| 81 | // No changes, bail out. |
| 82 | if ( ! $has_change ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $placements = array_map( |
| 87 | function ( $placement ) { |
| 88 | return wp_advads_get_placement( absint( $placement ) ); |
| 89 | }, |
| 90 | wp_unslash( Params::get( 'post', [], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ) |
| 91 | ); |
| 92 | |
| 93 | foreach ( $placements as $placement ) { |
| 94 | if ( ! empty( $ad_label ) ) { |
| 95 | $placement->set_prop( 'ad_label', $ad_label ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Allow add-on to bulk save placements. |
| 100 | * |
| 101 | * @param Placement $placement current placement being saved. |
| 102 | */ |
| 103 | $placement = apply_filters( 'advanced-ads-placement-bulk-edit-save', $placement ); |
| 104 | |
| 105 | $placement->save(); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 |