Skip to content

Commit ac533c5

Browse files
authored
feat: suppress ads on sponsored posts (#62)
1 parent e58d61c commit ac533c5

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

includes/class-newspack-sponsors-core.php

+15
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ public static function instance() {
4646
*/
4747
public function __construct() {
4848
add_action( 'init', [ __CLASS__, 'init' ] );
49+
add_filter( 'newspack_ads_should_show_ads', [ __CLASS__, 'suppress_ads' ], 10, 2 );
50+
}
51+
52+
/**
53+
* Disable ads if the post is sponsored.
54+
*
55+
* @param bool $should_display Should ads be displayed on this post.
56+
* @param int $post_id Post ID.
57+
*/
58+
public static function suppress_ads( $should_display, $post_id ) {
59+
$sponsors = get_sponsors_for_post( $post_id );
60+
if ( $sponsors && count( $sponsors ) ) {
61+
return false;
62+
}
63+
return $should_display;
4964
}
5065

5166
/**

includes/class-newspack-sponsors-editor.php

-27
Original file line numberDiff line numberDiff line change
@@ -44,37 +44,10 @@ public static function instance() {
4444
* Constructor.
4545
*/
4646
public function __construct() {
47-
add_action( 'the_post', [ __CLASS__, 'strip_editor_modifications' ] );
4847
add_filter( 'wpseo_primary_term_taxonomies', [ __CLASS__, 'disable_yoast_primary_category_picker' ], 10, 2 );
4948
add_action( 'enqueue_block_editor_assets', [ __CLASS__, 'enqueue_block_editor_assets' ] );
5049
}
5150

52-
/**
53-
* Remove certain editor enqueued assets which might not be compatible with this post type.
54-
*/
55-
public static function strip_editor_modifications() {
56-
if ( ! self::is_editing_sponsor() ) {
57-
return;
58-
}
59-
60-
$enqueue_block_editor_assets_filters = $GLOBALS['wp_filter']['enqueue_block_editor_assets']->callbacks;
61-
$disallowed_assets = [
62-
'Newspack_Popups::enqueue_block_editor_assets',
63-
'Newspack_Newsletters_Editor::enqueue_block_editor_assets',
64-
'Newspack_Ads_Blocks::enqueue_block_editor_assets',
65-
'newspack_ads_enqueue_suppress_ad_assets',
66-
];
67-
68-
foreach ( $enqueue_block_editor_assets_filters as $index => $filter ) {
69-
$action_handlers = array_keys( $filter );
70-
foreach ( $action_handlers as $handler ) {
71-
if ( in_array( $handler, $disallowed_assets ) ) {
72-
remove_action( 'enqueue_block_editor_assets', $handler, $index );
73-
}
74-
}
75-
}
76-
}
77-
7851
/**
7952
* Disable the Yoast primary category picker for Sponsor posts and terms.
8053
*

0 commit comments

Comments
 (0)