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

Add $context to get_capability() #3866

Merged
merged 1 commit into from
Mar 20, 2024
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
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
Loading