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
78 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 | if (!isset($_POST['email']) || empty($_POST['email'])) { |
| 35 | wp_send_json_error(array('message' => __("Email address is required.", "superb-blocks"))); |
| 36 | } |
| 37 | |
| 38 | $email = sanitize_email(wp_unslash($_POST['email'])); |
| 39 | if (!is_email($email)) { |
| 40 | wp_send_json_error(array('message' => __("Invalid email address.", "superb-blocks"))); |
| 41 | } |
| 42 | |
| 43 | $response = false; |
| 44 | $response = DomainShiftController::RemotePost( |
| 45 | self::NEWSLETTER_ENDPOINT, |
| 46 | array( |
| 47 | 'headers' => array('Content-Type' => 'application/json'), |
| 48 | 'method' => 'POST', |
| 49 | 'body' => wp_json_encode( |
| 50 | array( |
| 51 | 'action' => 'signup_newsletter', |
| 52 | 'email' => $email |
| 53 | ) |
| 54 | ) |
| 55 | ) |
| 56 | ); |
| 57 | |
| 58 | if (!$response || is_wp_error($response)) { |
| 59 | wp_send_json_error(array('message' => __("A connection error occurred. Please try again later.", "superb-blocks"))); |
| 60 | } |
| 61 | |
| 62 | $status_code = wp_remote_retrieve_response_code($response); |
| 63 | |
| 64 | if ($status_code !== 200) { |
| 65 | wp_send_json_error(array('message' => __("Something went wrong during the signup process. Please try again later or contact support for assistance.", "superb-blocks"))); |
| 66 | } |
| 67 | |
| 68 | update_user_meta(get_current_user_id(), self::META_KEY, true); |
| 69 | |
| 70 | wp_send_json_success(); |
| 71 | } |
| 72 | |
| 73 | public static function Cleanup() |
| 74 | { |
| 75 | delete_metadata('user', 0, self::META_KEY, false, true); |
| 76 | } |
| 77 | } |
| 78 |