class-ad-display-condition.php
1 year ago
class-ad-renderer.php
2 weeks ago
class-debug-ads.php
1 year ago
class-manager.php
1 year ago
class-scripts.php
2 months ago
class-stats.php
1 year ago
class-ad-display-condition.php
267 lines
| 1 | <?php |
| 2 | /** |
| 3 | * This class is responsible for handling the display conditions of ads. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.50.0 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Frontend; |
| 11 | |
| 12 | use Advanced_Ads_Utils; |
| 13 | use AdvancedAds\Options; |
| 14 | use AdvancedAds\Utilities\WordPress; |
| 15 | use AdvancedAds\Utilities\Conditional; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Page display condition class. |
| 21 | */ |
| 22 | class Ad_Display_Condition { |
| 23 | /** |
| 24 | * Identifier that supplement the disabled reason. |
| 25 | * |
| 26 | * @var int|string |
| 27 | */ |
| 28 | public $disabled_id = ''; |
| 29 | |
| 30 | /** |
| 31 | * Reason why are disabled. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | public $disabled_reason = ''; |
| 36 | |
| 37 | /** |
| 38 | * Get the disabled id. |
| 39 | * |
| 40 | * @return int|string |
| 41 | */ |
| 42 | public function get_disabled_id() { |
| 43 | return $this->disabled_id; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Get the reason why ads are disabled. |
| 48 | * |
| 49 | * @return string |
| 50 | */ |
| 51 | public function get_disabled_reason(): string { |
| 52 | return $this->disabled_reason ?? ''; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Runs checks and set disabled constants accordingly. |
| 57 | * |
| 58 | * @return void |
| 59 | */ |
| 60 | public function run_checks(): void { |
| 61 | global $wp_the_query; |
| 62 | |
| 63 | // Early bail!! |
| 64 | if ( Conditional::is_ad_disabled() ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $is_rest = Conditional::is_rest_request(); |
| 69 | |
| 70 | $options = Options::instance()->get( 'advanced-ads' ); |
| 71 | $checks = [ |
| 72 | // Check if ads are disabled completely. |
| 73 | 'all' => [ |
| 74 | 'check' => ! $is_rest && ! is_feed() && ! empty( $options['disabled-ads']['all'] ), |
| 75 | 'args' => [ 'all' ], |
| 76 | ], |
| 77 | // Check if ads are disabled in REST API. |
| 78 | 'rest' => [ |
| 79 | 'check' => $is_rest && ! empty( $options['disabled-ads']['rest-api'] ), |
| 80 | 'args' => [ 'rest-api' ], |
| 81 | ], |
| 82 | // Check if ads are disabled from 404 pages. |
| 83 | 'error404' => [ |
| 84 | 'check' => $wp_the_query->is_404() && ! empty( $options['disabled-ads']['404'] ), |
| 85 | 'args' => [ '404' ], |
| 86 | ], |
| 87 | // Check if ads are disabled from non-singular frontend pages (often = archives). |
| 88 | 'archive' => [ |
| 89 | 'check' => ! is_feed() && ! $is_rest && ! $wp_the_query->is_singular() && ! empty( $options['disabled-ads']['archives'] ), |
| 90 | 'args' => [ 'archive' ], |
| 91 | ], |
| 92 | // Check if ads are disabled in Feed. |
| 93 | 'feed' => [ |
| 94 | 'check' => $wp_the_query->is_feed() && ( $options['disabled-ads']['feed'] ?? false ), |
| 95 | 'args' => [ 'feed' ], |
| 96 | ], |
| 97 | 'current_page' => [ $this, 'check_current_page' ], |
| 98 | 'posts_page' => [ $this, 'check_posts_page' ], |
| 99 | 'shop' => [ $this, 'check_shop' ], |
| 100 | 'user_roles' => [ $this, 'check_user_roles' ], |
| 101 | 'bots' => [ $this, 'check_bots' ], |
| 102 | 'ip_addresses' => [ $this, 'check_ip_addresses' ], |
| 103 | ]; |
| 104 | |
| 105 | /** |
| 106 | * Allows experienced user to customize the rules that disable ads on a page |
| 107 | * |
| 108 | * @param array $checks list of the rules that will be checked. |
| 109 | * @param array $options plugin options. |
| 110 | */ |
| 111 | $checks = apply_filters( 'advanced-ads-ad-display-check', $checks, $options ); |
| 112 | |
| 113 | if ( ! is_array( $checks ) ) { |
| 114 | $checks = []; |
| 115 | } |
| 116 | |
| 117 | foreach ( $checks as $check ) { |
| 118 | if ( isset( $check['check'] ) && $check['check'] ) { |
| 119 | $this->disable_ads( ...$check['args'] ); |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | if ( is_callable( $check ) ) { |
| 124 | $truthiness = call_user_func( $check ); |
| 125 | if ( $truthiness ) { |
| 126 | return; |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Check if ads are disabled on the current page. |
| 134 | * |
| 135 | * @return bool |
| 136 | */ |
| 137 | private function check_current_page(): bool { |
| 138 | global $post, $wp_the_query; |
| 139 | |
| 140 | if ( $wp_the_query->is_singular() && isset( $post->ID ) ) { |
| 141 | $settings = get_post_meta( $post->ID, '_advads_ad_settings', true ); |
| 142 | |
| 143 | if ( ! empty( $settings['disable_ads'] ) ) { |
| 144 | $this->disable_ads( 'page', $post->ID ); |
| 145 | return true; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Check if ads are disabled on "Posts page" set on the WordPress Reading settings page. |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | private function check_posts_page(): bool { |
| 158 | global $wp_the_query; |
| 159 | |
| 160 | if ( $wp_the_query->is_posts_page ) { |
| 161 | $settings = get_post_meta( $wp_the_query->queried_object_id, '_advads_ad_settings', true ); |
| 162 | |
| 163 | if ( ! empty( $settings['disable_ads'] ) ) { |
| 164 | $this->disable_ads( 'page', $wp_the_query->queried_object_id ); |
| 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Check if ads are disabled on WooCommerce shop page (and currently on shop page). |
| 174 | * |
| 175 | * @return bool |
| 176 | */ |
| 177 | private function check_shop(): bool { |
| 178 | if ( function_exists( 'is_shop' ) && is_shop() ) { |
| 179 | $shop_id = wc_get_page_id( 'shop' ); |
| 180 | $settings = get_post_meta( $shop_id, '_advads_ad_settings', true ); |
| 181 | if ( ! empty( $settings['disable_ads'] ) ) { |
| 182 | $this->disable_ads( 'page', $shop_id ); |
| 183 | return true; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return false; |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Check if ads are disabled for current user role. |
| 192 | * |
| 193 | * @return bool |
| 194 | */ |
| 195 | private function check_user_roles(): bool { |
| 196 | $options = Options::instance()->get( 'advanced-ads' ); |
| 197 | $current_user = wp_get_current_user(); |
| 198 | $hide_for_roles = isset( $options['hide-for-user-role'] ) |
| 199 | ? Advanced_Ads_Utils::maybe_translate_cap_to_role( $options['hide-for-user-role'] ) |
| 200 | : []; |
| 201 | |
| 202 | if ( |
| 203 | $hide_for_roles && is_user_logged_in() && is_array( $current_user->roles ) && |
| 204 | array_intersect( $hide_for_roles, $current_user->roles ) |
| 205 | ) { |
| 206 | $this->disable_ads( 'user-role' ); |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Check if ads are disabled for bots. |
| 215 | * |
| 216 | * @return bool |
| 217 | */ |
| 218 | private function check_bots(): bool { |
| 219 | $options = Options::instance()->get( 'advanced-ads' ); |
| 220 | if ( |
| 221 | isset( $options['block-bots'] ) && $options['block-bots'] && |
| 222 | ! WordPress::is_cache_bot() && Conditional::is_ua_bot() |
| 223 | ) { |
| 224 | $this->disable_ads(); |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Check if ads are disabled for IP addresses. |
| 233 | * |
| 234 | * @return bool |
| 235 | */ |
| 236 | private function check_ip_addresses(): bool { |
| 237 | $options = Options::instance()->get( 'advanced-ads' ); |
| 238 | |
| 239 | if ( isset( $options['hide-for-ip-address']['enabled'] ) ) { |
| 240 | $ip_addresses = isset( $options['hide-for-ip-address']['ips'] ) |
| 241 | ? explode( "\n", $options['hide-for-ip-address']['ips'] ) |
| 242 | : []; |
| 243 | $ip_addresses = array_map( 'trim', $ip_addresses ); |
| 244 | $user_ip = get_user_ip_address(); |
| 245 | |
| 246 | if ( $user_ip && ! empty( $ip_addresses ) && in_array( $user_ip, $ip_addresses, true ) ) { |
| 247 | $this->disable_ads( 'ip-address' ); |
| 248 | return true; |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Disable ads. |
| 257 | * |
| 258 | * @param string $reason Reason why are disabled. |
| 259 | * @param int|string $id Identifier that supplement the disabled reason. |
| 260 | */ |
| 261 | private function disable_ads( $reason = null, $id = null ) { |
| 262 | $this->disabled_id = $id; |
| 263 | $this->disabled_reason = $reason; |
| 264 | define( 'ADVADS_ADS_DISABLED', true ); |
| 265 | } |
| 266 | } |
| 267 |