class-aawp-ad.php
1 week ago
class-aawp.php
1 year ago
class-admin-compatibility.php
1 week ago
class-capability-manager.php
1 year ago
class-compatibility.php
1 week ago
class-inline-js.php
1 year ago
class-peepso-ad.php
1 week ago
class-peepso.php
1 week ago
class-compatibility.php
306 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Compatibility Compatibility. |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.48.2 |
| 8 | */ |
| 9 | |
| 10 | namespace AdvancedAds\Compatibility; |
| 11 | |
| 12 | use Exception; |
| 13 | use ReflectionClass; |
| 14 | use AdvancedAds\Constants; |
| 15 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 16 | |
| 17 | defined( 'ABSPATH' ) || exit; |
| 18 | |
| 19 | /** |
| 20 | * Compatibility Compatibility. |
| 21 | */ |
| 22 | class Compatibility implements Integration_Interface { |
| 23 | |
| 24 | /** |
| 25 | * Hook into WordPress. |
| 26 | * |
| 27 | * @return void |
| 28 | */ |
| 29 | public function hooks(): void { |
| 30 | if ( is_admin() ) { |
| 31 | ( new Admin_Compatibility() )->hooks(); |
| 32 | } |
| 33 | |
| 34 | add_filter( 'wpseo_sitemap_entry', [ $this, 'wpseo_noindex_ad_attachments' ], 10, 3 ); |
| 35 | add_filter( 'mailpoet_newsletter_shortcode', [ $this, 'mailpoet_ad_shortcode' ] ); |
| 36 | if ( defined( 'ELEMENTOR_VERSION' ) ) { |
| 37 | add_filter( 'advanced-ads-placement-content-injection-xpath', [ $this, 'elementor_content_injection' ] ); |
| 38 | } |
| 39 | if ( defined( 'BORLABS_COOKIE_VERSION' ) ) { |
| 40 | add_filter( 'advanced-ads-can-display-ads-in-header', [ $this, 'borlabs_cookie_can_add_auto_ads' ], 10 ); |
| 41 | } |
| 42 | if ( defined( 'AIOVG_PLUGIN_VERSION' ) ) { |
| 43 | add_filter( 'advanced-ads-ad-select-args', [ $this, 'aiovg_modify_ad_select_args' ], 10 ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * WordPress SEO: remove attachments attached to ads from `/attachment-sitemap.xml`. |
| 49 | * |
| 50 | * @param array $url Array of URL parts. |
| 51 | * @param string $type URL type. |
| 52 | * @param object $post WP_Post object of attachment. |
| 53 | * |
| 54 | * @return array|bool Unmodified array of URL parts or false to remove URL. |
| 55 | */ |
| 56 | public function wpseo_noindex_ad_attachments( $url, $type, $post ) { |
| 57 | if ( 'post' !== $type ) { |
| 58 | return $url; |
| 59 | } |
| 60 | |
| 61 | if ( ! empty( $post->post_parent ) && Constants::POST_TYPE_AD === get_post_type( (int) $post->post_parent ) ) { |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | return $url; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Display an ad or ad group in a newsletter created by MailPoet. |
| 70 | * |
| 71 | * Usage: |
| 72 | * [custom:ad:123] to display ad with the ID 123 |
| 73 | * [custom:ad_group:345] to display ad group with the ID 345 |
| 74 | * |
| 75 | * @param string $shortcode Shortcode that placed the ad. |
| 76 | * |
| 77 | * @return string |
| 78 | */ |
| 79 | public function mailpoet_ad_shortcode( $shortcode ): string { |
| 80 | // Display an ad group. |
| 81 | if ( sscanf( $shortcode, '[custom:ad_group:%d]', $id ) === 1 ) { |
| 82 | $ad_group = wp_advads_get_group( $id ); |
| 83 | |
| 84 | return ( $ad_group && $ad_group->is_type( [ 'default', 'ordered' ] ) ) |
| 85 | ? get_the_group( $ad_group ) |
| 86 | : ''; |
| 87 | } |
| 88 | |
| 89 | // Display individual ad. |
| 90 | if ( sscanf( $shortcode, '[custom:ad:%d]', $id ) === 1 ) { |
| 91 | $ad = wp_advads_get_ad( $id ); |
| 92 | |
| 93 | if ( $ad && $ad->is_type( [ 'plain', 'image' ] ) ) { |
| 94 | $ad_content = get_the_ad( $ad ); |
| 95 | // Add responsive styles for email compatibility. |
| 96 | if ( $ad->is_type( 'image' ) ) { |
| 97 | return str_replace( |
| 98 | '<img', |
| 99 | '<img style="max-width: 100%; height: auto; display: block;"', |
| 100 | $ad_content |
| 101 | ); |
| 102 | } |
| 103 | return $ad_content; |
| 104 | } |
| 105 | |
| 106 | return ''; |
| 107 | } |
| 108 | |
| 109 | return $shortcode; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Modify xPath expression for Elementor plugin. |
| 114 | * The plugin does not wrap newly created text in 'p' tags. |
| 115 | * |
| 116 | * @param string $tag Xpath tag. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | public function elementor_content_injection( $tag ): string { |
| 121 | // 'p' or 'div.elementor-widget-text-editor' without nested 'p' |
| 122 | if ( 'p' === $tag ) { |
| 123 | $tag = "*[self::p or self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' elementor-widget-text-editor ') and not(descendant::p)]]"; |
| 124 | } |
| 125 | |
| 126 | return $tag; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Check if Adsense Auto ads code can be added to the header. |
| 131 | * |
| 132 | * @param bool $can_display If the ad can be displayed. |
| 133 | * |
| 134 | * @return bool |
| 135 | */ |
| 136 | public function borlabs_cookie_can_add_auto_ads( $can_display ): bool { |
| 137 | if ( ! $can_display ) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | return ! self::borlabs_cookie_adsense_auto_ads_code_exists(); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Thrive Theme Builder Check if placements of type other than `header` can be injected during `wp_head` action. |
| 146 | */ |
| 147 | public static function can_inject_during_wp_head() { |
| 148 | if ( did_action( 'before_theme_builder_template_render' ) && ! did_action( 'after_theme_builder_template_render' ) ) { |
| 149 | return true; |
| 150 | } |
| 151 | |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Check if Adsense Auto ads code is added by the Borlabs Cookie plugin. |
| 157 | * |
| 158 | * This allows to prevent the "Only one 'enable_page_level_ads' allowed per page" error |
| 159 | * that makes impossible to close the "Privacy Preference" window created by the "Borlabs Cookie" plugin. |
| 160 | * |
| 161 | * @return bool |
| 162 | */ |
| 163 | public static function borlabs_cookie_adsense_auto_ads_code_exists(): bool { |
| 164 | static $result = null; |
| 165 | |
| 166 | if ( null !== $result ) { |
| 167 | return $result; |
| 168 | } |
| 169 | |
| 170 | $opt_in_js = ''; |
| 171 | $all_cookies = self::borlabs_get_cookies(); |
| 172 | if ( empty( $all_cookies ) ) { |
| 173 | $result = false; |
| 174 | return $result; |
| 175 | } |
| 176 | |
| 177 | foreach ( $all_cookies as $cookie_group_data ) { |
| 178 | if ( self::is_marketing_cookie( $cookie_group_data ) ) { |
| 179 | foreach ( $cookie_group_data->cookies as $cookie_data ) { |
| 180 | if ( self::is_adsense_cookie( $cookie_data ) ) { |
| 181 | $opt_in_js = $cookie_data->opt_in_js; |
| 182 | break 2; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if ( empty( $opt_in_js ) ) { |
| 189 | $result = false; |
| 190 | return $result; |
| 191 | } |
| 192 | |
| 193 | $result = preg_match( '/<script[^>]+data-ad-client/', $opt_in_js ) || false !== strpos( $opt_in_js, 'enable_page_level_ads:' ); |
| 194 | return $result; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Get cookies from borlabs plugin |
| 199 | * |
| 200 | * @return bool|array |
| 201 | */ |
| 202 | private static function borlabs_get_cookies() { |
| 203 | // Early bail!! |
| 204 | if ( ! class_exists( '\BorlabsCookie\Cookie\Frontend\Cookies' ) ) { |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | $all_cookies = []; |
| 209 | |
| 210 | try { |
| 211 | $refl_cookies = new ReflectionClass( '\BorlabsCookie\Cookie\Frontend\Cookies' ); |
| 212 | |
| 213 | if ( $refl_cookies->hasMethod( 'getInstance' ) && $refl_cookies->hasMethod( 'getAllCookieGroups' ) ) { |
| 214 | $instance = $refl_cookies->getMethod( 'getInstance' ); |
| 215 | $cookie_groups = $refl_cookies->getMethod( 'getAllCookieGroups' ); |
| 216 | |
| 217 | if ( $instance->isPublic() && $instance->isStatic() && $cookie_groups->isPublic() ) { |
| 218 | $all_cookies = \BorlabsCookie\Cookie\Frontend\Cookies::getInstance()->getAllCookieGroups(); |
| 219 | } |
| 220 | } |
| 221 | } catch ( Exception $e ) { |
| 222 | return false; |
| 223 | } |
| 224 | |
| 225 | return $all_cookies; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Is cookie is of marketing and has data |
| 230 | * |
| 231 | * @param mixed $cookie Cookie to check. |
| 232 | * |
| 233 | * @return bool |
| 234 | */ |
| 235 | private static function is_marketing_cookie( $cookie ): bool { |
| 236 | if ( |
| 237 | ! empty( $cookie->group_id ) && |
| 238 | 'marketing' === $cookie->group_id && |
| 239 | ! empty( $cookie->cookies ) |
| 240 | ) { |
| 241 | return true; |
| 242 | } |
| 243 | |
| 244 | return false; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Whether the cookie is the Google AdSense entry with opt-in JS. |
| 249 | * |
| 250 | * @param mixed $cookie Cookie to check. |
| 251 | * |
| 252 | * @return bool |
| 253 | */ |
| 254 | private static function is_adsense_cookie( $cookie ): bool { |
| 255 | if ( |
| 256 | ! empty( $cookie->cookie_id ) && |
| 257 | 'google-adsense' === $cookie->cookie_id && |
| 258 | ! empty( $cookie->opt_in_js ) |
| 259 | ) { |
| 260 | return true; |
| 261 | } |
| 262 | |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Modify ad select arguments to treat AIOVG shortcode pages as native taxonomy archives. |
| 268 | * |
| 269 | * @param array<string, mixed> $args Current ad selection arguments. |
| 270 | * |
| 271 | * @return array<string, mixed> Modified ad selection arguments. |
| 272 | */ |
| 273 | public function aiovg_modify_ad_select_args( array $args ): array { |
| 274 | $aiovg_taxs = [ |
| 275 | 'aiovg_categories' => 'aiovg_category', |
| 276 | 'aiovg_tags' => 'aiovg_tag', |
| 277 | ]; |
| 278 | |
| 279 | foreach ( $aiovg_taxs as $aiovg_tax => $aiovg_query_var ) { |
| 280 | if ( taxonomy_exists( $aiovg_tax ) ) { |
| 281 | $aiovg_slug = get_query_var( $aiovg_query_var ); |
| 282 | |
| 283 | if ( ! empty( $aiovg_slug ) && is_scalar( $aiovg_slug ) ) { |
| 284 | $aiovg_term = get_term_by( 'slug', (string) $aiovg_slug, $aiovg_tax ); |
| 285 | |
| 286 | if ( $aiovg_term instanceof \WP_Term ) { |
| 287 | if ( ! isset( $args['wp_the_query'] ) || ! is_array( $args['wp_the_query'] ) ) { |
| 288 | $args['wp_the_query'] = []; |
| 289 | } |
| 290 | |
| 291 | // Override the standard page parameters so Advanced Ads treats it as an archive. |
| 292 | $args['wp_the_query']['term_id'] = $aiovg_term->term_id; |
| 293 | $args['wp_the_query']['taxonomy'] = $aiovg_tax; |
| 294 | $args['wp_the_query']['is_archive'] = true; |
| 295 | $args['wp_the_query']['is_singular'] = false; |
| 296 | |
| 297 | break; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return $args; |
| 304 | } |
| 305 | } |
| 306 |