ads
3 months ago
groups
3 months ago
metaboxes
1 year ago
pages
3 months ago
placements
2 months ago
class-action-links.php
1 year ago
class-addon-box.php
1 year ago
class-addon-updater.php
3 months ago
class-admin-menu.php
3 months ago
class-admin-notices.php
1 year ago
class-ajax.php
3 months ago
class-assets.php
3 months ago
class-authors.php
1 year ago
class-compatibility.php
1 year ago
class-edd-updater.php
3 months ago
class-list-filters.php
2 months ago
class-marketing.php
1 year ago
class-metabox-ad-settings.php
1 year ago
class-metabox-ad.php
1 year ago
class-misc.php
1 year ago
class-page-quick-edit.php
1 year ago
class-plugin-installer.php
1 year ago
class-post-list.php
1 year ago
class-post-types.php
3 months ago
class-screen-options.php
3 months ago
class-settings.php
1 year ago
class-shortcode-creator.php
1 year ago
class-system-info.php
1 year ago
class-tinymce.php
2 years ago
class-translation-promo.php
1 year ago
class-upgrades.php
1 year ago
class-version-control.php
3 months ago
class-welcome.php
1 year ago
class-wordpress-dashboard.php
1 year ago
index.php
2 years ago
class-post-list.php
193 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Display ad-related information on the post and page overview page. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.x.x |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Admin; |
| 11 | |
| 12 | use AdvancedAds\Framework\Utilities\Params; |
| 13 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | /** |
| 18 | * Class Post_List |
| 19 | */ |
| 20 | class Post_List implements Integration_Interface { |
| 21 | |
| 22 | /** |
| 23 | * Hook into WordPress. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function hooks(): void { |
| 28 | add_action( 'restrict_manage_posts', [ $this, 'add_ads_filter_dropdown' ] ); |
| 29 | add_action( 'pre_get_posts', [ $this, 'filter_posts_by_ads_status' ] ); |
| 30 | add_filter( 'manage_posts_columns', [ $this, 'ads_column_init' ] ); |
| 31 | add_filter( 'manage_pages_columns', [ $this, 'ads_column_init' ] ); |
| 32 | add_filter( 'manage_edit-post_sortable_columns', [ $this, 'sortable_ads_column' ] ); |
| 33 | add_filter( 'manage_edit-page_sortable_columns', [ $this, 'sortable_ads_column' ] ); |
| 34 | add_filter( 'posts_clauses', [ $this, 'request_clauses' ], 10, 2 ); |
| 35 | add_action( 'manage_posts_custom_column', [ $this, 'ads_column_content' ], 10, 2 ); |
| 36 | add_action( 'manage_pages_custom_column', [ $this, 'ads_column_content' ], 10, 2 ); |
| 37 | add_filter( 'default_hidden_columns', [ $this, 'hide_ads_column_by_default' ], 10, 2 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Add a filter dropdown to the post and pages lists. |
| 42 | * |
| 43 | * @param string $post_type current post type. |
| 44 | * |
| 45 | * @return void |
| 46 | */ |
| 47 | public function add_ads_filter_dropdown( string $post_type ): void { |
| 48 | if ( ! in_array( $post_type, [ 'post', 'page' ], true ) ) { |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | $viewability = Params::get( 'ad-viewability', '' ); |
| 53 | include ADVADS_ABSPATH . 'admin/views/post-list-filter-dropdown.php'; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Filter the list of posts and pages based on their ads settings |
| 58 | * |
| 59 | * @param \WP_Query $query The WP_Query object. |
| 60 | * |
| 61 | * @return void |
| 62 | */ |
| 63 | public function filter_posts_by_ads_status( \WP_Query $query ): void { |
| 64 | if ( ! is_admin() || ! $query->is_main_query() || ! $query->get( 'post_type' ) || ! in_array( $query->get( 'post_type' ), [ 'post', 'page' ], true ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $viewability = Params::get( 'ad-viewability', '' ); |
| 69 | if ( ! $viewability ) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | if ( in_array( $viewability, [ 'disable_ads', 'disable_the_content' ], true ) ) { |
| 74 | $query->set( 'meta_key', '_advads_ad_settings' ); |
| 75 | $query->set( 'meta_compare', 'LIKE' ); |
| 76 | $query->set( 'meta_value', '"' . $viewability . '";i:1;' ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Order post list by ad status |
| 82 | * |
| 83 | * @param array $clauses existing request clauses. |
| 84 | * @param \WP_Query $query the current WP Query. |
| 85 | * |
| 86 | * @return array |
| 87 | */ |
| 88 | public function request_clauses( $clauses, $query ) { |
| 89 | global $wpdb; |
| 90 | |
| 91 | if ( empty( $query->query_vars['orderby'] ) || 'ad-status' !== $query->query_vars['orderby'] || ! $query->is_main_query() ) { |
| 92 | // No need to order by ad status. |
| 93 | return $clauses; |
| 94 | } |
| 95 | |
| 96 | if ( ! function_exists( 'get_current_screen' ) ) { |
| 97 | return $clauses; |
| 98 | } |
| 99 | |
| 100 | $screen = get_current_screen(); |
| 101 | |
| 102 | if ( ! $screen || ! in_array( $screen->id, [ 'edit-post', 'edit-page' ], true ) ) { |
| 103 | // Not the page we're interested in. |
| 104 | return $clauses; |
| 105 | } |
| 106 | |
| 107 | // Create aliases for ads disabled on the post/page and injection into the content disabled. |
| 108 | $clauses['join'] .= " LEFT JOIN (SELECT post_id, IF(meta_value LIKE '%disable_ads\";i:1%', 1, 0) as ads, IF(meta_value LIKE '%disable_the_content\";s:1%', 1, 0) as content FROM {$wpdb->postmeta}" |
| 109 | . " WHERE meta_key = '_advads_ad_settings') as advads_meta on {$wpdb->posts}.ID = advads_meta.post_id"; |
| 110 | |
| 111 | $order = 'asc' === strtolower( $query->query_vars['order'] ) ? 'DESC' : 'ASC'; |
| 112 | |
| 113 | $clauses['orderby'] = "advads_meta.ads {$order}, advads_meta.content {$order}"; |
| 114 | |
| 115 | return $clauses; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Make the ad status column sortable |
| 120 | * |
| 121 | * @param array $columns columns list. |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function sortable_ads_column( $columns ) { |
| 126 | $columns['ad-status'] = 'ad-status'; |
| 127 | |
| 128 | return $columns; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Adds a new column to the post overview page for public post types. |
| 133 | * |
| 134 | * @param array $columns An array of column names. |
| 135 | * |
| 136 | * @return array The modified array of column names. |
| 137 | */ |
| 138 | public function ads_column_init( array $columns ): array { |
| 139 | $post_type = wp_doing_ajax() ? Params::post( 'post_type', '' ) : get_current_screen()->post_type; |
| 140 | |
| 141 | if ( $post_type && get_post_type_object( $post_type )->public ) { |
| 142 | $columns['ad-status'] = __( 'Ad injection', 'advanced-ads' ); |
| 143 | } |
| 144 | |
| 145 | return $columns; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Displays the value of the "ads" post meta in the "Ads" column. |
| 150 | * |
| 151 | * @param string $column The name of the column. |
| 152 | * @param int $post_id The ID of the post. |
| 153 | * |
| 154 | * @return void |
| 155 | */ |
| 156 | public function ads_column_content( string $column, int $post_id ): void { |
| 157 | // Early bail!! |
| 158 | if ( 'ad-status' !== $column ) { |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | $ads_post_meta = get_post_meta( $post_id, '_advads_ad_settings', true ); |
| 163 | |
| 164 | if ( ! empty( $ads_post_meta['disable_ads'] ) ) { |
| 165 | echo '<p>' . esc_html__( 'All ads disabled', 'advanced-ads' ) . '</p>'; |
| 166 | } |
| 167 | |
| 168 | if ( defined( 'AAP_VERSION' ) && ! empty( $ads_post_meta['disable_the_content'] ) ) { |
| 169 | echo '<p>' . esc_html__( 'Ads in content disabled', 'advanced-ads' ) . '</p>'; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Hide the Ads column by default |
| 175 | * |
| 176 | * @param string[] $hidden hidden columns. |
| 177 | * @param \WP_Screen $screen screen object. |
| 178 | * |
| 179 | * @return string[] |
| 180 | */ |
| 181 | public function hide_ads_column_by_default( array $hidden, \WP_Screen $screen ): array { |
| 182 | $post_type_object = get_post_type_object( $screen->post_type ); |
| 183 | |
| 184 | if ( ! $post_type_object || ! $post_type_object->public ) { |
| 185 | return $hidden; |
| 186 | } |
| 187 | |
| 188 | $hidden[] = 'ad-status'; |
| 189 | |
| 190 | return $hidden; |
| 191 | } |
| 192 | } |
| 193 |