Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a global setting to suppress ads on sponsored posts #68

Merged
merged 1 commit into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions includes/class-newspack-sponsors-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace Newspack_Sponsors;

use Newspack_Sponsors\Newspack_Sponsors_Settings as Settings;

defined( 'ABSPATH' ) || exit;

/**
Expand Down Expand Up @@ -59,8 +61,10 @@ public static function suppress_ads( $should_display, $post_id ) {
if ( ! is_single() ) {
return $should_display;
}
$sponsors = get_sponsors_for_post( $post_id );
if ( $sponsors && count( $sponsors ) ) {

$suppress_ads = Settings::get_settings( 'suppress' );
$sponsors = get_sponsors_for_post( $post_id );
if ( boolval( $suppress_ads ) && $sponsors && count( $sponsors ) ) {
return false;
}
return $should_display;
Expand Down
26 changes: 24 additions & 2 deletions includes/class-newspack-sponsors-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static function get_default_settings() {
),
get_bloginfo( 'name' )
),
'suppress' => false,
];

return $defaults;
Expand All @@ -48,14 +49,17 @@ public static function get_default_settings() {
/**
* Get current site-wide settings, or defaults if not set.
*
* @param string $setting Key name of a setting to retrieve. If given, only the matching setting's value will be returned.
*
* @return array Array of current site-wide settings.
*/
public static function get_settings() {
public static function get_settings( $setting = null ) {
$defaults = self::get_default_settings();
$settings = [
'byline' => get_option( 'newspack_sponsors_default_byline', $defaults['byline'] ),
'flag' => get_option( 'newspack_sponsors_default_flag', $defaults['flag'] ),
'disclaimer' => get_option( 'newspack_sponsors_default_disclaimer', $defaults['disclaimer'] ),
'suppress' => get_option( 'newspack_sponsors_suppress_ads', $defaults['suppress'] ),
];

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

if ( $setting && isset( $settings[ $setting ] ) ) {
return $settings[ $setting ];
}

return $settings;
}

Expand Down Expand Up @@ -95,6 +103,12 @@ public static function get_settings_list() {
'key' => 'newspack_sponsors_default_disclaimer',
'type' => 'textarea',
],
[
'label' => __( 'Suppress Ads for All Sponsored Posts', 'newspack-sponsors' ),
'value' => $defaults['suppress'],
'key' => 'newspack_sponsors_suppress_ads',
'type' => 'checkbox',
],
];
}

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

if ( 'textarea' === $type ) {
if ( 'checkbox' === $type ) {
printf(
'<input type="checkbox" id="%s" name="%s" %s />',
esc_attr( $key ),
esc_attr( $key ),
! empty( $value ) ? 'checked' : '',
esc_attr( $key )
);
} elseif ( 'textarea' === $type ) {
printf(
'<textarea id="%s" name="%s" class="widefat" rows="4">%s</textarea>',
esc_attr( $key ),
Expand Down