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: new global setting and post option to hide publish dates #57

Merged
merged 2 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 49 additions & 10 deletions includes/class-newspack-listings-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function __construct() {
add_filter( 'body_class', [ __CLASS__, 'set_template_class' ] );
add_action( 'save_post', [ __CLASS__, 'sync_post_meta' ], 10, 2 );
add_filter( 'newspack_listings_hide_author', [ __CLASS__, 'hide_author' ] );
add_filter( 'newspack_listings_hide_publish_date', [ __CLASS__, 'hide_publish_date' ] );
add_filter( 'newspack_theme_featured_image_post_types', [ __CLASS__, 'support_featured_image_options' ] );
register_activation_hook( NEWSPACK_LISTINGS_FILE, [ __CLASS__, 'activation_hook' ] );
}
Expand Down Expand Up @@ -267,7 +268,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
}

$all_meta_fields = [
'newspack_listings_contact_email' => [
'newspack_listings_contact_email' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
Expand Down Expand Up @@ -299,7 +300,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_contact_phone' => [
'newspack_listings_contact_phone' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
Expand Down Expand Up @@ -331,7 +332,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_contact_address' => [
'newspack_listings_contact_address' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
Expand Down Expand Up @@ -380,7 +381,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
],
],
],
'newspack_listings_business_hours' => [
'newspack_listings_business_hours' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
Expand Down Expand Up @@ -427,7 +428,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
],
],
],
'newspack_listings_locations' => [
'newspack_listings_locations' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
Expand Down Expand Up @@ -484,7 +485,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_event_start_date' => [
'newspack_listings_event_start_date' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
],
Expand All @@ -507,7 +508,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_price' => [
'newspack_listings_price' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
],
Expand All @@ -530,7 +531,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_hide_author' => [
'newspack_listings_hide_author' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
Expand All @@ -551,7 +552,28 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_hide_parents' => [
'newspack_listings_hide_publish_date' => [
'post_types' => [
self::NEWSPACK_LISTINGS_POST_TYPES['event'],
self::NEWSPACK_LISTINGS_POST_TYPES['generic'],
self::NEWSPACK_LISTINGS_POST_TYPES['marketplace'],
self::NEWSPACK_LISTINGS_POST_TYPES['place'],
],
'label' => __( 'Hide publish date', 'newspack-listings' ),
'settings' => [
'object_subtype' => $post_type,
'default' => boolval( Settings::get_settings( 'newspack_listings_hide_publish_date' ) ), // Configurable in plugin-wide settings.
'description' => __( 'Hide publish and updated dates for this listing', 'newspack-listings' ),
'type' => 'boolean',
'sanitize_callback' => 'rest_sanitize_boolean',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
],
],
'newspack_listings_hide_parents' => [
'post_types' => [
'page',
'post',
Expand All @@ -574,7 +596,7 @@ public static function get_meta_fields( $post_type = null, $field_names_only = f
},
],
],
'newspack_listings_hide_children' => [
'newspack_listings_hide_children' => [
'post_types' => [
'page',
'post',
Expand Down Expand Up @@ -701,6 +723,23 @@ public static function hide_author( $hide_author = false ) {
return $hide_author;
}

/**
* Filter callback to decide whether to show the publish and updated dates for the current singular post.
* Can be used from other plugins and/or themes to modify default template behavior.
*
* @param boolean $hide_publish_dates Whether or not to hide the publish and updated dates.
* @return boolean If the current post a.) is a listing and b.) has enabled the option to hide publish and updated dates.
*/
public static function hide_publish_date( $hide_publish_dates = false ) {
$post_id = get_the_ID();

if ( self::is_listing() && ! empty( get_post_meta( $post_id, 'newspack_listings_hide_publish_date', true ) ) ) {
return true;
}

return $hide_publish_dates;
}

/**
* If using a Newspack theme, respect the "default template" option setting in the Customizer.
*
Expand Down
7 changes: 7 additions & 0 deletions includes/class-newspack-listings-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public static function get_default_settings() {
'type' => 'checkbox',
'value' => true,
],
[
'description' => __( 'This setting can be overridden per listing.', 'newspack-listings' ),
'key' => 'newspack_listings_hide_publish_date',
'label' => __( 'Hide publish and updated dates for listings by default', 'newpack-listings' ),
'type' => 'checkbox',
'value' => true,
],
[
'description' => __( 'This setting can be overridden per listing, post, or page.', 'newspack-listings' ),
'key' => 'newspack_listings_hide_parents',
Expand Down
36 changes: 28 additions & 8 deletions src/editor/sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import './style.scss';

const SidebarComponent = ( { meta, updateMetaValue } ) => {
const { post_type_label: postTypeLabel, post_types: postTypes } = window.newspack_listings_data;
const { newspack_listings_hide_author } = meta;
const { newspack_listings_hide_author, newspack_listings_hide_publish_date } = meta;

if ( ! postTypes ) {
return null;
Expand All @@ -30,22 +30,42 @@ const SidebarComponent = ( { meta, updateMetaValue } ) => {
isListing() ? postTypeLabel : __( 'Newspack Listings', 'newspack-listings' )
) }
>
<p>
<em>
{ __( 'Overrides ', 'newspack-listings' ) }
<ExternalLink href="/wp-admin/admin.php?page=newspack-listings-settings-admin">
{ __( 'global settings', 'newspack-listings' ) }
</ExternalLink>
</em>
</p>
<PanelRow>
<ToggleControl
className={ 'newspack-listings__toggle-control' }
label={ __( 'Hide listing author', 'newspack-listings' ) }
help={ () => (
<p>
{ __( 'Overrides ', 'newspack-listings' ) }
<ExternalLink href="/wp-admin/admin.php?page=newspack-listings-settings-admin">
{ __( 'global settings', 'newspack-listings' ) }
</ExternalLink>
</p>
help={ sprintf(
__( '%s the author byline for this listing.', 'newspack-listings' ),
newspack_listings_hide_author
? __( 'Hide', 'newspack-listings' )
: __( 'Show', 'newspack-listings' )
) }
checked={ newspack_listings_hide_author }
onChange={ value => updateMetaValue( 'newspack_listings_hide_author', value ) }
/>
</PanelRow>
<PanelRow>
<ToggleControl
className={ 'newspack-listings__toggle-control' }
label={ __( 'Hide publish date', 'newspack-listings' ) }
help={ sprintf(
__( '%s the publish and updated dates for this listing.', 'newspack-listings' ),
newspack_listings_hide_publish_date
? __( 'Hide', 'newspack-listings' )
: __( 'Show', 'newspack-listings' )
) }
checked={ newspack_listings_hide_publish_date }
onChange={ value => updateMetaValue( 'newspack_listings_hide_publish_date', value ) }
/>
</PanelRow>
</PluginDocumentSettingPanel>
);
};
Expand Down