PluginProbe ʕ •ᴥ•ʔ
Hustle – Email Marketing, Lead Generation, Optins, Popups / 7.8.14.1
Hustle – Email Marketing, Lead Generation, Optins, Popups v7.8.14.1
7.8.14 7.8.14.1 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 months ago class-hustle-provider-admin-ajax.php 1 month ago class-hustle-provider-autoload.php 5 months ago class-hustle-provider-container.php 3 years ago class-hustle-provider-form-hooks-abstract.php 5 months ago class-hustle-provider-form-settings-abstract.php 1 month ago class-hustle-provider-interface.php 5 months ago class-hustle-provider-utils.php 1 month ago
class-hustle-provider-utils.php
665 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_value Either unset it or not.
87 * @return string
88 */
89 final public function get_last_data_received( $unset_value = true ) {
90 $last_data_received = $this->last_data_received;
91 if ( $unset_value ) {
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 } elseif ( $addon['is_connected'] ) {
154 $connected_addons[] = $addon;
155 } elseif ( 'zapier' !== $key ) {
156 $not_connected_addons[] = $addon;
157 } else {
158 // Add Zapier at the top of the non-connected providers
159 // in order to promote it in a more noticeable way.
160 array_unshift( $not_connected_addons, $addon );
161 }
162 }
163
164 return array(
165 'connected' => $connected_addons,
166 'not_connected' => $not_connected_addons,
167 );
168 }
169
170 /**
171 * Get the instance of the providers that are connected to a module.
172 *
173 * @since 4.0.0
174 *
175 * @param string $module_id Model ID.
176 * @return array Hustle_Provider_Abstract[]
177 */
178 public static function get_addons_instance_connected_with_module( $module_id ) {
179 $providers = array();
180
181 $active_addons_slug = Hustle_Providers::get_instance()->get_activated_addons();
182
183 // TODO: move local list first.
184
185 foreach ( $active_addons_slug as $active_addon_slug ) {
186 $provider = self::get_provider_by_slug( $active_addon_slug );
187 if ( $provider ) {
188 if ( $provider->is_form_connected( $module_id ) ) {
189 $class_name = $provider->get_form_settings_class_name();
190 $form_settings_instance = new $class_name( $provider, $module_id );
191 $form_settings_values = $form_settings_instance->get_form_settings_values();
192 if ( ! empty( $form_settings_values['selected_global_multi_id'] ) ) {
193 $provider->selected_global_multi_id = $form_settings_values['selected_global_multi_id'];
194 }
195 $providers[] = $provider;
196 }
197 }
198 }
199
200 return $providers;
201 }
202
203 /**
204 * Get provider(s) in array format grouped by connected / not connected with $module_id
205 *
206 * Every addon inside this array will be formatted first by @see Hustle_Provider_Abstract::to_array_with_form().
207 *
208 * @since 4.0.0
209 *
210 * @param string $module_id Optional. Module ID.Get module id from URL if it's not set.
211 * @return array
212 */
213 public static function get_registered_addons_grouped_by_form_connected( $module_id = null ) {
214
215 if ( is_null( $module_id ) ) {
216 $module_id = filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT );
217 if ( is_null( $module_id ) ) {
218 $module_id = '';
219 }
220 }
221
222 $connected_addons = array();
223 $not_connected_addons = array();
224
225 $providers = self::get_registered_providers_list();
226
227 foreach ( $providers as $slug => $data ) {
228 if ( ! $data['is_connected'] ) {
229 continue;
230 }
231
232 $provider = self::get_provider_by_slug( $slug );
233
234 if ( $provider->is_allow_multi_on_form() ) {
235 $provider_array = $provider->to_array_with_form( $module_id );
236 if ( isset( $provider_array['multi_ids'] ) && is_array( $provider_array['multi_ids'] ) ) {
237 foreach ( $provider_array['multi_ids'] as $multi_id ) {
238 $provider_array['multi_id'] = $multi_id['id'];
239 $provider_array['multi_name'] = ! empty( $multi_id['label'] ) ? $multi_id['label'] : $multi_id['id'];
240 $connected_addons[] = $provider_array;
241 }
242 }
243 $not_connected_addons[] = $provider->to_array_with_form( $module_id );
244 } elseif ( $provider->is_connected() && $provider->is_form_connected( $module_id ) ) {
245 $data = $provider->to_array_with_form( $module_id );
246 $data = $provider->maybe_add_multi_name( $data, $module_id );
247 $connected_addons[] = $data;
248 } else {
249 $not_connected_addons[] = $provider->to_array_with_form( $module_id );
250 }
251 }
252
253 return array(
254 'connected' => $connected_addons,
255 'not_connected' => $not_connected_addons,
256 );
257 }
258
259 /**
260 * Get registered addons.
261 *
262 * @since 4.0
263 *
264 * @return Hustle_Provider_Abstract[]
265 */
266 public static function get_registered_addons() {
267 $addons = array();
268 $registered_addons = Hustle_Providers::get_instance()->get_providers();
269
270 foreach ( $registered_addons as $slug => $registered_addon ) {
271 $addon = self::get_provider_by_slug( $slug );
272 if ( $addon instanceof Hustle_Provider_Abstract ) {
273 $addons[ $addon->get_slug() ] = $addon;
274 }
275 }
276
277 return $addons;
278 }
279
280 /**
281 * Attach default hooks for provider.
282 *
283 * Call when needed only,
284 * defined in @see Hustle_Provider_Abstract::global_hookable()
285 * and @see Hustle_Provider_Abstract::admin_hookable() on admin side.
286 *
287 * @since 4.0
288 *
289 * @param Hustle_Provider_Abstract $provider Provider instance.
290 */
291 public static function maybe_attach_addon_hook( Hustle_Provider_Abstract $provider ) {
292
293 $provider->global_hookable();
294 // Hooks that are available on admin only.
295 if ( is_admin() ) {
296 $provider->admin_hookable();
297 }
298 }
299
300 /**
301 * Get all activable providers as list
302 *
303 * @since 3.0.5
304 * @return array
305 */
306 public static function get_activable_providers_list() {
307 $providers_list = self::get_registered_providers_list();
308 foreach ( $providers_list as $key => $provider ) {
309 if ( ! $providers_list[ $key ]['is_activable'] ) {
310 unset( $providers_list[ $key ] );
311 }
312 }
313 return $providers_list;
314 }
315
316 /**
317 * Returns provider class by name
318 *
319 * @since 3.0.5
320 *
321 * @param string $slug provider slug.
322 * @return bool|Opt_In_Provider_Abstract
323 */
324 public static function get_provider_by_slug( $slug ) {
325 return Hustle_Providers::get_instance()->get_provider( $slug );
326 }
327
328 /**
329 * Return if the passed provider is active or not.
330 *
331 * @since 4.0.0
332 *
333 * @param string $slug Provider's slug.
334 * @return boolean
335 */
336 public static function is_provider_active( $slug ) {
337 return Hustle_Providers::get_instance()->addon_is_active( $slug );
338 }
339
340 /**
341 * Get the modules that are connected to the given provider.
342 * Optionally, check that the given global multi id is in use.
343 *
344 * @since 4.0.1
345 *
346 * @param string $slug Provider's slug.
347 * @param boolean $global_multi_id Id of the provider global instance.
348 * @return array Hustle_Module_Model[]
349 */
350 public static function get_modules_by_active_provider( $slug, $global_multi_id = false ) {
351
352 $modules_ids = Hustle_Module_Collection::get_active_providers_module( $slug );
353 $modules = array();
354
355 foreach ( $modules_ids as $id ) {
356
357 $module = Hustle_Module_Model::new_instance( $id );
358
359 if ( is_wp_error( $module ) ) {
360 continue;
361 }
362
363 if ( $global_multi_id ) {
364
365 $provider_settings = $module->get_provider_settings( $slug );
366
367 // If the provider in this module isn't connected to the instance being disconnected, skip.
368 if ( ! isset( $provider_settings['selected_global_multi_id'] ) || $provider_settings['selected_global_multi_id'] !== $global_multi_id ) {
369 continue;
370 }
371 }
372
373 $modules[] = $module;
374 }
375
376 return $modules;
377 }
378
379 /**
380 * Format Form Fields
381 *
382 * @since 4.0
383 *
384 * @param Hustle_Module_Model $module Module instance.
385 *
386 * @return array
387 */
388 public static function addon_format_form_fields( Hustle_Module_Model $module ) {
389 $formatted_fields = array();
390 $fields = $module->get_form_fields();
391
392 foreach ( $fields as $field ) {
393 $ignored_fields = Hustle_Entry_Model::ignored_fields();
394 if ( in_array( $field['type'], $ignored_fields, true ) ) {
395 continue;
396 }
397
398 $formatted_fields[] = $field;
399 }
400
401 return $formatted_fields;
402 }
403
404 /**
405 * Find addon meta data from entry model that saved on db
406 *
407 * @since 4.0.0
408 *
409 * @param Hustle_Provider_Abstract $connected_addon Provider instance.
410 * @param Hustle_Entry_Model $entry_model Entry model.
411 *
412 * @return array
413 */
414 public static function find_addon_meta_data_from_entry_model( Hustle_Provider_Abstract $connected_addon, Hustle_Entry_Model $entry_model ) {
415 $addon_meta_data = array();
416 $addon_meta_data_prefix = 'hustle_provider_' . $connected_addon->get_slug() . '_';
417 foreach ( $entry_model->meta_data as $key => $meta_datum ) {
418 if ( false !== stripos( $key, $addon_meta_data_prefix ) ) {
419 $addon_meta_data[] = array(
420 'name' => str_ireplace( $addon_meta_data_prefix, '', $key ),
421 'value' => $meta_datum['value'],
422 );
423 }
424 }
425
426 /**
427 * Filter addon's meta data retrieved from db
428 *
429 * @since 4.0
430 */
431 $addon_meta_data = apply_filters( 'hustle_provider_meta_data_from_entry_model', $addon_meta_data, $connected_addon, $entry_model, $addon_meta_data_prefix );
432
433 return $addon_meta_data;
434 }
435
436 /**
437 * Unset technical data of a form.
438 *
439 * @since 4.0.0
440 *
441 * @param array $data $_POST.
442 *
443 * @return array
444 */
445 public static function format_submitted_data_for_addon( $data ) {
446 $new_data = $data;
447 unset(
448 $new_data['form'],
449 $new_data['module_id'],
450 $new_data['uri'],
451 $new_data['hustle_module_id'],
452 $new_data['post_id'],
453 $new_data['g-recaptcha-response'],
454 $new_data['hustle_sub_type'],
455 $new_data['recaptcha-response'],
456 $new_data['cf-turnstile-response']
457 );
458 $new_data = apply_filters( 'hustle_provider_form_formatted_submitted_data', $new_data, $data );
459
460 return $new_data;
461 }
462
463 /**
464 * Retrieves the HTML markup given an array of options.
465 * Renders it from the file "general/option.php", which is a template.
466 * The array should be something like:
467 * array(
468 * "optin_url_label" => array(
469 * "id" => "optin_url_label",
470 * "for" => "optin_url",
471 * "value" => "Enter a Webhook URL:",
472 * "type" => "label",
473 * ),
474 * "optin_url_field_wrapper" => array(
475 * "id" => "optin_url_id",
476 * "class" => "optin_url_id_wrapper",
477 * "type" => "wrapper",
478 * "elements" => array(
479 * "optin_url_field" => array(
480 * "id" => "optin_url",
481 * "name" => "api_key",
482 * "type" => "text",
483 * "default" => "",
484 * "value" => "",
485 * "placeholder" => "",
486 * "class" => "wpmudev-input_text",
487 * )
488 * )
489 * ),
490 * );
491 *
492 * @since 4.0.0
493 * @since 4.2.0 Uses Hustle_Layout_Helper::render() instead of Opt_In::static_render().
494 *
495 * @uses Hustle_Layout_Helper::render()
496 * @param array $options Layout options.
497 * @return string
498 */
499 public static function get_html_for_options( $options ) {
500 $html = '';
501 $renderer = self::get_renderer();
502 foreach ( $options as $key => $option ) {
503 $html .= $renderer->render( 'general/option', array_merge( $option, array( 'key' => $key ) ), true );
504 }
505 return $html;
506 }
507
508 /**
509 * Temporary
510 *
511 * @todo remove
512 */
513 private static function get_renderer() {
514 if ( ! self::$renderer ) {
515 self::$renderer = new Hustle_Layout_Helper();
516 }
517 return self::$renderer;
518 }
519
520 /**
521 * Return the markup used for the title of Integrations modal.
522 *
523 * @since 4.0.0
524 *
525 * @param string $title Integration wizard title.
526 * @param string $subtitle Integration wizard subtitle.
527 * @param string $class_name Title wrapper extra classes.
528 * @return string
529 */
530 public static function get_integration_modal_title_markup( $title = '', $subtitle = '', $class_name = '' ) {
531
532 $html = '<div class="integration-header ' . esc_attr( $class_name ) . '">';
533
534 if ( ! empty( $title ) ) {
535 $html .= '<h3 class="sui-box-title sui-lg" id="dialogTitle2">' . esc_html( $title ) . '</h3>';
536 }
537
538 if ( ! empty( $subtitle ) ) {
539 $html .= '<p class="sui-description">' . wp_kses_post( $subtitle ) . '</p>';
540 }
541
542 $html .= '</div>';
543
544 return $html;
545 }
546
547 /**
548 * Return the markup for buttons.
549 *
550 * @since 4.0.0
551 *
552 * @param string $value Button text.
553 * @param string $class_name Extra classes.
554 * @param string $action next/prev/close/connect/disconnect. Action that this button triggers.
555 * @param bool $loading whether the button should have the 'loading' markup.
556 * @param bool $disabled Whether the button is disabled.
557 * @param string $custom_url Url to which the button should take the user to, if any.
558 * @return string
559 */
560 public static function get_provider_button_markup( $value = '', $class_name = '', $action = '', $loading = false, $disabled = false, $custom_url = '' ) {
561
562 $html = '';
563 $action_class = '';
564
565 if ( ! empty( $action ) ) {
566
567 switch ( $action ) {
568 case 'next':
569 $action_class = 'hustle-provider-next ';
570 break;
571 case 'prev':
572 $action_class = 'hustle-provider-back ';
573 break;
574 case 'close':
575 $action_class = 'hustle-provider-close hustle-modal-close ';
576 break;
577 case 'connect':
578 $action_class = 'hustle-provider-connect ';
579 break;
580 case 'disconnect':
581 $action_class = 'hustle-provider-disconnect ';
582 break;
583 case 'disconnect_form':
584 $action_class = 'hustle-provider-form-disconnect ';
585 break;
586 case 'refresh_list':
587 $action_class = 'hustle-refresh-email-lists ';
588 break;
589 default:
590 $action_class = '';
591 }
592 }
593
594 if ( empty( $custom_url ) ) {
595 $tag = 'button';
596 $custom_attr = 'type="button"';
597 } else {
598 $tag = 'a';
599 $custom_attr = 'href="' . esc_url( $custom_url ) . '"';
600 }
601
602 if ( $loading ) {
603 $action_class .= 'hustle-onload-icon-action ';
604 $inner = '<span class="sui-loading-text">' . esc_html( $value ) . '</span><i class="sui-icon-loader sui-loading" aria-hidden="true"></i>';
605
606 } else {
607 $inner = esc_html( $value );
608 }
609
610 if ( 'refresh_list' === $action ) {
611
612 $html .= sprintf(
613 '<%1$s class="sui-button-icon sui-tooltip %2$s" data-tooltip="%3$s" %4$s >',
614 $tag,
615 esc_attr( $action_class . $class_name ),
616 esc_html__( 'Refresh list', 'hustle' ),
617 disabled( $disabled, true, false )
618 );
619 $html .= '<span class="sui-loading-text" aria-hidden="true">';
620 $html .= '<i class="sui-icon-refresh"></i>';
621 $html .= '</span>';
622 $html .= '<i class="sui-icon-loader sui-loading" aria-hidden="true"></i>';
623 $html .= '<span class="sui-screen-reader-text">' . esc_html( $value ) . '</span>';
624 $html .= '</' . $tag . '>';
625
626 } else {
627
628 $html .= '<' . $tag . ' ' . $custom_attr . ' class="sui-button ' . esc_attr( $action_class ) . esc_attr( $class_name ) . '" ' . disabled( $disabled, true, false ) . '>';
629 $html .= $inner;
630 $html .= '</' . $tag . '>';
631
632 }
633
634 return $html;
635 }
636
637 /**
638 * Adds an entry to debug log
639 *
640 * By default it will check `HUSTLE_PROVIDER_DEBUG` to decide whether to add the log,
641 * then will check `filters`.
642 *
643 * @since 4.0
644 */
645 public static function maybe_log() {
646 $enabled = ( ! defined( 'HUSTLE_PROVIDER_DEBUG' ) || HUSTLE_PROVIDER_DEBUG );
647
648 /**
649 * Filter to enable or disable log for Hustle
650 *
651 * @since 4.0
652 *
653 * @param bool $enabled current enabled status
654 */
655 $enabled = apply_filters( 'hustle_provider_enable_log', $enabled );
656
657 if ( $enabled && is_callable( array( 'Opt_In_Utils', 'maybe_log' ) ) ) {
658 $args = array( '[PROVIDER]' );
659 $fargs = func_get_args();
660 $args = array_merge( $args, $fargs );
661 call_user_func_array( array( 'Opt_In_Utils', 'maybe_log' ), $args );
662 }
663 }
664 }
665