Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove "Add New" for shadow taxonomies #45

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions includes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

namespace ShadowTerms\API;

/**
* Determine if a taxonomy is a shadow taxonomy.
*
* @param string|\WP_Taxonomy $taxonomy Name of taxonomy, or the taxonomy object.
* @return bool
*/
function is_shadow_taxonomy( $taxonomy ) {

if ( $taxonomy instanceof \WP_Taxonomy ) {
$taxonomy = $taxonomy->name;
}

$is_shadow_taxonomy = strcasecmp( substr( $taxonomy, -8 ), '_connect' ) === 0;

return $is_shadow_taxonomy;
}

/**
* Retrieve a post's shadow taxonomy slug.
*
Expand Down
103 changes: 103 additions & 0 deletions includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@

use ShadowTerms\API;

use function ShadowTerms\API\get_connected_post_types;
use function ShadowTerms\API\is_shadow_taxonomy;

add_action( 'init', __NAMESPACE__ . '\register', 9999 );
add_action( 'rest_api_init', __NAMESPACE__ . '\register_route' );
add_action( 'rest_api_init', __NAMESPACE__ . '\remove_action_create_links' );
add_filter( 'rest_prepare_taxonomy', __NAMESPACE__ . '\rest_prepare_taxonomy', 10, 2 );

/**
* Register all shadow taxonomies.
Expand Down Expand Up @@ -179,3 +184,101 @@ function handle_rest_associate( \WP_REST_Request $request ): \WP_REST_Response {
]
);
}

/**
* Remove the `action-create-<shadow_taxonomy>` links for shadow taxonomies.
*
* The `action-create-<shadow_taxonomy>` link is used by the FlatTermSelector and HierarchicalTermSelector to determine if a term can be created.
*
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/editor/src/components/post-taxonomies/flat-term-selector.js#L82
* @see https://github.com/WordPress/gutenberg/blob/trunk/packages/editor/src/components/post-taxonomies/hierarchical-term-selector.js#L185
*
* @since 1.2.0
*
* @return void
*/
function remove_action_create_links() {

$post_types = get_post_types( [ 'show_in_rest' => true ] );
if ( empty ( $post_types ) ) {
return;
}

$filtered_post_types = [];
foreach ( $post_types as $post_type ) {

$connected_post_types = get_connected_post_types( $post_type );
if ( empty( $connected_post_types ) ) {
continue;
}

foreach ( $connected_post_types as $connected_post_type ) {
if ( in_array( $connected_post_type, $filtered_post_types, true ) ) {
continue;
}

$filtered_post_types[] = $connected_post_type;
}
}

if ( empty ( $filtered_post_types ) ) {
return;
}

foreach ( $filtered_post_types as $post_type ) {
add_filter( "rest_prepare_{$post_type}", __NAMESPACE__ . '\remove_action_create_link' );
}
}

/**
* Remove the `action-create-<shadow_taxonomy>` links for a shadow taxonomy.
*
* @since 1.2.0
*
* @param \WP_REST_Response $response The response object.
* @return \WP_REST_Response
*/
function remove_action_create_link( $response ) {

$links = $response->get_links();
if ( empty( $links ) ) {
return $response;
}

$pattern = '/^https:\/\/api\.w\.org\/action-create-([a-zA-Z0-9_]+)_connect$/';

foreach ( $links as $rel => $link ) {

if ( preg_match( $pattern, $rel, $matches ) ) {
$response->remove_link( $rel );
}
}

return $response;
}

/**
* Add shadow taxonomy identifier for REST responses.
*
* @since 1.2.0
*
* @param \WP_REST_Response $response The response object.
* @param \WP_Taxonomy $item The original taxonomy object.
* @return \WP_REST_Response
*/
function rest_prepare_taxonomy( $response, $taxonomy ) {

$data = $response->get_data();

if ( ! is_shadow_taxonomy( $taxonomy ) ) {
return $response;
}

if ( ! isset( $data['shadow_terms'] ) ) {
$data['shadow_terms'] = true;
}

$response = rest_ensure_response( $data );

return $response;
}