Skip to content

Commit 5103216

Browse files
authored
feat: new options to show authors and categories (#186)
* 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
1 parent 191a107 commit 5103216

File tree

6 files changed

+404
-114
lines changed

6 files changed

+404
-114
lines changed

includes/class-core.php

+74-1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@ public static function init() {
106106
self::create_shadow_relationship();
107107
}
108108

109+
/**
110+
* Is the current post a sponsor?
111+
*
112+
* @return boolean True if a sponsor.
113+
*/
114+
public static function is_sponsor() {
115+
return self::NEWSPACK_SPONSORS_CPT === get_post_type();
116+
}
117+
109118
/**
110119
* Registers Sponsors custom post type.
111120
*/
@@ -212,7 +221,6 @@ public static function register_meta() {
212221
'post',
213222
'newspack_sponsor_sponsorship_scope',
214223
[
215-
'object_subtype' => self::NEWSPACK_SPONSORS_CPT,
216224
'description' => __( 'Scope of sponsorship this sponsor offers (native content vs. underwritten).', 'newspack-sponsors' ),
217225
'type' => 'string',
218226
'sanitize_callback' => 'sanitize_text_field',
@@ -223,6 +231,66 @@ public static function register_meta() {
223231
},
224232
]
225233
);
234+
register_meta(
235+
'post',
236+
'newspack_sponsor_native_byline_display',
237+
[
238+
'description' => __( 'Display the sponsorship only, the author byline only, or both.', 'newspack-sponsors' ),
239+
'type' => 'string',
240+
'default' => self::is_sponsor() ? 'sponsor' : 'inherit',
241+
'sanitize_callback' => 'sanitize_text_field',
242+
'single' => true,
243+
'show_in_rest' => true,
244+
'auth_callback' => function() {
245+
return current_user_can( 'edit_posts' );
246+
},
247+
]
248+
);
249+
register_meta(
250+
'post',
251+
'newspack_sponsor_native_category_display',
252+
[
253+
'description' => __( 'Display the sponsor only, or display categories alongside the sponsor.', 'newspack-sponsors' ),
254+
'type' => 'string',
255+
'default' => self::is_sponsor() ? 'sponsor' : 'inherit',
256+
'sanitize_callback' => 'sanitize_text_field',
257+
'single' => true,
258+
'show_in_rest' => true,
259+
'auth_callback' => function() {
260+
return current_user_can( 'edit_posts' );
261+
},
262+
]
263+
);
264+
register_meta(
265+
'post',
266+
'newspack_sponsor_underwriter_style',
267+
[
268+
'description' => __( 'Display the underwriter blurb in standard or simple-text format.', 'newspack-sponsors' ),
269+
'type' => 'string',
270+
'default' => self::is_sponsor() ? 'standard' : 'inherit',
271+
'sanitize_callback' => 'sanitize_text_field',
272+
'single' => true,
273+
'show_in_rest' => true,
274+
'auth_callback' => function() {
275+
return current_user_can( 'edit_posts' );
276+
},
277+
]
278+
);
279+
register_meta(
280+
'post',
281+
'newspack_sponsor_underwriter_placement',
282+
[
283+
'description' => __( 'Display the underwriter blurb at the top or bottom of the post.', 'newspack-sponsors' ),
284+
'type' => 'string',
285+
'default' => self::is_sponsor() ? 'top' : 'inherit',
286+
'sanitize_callback' => 'sanitize_text_field',
287+
'single' => true,
288+
'show_in_rest' => true,
289+
'auth_callback' => function() {
290+
return current_user_can( 'edit_posts' );
291+
},
292+
]
293+
);
226294
register_meta(
227295
'post',
228296
'newspack_sponsor_only_direct',
@@ -268,6 +336,11 @@ public static function register_tax() {
268336
];
269337

270338
$tax_args = [
339+
'capabilities' => [
340+
'manage_terms' => '',
341+
'edit_terms' => '',
342+
'delete_terms' => '',
343+
],
271344
'hierarchical' => true,
272345
'labels' => $labels,
273346
'public' => true,

includes/class-editor.php

+14-10
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,19 @@ public static function disable_yoast_primary_category_picker( $taxonomies, $post
6868
return $taxonomies;
6969
}
7070

71-
/**
72-
* Is editing a sponsor?
73-
*/
74-
public static function is_editing_sponsor() {
75-
return Core::NEWSPACK_SPONSORS_CPT === get_post_type();
76-
}
77-
7871
/**
7972
* Load up JS/CSS for editor.
8073
*/
8174
public static function enqueue_block_editor_assets() {
82-
if ( ! self::is_editing_sponsor() && 'post' !== get_post_type() ) {
75+
$allowed_post_types = apply_filters(
76+
'newspack_sponsors_post_types',
77+
[ 'post', 'page' ]
78+
);
79+
80+
$allowed_post_types[] = Core::NEWSPACK_SPONSORS_CPT;
81+
82+
// Only enqueue assets for allowed post types.
83+
if ( ! in_array( get_post_type(), $allowed_post_types, true ) ) {
8384
return;
8485
}
8586

@@ -95,8 +96,11 @@ public static function enqueue_block_editor_assets() {
9596
'newspack-sponsors-editor',
9697
'newspack_sponsors_data',
9798
[
98-
'settings' => Settings::get_settings(),
99-
'defaults' => Settings::get_default_settings(),
99+
'post_type' => get_post_type(),
100+
'settings' => Settings::get_settings(),
101+
'defaults' => Settings::get_default_settings(),
102+
'cpt' => Core::NEWSPACK_SPONSORS_CPT,
103+
'tax' => Core::NEWSPACK_SPONSORS_TAX,
100104
]
101105
);
102106

includes/theme-helpers.php

+87-7
Original file line numberDiff line numberDiff line change
@@ -356,12 +356,16 @@ function convert_post_to_sponsor( $post, $type = 'direct', $logo_options = [] )
356356

357357
$sponsor_sitewide_settings = Settings::get_settings();
358358

359-
$sponsor_byline = get_post_meta( $post->ID, 'newspack_sponsor_byline_prefix', true );
360-
$sponsor_url = get_post_meta( $post->ID, 'newspack_sponsor_url', true );
361-
$sponsor_flag = get_post_meta( $post->ID, 'newspack_sponsor_flag_override', true );
362-
$sponsor_scope = get_post_meta( $post->ID, 'newspack_sponsor_sponsorship_scope', true );
363-
$sponsor_disclaimer = get_post_meta( $post->ID, 'newspack_sponsor_disclaimer_override', true );
364-
$sponsor_logo = get_logo_info( $post->ID, $logo_options );
359+
$sponsor_byline = get_post_meta( $post->ID, 'newspack_sponsor_byline_prefix', true );
360+
$sponsor_url = get_post_meta( $post->ID, 'newspack_sponsor_url', true );
361+
$sponsor_flag = get_post_meta( $post->ID, 'newspack_sponsor_flag_override', true );
362+
$sponsor_scope = get_post_meta( $post->ID, 'newspack_sponsor_sponsorship_scope', true );
363+
$sponsor_byline_display = get_post_meta( $post->ID, 'newspack_sponsor_native_byline_display', true );
364+
$sponsor_category_display = get_post_meta( $post->ID, 'newspack_sponsor_native_category_display', true );
365+
$sponsor_underwriter_style = get_post_meta( $post->ID, 'newspack_sponsor_underwriter_style', true );
366+
$sponsor_underwriter_placement = get_post_meta( $post->ID, 'newspack_sponsor_underwriter_placement', true );
367+
$sponsor_disclaimer = get_post_meta( $post->ID, 'newspack_sponsor_disclaimer_override', true );
368+
$sponsor_logo = get_logo_info( $post->ID, $logo_options );
365369

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

377-
return [
381+
$sponsor = [
378382
'sponsor_type' => $type,
379383
'sponsor_id' => $post->ID,
380384
'sponsor_name' => $post->post_title,
@@ -387,6 +391,16 @@ function convert_post_to_sponsor( $post, $type = 'direct', $logo_options = [] )
387391
'sponsor_scope' => ! empty( $sponsor_scope ) ? $sponsor_scope : 'native', // Default: native, not underwritten.
388392
'sponsor_disclaimer' => $sponsor_disclaimer,
389393
];
394+
395+
if ( 'native' === $sponsor['sponsor_scope'] ) {
396+
$sponsor['sponsor_byline_display'] = $sponsor_byline_display;
397+
$sponsor['sponsor_category_display'] = $sponsor_category_display;
398+
} else {
399+
$sponsor['sponsor_underwriter_style'] = $sponsor_underwriter_style;
400+
$sponsor['sponsor_underwriter_placement'] = $sponsor_underwriter_placement;
401+
}
402+
403+
return $sponsor;
390404
}
391405

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

424438
return $logo_info;
425439
}
440+
441+
/**
442+
* If at least one native sponsor is set to display both sponsors and authors, show the authors.
443+
*
444+
* @param array $sponsors Array of sponsors.
445+
*
446+
* @return boolean True if we should display both sponsors and categories, false if we should display only sponsors.
447+
*/
448+
function newspack_display_sponsors_and_authors( $sponsors ) {
449+
if ( ! is_array( $sponsors ) ) {
450+
return false;
451+
}
452+
453+
// If the post is set to display author, show it.
454+
$override = get_post_meta( get_the_ID(), 'newspack_sponsor_native_byline_display', true );
455+
if ( 'author' === $override ) {
456+
return true;
457+
}
458+
if ( 'sponsor' === $override ) {
459+
return false;
460+
}
461+
462+
return array_reduce(
463+
$sponsors,
464+
function( $acc, $sponsor ) {
465+
if ( isset( $sponsor['sponsor_byline_display'] ) && 'author' === $sponsor['sponsor_byline_display'] ) {
466+
$acc = true;
467+
}
468+
return $acc;
469+
},
470+
false
471+
);
472+
}
473+
474+
/**
475+
* If at least one native sponsor is set to display both sponsors and categories, show the categories.
476+
*
477+
* @param array $sponsors Array of sponsors.
478+
*
479+
* @return boolean True if we should display both sponsors and categories, false if we should display only sponsors.
480+
*/
481+
function newspack_display_sponsors_and_categories( $sponsors ) {
482+
if ( ! is_array( $sponsors ) ) {
483+
return false;
484+
}
485+
486+
// If the post is set to display categories, show them.
487+
$override = get_post_meta( get_the_ID(), 'newspack_sponsor_native_category_display', true );
488+
if ( 'category' === $override ) {
489+
return true;
490+
}
491+
if ( 'sponsor' === $override ) {
492+
return false;
493+
}
494+
495+
return array_reduce(
496+
$sponsors,
497+
function( $acc, $sponsor ) {
498+
if ( isset( $sponsor['sponsor_category_display'] ) && 'category' === $sponsor['sponsor_category_display'] ) {
499+
$acc = true;
500+
}
501+
return $acc;
502+
},
503+
false
504+
);
505+
}

src/editor/index.js

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,40 @@
11
/**
22
* WordPress dependencies
33
*/
4+
import { __ } from '@wordpress/i18n';
45
import { addFilter } from '@wordpress/hooks';
56
import { registerPlugin } from '@wordpress/plugins';
7+
import { PluginDocumentSettingPanel } from '@wordpress/edit-post';
68

79
/**
810
* Internal dependencies
911
*/
1012
import { Sidebar } from './sidebar';
1113
import { TaxonomyPanel } from './taxonomy-panel';
1214

15+
const { post_type: postType, cpt } = window.newspack_sponsors_data;
16+
1317
/**
1418
* Filter the PostTaxonomies component.
1519
*/
1620
addFilter( 'editor.PostTaxonomyType', 'newspack-sponsors-editor', TaxonomyPanel );
1721

1822
/**
1923
* Register plugin editor settings.
24+
* For sponsor posts, this is a separate sidebar panel.
25+
* For all other post types, it is pre-pended to the Sponsors sidebar panel.
2026
*/
21-
registerPlugin( 'newspack-sponsors-editor', {
22-
render: Sidebar,
23-
icon: null,
24-
} );
27+
if ( cpt === postType ) {
28+
registerPlugin( 'newspack-sponsors-editor', {
29+
render: () => (
30+
<PluginDocumentSettingPanel
31+
className="newspack-sponsors"
32+
name="newspack-sponsors"
33+
title={ __( 'Sponsor Settings', 'newspack-sponsors' ) }
34+
>
35+
<Sidebar />
36+
</PluginDocumentSettingPanel>
37+
),
38+
icon: null,
39+
} );
40+
}

0 commit comments

Comments
 (0)