| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Settings OAuth class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers OAuth integration that is be accessed at Settings > Kit, when the Plugin |
| 11 |
* has no Access Token specified. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
class ConvertKit_Admin_Section_OAuth extends ConvertKit_Admin_Section_Base { |
| 17 |
|
| 18 |
/** |
| 19 |
* Constructor |
| 20 |
* |
| 21 |
* @since 2.2.0 |
| 22 |
*/ |
| 23 |
public function __construct() { |
| 24 |
|
| 25 |
// Define the class that reads/writes settings. |
| 26 |
$this->settings = new ConvertKit_Settings(); |
| 27 |
|
| 28 |
// Define the settings key. |
| 29 |
$this->settings_key = $this->settings::SETTINGS_NAME; |
| 30 |
|
| 31 |
$this->name = 'oauth'; |
| 32 |
$this->title = __( 'OAuth', 'convertkit' ); |
| 33 |
$this->tab_text = __( 'OAuth', 'convertkit' ); |
| 34 |
|
| 35 |
// Maybe output notices for this settings screen, and the Intercom messenger. |
| 36 |
if ( $this->on_settings_screen( 'general' ) ) { |
| 37 |
add_action( 'convertkit_settings_base_render_before', array( $this, 'maybe_output_notices' ) ); |
| 38 |
add_action( 'admin_footer', array( $this, 'output_intercom' ) ); |
| 39 |
} |
| 40 |
|
| 41 |
parent::__construct(); |
| 42 |
|
| 43 |
$this->maybe_get_and_store_access_token(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Requests an access token via OAuth, if an authorization code and verifier are included in the request. |
| 48 |
* |
| 49 |
* @since 2.2.0 |
| 50 |
*/ |
| 51 |
private function maybe_get_and_store_access_token() { |
| 52 |
|
| 53 |
// Bail if we're not on the settings screen. |
| 54 |
if ( ! $this->on_settings_screen( 'general' ) ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
// Bail if no authorization code is included in the request. |
| 59 |
if ( ! array_key_exists( 'code', $_REQUEST ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
// Sanitize token. |
| 64 |
$authorization_code = sanitize_text_field( wp_unslash( $_REQUEST['code'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 65 |
|
| 66 |
// Exchange the authorization code and verifier for an access token. |
| 67 |
$api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI ); |
| 68 |
$result = $api->get_access_token( $authorization_code ); |
| 69 |
|
| 70 |
// Redirect with an error if we could not fetch the access token. |
| 71 |
if ( is_wp_error( $result ) ) { |
| 72 |
wp_safe_redirect( |
| 73 |
add_query_arg( |
| 74 |
array( |
| 75 |
'page' => '_wp_convertkit_settings', |
| 76 |
'error_description' => $result->get_error_message(), |
| 77 |
), |
| 78 |
'options-general.php' |
| 79 |
) |
| 80 |
); |
| 81 |
exit(); |
| 82 |
} |
| 83 |
|
| 84 |
// Store Access Token, Refresh Token and expiry. |
| 85 |
$this->settings->save( |
| 86 |
array( |
| 87 |
'access_token' => $result['access_token'], |
| 88 |
'refresh_token' => $result['refresh_token'], |
| 89 |
'token_expires' => ( $result['created_at'] + $result['expires_in'] ), |
| 90 |
) |
| 91 |
); |
| 92 |
|
| 93 |
// Redirect to General screen, which will now show the Plugin's settings, because the Plugin |
| 94 |
// is now authenticated. |
| 95 |
wp_safe_redirect( |
| 96 |
add_query_arg( |
| 97 |
array( |
| 98 |
'page' => '_wp_convertkit_settings', |
| 99 |
'success' => 'oauth2_success', |
| 100 |
), |
| 101 |
'options-general.php' |
| 102 |
) |
| 103 |
); |
| 104 |
exit(); |
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Outputs the OAuth screen. |
| 110 |
* |
| 111 |
* @since 2.2.0 |
| 112 |
*/ |
| 113 |
public function render() { |
| 114 |
|
| 115 |
// Determine the OAuth URL to begin the authorization process. |
| 116 |
$api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI ); |
| 117 |
$oauth_url = $api->get_oauth_url( admin_url( 'options-general.php?page=_wp_convertkit_settings' ), get_site_url() ); |
| 118 |
|
| 119 |
/** |
| 120 |
* Performs actions prior to rendering the settings form. |
| 121 |
* |
| 122 |
* @since 2.0.0 |
| 123 |
*/ |
| 124 |
do_action( 'convertkit_settings_base_render_before' ); |
| 125 |
|
| 126 |
// Output view. |
| 127 |
require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/oauth.php'; |
| 128 |
|
| 129 |
/** |
| 130 |
* Performs actions after rendering of the settings form. |
| 131 |
* |
| 132 |
* @since 2.0.0 |
| 133 |
*/ |
| 134 |
do_action( 'convertkit_settings_base_render_after' ); |
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Prints help info for this section |
| 140 |
* |
| 141 |
* @since 2.2.0 |
| 142 |
*/ |
| 143 |
public function print_section_info() { |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Returns the URL for the ConvertKit documentation for this setting section. |
| 148 |
* |
| 149 |
* @since 2.2.0 |
| 150 |
* |
| 151 |
* @return string Documentation URL. |
| 152 |
*/ |
| 153 |
public function documentation_url() { |
| 154 |
|
| 155 |
return 'https://help.kit.com/en/articles/2502591-the-convertkit-wordpress-plugin'; |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|