Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/bdthemes-element-pack/modules/mailchimp/module.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 +
3 + namespace ElementPack\Modules\Mailchimp;
4 +
5 + use ElementPack\Base\Element_Pack_Module_Base;
6 +
7 + if (!defined('ABSPATH')) exit; // Exit if accessed directly
8 +
9 + class Module extends Element_Pack_Module_Base {
10 +
11 + public function __construct() {
12 + parent::__construct();
13 +
14 + add_action('wp_ajax_element_pack_mailchimp_subscribe', [$this, 'mailchimp_subscribe']);
15 + add_action('wp_ajax_nopriv_element_pack_mailchimp_subscribe', [$this, 'mailchimp_subscribe']);
16 + }
17 +
18 + public function get_name() {
19 + return 'mailchimp';
20 + }
21 +
22 + public function get_widgets() {
23 +
24 + $widgets = ['Mailchimp'];
25 +
26 + return $widgets;
27 + }
28 +
29 + /**
30 + * subscribe mailchimp with api key
31 + * @param string $email any valid email
32 + * @param string $status subscribe or unsubscribe
33 + * @param array $merge_fields First name and last name of subscriber
34 + * @return [type] [description]
35 + */
36 + public function mailchimp_subscriber_status($email, $status, $merge_fields = array('FNAME' => '', 'LNAME' => '')) {
37 +
38 + $options = get_option('element_pack_api_settings');
39 + $list_id = (!empty($options['mailchimp_list_id'])) ? $options['mailchimp_list_id'] : ''; // Your list is here
40 + $api_key = (!empty($options['mailchimp_api_key'])) ? $options['mailchimp_api_key'] : ''; // Your mailchimp api key here
41 +
42 + $args = array(
43 + 'method' => 'PUT',
44 + 'headers' => array(
45 + 'Authorization' => 'Basic ' . base64_encode('user:' . $api_key)
46 + ),
47 + 'body' => json_encode(array(
48 + 'email_address' => $email,
49 + 'status' => $status,
50 + 'merge_fields' => $merge_fields
51 + ))
52 + );
53 + $response = wp_remote_post('https://' . substr($api_key, strpos($api_key, '-') + 1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($email)), $args);
54 +
55 + $body = json_decode($response['body']);
56 +
57 + return $body;
58 + }
59 +
60 +
61 + public function mailchimp_subscribe() {
62 +
63 + $fname = (isset($_POST['fname']) && !empty($_POST['fname'])) ? sanitize_text_field( wp_unslash( $_POST['fname'] ) ) : '';
64 + $result = $this->mailchimp_subscriber_status( isset( $_POST['email'] ) ? sanitize_text_field( wp_unslash( $_POST['email'] ) ) : '', 'subscribed', ['FNAME' => $fname, 'LNAME' => '']);
65 +
66 + if ($result->status == 400) {
67 + if (isset($result->detail) && !empty($result->detail)) {
68 + echo '<div class="bdt-text-warning">' . esc_html($result->detail) . '</div>';
69 + } else {
70 + echo '<div class="bdt-text-warning">' . esc_html_x('Your request could not be processed', 'Mailchimp String', 'bdthemes-element-pack') . '</div>';
71 + }
72 + } elseif ($result->status == 401) {
73 + echo '<div class="bdt-text-warning">' . esc_html_x('Error: You did not set the API keys or List ID in admin settings!', 'Mailchimp String', 'bdthemes-element-pack') . '</div>';
74 + } elseif ($result->status == 200 || $result->status == 'subscribed') {
75 + echo '<span bdt-icon="icon: check" class="bdt-icon"></span> ' . esc_html_x('Thank you, You have subscribed successfully', 'Mailchimp String', 'bdthemes-element-pack');
76 + } else {
77 + echo '<div class="bdt-text-danger">' . esc_html_x('An unexpected internal error has occurred. Please contact Support for more information.', 'Mailchimp String', 'bdthemes-element-pack') . '</div>';
78 + }
79 + die;
80 + }
81 + }
82 +