PluginProbe ʕ •ᴥ•ʔ
Advanced Ads – Ad Manager & AdSense / 2.0.25
Advanced Ads – Ad Manager & AdSense v2.0.25
2.0.25 2.0.24 2.0.23 2.0.22 2.0.21 1.38.0 1.39.0 1.39.1 1.39.2 1.39.3 1.39.4 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.40.0 1.40.1 1.40.2 1.41.0 1.42.0 1.42.1 1.42.2 1.43.0 1.44.0 1.44.1 1.45.0 1.45.1 1.46.0 1.47.0 1.47.1 1.47.2 1.47.3 1.47.4 1.47.5 1.48.0 1.48.1 1.49.0 1.5.0 1.5.0.1 1.5.1 1.5.2 1.5.2.1 1.5.4 1.5.4.1 1.5.5 1.50.0 1.51.0 1.51.1 1.51.2 1.51.3 1.52.0 1.52.1 1.52.2 1.52.3 1.52.4 1.53.0 1.53.1 1.53.2 1.54.0 1.54.1 1.55.0 1.56.0 1.56.1 1.56.2 1.56.3 1.56.4 1.6 1.6.1 1.6.10 1.6.10.1 1.6.10.2 1.6.11 1.6.11.1 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.17.1 1.6.17.2 1.6.2 1.6.2.1 1.6.3 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.6.1 1.6.7 1.6.7.1 1.6.8 1.6.8.1 1.6.8.2 1.6.8.3 1.6.9 1.6.9.1 1.6.9.2 1.6.9.3 1.6.9.4 1.7 1.7.0.1 1.7.0.2 1.7.0.3 1.7.1 1.7.1.1 1.7.1.2 1.7.1.3 1.7.1.4 1.7.1.5 1.7.10 trunk 1.7.11 1.0.1 1.7.12 1.0.2 1.7.13 1.0.3 1.7.14 1.1.0 1.7.15 1.1.1 1.7.16 1.1.2 1.7.17 1.1.3 1.7.18 1.10 1.7.19 1.10.1 1.7.2 1.10.10 1.7.2.1 1.10.11 1.7.20 1.10.12 1.7.21 1.10.2 1.7.22 1.10.3 1.7.23 1.10.4 1.7.24 1.10.5 1.7.25 1.10.6 1.7.3 1.10.7 1.7.4 1.10.8 1.7.4.1 1.10.9 1.7.4.2 1.11 1.7.4.3 1.11.1 1.7.4.4 1.11.2 1.7.4.5 1.12 1.7.5 1.13 1.7.5.1 1.13.1 1.7.6 1.13.2 1.7.7 1.13.3 1.7.8 1.13.4 1.7.9 1.13.5 1.7.9.1 1.13.6 1.7.9.2 1.13.7 1.7.9.3 1.13.8 1.8 1.14 1.8.1 1.14.1 1.8.10 1.14.10 1.8.11 1.14.11 1.8.12 1.14.2 1.8.13 1.14.3 1.8.14 1.14.4 1.8.15 1.14.5 1.8.16 1.14.6 1.8.17 1.14.7 1.8.18 1.14.8 1.8.19 1.14.9 1.8.2 1.15 1.8.20 1.16 1.8.21 1.16.1 1.8.22 1.17 1.8.23 1.17.1 1.8.24 1.17.10 1.8.25 1.17.10-rc.1 1.8.26 1.17.11 1.8.27 1.17.12 1.8.28 1.17.12-rc.1 1.8.29 1.17.2 1.8.3 1.17.3 1.8.30 1.17.4 1.8.4 1.17.5 1.8.5 1.17.6 1.8.6 1.17.7 1.8.7 1.17.8 1.8.8 1.17.9 1.8.9 1.17.9-beta.1 1.9 1.18.0 2.0.0 1.19.0 2.0.1 1.19.1 2.0.10 1.2 2.0.11 1.2.1 2.0.12 1.2.2 2.0.13 1.2.3 2.0.14 1.2.4 2.0.15 1.2.5 2.0.16 1.2.6 2.0.17 1.2.7 2.0.18 1.20.0 2.0.19 1.20.0-rc.1 2.0.2 1.20.0-rc.2 2.0.20 1.20.1 2.0.3 1.20.2 2.0.4 1.20.3 2.0.5 1.21.0 2.0.6 1.21.1 2.0.7 1.22.0 2.0.8 1.22.1 2.0.9 1.22.2 1.23.0 1.23.1 1.23.2 1.24.0 1.24.1 1.24.2 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.29.0 1.29.1 1.3 1.3.1 1.3.10 1.3.11 1.3.12 1.3.13 1.3.14 1.3.15 1.3.16 1.3.17 1.3.18 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.30.0 1.30.1 1.30.2 1.30.2-rc.1 1.30.3 1.30.4 1.30.4-rc.1 1.30.5 1.31.0 1.31.1 1.32.0 1.32.0-rc.1 1.33.0 1.33.1 1.33.2 1.34.0 1.35.0 1.35.1 1.36.0 1.36.1 1.36.2 1.36.3 1.37.0 1.37.1 1.37.2
advanced-ads / includes / admin / ads / class-quick-bulk-edit.php
advanced-ads / includes / admin / ads Last commit date
class-ajax.php 3 months ago class-list-table.php 1 week ago class-quick-bulk-edit.php 3 months ago
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