| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class Admin |
| 4 |
* |
| 5 |
* @package ContentControl\Vendor\TrustedLogin\Client |
| 6 |
* |
| 7 |
* @copyright 2021 Katz Web Services, Inc. |
| 8 |
* |
| 9 |
* @license GPL-2.0-or-later |
| 10 |
* Modified by code-atlantic on 18-September-2023 using Strauss. |
| 11 |
* @see https://github.com/BrianHenryIE/strauss |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace ContentControl\Vendor\TrustedLogin; |
| 15 |
|
| 16 |
// Exit if accessed directly |
| 17 |
if ( ! defined( 'ABSPATH' ) ) { |
| 18 |
exit; |
| 19 |
} |
| 20 |
|
| 21 |
use \WP_User; |
| 22 |
use \WP_Admin_Bar; |
| 23 |
|
| 24 |
final class Admin { |
| 25 |
|
| 26 |
/** |
| 27 |
* URL pointing to the "About TrustedLogin" page, shown below the Grant Access dialog |
| 28 |
*/ |
| 29 |
const ABOUT_TL_URL = 'https://www.trustedlogin.com/about/easy-and-safe/'; |
| 30 |
|
| 31 |
const ABOUT_LIVE_ACCESS_URL = 'https://www.trustedlogin.com/about/live-access/'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Config |
| 35 |
*/ |
| 36 |
private $config; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var SupportUser $support_user |
| 40 |
*/ |
| 41 |
private $support_user; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var Form $form |
| 45 |
*/ |
| 46 |
private $form; |
| 47 |
|
| 48 |
/** |
| 49 |
* Admin constructor. |
| 50 |
* |
| 51 |
* @param Config $config |
| 52 |
*/ |
| 53 |
public function __construct( Config $config, Form $form, SupportUser $support_user ) { |
| 54 |
$this->config = $config; |
| 55 |
$this->form = $form; |
| 56 |
$this->support_user = $support_user; |
| 57 |
} |
| 58 |
|
| 59 |
public function init() { |
| 60 |
add_action( 'trustedlogin/' . $this->config->ns() . '/button', |
| 61 |
array( |
| 62 |
$this->form, |
| 63 |
'generate_button' |
| 64 |
), 10, 2 |
| 65 |
); |
| 66 |
add_action( 'trustedlogin/' . $this->config->ns() . '/users_table', array( |
| 67 |
$this->form, |
| 68 |
'output_support_users' |
| 69 |
), 20 ); |
| 70 |
add_action( 'trustedlogin/' . $this->config->ns() . '/auth_screen', array( |
| 71 |
$this->form, 'print_auth_screen' ), 20 |
| 72 |
); |
| 73 |
add_action( 'login_form_trustedlogin', array( |
| 74 |
$this->form, 'maybe_print_request_screen' ), 20 |
| 75 |
); |
| 76 |
add_filter( 'user_row_actions', array( |
| 77 |
$this, 'user_row_action_revoke' ), |
| 78 |
10, 2 ); |
| 79 |
add_action( 'admin_bar_menu', array( |
| 80 |
$this, 'admin_bar_add_toolbar_items' ), |
| 81 |
100 ); |
| 82 |
|
| 83 |
if ( $this->config->get_setting( 'menu' ) ) { |
| 84 |
$menu_priority = $this->config->get_setting( 'menu/priority', 100 ); |
| 85 |
add_action( 'admin_menu', array( |
| 86 |
$this, 'admin_menu_auth_link_page' |
| 87 |
), $menu_priority ); |
| 88 |
} |
| 89 |
|
| 90 |
if ( $this->config->get_setting( 'register_assets', true ) ) { |
| 91 |
add_action( 'admin_enqueue_scripts', array( |
| 92 |
$this->form, 'register_assets' |
| 93 |
) ); |
| 94 |
add_action( 'login_enqueue_scripts',array( |
| 95 |
$this->form, 'register_assets' |
| 96 |
) ); |
| 97 |
} |
| 98 |
|
| 99 |
add_action( 'trustedlogin/' . $this->config->ns() . '/admin/access_revoked', array( |
| 100 |
$this, 'admin_notices' ) |
| 101 |
); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Filter: Update the actions on the users.php list for our support users. |
| 106 |
* |
| 107 |
* @since 1.0.0 |
| 108 |
* |
| 109 |
* @param array $actions |
| 110 |
* @param WP_User $user_object |
| 111 |
* |
| 112 |
* @return array |
| 113 |
*/ |
| 114 |
public function user_row_action_revoke( $actions, $user_object ) { |
| 115 |
|
| 116 |
if ( ! current_user_can( $this->support_user->role->get_name() ) && ! current_user_can( 'delete_users' ) ) { |
| 117 |
return $actions; |
| 118 |
} |
| 119 |
|
| 120 |
$revoke_url = $this->support_user->get_revoke_url( $user_object ); |
| 121 |
|
| 122 |
if ( ! $revoke_url ) { |
| 123 |
return $actions; |
| 124 |
} |
| 125 |
|
| 126 |
return array( |
| 127 |
'revoke' => "<a class='trustedlogin tl-revoke submitdelete' href='" . esc_url( $revoke_url ) . "'>" . esc_html__( 'Revoke Access', 'trustedlogin' ) . '</a>', |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
/** |
| 133 |
* Adds a "Revoke TrustedLogin" menu item to the admin toolbar |
| 134 |
* |
| 135 |
* @param WP_Admin_Bar $admin_bar |
| 136 |
* |
| 137 |
* @return void |
| 138 |
*/ |
| 139 |
public function admin_bar_add_toolbar_items( $admin_bar ) { |
| 140 |
|
| 141 |
if ( ! current_user_can( $this->support_user->role->get_name() ) ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if ( ! $admin_bar instanceof WP_Admin_Bar ) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$is_user_active = $this->support_user->is_active(); |
| 150 |
|
| 151 |
if ( ! $is_user_active ) { |
| 152 |
return; |
| 153 |
} |
| 154 |
|
| 155 |
$icon = '<span style=" |
| 156 |
height: 32px; |
| 157 |
width: 23px; |
| 158 |
margin: 0 1px; |
| 159 |
display: inline-block; |
| 160 |
vertical-align: top; |
| 161 |
background: url(\'data:image/svg+xml;base64,PHN2ZyBlbmFibGUtYmFja2dyb3VuZD0ibmV3IDAgMCAyNTAgMjUwIiB2aWV3Qm94PSIwIDAgMjUwIDI1MCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48cGF0aCBkPSJtLTQ0NC42IDE0LjdjLTI2LjUgMC00OC4xIDIxLjYtNDguMSA0OC4xdjM5LjhoMjAuNnYtMzkuOGMwLTE1LjIgMTIuMy0yNy41IDI3LjUtMjcuNSAxNS4xIDAgMjcuNSAxMi4zIDI3Lj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 164 |
|
| 165 |
$admin_bar->add_menu( array( |
| 166 |
'id' => 'tl-' . $this->config->ns() . '-revoke', |
| 167 |
'title' => $icon . esc_html__( 'Revoke Access', 'trustedlogin' ), |
| 168 |
'href' => $this->support_user->get_revoke_url( 'all' ), |
| 169 |
'parent' => 'top-secondary', |
| 170 |
'meta' => array( |
| 171 |
'class' => 'tl-destroy-session', |
| 172 |
'title' => esc_html__( 'You are logged in as a support user. Click to permanently revoke access.', 'trustedlogin' ), |
| 173 |
), |
| 174 |
) ); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Generates the auth link page |
| 179 |
* |
| 180 |
* This simulates the addition of an admin submenu item with null as the menu location |
| 181 |
* |
| 182 |
* @since 1.0.0 |
| 183 |
* |
| 184 |
* @return void |
| 185 |
*/ |
| 186 |
public function admin_menu_auth_link_page() { |
| 187 |
|
| 188 |
$parent_slug = $this->config->get_setting( 'menu/slug', null ); |
| 189 |
|
| 190 |
// When false, there will be no menus added. |
| 191 |
if ( false === $parent_slug ) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
$ns = $this->config->ns(); |
| 196 |
|
| 197 |
$menu_slug = apply_filters( 'trustedlogin/' . $this->config->ns() . '/admin/menu/menu_slug', 'grant-' . $ns . '-access' ); |
| 198 |
|
| 199 |
$menu_title = $this->config->get_setting( 'menu/title', esc_html__( 'Grant Support Access', 'trustedlogin' ) ); |
| 200 |
|
| 201 |
// If empty (null or empty string), add top-level menu |
| 202 |
if ( empty( $parent_slug ) ) { |
| 203 |
|
| 204 |
add_menu_page( |
| 205 |
$menu_title, |
| 206 |
$menu_title, |
| 207 |
'create_users', |
| 208 |
$menu_slug, |
| 209 |
array( $this->form, 'print_auth_screen' ), |
| 210 |
$this->config->get_setting( 'menu/icon_url', '' ), |
| 211 |
$this->config->get_setting( 'menu/position', null ) |
| 212 |
); |
| 213 |
|
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
add_submenu_page( |
| 218 |
$parent_slug, |
| 219 |
$menu_title, |
| 220 |
$menu_title, |
| 221 |
'create_users', |
| 222 |
$menu_slug, |
| 223 |
array( $this->form, 'print_auth_screen' ), |
| 224 |
$this->config->get_setting( 'menu/position', null ) |
| 225 |
); |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Add admin_notices hooks |
| 230 |
* |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
public function admin_notices() { |
| 234 |
add_action( 'admin_notices', array( $this->form, 'admin_notice_revoked' ) ); |
| 235 |
} |
| 236 |
|
| 237 |
} |
| 238 |
|