| 1 |
<?php |
| 2 |
/** |
| 3 |
* @license GPL-2.0-or-later |
| 4 |
* |
| 5 |
* Modified by code-atlantic on 18-September-2023 using Strauss. |
| 6 |
* @see https://github.com/BrianHenryIE/strauss |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Vendor\TrustedLogin; |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if ( ! defined( 'ABSPATH' ) ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
use \Exception; |
| 17 |
use \WP_Error; |
| 18 |
use \WP_User; |
| 19 |
use \WP_Admin_Bar; |
| 20 |
|
| 21 |
final class Ajax { |
| 22 |
|
| 23 |
/** |
| 24 |
* @var \ContentControl\Vendor\TrustedLogin\Config |
| 25 |
*/ |
| 26 |
private $config; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var null|\ContentControl\Vendor\TrustedLogin\Logging $logging |
| 30 |
*/ |
| 31 |
private $logging; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var string[] Fields that may be included in the support data. |
| 35 |
* @see grantAccess() in trustedlogin.js |
| 36 |
*/ |
| 37 |
private $generate_support_fields = array( |
| 38 |
'action', |
| 39 |
'vendor', |
| 40 |
'_nonce', |
| 41 |
'reference_id', |
| 42 |
'debug_data_consent', |
| 43 |
'ticket', |
| 44 |
); |
| 45 |
|
| 46 |
/** |
| 47 |
* Cron constructor. |
| 48 |
* |
| 49 |
* @param Config $config |
| 50 |
* @param Logging|null $logging |
| 51 |
*/ |
| 52 |
public function __construct( Config $config, Logging $logging ) { |
| 53 |
$this->config = $config; |
| 54 |
$this->logging = $logging; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* |
| 59 |
*/ |
| 60 |
public function init() { |
| 61 |
add_action( 'wp_ajax_tl_' . $this->config->ns() . '_gen_support', array( $this, 'ajax_generate_support' ) ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* AJAX handler for maybe generating a Support User |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* |
| 69 |
* @return void Sends a JSON success or error message based on what happens |
| 70 |
*/ |
| 71 |
public function ajax_generate_support() { |
| 72 |
|
| 73 |
// Remove any fields that are not in the $ajax_fields array. |
| 74 |
$posted_data = array_intersect_key( $_POST, array_flip( $this->generate_support_fields ) ); |
| 75 |
|
| 76 |
if ( empty( $posted_data['vendor'] ) ) { |
| 77 |
|
| 78 |
$this->logging->log( 'Vendor not defined in TrustedLogin configuration.', __METHOD__, 'critical' ); |
| 79 |
|
| 80 |
wp_send_json_error( array( 'message' => 'Vendor not defined in TrustedLogin configuration.' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
// There are multiple TrustedLogin instances, and this is not the one being called. |
| 84 |
// This should not occur, since the AJAX action is namespaced. |
| 85 |
if ( $this->config->ns() !== $posted_data['vendor'] ) { |
| 86 |
|
| 87 |
$this->logging->log( 'Vendor does not match TrustedLogin configuration.', __METHOD__, 'critical' ); |
| 88 |
|
| 89 |
wp_send_json_error( array( 'message' => 'Vendor does not match.' ) ); |
| 90 |
|
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
if ( empty( $posted_data['_nonce'] ) ) { |
| 95 |
wp_send_json_error( array( 'message' => 'Nonce not sent in the request.' ) ); |
| 96 |
} |
| 97 |
|
| 98 |
if ( ! check_ajax_referer( 'tl_nonce-' . get_current_user_id(), '_nonce', false ) ) { |
| 99 |
wp_send_json_error( array( 'message' => esc_html__( 'Verification issue: Request could not be verified. Please reload the page.', 'trustedlogin' ) ) ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! current_user_can( 'create_users' ) ) { |
| 103 |
|
| 104 |
$this->logging->log( 'Current user does not have `create_users` capability when trying to grant access.', __METHOD__, 'error' ); |
| 105 |
|
| 106 |
wp_send_json_error( array( 'message' => esc_html__( 'You do not have the ability to create users.', 'trustedlogin' ) ) ); |
| 107 |
} |
| 108 |
|
| 109 |
$client = new Client( $this->config, false ); |
| 110 |
|
| 111 |
// Passed from grantAccess() in trustedlogin.js. |
| 112 |
$include_debug_data = ! empty( $posted_data['debug_data_consent'] ); |
| 113 |
|
| 114 |
// Passed from grantAccess() in trustedlogin.js. |
| 115 |
$ticket_data = ! empty( $posted_data['ticket'] ) ? $posted_data['ticket'] : null; |
| 116 |
|
| 117 |
$response = $client->grant_access( $include_debug_data, $ticket_data ); |
| 118 |
|
| 119 |
if ( is_wp_error( $response ) ) { |
| 120 |
|
| 121 |
$error_data = $response->get_error_data(); |
| 122 |
$error_code = isset( $error_data['error_code'] ) ? $error_data['error_code'] : 500; |
| 123 |
|
| 124 |
wp_send_json_error( array( 'message' => $response->get_error_message() ), $error_code ); |
| 125 |
} |
| 126 |
|
| 127 |
wp_send_json_success( $response, 201 ); |
| 128 |
} |
| 129 |
|
| 130 |
} |
| 131 |
|