true, 'timeout' => 10 ); $result = WPFunctions::wpRemoteGet(self::BRIDGE_URL, $params); return WPFunctions::wpRemoteRetrieveResponseCode($result) === 200; } function initApi($api_key) { if($this->api) { $this->api->setKey($api_key); } else { $this->api = new Bridge\API($api_key); } } function checkMSSKey($api_key) { $this->initApi($api_key); $result = $this->api->checkMSSKey(); return $this->processKeyCheckResult($result); } function storeMSSKeyAndState($key, $state) { if(empty($state['state']) || $state['state'] === self::KEY_CHECK_ERROR ) { return false; } // store the key itself Setting::setValue( self::API_KEY_SETTING_NAME, $key ); // store the key state Setting::setValue( self::API_KEY_STATE_SETTING_NAME, $state ); } function checkPremiumKey($key) { $this->initApi($key); $result = $this->api->checkPremiumKey(); return $this->processKeyCheckResult($result); } private function processKeyCheckResult(array $result) { $state_map = array( 200 => self::KEY_VALID, 401 => self::KEY_INVALID, 402 => self::KEY_ALREADY_USED, 403 => self::KEY_INVALID ); if(!empty($result['code']) && isset($state_map[$result['code']])) { if($state_map[$result['code']] == self::KEY_VALID && !empty($result['data']['expire_at']) ) { $key_state = self::KEY_EXPIRING; } else { $key_state = $state_map[$result['code']]; } } else { $key_state = self::KEY_CHECK_ERROR; } return $this->buildKeyState( $key_state, $result ); } function storePremiumKeyAndState($key, $state) { if(empty($state['state']) || $state['state'] === self::KEY_CHECK_ERROR ) { return false; } // store the key itself Setting::setValue( self::PREMIUM_KEY_SETTING_NAME, $key ); // store the key state Setting::setValue( self::PREMIUM_KEY_STATE_SETTING_NAME, $state ); } private function buildKeyState($key_state, $result) { $state = array( 'state' => $key_state, 'data' => !empty($result['data']) ? $result['data'] : null, 'code' => !empty($result['code']) ? $result['code'] : self::CHECK_ERROR_UNKNOWN ); return $state; } function updateSubscriberCount($result) { if(!empty($result['state']) && ($result['state'] === self::KEY_VALID || $result['state'] === self::KEY_EXPIRING) ) { return $this->api->updateSubscriberCount(Subscriber::getTotalSubscribers()); } return null; } static function invalidateKey() { Setting::setValue( self::API_KEY_STATE_SETTING_NAME, array('state' => self::KEY_INVALID) ); } function onSettingsSave($settings) { $api_key_set = !empty($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']); $premium_key_set = !empty($settings['premium']['premium_key']); if($api_key_set) { $api_key = $settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']; $state = $this->checkMSSKey($api_key); $this->storeMSSKeyAndState($api_key, $state); if(self::isMPSendingServiceEnabled()) { $this->updateSubscriberCount($state); } } if($premium_key_set) { $premium_key = $settings['premium']['premium_key']; $state = $this->checkPremiumKey($premium_key); $this->storePremiumKeyAndState($premium_key, $state); } } }