Skip to content

Commit ce45916

Browse files
authored
feat: add a global setting to suppress ads on sponsored posts (#68)
1 parent c555e57 commit ce45916

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

includes/class-newspack-sponsors-core.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
namespace Newspack_Sponsors;
1212

13+
use Newspack_Sponsors\Newspack_Sponsors_Settings as Settings;
14+
1315
defined( 'ABSPATH' ) || exit;
1416

1517
/**
@@ -59,8 +61,10 @@ public static function suppress_ads( $should_display, $post_id ) {
5961
if ( ! is_single() ) {
6062
return $should_display;
6163
}
62-
$sponsors = get_sponsors_for_post( $post_id );
63-
if ( $sponsors && count( $sponsors ) ) {
64+
65+
$suppress_ads = Settings::get_settings( 'suppress' );
66+
$sponsors = get_sponsors_for_post( $post_id );
67+
if ( boolval( $suppress_ads ) && $sponsors && count( $sponsors ) ) {
6468
return false;
6569
}
6670
return $should_display;

includes/class-newspack-sponsors-settings.php

+24-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public static function get_default_settings() {
4040
),
4141
get_bloginfo( 'name' )
4242
),
43+
'suppress' => false,
4344
];
4445

4546
return $defaults;
@@ -48,14 +49,17 @@ public static function get_default_settings() {
4849
/**
4950
* Get current site-wide settings, or defaults if not set.
5051
*
52+
* @param string $setting Key name of a setting to retrieve. If given, only the matching setting's value will be returned.
53+
*
5154
* @return array Array of current site-wide settings.
5255
*/
53-
public static function get_settings() {
56+
public static function get_settings( $setting = null ) {
5457
$defaults = self::get_default_settings();
5558
$settings = [
5659
'byline' => get_option( 'newspack_sponsors_default_byline', $defaults['byline'] ),
5760
'flag' => get_option( 'newspack_sponsors_default_flag', $defaults['flag'] ),
5861
'disclaimer' => get_option( 'newspack_sponsors_default_disclaimer', $defaults['disclaimer'] ),
62+
'suppress' => get_option( 'newspack_sponsors_suppress_ads', $defaults['suppress'] ),
5963
];
6064

6165
// Guard against empty strings, which can happen if an option is set and then unset.
@@ -65,6 +69,10 @@ public static function get_settings() {
6569
}
6670
}
6771

72+
if ( $setting && isset( $settings[ $setting ] ) ) {
73+
return $settings[ $setting ];
74+
}
75+
6876
return $settings;
6977
}
7078

@@ -95,6 +103,12 @@ public static function get_settings_list() {
95103
'key' => 'newspack_sponsors_default_disclaimer',
96104
'type' => 'textarea',
97105
],
106+
[
107+
'label' => __( 'Suppress Ads for All Sponsored Posts', 'newspack-sponsors' ),
108+
'value' => $defaults['suppress'],
109+
'key' => 'newspack_sponsors_suppress_ads',
110+
'type' => 'checkbox',
111+
],
98112
];
99113
}
100114

@@ -166,7 +180,15 @@ public static function newspack_sponsors_settings_callback( $setting ) {
166180
$type = $setting['type'];
167181
$value = ( '' !== get_option( $key, false ) ) ? get_option( $key, false ) : $setting['value'];
168182

169-
if ( 'textarea' === $type ) {
183+
if ( 'checkbox' === $type ) {
184+
printf(
185+
'<input type="checkbox" id="%s" name="%s" %s />',
186+
esc_attr( $key ),
187+
esc_attr( $key ),
188+
! empty( $value ) ? 'checked' : '',
189+
esc_attr( $key )
190+
);
191+
} elseif ( 'textarea' === $type ) {
170192
printf(
171193
'<textarea id="%s" name="%s" class="widefat" rows="4">%s</textarea>',
172194
esc_attr( $key ),

0 commit comments

Comments
 (0)