Skip to content

Instantly share code, notes, and snippets.

@thefrosty
Created May 4, 2017 17:32
Show Gist options
  • Save thefrosty/019e13d50169fd6fd4a02d418b7aafe8 to your computer and use it in GitHub Desktop.
Save thefrosty/019e13d50169fd6fd4a02d418b7aafe8 to your computer and use it in GitHub Desktop.
Dovedi and WP User Profiles support.
<?php
namespace Dwnload\Plugins\Dovedi;
/**
* Class Add2faMetaBox
*
* @package Dwnload\Plugins\Dovedi
*/
class Add2faMetaBox {
const ACCOUNT = 'account';
/**
* Add class hooks.
*/
public function addHooks() {
add_action( 'wp_user_profiles_add_' . self::ACCOUNT . '_meta_boxes', [ $this, 'addMetaBox' ], 10, 2 );
add_action( 'admin_notices', [ $this, 'addEditUserFilter' ], 9 );
add_action( 'admin_notices', [ $this, 'removeEditUserFilter' ], 11 );
}
/**
* Add the meta boxes for this section.
*
* @param string $type
* @param \WP_User $user
*/
public function addMetaBox( $type = '', $user = null ) {
if ( function_exists( '\EAMann\Dovedi\Core\user_options' ) ) {
add_meta_box(
'dovedi-2fa',
_x( '2FA', 'users user-admin edit screen', 'wp-user-profiles' ),
'\EAMann\Dovedi\Core\user_options',
$type,
'normal',
'core',
$user
);
}
}
/**
* Add the `get_edit_user_link` filter before 'Dovedi' outputs its admin notice.
*/
public function addEditUserFilter() {
add_filter( 'get_edit_user_link', [ $this, 'getEditUserLink' ], 10, 3 );
}
/**
* Remove the `get_edit_user_link` filter after the notice has been output.
*/
public function removeEditUserFilter() {
remove_filter( 'get_edit_user_link', [ $this, 'getEditUserLink' ], 10 );
}
/**
* Modify the edit profile link to the 'account' page instead of the profile page.
*
* @param string $url
* @param int $user_id
* @param string $scheme
*
* @return string
*/
public function getEditUserLink( $url = '', $user_id = 0, $scheme = '' ): string {
$url = wp_user_profiles_get_admin_area_url( $user_id, $scheme );
return add_query_arg( [
'page' => self::ACCOUNT,
], $url );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment