Skip to content

Commit 6ab54ce

Browse files
authored
feat: ad targeting for gpt (#180)
1 parent 7834583 commit 6ab54ce

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

includes/class-newspack-sponsors-core.php

+26
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static function instance() {
4949
public function __construct() {
5050
add_action( 'init', [ __CLASS__, 'init' ] );
5151
add_filter( 'newspack_ads_should_show_ads', [ __CLASS__, 'suppress_ads' ], 10, 2 );
52+
add_filter( 'newspack_ads_ad_targeting', [ __CLASS__, 'ad_targeting' ], 10, 2 );
5253
}
5354

5455
/**
@@ -70,6 +71,31 @@ public static function suppress_ads( $should_display, $post_id ) {
7071
return $should_display;
7172
}
7273

74+
/**
75+
* Set ad targeting for sponsored posts.
76+
*
77+
* @param array $targeting Ad targeting.
78+
*
79+
* @return array
80+
*/
81+
public static function ad_targeting( $targeting ) {
82+
$sponsors = [];
83+
if ( is_singular() ) {
84+
$sponsors = get_sponsors_for_post( get_the_ID() );
85+
} elseif ( is_archive() ) {
86+
$sponsors = get_sponsors_for_archive();
87+
}
88+
if ( ! is_wp_error( $sponsors ) && ! empty( $sponsors ) && ! isset( $targeting['sponsors'] ) ) {
89+
$targeting['sponsors'] = array_map(
90+
function( $sponsor ) {
91+
return $sponsor['sponsor_slug'];
92+
},
93+
$sponsors
94+
);
95+
}
96+
return $targeting;
97+
}
98+
7399
/**
74100
* After WP init.
75101
*/

0 commit comments

Comments
 (0)