PluginProbe
Email Marketing by EmailOctopus / 3.1.8
Email Marketing by EmailOctopus v3.1.8
3.1.10 trunk 1.0.0 1.0.1 1.1.0 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 All 39 releases
emailoctopus / src / Form.php

Form.php in Email Marketing by EmailOctopus 3.1.8, at src/Form.php

290 lines 7.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailOctopus;
4
5 use InvalidArgumentException;
6
7 // Exit if accessed directly.
8 if (!defined('ABSPATH')) {
9 exit;
10 }
11
12 /**
13 * Form class for EmailOctopus.
14 */
15 class Form
16 {
17 /**
18 * The EmailOctopus form ID.
19 */
20 private string $form_id = '';
21
22 /**
23 * Form data as received from EmailOctopus.com API.
24 */
25 private array $form_data = [];
26
27 /**
28 * Errors to be rendered to the UI.
29 */
30 private array $errors = [];
31
32 /**
33 * Is this a pre-3.0 form?
34 */
35 private bool $is_deprecated_form = false;
36
37 /**
38 * Constructor.
39 *
40 * @param string $form_id The form ID.
41 */
42 public function __construct(string $form_id = '', bool $populate_data_from_api = true)
43 {
44 if (empty($form_id)) {
45 // Nothing to render: form not selected yet.
46 return;
47 }
48
49 $this->form_id = $form_id;
50 if ($populate_data_from_api) {
51 $this->populate_data_from_api();
52 }
53 }
54
55 public function set_form_data(array $data)
56 {
57 $this->form_data = $data;
58 }
59
60 /**
61 * Sets Form Data available to other class methods.
62 */
63 private function populate_data_from_api(): void
64 {
65 if (empty($this->form_id)) {
66 throw new InvalidArgumentException(
67 'This form does not have a form_id set, so its data cannot be retrieved from the API.'
68 );
69 }
70
71 if (Utils::site_has_deprecated_forms()) {
72 $deprecated_form_data = Deprecated::get_form_data($this->form_id);
73
74 if (is_array($deprecated_form_data) && !empty($deprecated_form_data)) {
75 $this->is_deprecated_form = true;
76 $this->form_data = $deprecated_form_data;
77
78 return;
79 }
80 }
81
82 if ($this->has_errors()) {
83 return;
84 }
85
86 $data = Utils::get_api_data(
87 'https://emailoctopus.com/api/1.6/forms/' . $this->form_id,
88 ['api_key' => get_option('emailoctopus_api_key', false)],
89 MINUTE_IN_SECONDS,
90 is_admin()
91 );
92
93 if (is_wp_error($data) || isset($data->error)) {
94 $this->errors[] = $data->error;
95
96 return;
97 }
98
99 $this->set_form_data(
100 (array) $data // Cast from stdClass to array
101 );
102 }
103
104 /**
105 * Accessor for form data.
106 */
107 public function get_form_data(): array
108 {
109 return $this->form_data ?? [];
110 }
111
112 /**
113 * Get the form's ID as defined on EmailOctopus.com.
114 */
115 public function get_id(): string
116 {
117 return trim($this->get_form_data()['id'] ?? '');
118 }
119
120 /**
121 * Get the form's Name as defined on EmailOctopus.com.
122 */
123 public function get_name(): string
124 {
125 return trim($this->get_form_data()['name'] ?? '');
126 }
127
128 /**
129 * Get the form's Type as defined on EmailOctopus.com.
130 */
131 public function get_type(): string
132 {
133 return trim($this->get_form_data()['type'] ?? '');
134 }
135
136 /**
137 * Get the form's screenshot URL as defined on EmailOctopus.com.
138 */
139 public function get_screenshot_url(): string
140 {
141 return trim($this->get_form_data()['screenshot_url'] ?? '');
142 }
143
144 /**
145 * Get the ID of this form's connected List.
146 */
147 public function get_list_id(): string
148 {
149 return trim($this->get_form_data()['list_id'] ?? '');
150 }
151
152 /**
153 * Get the Name of this form's connected List.
154 */
155 public function get_list_name(): string
156 {
157 if (!empty($this->get_form_data()['list_name'])) {
158 return trim($this->get_form_data()['list_name']);
159 }
160
161 $list_id = $this->get_list_id();
162
163 if (empty($list_id)) {
164 return '';
165 }
166
167 $list = new ContactList($list_id);
168
169 return trim($list->get_list_data()['name'] ?? '');
170 }
171
172 /**
173 * Get the script URL for this form.
174 */
175 public function get_script_url(): string
176 {
177 return trim($this->get_form_data()['script_url'] ?? '');
178 }
179
180 /**
181 * Get the script tag necessary for rendering the form.
182 */
183 public function get_script_tag(): string
184 {
185 return sprintf(
186 '<script async src="%s" data-form="%s"></script>',
187 $this->get_script_url(),
188 $this->get_id()
189 );
190 }
191
192 /**
193 * Looks for an `emailoctopus_form` post type corresponding to the Form ID.
194 *
195 * @return array|int|WP_Post
196 */
197 public function get_form_post()
198 {
199 $form_post = get_posts([
200 'post_type' => 'emailoctopus_form',
201 'posts_per_page' => 1,
202 'post_status' => 'publish',
203 'meta_key' => '_emailoctopus_form_id',
204 'meta_value' => $this->form_id,
205 'no_found_rows' => true,
206 ]);
207
208 if (empty($form_post)) {
209 $form_post_id = $this->create_form_post();
210 $form_post = [get_post($form_post_id)];
211 }
212
213 return $form_post[0];
214 }
215
216 /**
217 * Creates a new `emailoctopus_form` post type, whose meta links the WordPress data to the EmailOctopus.com version of the form.
218 */
219 public function create_form_post(): int
220 {
221 $post_id = wp_insert_post(
222 [
223 'post_type' => 'emailoctopus_form',
224 'post_status' => 'publish',
225 ]
226 );
227
228 update_post_meta($post_id, '_emailoctopus_form_id', $this->form_id);
229
230 return $post_id;
231 }
232
233 public function has_errors(): bool
234 {
235 return !empty($this->errors);
236 }
237
238 /**
239 * Display form.
240 *
241 * @todo Remove deprecated method arguments when pre-3.0 style forms no longer supported.
242 */
243 public function render_form(): void
244 {
245 if ($this->is_deprecated_form()) {
246 Deprecated::output_template($this->form_id);
247
248 return;
249 }
250
251 if ($this->has_errors()) {
252 ?>
253 <div class="emailoctopus-form-error" role="alert">
254 <p>
255 <?php
256 esc_html_e('This EmailOctopus form cannot be rendered.', 'emailoctopus');
257 if (is_user_logged_in()) {
258 echo ' ' . esc_html__('Errors:', 'emailoctopus');
259 } ?>
260 </p>
261 <?php
262 if (is_user_logged_in()) {
263 echo '<ul>';
264 foreach ($this->errors as $error) {
265 echo sprintf(
266 '<li>%s: %s</li>',
267 isset($error->code) ? esc_html($error->code) : '',
268 isset($error->message) ? esc_html($error->message) : ''
269 );
270 }
271 echo '</ul>';
272 } ?>
273 </div>
274 <?php
275
276 return;
277 }
278
279 echo $this->get_script_tag();
280 }
281
282 /**
283 * Accessor for deprecated form flag.
284 */
285 public function is_deprecated_form(): bool
286 {
287 return $this->is_deprecated_form;
288 }
289 }
290