wizard
1 year ago
class-dashboard-controller.php
1 year ago
class-newsletter-signup-controller.php
1 year ago
class-notice-controller.php
1 year ago
class-settings-controller.php
1 year ago
class-troubleshooting-controller.php
1 year ago
class-newsletter-signup-controller.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | namespace SuperbAddons\Admin\Controllers; |
| 4 | |
| 5 | use SuperbAddons\Config\Capabilities; |
| 6 | use SuperbAddons\Data\Controllers\DomainShiftController; |
| 7 | |
| 8 | defined('ABSPATH') || exit(); |
| 9 | |
| 10 | class NewsletterSignupController |
| 11 | { |
| 12 | const ID = 'superbaddons-newsletter-form'; |
| 13 | const ACTION = 'superbaddons_newsletter_form'; |
| 14 | const META_KEY = 'superbaddons_newsletter_subscribed'; |
| 15 | |
| 16 | const NEWSLETTER_ENDPOINT = 'addons-status/newsletter/signup'; |
| 17 | |
| 18 | public static function Initialize() |
| 19 | { |
| 20 | if (!is_admin()) { |
| 21 | return; |
| 22 | } |
| 23 | add_action('wp_ajax_superbaddons_newsletter_form', array(__CLASS__, 'AjaxNewsletterSignupForm')); |
| 24 | } |
| 25 | |
| 26 | public static function AjaxNewsletterSignupForm() |
| 27 | { |
| 28 | check_ajax_referer(NewsletterSignupController::ID, 'nonce'); |
| 29 | |
| 30 | if (!current_user_can(Capabilities::CONTRIBUTOR)) { |
| 31 | wp_send_json_error(array('message' => __("You do not have permission to perform this action.", "superb-blocks"))); |
| 32 | } |
| 33 | |
| 34 | $email = sanitize_email($_POST['email']); |
| 35 | if (!is_email($email)) { |
| 36 | wp_send_json_error(array('message' => __("Invalid email address.", "superb-blocks"))); |
| 37 | } |
| 38 | |
| 39 | $response = false; |
| 40 | $response = DomainShiftController::RemotePost( |
| 41 | self::NEWSLETTER_ENDPOINT, |
| 42 | array( |
| 43 | 'headers' => array('Content-Type' => 'application/json'), |
| 44 | 'method' => 'POST', |
| 45 | 'body' => json_encode( |
| 46 | array( |
| 47 | 'action' => 'signup_newsletter', |
| 48 | 'email' => $email |
| 49 | ) |
| 50 | ) |
| 51 | ) |
| 52 | ); |
| 53 | |
| 54 | if (!$response || is_wp_error($response)) { |
| 55 | wp_send_json_error(array('message' => __("A connection error occurred. Please try again later.", "superb-blocks"))); |
| 56 | } |
| 57 | |
| 58 | $status_code = wp_remote_retrieve_response_code($response); |
| 59 | |
| 60 | if ($status_code !== 200) { |
| 61 | wp_send_json_error(array('message' => __("Something went wrong during the signup process. Please try again later or contact support for assistance.", "superb-blocks"))); |
| 62 | } |
| 63 | |
| 64 | update_user_meta(get_current_user_id(), self::META_KEY, true); |
| 65 | |
| 66 | wp_send_json_success(); |
| 67 | } |
| 68 | |
| 69 | public static function Cleanup() |
| 70 | { |
| 71 | delete_metadata('user', 0, self::META_KEY, false, true); |
| 72 | } |
| 73 | } |
| 74 |