PluginProbe
Advanced Ads – Ad Manager & AdSense / 2.0.21
Advanced Ads – Ad Manager & AdSense v2.0.21
2.0.26 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 All 348 releases
advanced-ads / includes / admin / ads / class-list-table.php
class-list-table.php
477 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Ad List Table.
4 *
5 * @package AdvancedAds
6 * @author Advanced Ads <info@wpadvancedads.com>
7 * @since 1.48.2
8 */
9
10 namespace AdvancedAds\Admin\Ads;
11
12 defined( 'ABSPATH' ) || exit;
13
14 use AdvancedAds\Options;
15 use AdvancedAds\Constants;
16 use AdvancedAds\Abstracts\Ad;
17 use AdvancedAds\Abstracts\Admin_List_Table;
18 use AdvancedAds\Framework\Utilities\Params;
19
20 defined( 'ABSPATH' ) || exit;
21
22 /**
23 * Admin Ad List Table.
24 */
25 class List_Table extends Admin_List_Table {
26
27 /**
28 * Object being shown on the row.
29 *
30 * @var Ad|null
31 */
32 protected $object = null;
33
34 /**
35 * Object type.
36 *
37 * @var string
38 */
39 protected $object_type = 'ad';
40
41 /**
42 * Post type.
43 *
44 * @var string
45 */
46 protected $list_table_type = Constants::POST_TYPE_AD;
47
48 /**
49 * Hook into WordPress.
50 *
51 * @return void
52 */
53 public function hooks(): void {
54 parent::hooks();
55 add_filter( 'pre_get_posts', [ $this, 'posts_ordering' ] );
56 add_filter( 'views_edit-' . $this->list_table_type, [ $this, 'add_views' ] );
57
58 // Filters.
59 add_filter( 'disable_months_dropdown', '__return_true' );
60 }
61
62 /**
63 * Define which columns to show on this screen.
64 *
65 * @param array $columns Existing columns.
66 *
67 * @return array
68 */
69 public function define_columns( $columns ): array {
70 // Remove the group taxonomy column as we have custom 'Used' column.
71 unset( $columns[ 'taxonomy-' . Constants::TAXONOMY_GROUP ] );
72
73 $new_columns = [];
74
75 foreach ( $columns as $key => $value ) {
76 $new_columns[ $key ] = $value;
77
78 if ( 'cb' === $key ) {
79 $new_columns['ad_type'] = __( 'Type', 'advanced-ads' );
80 continue;
81 }
82
83 if ( 'title' === $key ) {
84 $new_columns['title'] = __( 'Name', 'advanced-ads' );
85 $new_columns['ad_size'] = __( 'Size', 'advanced-ads' );
86 $new_columns['ad_timing'] = __( 'Ad Planning', 'advanced-ads' );
87 $new_columns['ad_shortcode'] = __( 'Ad Shortcode', 'advanced-ads' );
88 $new_columns['ad_adsense_id'] = __( 'AdSense ID', 'advanced-ads' );
89 $new_columns['ad_date'] = __( 'Date', 'advanced-ads' );
90 $new_columns['ad_description'] = __( 'Notes', 'advanced-ads' );
91 $new_columns['ad_preview'] = __( 'Preview', 'advanced-ads' );
92 $new_columns['ad_used'] = __( 'Used', 'advanced-ads' );
93 $new_columns['ad_debugmode'] = __( 'Debug Mode', 'advanced-ads' );
94
95 // show only when privacy setting is enabled.
96 if ( Options::instance()->get( 'privacy.enabled' ) ) {
97 $new_columns['ad_privacyignore'] = __( 'Privacy Ignore', 'advanced-ads' );
98 }
99 }
100 }
101
102 unset( $new_columns['date'] );
103
104 return apply_filters( 'advanced-ads-ad-columns', $new_columns );
105 }
106
107 /**
108 * Define which columns are sortable.
109 *
110 * @param array $columns Existing columns.
111 *
112 * @return array
113 */
114 public function define_sortable_columns( $columns ): array {
115 $columns['ad_date'] = 'ad_date';
116
117 return $columns;
118 }
119
120 /**
121 * Define hidden columns.
122 *
123 * @return array
124 */
125 protected function define_hidden_columns(): array {
126 $hidden[] = 'ad_description';
127 $hidden[] = 'author';
128 $hidden[] = 'ad_size';
129 $hidden[] = 'ad_shortcode';
130 $hidden[] = 'ad_date';
131 $hidden[] = 'ad_preview';
132 $hidden[] = 'ad_adsense_id';
133 $hidden[] = 'ad_debugmode';
134 $hidden[] = 'ad_privacyignore';
135
136 return $hidden;
137 }
138
139 /**
140 * Render any custom filters and search inputs for the list table.
141 *
142 * @return void
143 */
144 protected function render_filters(): void {
145 include_once ADVADS_ABSPATH . 'views/admin/ads/filters.php';
146 }
147
148 /**
149 * Add expiring and expired ads view.
150 *
151 * @param array $views Available list table views.
152 *
153 * @return array
154 */
155 public function add_views( $views ): array {
156 global $wp_list_table;
157
158 $counts = wp_count_posts( $this->list_table_type, 'readable' );
159 $expired = $counts->{Constants::AD_STATUS_EXPIRED};
160 $expiring = $counts->{Constants::AD_STATUS_EXPIRING};
161
162 if ( $expiring > 0 ) {
163 $views[ Constants::AD_STATUS_EXPIRING ] = sprintf(
164 '<a href="%s" %s>%s <span class="count">(%d)</span></a>',
165 add_query_arg(
166 [
167 'post_type' => Constants::POST_TYPE_AD,
168 'addate' => 'advads-filter-expiring',
169 'orderby' => 'expiry_date',
170 'order' => 'ASC',
171 ],
172 'edit.php'
173 ),
174 'advads-filter-expiring' === Params::request( 'addate' ) ? 'class="current" aria-current="page"' : '',
175 esc_attr_x( 'Expiring', 'Post list header for ads expiring in the future.', 'advanced-ads' ),
176 $expiring
177 );
178 }
179
180 if ( $expired > 0 ) {
181 $views[ Constants::AD_STATUS_EXPIRED ] = sprintf(
182 '<a href="%s" %s>%s <span class="count">(%d)</span></a>',
183 add_query_arg(
184 [
185 'post_type' => Constants::POST_TYPE_AD,
186 'addate' => 'advads-filter-expired',
187 'orderby' => 'expiry_date',
188 'order' => 'DESC',
189 ],
190 'edit.php'
191 ),
192 'advads-filter-expired' === Params::request( 'addate' ) ? 'class="current" aria-current="page"' : '',
193 esc_attr_x( 'Expired', 'Post list header for expired ads.', 'advanced-ads' ),
194 $expired
195 );
196 }
197
198 // Render views.
199 $wp_list_table->screen->render_screen_reader_content( 'heading_views' );
200
201 $is_all = count(
202 array_diff_key(
203 $_GET, // phpcs:ignore WordPress.Security.NonceVerification.Recommended
204 [
205 'post_type' => $this->list_table_type,
206 'orderby' => '',
207 'order' => '',
208 'paged' => '',
209 'mode' => '',
210 ]
211 )
212 ) === 0;
213
214 include_once ADVADS_ABSPATH . 'views/admin/table-views-list.php';
215
216 return [];
217 }
218
219 /**
220 * Query filters.
221 *
222 * @param array $query_vars Query vars.
223 *
224 * @return array
225 */
226 protected function query_filters( $query_vars ): array {
227 // Early bail!!
228 if ( wp_doing_ajax() ) {
229 return $query_vars;
230 }
231
232 if ( 'expiry_date' === $query_vars['orderby'] ) {
233 $query_vars['orderby'] = 'meta_value';
234 $query_vars['meta_key'] = Constants::AD_META_EXPIRATION_TIME; // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
235 $query_vars['order'] = strtoupper( $query_vars['order'] ) === 'DESC' ? 'DESC' : 'ASC';
236
237 if ( 'advads-filter-expired' === Params::get( 'addate' ) ) {
238 $query_vars['post_status'] = Constants::AD_STATUS_EXPIRED;
239 }
240 }
241
242 $filter_author = Params::get( 'ad_author' );
243 if ( $filter_author ) {
244 $query_vars['author'] = $filter_author;
245 }
246
247 return $query_vars;
248 }
249
250 /**
251 * Modify the post listing order in the admin panel for a specific custom post type.
252 *
253 * @param WP_Query $query The WP_Query object.
254 *
255 * @return void
256 */
257 public function posts_ordering( $query ): void {
258 global $typenow;
259
260 // Early bail!!
261 if ( ! $query->is_main_query() ) {
262 return;
263 }
264
265 if ( $this->list_table_type === $typenow ) {
266 $orderby = Params::get( 'orderby', 'title' );
267 $order = strtoupper( Params::get( 'order', 'ASC' ) );
268
269 if ( 'ad_date' === $orderby ) {
270 $orderby = 'post_modified';
271 }
272
273 $query->set( 'orderby', $orderby );
274 $query->set( 'order', $order );
275 }
276 }
277
278 /**
279 * Pre-fetch any data for the row each column has access to it.
280 *
281 * @param int $post_id Post ID being shown.
282 *
283 * @return void
284 */
285 protected function prepare_row_data( $post_id ): void {
286 if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) {
287 $this->object = wp_advads_get_ad( $post_id );
288 }
289 }
290
291 /**
292 * Display the ad type icon in the ads list.
293 *
294 * @return void
295 */
296 protected function render_ad_type_column(): void {
297 $ad = $this->object;
298 $size_string = $this->get_ad_size_string();
299
300 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-type.php';
301 }
302
303 /**
304 * Display the ad description in the ads list
305 *
306 * @return void
307 */
308 protected function render_ad_description_column(): void {
309 $description = wp_trim_words( $this->object->get_description(), 50 );
310
311 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-description.php';
312 }
313
314 /**
315 * Display an ad preview in ads list.
316 *
317 * @return void
318 */
319 protected function render_ad_preview_column(): void {
320 $type_object = $this->object->get_type_object();
321
322 // Early bail!!
323 if ( ! $type_object ) {
324 return;
325 }
326
327 if ( is_callable( [ $type_object, 'render_preview' ] ) ) {
328 $type_object->render_preview( $this->object );
329 }
330
331 do_action( 'advanced-ads-ad-list-details-column-after', $this->object, $type_object );
332 }
333
334 /**
335 * Display the ad size in the ads list
336 *
337 * @return void
338 */
339 protected function render_ad_size_column(): void {
340 $size = $this->get_ad_size_string();
341
342 // Early bail!!
343 if ( empty( $size ) ) {
344 return;
345 }
346
347 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-size.php';
348 }
349
350 /**
351 * Display ad timing in ads list
352 *
353 * @return void
354 */
355 protected function render_ad_timing_column(): void {
356 list(
357 'status_strings' => $status_strings,
358 'html_classes' => $html_classes,
359 ) = $this->object->get_ad_schedule_details();
360
361 ob_start();
362 do_action_ref_array(
363 'advanced-ads-ad-list-timing-column-after',
364 [
365 $this->object,
366 &$html_classes,
367 ]
368 );
369 $content_after = ob_get_clean();
370
371 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-timing.php';
372 }
373
374 /**
375 * Display ad shortcode in ads list
376 *
377 * @return void
378 */
379 protected function render_ad_shortcode_column(): void {
380 $ad_id = $this->object->get_id();
381 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-shortcode.php';
382 }
383
384 /**
385 * Display an ad date in ads list.
386 *
387 * @return void
388 */
389 protected function render_ad_date_column(): void {
390 $id = $this->object->get_id();
391
392 if ( ! $id ) {
393 return;
394 }
395
396 $datetime_regex = get_option( 'date_format' ) . ' \\a\\t ' . get_option( 'time_format' );
397 $published_date = get_the_date( $datetime_regex, $id );
398 $modified_date = get_the_modified_date( $datetime_regex, $id );
399
400 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-date.php';
401 }
402
403 /**
404 * Display the adsense id.
405 *
406 * @return void
407 */
408 protected function render_ad_adsense_id_column(): void {
409 if ( null === $this->object->get_content() ) {
410 return;
411 }
412
413 $content = json_decode( $this->object->get_content() );
414 // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
415 $slotid = $content->slotId ?? null;
416
417 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-adsense.php';
418 }
419
420 /**
421 * Display ad usage in groups & placements.
422 *
423 * @return void
424 */
425 protected function render_ad_used_column(): void {
426 $ad_id = $this->object->get_id();
427
428 if ( ! $ad_id ) {
429 return;
430 }
431
432 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-used.php';
433 }
434
435 /**
436 * Display the debug mode in the ads list.
437 *
438 * @return void
439 */
440 protected function render_ad_debugmode_column(): void {
441 $debug_mode = $this->object->is_debug_mode();
442
443 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-debug.php';
444 }
445
446 /**
447 * Display the privacy ignore status in the ads list.
448 *
449 * @return void
450 */
451 protected function render_ad_privacyignore_column(): void {
452 $privacyignore = $this->object->get_prop( 'privacy.ignore-consent' ) ?? false;
453
454 include ADVADS_ABSPATH . 'views/admin/tables/ads/column-privacyignore.php';
455 }
456
457 /**
458 * Get the ad size string to display in post list.
459 *
460 * @return string
461 */
462 private function get_ad_size_string(): string {
463 $size = '';
464 if ( ! empty( $this->object->get_width() ) || ! empty( $this->object->get_height() ) ) {
465 $size = sprintf( '%d &times; %d', $this->object->get_width(), $this->object->get_height() );
466 }
467
468 /**
469 * Filter the ad size string to display in the ads post list.
470 *
471 * @param string $size Size string.
472 * @param Ad $ad Ad instance.
473 */
474 return (string) apply_filters( 'advanced-ads-list-ad-size', $size, $this->object );
475 }
476 }
477