* @since 1.48.2
*/
namespace AdvancedAds\Compatibility;
use Exception;
use ReflectionClass;
use AdvancedAds\Utilities\Data;
use AdvancedAds\Framework\Utilities\Str;
use AdvancedAds\Framework\Interfaces\Integration_Interface;
defined( 'ABSPATH' ) || exit;
/**
* Compatibility Compatibility.
*/
class Compatibility implements Integration_Interface {
/**
* Hook into WordPress.
*
* @return void
*/
public function hooks(): void {
if ( is_admin() ) {
( new Admin_Compatibility() )->hooks();
}
add_filter( 'wpseo_sitemap_entry', [ $this, 'wpseo_noindex_ad_attachments' ], 10, 3 );
add_filter( 'mailpoet_newsletter_shortcode', [ $this, 'mailpoet_ad_shortcode' ] );
if ( defined( 'ELEMENTOR_VERSION' ) ) {
add_filter( 'advanced-ads-placement-content-injection-xpath', [ $this, 'elementor_content_injection' ] );
}
if ( defined( 'BORLABS_COOKIE_VERSION' ) ) {
add_filter( 'advanced-ads-can-display-ads-in-header', [ $this, 'borlabs_cookie_can_add_auto_ads' ], 10 );
}
}
/**
* WordPress SEO: remove attachments attached to ads from `/attachment-sitemap.xml`.
*
* @param array $url Array of URL parts.
* @param string $type URL type.
* @param object $post WP_Post object of attachment.
*
* @return array|bool Unmodified array of URL parts or false to remove URL.
*/
public function wpseo_noindex_ad_attachments( $url, $type, $post ) {
if ( 'post' !== $type ) {
return $url;
}
$ad_ids = Data::get_ads_ids();
if ( isset( $post->post_parent ) && in_array( $post->post_parent, $ad_ids, true ) ) {
return false;
}
return $url;
}
/**
* Display an ad or ad group in a newsletter created by MailPoet.
*
* Usage:
* [custom:ad:123] to display ad with the ID 123
* [custom:ad_group:345] to display ad group with the ID 345
*
* @param string $shortcode Shortcode that placed the ad.
*
* @return string
*/
public function mailpoet_ad_shortcode( $shortcode ): string {
// Display an ad group.
if ( sscanf( $shortcode, '[custom:ad_group:%d]', $id ) === 1 ) {
$ad_group = wp_advads_get_group( $id );
return ( $ad_group && $ad_group->is_type( [ 'default', 'ordered' ] ) )
? get_the_group( $ad_group )
: '';
}
// Display individual ad.
if ( sscanf( $shortcode, '[custom:ad:%d]', $id ) === 1 ) {
$ad = wp_advads_get_ad( $id );
if ( $ad && $ad->is_type( [ 'plain', 'image' ] ) ) {
$ad_content = get_the_ad( $ad );
// Add responsive styles for email compatibility.
if ( $ad->is_type( 'image' ) ) {
return str_replace(
'
cookies as $cookie_data ) {
if ( self::is_adsense_cookie( $cookie_data ) ) {
$opt_in_js = $cookie_data->opt_in_js;
}
}
}
}
if ( empty( $opt_in_js ) ) {
$result = false;
return $result;
}
$result = preg_match( '/