PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.6
3.4.6 3.4.5 3.4.4 3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 All 199 releases
convertkit / admin / section / class-convertkit-admin-section-oauth.php

class-convertkit-admin-section-oauth.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.6, at admin/section/class-convertkit-admin-section-oauth.php

180 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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
39 // OAuth is a special case, as it'll register as the 'general' screen - so the Intercom
40 // call in ConvertKit_Admin_Section_Base's construct won't work.
41 add_action( 'admin_footer', array( $this, 'output_intercom' ) );
42 }
43
44 parent::__construct();
45
46 $this->maybe_get_and_store_access_token();
47 }
48
49 /**
50 * Requests an access token via OAuth, if an authorization code and verifier are included in the request.
51 *
52 * @since 2.2.0
53 */
54 private function maybe_get_and_store_access_token() {
55
56 // Bail if we're not on the settings screen.
57 if ( ! $this->on_settings_screen( 'general' ) ) {
58 return;
59 }
60
61 // Bail if no authorization code is included in the request, as this isn't an OAuth callback.
62 if ( ! filter_has_var( INPUT_GET, 'code' ) ) {
63 return;
64 }
65
66 // Bail if the user is not permitted to connect the Plugin to a Kit account.
67 if ( ! current_user_can( 'manage_options' ) ) {
68 return;
69 }
70
71 // Redirect with an error if the nonce is missing or invalid.
72 $nonce = filter_input( INPUT_GET, 'nonce', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
73 if ( ! $nonce || ! wp_verify_nonce( sanitize_key( $nonce ), CONVERTKIT_NONCE_ACTION_OAUTH_CONNECT ) ) {
74 wp_safe_redirect(
75 convertkit_get_settings_link(
76 array(
77 'error_description' => __( 'The Kit authorization request could not be verified. Please click Connect again.', 'convertkit' ),
78 )
79 )
80 );
81 exit();
82 }
83
84 // Sanitize token.
85 $authorization_code = filter_input( INPUT_GET, 'code', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
86
87 // Exchange the authorization code and verifier for an access token.
88 $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI );
89 $result = $api->get_access_token( $authorization_code );
90
91 // Redirect with an error if we could not fetch the access token.
92 if ( is_wp_error( $result ) ) {
93 wp_safe_redirect(
94 add_query_arg(
95 array(
96 'page' => '_wp_convertkit_settings',
97 'error_description' => $result->get_error_message(),
98 ),
99 'options-general.php'
100 )
101 );
102 exit();
103 }
104
105 // Store Access Token, Refresh Token and expiry.
106 $this->settings->save(
107 array(
108 'access_token' => $result['access_token'],
109 'refresh_token' => $result['refresh_token'],
110 'token_expires' => ( time() + $result['expires_in'] ),
111 )
112 );
113
114 // Redirect to General screen, which will now show the Plugin's settings, because the Plugin
115 // is now authenticated.
116 wp_safe_redirect(
117 add_query_arg(
118 array(
119 'page' => '_wp_convertkit_settings',
120 'success' => 'oauth2_success',
121 ),
122 'options-general.php'
123 )
124 );
125 exit();
126
127 }
128
129 /**
130 * Outputs the OAuth screen.
131 *
132 * @since 2.2.0
133 */
134 public function render() {
135
136 // Determine the OAuth URL to begin the authorization process.
137 $oauth_url = convertkit_get_oauth_url();
138
139 /**
140 * Performs actions prior to rendering the settings form.
141 *
142 * @since 2.0.0
143 */
144 do_action( 'convertkit_settings_base_render_before' );
145
146 // Output view.
147 require_once CONVERTKIT_PLUGIN_PATH . '/views/backend/settings/oauth.php';
148
149 /**
150 * Performs actions after rendering of the settings form.
151 *
152 * @since 2.0.0
153 */
154 do_action( 'convertkit_settings_base_render_after' );
155
156 }
157
158 /**
159 * Prints help info for this section
160 *
161 * @since 2.2.0
162 */
163 public function print_section_info() {
164 }
165
166 /**
167 * Returns the URL for the ConvertKit documentation for this setting section.
168 *
169 * @since 2.2.0
170 *
171 * @return string Documentation URL.
172 */
173 public function documentation_url() {
174
175 return 'https://help.kit.com/en/articles/2502591-how-to-set-up-the-kit-plugin-on-your-wordpress-website';
176
177 }
178
179 }
180