Skip to content

Commit

Permalink
feat: new options to show authors and categories (#186)
Browse files Browse the repository at this point in the history
* refactor: simplify class names with namespaces

* feat: new options for bylines, categories, and underwriter style/placement

* feat: move override logic to plugin theme helpers

* fix: properly restrict adding/deleting of sponsor terms

* refactor: combine sponsor override settings with sponsors taxonomy panel

* chore: update help description for sponsor URL setting

* refactor: update name of localized variable, to avoid confusion

* fix: negative override
  • Loading branch information
dkoo authored Jul 14, 2022
1 parent 191a107 commit 5103216
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 114 deletions.
75 changes: 74 additions & 1 deletion includes/class-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@ public static function init() {
self::create_shadow_relationship();
}

/**
* Is the current post a sponsor?
*
* @return boolean True if a sponsor.
*/
public static function is_sponsor() {
return self::NEWSPACK_SPONSORS_CPT === get_post_type();
}

/**
* Registers Sponsors custom post type.
*/
Expand Down Expand Up @@ -212,7 +221,6 @@ public static function register_meta() {
'post',
'newspack_sponsor_sponsorship_scope',
[
'object_subtype' => self::NEWSPACK_SPONSORS_CPT,
'description' => __( 'Scope of sponsorship this sponsor offers (native content vs. underwritten).', 'newspack-sponsors' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
Expand All @@ -223,6 +231,66 @@ public static function register_meta() {
},
]
);
register_meta(
'post',
'newspack_sponsor_native_byline_display',
[
'description' => __( 'Display the sponsorship only, the author byline only, or both.', 'newspack-sponsors' ),
'type' => 'string',
'default' => self::is_sponsor() ? 'sponsor' : 'inherit',
'sanitize_callback' => 'sanitize_text_field',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
]
);
register_meta(
'post',
'newspack_sponsor_native_category_display',
[
'description' => __( 'Display the sponsor only, or display categories alongside the sponsor.', 'newspack-sponsors' ),
'type' => 'string',
'default' => self::is_sponsor() ? 'sponsor' : 'inherit',
'sanitize_callback' => 'sanitize_text_field',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
]
);
register_meta(
'post',
'newspack_sponsor_underwriter_style',
[
'description' => __( 'Display the underwriter blurb in standard or simple-text format.', 'newspack-sponsors' ),
'type' => 'string',
'default' => self::is_sponsor() ? 'standard' : 'inherit',
'sanitize_callback' => 'sanitize_text_field',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
]
);
register_meta(
'post',
'newspack_sponsor_underwriter_placement',
[
'description' => __( 'Display the underwriter blurb at the top or bottom of the post.', 'newspack-sponsors' ),
'type' => 'string',
'default' => self::is_sponsor() ? 'top' : 'inherit',
'sanitize_callback' => 'sanitize_text_field',
'single' => true,
'show_in_rest' => true,
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
]
);
register_meta(
'post',
'newspack_sponsor_only_direct',
Expand Down Expand Up @@ -268,6 +336,11 @@ public static function register_tax() {
];

$tax_args = [
'capabilities' => [
'manage_terms' => '',
'edit_terms' => '',
'delete_terms' => '',
],
'hierarchical' => true,
'labels' => $labels,
'public' => true,
Expand Down
24 changes: 14 additions & 10 deletions includes/class-editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,19 @@ public static function disable_yoast_primary_category_picker( $taxonomies, $post
return $taxonomies;
}

/**
* Is editing a sponsor?
*/
public static function is_editing_sponsor() {
return Core::NEWSPACK_SPONSORS_CPT === get_post_type();
}

/**
* Load up JS/CSS for editor.
*/
public static function enqueue_block_editor_assets() {
if ( ! self::is_editing_sponsor() && 'post' !== get_post_type() ) {
$allowed_post_types = apply_filters(
'newspack_sponsors_post_types',
[ 'post', 'page' ]
);

$allowed_post_types[] = Core::NEWSPACK_SPONSORS_CPT;

// Only enqueue assets for allowed post types.
if ( ! in_array( get_post_type(), $allowed_post_types, true ) ) {
return;
}

Expand All @@ -95,8 +96,11 @@ public static function enqueue_block_editor_assets() {
'newspack-sponsors-editor',
'newspack_sponsors_data',
[
'settings' => Settings::get_settings(),
'defaults' => Settings::get_default_settings(),
'post_type' => get_post_type(),
'settings' => Settings::get_settings(),
'defaults' => Settings::get_default_settings(),
'cpt' => Core::NEWSPACK_SPONSORS_CPT,
'tax' => Core::NEWSPACK_SPONSORS_TAX,
]
);

Expand Down
94 changes: 87 additions & 7 deletions includes/theme-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,16 @@ function convert_post_to_sponsor( $post, $type = 'direct', $logo_options = [] )

$sponsor_sitewide_settings = Settings::get_settings();

$sponsor_byline = get_post_meta( $post->ID, 'newspack_sponsor_byline_prefix', true );
$sponsor_url = get_post_meta( $post->ID, 'newspack_sponsor_url', true );
$sponsor_flag = get_post_meta( $post->ID, 'newspack_sponsor_flag_override', true );
$sponsor_scope = get_post_meta( $post->ID, 'newspack_sponsor_sponsorship_scope', true );
$sponsor_disclaimer = get_post_meta( $post->ID, 'newspack_sponsor_disclaimer_override', true );
$sponsor_logo = get_logo_info( $post->ID, $logo_options );
$sponsor_byline = get_post_meta( $post->ID, 'newspack_sponsor_byline_prefix', true );
$sponsor_url = get_post_meta( $post->ID, 'newspack_sponsor_url', true );
$sponsor_flag = get_post_meta( $post->ID, 'newspack_sponsor_flag_override', true );
$sponsor_scope = get_post_meta( $post->ID, 'newspack_sponsor_sponsorship_scope', true );
$sponsor_byline_display = get_post_meta( $post->ID, 'newspack_sponsor_native_byline_display', true );
$sponsor_category_display = get_post_meta( $post->ID, 'newspack_sponsor_native_category_display', true );
$sponsor_underwriter_style = get_post_meta( $post->ID, 'newspack_sponsor_underwriter_style', true );
$sponsor_underwriter_placement = get_post_meta( $post->ID, 'newspack_sponsor_underwriter_placement', true );
$sponsor_disclaimer = get_post_meta( $post->ID, 'newspack_sponsor_disclaimer_override', true );
$sponsor_logo = get_logo_info( $post->ID, $logo_options );

// Check for single-sponsor overrides, default to site-wide options.
if ( empty( $sponsor_byline ) ) {
Expand All @@ -374,7 +378,7 @@ function convert_post_to_sponsor( $post, $type = 'direct', $logo_options = [] )
$sponsor_disclaimer = str_replace( '[sponsor name]', $post->post_title, $sponsor_sitewide_settings['disclaimer'] );
}

return [
$sponsor = [
'sponsor_type' => $type,
'sponsor_id' => $post->ID,
'sponsor_name' => $post->post_title,
Expand All @@ -387,6 +391,16 @@ function convert_post_to_sponsor( $post, $type = 'direct', $logo_options = [] )
'sponsor_scope' => ! empty( $sponsor_scope ) ? $sponsor_scope : 'native', // Default: native, not underwritten.
'sponsor_disclaimer' => $sponsor_disclaimer,
];

if ( 'native' === $sponsor['sponsor_scope'] ) {
$sponsor['sponsor_byline_display'] = $sponsor_byline_display;
$sponsor['sponsor_category_display'] = $sponsor_category_display;
} else {
$sponsor['sponsor_underwriter_style'] = $sponsor_underwriter_style;
$sponsor['sponsor_underwriter_placement'] = $sponsor_underwriter_placement;
}

return $sponsor;
}

/**
Expand Down Expand Up @@ -423,3 +437,69 @@ function get_logo_info( $sponsor_id, $logo_options = [] ) {

return $logo_info;
}

/**
* If at least one native sponsor is set to display both sponsors and authors, show the authors.
*
* @param array $sponsors Array of sponsors.
*
* @return boolean True if we should display both sponsors and categories, false if we should display only sponsors.
*/
function newspack_display_sponsors_and_authors( $sponsors ) {
if ( ! is_array( $sponsors ) ) {
return false;
}

// If the post is set to display author, show it.
$override = get_post_meta( get_the_ID(), 'newspack_sponsor_native_byline_display', true );
if ( 'author' === $override ) {
return true;
}
if ( 'sponsor' === $override ) {
return false;
}

return array_reduce(
$sponsors,
function( $acc, $sponsor ) {
if ( isset( $sponsor['sponsor_byline_display'] ) && 'author' === $sponsor['sponsor_byline_display'] ) {
$acc = true;
}
return $acc;
},
false
);
}

/**
* If at least one native sponsor is set to display both sponsors and categories, show the categories.
*
* @param array $sponsors Array of sponsors.
*
* @return boolean True if we should display both sponsors and categories, false if we should display only sponsors.
*/
function newspack_display_sponsors_and_categories( $sponsors ) {
if ( ! is_array( $sponsors ) ) {
return false;
}

// If the post is set to display categories, show them.
$override = get_post_meta( get_the_ID(), 'newspack_sponsor_native_category_display', true );
if ( 'category' === $override ) {
return true;
}
if ( 'sponsor' === $override ) {
return false;
}

return array_reduce(
$sponsors,
function( $acc, $sponsor ) {
if ( isset( $sponsor['sponsor_category_display'] ) && 'category' === $sponsor['sponsor_category_display'] ) {
$acc = true;
}
return $acc;
},
false
);
}
24 changes: 20 additions & 4 deletions src/editor/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { addFilter } from '@wordpress/hooks';
import { registerPlugin } from '@wordpress/plugins';
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';

/**
* Internal dependencies
*/
import { Sidebar } from './sidebar';
import { TaxonomyPanel } from './taxonomy-panel';

const { post_type: postType, cpt } = window.newspack_sponsors_data;

/**
* Filter the PostTaxonomies component.
*/
addFilter( 'editor.PostTaxonomyType', 'newspack-sponsors-editor', TaxonomyPanel );

/**
* Register plugin editor settings.
* For sponsor posts, this is a separate sidebar panel.
* For all other post types, it is pre-pended to the Sponsors sidebar panel.
*/
registerPlugin( 'newspack-sponsors-editor', {
render: Sidebar,
icon: null,
} );
if ( cpt === postType ) {
registerPlugin( 'newspack-sponsors-editor', {
render: () => (
<PluginDocumentSettingPanel
className="newspack-sponsors"
name="newspack-sponsors"
title={ __( 'Sponsor Settings', 'newspack-sponsors' ) }
>
<Sidebar />
</PluginDocumentSettingPanel>
),
icon: null,
} );
}
Loading

0 comments on commit 5103216

Please sign in to comment.