PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.0.12
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.0.12
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
content-control / vendor-prefixed / trustedlogin / client / src / Ajax.php

Ajax.php in Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More 2.0.12, at vendor-prefixed/trustedlogin/client/src/Ajax.php

130 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @license GPL-2.0-or-later
4 *
5 * Modified by code-atlantic on 26-October-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 // Remove any fields that are not in the $ajax_fields array.
73 $posted_data = array_intersect_key( $_POST, array_flip( $this->generate_support_fields ) );
74
75 if ( empty( $posted_data['vendor'] ) ) {
76
77 $this->logging->log( 'Vendor not defined in TrustedLogin configuration.', __METHOD__, 'critical' );
78
79 wp_send_json_error( array( 'message' => 'Vendor not defined in TrustedLogin configuration.' ) );
80 }
81
82 // There are multiple TrustedLogin instances, and this is not the one being called.
83 // This should not occur, since the AJAX action is namespaced.
84 if ( $this->config->ns() !== $posted_data['vendor'] ) {
85
86 $this->logging->log( 'Vendor does not match TrustedLogin configuration.', __METHOD__, 'critical' );
87
88 wp_send_json_error( array( 'message' => 'Vendor does not match.' ) );
89
90 return;
91 }
92
93 if ( empty( $posted_data['_nonce'] ) ) {
94 wp_send_json_error( array( 'message' => 'Nonce not sent in the request.' ) );
95 }
96
97 if ( ! check_ajax_referer( 'tl_nonce-' . get_current_user_id(), '_nonce', false ) ) {
98 wp_send_json_error( array( 'message' => esc_html__( 'Verification issue: Request could not be verified. Please reload the page.', 'trustedlogin' ) ) );
99 }
100
101 if ( ! current_user_can( 'create_users' ) ) {
102
103 $this->logging->log( 'Current user does not have `create_users` capability when trying to grant access.', __METHOD__, 'error' );
104
105 wp_send_json_error( array( 'message' => esc_html__( 'You do not have the ability to create users.', 'trustedlogin' ) ) );
106 }
107
108 $client = new Client( $this->config, false );
109
110 // Passed from grantAccess() in trustedlogin.js.
111 $include_debug_data = ! empty( $posted_data['debug_data_consent'] );
112
113 // Passed from grantAccess() in trustedlogin.js.
114 $ticket_data = ! empty( $posted_data['ticket'] ) ? $posted_data['ticket'] : null;
115
116 $response = $client->grant_access( $include_debug_data, $ticket_data );
117
118 if ( is_wp_error( $response ) ) {
119
120 $error_data = $response->get_error_data();
121 $status_code = isset( $error_data['status_code'] ) ? $error_data['status_code'] : 500;
122
123 wp_send_json_error( array( 'message' => $response->get_error_message() ), $status_code );
124 }
125
126 wp_send_json_success( $response, 201 );
127 }
128
129 }
130