PluginProbe
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation / trunk
WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation vtrunk
1.4.48 1.4.47 1.4.46 1.4.45 1.4.44 1.4.43 1.4.42 1.4.41 1.4.40 1.4.39 1.4.38 1.4.37 1.4.36 1.1.2 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 All 64 releases
optin / includes / integrations / implementations / class-sendfox.php

class-sendfox.php in WowOptin: Next-Gen Popup Maker – Create Stunning Popups and Optins for Lead Generation trunk, at includes/integrations/implementations/class-sendfox.php

189 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php // phpcs:ignore
2
3 namespace OPTN\Includes\Integrations\Implementations;
4
5 use OPTN\Includes\Integrations\Base\BaseMailIntegration;
6 use OPTN\Includes\Utils\Cache;
7 use OPTN\Includes\Utils\Utils;
8
9 defined( 'ABSPATH' ) || exit;
10
11 /**
12 * Sendfox Integration class.
13 *
14 * @link https://help.sendfox.com/category/270-api-integrations
15 */
16 class Sendfox extends BaseMailIntegration {
17
18 /**
19 * Get name
20 *
21 * @return string
22 */
23 public function get_name() {
24 return 'sendfox';
25 }
26
27 /**
28 * Adds a subscribe to Sendfox
29 *
30 * @param array $data data.
31 * @return bool
32 *
33 * @link https://help.sendfox.com/article/278-endpoints
34 */
35 public function add_subscriber( $data ): bool {
36
37 if ( ! isset( $data['fields'] ) || ! isset( $data['fields']['email'] ) ) {
38 return false;
39 }
40
41 if ( isset( $data['fields']['lists'] ) ) {
42 $data['fields']['lists'] = array_map( 'intval', $data['fields']['lists'] );
43 }
44
45 $res = wp_remote_post(
46 'https://api.sendfox.com/contacts',
47 array(
48 'body' => wp_json_encode( $data['fields'] ),
49 'timeout' => 45,
50 'headers' => $this->get_headers(),
51 )
52 );
53
54 // Response code should be either 200 or 201.
55 if ( ! is_wp_error( $res ) && in_array( wp_remote_retrieve_response_code( $res ), array( 200, 201 ) ) ) { // phpcs:ignore
56 $this->add_lead( $data );
57 return true;
58 }
59
60 return false;
61 }
62
63 /**
64 * Adds a lead to the leads table
65 *
66 * @param array $data data.
67 * @return void
68 */
69 public function add_lead( $data ) {
70
71 $fields = Utils::sanitize_json_array( $data['fields'] );
72 $fields['ip'] = Utils::get_user_ip();
73
74 $leads_data = array(
75 'email' => sanitize_email( $fields['email'] ),
76 'name' => trim( ( $fields['first_name'] ?? '' ) . ' ' . ( $fields['last_name'] ?? '' ) ),
77 'conv_id' => intval( $data['conv_id'] ),
78 'data' => $fields,
79 'integration' => 'sendfox',
80 );
81
82 $this->db->add_lead( $leads_data );
83 }
84
85
86 /**
87 * Gets the fields for Sendfox
88 *
89 * @param array $args receive data as array.
90 * @return array
91 *
92 * @link https://help.sendfox.com/article/278-endpoints
93 */
94 public function get_fields( $args = array() ): array {
95 return array(
96 array(
97 'value' => 'email',
98 'label' => 'Email',
99 ),
100 array(
101 'value' => 'first_name',
102 'label' => 'First Name',
103 ),
104 array(
105 'value' => 'last_name',
106 'label' => 'Last Name',
107 ),
108 );
109 }
110
111 /**
112 * Gets lists for sendfox
113 *
114 * @param array $args receive data as array.
115 * @return array
116 *
117 * @link https://help.sendfox.com/article/278-endpoints
118 */
119 public function get_lists( $args = array() ): array {
120 $use_cache = $args['use_cache'];
121
122 if ( $use_cache ) {
123 $res = $this->get_cached_lists();
124
125 if ( ! empty( $res ) ) {
126 return $res;
127 }
128 }
129
130 $max_pages = 100;
131 $curr_page = 1;
132 $ret = array();
133
134 while ( true ) {
135 $res = wp_remote_get(
136 'https://api.sendfox.com/lists?page=' . $curr_page,
137 array(
138 'timeout' => 45,
139 'headers' => $this->get_headers(),
140 )
141 );
142
143 if ( is_wp_error( $res ) || 200 != wp_remote_retrieve_response_code( $res ) ) { // phpcs:ignore
144 break;
145 }
146
147 $body = wp_remote_retrieve_body( $res );
148 $body = json_decode( $body, true );
149
150 if ( empty( $body['data'] ) ) {
151 break;
152 }
153
154 foreach ( $body['data'] as $d ) {
155 $ret[] = array(
156 'value' => $d['id'],
157 'label' => $d['name'],
158 );
159 }
160
161 ++$curr_page;
162
163 if ( $curr_page > $max_pages ) {
164 break;
165 }
166 }
167
168 $this->set_lists_cache( $ret );
169
170 return $ret;
171 }
172
173 /**
174 * Gets headers with authorization token
175 *
176 * @return array
177 */
178 protected function get_headers() {
179
180 $settings = $this->get_settings();
181
182 return array(
183 'Content-Type' => 'application/json',
184 'Accept' => 'application/json',
185 'Authorization' => 'Bearer ' . $settings['apiKey'],
186 );
187 }
188 }
189