Skip to content

Commit

Permalink
Add custom capability `manage_redirects’
Browse files Browse the repository at this point in the history
Add the custom capability and checks for it, as well as add it by default to `administrators` and `editors`.

Uses `wpcom_vip_add_role_caps` with fallbacks for non-vip environments.
  • Loading branch information
mikeyarce committed Apr 10, 2018
1 parent 4313bad commit 499e4e5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 3 additions & 4 deletions includes/class-wpcom-legacy-redirector-ui.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function admin_menu() {
'edit.php?post_type=vip-legacy-redirect',
__( 'Add Redirect', 'wpcom-legacy-redirector' ),
__( 'Add Redirect', 'wpcom-legacy-redirector' ),
'manage_options',
'manage_redirects',
'wpcom-legacy-redirector',
array( $this, 'generate_page_html' )
);
Expand Down Expand Up @@ -161,8 +161,7 @@ public function modify_list_row_actions( $actions, $post ) {
$trash = $actions['trash'];
$actions = array();

if ( current_user_can( 'manage_options' ) ) {

if ( current_user_can( 'manage_redirects' ) ) {
// Add a nonce to Validate Link
$validate_link = wp_nonce_url( add_query_arg( array(
'action' => 'validate',
Expand Down Expand Up @@ -306,7 +305,7 @@ public function validate_vip_legacy_redirect() {
}
}
public function generate_page_html() {
if ( ! current_user_can( 'manage_options' ) ) {
if ( ! current_user_can( 'manage_redirects' ) ) {
return;
}

Expand Down
14 changes: 14 additions & 0 deletions wpcom-legacy-redirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WPCOM_Legacy_Redirector {

static function start() {
add_action( 'init', array( __CLASS__, 'init' ) );
add_action( 'init', array( __CLASS__, 'register_redirect_custom_capability') );
add_filter( 'template_redirect', array( __CLASS__, 'maybe_do_redirect' ), 0 ); // hook in early, before the canonical redirect
add_action( 'admin_menu', array( new WPCOM_Legacy_Redirector_UI, 'admin_menu' ) );
load_plugin_textdomain( 'vip-legacy-redirect', false, basename( dirname( __FILE__ ) ) . '/languages' );
Expand Down Expand Up @@ -72,6 +73,19 @@ static function init() {
);
register_post_type( self::POST_TYPE, $args );
}
/**
* Register custom role using VIP Helpers with fallbacks.
*/
static function register_redirect_custom_capability() {
$cap = apply_filters( 'manage_redirect_capability', 'manage_redirects' );
if ( function_exists( 'wpcom_vip_add_role_caps' ) ) {
wpcom_vip_add_role_caps( 'administrator', $cap );
wpcom_vip_add_role_caps( 'editor', $cap );
} else {
$role = get_role( 'editor' );
$role->add_cap( $cap );
}
}

static function maybe_do_redirect() {
// Avoid the overhead of running this on every single pageload.
Expand Down

0 comments on commit 499e4e5

Please sign in to comment.