admin-bar
1 year ago
client-migration
6 months ago
compatibility
1 year ago
customizer
1 year ago
freemius
8 months ago
menu-icons
1 year ago
metabox
1 year ago
onboarding
2 weeks ago
panel
9 months ago
post-settings
2 months ago
preloader
1 year ago
shortcodes
1 year ago
themepanel
1 year ago
widgets
8 months ago
wizard
3 years ago
adobe-font.php
1 year ago
custom-code.php
8 months ago
dashboard.php
1 year ago
image-resizer.php
1 year ago
jshrink.php
3 years ago
mautic.php
2 months ago
ocean-extra-strings.php
3 years ago
plugins-tab.php
1 year ago
update-message.php
1 year ago
utils.php
1 year ago
walker.php
4 years ago
mautic.php
128 lines
| 1 | <?php |
| 2 | /** |
| 3 | * OceanWP Mautic Integration |
| 4 | * |
| 5 | * @package Ocean_Extra |
| 6 | * @category Core |
| 7 | * @author OceanWP |
| 8 | */ |
| 9 | |
| 10 | // Exit if accessed directly. |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Ocean Extra Mautic Integration |
| 17 | */ |
| 18 | class Ocean_Extra_Mautic { |
| 19 | |
| 20 | /** |
| 21 | * Class instance. |
| 22 | * |
| 23 | * @var object |
| 24 | * @access private |
| 25 | */ |
| 26 | private static $_instance = null; |
| 27 | |
| 28 | /** |
| 29 | * Main Ocean_Extra_Mautic Instance |
| 30 | * |
| 31 | * @static |
| 32 | * @return Main Ocean_Extra_Mautic instance |
| 33 | */ |
| 34 | public static function instance() { |
| 35 | if ( is_null( self::$_instance ) ) { |
| 36 | self::$_instance = new self(); |
| 37 | } |
| 38 | return self::$_instance; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Constructor |
| 43 | */ |
| 44 | public function __construct() { |
| 45 | |
| 46 | add_action('wp_ajax_oe_mautic_subscribe', [$this, 'ajax_subscribe']); |
| 47 | add_action('wp_ajax_nopriv_oe_mautic_subscribe', [$this, 'ajax_subscribe']); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * AJAX handler |
| 52 | */ |
| 53 | public function ajax_subscribe() { |
| 54 | |
| 55 | check_ajax_referer('owp-onboarding', 'security'); |
| 56 | |
| 57 | $email = isset($_POST['email']) |
| 58 | ? sanitize_email($_POST['email']) |
| 59 | : ''; |
| 60 | |
| 61 | $user_type = isset($_POST['user_type']) |
| 62 | ? sanitize_text_field($_POST['user_type']) |
| 63 | : 'free'; |
| 64 | |
| 65 | if (empty($email) || !is_email($email)) { |
| 66 | wp_send_json_error('Invalid email address.'); |
| 67 | } |
| 68 | |
| 69 | $result = $this->subscribe_to_mautic($email, $user_type); |
| 70 | |
| 71 | if ($result === true) { |
| 72 | |
| 73 | wp_send_json_success('Successfully subscribed!'); |
| 74 | } |
| 75 | |
| 76 | wp_send_json_error($result); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Send data to Mautic |
| 81 | */ |
| 82 | private function subscribe_to_mautic($email, $user_type) { |
| 83 | |
| 84 | $mautic_base_url = 'http://mautic.oceanwp.org'; |
| 85 | |
| 86 | $forms = [ |
| 87 | 'free' => 12, |
| 88 | 'pro' => 13 |
| 89 | ]; |
| 90 | |
| 91 | $form_id = isset($forms[$user_type]) ? $forms[$user_type] : $forms['free']; |
| 92 | |
| 93 | $submit_url = "{$mautic_base_url}/form/submit"; |
| 94 | |
| 95 | $body = [ |
| 96 | "mauticform[email]" => $email, |
| 97 | "mauticform[formId]" => $form_id, |
| 98 | "mauticform[return]" => '', |
| 99 | ]; |
| 100 | |
| 101 | $response = wp_remote_post($submit_url, [ |
| 102 | 'method' => 'POST', |
| 103 | 'timeout' => 15, |
| 104 | 'headers' => [ |
| 105 | 'User-Agent' => 'OceanExtra-Onboarding/1.0' |
| 106 | ], |
| 107 | 'body' => $body |
| 108 | ]); |
| 109 | |
| 110 | if (is_wp_error($response)) { |
| 111 | return esc_html__('Connection to mailing server failed.', 'ocean-extra'); |
| 112 | } |
| 113 | |
| 114 | $code = wp_remote_retrieve_response_code($response); |
| 115 | |
| 116 | if ($code !== 200) { |
| 117 | return esc_html__('Subscription failed. Please try again.', 'ocean-extra'); |
| 118 | } |
| 119 | |
| 120 | return true; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Initialize |
| 126 | */ |
| 127 | return Ocean_Extra_Mautic::instance(); |
| 128 |