@@ -230,7 +230,12 @@ public static function register_tax() {
230
230
'show_ui ' => true ,
231
231
];
232
232
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 );
234
239
}
235
240
236
241
/**
@@ -249,13 +254,46 @@ public static function create_shadow_relationship() {
249
254
* @return void
250
255
*/
251
256
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 ) ) {
253
259
self ::update_shadow_term ( $ post_id , $ post );
254
260
} else {
255
261
self ::delete_shadow_term ( $ post_id );
256
262
}
257
263
}
258
264
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
+
259
297
/**
260
298
* Creates a new taxonomy term, or updates an existing one.
261
299
*
@@ -265,12 +303,7 @@ public static function update_or_delete_shadow_term( $post_id, $post ) {
265
303
*/
266
304
public static function update_shadow_term ( $ post_id , $ post ) {
267
305
// 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 ) ) {
274
307
return false ;
275
308
}
276
309
@@ -328,7 +361,13 @@ public static function get_shadow_term( $post ) {
328
361
return false ;
329
362
}
330
363
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
+ }
332
371
333
372
if ( empty ( $ shadow_term ) ) {
334
373
return false ;
0 commit comments