Created
September 5, 2019 19:55
-
-
Save BrianHenryIE/eb33e398c12a3cb2fde323b637d2a34c to your computer and use it in GitHub Desktop.
WP-E-Signature (Approve Me) admin permissions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Sets/overwrites the super-admin user-id in the e-signature cache. | |
* | |
* Allows editing documents and saving documents as PDF, | |
* but not previewing documents in draft or viewing signed documents in the browser. | |
*/ | |
function bh_set_esig_super_admin() { | |
/** | |
* Seems to be done as the plugin files are loaded, so this is just precautionary. | |
* | |
* @see WP_E_Digital_Signature::setup_constants() | |
*/ | |
if ( ! defined( 'ESIG_CACHE_GROUP' ) ) { | |
define( 'ESIG_CACHE_GROUP', 'esig' ); | |
} | |
/** | |
* Get the current user so we can check are they an administrator. | |
* | |
* @var WP_User $user The currently logged in user. | |
*/ | |
$user = wp_get_current_user(); | |
/** | |
* If we're in the admin UI and the user is an administrator. | |
*/ | |
if ( is_admin() && in_array( 'administrator', $user->roles, true ) ) { | |
/** | |
* Set/overwrite the cache entry for the e-sig super-admin to the current user's user-id. | |
* | |
* @see WP_E_User::esig_get_super_admin_id() | |
*/ | |
wp_cache_set( 'esig_superadmin_user', $user->ID, ESIG_CACHE_GROUP ); | |
} | |
} | |
add_action( 'init', 'bh_set_esig_super_admin' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Uses User Switching plugin to change the My Documents / Signed link to switch into the super admin's profile first. | |
* | |
* @see WP_E_DocumentsController::index() $template_data['manage_signed_url'] | |
* | |
* @param array $template_data | |
* | |
* @return array | |
*/ | |
function bh_user_swithing_signed_docs_url( $template_data ) { | |
if ( ! class_exists( 'user_switching' ) ) { | |
return $template_data; | |
} | |
if ( ! class_exists( 'WP_E_Digital_Signature' ) ) { | |
return $template_data; | |
} | |
$current_user = wp_get_current_user(); | |
if ( is_admin() && in_array( 'administrator', $current_user->roles, true ) ) { | |
// Clear the cache so the correct user id is returned. | |
wp_cache_set( 'esig_superadmin_user', false, ESIG_CACHE_GROUP ); | |
/** | |
* E-Sig doc comment suggests this is void but the function returns $_instance. | |
*/ | |
$esig = WP_E_Digital_Signature::instance(); | |
/** | |
* The e signature user helper class. | |
* | |
* @var WP_E_User | |
*/ | |
$esig_user = $esig->user; | |
$super_admin_user_id = $esig_user->esig_get_super_admin_id(); | |
if ( $super_admin_user_id !== $current_user->id ) { | |
$super_admin_user = get_user_by( 'id', $super_admin_user_id ); | |
$super_admin_login_url = user_switching::maybe_switch_url( $super_admin_user ); | |
$redirect_to = get_admin_url() . $template_data['manage_signed_url']; | |
$super_admin_login_manage_signed_docs_url = add_query_arg( 'redirect_to', $redirect_to, $super_admin_login_url ); | |
$template_data['manage_signed_url'] = $super_admin_login_manage_signed_docs_url; | |
} | |
// Set the super-admin to the current user. | |
bh_set_esig_super_admin(); | |
} | |
return $template_data; | |
} | |
add_filter( 'esig-document-index-data', 'bh_user_swithing_signed_docs_url' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment