| 1 |
<?php |
| 2 |
/** |
| 3 |
* AuthController. |
| 4 |
* php version 5.6 |
| 5 |
* |
| 6 |
* @category AuthController |
| 7 |
* @package SureTriggers |
| 8 |
* @author BSF <username@example.com> |
| 9 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 10 |
* @link https://www.brainstormforce.com/ |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace SureTriggers\Controllers; |
| 15 |
|
| 16 |
use SureCart\Models\ApiToken; |
| 17 |
use SureTriggers\Models\SaasApiToken; |
| 18 |
use SureTriggers\Traits\SingletonLoader; |
| 19 |
|
| 20 |
/** |
| 21 |
* AuthController- Connect and revoke user access_token. |
| 22 |
* |
| 23 |
* @category AuthController |
| 24 |
* @package SureTriggers |
| 25 |
* @author BSF <username@example.com> |
| 26 |
* @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3 |
| 27 |
* @link https://www.brainstormforce.com/ |
| 28 |
* @since 1.0.0 |
| 29 |
* |
| 30 |
* @psalm-suppress UndefinedTrait |
| 31 |
*/ |
| 32 |
class AuthController { |
| 33 |
|
| 34 |
|
| 35 |
use SingletonLoader; |
| 36 |
|
| 37 |
/** |
| 38 |
* Access token for authentication. |
| 39 |
* |
| 40 |
* @var string $secret_key |
| 41 |
*/ |
| 42 |
private $access_token; |
| 43 |
|
| 44 |
/** |
| 45 |
* Connection id for authentication. |
| 46 |
* |
| 47 |
* @var string $secret_key |
| 48 |
*/ |
| 49 |
private $connection_id; |
| 50 |
|
| 51 |
/** |
| 52 |
* Secret Key for authentication. |
| 53 |
* |
| 54 |
* @var string|mixed $secret_key |
| 55 |
*/ |
| 56 |
private $secret_key; |
| 57 |
|
| 58 |
/** |
| 59 |
* List of conected integrations/plugins. |
| 60 |
* |
| 61 |
* @var array $connected_integrations |
| 62 |
*/ |
| 63 |
private $connected_integrations; |
| 64 |
|
| 65 |
/** |
| 66 |
* Initialise data. |
| 67 |
*/ |
| 68 |
public function __construct() { |
| 69 |
$this->access_token = OptionController::get_option( 'access_token' ); |
| 70 |
$this->connection_id = OptionController::get_option( 'connection_id' ); |
| 71 |
$this->connected_integrations = OptionController::get_option( 'connected_integrations', [] ); |
| 72 |
$this->secret_key = SaasApiToken::get(); |
| 73 |
add_action( 'admin_init', [ $this, 'save_connection' ] ); |
| 74 |
add_action( 'updated_option', [ $this, 'updated_sc_api_key' ], 10, 3 ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Remove the respective integration triggers after deleting the connection |
| 79 |
* |
| 80 |
* @param string $integration Integration Name. |
| 81 |
*/ |
| 82 |
public static function remove_integration_triggers( $integration ) { |
| 83 |
$saved_triggers = OptionController::get_option( 'triggers', [] ); |
| 84 |
|
| 85 |
foreach ( $saved_triggers as $index => $trigger ) { |
| 86 |
if ( ! empty( $trigger['integration'] ) && $integration === $trigger['integration'] ) { |
| 87 |
unset( $saved_triggers[ $index ] ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
$saved_triggers = OptionController::set_option( 'triggers', $saved_triggers ); |
| 92 |
|
| 93 |
// Remove the respective integration triggers field data after deleting the connection. |
| 94 |
$saved_triggers_data = OptionController::get_option( 'trigger_data', [] ); |
| 95 |
foreach ( $saved_triggers_data as $index => $trigger ) { |
| 96 |
if ( is_array( $saved_triggers_data ) && is_array( $trigger ) && ! empty( $trigger ) && $integration === $index ) { |
| 97 |
unset( $saved_triggers_data[ $index ] ); |
| 98 |
} |
| 99 |
} |
| 100 |
$saved_triggers_data = OptionController::set_option( 'trigger_data', $saved_triggers_data ); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Add or revoke access token from Sass. |
| 105 |
* |
| 106 |
* @param object $request Request. |
| 107 |
*/ |
| 108 |
public function revoke_connection( $request ) { |
| 109 |
$secret_key = $request->get_header( 'st_authorization' ); |
| 110 |
list($secret_key) = sscanf( $secret_key, 'Bearer %s' ); |
| 111 |
|
| 112 |
if ( $this->secret_key !== $secret_key ) { |
| 113 |
return RestController::error_message( 'Invalid secret key.' ); |
| 114 |
} |
| 115 |
|
| 116 |
// delete the suretrigger_options from wp_options table once the connection is deleted on SAAS. |
| 117 |
SaasApiToken::save( null ); |
| 118 |
|
| 119 |
return RestController::success_message(); |
| 120 |
|
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Save sure triggers connection. |
| 125 |
* |
| 126 |
* @return void |
| 127 |
*/ |
| 128 |
public function save_connection() { |
| 129 |
if ( ! isset( $_GET['sure-trigger-connect-nonce'] ) ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
if ( ! isset( $_GET['connection-status'] ) ) { |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
$nonce = sanitize_text_field( wp_unslash( $_GET['sure-trigger-connect-nonce'] ) ); |
| 138 |
$connection_status = (bool) sanitize_text_field( wp_unslash( $_GET['connection-status'] ) ); |
| 139 |
|
| 140 |
if ( false === wp_verify_nonce( $nonce, 'sure-trigger-connect' ) ) { |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
if ( false === current_user_can( 'administrator' ) ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
$access_key = isset( $_GET['sure-triggers-access-key'] ) ? sanitize_text_field( wp_unslash( $_GET['sure-triggers-access-key'] ) ) : false; |
| 149 |
|
| 150 |
if ( false === $connection_status ) { |
| 151 |
$access_key = 'connection-denied'; |
| 152 |
} |
| 153 |
|
| 154 |
$connected_email_id = isset( $_GET['connected_email'] ) ? sanitize_email( wp_unslash( $_GET['connected_email'] ) ) : ''; |
| 155 |
|
| 156 |
if ( isset( $access_key ) ) { |
| 157 |
SaasApiToken::save( $access_key ); |
| 158 |
} |
| 159 |
OptionController::set_option( 'connected_email_key', $connected_email_id ); |
| 160 |
|
| 161 |
/** |
| 162 |
* If there any SureCart |
| 163 |
*/ |
| 164 |
$this->post_authorize_create_sc_connection(); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Create SureCart connection at saas end. |
| 169 |
* |
| 170 |
* @return void |
| 171 |
*/ |
| 172 |
public function post_authorize_create_sc_connection() { |
| 173 |
if ( ! is_plugin_active( 'surecart/surecart.php' ) || ! class_exists( ApiToken::class ) ) { |
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
$this->create_sc_connection(); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Send a request to the SAAS to create SureCart connection for authorized user |
| 182 |
* |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
public function create_sc_connection() { |
| 186 |
$sc_api_key = ApiToken::get(); |
| 187 |
|
| 188 |
if ( empty( $sc_api_key ) ) { |
| 189 |
return; |
| 190 |
} |
| 191 |
|
| 192 |
$secret_key = SaasApiToken::get(); |
| 193 |
$connected_email = OptionController::get_option( 'connected_email_key' ); |
| 194 |
|
| 195 |
wp_remote_post( |
| 196 |
trailingslashit( SURE_TRIGGERS_API_SERVER_URL ) . 'connection/create-sc', |
| 197 |
[ |
| 198 |
'sslverify' => false, |
| 199 |
'timeout' => 60, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout |
| 200 |
'headers' => [ |
| 201 |
'Authorization' => 'Bearer ' . $secret_key, |
| 202 |
'scapikey' => $sc_api_key, |
| 203 |
], |
| 204 |
'body' => [ |
| 205 |
'email' => $connected_email, |
| 206 |
'title' => 'SureCart | ' . get_bloginfo( 'name' ), |
| 207 |
], |
| 208 |
] |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Update Sure Cart connection whenever update the API key |
| 214 |
* |
| 215 |
* @param string $option Option. |
| 216 |
* @param mixed $old_value Old value. |
| 217 |
* @param mixed $value Value. |
| 218 |
* @return void |
| 219 |
*/ |
| 220 |
public function updated_sc_api_key( $option, $old_value, $value ) { |
| 221 |
if ( 'sc_api_token' !== $option ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
if ( $value ) { |
| 226 |
$this->create_sc_connection(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
} |
| 231 |
|
| 232 |
AuthController::get_instance(); |
| 233 |
|