Skip to content

Commit

Permalink
Merge pull request #3866 from 10up/feature/issue-3787
Browse files Browse the repository at this point in the history
Add $context to get_capability()
  • Loading branch information
felipeelia authored Mar 20, 2024
2 parents ff1ffee + 5333450 commit b7d2afa
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 22 deletions.
4 changes: 2 additions & 2 deletions includes/classes/Feature/Search/Synonyms.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function admin_menu() {
'elasticpress',
esc_html__( 'ElasticPress Synonyms', 'elasticpress' ),
esc_html__( 'Synonyms', 'elasticpress' ),
Utils\get_capability(),
Utils\get_capability( 'synonyms' ),
'elasticpress-synonyms',
[ $this, 'admin_page' ]
);
Expand Down Expand Up @@ -248,7 +248,7 @@ public function register_post_type() {
'show_ui' => false,
'show_in_menu' => false,
'query_var' => true,
'capabilities' => Utils\get_post_map_capabilities(),
'capabilities' => Utils\get_post_map_capabilities( 'synonyms' ),
'has_archive' => false,
'hierarchical' => false,
'menu_position' => 100,
Expand Down
4 changes: 2 additions & 2 deletions includes/classes/Feature/SearchOrdering/SearchOrdering.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public function admin_menu() {
'elasticpress',
esc_html__( 'Custom Results', 'elasticpress' ),
esc_html__( 'Custom Results', 'elasticpress' ),
Utils\get_capability(),
Utils\get_capability( 'search-ordering' ),
'edit.php?post_type=' . self::POST_TYPE_NAME
);
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public function register_post_type() {
'show_in_menu' => false,
'query_var' => true,
'rewrite' => array( 'slug' => 'ep-pointer' ),
'capabilities' => Utils\get_post_map_capabilities(),
'capabilities' => Utils\get_post_map_capabilities( 'search-ordering' ),
'has_archive' => false,
'hierarchical' => false,
'menu_position' => 100,
Expand Down
2 changes: 1 addition & 1 deletion includes/classes/REST/SearchOrdering.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function get_args() {
* @return boolean
*/
public function check_permission() {
$capability = Utils\get_capability();
$capability = Utils\get_capability( 'search-ordering' );

return current_user_can( $capability );
}
Expand Down
41 changes: 30 additions & 11 deletions includes/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,47 +54,66 @@ function get_epio_credentials() {
/**
* Get WP capability needed for a user to interact with ElasticPress in the admin
*
* @since 4.5.0
* @since 4.5.0, 5.1.0 added $context
* @param string $context Context for the capability. Defaults to empty string.
* @return string
*/
function get_capability() : string {
function get_capability( string $context = '' ) : string {
/**
* Filter the WP capability needed to interact with ElasticPress in the admin
*
* @since 4.5.0
* Example:
* ```
* add_filter(
* 'ep_capability',
* function ( $cacapability, $context ) {
* return ( 'synonyms' === $context ) ?
* 'manage_elasticpress_synonyms' :
* $cacapability;
* },
* 10,
* 2
* );
* ```
*
* @since 4.5.0, 5.1.0 added $context
* @hook ep_capability
* @param {string} $capability Capability name. Defaults to `'manage_elasticpress'`
* @param {string} $context Additional context
* @return {string} New capability value
*/
return apply_filters( 'ep_capability', 'manage_elasticpress' );
return apply_filters( 'ep_capability', 'manage_elasticpress', $context );
}

/**
* Get WP capability needed for a user to interact with ElasticPress in the network admin
*
* @since 4.5.0
* @since 4.5.0, 5.1.0 added $context
* @param string $context Context for the capability. Defaults to empty string.
* @return string
*/
function get_network_capability() : string {
function get_network_capability( string $context = '' ) : string {
/**
* Filter the WP capability needed to interact with ElasticPress in the network admin
*
* @since 4.5.0
* @since 4.5.0, 5.1.0 added $context
* @hook ep_network_capability
* @param {string} $capability Capability name. Defaults to `'manage_network_elasticpress'`
* @param {string} $context Additional context
* @return {string} New capability value
*/
return apply_filters( 'ep_network_capability', 'manage_network_elasticpress' );
return apply_filters( 'ep_network_capability', 'manage_network_elasticpress', $context );
}

/**
* Get mapped capabilities for post types
*
* @since 4.5.0
* @since 4.5.0, 5.1.0 added $context
* @param string $context Context for the capability. Defaults to empty string.
* @return array
*/
function get_post_map_capabilities() : array {
$capability = get_capability();
function get_post_map_capabilities( string $context = '' ) : array {
$capability = get_capability( $context );

return [
'edit_post' => $capability,
Expand Down
40 changes: 34 additions & 6 deletions tests/php/TestUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,14 @@ public function testGetCapability() {
/**
* Test the `ep_capability` filter.
*/
$change_cap_name = function( $cap ) {
$change_cap_name = function( $cap, $context ) {
$this->assertSame( 'manage_elasticpress', $cap );
$this->assertSame( 'context', $context );
return 'custom_manage_ep';
};
add_filter( 'ep_capability', $change_cap_name );
add_filter( 'ep_capability', $change_cap_name, 10, 2 );

$this->assertSame( 'custom_manage_ep', Utils\get_capability() );
$this->assertSame( 'custom_manage_ep', Utils\get_capability( 'context' ) );
}

/**
Expand All @@ -296,13 +297,14 @@ public function testGetNetworkCapability() {
/**
* Test the `ep_network_capability` filter.
*/
$change_cap_name = function( $cap ) {
$change_cap_name = function( $cap, $context ) {
$this->assertSame( 'manage_network_elasticpress', $cap );
$this->assertSame( 'context', $context );
return 'custom_manage_network_ep';
};
add_filter( 'ep_network_capability', $change_cap_name );
add_filter( 'ep_network_capability', $change_cap_name, 10, 2 );

$this->assertSame( 'custom_manage_network_ep', Utils\get_network_capability() );
$this->assertSame( 'custom_manage_network_ep', Utils\get_network_capability( 'context' ) );
}

/**
Expand All @@ -324,6 +326,32 @@ public function testGetPostMapCapabilities() {
$this->assertSame( $expected, Utils\get_post_map_capabilities() );
}

/**
* Test the `get_post_map_capabilities` function passing context
*
* @since 5.1.0
*/
public function test_get_post_map_capabilities_with_context() {
$change_cap_name = function( $cap, $context ) {
$this->assertSame( 'manage_elasticpress', $cap );
$this->assertSame( 'context', $context );
return 'custom_manage_ep';
};
add_filter( 'ep_capability', $change_cap_name, 10, 2 );

$expected = [
'edit_post' => 'custom_manage_ep',
'edit_posts' => 'custom_manage_ep',
'edit_others_posts' => 'custom_manage_ep',
'publish_posts' => 'custom_manage_ep',
'read_post' => 'custom_manage_ep',
'read_private_posts' => 'custom_manage_ep',
'delete_post' => 'custom_manage_ep',
];

$this->assertSame( $expected, Utils\get_post_map_capabilities( 'context' ) );
}

/**
* Test the `get_elasticsearch_error_reason` function
*
Expand Down

0 comments on commit b7d2afa

Please sign in to comment.