class-aawp-ad.php
1 year ago
class-aawp.php
1 year ago
class-admin-compatibility.php
1 year ago
class-capability-manager.php
1 year ago
class-compatibility.php
1 year ago
class-inline-js.php
1 year ago
class-peepso-ad.php
1 year ago
class-peepso.php
1 year ago
class-compatibility.php
258 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\Utilities\Data; |
| 15 | use AdvancedAds\Framework\Utilities\Str; |
| 16 | use AdvancedAds\Framework\Interfaces\Integration_Interface; |
| 17 | |
| 18 | defined( 'ABSPATH' ) || exit; |
| 19 | |
| 20 | /** |
| 21 | * Compatibility Compatibility. |
| 22 | */ |
| 23 | class Compatibility implements Integration_Interface { |
| 24 | |
| 25 | /** |
| 26 | * Hook into WordPress. |
| 27 | * |
| 28 | * @return void |
| 29 | */ |
| 30 | public function hooks(): void { |
| 31 | if ( is_admin() ) { |
| 32 | ( new Admin_Compatibility() )->hooks(); |
| 33 | } |
| 34 | |
| 35 | add_filter( 'wpseo_sitemap_entry', [ $this, 'wpseo_noindex_ad_attachments' ], 10, 3 ); |
| 36 | add_filter( 'mailpoet_newsletter_shortcode', [ $this, 'mailpoet_ad_shortcode' ] ); |
| 37 | if ( defined( 'ELEMENTOR_VERSION' ) ) { |
| 38 | add_filter( 'advanced-ads-placement-content-injection-xpath', [ $this, 'elementor_content_injection' ] ); |
| 39 | } |
| 40 | if ( defined( 'BORLABS_COOKIE_VERSION' ) ) { |
| 41 | add_filter( 'advanced-ads-can-display-ads-in-header', [ $this, 'borlabs_cookie_can_add_auto_ads' ], 10 ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * WordPress SEO: remove attachments attached to ads from `/attachment-sitemap.xml`. |
| 47 | * |
| 48 | * @param array $url Array of URL parts. |
| 49 | * @param string $type URL type. |
| 50 | * @param object $post WP_Post object of attachment. |
| 51 | * |
| 52 | * @return array|bool Unmodified array of URL parts or false to remove URL. |
| 53 | */ |
| 54 | public function wpseo_noindex_ad_attachments( $url, $type, $post ) { |
| 55 | if ( 'post' !== $type ) { |
| 56 | return $url; |
| 57 | } |
| 58 | |
| 59 | $ad_ids = Data::get_ads_ids(); |
| 60 | |
| 61 | if ( isset( $post->post_parent ) && in_array( $post->post_parent, $ad_ids, true ) ) { |
| 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 | preg_match( '/\d+/', $shortcode, $matches ); |
| 81 | $entity_id = empty( $matches[0] ) ? false : $matches[0]; |
| 82 | |
| 83 | // Display an ad group. |
| 84 | if ( Str::starts_with( '[custom:ad_group:', $shortcode ) && $entity_id ) { |
| 85 | $ad_group = wp_advads_get_group( $entity_id ); |
| 86 | if ( $ad_group->is_type( 'default', 'ordered' ) ) { |
| 87 | return get_the_group( $ad_group ); |
| 88 | } |
| 89 | |
| 90 | return ''; |
| 91 | } |
| 92 | |
| 93 | // Display individual ad. |
| 94 | if ( Str::starts_with( '[custom:ad:', $shortcode ) && $entity_id ) { |
| 95 | $ad = wp_advads_get_ad( $entity_id ); |
| 96 | if ( $ad->is_type( 'plain', 'image' ) ) { |
| 97 | return get_the_ad( $ad ); |
| 98 | } |
| 99 | |
| 100 | return ''; |
| 101 | } |
| 102 | |
| 103 | return $shortcode; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Modify xPath expression for Elementor plugin. |
| 108 | * The plugin does not wrap newly created text in 'p' tags. |
| 109 | * |
| 110 | * @param string $tag Xpath tag. |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public function elementor_content_injection( $tag ): string { |
| 115 | // 'p' or 'div.elementor-widget-text-editor' without nested 'p' |
| 116 | if ( 'p' === $tag ) { |
| 117 | $tag = "*[self::p or self::div[@class and contains(concat(' ', normalize-space(@class), ' '), ' elementor-widget-text-editor ') and not(descendant::p)]]"; |
| 118 | } |
| 119 | |
| 120 | return $tag; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Check if Adsense Auto ads code can be added to the header. |
| 125 | * |
| 126 | * @param bool $can_display If the ad can be displayed. |
| 127 | * |
| 128 | * @return bool |
| 129 | */ |
| 130 | public function borlabs_cookie_can_add_auto_ads( $can_display ): bool { |
| 131 | if ( ! $can_display ) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return ! self::borlabs_cookie_adsense_auto_ads_code_exists(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Thrive Theme Builder Check if placements of type other than `header` can be injected during `wp_head` action. |
| 140 | */ |
| 141 | public static function can_inject_during_wp_head() { |
| 142 | if ( did_action( 'before_theme_builder_template_render' ) && ! did_action( 'after_theme_builder_template_render' ) ) { |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | return false; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Check if Adsense Auto ads code is added by the Borlabs Cookie plugin. |
| 151 | * |
| 152 | * This allows to prevent the "Only one 'enable_page_level_ads' allowed per page" error |
| 153 | * that makes impossible to close the "Privacy Preference" window created by the "Borlabs Cookie" plugin. |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | public static function borlabs_cookie_adsense_auto_ads_code_exists(): bool { |
| 158 | static $result = null; |
| 159 | |
| 160 | if ( null !== $result ) { |
| 161 | return $result; |
| 162 | } |
| 163 | |
| 164 | $all_cookies = self::borlabs_get_cookies(); |
| 165 | if ( empty( $all_cookies ) ) { |
| 166 | $result = false; |
| 167 | return $result; |
| 168 | } |
| 169 | |
| 170 | foreach ( $all_cookies as $cookie_group_data ) { |
| 171 | if ( self::is_marketing_cookie( $cookie_group_data ) ) { |
| 172 | foreach ( $cookie_group_data->cookies as $cookie_data ) { |
| 173 | if ( self::is_adsense_cookie( $cookie_data ) ) { |
| 174 | $opt_in_js = $cookie_data->opt_in_js; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if ( empty( $opt_in_js ) ) { |
| 181 | $result = false; |
| 182 | return $result; |
| 183 | } |
| 184 | |
| 185 | $result = preg_match( '/<script[^>]+data-ad-client/', $opt_in_js ) || false !== strpos( $opt_in_js, 'enable_page_level_ads:' ); |
| 186 | return $result; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Get cookies from borlabs plugin |
| 191 | * |
| 192 | * @return bool|array |
| 193 | */ |
| 194 | private static function borlabs_get_cookies() { |
| 195 | // Early bail!! |
| 196 | if ( ! class_exists( '\BorlabsCookie\Cookie\Frontend\Cookies' ) ) { |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | $all_cookies = []; |
| 201 | |
| 202 | try { |
| 203 | $refl_cookies = new ReflectionClass( '\BorlabsCookie\Cookie\Frontend\Cookies' ); |
| 204 | |
| 205 | if ( $refl_cookies->hasMethod( 'getInstance' ) && $refl_cookies->hasMethod( 'getAllCookieGroups' ) ) { |
| 206 | $instance = $refl_cookies->getMethod( 'getInstance' ); |
| 207 | $cookie_groups = $refl_cookies->getMethod( 'getAllCookieGroups' ); |
| 208 | |
| 209 | if ( $instance->isPublic() && $instance->isStatic() && $cookie_groups->isPublic() ) { |
| 210 | $all_cookies = \BorlabsCookie\Cookie\Frontend\Cookies::getInstance()->getAllCookieGroups(); |
| 211 | } |
| 212 | } |
| 213 | } catch ( Exception $e ) { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | return $all_cookies; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Is cookie is of marketing and has data |
| 222 | * |
| 223 | * @param mixed $cookie Cookie to check. |
| 224 | * |
| 225 | * @return bool |
| 226 | */ |
| 227 | private static function is_marketing_cookie( $cookie ): bool { |
| 228 | if ( |
| 229 | ! empty( $cookie->group_id ) && |
| 230 | 'marketing' === $cookie->group_id && |
| 231 | ! empty( $cookie->cookies ) |
| 232 | ) { |
| 233 | return true; |
| 234 | } |
| 235 | |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Is cookie is of marketing and has data |
| 241 | * |
| 242 | * @param mixed $cookie Cookie to check. |
| 243 | * |
| 244 | * @return bool |
| 245 | */ |
| 246 | private static function is_adsense_cookie( $cookie ): bool { |
| 247 | if ( |
| 248 | ! empty( $cookie->cookie_id ) && |
| 249 | 'google-adsense' === $cookie->cookie_id && |
| 250 | ! empty( $cookie->opt_in_js ) |
| 251 | ) { |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | return false; |
| 256 | } |
| 257 | } |
| 258 |