Skip to content

Commit

Permalink
Show author taxonomy in REST API, hide description based on capabilit…
Browse files Browse the repository at this point in the history
…ies (#931)

* enable author taxonomy in rest, hide description

* always use a capability, unset description

* update comment to match

* update filter name
  • Loading branch information
douglas-johnson authored May 29, 2023
1 parent 7fa409a commit fd94f7d
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions co-authors-plus.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ function __construct() {

// Block editor assets for the sidebar plugin.
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_sidebar_plugin_assets' ) );

// REST API: Depending on user capabilities, hide author term description.
add_action( 'rest_prepare_author', array( $this, 'conditionally_hide_author_term_description' ) );
}

/**
Expand Down Expand Up @@ -231,6 +234,8 @@ public function action_init_late() {
'sort' => true,
'args' => array( 'orderby' => 'term_order' ),
'show_ui' => false,
'show_in_rest' => true,
'rest_base' => 'coauthors',
);

// If we use the nasty SQL query, we need our custom callback. Otherwise, we still need to flush cache.
Expand Down Expand Up @@ -1833,6 +1838,39 @@ public function filter_pre_get_avatar_data_url( $args, $id ) {
}
return $args;
}

/**
* Conditionally Hide Author Term Description
*
* If the current user does not have the required capability,
* hide the author term description by unsetting it.
*
* @link https://github.com/Automattic/Co-Authors-Plus/issues/930
* @param WP_REST_Response $response Response for an individual author taxonomy term.
* @return WP_REST_Response $response Same response, possibly mutated to eliminate value of description.
*/
public function conditionally_hide_author_term_description( WP_REST_Response $response ) : WP_REST_Response {
$capability = apply_filters(
'coauthors_rest_view_description_cap',
'edit_posts'
);

if ( current_user_can( $capability ) ) {
return $response;
}

$data = $response->get_data();

if ( ! is_array( $data ) || ! array_key_exists( 'description', $data ) ) {
return $response;
}

unset( $data['description'] );

$response->set_data( $data );

return $response;
}
}

global $coauthors_plus;
Expand Down

0 comments on commit fd94f7d

Please sign in to comment.