Skip to content

Commit

Permalink
WPCOM merge: list post type taxonomies endpoint (Automattic#9428)
Browse files Browse the repository at this point in the history
* REST API: CPT: Preserve custom taxonomy registrations when localizing

This revision seeks to resolve an issue where localized requests for taxonomies for themes where custom taxonomy associations exist will not preserve those associations.

For example, themes where "featured-content" is supported (e.g. Sketch) register the "post_tag" taxonomy for "page" types. If a request is issued with locale specified for this site to retrieve page taxonomies, previously the "post_tag" taxonomy was not returned.

The fix here is to track taxonomies for the requested post type before calling this method, then reapplying them after create_initial_taxonomies is called.

We need to call create_initial_taxonomies because it runs very early in the page lifecycle, before localization has had a chance to take effect.

See: Automattic/wp-calypso#6934 (comment)
Differential Revision: D3465

Merges r146417-wpcom.

* Moved post type taxonomies lister endpoint instantiation to the file that defines it.

Details: https://[private link]

Merges r161691-wpcom.
  • Loading branch information
nylen authored and dereksmart committed Apr 30, 2018
1 parent d069002 commit eeb7243
Showing 1 changed file with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ function callback( $path = '', $blog_id = 0, $post_type = 'post' ) {
$this->load_theme_functions();
}

/** This filter is documented in jetpack/json-endpoints/class.wpcom-json-api-list-post-types-endpoint.php */
if ( apply_filters( 'rest_api_localize_response', false ) ) {
// API localization occurs after the initial taxonomies have been
// registered, so re-register if localizing response
create_initial_taxonomies();
}
$this->localize_initial_taxonomies( $post_type );

$args = $this->query_args();

Expand Down Expand Up @@ -79,4 +74,25 @@ function callback( $path = '', $blog_id = 0, $post_type = 'post' ) {
'taxonomies' => $formatted_taxonomy_objects,
);
}

protected function localize_initial_taxonomies( $post_type ) {
/** This filter is documented in jetpack/json-endpoints/class.wpcom-json-api-list-post-types-endpoint.php */
if ( ! apply_filters( 'rest_api_localize_response', false ) ) {
return;
}

// Since recreating initial taxonomies will restore the default post
// types to which they are associated, save post type's taxonomies in
// case it was customized via `register_taxonomy_for_object_type`
$post_type_taxonomies = get_object_taxonomies( $post_type );

// API localization occurs after the initial taxonomies have been
// registered, so re-register if localizing response
create_initial_taxonomies();

// Restore registered taxonomies for post type
foreach ( $post_type_taxonomies as $taxonomy ) {
register_taxonomy_for_object_type( $taxonomy, $post_type );
}
}
}

0 comments on commit eeb7243

Please sign in to comment.