Skip to content

Commit e58d61c

Browse files
authored
feat: create filter to let other plugins add supported post types (#58)
* feat: create filter to let other plugins add supported post types * fix: update shadow term if post title is changed
1 parent 8410d5e commit e58d61c

File tree

2 files changed

+60
-11
lines changed

2 files changed

+60
-11
lines changed

includes/class-newspack-sponsors-core.php

+48-9
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,12 @@ public static function register_tax() {
230230
'show_ui' => true,
231231
];
232232

233-
register_taxonomy( self::NEWSPACK_SPONSORS_TAX, 'post', $tax_args );
233+
$post_types = apply_filters(
234+
'newspack_sponsors_post_types',
235+
[ 'post', 'page' ]
236+
);
237+
238+
register_taxonomy( self::NEWSPACK_SPONSORS_TAX, $post_types, $tax_args );
234239
}
235240

236241
/**
@@ -249,13 +254,46 @@ public static function create_shadow_relationship() {
249254
* @return void
250255
*/
251256
public static function update_or_delete_shadow_term( $post_id, $post ) {
252-
if ( 'publish' === $post->post_status ) {
257+
// If the post is a valid post, update or create the shadow term. Otherwise, delete it.
258+
if ( self::should_update_shadow_term( $post ) ) {
253259
self::update_shadow_term( $post_id, $post );
254260
} else {
255261
self::delete_shadow_term( $post_id );
256262
}
257263
}
258264

265+
/**
266+
* Check whether a given post object should have a shadow term.
267+
*
268+
* @param object $post Post object to check.
269+
* @return bool True if the post should have a shadow term, otherwise false.
270+
*/
271+
public static function should_update_shadow_term( $post ) {
272+
$should_update_shadow_term = true;
273+
274+
// If post isn't published.
275+
if ( 'publish' !== $post->post_status ) {
276+
$should_update_shadow_term = false;
277+
}
278+
279+
// If post lacks a valid title.
280+
if ( ! $post->post_title || 'Auto Draft' === $post->post_title ) {
281+
$should_update_shadow_term = false;
282+
}
283+
284+
// If post lacks a valid slug.
285+
if ( ! $post->post_name ) {
286+
$should_update_shadow_term = false;
287+
}
288+
289+
// If post isn't the right post type.
290+
if ( self::NEWSPACK_SPONSORS_CPT !== $post->post_type ) {
291+
return false;
292+
}
293+
294+
return $should_update_shadow_term;
295+
}
296+
259297
/**
260298
* Creates a new taxonomy term, or updates an existing one.
261299
*
@@ -265,12 +303,7 @@ public static function update_or_delete_shadow_term( $post_id, $post ) {
265303
*/
266304
public static function update_shadow_term( $post_id, $post ) {
267305
// Bail if we don't have a valid post or post type.
268-
if ( empty( $post ) || self::NEWSPACK_SPONSORS_CPT !== $post->post_type ) {
269-
return false;
270-
}
271-
272-
// Bail if post is an auto draft.
273-
if ( 'auto-draft' === $post->post_status || 'Auto Draft' === $post->post_title ) {
306+
if ( empty( $post ) ) {
274307
return false;
275308
}
276309

@@ -328,7 +361,13 @@ public static function get_shadow_term( $post ) {
328361
return false;
329362
}
330363

331-
$shadow_term = get_term_by( 'name', $post->post_title, self::NEWSPACK_SPONSORS_TAX );
364+
// Try finding the shadow term by slug first.
365+
$shadow_term = get_term_by( 'slug', $post->post_name, self::NEWSPACK_SPONSORS_TAX );
366+
367+
// If we can't find a term by slug, the post slug may have been updated. Try finding by title instead.
368+
if ( empty( $shadow_term ) ) {
369+
$shadow_term = get_term_by( 'name', $post->post_title, self::NEWSPACK_SPONSORS_TAX );
370+
}
332371

333372
if ( empty( $shadow_term ) ) {
334373
return false;

includes/newspack-sponsors-theme-helpers.php

+12-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ function get_all_sponsors( $id = null, $scope = null, $type = null, $logo_option
4242

4343
// If no type given, try to guess based on the current page context.
4444
if ( null === $type ) {
45-
if ( is_singular( 'post' ) ) {
45+
$post_types = apply_filters(
46+
'newspack_sponsors_post_types',
47+
[ 'post', 'page' ]
48+
);
49+
50+
if ( is_singular( $post_types ) ) {
4651
$type = 'post';
4752
} elseif ( is_archive() ) {
4853
$type = 'archive';
@@ -85,8 +90,13 @@ function get_sponsors_for_post( $post_id = null, $scope = null, $logo_options =
8590
return false;
8691
}
8792

93+
$post_types = apply_filters(
94+
'newspack_sponsors_post_types',
95+
[ 'post', 'page' ]
96+
);
97+
8898
if ( null === $post_id ) {
89-
if ( ! is_singular( 'post' ) ) {
99+
if ( ! is_singular( $post_types ) ) {
90100
return new WP_Error(
91101
'newspack-sponsors__is_not_post',
92102
__( 'Please provide a $post_id if not invoking within a single post.' )

0 commit comments

Comments
 (0)