Brak opisu

Bridge.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace MailPoet\Services;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\DI\ContainerWrapper;
  5. use MailPoet\Mailer\Mailer;
  6. use MailPoet\Services\Bridge\API;
  7. use MailPoet\Settings\SettingsController;
  8. use MailPoet\Util\License\Features\Subscribers as SubscribersFeature;
  9. use MailPoet\WP\Functions as WPFunctions;
  10. class Bridge {
  11. const API_KEY_SETTING_NAME = 'mta.mailpoet_api_key';
  12. const API_KEY_STATE_SETTING_NAME = 'mta.mailpoet_api_key_state';
  13. const AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING_NAME = 'authorized_emails_addresses_check';
  14. const PREMIUM_KEY_SETTING_NAME = 'premium.premium_key';
  15. const PREMIUM_KEY_STATE_SETTING_NAME = 'premium.premium_key_state';
  16. const PREMIUM_KEY_VALID = 'valid'; // for backwards compatibility until version 3.0.0
  17. const KEY_VALID = 'valid';
  18. const KEY_INVALID = 'invalid';
  19. const KEY_EXPIRING = 'expiring';
  20. const KEY_ALREADY_USED = 'already_used';
  21. const KEY_CHECK_ERROR = 'check_error';
  22. const CHECK_ERROR_UNAVAILABLE = 503;
  23. const CHECK_ERROR_UNKNOWN = 'unknown';
  24. const BRIDGE_URL = 'https://bridge.mailpoet.com';
  25. /** @var API|null */
  26. public $api;
  27. /** @var SettingsController */
  28. private $settings;
  29. /** @var SubscribersFeature */
  30. private $subscribersFeature;
  31. public function __construct(
  32. SettingsController $settingsController = null,
  33. SubscribersFeature $subscribersFeature = null
  34. ) {
  35. if ($settingsController === null) {
  36. $settingsController = SettingsController::getInstance();
  37. }
  38. if ($subscribersFeature === null) {
  39. $subscribersFeature = ContainerWrapper::getInstance()->get(SubscribersFeature::class);
  40. }
  41. $this->settings = $settingsController;
  42. $this->subscribersFeature = $subscribersFeature;
  43. }
  44. /**
  45. * @deprecated Use non static function isMailpoetSendingServiceEnabled instead
  46. * @return bool
  47. */
  48. public static function isMPSendingServiceEnabled() {
  49. try {
  50. $mailerConfig = SettingsController::getInstance()->get(Mailer::MAILER_CONFIG_SETTING_NAME);
  51. return !empty($mailerConfig['method'])
  52. && $mailerConfig['method'] === Mailer::METHOD_MAILPOET;
  53. } catch (\Exception $e) {
  54. return false;
  55. }
  56. }
  57. public function isMailpoetSendingServiceEnabled() {
  58. try {
  59. $mailerConfig = SettingsController::getInstance()->get(Mailer::MAILER_CONFIG_SETTING_NAME);
  60. return !empty($mailerConfig['method'])
  61. && $mailerConfig['method'] === Mailer::METHOD_MAILPOET;
  62. } catch (\Exception $e) {
  63. return false;
  64. }
  65. }
  66. public static function isMSSKeySpecified() {
  67. $settings = SettingsController::getInstance();
  68. $key = $settings->get(self::API_KEY_SETTING_NAME);
  69. return !empty($key);
  70. }
  71. public static function isPremiumKeySpecified() {
  72. $settings = SettingsController::getInstance();
  73. $key = $settings->get(self::PREMIUM_KEY_SETTING_NAME);
  74. return !empty($key);
  75. }
  76. public static function pingBridge() {
  77. $params = [
  78. 'blocking' => true,
  79. 'timeout' => 10,
  80. ];
  81. $wp = new WPFunctions();
  82. $result = $wp->wpRemoteGet(self::BRIDGE_URL, $params);
  83. return $wp->wpRemoteRetrieveResponseCode($result) === 200;
  84. }
  85. public function initApi($apiKey) {
  86. if ($this->api) {
  87. $this->api->setKey($apiKey);
  88. } else {
  89. $this->api = new Bridge\API($apiKey);
  90. }
  91. }
  92. /**
  93. * @param string $key
  94. * @return API
  95. */
  96. public function getApi($key) {
  97. $this->initApi($key);
  98. assert($this->api instanceof API);
  99. return $this->api;
  100. }
  101. public function getAuthorizedEmailAddresses() {
  102. return $this
  103. ->getApi($this->settings->get(self::API_KEY_SETTING_NAME))
  104. ->getAuthorizedEmailAddresses();
  105. }
  106. public function checkMSSKey($apiKey) {
  107. $result = $this
  108. ->getApi($apiKey)
  109. ->checkMSSKey();
  110. return $this->processKeyCheckResult($result);
  111. }
  112. public function storeMSSKeyAndState($key, $state) {
  113. if (empty($state['state'])
  114. || $state['state'] === self::KEY_CHECK_ERROR
  115. ) {
  116. return false;
  117. }
  118. // store the key itself
  119. $this->settings->set(
  120. self::API_KEY_SETTING_NAME,
  121. $key
  122. );
  123. // store the key state
  124. $this->settings->set(
  125. self::API_KEY_STATE_SETTING_NAME,
  126. $state
  127. );
  128. }
  129. public function checkPremiumKey($key) {
  130. $result = $this
  131. ->getApi($key)
  132. ->checkPremiumKey();
  133. return $this->processKeyCheckResult($result);
  134. }
  135. private function processKeyCheckResult(array $result) {
  136. $stateMap = [
  137. 200 => self::KEY_VALID,
  138. 401 => self::KEY_INVALID,
  139. 402 => self::KEY_ALREADY_USED,
  140. 403 => self::KEY_INVALID,
  141. ];
  142. if (!empty($result['code']) && isset($stateMap[$result['code']])) {
  143. if ($stateMap[$result['code']] == self::KEY_VALID
  144. && !empty($result['data']['expire_at'])
  145. ) {
  146. $keyState = self::KEY_EXPIRING;
  147. } else {
  148. $keyState = $stateMap[$result['code']];
  149. }
  150. } else {
  151. $keyState = self::KEY_CHECK_ERROR;
  152. }
  153. return $this->buildKeyState(
  154. $keyState,
  155. $result
  156. );
  157. }
  158. public function storePremiumKeyAndState($key, $state) {
  159. if (empty($state['state'])
  160. || $state['state'] === self::KEY_CHECK_ERROR
  161. ) {
  162. return false;
  163. }
  164. // store the key itself
  165. $this->settings->set(
  166. self::PREMIUM_KEY_SETTING_NAME,
  167. $key
  168. );
  169. // store the key state
  170. $this->settings->set(
  171. self::PREMIUM_KEY_STATE_SETTING_NAME,
  172. $state
  173. );
  174. }
  175. private function buildKeyState($keyState, $result) {
  176. $state = [
  177. 'state' => $keyState,
  178. 'data' => !empty($result['data']) ? $result['data'] : null,
  179. 'code' => !empty($result['code']) ? $result['code'] : self::CHECK_ERROR_UNKNOWN,
  180. ];
  181. return $state;
  182. }
  183. public function updateSubscriberCount($result) {
  184. if (
  185. (
  186. !empty($result['state'])
  187. && (
  188. $result['state'] === self::KEY_VALID
  189. || $result['state'] === self::KEY_EXPIRING
  190. )
  191. )
  192. && ($this->api instanceof API)
  193. ) {
  194. return $this->api->updateSubscriberCount($this->subscribersFeature->getSubscribersCount());
  195. }
  196. return null;
  197. }
  198. public static function invalidateKey() {
  199. $settings = SettingsController::getInstance();
  200. $settings->set(
  201. self::API_KEY_STATE_SETTING_NAME,
  202. ['state' => self::KEY_INVALID]
  203. );
  204. }
  205. public function onSettingsSave($settings) {
  206. $apiKeySet = !empty($settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key']);
  207. $premiumKeySet = !empty($settings['premium']['premium_key']);
  208. if ($apiKeySet) {
  209. $apiKey = $settings[Mailer::MAILER_CONFIG_SETTING_NAME]['mailpoet_api_key'];
  210. $state = $this->checkMSSKey($apiKey);
  211. $this->storeMSSKeyAndState($apiKey, $state);
  212. if (self::isMPSendingServiceEnabled()) {
  213. $this->updateSubscriberCount($state);
  214. }
  215. }
  216. if ($premiumKeySet) {
  217. $premiumKey = $settings['premium']['premium_key'];
  218. $state = $this->checkPremiumKey($premiumKey);
  219. $this->storePremiumKeyAndState($premiumKey, $state);
  220. }
  221. }
  222. }