From 0df2bfda79928a1e440018d8c2f5fb026fc8a281 Mon Sep 17 00:00:00 2001 From: Adam Borowski Date: Mon, 13 Sep 2021 17:07:30 +0200 Subject: [PATCH] feat(ads): suppress ads on sponsored posts --- includes/class-newspack-sponsors-core.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/includes/class-newspack-sponsors-core.php b/includes/class-newspack-sponsors-core.php index b27214d..2d3b27f 100644 --- a/includes/class-newspack-sponsors-core.php +++ b/includes/class-newspack-sponsors-core.php @@ -46,6 +46,21 @@ public static function instance() { */ public function __construct() { add_action( 'init', [ __CLASS__, 'init' ] ); + add_filter( 'newspack_ads_should_display_for_post', [ __CLASS__, 'suppress_ads' ], 10, 2 ); + } + + /** + * Add Sponsors CPT to the CPTs which should have ads suppressed. + * + * @param bool $should_display Should ads be displayed on this post? + * @param int $post_id Post ID. + */ + public static function suppress_ads( $should_display, $post_id ) { + $sponsors = get_sponsors_for_post( $post_id ); + if ( $sponsors && count( $sponsors ) ) { + return false; + } + return $should_display; } /**