Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions includes/class-content-distribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,24 @@ public static function get_reserved_post_meta_keys() {
);
}

/**
* Get taxonomies that should not be distributed.
*
* @return string[] The reserved taxonomies.
*/
public static function get_reserved_taxonomies() {
$reserved_taxonomies = [
'author', // Co-Authors Plus 'author' taxonomy should be ignored as it requires custom handling.
];

/**
* Filters the reserved taxonomies that should not be distributed.
*
* @param string[] $reserved_taxonomies The reserved taxonomies.
*/
return apply_filters( 'newspack_network_content_distribution_reserved_taxonomies', $reserved_taxonomies );
}

/**
* Whether a given post is distributed.
*
Expand Down
10 changes: 7 additions & 3 deletions includes/content-distribution/class-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,16 +288,20 @@ protected function upload_thumbnail() {
* @return void
*/
protected function update_taxonomy_terms() {
$data = $this->payload['post_data']['taxonomy'];
$reserved_taxonomies = Content_Distribution::get_reserved_taxonomies();
$data = $this->payload['post_data']['taxonomy'];
foreach ( $data as $taxonomy => $terms ) {
if ( in_array( $taxonomy, $reserved_taxonomies, true ) ) {
continue;
}
if ( ! taxonomy_exists( $taxonomy ) ) {
continue;
}
$term_ids = [];
foreach ( $terms as $term_data ) {
$term = get_term_by( 'slug', $term_data['slug'], $taxonomy, ARRAY_A );
$term = get_term_by( 'name', $term_data['name'], $taxonomy, ARRAY_A );
if ( ! $term ) {
$term = wp_insert_term( $term_data['name'], $taxonomy, [ 'slug' => $term_data['slug'] ] );
$term = wp_insert_term( $term_data['name'], $taxonomy );
if ( is_wp_error( $term ) ) {
continue;
}
Expand Down
8 changes: 6 additions & 2 deletions includes/content-distribution/class-outgoing-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,13 @@ protected function get_processed_post_content() {
* @return array The taxonomy term data.
*/
protected function get_post_taxonomy_terms() {
$taxonomies = get_object_taxonomies( $this->post->post_type, 'objects' );
$data = [];
$reserved_taxonomies = Content_Distribution::get_reserved_taxonomies();
$taxonomies = get_object_taxonomies( $this->post->post_type, 'objects' );
$data = [];
foreach ( $taxonomies as $taxonomy ) {
if ( in_array( $taxonomy->name, $reserved_taxonomies, true ) ) {
continue;
}
if ( ! $taxonomy->public ) {
continue;
}
Expand Down
25 changes: 25 additions & 0 deletions tests/unit-tests/test-incoming-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,29 @@ public function test_post_meta_sync() {
// Assert that the custom post meta was removed on relink.
$this->assertEmpty( get_post_meta( $post_id, 'custom', true ) );
}

/**
* Test reserved taxonomies.
*/
public function test_reserved_taxonomies() {
$payload = $this->get_sample_payload();
$taxonomy = 'author';

// Register a reserved taxonomy.
register_taxonomy( $taxonomy, 'post', [ 'public' => true ] );

$payload['post_data']['taxonomy']['author'] = [
[
'name' => 'Author 1',
'slug' => 'author-1',
],
];

// Insert the linked post.
$post_id = $this->incoming_post->insert( $payload );

// Assert that the post does not have the reserved taxonomy term.
$terms = wp_get_post_terms( $post_id, $taxonomy );
$this->assertEmpty( $terms );
}
}
15 changes: 15 additions & 0 deletions tests/unit-tests/test-outgoing-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,19 @@ public function test_post_meta() {
$this->assertSame( 'a', $payload['post_data']['post_meta'][ $multiple_meta_key ][0] );
$this->assertSame( 'b', $payload['post_data']['post_meta'][ $multiple_meta_key ][1] );
}

/**
* Test reserved taxonomies.
*/
public function test_reserved_taxonomies() {
$post = $this->outgoing_post->get_post();
$taxonomy = 'author';
register_taxonomy( $taxonomy, 'post', [ 'public' => true ] );

$term = $this->factory->term->create( [ 'taxonomy' => $taxonomy ] );
wp_set_post_terms( $post->ID, [ $term ], $taxonomy );

$payload = $this->outgoing_post->get_payload();
$this->assertTrue( empty( $payload['post_data']['taxonomy'][ $taxonomy ] ) );
}
}
Loading