AdminUserSubscription.php
2 months ago
Blacklist.php
1 year ago
Comment.php
2 months ago
Form.php
3 weeks ago
Manage.php
1 week ago
ManageSubscriptionFormRenderer.php
1 week ago
Pages.php
1 week ago
Registration.php
2 months ago
SubscriptionUrlFactory.php
1 week ago
Throttling.php
2 months ago
index.php
3 years ago
Form.php
98 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Subscription; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\API\JSON\API; |
| 9 | use MailPoet\API\JSON\Endpoint; |
| 10 | use MailPoet\API\JSON\Response as APIResponse; |
| 11 | use MailPoet\Util\Url as UrlHelper; |
| 12 | use MailPoet\WP\Functions as WPFunctions; |
| 13 | |
| 14 | class Form { |
| 15 | |
| 16 | /** @var API */ |
| 17 | private $api; |
| 18 | |
| 19 | /** @var UrlHelper */ |
| 20 | private $urlHelper; |
| 21 | |
| 22 | private const SUBSCRIBE_ENDPOINT = 'subscribers'; |
| 23 | private const SUBSCRIBE_METHOD = 'subscribe'; |
| 24 | |
| 25 | public function __construct( |
| 26 | API $api, |
| 27 | UrlHelper $urlHelper |
| 28 | ) { |
| 29 | $this->api = $api; |
| 30 | $this->urlHelper = $urlHelper; |
| 31 | } |
| 32 | |
| 33 | public function onSubmit($requestData = false) { |
| 34 | $requestData = ($requestData) ? $requestData : $_REQUEST; |
| 35 | |
| 36 | // When the admin-post action URL is hit directly without a form payload |
| 37 | // (e.g. a crawler GETting the URL), redirect away before reaching the JSON |
| 38 | // API. Otherwise processRoute() throws "Invalid API endpoint." and the |
| 39 | // exception ends up in the WordPress debug log. Mirrors Manage::onSave(). |
| 40 | $action = (isset($requestData['action']) && is_string($requestData['action'])) |
| 41 | ? sanitize_text_field(wp_unslash($requestData['action'])) |
| 42 | : ''; |
| 43 | $apiVersion = (isset($requestData['api_version']) && is_string($requestData['api_version'])) |
| 44 | ? trim($requestData['api_version']) |
| 45 | : ''; |
| 46 | $endpoint = (isset($requestData['endpoint']) && is_string($requestData['endpoint'])) |
| 47 | ? trim($requestData['endpoint']) |
| 48 | : ''; |
| 49 | $methodParamName = isset($requestData['mailpoet_method']) ? 'mailpoet_method' : 'method'; |
| 50 | $method = (isset($requestData[$methodParamName]) && is_string($requestData[$methodParamName])) |
| 51 | ? trim($requestData[$methodParamName]) |
| 52 | : ''; |
| 53 | $token = (isset($requestData['token']) && is_string($requestData['token'])) |
| 54 | ? trim($requestData['token']) |
| 55 | : ''; |
| 56 | $rawFormId = (isset($requestData['data']) && is_array($requestData['data'])) |
| 57 | ? ($requestData['data']['form_id'] ?? false) |
| 58 | : false; |
| 59 | if ( |
| 60 | $action !== 'mailpoet_subscription_form' |
| 61 | || empty($requestData['data']) |
| 62 | || !is_array($requestData['data']) |
| 63 | || (isset($requestData['data']['form_id']) && !is_scalar($rawFormId)) |
| 64 | || $apiVersion === '' |
| 65 | || $endpoint === '' |
| 66 | || $method === '' |
| 67 | || $endpoint !== self::SUBSCRIBE_ENDPOINT |
| 68 | || $method !== self::SUBSCRIBE_METHOD |
| 69 | || WPFunctions::get()->wpVerifyNonce($token, 'mailpoet_token') === false |
| 70 | ) { |
| 71 | return $this->urlHelper->redirectBack(); |
| 72 | } |
| 73 | |
| 74 | $this->api->setRequestData($requestData, Endpoint::TYPE_POST); |
| 75 | $formId = is_numeric($rawFormId) ? (int)$rawFormId : false; |
| 76 | $response = $this->api->processRoute(); |
| 77 | if ($response->status !== APIResponse::STATUS_OK) { |
| 78 | return (isset($response->meta['redirect_url'])) ? |
| 79 | $this->urlHelper->redirectTo($response->meta['redirect_url']) : |
| 80 | $this->urlHelper->redirectBack( |
| 81 | [ |
| 82 | 'mailpoet_error' => ($formId) ? $formId : true, |
| 83 | 'mailpoet_success' => null, |
| 84 | ] |
| 85 | ); |
| 86 | } else { |
| 87 | return (isset($response->meta['redirect_url'])) ? |
| 88 | $this->urlHelper->redirectTo($response->meta['redirect_url']) : |
| 89 | $this->urlHelper->redirectBack( |
| 90 | [ |
| 91 | 'mailpoet_success' => $formId, |
| 92 | 'mailpoet_error' => null, |
| 93 | ] |
| 94 | ); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 |