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 / opt-in-utils.php
wordpress-popup / inc Last commit date
display-conditions 5 years ago front 5 years ago helpers 5 years ago metas 5 years ago palettes 5 years ago provider 5 years ago providers 5 years ago templates 5 years ago update 5 years ago class-hustle-admin-page-abstract.php 5 years ago class-hustle-condition-factory.php 6 years ago class-hustle-dashboard-admin.php 5 years ago class-hustle-data.php 5 years ago class-hustle-db.php 6 years ago class-hustle-module-admin.php 5 years ago class-hustle-module-collection.php 5 years ago class-hustle-module-decorator.php 5 years ago class-hustle-module-page-abstract.php 5 years ago class-hustle-notifications.php 5 years ago class-hustle-settings-admin.php 5 years ago class-hustle-upsell-page.php 5 years ago class-hustle-wp-dashboard-page.php 5 years ago hustle-collection.php 6 years ago hustle-deletion.php 5 years ago hustle-embedded-admin.php 6 years ago hustle-entries-admin.php 5 years ago hustle-entry-model.php 5 years ago hustle-general-data-protection.php 6 years ago hustle-init.php 5 years ago hustle-mail.php 5 years ago hustle-meta.php 5 years ago hustle-migration.php 5 years ago hustle-model.php 5 years ago hustle-module-model.php 5 years ago hustle-module-widget-legacy.php 5 years ago hustle-module-widget.php 5 years ago hustle-modules-common-admin-ajax.php 5 years ago hustle-popup-admin.php 6 years ago hustle-providers-admin.php 5 years ago hustle-providers.php 6 years ago hustle-settings-admin-ajax.php 5 years ago hustle-settings-page.php 5 years ago hustle-slidein-admin.php 6 years ago hustle-sshare-admin.php 5 years ago hustle-sshare-model.php 5 years ago hustle-tracking-model.php 5 years ago opt-in-geo.php 5 years ago opt-in-utils.php 5 years ago opt-in-wpmudev-api.php 6 years ago
opt-in-utils.php
1125 lines
1 <?php
2
3 /**
4 * Conditions utils
5 *
6 * Most of the methods are courtesy Philipp Stracker
7 *
8 * Class Opt_In_Utils
9 */
10 class Opt_In_Utils {
11
12 /**
13 * CPT
14 *
15 * @var array
16 */
17 private static $post_types;
18
19 /**
20 * Array of administrator roles
21 *
22 * @var array
23 */
24 private static $admin_roles;
25
26 /**
27 * Returns the referrer.
28 *
29 * @return string
30 */
31 public function get_referrer() {
32 $referrer = '';
33
34 $is_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX )
35 || ( ! empty( $_POST['_po_method_'] ) && 'raw' === $_POST['_po_method_'] ); // WPCS: CSRF ok.
36
37 if ( isset( $_REQUEST['thereferrer'] ) ) { // WPCS: CSRF ok.
38 $referrer = $_REQUEST['thereferrer']; // WPCS: CSRF ok.
39 } elseif ( ! $is_ajax && isset( $_SERVER['HTTP_REFERER'] ) ) {
40 // When doing Ajax request we NEVER use the HTTP_REFERER!
41 $referrer = $_SERVER['HTTP_REFERER'];
42 }
43
44 return $referrer;
45 }
46
47 /**
48 * Tests if the current referrer is one of the referers of the list.
49 * Current referrer has to be specified in the URL param "thereferer".
50 *
51 * @param array $list List of referers to check.
52 * @return bool
53 */
54 public function test_referrer( $list ) {
55 $response = false;
56 if ( is_string( $list ) ) {
57 $list = preg_split( '/\r\n|\r|\n/', $list );
58 }
59 if ( ! is_array( $list ) ) {
60 return true;
61 }
62
63 $referrer = $this->get_referrer();
64
65 if ( ! empty( $referrer ) ) {
66 foreach ( $list as $item ) {
67 $item = trim( $item );
68 $res = stripos( $referrer, $item );
69 if ( false !== $res ) {
70 $response = true;
71 break;
72 }
73 }
74 }
75
76 return $response;
77 }
78
79 /**
80 * Returns current url
81 * should only be called after plugins_loaded hook is fired
82 *
83 * @return string
84 */
85 public static function get_current_url() {
86 if ( ! did_action( 'plugins_loaded' ) ) {
87 new Exception( 'This method should only be called after plugins_loaded hook is fired' ); }
88
89 global $wp;
90 return add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
91 }
92
93 /**
94 * Checks if user is allowed to perform the ajax actions
95 *
96 * @since 4.0
97 * @param array $capability Hustle capability
98 * @param int $module_id Optional. Module id
99 */
100 public static function is_user_allowed_ajax( $capability, $module_id = null ) {
101 if ( is_null( $module_id ) ) {
102 $allowed = current_user_can( $capability );
103 } else {
104 $allowed = self::is_user_allowed( $capability, $module_id );
105 }
106
107 if ( ! $allowed ) {
108 wp_send_json_error( __( 'Invalid request, you are not allowed to make this request', 'hustle' ) );
109 }
110 }
111
112 /**
113 * Check is it admin role or not
114 *
115 * @param string|array $role
116 * @return bool
117 */
118 public static function is_admin_role( $role ) {
119 $admin_roles = array_keys( self::get_admin_roles() );
120
121 if ( ! is_array( $role ) ) {
122 return in_array( $role, $admin_roles, true );
123 }
124
125 return (bool) array_intersect( $role, $admin_roles );
126 }
127
128 /**
129 * Get admin role array
130 *
131 * @since 4.1.0
132 * @return array
133 */
134 public static function get_admin_roles() {
135
136 if ( is_null( self::$admin_roles ) ) {
137 $admins = array();
138 $all_roles = wp_roles();
139
140 if ( $all_roles->is_role( 'administrator' ) ) {
141 $admins['administrator'] = ucfirst( translate_user_role( 'administrator', 'hustle' ) );
142
143 } else {
144 foreach ( $all_roles->roles as $name => $data ) {
145 if ( ! empty( $data['capabilities']['manage_options'] ) && true === $data['capabilities']['manage_options'] ) {
146 $admins[ $name ] = $data['name'];
147 }
148 }
149 }
150
151 self::$admin_roles = apply_filters( 'hustle_get_admin_roles', $admins );
152 }
153
154 return self::$admin_roles;
155 }
156
157 /**
158 * Checks if user has the capability
159 *
160 * @since 4.0
161 * @param array $capability Hustle capability
162 * @param int $module_id Optional. Module id
163 * @return bool
164 */
165 public static function is_user_allowed( $capability, $module_id = null ) {
166
167 // Super admins can do everything.
168 if ( current_user_can( 'setup_network' ) ) {
169 return true;
170 }
171
172 $user = wp_get_current_user();
173 $current_user_caps = (array) $user->allcaps;
174 $current_user_roles = (array) $user->roles;
175
176 if ( self::is_admin_role( $current_user_roles ) ) {
177 // If editing a module and the user is godish, allow.
178 return true;
179
180 } elseif ( 'hustle_edit_module' === $capability && ! empty( $current_user_caps['hustle_create'] ) ) {
181 // If the user can create, it also can edit. Allow.
182 return true;
183
184 } elseif ( is_null( $module_id ) ) {
185 // If we're not editing a module, check for the requested capability.
186 return ! empty( $current_user_caps[ $capability ] );
187
188 } else {
189
190 // If editing a module and the user isn't godish...
191 $module = new Hustle_Module_Model( $module_id );
192
193 // If the module isn't valid, abort.
194 if ( is_wp_error( $module ) ) {
195 return false;
196 }
197
198 // Check for the specific allowed roles.
199 $allowed_roles = $module->get_edit_roles();
200 return (bool) array_intersect( $allowed_roles, $current_user_roles );
201 }
202
203 return false;
204 }
205
206 /**
207 * Get's the status of the membership.
208 *
209 * @since 4.3.3
210 *
211 * @return string
212 */
213 public static function get_membership_status() {
214 // Dashboard is active.
215 if ( class_exists( 'WPMUDEV_Dashboard' ) ) {
216 // Get membership type.
217 $status = WPMUDEV_Dashboard::$api->get_membership_type();
218 // Check if API key is available.
219 if ( 'free' === $status && WPMUDEV_Dashboard::$api->has_key() ) {
220 $status = 'expired';
221 }
222 } else {
223 $status = 'free';
224 }
225 return $status;
226 }
227
228 /**
229 * Checks whether Hustle is included in the membership.
230 *
231 * @since 4.3.3
232 *
233 * @return boolean
234 */
235 public static function is_hustle_included_in_membership() {
236 if ( class_exists( 'WPMUDEV_Dashboard' ) ) {
237 if ( method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_projects' ) && method_exists( 'WPMUDEV_Dashboard_Api', 'get_membership_type' ) ) {
238 $type = WPMUDEV_Dashboard::$api->get_membership_type();
239 $projects = WPMUDEV_Dashboard::$api->get_membership_projects();
240
241 if ( ( 'unit' === $type && in_array( 1107020, $projects, true ) ) || ( 'single' === $type && 1107020 === $projects ) ) {
242 return true;
243 }
244
245 if ( function_exists( 'is_wpmudev_member' ) ) {
246 return is_wpmudev_member();
247 }
248
249 return false;
250 }
251 }
252
253 return false;
254 }
255
256 /**
257 * Checks if the ajax
258 *
259 * @since 1.0
260 * @param $action string ajax call action name
261 */
262 public static function validate_ajax_call( $action ) {
263 if ( ! check_ajax_referer( $action, false, false ) ) {
264 wp_send_json_error( __( 'Invalid request, you are not allowed to make this request', 'hustle' ) ); }
265 }
266
267 /**
268 * Verify if current version is FREE
269 **/
270 public static function _is_free() {
271 $is_free = ! file_exists( Opt_In::$plugin_path . 'lib/wpmudev-dashboard/wpmudev-dash-notification.php' );
272
273 return $is_free;
274 }
275
276 /**
277 * Get the user roles options.
278 *
279 * @since 4.0
280 *
281 * @return array
282 */
283 public static function get_user_roles() {
284
285 global $wp_roles;
286 $roles = $wp_roles->get_names();
287
288 return apply_filters( 'hustle_get_module_permissions_roles', $roles );
289 }
290
291 // ====================================
292 // INTEGRATIONS
293 // ====================================
294
295 /**
296 * Used for sanitizing form submissions.
297 * This method will do a simple sanitation of $post_data. It applies sanitize_text_field() to the keys and values of the first level array.
298 * The keys from second level arrays are converted to numbers, and their values are sanitized with sanitize_text_field() as well.
299 * This method doesn’t do an exhaustive sanitation, so you should handled special cases if your integration requires something different.
300 * The names passed on $required_fields are searched into $post_data array keys. If the key is not set, an array with the key “errors” is returned.
301 *
302 * @since 3.0.5
303 * @param array $post_data The data to be sanitized and validated.
304 * @param array $required_fields Fields that must exist on $post_data so the validation doesn't fail.
305 * @return array
306 */
307 public static function validate_and_sanitize_fields( $post_data, $required_fields = array() ) {
308 // for serialized data or form
309 if ( ! is_array( $post_data ) && is_string( $post_data ) ) {
310 $post_string = $post_data;
311 $post_data = array();
312 wp_parse_str( $post_string, $post_data );
313 }
314
315 $errors = array();
316 foreach ( $required_fields as $key => $required_field ) {
317 if ( ! isset( $post_data[ $required_field ] ) || ( empty( trim( $post_data[ $required_field ] ) ) && '0' !== $post_data[ $required_field ] ) ) {
318 /* translators: ... */
319 $errors[ $required_field ] = sprintf( __( 'Field %s is required.', 'hustle' ), $required_field );
320 continue;
321 }
322 }
323
324 if ( ! empty( $errors ) ) {
325 return array( 'errors' => $errors );
326 }
327
328 $sanitized_data = array();
329 foreach ( $post_data as $key => $post_datum ) {
330 /**
331 * Sanitize here every request so we dont need to sanitize it again on other methods,
332 * unless special treatment is required.
333 */
334 $sanitized_data[ sanitize_text_field( $key ) ] = self::sanitize_text_input_deep( $post_datum );
335 }
336
337 return $sanitized_data;
338 }
339
340 /**
341 * Sanitizes the values of a multi-dimensional array.
342 * The keys of the sub-arrays are converted to numerical arrays.
343 * Sub-arrays are expected to have numerical indexes.
344 *
345 * @since 3.0.5
346 * @param array|string $value
347 * @return string
348 */
349 public static function sanitize_text_input_deep( $value, $key = null ) {
350 $value = is_array( $value ) ?
351 array_map( array( 'Opt_In_Utils', 'sanitize_text_input_deep' ), $value, array_keys( $value ) ) :
352 sanitize_text_field( $value );
353
354 return $value;
355 }
356
357 /**
358 * Adds an entry to debug log
359 *
360 * By default it will check `WP_DEBUG` and HUSTLE_DEBUG to decide whether to add the log,
361 * then will check `filters`.
362 *
363 * @since 3.0.5
364 * @since 4.0 also checks HUSTLE_DEBUG
365 */
366 public static function maybe_log() {
367
368 $wp_debug_enabled = ( defined( 'WP_DEBUG' ) && WP_DEBUG );
369
370 $enabled = ( defined( 'HUSTLE_DEBUG' ) && HUSTLE_DEBUG );
371
372 $stored_settings = Hustle_Settings_Admin::get_general_settings();
373 $debug_setting_enabled = '1' === $stored_settings['debug_enabled'];
374
375 $enabled = ( $wp_debug_enabled && ( $debug_setting_enabled || $enabled ) );
376
377 /**
378 * Filter to enable or disable log for Hustle
379 *
380 * By default it will check `WP_DEBUG`
381 *
382 * @since 3.0.5
383 *
384 * @param bool $enabled current enabled status
385 */
386 $enabled = apply_filters( 'hustle_enable_log', $enabled );
387
388 if ( $enabled ) {
389 $args = func_get_args();
390 $message = wp_json_encode( $args );
391 if ( false !== $message ) {
392 error_log( '[Hustle] ' . $message ); // phpcs:ignore
393 }
394 }
395 }
396
397 // ====================================
398 // MISC?
399 // ====================================
400
401 /**
402 * Returns list of optin providers based on their declared classes that implement Opt_In_Provider_Interface
403 *
404 * @return array
405 */
406 public static function get_post_types() {
407 if ( empty( self::$post_types ) ) {
408 /**
409 * Add all custom post types
410 */
411 $post_types = array();
412 $cpts = get_post_types(
413 array(
414 'public' => true,
415 '_builtin' => false,
416 ),
417 'objects'
418 );
419 foreach ( $cpts as $cpt ) {
420
421 // skip ms_invoice
422 if ( 'ms_invoice' === $cpt->name ) {
423 continue;
424 }
425
426 $cpt_array['name'] = $cpt->name;
427 $cpt_array['label'] = $cpt->label;
428 $cpt_array['data'] = self::get_select2_data( $cpt->name );
429
430 $post_types[ $cpt->name ] = $cpt_array;
431 }
432 self::$post_types = $post_types;
433 }
434 return self::$post_types;
435 }
436
437
438 /**
439 * Get usable object for select2
440 *
441 * @param $post_type post type
442 * @return array
443 */
444 public static function get_select2_data( $post_type, $include_ids = null ) {
445 $data = array();
446
447 // We only make query if include_ids are set becuase in other cases the posts
448 // the data is gathered with AJAX.
449 if ( ! empty( $include_ids ) ) {
450 $args = array(
451 'numberposts' => -1,
452 'post_type' => $post_type,
453 'post_status' => 'publish',
454 'order' => 'ASC',
455 'post__in' => $include_ids,
456 );
457
458 $posts = get_posts( $args );
459 foreach ( $posts as $post ) {
460 $data[] = (object) array(
461 'id' => $post->ID,
462 'text' => $post->post_title,
463 );
464 }
465 }
466
467 return $data;
468 }
469
470 /**
471 * Return reCAPTCHA languages
472 *
473 * @since 4.0
474 * @return array
475 */
476 public static function get_captcha_languages() {
477 return apply_filters(
478 'hustle_captcha_languages',
479 array(
480 'ar' => esc_html__( 'Arabic', 'hustle' ),
481 'af' => esc_html__( 'Afrikaans', 'hustle' ),
482 'am' => esc_html__( 'Amharic', 'hustle' ),
483 'hy' => esc_html__( 'Armenian', 'hustle' ),
484 'az' => esc_html__( 'Azerbaijani', 'hustle' ),
485 'eu' => esc_html__( 'Basque', 'hustle' ),
486 'bn' => esc_html__( 'Bengali', 'hustle' ),
487 'bg' => esc_html__( 'Bulgarian', 'hustle' ),
488 'ca' => esc_html__( 'Catalan', 'hustle' ),
489 'zh-HK' => esc_html__( 'Chinese (Hong Kong)', 'hustle' ),
490 'zh-CN' => esc_html__( 'Chinese (Simplified)', 'hustle' ),
491 'zh-TW' => esc_html__( 'Chinese (Traditional)', 'hustle' ),
492 'hr' => esc_html__( 'Croatian', 'hustle' ),
493 'cs' => esc_html__( 'Czech', 'hustle' ),
494 'da' => esc_html__( 'Danish', 'hustle' ),
495 'nl' => esc_html__( 'Dutch', 'hustle' ),
496 'en-GB' => esc_html__( 'English (UK)', 'hustle' ),
497 'en' => esc_html__( 'English (US)', 'hustle' ),
498 'et' => esc_html__( 'Estonian', 'hustle' ),
499 'fil' => esc_html__( 'Filipino', 'hustle' ),
500 'fi' => esc_html__( 'Finnish', 'hustle' ),
501 'fr' => esc_html__( 'French', 'hustle' ),
502 'fr-CA' => esc_html__( 'French (Canadian)', 'hustle' ),
503 'gl' => esc_html__( 'Galician', 'hustle' ),
504 'ka' => esc_html__( 'Georgian', 'hustle' ),
505 'de' => esc_html__( 'German', 'hustle' ),
506 'de-AT' => esc_html__( 'German (Austria)', 'hustle' ),
507 'de-CH' => esc_html__( 'German (Switzerland)', 'hustle' ),
508 'el' => esc_html__( 'Greek', 'hustle' ),
509 'gu' => esc_html__( 'Gujarati', 'hustle' ),
510 'iw' => esc_html__( 'Hebrew', 'hustle' ),
511 'hi' => esc_html__( 'Hindi', 'hustle' ),
512 'hu' => esc_html__( 'Hungarain', 'hustle' ),
513 'is' => esc_html__( 'Icelandic', 'hustle' ),
514 'id' => esc_html__( 'Indonesian', 'hustle' ),
515 'it' => esc_html__( 'Italian', 'hustle' ),
516 'ja' => esc_html__( 'Japanese', 'hustle' ),
517 'kn' => esc_html__( 'Kannada', 'hustle' ),
518 'ko' => esc_html__( 'Korean', 'hustle' ),
519 'lo' => esc_html__( 'Laothian', 'hustle' ),
520 'lv' => esc_html__( 'Latvian', 'hustle' ),
521 'lt' => esc_html__( 'Lithuanian', 'hustle' ),
522 'ms' => esc_html__( 'Malay', 'hustle' ),
523 'ml' => esc_html__( 'Malayalam', 'hustle' ),
524 'mr' => esc_html__( 'Marathi', 'hustle' ),
525 'mn' => esc_html__( 'Mongolian', 'hustle' ),
526 'no' => esc_html__( 'Norwegian', 'hustle' ),
527 'fa' => esc_html__( 'Persian', 'hustle' ),
528 'pl' => esc_html__( 'Polish', 'hustle' ),
529 'pt' => esc_html__( 'Portuguese', 'hustle' ),
530 'pt-BR' => esc_html__( 'Portuguese (Brazil)', 'hustle' ),
531 'pt-PT' => esc_html__( 'Portuguese (Portugal)', 'hustle' ),
532 'ro' => esc_html__( 'Romanian', 'hustle' ),
533 'ru' => esc_html__( 'Russian', 'hustle' ),
534 'sr' => esc_html__( 'Serbian', 'hustle' ),
535 'si' => esc_html__( 'Sinhalese', 'hustle' ),
536 'sk' => esc_html__( 'Slovak', 'hustle' ),
537 'sl' => esc_html__( 'Slovenian', 'hustle' ),
538 'es' => esc_html__( 'Spanish', 'hustle' ),
539 'es-419' => esc_html__( 'Spanish (Latin America)', 'hustle' ),
540 'sw' => esc_html__( 'Swahili', 'hustle' ),
541 'sv' => esc_html__( 'Swedish', 'hustle' ),
542 'ta' => esc_html__( 'Tamil', 'hustle' ),
543 'te' => esc_html__( 'Telugu', 'hustle' ),
544 'th' => esc_html__( 'Thai', 'hustle' ),
545 'tr' => esc_html__( 'Turkish', 'hustle' ),
546 'uk' => esc_html__( 'Ukrainian', 'hustle' ),
547 'ur' => esc_html__( 'Urdu', 'hustle' ),
548 'vi' => esc_html__( 'Vietnamese', 'hustle' ),
549 'zu' => esc_html__( 'Zulu', 'hustle' ),
550 )
551 );
552 }
553
554 /**
555 * Gets post property
556 *
557 * @since 4.0.4
558 * @param string $property Requested post property.
559 * @param string $default Fallback value.
560 * @return string
561 */
562 public static function get_post_data( $property, $default = '' ) {
563 global $post;
564
565 if ( ! $post ) {
566 // fallback on wp_ajax, `global $post` not available.
567 $wp_referer = wp_get_referer();
568 if ( $wp_referer ) {
569 $post_id = ! function_exists( 'wpcom_vip_url_to_postid' ) ? url_to_postid( $wp_referer ) : wpcom_vip_url_to_postid( $wp_referer );
570 if ( $post_id ) {
571 $post_object = get_post( $post_id );
572 // make sure it's wp_post.
573 if ( $post_object instanceof WP_Post ) {
574 // set global $post as $post_object retrieved from `get_post` for next usage.
575 $post = $post_object; // phpcs:ignore
576 }
577 }
578 }
579 }
580
581 $post_data = (array) $post;
582 if ( isset( $post_data[ $property ] ) ) {
583 return $post_data[ $property ];
584 } else {
585 return $default;
586 }
587 }
588
589 // ====================================
590 // MODULES HELPERS
591 // ====================================
592
593 /**
594 * Get current post id
595 *
596 * @since 4.0
597 *
598 * @return int|string
599 */
600 public static function get_post_id() {
601 return get_post() ? get_the_ID() : '0';
602 }
603
604 /**
605 * Gets the a current user's property.
606 *
607 * @since 4.0.4
608 * @param string $property The user's property to be retrieved.
609 * @return string
610 */
611 public static function get_user_data( $property ) {
612 $current_user = wp_get_current_user();
613
614 if ( $current_user && $current_user->exists() ) {
615 return $current_user->get( $property );
616 }
617 return '';
618 }
619
620 /**
621 * Replace a key in an array without changing its order.
622 *
623 * @since 4.0
624 *
625 * @param string $old_key
626 * @param string $new_key
627 * @param array $array
628 * @return array
629 */
630 public static function replace_array_key( $old_key, $new_key, $array ) {
631
632 // Replace the name without changing the array's order.
633 $keys_array = array_keys( $array );
634 $index = array_search( $old_key, $keys_array, true );
635
636 if ( false === $index ) {
637 return $array;
638 }
639
640 $keys_array[ $index ] = $new_key;
641
642 $new_array = array_combine( $keys_array, array_values( $array ) );
643
644 return $new_array;
645 }
646
647 /*
648 * Get the display name of a module type.
649 *
650 * @since 4.0
651 *
652 * @param string $module_type
653 * @param boolean $plural
654 * @param boolean $capitalized
655 * @return string
656 */
657 public static function get_module_type_display_name( $module_type, $plural = false, $capitalized = false ) {
658
659 $display_name = '';
660
661 if ( Hustle_Module_Model::POPUP_MODULE === $module_type ) {
662 if ( ! $plural ) {
663 $display_name = __( 'pop-up', 'hustle' );
664 } else {
665 $display_name = __( 'pop-ups', 'hustle' );
666 }
667 } elseif ( Hustle_Module_Model::SLIDEIN_MODULE === $module_type ) {
668 if ( ! $plural ) {
669 $display_name = __( 'slide-in', 'hustle' );
670 } else {
671 $display_name = __( 'slide-ins', 'hustle' );
672 }
673 } elseif ( Hustle_Module_Model::EMBEDDED_MODULE === $module_type ) {
674 if ( ! $plural ) {
675 $display_name = __( 'embed', 'hustle' );
676 } else {
677 $display_name = __( 'embeds', 'hustle' );
678 }
679 } elseif ( Hustle_Module_Model::SOCIAL_SHARING_MODULE === $module_type ) {
680 if ( ! $plural ) {
681 $display_name = __( 'social sharing', 'hustle' );
682 } else {
683 $display_name = __( 'social shares', 'hustle' );
684 }
685 }
686
687 if ( $capitalized ) {
688 $display_name = ucwords( $display_name );
689 }
690
691 return $display_name;
692 }
693
694 /**
695 * Get page templates
696 *
697 * @since 4.0.3
698 */
699 public static function hustle_get_page_templates() {
700 $templates = get_page_templates();
701 $page_templates = array();
702 foreach ( $templates as $template_name => $template_filename ) {
703 $page_templates[ $template_filename ] = $template_name;
704 }
705 return $page_templates;
706 }
707
708 /**
709 * Add special scripts for IE if it's detected
710 *
711 * @global bool $is_IE
712 * @global bool $is_edge
713 */
714 public static function maybe_add_scripts_for_ie() {
715 global $is_IE, $is_edge;
716
717 if ( $is_IE || $is_edge ) {
718 wp_enqueue_script(
719 'optin_admin_fitie',
720 Opt_In::$plugin_url . 'assets/js/vendor/fitie/fitie.js',
721 array(),
722 Opt_In::VERSION,
723 true
724 );
725 }
726 }
727
728 /**
729 * Check if WooCommerce is active or not
730 *
731 * @return bool
732 */
733 public static function is_woocommerce_active() {
734 return is_plugin_active( 'woocommerce/woocommerce.php' );
735 }
736
737 /**
738 * Gets the first key of an array.
739 *
740 * @since 4.0.0
741 *
742 * @param array $array
743 * @return mixed
744 */
745 public static function array_key_first( array $array ) {
746 return $array ? array_keys( $array )[0] : null;
747 }
748
749 /**
750 * Get the global placeholders for display.
751 * The array's key has the placeholder value, that's what's inserted between
752 * brackets and then replaced by self::replace_global_placeholders().
753 * The array's value has the display name for the placeholder.
754 *
755 * @since 4.0.3
756 * @see Opt_In_Utils::replace_global_placeholders()
757 * @return array
758 */
759 public static function get_global_placeholders() {
760
761 $placeholders = array(
762 'site_url' => __( 'Site URL', 'hustle' ),
763 'site_name' => __( 'Site name', 'hustle' ),
764 'post_url' => __( 'Post/page URL', 'hustle' ),
765 'post_title' => __( 'Post/page title', 'hustle' ),
766 );
767
768 /**
769 * Filter the available global placeholders.
770 * These are used in some text fields, to be replaced by
771 * self::replace_global_placeholders().
772 *
773 * @since 4.0.3
774 * @see Opt_In_Utils::replace_global_placeholders()
775 * @return array
776 */
777 return apply_filters( 'hustle_get_global_placeholders', $placeholders );
778 }
779
780 /**
781 * Replace the global placeholders from a string.
782 * These are added to some text fields by the admin.
783 * The available ones are returned by self::get_global_placeholders().
784 *
785 * @since 4.0.3
786 * @see Opt_In_Utils::replace_global_placeholders()
787 * @param string $string String with placeholders to be replaced.
788 * @return string
789 */
790 public static function replace_global_placeholders( $string ) {
791
792 preg_match_all( '/\{[^}]*\}/', $string, $matches );
793
794 if ( ! empty( $matches[0] ) && is_array( $matches[0] ) ) {
795
796 $defined_placeholders = array(
797 '{site_url}' => site_url(),
798 '{site_name}' => get_bloginfo( 'name' ),
799 '{post_url}' => get_permalink(),
800 '{post_title}' => esc_html( get_the_title() ),
801 );
802
803 /**
804 * Filter the placeholders and their values.
805 * The keys of the array belong to the placeholder to be replaced.
806 * The values of the array belong to the value to use as replacement.
807 * Eg: [ '{post_url}' => get_permalink() ]
808 *
809 * @since 4.0.3
810 * @return array
811 */
812 $defined_placeholders = apply_filters( 'hustle_global_placeholders_to_replace', $defined_placeholders );
813
814 foreach ( $matches[0] as $placeholder ) {
815
816 if ( key_exists( $placeholder, $defined_placeholders ) ) {
817 $replacement = $defined_placeholders[ $placeholder ];
818
819 if ( $replacement !== $placeholder ) {
820 // Replace if we found something.
821 $string = str_replace( $placeholder, $replacement, $string );
822 }
823 }
824 }
825 }
826
827 return $string;
828 }
829
830 // Static stuff below.
831
832 /**
833 * Returns array of browsers
834 *
835 * @since 4.1
836 * @return array|mixed|null|void
837 */
838 public static function get_browsers() {
839
840 $browsers = array(
841 'chrome' => __( 'Chrome', 'hustle' ),
842 'firefox' => __( 'Firefox', 'hustle' ),
843 'safari' => __( 'Safari', 'hustle' ),
844 'edge' => __( 'Edge', 'hustle' ),
845 'MSIE' => __( 'Internet Explorer', 'hustle' ),
846 'opera' => __( 'Opera', 'hustle' ),
847 );
848
849 /**
850 * Filter the list of browsers
851 * Must return an associative array where the key is the browser's slug
852 * and the value is its display name.
853 *
854 * @since 4.1
855 */
856 return apply_filters( 'hustle_get_browsers_list', $browsers );
857 }
858
859 /**
860 * Returns array of countries
861 *
862 * @return array|mixed|null|void
863 */
864 public static function get_countries() {
865
866 $countries = array(
867 'AU' => __( 'Australia', 'hustle' ),
868 'AF' => __( 'Afghanistan', 'hustle' ),
869 'AL' => __( 'Albania', 'hustle' ),
870 'DZ' => __( 'Algeria', 'hustle' ),
871 'AS' => __( 'American Samoa', 'hustle' ),
872 'AD' => __( 'Andorra', 'hustle' ),
873 'AO' => __( 'Angola', 'hustle' ),
874 'AI' => __( 'Anguilla', 'hustle' ),
875 'AQ' => __( 'Antarctica', 'hustle' ),
876 'AG' => __( 'Antigua and Barbuda', 'hustle' ),
877 'AR' => __( 'Argentina', 'hustle' ),
878 'AM' => __( 'Armenia', 'hustle' ),
879 'AW' => __( 'Aruba', 'hustle' ),
880 'AT' => __( 'Austria', 'hustle' ),
881 'AZ' => __( 'Azerbaijan', 'hustle' ),
882 'BS' => __( 'Bahamas', 'hustle' ),
883 'BH' => __( 'Bahrain', 'hustle' ),
884 'BD' => __( 'Bangladesh', 'hustle' ),
885 'BB' => __( 'Barbados', 'hustle' ),
886 'BY' => __( 'Belarus', 'hustle' ),
887 'BE' => __( 'Belgium', 'hustle' ),
888 'BZ' => __( 'Belize', 'hustle' ),
889 'BJ' => __( 'Benin', 'hustle' ),
890 'BM' => __( 'Bermuda', 'hustle' ),
891 'BT' => __( 'Bhutan', 'hustle' ),
892 'BO' => __( 'Bolivia', 'hustle' ),
893 'BA' => __( 'Bosnia and Herzegovina', 'hustle' ),
894 'BW' => __( 'Botswana', 'hustle' ),
895 'BV' => __( 'Bouvet Island', 'hustle' ),
896 'BR' => __( 'Brazil', 'hustle' ),
897 'IO' => __( 'British Indian Ocean Territory', 'hustle' ),
898 'BN' => __( 'Brunei', 'hustle' ),
899 'BG' => __( 'Bulgaria', 'hustle' ),
900 'BF' => __( 'Burkina Faso', 'hustle' ),
901 'BI' => __( 'Burundi', 'hustle' ),
902 'KH' => __( 'Cambodia', 'hustle' ),
903 'CM' => __( 'Cameroon', 'hustle' ),
904 'CA' => __( 'Canada', 'hustle' ),
905 'CV' => __( 'Cape Verde', 'hustle' ),
906 'KY' => __( 'Cayman Islands', 'hustle' ),
907 'CF' => __( 'Central African Republic', 'hustle' ),
908 'TD' => __( 'Chad', 'hustle' ),
909 'CL' => __( 'Chile', 'hustle' ),
910 'CN' => __( 'China, People\'s Republic of', 'hustle' ),
911 'CX' => __( 'Christmas Island', 'hustle' ),
912 'CC' => __( 'Cocos Islands', 'hustle' ),
913 'CO' => __( 'Colombia', 'hustle' ),
914 'KM' => __( 'Comoros', 'hustle' ),
915 'CD' => __( 'Congo, Democratic Republic of the', 'hustle' ),
916 'CG' => __( 'Congo, Republic of the', 'hustle' ),
917 'CK' => __( 'Cook Islands', 'hustle' ),
918 'CR' => __( 'Costa Rica', 'hustle' ),
919 'CI' => __( 'Côte d\'Ivoire', 'hustle' ),
920 'HR' => __( 'Croatia', 'hustle' ),
921 'CU' => __( 'Cuba', 'hustle' ),
922 'CW' => __( 'Curaçao', 'hustle' ),
923 'CY' => __( 'Cyprus', 'hustle' ),
924 'CZ' => __( 'Czech Republic', 'hustle' ),
925 'DK' => __( 'Denmark', 'hustle' ),
926 'DJ' => __( 'Djibouti', 'hustle' ),
927 'DM' => __( 'Dominica', 'hustle' ),
928 'DO' => __( 'Dominican Republic', 'hustle' ),
929 'TL' => __( 'East Timor', 'hustle' ),
930 'EC' => __( 'Ecuador', 'hustle' ),
931 'EG' => __( 'Egypt', 'hustle' ),
932 'SV' => __( 'El Salvador', 'hustle' ),
933 'GQ' => __( 'Equatorial Guinea', 'hustle' ),
934 'ER' => __( 'Eritrea', 'hustle' ),
935 'EE' => __( 'Estonia', 'hustle' ),
936 'ET' => __( 'Ethiopia', 'hustle' ),
937 'FK' => __( 'Falkland Islands', 'hustle' ),
938 'FO' => __( 'Faroe Islands', 'hustle' ),
939 'FJ' => __( 'Fiji', 'hustle' ),
940 'FI' => __( 'Finland', 'hustle' ),
941 'FR' => __( 'France', 'hustle' ),
942 'FX' => __( 'France, Metropolitan', 'hustle' ),
943 'GF' => __( 'French Guiana', 'hustle' ),
944 'PF' => __( 'French Polynesia', 'hustle' ),
945 'TF' => __( 'French South Territories', 'hustle' ),
946 'GA' => __( 'Gabon', 'hustle' ),
947 'GM' => __( 'Gambia', 'hustle' ),
948 'GE' => __( 'Georgia', 'hustle' ),
949 'DE' => __( 'Germany', 'hustle' ),
950 'GH' => __( 'Ghana', 'hustle' ),
951 'GI' => __( 'Gibraltar', 'hustle' ),
952 'GR' => __( 'Greece', 'hustle' ),
953 'GL' => __( 'Greenland', 'hustle' ),
954 'GD' => __( 'Grenada', 'hustle' ),
955 'GP' => __( 'Guadeloupe', 'hustle' ),
956 'GU' => __( 'Guam', 'hustle' ),
957 'GT' => __( 'Guatemala', 'hustle' ),
958 'GN' => __( 'Guinea', 'hustle' ),
959 'GW' => __( 'Guinea-Bissau', 'hustle' ),
960 'GY' => __( 'Guyana', 'hustle' ),
961 'HT' => __( 'Haiti', 'hustle' ),
962 'HM' => __( 'Heard Island And Mcdonald Island', 'hustle' ),
963 'HN' => __( 'Honduras', 'hustle' ),
964 'HK' => __( 'Hong Kong', 'hustle' ),
965 'HU' => __( 'Hungary', 'hustle' ),
966 'IS' => __( 'Iceland', 'hustle' ),
967 'IN' => __( 'India', 'hustle' ),
968 'ID' => __( 'Indonesia', 'hustle' ),
969 'IR' => __( 'Iran', 'hustle' ),
970 'IQ' => __( 'Iraq', 'hustle' ),
971 'IE' => __( 'Ireland', 'hustle' ),
972 'IL' => __( 'Israel', 'hustle' ),
973 'IT' => __( 'Italy', 'hustle' ),
974 'JM' => __( 'Jamaica', 'hustle' ),
975 'JP' => __( 'Japan', 'hustle' ),
976 'JT' => __( 'Johnston Island', 'hustle' ),
977 'JO' => __( 'Jordan', 'hustle' ),
978 'KZ' => __( 'Kazakhstan', 'hustle' ),
979 'KE' => __( 'Kenya', 'hustle' ),
980 'XK' => __( 'Kosovo', 'hustle' ),
981 'KI' => __( 'Kiribati', 'hustle' ),
982 'KP' => __( 'Korea, Democratic People\'s Republic of', 'hustle' ),
983 'KR' => __( 'Korea, Republic of', 'hustle' ),
984 'KW' => __( 'Kuwait', 'hustle' ),
985 'KG' => __( 'Kyrgyzstan', 'hustle' ),
986 'LA' => __( 'Lao People\'s Democratic Republic', 'hustle' ),
987 'LV' => __( 'Latvia', 'hustle' ),
988 'LB' => __( 'Lebanon', 'hustle' ),
989 'LS' => __( 'Lesotho', 'hustle' ),
990 'LR' => __( 'Liberia', 'hustle' ),
991 'LY' => __( 'Libya', 'hustle' ),
992 'LI' => __( 'Liechtenstein', 'hustle' ),
993 'LT' => __( 'Lithuania', 'hustle' ),
994 'LU' => __( 'Luxembourg', 'hustle' ),
995 'MO' => __( 'Macau', 'hustle' ),
996 'MK' => __( 'Macedonia', 'hustle' ),
997 'MG' => __( 'Madagascar', 'hustle' ),
998 'MW' => __( 'Malawi', 'hustle' ),
999 'MY' => __( 'Malaysia', 'hustle' ),
1000 'MV' => __( 'Maldives', 'hustle' ),
1001 'ML' => __( 'Mali', 'hustle' ),
1002 'MT' => __( 'Malta', 'hustle' ),
1003 'MH' => __( 'Marshall Islands', 'hustle' ),
1004 'MQ' => __( 'Martinique', 'hustle' ),
1005 'MR' => __( 'Mauritania', 'hustle' ),
1006 'MU' => __( 'Mauritius', 'hustle' ),
1007 'YT' => __( 'Mayotte', 'hustle' ),
1008 'MX' => __( 'Mexico', 'hustle' ),
1009 'FM' => __( 'Micronesia', 'hustle' ),
1010 'MD' => __( 'Moldova', 'hustle' ),
1011 'MC' => __( 'Monaco', 'hustle' ),
1012 'MN' => __( 'Mongolia', 'hustle' ),
1013 'ME' => __( 'Montenegro', 'hustle' ),
1014 'MS' => __( 'Montserrat', 'hustle' ),
1015 'MA' => __( 'Morocco', 'hustle' ),
1016 'MZ' => __( 'Mozambique', 'hustle' ),
1017 'MM' => __( 'Myanmar', 'hustle' ),
1018 'NA' => __( 'Namibia', 'hustle' ),
1019 'NR' => __( 'Nauru', 'hustle' ),
1020 'NP' => __( 'Nepal', 'hustle' ),
1021 'NL' => __( 'Netherlands', 'hustle' ),
1022 'AN' => __( 'Netherlands Antilles', 'hustle' ),
1023 'NC' => __( 'New Caledonia', 'hustle' ),
1024 'NZ' => __( 'New Zealand', 'hustle' ),
1025 'NI' => __( 'Nicaragua', 'hustle' ),
1026 'NE' => __( 'Niger', 'hustle' ),
1027 'NG' => __( 'Nigeria', 'hustle' ),
1028 'NU' => __( 'Niue', 'hustle' ),
1029 'NF' => __( 'Norfolk Island', 'hustle' ),
1030 'MP' => __( 'Northern Mariana Islands', 'hustle' ),
1031 'MP' => __( 'Mariana Islands, Northern', 'hustle' ),
1032 'NO' => __( 'Norway', 'hustle' ),
1033 'OM' => __( 'Oman', 'hustle' ),
1034 'PK' => __( 'Pakistan', 'hustle' ),
1035 'PW' => __( 'Palau', 'hustle' ),
1036 'PS' => __( 'Palestine, State of', 'hustle' ),
1037 'PA' => __( 'Panama', 'hustle' ),
1038 'PG' => __( 'Papua New Guinea', 'hustle' ),
1039 'PY' => __( 'Paraguay', 'hustle' ),
1040 'PE' => __( 'Peru', 'hustle' ),
1041 'PH' => __( 'Philippines', 'hustle' ),
1042 'PN' => __( 'Pitcairn Islands', 'hustle' ),
1043 'PL' => __( 'Poland', 'hustle' ),
1044 'PT' => __( 'Portugal', 'hustle' ),
1045 'PR' => __( 'Puerto Rico', 'hustle' ),
1046 'QA' => __( 'Qatar', 'hustle' ),
1047 'RE' => __( 'Réunion', 'hustle' ),
1048 'RO' => __( 'Romania', 'hustle' ),
1049 'RU' => __( 'Russia', 'hustle' ),
1050 'RW' => __( 'Rwanda', 'hustle' ),
1051 'SH' => __( 'Saint Helena', 'hustle' ),
1052 'KN' => __( 'Saint Kitts and Nevis', 'hustle' ),
1053 'LC' => __( 'Saint Lucia', 'hustle' ),
1054 'PM' => __( 'Saint Pierre and Miquelon', 'hustle' ),
1055 'VC' => __( 'Saint Vincent and the Grenadines', 'hustle' ),
1056 'WS' => __( 'Samoa', 'hustle' ),
1057 'SM' => __( 'San Marino', 'hustle' ),
1058 'ST' => __( 'Sao Tome and Principe', 'hustle' ),
1059 'SA' => __( 'Saudi Arabia', 'hustle' ),
1060 'SN' => __( 'Senegal', 'hustle' ),
1061 'CS' => __( 'Serbia', 'hustle' ),
1062 'SC' => __( 'Seychelles', 'hustle' ),
1063 'SL' => __( 'Sierra Leone', 'hustle' ),
1064 'SG' => __( 'Singapore', 'hustle' ),
1065 'MF' => __( 'Sint Maarten', 'hustle' ),
1066 'SK' => __( 'Slovakia', 'hustle' ),
1067 'SI' => __( 'Slovenia', 'hustle' ),
1068 'SB' => __( 'Solomon Islands', 'hustle' ),
1069 'SO' => __( 'Somalia', 'hustle' ),
1070 'ZA' => __( 'South Africa', 'hustle' ),
1071 'GS' => __( 'South Georgia and the South Sandwich Islands', 'hustle' ),
1072 'ES' => __( 'Spain', 'hustle' ),
1073 'LK' => __( 'Sri Lanka', 'hustle' ),
1074 'XX' => __( 'Stateless Persons', 'hustle' ),
1075 'SD' => __( 'Sudan', 'hustle' ),
1076 'SD' => __( 'Sudan, South', 'hustle' ),
1077 'SR' => __( 'Suriname', 'hustle' ),
1078 'SJ' => __( 'Svalbard and Jan Mayen', 'hustle' ),
1079 'SZ' => __( 'Swaziland', 'hustle' ),
1080 'SE' => __( 'Sweden', 'hustle' ),
1081 'CH' => __( 'Switzerland', 'hustle' ),
1082 'SY' => __( 'Syria', 'hustle' ),
1083 'TW' => __( 'Taiwan, Republic of China', 'hustle' ),
1084 'TJ' => __( 'Tajikistan', 'hustle' ),
1085 'TZ' => __( 'Tanzania', 'hustle' ),
1086 'TH' => __( 'Thailand', 'hustle' ),
1087 'TG' => __( 'Togo', 'hustle' ),
1088 'TK' => __( 'Tokelau', 'hustle' ),
1089 'TO' => __( 'Tonga', 'hustle' ),
1090 'TT' => __( 'Trinidad and Tobago', 'hustle' ),
1091 'TN' => __( 'Tunisia', 'hustle' ),
1092 'TR' => __( 'Turkey', 'hustle' ),
1093 'TM' => __( 'Turkmenistan', 'hustle' ),
1094 'TC' => __( 'Turks and Caicos Islands', 'hustle' ),
1095 'TV' => __( 'Tuvalu', 'hustle' ),
1096 'UG' => __( 'Uganda', 'hustle' ),
1097 'UA' => __( 'Ukraine', 'hustle' ),
1098 'AE' => __( 'United Arab Emirates', 'hustle' ),
1099 'GB' => __( 'United Kingdom', 'hustle' ),
1100 'US' => __( 'United States of America (USA)', 'hustle' ),
1101 'UM' => __( 'US Minor Outlying Islands', 'hustle' ),
1102 'UY' => __( 'Uruguay', 'hustle' ),
1103 'UZ' => __( 'Uzbekistan', 'hustle' ),
1104 'VU' => __( 'Vanuatu', 'hustle' ),
1105 'VA' => __( 'Vatican City', 'hustle' ),
1106 'VE' => __( 'Venezuela', 'hustle' ),
1107 'VN' => __( 'Vietnam', 'hustle' ),
1108 'VG' => __( 'Virgin Islands, British', 'hustle' ),
1109 'VI' => __( 'Virgin Islands, U.S.', 'hustle' ),
1110 'WF' => __( 'Wallis And Futuna', 'hustle' ),
1111 'EH' => __( 'Western Sahara', 'hustle' ),
1112 'YE' => __( 'Yemen', 'hustle' ),
1113 'ZM' => __( 'Zambia', 'hustle' ),
1114 'ZW' => __( 'Zimbabwe', 'hustle' ),
1115 );
1116
1117 /**
1118 * Returns a list with countries
1119 * Must be an associative array where the key is the country code
1120 * and its value is its display name.
1121 */
1122 return apply_filters( 'opt_in-country-list', $countries );
1123 }
1124 }
1125