class-quick-bulk-edit.php
375 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Add quick/bulk edit fields on the ad overview page |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 2.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin\Ads; |
| 11 | |
| 12 | use Exception; |
| 13 | use Advanced_Ads_Privacy; |
| 14 | use AdvancedAds\Constants; |
| 15 | use AdvancedAds\Abstracts\Ad; |
| 16 | use AdvancedAds\Utilities\WordPress; |
| 17 | use AdvancedAds\Framework\Utilities\Params; |
| 18 | |
| 19 | /** |
| 20 | * WP integration |
| 21 | */ |
| 22 | class Quick_Bulk_Edit { |
| 23 | /** |
| 24 | * Hooks into WordPress |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | public function hooks() { |
| 29 | add_action( 'quick_edit_custom_box', [ $this, 'add_quick_edit_fields' ], 10, 2 ); |
| 30 | add_action( 'bulk_edit_custom_box', [ $this, 'add_bulk_edit_fields' ], 10, 2 ); |
| 31 | add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] ); |
| 32 | add_action( 'save_post', [ $this, 'save_quick_edits' ], 100 ); |
| 33 | add_action( 'save_post', [ $this, 'save_bulk_edit' ], 100 ); |
| 34 | add_action( 'advanced-ads-ad-render-column-ad_type', [ $this, 'print_ad_json' ] ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Print ad JSON for debugging |
| 39 | * |
| 40 | * @param Ad $ad the ad being saved. |
| 41 | * |
| 42 | * @return void |
| 43 | */ |
| 44 | public function print_ad_json( $ad ): void { |
| 45 | ?> |
| 46 | <script type="text/javascript"> |
| 47 | var ad_json_<?php echo esc_attr( $ad->get_id() ); ?> = <?php echo wp_json_encode( $this->get_json_data( $ad ) ); ?>; |
| 48 | </script> |
| 49 | <?php |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Save changes made during bulk edit |
| 54 | * |
| 55 | * @return void |
| 56 | */ |
| 57 | public function save_bulk_edit() { |
| 58 | // Not bulk edit, not ads or not enough permissions. |
| 59 | if ( |
| 60 | ! wp_verify_nonce( sanitize_key( Params::get( '_wpnonce', '', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ), 'bulk-posts' ) |
| 61 | || Constants::POST_TYPE_AD !== sanitize_key( Params::get( 'post_type' ) ) |
| 62 | || ! current_user_can( 'advanced_ads_edit_ads' ) |
| 63 | ) { |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | $changes = [ 'on', 'off' ]; |
| 68 | |
| 69 | $debug_mode = Params::get( 'debug_mode' ); |
| 70 | $set_expiry = Params::get( 'expiry_date' ); |
| 71 | $ad_label = Params::get( 'ad_label', false ); |
| 72 | $ignore_privacy = Params::get( 'ignore_privacy' ); |
| 73 | |
| 74 | $has_change = in_array( $debug_mode, $changes, true ) || in_array( $set_expiry, $changes, true ) || in_array( $ignore_privacy, $changes, true ) || false !== $ad_label; |
| 75 | |
| 76 | /** |
| 77 | * Allow add-ons to confirm early abort if no change has been made and avoid iterating through an ad stack. |
| 78 | * |
| 79 | * @param bool $has_change whether some ads have been changed. |
| 80 | */ |
| 81 | $has_change = apply_filters( 'advanced-ads-bulk-edit-has-change', $has_change ); |
| 82 | |
| 83 | // No changes, bail out. |
| 84 | if ( ! $has_change ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | $expiry_date = 'on' === $set_expiry ? |
| 89 | $this->get_expiry_timestamp( 'get' ) : 0; |
| 90 | |
| 91 | $ads = array_map( |
| 92 | function ( $ad ) { |
| 93 | return wp_advads_get_ad( absint( $ad ) ); |
| 94 | }, |
| 95 | wp_unslash( Params::get( 'post', [], FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ) ) |
| 96 | ); |
| 97 | |
| 98 | foreach ( $ads as $ad ) { |
| 99 | if ( in_array( $debug_mode, $changes, true ) ) { |
| 100 | $ad->set_debugmode( 'on' === $debug_mode ); |
| 101 | } |
| 102 | |
| 103 | if ( in_array( $set_expiry, $changes, true ) ) { |
| 104 | $ad->set_prop( 'expiry_date', $expiry_date ); |
| 105 | } |
| 106 | |
| 107 | if ( false !== $ad_label ) { |
| 108 | $ad->set_prop( 'ad_label', esc_html( trim( $ad_label ) ) ); |
| 109 | } |
| 110 | |
| 111 | if ( 'on' === $ignore_privacy ) { |
| 112 | $ad->set_prop( 'privacy', [ 'ignore-consent' => 'on' ] ); |
| 113 | } elseif ( 'off' === $ignore_privacy ) { |
| 114 | $ad->unset_prop( 'privacy' ); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Allow add-on to bulk save ads. |
| 119 | * |
| 120 | * @param Ad $ad current ad being saved. |
| 121 | */ |
| 122 | $ad = apply_filters( 'advanced-ads-bulk-edit-save', $ad ); |
| 123 | |
| 124 | $ad->save(); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Save ad edited with quick edit |
| 130 | * |
| 131 | * @param int $id the ad being saved. |
| 132 | * |
| 133 | * @return void |
| 134 | */ |
| 135 | public function save_quick_edits( $id ) { |
| 136 | // Not inline edit, or no permission. |
| 137 | if ( |
| 138 | ! wp_verify_nonce( sanitize_key( Params::post( '_inline_edit' ) ), 'inlineeditnonce' ) || |
| 139 | ! current_user_can( 'advanced_ads_edit_ads' ) |
| 140 | ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | $ad = wp_advads_get_ad( $id ); |
| 145 | |
| 146 | // Not an ad. |
| 147 | if ( ! $ad ) { |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | // Render columns properly. |
| 152 | ( new List_Table() )->hooks(); |
| 153 | |
| 154 | $ad->set_prop( 'debugmode', Params::post( 'debugmode', false, FILTER_VALIDATE_BOOLEAN ) ); |
| 155 | $ad->set_prop( |
| 156 | 'expiry_date', |
| 157 | Params::post( 'enable_expiry' ) ? $this->get_expiry_timestamp() : 0 |
| 158 | ); |
| 159 | |
| 160 | if ( isset( Advanced_Ads_Privacy::get_instance()->options()['enabled'] ) ) { |
| 161 | if ( Params::post( 'ignore_privacy' ) ) { |
| 162 | $ad->set_prop( 'privacy', [ 'ignore-consent' => 'on' ] ); |
| 163 | } else { |
| 164 | $ad->unset_prop( 'privacy' ); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | $ad_label = Params::post( 'ad_label', false ); |
| 169 | if ( false !== $ad_label ) { |
| 170 | $ad->set_prop( 'ad_label', esc_html( trim( $ad_label ) ) ); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Allow add-ons to edit and ad before it is saved. |
| 175 | * |
| 176 | * @param Ad $ad the ad being saved. |
| 177 | */ |
| 178 | $ad = apply_filters( 'advanced-ads-quick-edit-save', $ad ); |
| 179 | |
| 180 | $ad->save(); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get Unix timestamp from the date time inputs values |
| 185 | * |
| 186 | * @param string $method method used for the form - `post` or `get`. |
| 187 | * |
| 188 | * @return int |
| 189 | */ |
| 190 | private function get_expiry_timestamp( $method = 'post' ) { |
| 191 | $day = absint( 'get' === $method ? Params::get( 'day' ) : Params::post( 'day' ) ); |
| 192 | $month = absint( 'get' === $method ? Params::get( 'month' ) : Params::post( 'month' ) ); |
| 193 | $year = 'get' === $method ? Params::get( 'year', 0, FILTER_VALIDATE_INT ) : Params::post( 'year', 0, FILTER_VALIDATE_INT ); |
| 194 | $hours = absint( 'get' === $method ? Params::get( 'hour' ) : Params::post( 'hour' ) ); |
| 195 | $minutes = absint( 'get' === $method ? Params::get( 'minute' ) : Params::post( 'minute' ) ); |
| 196 | |
| 197 | try { |
| 198 | $local_dt = new \DateTimeImmutable( 'now', WordPress::get_timezone() ); |
| 199 | $local_dt = $local_dt->setDate( $year, $month, $day )->setTime( $hours, $minutes ); |
| 200 | |
| 201 | return $local_dt->getTimestamp(); |
| 202 | } catch ( Exception $e ) { |
| 203 | return 0; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Enqueue scripts and print inline JS variable. |
| 209 | * |
| 210 | * @return void |
| 211 | */ |
| 212 | public function enqueue_scripts() { |
| 213 | $screen = get_current_screen(); |
| 214 | |
| 215 | if ( 'edit-advanced_ads' !== $screen->id ) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | wp_advads()->registry->enqueue_script( 'screen-ads-listing' ); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Add the bulk edit inputs |
| 224 | * |
| 225 | * @param string $column_name the current column. |
| 226 | * @param string $post_type the current post type. |
| 227 | * |
| 228 | * @return void |
| 229 | */ |
| 230 | public function add_bulk_edit_fields( $column_name, $post_type ) { |
| 231 | if ( Constants::POST_TYPE_AD !== $post_type || 'ad_type' !== $column_name ) { |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | $privacy_options = \Advanced_Ads_Privacy::get_instance()->options(); |
| 236 | include plugin_dir_path( ADVADS_FILE ) . 'views/admin/bulk-edit.php'; |
| 237 | |
| 238 | /** |
| 239 | * Allow add-ons to add more fields. |
| 240 | */ |
| 241 | do_action( 'advanced-ads-bulk-edit-fields' ); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Add the quick edit inputs |
| 246 | * |
| 247 | * @param string $column_name the current column. |
| 248 | * @param string $post_type the current post type. |
| 249 | * |
| 250 | * @return void |
| 251 | */ |
| 252 | public function add_quick_edit_fields( $column_name, $post_type ) { |
| 253 | if ( Constants::POST_TYPE_AD !== $post_type || 'ad_date' !== $column_name ) { |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | $privacy_options = \Advanced_Ads_Privacy::get_instance()->options(); |
| 258 | include plugin_dir_path( ADVADS_FILE ) . 'views/admin/quick-edit.php'; |
| 259 | |
| 260 | /** |
| 261 | * Allow add-ons to add more fields. |
| 262 | */ |
| 263 | do_action( 'advanced-ads-quick-edit-fields' ); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Print date and time inputs for the ad expiry |
| 268 | * |
| 269 | * @param int $timestamp default expiry date. |
| 270 | * @param string $prefix prefix for input names. |
| 271 | * @param bool $seconds whether to add seconds input. |
| 272 | * |
| 273 | * @return void |
| 274 | */ |
| 275 | public static function print_date_time_inputs( $timestamp = 0, $prefix = '', $seconds = false ) { |
| 276 | try { |
| 277 | $initial_date = (bool) $timestamp ? new \DateTimeImmutable( "@$timestamp", new \DateTimeZone( 'UTC' ) ) : current_datetime(); |
| 278 | } catch ( Exception $e ) { |
| 279 | $initial_date = current_datetime(); |
| 280 | } |
| 281 | |
| 282 | $current_year = (int) ( current_datetime()->format( 'Y' ) ); |
| 283 | |
| 284 | global $wp_locale; |
| 285 | ?> |
| 286 | <label> |
| 287 | <span class="screen-reader-text"><?php esc_html_e( 'Month', 'advanced-ads' ); ?></span> |
| 288 | <select name="<?php echo esc_attr( $prefix ); ?>month"> |
| 289 | <?php for ( $mo = 1; $mo < 13; $mo++ ) : ?> |
| 290 | <?php $month = zeroise( $mo, 2 ); ?> |
| 291 | <option value="<?php echo esc_attr( $month ); ?>" <?php selected( $month, $initial_date->format( 'm' ) ); ?>> |
| 292 | <?php echo esc_html( $month . '-' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $mo, 2 ) ) ); ?> |
| 293 | </option> |
| 294 | <?php endfor; ?> |
| 295 | </select> |
| 296 | </label> |
| 297 | <label> |
| 298 | <span class="screen-reader-text"><?php esc_html_e( 'Day', 'advanced-ads' ); ?></span> |
| 299 | <input type="number" name="<?php echo esc_attr( $prefix ); ?>day" min="1" max="31" value="<?php echo esc_attr( $initial_date->format( 'd' ) ); ?>"/> |
| 300 | </label>, |
| 301 | <label> |
| 302 | <span class="screen-reader-text"><?php esc_html_e( 'Year', 'advanced-ads' ); ?></span> |
| 303 | <select name="<?php echo esc_attr( $prefix ); ?>year"> |
| 304 | <?php for ( $y = $current_year; $y < $current_year + 11; $y++ ) : ?> |
| 305 | <option value="<?php echo esc_attr( $y ); ?>" <?php selected( $y, (int) $initial_date->format( 'Y' ) ); ?>><?php echo esc_html( $y ); ?></option> |
| 306 | <?php endfor; ?> |
| 307 | </select> |
| 308 | </label> |
| 309 | @ |
| 310 | <label> |
| 311 | <span class="screen-reader-text"><?php esc_html_e( 'Hour', 'advanced-ads' ); ?></span> |
| 312 | <input type="number" name="<?php echo esc_attr( $prefix ); ?>hour" min="0" max="23" value="<?php echo esc_attr( $initial_date->format( 'H' ) ); ?>"/> |
| 313 | </label>: |
| 314 | <label> |
| 315 | <span class="screen-reader-text"><?php esc_html_e( 'Minute', 'advanced-ads' ); ?></span> |
| 316 | <input type="number" name="<?php echo esc_attr( $prefix ); ?>minute" min="0" max="59" value="<?php echo esc_attr( $initial_date->format( 'i' ) ); ?>"/> |
| 317 | </label> |
| 318 | <?php if ( $seconds ) : ?> |
| 319 | : |
| 320 | <label> |
| 321 | <span class="screen-reader-text"><?php esc_html_e( 'Second', 'advanced-ads' ); ?></span> |
| 322 | <input type="number" name="<?php echo esc_attr( $prefix ); ?>second" min="0" max="59" value="<?php echo esc_attr( $initial_date->format( 's' ) ); ?>"/> |
| 323 | </label> |
| 324 | <?php endif; ?> |
| 325 | <?php $timezone = wp_timezone_string(); ?> |
| 326 | <span><?php echo esc_html( strlen( $timezone ) !== strlen( str_replace( [ '+', '-' ], '', $timezone ) ) ? "UTC$timezone" : $timezone ); ?></span> |
| 327 | <?php |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Get ad data for json output |
| 332 | * |
| 333 | * @param Ad $ad Ad instance. |
| 334 | * |
| 335 | * @return array |
| 336 | */ |
| 337 | private function get_json_data( $ad ): array { |
| 338 | $expiry = $ad->get_expiry_date(); |
| 339 | |
| 340 | if ( $expiry ) { |
| 341 | $expiry_date = array_combine( |
| 342 | [ 'year', 'month', 'day', 'hour', 'minute' ], |
| 343 | explode( '-', wp_date( 'Y-m-d-H-i', $expiry ) ) |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | $ad_data = [ |
| 348 | 'debug_mode' => $ad->is_debug_mode(), |
| 349 | 'expiry' => $expiry |
| 350 | ? [ |
| 351 | 'expires' => true, |
| 352 | 'expiry_date' => $expiry_date, |
| 353 | ] |
| 354 | : [ |
| 355 | 'expires' => false, |
| 356 | ], |
| 357 | 'ad_label' => $ad->get_prop( 'ad_label' ), |
| 358 | ]; |
| 359 | |
| 360 | if ( isset( Advanced_Ads_Privacy::get_instance()->options()['enabled'] ) ) { |
| 361 | $ad_data['ignore_privacy'] = isset( $ad->get_data()['privacy']['ignore-consent'] ); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Allow add-ons to add more ad data fields. |
| 366 | * |
| 367 | * @param array $ad_data the fields to be sent back to the browser. |
| 368 | * @param $ad Ad the ad being currently edited. |
| 369 | */ |
| 370 | $ad_data = apply_filters( 'advanced-ads-quick-edit-ad-data', $ad_data, $ad ); |
| 371 | |
| 372 | return $ad_data; |
| 373 | } |
| 374 | } |
| 375 |