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
3 years ago
WooCommerce
2 years ago
Actions.php
2 years ago
Ajax.php
4 years ago
Api.php
2 years ago
ApiAuth.php
4 years ago
ApiKey.php
2 years ago
AssetLoader.php
5 years ago
BaseRestApi.php
3 years ago
Blocks.php
2 years ago
ClassicEditor.php
3 years ago
ConstantContact.php
4 years ago
Debug.php
4 years ago
EasyDigitalDownloads.php
3 years ago
Elementor.php
3 years ago
Inserter.php
3 years ago
InstallSkin.php
5 years ago
InstallSkinCompat.php
5 years ago
MailPoet.php
4 years ago
MemberPress.php
2 years ago
Menu.php
2 years ago
Notifications.php
3 years ago
OmuApi.php
4 years ago
Output.php
2 years ago
Pages.php
2 years ago
Partners.php
2 years ago
Plugins.php
3 years ago
Promos.php
3 years ago
Refresh.php
2 years ago
RestApi.php
2 years ago
RevenueAttribution.php
4 years ago
Review.php
4 years ago
Rules.php
3 years ago
Save.php
2 years ago
Shortcode.php
4 years ago
Sites.php
2 years ago
Support.php
3 years ago
Type.php
3 years ago
Urls.php
2 years ago
Utils.php
3 years ago
Validate.php
4 years ago
WPForms.php
2 years ago
Welcome.php
4 years ago
Widget.php
4 years ago
WooCommerce.php
2 years ago
Wordfence.php
3 years ago
WpErrorException.php
5 years ago
ApiKey.php
218 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 ) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found |
| 129 | $creds = compact( '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 | if ( in_array( $site_id, $api_key_sites['siteIds'] ) ) { // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict |
| 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Determine if we have a valid api key stored. |
| 174 | * |
| 175 | * @since 2.0.0 |
| 176 | * |
| 177 | * @return bool |
| 178 | */ |
| 179 | public static function has_credentials() { |
| 180 | $creds = OMAPI::get_instance()->get_api_credentials(); |
| 181 | |
| 182 | return ! empty( $creds['apikey'] ) || self::has_legacy(); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Determine if we have legacy api credentials. |
| 187 | * |
| 188 | * @since 2.0.0 |
| 189 | * |
| 190 | * @return bool |
| 191 | */ |
| 192 | public static function has_legacy() { |
| 193 | $creds = OMAPI::get_instance()->get_api_credentials(); |
| 194 | |
| 195 | return ! empty( $creds['user'] ) && ! empty( $creds['key'] ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Handles regnerating api key. |
| 200 | * |
| 201 | * @since 2.6.5 |
| 202 | * |
| 203 | * @param string $apikey Api Key to replace after regeneration. |
| 204 | * |
| 205 | * @return mixed $value The response to the API call. |
| 206 | */ |
| 207 | public static function regenerate( $apikey ) { |
| 208 | return OMAPI_Api::build( 'v2', 'key/regenerate', 'POST', compact( 'apikey' ) ) |
| 209 | ->request( |
| 210 | array( |
| 211 | 'tt' => OMAPI_ApiAuth::get_tt(), |
| 212 | ) |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | } |
| 217 | |
| 218 |