PluginProbe ʕ •ᴥ•ʔ
Popup Builder & Popup Maker for WordPress – OptinMonster Email Marketing and Lead Generation / 2.16.5
Popup Builder & Popup Maker for WordPress – OptinMonster Email Marketing and Lead Generation v2.16.5
2.16.24 trunk 2.13.8 2.14.0 2.14.1 2.15.0 2.15.1 2.15.2 2.15.3 2.16.0 2.16.1 2.16.10 2.16.11 2.16.12 2.16.13 2.16.14 2.16.15 2.16.16 2.16.17 2.16.18 2.16.19 2.16.2 2.16.20 2.16.21 2.16.22 2.16.3 2.16.4 2.16.5 2.16.6 2.16.7 2.16.8 2.16.9
optinmonster / OMAPI / ApiKey.php
optinmonster / OMAPI Last commit date
EasyDigitalDownloads 3 years ago Elementor 2 years ago Integrations 3 years ago MemberPress 2 years ago Plugins 2 years ago Promos 3 years ago Rules 2 years ago Shortcodes 2 years ago WPForms 1 year ago WooCommerce 1 year ago Actions.php 1 year ago Ajax.php 4 years ago Api.php 1 year ago ApiAuth.php 4 years ago ApiKey.php 1 year ago AssetLoader.php 5 years ago BaseRestApi.php 3 years ago Blocks.php 1 year ago ClassicEditor.php 3 years ago ConstantContact.php 1 year ago Debug.php 1 year ago EasyDigitalDownloads.php 1 year ago Elementor.php 3 years ago Inserter.php 3 years ago InstallSkin.php 5 years ago InstallSkinCompat.php 5 years ago MailPoet.php 1 year ago MemberPress.php 2 years ago Menu.php 1 year ago Notifications.php 1 year ago OmuApi.php 4 years ago Output.php 1 year ago Pages.php 1 year ago Partners.php 1 year ago Plugins.php 2 years ago Promos.php 3 years ago Refresh.php 1 year ago RestApi.php 1 year ago RevenueAttribution.php 4 years ago Review.php 4 years ago Rules.php 1 year ago Save.php 2 years ago Shortcode.php 4 years ago Sites.php 1 year ago Support.php 1 year ago Type.php 3 years ago Urls.php 1 year ago Utils.php 1 year ago Validate.php 2 years ago WPForms.php 2 years ago Welcome.php 4 years ago Widget.php 4 years ago WooCommerce.php 1 year ago Wordfence.php 3 years ago WpErrorException.php 5 years ago
ApiKey.php
219 lines
1 <?php
2 /**
3 * Mailpoet integration class.
4 *
5 * @since 2.0.0
6 *
7 * @package OMAPI
8 * @author Justin Sternberg
9 */
10
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 /**
17 * OM API Key management class.
18 *
19 * @since 2.0.0
20 */
21 class OMAPI_ApiKey {
22
23 /**
24 * Handles storing the API key and initiating the API connection.
25 *
26 * @since 2.0.0
27 *
28 * @param string $apikey The OM api key.
29 *
30 * @return bool True if the Key can be validated
31 */
32 public static function init_connection( $apikey ) {
33 $base = OMAPI::get_instance();
34
35 $creds = compact( 'apikey' );
36 $option = $base->get_option();
37 $option['api']['apikey'] = $apikey;
38
39 // Let's store the api-key first.
40 $base->save->update_option( $option, $creds );
41
42 // Go ahead and remove the old user and key.
43 $option['api']['user'] = '';
44 $option['api']['key'] = '';
45
46 // Remove any error messages.
47 $option['is_invalid'] = false;
48 $option['is_expired'] = false;
49 $option['is_disabled'] = false;
50 $option['connected'] = time();
51 $option['auto_updates'] = 'all';
52 $option['usage_tracking'] = true;
53
54 // Remove any pre-saved site/user/account data, so we re-fetch it elsewhere.
55 unset( $option['siteId'] );
56 unset( $option['siteIds'] );
57 unset( $option['customApiUrl'] );
58 unset( $option['apiCname'] );
59 unset( $option['userId'] );
60 unset( $option['accountUserId'] );
61 unset( $option['accountId'] );
62 unset( $option['currentLevel'] );
63 unset( $option['plan'] );
64 unset( $option['revenueAttribution'] );
65
66 // Fetch the userId and accountId now.
67 $option = OMAPI_Api::fetch_me( $option, $creds );
68 if ( is_wp_error( $option ) ) {
69 return $option;
70 }
71
72 // Fetch the SiteIds for this site now.
73 $result = $base->sites->fetch( $apikey );
74 if ( is_wp_error( $result ) ) {
75 return $result;
76 }
77
78 $option = array_merge( $option, $result );
79
80 // Fetch the campaigns for this site now.
81 $base->refresh->refresh( $apikey );
82
83 // Save the option one more time, with all the new good stuff..
84 $base->save->update_option( $option, $creds );
85
86 return $option;
87 }
88
89 /**
90 * Remove the API key and disconnect from the OptinMonster app.
91 *
92 * @since 2.0.0
93 *
94 * @return mixed The results of update_option.
95 */
96 public static function disconnect() {
97 $option = OMAPI::get_instance()->get_option();
98
99 $option['connected'] = 0;
100 $option['api']['apikey'] = '';
101
102 // Remove any pre-saved site/user/account data, so we re-fetch it elsewhere.
103 unset( $option['userId'] );
104 unset( $option['accountUserId'] );
105 unset( $option['accountId'] );
106 unset( $option['currentLevel'] );
107 unset( $option['plan'] );
108 unset( $option['siteId'] );
109 unset( $option['siteIds'] );
110 unset( $option['customApiUrl'] );
111 unset( $option['apiCname'] );
112 unset( $option['api']['user'] );
113 unset( $option['api']['key'] );
114
115 // Save the updated option.
116 return OMAPI::get_instance()->save->update_option( $option );
117 }
118
119 /**
120 * Determine if we can store the given api key.
121 *
122 * @since 2.0.0
123 *
124 * @param string $apikey The OM api key.
125 *
126 * @return bool True if the Key can be validated
127 */
128 public static function verify( $apikey ) {
129 $creds = array( 'apikey' => $apikey );
130
131 // Verify this new API Key works by posting to the Legacy route.
132 return OMAPI_Api::build( 'v1', 'verify/', 'POST', $creds )->request();
133 }
134
135 /**
136 * Validate this API Key
137 * We validate an API Key by fetching the Sites this key can fetch
138 * And then confirming that this key has access to at least one of these sites
139 *
140 * @since 2.0.0
141 *
142 * @param string $apikey The OM api key.
143 *
144 * @return bool True if the Key can be validated
145 */
146 public static function validate( $apikey ) {
147 if ( empty( $apikey ) ) {
148 return false;
149 }
150
151 $site_ids = OMAPI::get_instance()->get_site_ids();
152
153 if ( empty( $site_ids ) ) {
154 return false;
155 }
156
157 $api_key_sites = OMAPI::get_instance()->sites->fetch( $apikey, true );
158
159 if ( is_wp_error( $api_key_sites ) || empty( $api_key_sites['siteIds'] ) ) {
160 return false;
161 }
162
163 foreach ( $site_ids as $site_id ) {
164 // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
165 if ( in_array( $site_id, $api_key_sites['siteIds'] ) ) {
166 return true;
167 }
168 }
169
170 return false;
171 }
172
173 /**
174 * Determine if we have a valid api key stored.
175 *
176 * @since 2.0.0
177 *
178 * @return bool
179 */
180 public static function has_credentials() {
181 $creds = OMAPI::get_instance()->get_api_credentials();
182
183 return ! empty( $creds['apikey'] ) || self::has_legacy();
184 }
185
186 /**
187 * Determine if we have legacy api credentials.
188 *
189 * @since 2.0.0
190 *
191 * @return bool
192 */
193 public static function has_legacy() {
194 $creds = OMAPI::get_instance()->get_api_credentials();
195
196 return ! empty( $creds['user'] ) && ! empty( $creds['key'] );
197 }
198
199 /**
200 * Handles regnerating api key.
201 *
202 * @since 2.6.5
203 *
204 * @param string $apikey Api Key to replace after regeneration.
205 *
206 * @return mixed $value The response to the API call.
207 */
208 public static function regenerate( $apikey ) {
209 return OMAPI_Api::build( 'v2', 'key/regenerate', 'POST', compact( 'apikey' ) )
210 ->request(
211 array(
212 'tt' => OMAPI_ApiAuth::get_tt(),
213 )
214 );
215 }
216
217 }
218
219