PluginProbe
Snippet Shortcodes / 5.1
Snippet Shortcodes v5.1
5.2.1 5.2 5.1.8 5.1.6 5.1.7 5.1.5 trunk 1.0 1.1 1.2 1.3 1.3.1 1.4 1.5 1.5.1 1.6 1.6.1 1.7 1.7.1 1.7.2 1.7.3 1.7.4 1.8 2.0 2.0.1 All 74 releases
shortcode-variables / includes / functions.php

functions.php in Snippet Shortcodes 5.1, at includes/functions.php

868 lines 19.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined('ABSPATH') or die('Jog on!');
4
5 /**
6 * Is the Premium plugin enabled and do we have a valid license?
7 *
8 * @return bool
9 */
10 function sh_cd_is_premium() {
11
12 if ( false === sh_cd_is_premium_plugin_activated() ) {
13 return false;
14 }
15
16 return apply_filters( 'sh-cd-license-is-premium', false );
17 }
18
19 /**
20 * Is Premium plugin enabled?
21 */
22 function sh_cd_is_premium_plugin_activated() {
23 return defined( 'YK_SS_PLUGIN_NAME' );
24 }
25
26 /**
27 * Generate a site hash to identify this site.
28 **/
29 function sh_cd_generate_site_hash() {
30
31 $site_hash = get_option( 'sh-cd-hash' );
32
33 // Generate a basic site key from URL and plugin slug
34 if( false == $site_hash ) {
35
36 $site_hash = md5( 'yeken-sh-cd-' . site_url() );
37 $site_hash = substr( $site_hash, 0, 6 );
38
39 update_option( 'sh-cd-hash', $site_hash );
40
41 }
42 return $site_hash;
43 }
44
45 /**
46 * Save / Insert a shortcode
47 *
48 * @return bool
49 */
50 function sh_cd_shortcodes_save_post() {
51
52 $fields = apply_filters( 'sh-cd-post-field-keys', [ 'id', 'slug', 'previous_slug', 'data', 'disabled', 'multisite', 'editor' ] );
53
54 // Capture the raw $_POST fields, the save functions will process and validate the data
55 $shortcode = sh_cd_get_values_from_post( $fields );
56
57 // If we are not premium, then the user is not allowed to change the site slug (otherwise they could just re-use variables and by pass the limit)
58 if ( ! sh_cd_is_premium() && false === empty( $shortcode[ 'previous_slug' ] ) ) {
59 $shortcode[ 'slug' ] = $shortcode[ 'previous_slug' ];
60 }
61
62 return sh_cd_db_shortcodes_save( $shortcode );
63 }
64
65 /**
66 * Replace user parameters within a shortcode e.g. look for %%parameter%% and replace
67 *
68 * @param $shortcode
69 * @param $user_defined_parameters
70 *
71 * @return mixed
72 */
73 function sh_cd_apply_user_defined_parameters( $shortcode, $user_defined_parameters ){
74
75 // Ensure we have something to do!
76 if ( true === empty( $user_defined_parameters ) || false === is_array( $user_defined_parameters ) ) {
77 return $shortcode;
78 }
79
80 foreach ( $user_defined_parameters as $key => $value ) {
81 $shortcode = str_replace( '%%' . $key . '%%', $value, $shortcode );
82 }
83
84 return $shortcode;
85 }
86
87 /**
88 * Generate a unique slug
89 *
90 * @param $slug
91 *
92 * @return string
93 */
94 function sh_cd_slug_generate( $slug, $exising_id = NULL ) {
95
96 if ( true === empty( $slug ) ) {
97 return NULL;
98 }
99
100 $slug = sanitize_key( $slug );
101
102 $original_slug = $slug;
103
104 $try = 1;
105
106 // Ensure the slug is unique
107 while ( false === sh_cd_slug_is_unique( $slug, $exising_id ) ) {
108
109 $slug = sprintf( '%s_%d', $original_slug, $try );
110
111 $try++;
112 }
113
114 return $slug;
115 }
116
117 /**
118 * Clone an existing shortcode!
119 *
120 * @param $id
121 *
122 * @return bool
123 */
124 function sh_cd_clone( $id ) {
125
126 if( false === sh_cd_is_premium() ) {
127 return true;
128 }
129
130 if ( false === is_numeric( $id ) ) {
131 return false;
132 }
133
134 $to_be_cloned = sh_cd_db_shortcodes_by_id( $id );
135
136 if ( true === empty( $to_be_cloned ) ) {
137 return false;
138 }
139
140 unset( $to_be_cloned['id'] );
141
142 return sh_cd_db_shortcodes_save( $to_be_cloned );
143 }
144
145 /**
146 * Display message in admin UI
147 *
148 * @param $text
149 * @param bool $error
150 */
151 function sh_cd_message_display( $text, $error = false ) {
152
153 if ( true === empty( $text ) ) {
154 return;
155 }
156
157 printf( '<div class="%s"><p>%s</p></div>',
158 true === $error ? 'error' : 'updated',
159 esc_html( $text )
160 );
161
162 //TODO: Hook this to use admin_notices
163 }
164
165 /**
166 * Fetch cache item
167 *
168 * @param $key
169 *
170 * @return mixed
171 */
172 function sh_cd_cache_get( $key ) {
173
174 $key = sh_cd_cache_generate_key( $key );
175
176 return get_transient( $key );
177 }
178
179 /**
180 * Set cache item
181 *
182 * @param $key
183 * @param $data
184 */
185 function sh_cd_cache_set( $key, $data, $expire = NULL ) {
186
187
188 $expire = ( false === empty( $expire ) ) ? (int) $expire : 1 * HOUR_IN_SECONDS;
189
190 $key = sh_cd_cache_generate_key( $key );
191
192 set_transient( $key, $data, $expire );
193
194 do_action( 'sh-cd-global-cache-delete' );
195 }
196
197 /**
198 * Delete cache for given shortcode slug / ID
199 *
200 * @param $slug_or_key
201 */
202 function sh_cd_cache_delete_by_slug_or_key( $slug_or_key ) {
203
204 if ( true === is_numeric( $slug_or_key ) ) {
205
206 $slug_or_key = sh_cd_db_shortcodes_get_slug_by_id( $slug_or_key );
207
208 sh_cd_cache_delete( $slug_or_key );
209
210 } else {
211 sh_cd_cache_delete( $slug_or_key );
212 }
213
214 // Delete site option
215 $slug_or_key = SH_CD_PREFIX . $slug_or_key;
216
217 delete_site_option( $slug_or_key );
218
219 }
220
221 /**
222 * Delete cache item
223 *
224 * @param $key
225 *
226 * @return mixed
227 */
228 function sh_cd_cache_delete( $key, $trigger_global_hook = true ) {
229
230 $key = sh_cd_cache_generate_key( $key );
231
232 if ( true === $trigger_global_hook ) {
233 do_action( 'sh-cd-global-cache-delete' );
234 }
235
236 return delete_transient( $key );
237 }
238
239
240 /**
241 * Generate cache key
242 *
243 * @param $key
244 *
245 * @return string
246 */
247 function sh_cd_cache_generate_key( $key ) {
248 return SH_CD_SHORTCODE . SH_CD_PLUGIN_VERSION . $key;
249 }
250
251 /**
252 * Return link to list own shortcodes
253 *
254 * @return mixed
255 */
256 function sh_cd_link_your_shortcodes() {
257
258 $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes');
259
260 return esc_url( $link );
261 }
262
263 /**
264 * Return link to add own shortcode
265 *
266 * @return mixed
267 */
268 function sh_cd_link_your_shortcodes_add() {
269
270 $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=add');
271
272 return esc_url( $link );
273 }
274
275 /**
276 * Return link to edit own shortcode
277 *
278 * @return mixed
279 */
280 function sh_cd_link_your_shortcodes_edit( $id ) {
281
282 $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=edit&id=' . (int) $id );
283
284 return esc_url( $link );
285 }
286
287 /**
288 * Return link to delete own shortcode
289 *
290 * @param $id
291 * @return mixed
292 */
293 function sh_cd_link_your_shortcodes_delete( $id ) {
294
295 $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id );
296
297 return esc_url( $link );
298 }
299
300 /**
301 * Either fetch data from the $_POST object or from the array passed in!
302 *
303 * @param $object
304 * @param $key
305 * @return string
306 */
307 function sh_cd_get_value_from_post_or_obj( $object, $key ) {
308
309 if ( true === isset( $_POST[ $key ] ) ) {
310 return $_POST[ $key ];
311 }
312
313 if ( true === isset( $object[ $key ] ) ) {
314 return $object[ $key ];
315 }
316
317 return '';
318 }
319
320 /**
321 * Either fetch data from the $_POST object for the given object keys
322 *
323 * @param $keys
324 * @return array
325 */
326 function sh_cd_get_values_from_post( $keys ) {
327
328 $data = [];
329
330 foreach ( $keys as $key ) {
331
332 if ( true === isset( $_POST[ $key ] ) ) {
333 $data[ $key ] = $_POST[ $key ];
334 } else {
335 $data[ $key ] = '';
336 }
337
338 }
339
340 return $data;
341 }
342
343 /**
344 * Toggle the status of a shortcode
345 *
346 * @param $id
347 */
348 function sh_cd_toggle_status( $id ) {
349
350 $slug = sh_cd_db_shortcodes_by_id( (int) $id );
351
352 if ( false === empty( $slug ) ) {
353
354 $status = ( 1 === (int) $slug['disabled'] ) ? 0 : 1 ;
355
356 sh_cd_db_shortcodes_update_status( $id, $status );
357
358 return $status;
359 }
360
361 return NULL;
362 }
363
364 /**
365 * Toggle the multisite of a shortcode
366 *
367 * @param $id
368 * @return int|null
369 */
370 function sh_cd_toggle_multisite( $id ) {
371
372 $slug = sh_cd_db_shortcodes_by_id( (int) $id );
373
374 if ( false === empty( $slug ) ) {
375
376 $multisite = ( 1 === (int) $slug['multisite'] ) ? 0 : 1 ;
377
378 sh_cd_db_shortcodes_update_multisite( $id, $multisite );
379
380 return $multisite;
381 }
382
383 return NULL;
384 }
385
386 /**
387 * Display an upgrade button
388 *
389 * @param string $css_class
390 * @param null $link
391 */
392 function sh_cd_upgrade_button( $css_class = '', $link = NULL ) {
393
394 $link = ( false === empty( $link ) ) ? $link : SH_CD_UPGRADE_LINK . '?hash=' . sh_cd_generate_site_hash() ;
395
396 $price = sh_cd_license_price();
397 $price = ( false === empty( $price ) ) ? sprintf( '- £%s %s', $price, __( 'a year ', SH_CD_SLUG ) ) : '';
398
399 echo sprintf('<a href="%s" class="button-primary sh-cd-upgrade-button sh-cd-button %s"><i class="far fa-star"></i> %s %s</a>',
400 esc_url( $link ),
401 esc_attr( ' ' . $css_class ),
402 __( 'Purchase a license ', SH_CD_SLUG ),
403 $price
404 );
405 }
406
407 /**
408 * Display an upgrade button
409 *
410 * @param string $css_class
411 * @param null $link
412 */
413 function sh_cd_premium_shortcode_download( $return = false ) {
414
415 $link = SH_CD_GET_PREMIUM_LINK . '?hash=' . sh_cd_generate_site_hash();
416
417 $html = sprintf('<a href="%s" class="button-primary sh-cd-button sh-cd-upgrade-button"><i class="fas fa-download"></i> %s</a>',
418 esc_url( $link ),
419 __( 'Download Premium Plugin', SH_CD_SLUG )
420 );
421
422 if ( true === $return ) {
423 return $html;
424 }
425
426 echo $html;
427 }
428
429 /**
430 * Is multsite functionality active for this install?
431 *
432 * @return bool
433 */
434 function sh_cd_is_multisite_enabled() {
435
436 if ( true === defined( 'YK_TEST_IS_MULTISITE' ) && true === YK_TEST_IS_MULTISITE ) {
437 return true;
438 }
439
440 if ( false === is_multisite() ) {
441 return false;
442 }
443
444 if ( false === sh_cd_is_premium() ) {
445 return false;
446 }
447
448 return true;
449 }
450
451 /**
452 * Fetch all multisite slugs
453 *
454 * @return array|null
455 */
456 function sh_cd_multisite_slugs() {
457
458 if ( false === is_multisite() ) {
459 return [];
460 }
461
462 $cache = sh_cd_cache_get( 'sh-cd-multisite-slugs' );
463
464 if ( false !== $cache ) {
465 return $cache;
466 }
467
468 $slugs = sh_cd_db_shortcodes_multisite_slugs();
469
470 $slugs = ( false === empty( $slugs ) ) ? wp_list_pluck( $slugs, 'slug' ) : [];
471
472 // Cache this for a short time
473 sh_cd_cache_set( 'sh-cd-multisite-slugs', $slugs, 30 );
474
475 return ( true === is_array( $slugs ) ) ? $slugs : [];
476 }
477
478 /**
479 * Have we reached the limit of free shortcodes?
480 * @return bool
481 */
482 function sh_cd_reached_free_limit() {
483
484 if ( true === sh_cd_is_premium() ) {
485 return false;
486 }
487
488 $existing_shortcodes = sh_cd_db_shortcodes_count();
489
490 if ( true === empty( $existing_shortcodes ) ) {
491 return false;
492 }
493
494 return ( (int) $existing_shortcodes >= sh_cd_get_free_limit() );
495 }
496
497 /**
498 * Return free limit for shortcodes
499 */
500 function sh_cd_get_free_limit() {
501 return 10;
502 }
503
504 /**
505 * Get the minimum user role allowed for viewing data pages in admin
506 * @return mixed|void
507 */
508 function sh_cd_permission_role() {
509
510 // If not premium, then admin only
511 if ( false === sh_cd_is_premium() ) {
512 return 'manage_options';
513 }
514
515 $permission_role = get_option( 'sh-cd-edit-permissions', 'manage_options' );
516
517 return ( false === empty( $permission_role ) ) ? $permission_role : 'manage_options';
518 }
519
520 /**
521 * Does the user have the correct permissions to view this page?
522 */
523 function sh_cd_permission_check() {
524
525 $allowed_viewer = sh_cd_permission_role();
526
527 if ( false === current_user_can( $allowed_viewer ) ) {
528 wp_die( __( 'You do not have sufficient permissions to access this page.', SH_CD_SLUG ) );
529 }
530 }
531
532 /**
533 * Is the shortcode [sv slug="sc-db-value-by-id"] enabled
534 * @return bool (default false)
535 */
536 function sh_cd_is_shortcode_db_value_by_id_enabled() {
537
538 if ( false === sh_cd_is_premium() ) {
539 return false;
540 }
541
542 // Disabling by filter overrides the setting in WP admin
543 if ( true === apply_filters( 'disable-ss-sc-db-value-by-id', __return_false() ) ) {
544 return false;
545 }
546
547 $value = get_option( 'sh-cd-shortcode-db-value-by-id-enabled', false );
548
549 return sh_cd_to_bool( $value );
550 }
551
552 /**
553 * Display upgrade notice
554 *
555 * @param bool $pro_plus
556 */
557 function sh_cd_display_pro_upgrade_notice( $title = NULL, $content = '', $class = '' ) {
558
559 $title = ( true === empty( $title ) ) ? __( 'Upgrade Snippet Shortcodes and get more features!', SH_CD_SLUG ) : $title;
560
561 ?>
562 <div class="postbox sh-cd-advertise-premium <?php echo esc_attr( $class ) ?>">
563 <h3 class="hndle"><i class="fa-regular fa-star"></i> <?php echo esc_html( $title ) ?></h3>
564 <div style="padding: 0px 15px 0px 15px">
565 <p><?php echo wp_kses( $content, [ 'ul' => [ 'class' ], 'li' => [], 'strong' => [], 'span' => [], 'div' => [] ] ); ?></p>
566 <p><a href="<?php echo esc_url( admin_url('admin.php?page=sh-cd-shortcode-variables-upgrade') ); ?>" class="button-primary sh-cd-upgrade-button"><i class="fa-regular fa-star"></i> <?php echo __( 'Get Premium', SH_CD_SLUG ); ?></a></p>
567 </div>
568 </div>
569 <?php
570 }
571
572 /**
573 * Display a star to prompt for a Premioum upgrade
574 *
575 * @param bool $pro_plus
576 */
577 function sh_cd_display_premium_star() {
578
579 if ( true === sh_cd_is_premium() ) {
580 return ''; // We don't want to show the star if the user has already upgraded
581 }
582
583 return sprintf ('<a href="%s"><i class="fa-regular fa-star"></i></a>',
584 esc_url( admin_url('admin.php?page=sh-cd-shortcode-variables-upgrade') )
585 );
586 }
587
588 /**
589 * Display info symbol with tooltip
590 */
591 function sh_cd_display_info_tooltip( $text ) {
592
593 return sprintf ('<i class="fa-regular fa-circle-question sh-cd-tooltip" title="%s">',
594 esc_html( $text )
595 );
596 }
597
598 /**
599 * Process a CSV attachment and import into database
600 *
601 * @param $attachment_id
602 *
603 * @param bool $dry_run
604 *
605 * @return string
606 */
607 function sh_cd_import_csv( $attachment_id, $dry_run = true ) {
608
609 if ( false === sh_cd_permission_check() ) {
610 return 'You do not have the correct admin permissions';
611 }
612
613 if ( false === sh_cd_is_premium() ) {
614 return 'This is a premium feature';
615 }
616
617 $csv_path = get_attached_file( $attachment_id );
618 $admin_id = get_current_user_id();
619
620 if ( true === empty( $csv_path ) || false === file_exists( $csv_path )) {
621 return 'Error: Error loading CSV from disk.';
622 }
623
624 $csv = array_map('str_getcsv', file( $csv_path ) );
625
626 if ( true === empty( $csv ) ) {
627 return 'Error: The CSV appears to be empty.';
628 }
629
630 array_walk($csv, function(&$a) use ($csv) {
631 $a = array_combine($csv[0], $a);
632 });
633
634 $validate_header_result = sh_cd_import_csv_validate_header( $csv[0] );
635
636 if ( true !== $validate_header_result ) {
637 return $validate_header_result;
638 }
639
640 array_shift($csv );
641
642 if ( true === empty( $csv ) ) {
643 return 'Error: The CSV appears to be empty (when header hs been removed).';
644 }
645
646 $errors = 0;
647
648 $output = sprintf( '%d rows to process...' . PHP_EOL, count( $csv ) );
649
650 if ( true === $dry_run ) {
651 $output .= 'DRY RUN MODE! No data will be imported.' . PHP_EOL;
652 }
653
654 foreach ( $csv as $row ) {
655
656 if ( $errors >= 50 ) {
657 $output .= 'Aborted! More than 50 errors have been detected in this file.' . PHP_EOL;
658 break;
659 }
660
661 $row = array_change_key_case( $row ); // Force CSV headers to lowercase
662
663 $validation_result = sh_cd_import_csv_validate_row( $row );
664
665 // Validate a row before proceeding
666 if ( true !== $validation_result ) {
667 $output .= $validation_result . PHP_EOL;
668 $errors++;
669 continue;
670 }
671
672 if ( false === $dry_run ) {
673
674 $shortcode = [ 'slug' => $row[ 'slug' ],
675 'previous_slug' => '',
676 'data' => $row[ 'content' ],
677 'disabled' => ! sh_cd_to_bool( $row[ 'enabled' ] ),
678 'multisite' => sh_cd_to_bool( $row[ 'global' ] )
679 ];
680
681 $result = sh_cd_db_shortcodes_save( $shortcode );
682
683 if ( false === $result ) {
684 $output .= 'Skipped: Error inserting into database (most likely a field contains too many characters or in the wrong format): ' . implode( ',', $row ) . PHP_EOL;
685 }
686 }
687
688 }
689
690 if ( $errors > 0 ) {
691 $output .= sprintf( '%d errors were detected and the rows skipped.' . PHP_EOL, $errors );
692 }
693
694 $output .= 'Completed.';
695
696 return $output;
697
698 }
699
700 /**
701 * Verify header row
702 * @param $header_row
703 *
704 * @return bool|string
705 */
706 function sh_cd_import_csv_validate_header( $header_row ) {
707
708 $expected_headers = [ 'slug', 'content', 'global', 'enabled' ];
709
710 foreach ( $expected_headers as $column ) {
711
712 if ( false === isset( $header_row[ $column ] ) ) {
713 return 'Missing column: ' . $column . '. Expecting: ' . implode( ',', $expected_headers ) . PHP_EOL;
714 }
715 }
716
717 return true;
718 }
719
720 /**
721 * Validate CSV row
722 * @param $csv_row
723 *
724 * @return bool|string
725 */
726 function sh_cd_import_csv_validate_row( $csv_row ) {
727
728 if ( true === empty( $csv_row[ 'slug' ] ) ) {
729 return 'Skipped: Missing slug: ' . implode( ',', $csv_row );
730 }
731
732 if ( false === empty( $isset[ 'content' ] ) ) {
733 return 'Skipped: Content: ' . implode( ',', $csv_row );
734 }
735
736 $allowed_bools = [ 'yes', 'no', 'true', 'false', '1', '0' ];
737
738 if ( true === empty( $csv_row[ 'global' ] ) ||
739 false === in_array( $csv_row[ 'global' ], $allowed_bools ) ) {
740 return 'Skipped: Invalid "global" value. Must be "yes" or "no": ' . implode( ',', $csv_row );
741 }
742
743 if ( true === empty( $csv_row[ 'enabled' ] ) ||
744 false === in_array( $csv_row[ 'enabled' ], $allowed_bools ) ) {
745 return 'Skipped: Invalid "enabled" value. Must be "yes" or "no": ' . implode( ',', $csv_row );
746 }
747
748 return true;
749 }
750
751 /**
752 * Convert string to bool
753 * @param $string
754 * @return mixed
755 */
756 function sh_cd_to_bool( $string ) {
757 return filter_var( $string, FILTER_VALIDATE_BOOLEAN );
758 }
759
760 /**
761 * Our version of kses and the HTML we are happy with
762 */
763 function sh_cd_wp_kses( $value ) {
764
765 $basic_tags = wp_kses_allowed_html( 'html' );
766
767 $basic_tags[ 'a' ] = [ 'id' => true, 'class' => true, 'href' => true, 'title' => true, 'target' => true];
768 $basic_tags[ 'canvas' ] = [ 'id' => true, 'class' => true ];
769 $basic_tags[ 'div' ] = [ 'id' => true, 'class' => true, 'style' => true ];
770 $basic_tags[ 'i' ] = [ 'id' => true, 'class' => true ];
771 $basic_tags[ 'p' ] = [ 'id' => true, 'class' => true ];
772 $basic_tags[ 'span' ] = [ 'id' => true, 'class' => true ];
773 $basic_tags[ 'table' ] = [ 'id' => true, 'class' => true ];
774 $basic_tags[ 'tr' ] = [ 'id' => true, 'class' => true ];
775 $basic_tags[ 'td' ] = [ 'id' => true, 'class' => true ];
776 $basic_tags[ 'li' ] = [ 'class' => true ];
777
778 return wp_kses( $value, $basic_tags );
779 }
780
781 /**
782 * Return the current url
783 */
784 function sh_cd_get_current_url() {
785 $protocol = (
786 ( isset($_SERVER['HTTPS'] ) && 'on' == $_SERVER['HTTPS'] ) ||
787 ( isset($_SERVER['SERVER_PORT'] ) && 443 == $_SERVER['SERVER_PORT'] )
788 ) ? 'https://' : 'http://';
789
790 return $protocol . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
791 }
792
793 /**
794 * Get selected default editor
795 */
796 function sh_cd_default_editor_get() {
797 return get_option( 'sh-cd-option-default-editor', 'tinymce' );
798 }
799
800 /**
801 * Is editor valid
802 */
803 function sh_cd_editors_is_valid( $editor ) {
804 $editors = sh_cd_editors_options();
805
806 return ! empty( $editors[ $editor ] );
807 }
808
809 /**
810 * Return valid editors
811 */
812 function sh_cd_editors_options( $keys_only = true ) {
813 return [ 'tinymce' => __( 'WordPress Editor', SH_CD_SLUG ), 'code' => __( 'HTML Editor', SH_CD_SLUG ) ];
814 }
815
816 /**
817 * Are tooltips enabled?
818 */
819 function sh_cd_tooltips_is_enabled() {
820 return ( 'yes' === get_option( 'sh-cd-option-tool-tips-enabled', 'yes' ) );
821 }
822
823 /**
824 * Fetch icons for given shortcode
825 *
826 * @param [type] $shortcode
827 * @param boolean $return_array
828 * @return void
829 */
830 function sh_cd_icons_for_shortcode( $shortcode, $return_array = false ) {
831
832 if ( true === empty( $shortcode ) ) {
833 return [];
834 }
835
836 $icons = [];
837
838 if ( false === empty( $shortcode[ 'header' ] ) ) {
839 $icons[] = sprintf( '<i class="fa-solid fa-heading sh-cd-option-icon sh-cd-tooltip" title="%s"></i>', esc_html( __( 'Insert into WP Header', SH_CD_SLUG ) ) );
840 }
841
842 if ( false === empty( $shortcode[ 'footer' ] ) ) {
843 $icons[] = sprintf( '<i class="fa-solid fa-shoe-prints sh-cd-option-icon sh-cd-tooltip" title="%s"></i>', esc_html( __( 'Insert into WP Footer', SH_CD_SLUG ) ) );
844 }
845
846 if ( false === empty( $shortcode[ 'device_type' ] ) ) {
847
848 $shortcode[ 'device_type' ] = json_decode( $shortcode[ 'device_type' ] );
849
850 if ( true === in_array( 'desktop', $shortcode[ 'device_type' ] ) ) {
851 $icons[] = sprintf( '<i class="fa-solid fa-desktop sh-cd-option-icon sh-cd-tooltip" title="%s"></i>', esc_html( __( 'Display only on desktop devices', SH_CD_SLUG ) ) );
852 }
853
854 if ( true === in_array( 'mobile', $shortcode[ 'device_type' ] ) ) {
855 $icons[] = sprintf( '<i class="fa-solid fa-mobile-screen sh-cd-option-icon sh-cd-tooltip" title="%s"></i>', esc_html( __( 'Display only on mobile devices', SH_CD_SLUG ) ) );
856 }
857
858 if ( true === in_array( 'tablet', $shortcode[ 'device_type' ] ) ) {
859 $icons[] = sprintf( '<i class="fa-solid fa-tablet-screen-button sh-cd-option-icon sh-cd-tooltip" title="%s"></i>', esc_html( __( 'Display only on tablet devices', SH_CD_SLUG ) ) );
860 }
861 }
862
863 if ( true === $return_array ) {
864 return $icons;
865 }
866
867 return ( false === empty( $icons ) ) ? implode( PHP_EOL, $icons ) : '';
868 }