PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.3.7
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.3.7
7.8.13 7.8.13.1 trunk 3.0 3.1 3.1.1 3.1.2 3.1.3 3.1.4 4.3.2 4.4.4 4.4.5 4.4.5.1 4.4.5.4 4.6 4.6.1.1 4.6.1.4 4.7.0.2 4.7.0.3 4.7.0.7 4.7.0.9 4.7.1.0 4.7.1.1 4.8.0.0 5.0.0 5.0.1 5.0.1.1 5.0.1.2 5.1 5.1.1 5.1.2 5.1.3 5.1.3.1 5.1.3.2 5.1.4 5.1.5 6.0 6.0.1 6.0.2 6.0.3 6.0.4.2 6.0.5 6.0.6.1 6.0.7 6.0.8.1 6.0.9 7.0.0.1 7.0.2 7.0.3 7.0.4 7.1.0 7.1.1 7.2.0 7.2.1 7.3.0 7.3.1 7.3.3 7.3.5 7.3.6 7.3.7 7.4.0 7.4.1 7.4.11 7.4.13 7.4.13.1 7.4.2 7.4.3 7.4.4 7.4.5 7.4.5.1 7.4.5.2 7.4.6 7.4.7 7.5.0 7.6.0 7.6.1 7.6.3 7.6.4 7.6.6 7.7.0 7.7.1 7.8.0 7.8.1 7.8.10 7.8.10.1 7.8.10.2 7.8.11 7.8.12 7.8.12.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.8.9.1 7.8.9.2 7.8.9.3
wordpress-popup / inc / provider / class-hustle-provider-utils.php
wordpress-popup / inc / provider Last commit date
class-hustle-provider-abstract.php 5 years ago class-hustle-provider-admin-ajax.php 5 years ago class-hustle-provider-autoload.php 6 years ago class-hustle-provider-container.php 6 years ago class-hustle-provider-form-hooks-abstract.php 5 years ago class-hustle-provider-form-settings-abstract.php 5 years ago class-hustle-provider-interface.php 6 years ago class-hustle-provider-utils.php 5 years ago
class-hustle-provider-utils.php
661 lines
1 <?php
2 /**
3 * Hustle_Provider_Utils class.
4 *
5 * @package Hustle
6 * @since 3.5.0
7 */
8
9 /**
10 * Helper functions for providers
11 *
12 * @since 4.0.0
13 */
14 class Hustle_Provider_Utils {
15
16 /**
17 * Instance of Hustle Provider Utils.
18 *
19 * @since 4.0.0
20 * @var self
21 */
22 private static $instance = null;
23
24 /**
25 * Renderer class.
26 *
27 * @var Hustle_Layout_Helper
28 */
29 private static $renderer = null;
30
31 /**
32 * Slug will be used as additional info in submission entries.
33 *
34 * @since 4.0.0
35 * @var string
36 */
37 public $_last_url_request;
38
39 /**
40 * Slug will be used as additional info in submission entries.
41 *
42 * @since 4.0.0
43 * @var string
44 */
45 public $_last_data_received;
46
47 /**
48 * Slug will be used as additional info in submission entries.
49 *
50 * @since 4.0
51 * @var string
52 */
53 public $_last_data_sent;
54
55 /**
56 * Return the existing instance of Hustle_Provider_Utils, or create a new one if none exists.
57 *
58 * @since 4.0
59 * @return Hustle_Provider_Utils
60 */
61 public static function get_instance() {
62 if ( is_null( self::$instance ) ) {
63 self::$instance = new self();
64 }
65
66 return self::$instance;
67 }
68
69 /**
70 * Get the last URL request and unset it.
71 *
72 * @since 4.0
73 * @return string
74 */
75 final public function get_last_url_request() {
76 $last_url_request = $this->_last_url_request;
77 $this->_last_url_request = null;
78
79 return $last_url_request;
80 }
81
82 /**
83 * Get the last received data and unset it.
84 *
85 * @since 4.0
86 * @param bool $unset Either unset it or not.
87 * @return string
88 */
89 final public function get_last_data_received( $unset = true ) {
90 $last_data_received = $this->_last_data_received;
91 if ( $unset ) {
92 $this->_last_data_received = null;
93 }
94
95 return $last_data_received;
96 }
97
98 /**
99 * Get the last sent data and unset it.
100 *
101 * @since 4.0
102 * @return string
103 */
104 final public function get_last_data_sent() {
105 $last_data_sent = $this->_last_data_sent;
106 $this->_last_data_sent = null;
107
108 return $last_data_sent;
109 }
110
111 /**
112 * Gets all providers as list
113 *
114 * @since 3.0.5
115 * @return array
116 */
117 public static function get_registered_providers_list() {
118 $providers_list = Hustle_Providers::get_instance()->get_providers()->to_array();
119
120 // Late init properties.
121 foreach ( $providers_list as $key => $provider ) {
122 $providers_list[ $key ]['is_active'] = Hustle_Providers::get_instance()->addon_is_active( $key );
123 }
124
125 return $providers_list;
126 }
127
128 /**
129 * Get registered addons grouped by connected status
130 *
131 * @since 4.0
132 * @return array
133 */
134 public static function get_registered_addons_grouped_by_connected() {
135 $addon_list = self::get_registered_providers_list();
136 $connected_addons = array();
137 $not_connected_addons = array();
138
139 // Late init properties.
140 foreach ( $addon_list as $key => $addon ) {
141
142 if ( $addon['is_multi_on_global'] ) {
143 // Add instances to connected.
144 if ( isset( $addon['global_multi_ids'] ) && is_array( $addon['global_multi_ids'] ) ) {
145 foreach ( $addon['global_multi_ids'] as $multi_id ) {
146 $addon_array = $addon;
147 $addon_array['global_multi_id'] = $multi_id['id'];
148 $addon_array['multi_name'] = ! empty( $multi_id['label'] ) ? $multi_id['label'] : $multi_id['id'];
149 $connected_addons[] = $addon_array;
150 }
151 }
152 $not_connected_addons[] = $addon;
153 } else {
154 if ( $addon['is_connected'] ) {
155 $connected_addons[] = $addon;
156 } else {
157 if ( 'zapier' !== $key ) {
158 $not_connected_addons[] = $addon;
159 } else {
160 // Add Zapier at the top of the non-connected providers
161 // in order to promote it in a more noticeable way.
162 array_unshift( $not_connected_addons, $addon );
163 }
164 }
165 }
166 }
167
168 return array(
169 'connected' => $connected_addons,
170 'not_connected' => $not_connected_addons,
171 );
172 }
173
174 /**
175 * Get the instance of the providers that are connected to a module.
176 *
177 * @since 4.0.0
178 *
179 * @param string $module_id Model ID.
180 * @return array Hustle_Provider_Abstract[]
181 */
182 public static function get_addons_instance_connected_with_module( $module_id ) {
183 $providers = array();
184
185 $active_addons_slug = Hustle_Providers::get_instance()->get_activated_addons();
186
187 // TODO: move local list first.
188
189 foreach ( $active_addons_slug as $active_addon_slug ) {
190 $provider = self::get_provider_by_slug( $active_addon_slug );
191 if ( $provider ) {
192 if ( $provider->is_form_connected( $module_id ) ) {
193 $class_name = $provider->get_form_settings_class_name();
194 $form_settings_instance = new $class_name( $provider, $module_id );
195 $form_settings_values = $form_settings_instance->get_form_settings_values();
196 if ( ! empty( $form_settings_values['selected_global_multi_id'] ) ) {
197 $provider->selected_global_multi_id = $form_settings_values['selected_global_multi_id'];
198 }
199 $providers[] = $provider;
200 }
201 }
202 }
203
204 return $providers;
205 }
206
207 /**
208 * Get provider(s) in array format grouped by connected / not connected with $module_id
209 *
210 * Every addon inside this array will be formatted first by @see Hustle_Provider_Abstract::to_array_with_form().
211 *
212 * @since 4.0.0
213 *
214 * @param string $module_id Optional. Module ID.Get module id from URL if it's not set.
215 * @return array
216 */
217 public static function get_registered_addons_grouped_by_form_connected( $module_id = null ) {
218
219 if ( is_null( $module_id ) ) {
220 $module_id = filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT );
221 if ( is_null( $module_id ) ) {
222 $module_id = '';
223 }
224 }
225
226 $connected_addons = array();
227 $not_connected_addons = array();
228
229 $providers = self::get_registered_providers_list();
230
231 foreach ( $providers as $slug => $data ) {
232 if ( ! $data['is_connected'] ) {
233 continue;
234 }
235
236 $provider = self::get_provider_by_slug( $slug );
237
238 if ( $provider->is_allow_multi_on_form() ) {
239 $provider_array = $provider->to_array_with_form( $module_id );
240 if ( isset( $provider_array['multi_ids'] ) && is_array( $provider_array['multi_ids'] ) ) {
241 foreach ( $provider_array['multi_ids'] as $multi_id ) {
242 $provider_array['multi_id'] = $multi_id['id'];
243 $provider_array['multi_name'] = ! empty( $multi_id['label'] ) ? $multi_id['label'] : $multi_id['id'];
244 $connected_addons[] = $provider_array;
245 }
246 }
247 $not_connected_addons[] = $provider->to_array_with_form( $module_id );
248 } else {
249 if ( $provider->is_connected() && $provider->is_form_connected( $module_id ) ) {
250 $data = $provider->to_array_with_form( $module_id );
251 $data = $provider->maybe_add_multi_name( $data, $module_id );
252 $connected_addons[] = $data;
253 } else {
254 $not_connected_addons[] = $provider->to_array_with_form( $module_id );
255 }
256 }
257 }
258
259 return array(
260 'connected' => $connected_addons,
261 'not_connected' => $not_connected_addons,
262 );
263 }
264
265 /**
266 * Get registered addons.
267 *
268 * @since 4.0
269 *
270 * @return Hustle_Provider_Abstract[]
271 */
272 public static function get_registered_addons() {
273 $addons = array();
274 $registered_addons = Hustle_Providers::get_instance()->get_providers();
275
276 foreach ( $registered_addons as $slug => $registered_addon ) {
277 $addon = self::get_provider_by_slug( $slug );
278 if ( $addon instanceof Hustle_Provider_Abstract ) {
279 $addons[ $addon->get_slug() ] = $addon;
280 }
281 }
282
283 return $addons;
284 }
285
286 /**
287 * Attach default hooks for provider.
288 *
289 * Call when needed only,
290 * defined in @see Hustle_Provider_Abstract::global_hookable()
291 * and @see Hustle_Provider_Abstract::admin_hookable() on admin side.
292 *
293 * @since 4.0
294 *
295 * @param Hustle_Provider_Abstract $provider Provider instance.
296 */
297 public static function maybe_attach_addon_hook( Hustle_Provider_Abstract $provider ) {
298
299 $provider->global_hookable();
300 // Hooks that are available on admin only.
301 if ( is_admin() ) {
302 $provider->admin_hookable();
303 }
304 }
305
306 /**
307 * Get all activable providers as list
308 *
309 * @since 3.0.5
310 * @return array
311 */
312 public static function get_activable_providers_list() {
313 $providers_list = self::get_registered_providers_list();
314 foreach ( $providers_list as $key => $provider ) {
315 if ( ! $providers_list[ $key ]['is_activable'] ) {
316 unset( $providers_list[ $key ] );
317 }
318 }
319 return $providers_list;
320 }
321
322 /**
323 * Returns provider class by name
324 *
325 * @since 3.0.5
326 *
327 * @param string $slug provider slug.
328 * @return bool|Opt_In_Provider_Abstract
329 */
330 public static function get_provider_by_slug( $slug ) {
331 return Hustle_Providers::get_instance()->get_provider( $slug );
332 }
333
334 /**
335 * Return if the passed provider is active or not.
336 *
337 * @since 4.0.0
338 *
339 * @param string $slug Provider's slug.
340 * @return boolean
341 */
342 public static function is_provider_active( $slug ) {
343 return Hustle_Providers::get_instance()->addon_is_active( $slug );
344 }
345
346 /**
347 * Get the modules that are connected to the given provider.
348 * Optionally, check that the given global multi id is in use.
349 *
350 * @since 4.0.1
351 *
352 * @param string $slug Provider's slug.
353 * @param boolean $global_multi_id Id of the provider global instance.
354 * @return array Hustle_Module_Model[]
355 */
356 public static function get_modules_by_active_provider( $slug, $global_multi_id = false ) {
357
358 $modules_ids = Hustle_Module_Collection::get_active_providers_module( $slug );
359 $modules = array();
360
361 foreach ( $modules_ids as $id ) {
362
363 $module = new Hustle_Module_Model( $id );
364
365 if ( is_wp_error( $module ) ) {
366 continue;
367 }
368
369 if ( $global_multi_id ) {
370
371 $provider_settings = $module->get_provider_settings( $slug );
372
373 // If the provider in this module isn't connected to the instance being disconnected, skip.
374 if ( ! isset( $provider_settings['selected_global_multi_id'] ) || $provider_settings['selected_global_multi_id'] !== $global_multi_id ) {
375 continue;
376 }
377 }
378
379 $modules[] = $module;
380 }
381
382 return $modules;
383 }
384
385 /**
386 * Format Form Fields
387 *
388 * @since 4.0
389 *
390 * @param Hustle_Module_Model $module Module instance.
391 *
392 * @return array
393 */
394 public static function addon_format_form_fields( Hustle_Module_Model $module ) {
395 $formatted_fields = array();
396 $fields = $module->get_form_fields();
397
398 foreach ( $fields as $field ) {
399 $ignored_fields = Hustle_Entry_Model::ignored_fields();
400 if ( in_array( $field['type'], $ignored_fields, true ) ) {
401 continue;
402 }
403
404 $formatted_fields[] = $field;
405 }
406
407 return $formatted_fields;
408 }
409
410 /**
411 * Find addon meta data from entry model that saved on db
412 *
413 * @since 4.0.0
414 *
415 * @param Hustle_Provider_Abstract $connected_addon Provider instance.
416 * @param Hustle_Entry_Model $entry_model Entry model.
417 *
418 * @return array
419 */
420 public static function find_addon_meta_data_from_entry_model( Hustle_Provider_Abstract $connected_addon, Hustle_Entry_Model $entry_model ) {
421 $addon_meta_data = array();
422 $addon_meta_data_prefix = 'hustle_provider_' . $connected_addon->get_slug() . '_';
423 foreach ( $entry_model->meta_data as $key => $meta_datum ) {
424 if ( false !== stripos( $key, $addon_meta_data_prefix ) ) {
425 $addon_meta_data[] = array(
426 'name' => str_ireplace( $addon_meta_data_prefix, '', $key ),
427 'value' => $meta_datum['value'],
428 );
429 }
430 }
431
432 /**
433 * Filter addon's meta data retrieved from db
434 *
435 * @since 4.0
436 */
437 $addon_meta_data = apply_filters( 'hustle_provider_meta_data_from_entry_model', $addon_meta_data, $connected_addon, $entry_model, $addon_meta_data_prefix );
438
439 return $addon_meta_data;
440 }
441
442 /**
443 * Unset technical data of a form.
444 *
445 * @since 4.0.0
446 *
447 * @param array $data $_POST.
448 *
449 * @return array
450 */
451 public static function format_submitted_data_for_addon( $data ) {
452 $new_data = $data;
453 unset( $new_data['form'], $new_data['module_id'], $new_data['uri'], $new_data['hustle_module_id'], $new_data['post_id'], $new_data['g-recaptcha-response'], $new_data['hustle_sub_type'], $new_data['recaptcha-response'] );
454 $new_data = apply_filters( 'hustle_provider_form_formatted_submitted_data', $new_data, $data );
455
456 return $new_data;
457 }
458
459 /**
460 * Retrieves the HTML markup given an array of options.
461 * Renders it from the file "general/option.php", which is a template.
462 * The array should be something like:
463 * array(
464 * "optin_url_label" => array(
465 * "id" => "optin_url_label",
466 * "for" => "optin_url",
467 * "value" => "Enter a Webhook URL:",
468 * "type" => "label",
469 * ),
470 * "optin_url_field_wrapper" => array(
471 * "id" => "optin_url_id",
472 * "class" => "optin_url_id_wrapper",
473 * "type" => "wrapper",
474 * "elements" => array(
475 * "optin_url_field" => array(
476 * "id" => "optin_url",
477 * "name" => "api_key",
478 * "type" => "text",
479 * "default" => "",
480 * "value" => "",
481 * "placeholder" => "",
482 * "class" => "wpmudev-input_text",
483 * )
484 * )
485 * ),
486 * );
487 *
488 * @since 4.0.0
489 * @since 4.2.0 Uses Hustle_Layout_Helper::render() instead of Opt_In::static_render().
490 *
491 * @uses Hustle_Layout_Helper::render()
492 * @param array $options Layout options.
493 * @return string
494 */
495 public static function get_html_for_options( $options ) {
496 $html = '';
497 $renderer = self::get_renderer();
498 foreach ( $options as $key => $option ) {
499 $html .= $renderer->render( 'general/option', array_merge( $option, array( 'key' => $key ) ), true );
500 }
501 return $html;
502 }
503
504 /**
505 * Temporary
506 *
507 * @todo remove
508 */
509 private static function get_renderer() {
510 if ( ! self::$renderer ) {
511 self::$renderer = new Hustle_Layout_Helper();
512 }
513 return self::$renderer;
514 }
515
516 /**
517 * Return the markup used for the title of Integrations modal.
518 *
519 * @since 4.0.0
520 *
521 * @param string $title Integration wizard title.
522 * @param string $subtitle Integration wizard subtitle.
523 * @param string $class Title wrapper extra classes.
524 * @return string
525 */
526 public static function get_integration_modal_title_markup( $title = '', $subtitle = '', $class = '' ) {
527
528 $html = '<div class="integration-header ' . esc_attr( $class ) . '">';
529
530 if ( ! empty( $title ) ) {
531 $html .= '<h3 class="sui-box-title sui-lg" id="dialogTitle2">' . esc_html( $title ) . '</h3>';
532 }
533
534 if ( ! empty( $subtitle ) ) {
535 $html .= '<p class="sui-description">' . $subtitle . '</p>';
536 }
537
538 $html .= '</div>';
539
540 return $html;
541 }
542
543 /**
544 * Return the markup for buttons.
545 *
546 * @since 4.0.0
547 *
548 * @param string $value Button text.
549 * @param string $class Extra classes.
550 * @param string $action next/prev/close/connect/disconnect. Action that this button triggers.
551 * @param bool $loading whether the button should have the 'loading' markup.
552 * @param bool $disabled Whether the button is disabled.
553 * @param string $custom_url Url to which the button should take the user to, if any.
554 * @return string
555 */
556 public static function get_provider_button_markup( $value = '', $class = '', $action = '', $loading = false, $disabled = false, $custom_url = '' ) {
557
558 $html = '';
559 $action_class = '';
560
561 if ( ! empty( $action ) ) {
562
563 switch ( $action ) {
564 case 'next':
565 $action_class = 'hustle-provider-next ';
566 break;
567 case 'prev':
568 $action_class = 'hustle-provider-back ';
569 break;
570 case 'close':
571 $action_class = 'hustle-provider-close hustle-modal-close ';
572 break;
573 case 'connect':
574 $action_class = 'hustle-provider-connect ';
575 break;
576 case 'disconnect':
577 $action_class = 'hustle-provider-disconnect ';
578 break;
579 case 'disconnect_form':
580 $action_class = 'hustle-provider-form-disconnect ';
581 break;
582 case 'refresh_list':
583 $action_class = 'hustle-refresh-email-lists ';
584 break;
585 default:
586 $action_class = '';
587 }
588 }
589
590 if ( empty( $custom_url ) ) {
591 $tag = 'button';
592 $custom_attr = 'type="button"';
593 } else {
594 $tag = 'a';
595 $custom_attr = 'href="' . esc_url( $custom_url ) . '"';
596 }
597
598 if ( $loading ) {
599 $action_class .= 'hustle-onload-icon-action ';
600 $inner = '<span class="sui-loading-text">' . esc_html( $value ) . '</span><i class="sui-icon-loader sui-loading" aria-hidden="true"></i>';
601
602 } else {
603 $inner = esc_html( $value );
604 }
605
606 if ( 'refresh_list' === $action ) {
607
608 $html .= sprintf(
609 '<%1$s class="sui-button-icon sui-tooltip %2$s" data-tooltip="%3$s" %4$s >',
610 $tag,
611 esc_attr( $action_class . $class ),
612 __( 'Refresh list', 'hustle' ),
613 disabled( $disabled, true, false )
614 );
615 $html .= '<span class="sui-loading-text" aria-hidden="true">';
616 $html .= '<i class="sui-icon-refresh"></i>';
617 $html .= '</span>';
618 $html .= '<i class="sui-icon-loader sui-loading" aria-hidden="true"></i>';
619 $html .= '<span class="sui-screen-reader-text">' . esc_html( $value ) . '</span>';
620 $html .= '</' . $tag . '>';
621
622 } else {
623
624 $html .= '<' . $tag . ' ' . $custom_attr . ' class="sui-button ' . esc_attr( $action_class ) . esc_attr( $class ) . '" ' . disabled( $disabled, true, false ) . '>';
625 $html .= $inner;
626 $html .= '</' . $tag . '>';
627
628 }
629
630 return $html;
631 }
632
633 /**
634 * Adds an entry to debug log
635 *
636 * By default it will check `HUSTLE_PROVIDER_DEBUG` to decide whether to add the log,
637 * then will check `filters`.
638 *
639 * @since 4.0
640 */
641 public static function maybe_log() {
642 $enabled = ( ! defined( 'HUSTLE_PROVIDER_DEBUG' ) || HUSTLE_PROVIDER_DEBUG );
643
644 /**
645 * Filter to enable or disable log for Hustle
646 *
647 * @since 4.0
648 *
649 * @param bool $enabled current enabled status
650 */
651 $enabled = apply_filters( 'hustle_provider_enable_log', $enabled );
652
653 if ( $enabled && is_callable( array( 'Opt_In_Utils', 'maybe_log' ) ) ) {
654 $args = array( '[PROVIDER]' );
655 $fargs = func_get_args();
656 $args = array_merge( $args, $fargs );
657 call_user_func_array( array( 'Opt_In_Utils', 'maybe_log' ), $args );
658 }
659 }
660 }
661