| @@ -48,18 +48,16 @@ | ||
| 48 | 48 | * @return bool |
| 49 | 49 | */ |
| 50 | 50 | function sh_cd_shortcodes_save_post() { |
| 51 | 51 | |
| 52 | - $fields = apply_filters( 'sh-cd-post-field-keys', [ 'id', 'slug', 'previous_slug', 'data', 'disabled', 'multisite', 'editor' ] ); | |
| 53 | - | |
| 54 | 52 | // Capture the raw $_POST fields, the save functions will process and validate the data |
| 55 | - $shortcode = sh_cd_get_values_from_post( $fields ); | |
| 53 | + $shortcode = sh_cd_get_values_from_post( [ 'id', 'slug', 'previous_slug', 'data', 'disabled', 'multisite', 'editor' ] ); | |
| 56 | 54 | |
| 57 | 55 | // 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 | 56 | if ( ! sh_cd_is_premium() && false === empty( $shortcode[ 'previous_slug' ] ) ) { |
| 59 | 57 | $shortcode[ 'slug' ] = $shortcode[ 'previous_slug' ]; |
| 60 | 58 | } |
| 61 | - | |
| 59 | + | |
| 62 | 60 | return sh_cd_db_shortcodes_save( $shortcode ); |
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | /** |
| @@ -189,10 +187,8 @@ | ||
| 189 | 187 | |
| 190 | 188 | $key = sh_cd_cache_generate_key( $key ); |
| 191 | 189 | |
| 192 | 190 | set_transient( $key, $data, $expire ); |
| 193 | - | |
| 194 | - do_action( 'sh-cd-global-cache-delete' ); | |
| 195 | 191 | } |
| 196 | 192 | |
| 197 | 193 | /** |
| 198 | 194 | * Delete cache for given shortcode slug / ID |
| @@ -224,20 +220,15 @@ | ||
| 224 | 220 | * @param $key |
| 225 | 221 | * |
| 226 | 222 | * @return mixed |
| 227 | 223 | */ |
| 228 | -function sh_cd_cache_delete( $key, $trigger_global_hook = true ) { | |
| 224 | +function sh_cd_cache_delete( $key ) { | |
| 229 | 225 | |
| 230 | 226 | $key = sh_cd_cache_generate_key( $key ); |
| 231 | 227 | |
| 232 | - if ( true === $trigger_global_hook ) { | |
| 233 | - do_action( 'sh-cd-global-cache-delete' ); | |
| 234 | - } | |
| 235 | - | |
| 236 | 228 | return delete_transient( $key ); |
| 237 | 229 | } |
| 238 | 230 | |
| 239 | - | |
| 240 | 231 | /** |
| 241 | 232 | * Generate cache key |
| 242 | 233 | * |
| 243 | 234 | * @param $key |
| @@ -284,8 +275,21 @@ | ||
| 284 | 275 | return esc_url( $link ); |
| 285 | 276 | } |
| 286 | 277 | |
| 287 | 278 | /** |
| 279 | + * Return link to delete own shortcode | |
| 280 | + * | |
| 281 | + * @param $id | |
| 282 | + * @return mixed | |
| 283 | + */ | |
| 284 | +function sh_cd_link_your_shortcodes_delete( $id ) { | |
| 285 | + | |
| 286 | + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id ); | |
| 287 | + | |
| 288 | + return esc_url( $link ); | |
| 289 | +} | |
| 290 | + | |
| 291 | +/** | |
| 288 | 292 | * Either fetch data from the $_POST object or from the array passed in! |
| 289 | 293 | * |
| 290 | 294 | * @param $object |
| 291 | 295 | * @param $key |
| @@ -419,12 +423,8 @@ | ||
| 419 | 423 | * @return bool |
| 420 | 424 | */ |
| 421 | 425 | function sh_cd_is_multisite_enabled() { |
| 422 | 426 | |
| 423 | - if ( true === defined( 'YK_TEST_IS_MULTISITE' ) && true === YK_TEST_IS_MULTISITE ) { | |
| 424 | - return true; | |
| 425 | - } | |
| 426 | - | |
| 427 | 427 | if ( false === is_multisite() ) { |
| 428 | 428 | return false; |
| 429 | 429 | } |
| 430 | 430 | |
| @@ -540,21 +540,19 @@ | ||
| 540 | 540 | * Display upgrade notice |
| 541 | 541 | * |
| 542 | 542 | * @param bool $pro_plus |
| 543 | 543 | */ |
| 544 | -function sh_cd_display_pro_upgrade_notice( $title = NULL, $content = '', $class = '' ) { | |
| 545 | - | |
| 546 | - $title = ( true === empty( $title ) ) ? __( 'Upgrade Snippet Shortcodes and get more features!', SH_CD_SLUG ) : $title; | |
| 544 | +function sh_cd_display_pro_upgrade_notice( ) { | |
| 545 | + ?> | |
| 547 | 546 | |
| 548 | - ?> | |
| 549 | - <div class="postbox sh-cd-advertise-premium <?php echo esc_attr( $class ) ?>"> | |
| 550 | - <h3 class="hndle"><i class="fa-regular fa-star"></i> <?php echo esc_html( $title ) ?></h3> | |
| 547 | + <div class="postbox sh-cd-advertise-premium"> | |
| 548 | + <h3 class="hndle"><span><?php echo __( 'Upgrade Snippet Shortcodes and get more features!', SH_CD_SLUG ); ?> </span></h3> | |
| 551 | 549 | <div style="padding: 0px 15px 0px 15px"> |
| 552 | - <p><?php echo wp_kses( $content, [ 'ul' => [ 'class' ], 'li' => [], 'strong' => [], 'span' => [], 'div' => [] ] ); ?></p> | |
| 553 | 550 | <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> |
| 554 | 551 | </div> |
| 555 | 552 | </div> |
| 556 | -<?php | |
| 553 | + | |
| 554 | + <?php | |
| 557 | 555 | } |
| 558 | 556 | |
| 559 | 557 | /** |
| 560 | 558 | * Display a star to prompt for a Premioum upgrade |
| @@ -592,9 +590,11 @@ | ||
| 592 | 590 | * @return string |
| 593 | 591 | */ |
| 594 | 592 | function sh_cd_import_csv( $attachment_id, $dry_run = true ) { |
| 595 | 593 | |
| 596 | - sh_cd_permission_check(); | |
| 594 | + if ( false === sh_cd_permission_check() ) { | |
| 595 | + return 'You do not have the correct admin permissions'; | |
| 596 | + } | |
| 597 | 597 | |
| 598 | 598 | if ( false === sh_cd_is_premium() ) { |
| 599 | 599 | return 'This is a premium feature'; |
| 600 | 600 | } |
| @@ -611,13 +611,8 @@ | ||
| 611 | 611 | if ( true === empty( $csv ) ) { |
| 612 | 612 | return 'Error: The CSV appears to be empty.'; |
| 613 | 613 | } |
| 614 | 614 | |
| 615 | - // Lowercase the header row up front so it's compared consistently against our | |
| 616 | - // (lowercase) column names both here and per-row below - previously the header row | |
| 617 | - // was validated case-sensitively while each data row's keys were lowercased. | |
| 618 | - $csv[0] = array_map( 'strtolower', $csv[0] ); | |
| 619 | - | |
| 620 | 615 | array_walk($csv, function(&$a) use ($csv) { |
| 621 | 616 | $a = array_combine($csv[0], $a); |
| 622 | 617 | }); |
| 623 | 618 | |
| @@ -660,19 +655,15 @@ | ||
| 660 | 655 | } |
| 661 | 656 | |
| 662 | 657 | if ( false === $dry_run ) { |
| 663 | 658 | |
| 664 | - $shortcode = [ 'previous_slug' => '' ]; | |
| 659 | + $shortcode = [ 'slug' => $row[ 'slug' ], | |
| 660 | + 'previous_slug' => '', | |
| 661 | + 'data' => $row[ 'content' ], | |
| 662 | + 'disabled' => ! sh_cd_to_bool( $row[ 'enabled' ] ), | |
| 663 | + 'multisite' => sh_cd_to_bool( $row[ 'global' ] ) | |
| 664 | + ]; | |
| 665 | 665 | |
| 666 | - foreach ( sh_cd_csv_columns() as $column_name => $column ) { | |
| 667 | - | |
| 668 | - if ( false === isset( $row[ $column_name ] ) ) { | |
| 669 | - continue; | |
| 670 | - } | |
| 671 | - | |
| 672 | - $shortcode = array_merge( $shortcode, call_user_func( $column[ 'import' ], $row[ $column_name ] ) ); | |
| 673 | - } | |
| 674 | - | |
| 675 | 666 | $result = sh_cd_db_shortcodes_save( $shortcode ); |
| 676 | 667 | |
| 677 | 668 | if ( false === $result ) { |
| 678 | 669 | $output .= 'Skipped: Error inserting into database (most likely a field contains too many characters or in the wrong format): ' . implode( ',', $row ) . PHP_EOL; |
| @@ -698,16 +689,14 @@ | ||
| 698 | 689 | * @return bool|string |
| 699 | 690 | */ |
| 700 | 691 | function sh_cd_import_csv_validate_header( $header_row ) { |
| 701 | 692 | |
| 702 | - $required_columns = array_keys( array_filter( sh_cd_csv_columns(), function( $column ) { | |
| 703 | - return true === $column[ 'required' ]; | |
| 704 | - } ) ); | |
| 693 | + $expected_headers = [ 'slug', 'content', 'global', 'enabled' ]; | |
| 705 | 694 | |
| 706 | - foreach ( $required_columns as $column ) { | |
| 695 | + foreach ( $expected_headers as $column ) { | |
| 707 | 696 | |
| 708 | 697 | if ( false === isset( $header_row[ $column ] ) ) { |
| 709 | - return 'Missing column: ' . $column . '. Expecting at least: ' . implode( ',', $required_columns ) . PHP_EOL; | |
| 698 | + return 'Missing column: ' . $column . '. Expecting: ' . implode( ',', $expected_headers ) . PHP_EOL; | |
| 710 | 699 | } |
| 711 | 700 | } |
| 712 | 701 | |
| 713 | 702 | return true; |
| @@ -724,10 +713,10 @@ | ||
| 724 | 713 | if ( true === empty( $csv_row[ 'slug' ] ) ) { |
| 725 | 714 | return 'Skipped: Missing slug: ' . implode( ',', $csv_row ); |
| 726 | 715 | } |
| 727 | 716 | |
| 728 | - if ( true === empty( $csv_row[ 'content' ] ) ) { | |
| 729 | - return 'Skipped: Missing content: ' . implode( ',', $csv_row ); | |
| 717 | + if ( false === empty( $isset[ 'content' ] ) ) { | |
| 718 | + return 'Skipped: Content: ' . implode( ',', $csv_row ); | |
| 730 | 719 | } |
| 731 | 720 | |
| 732 | 721 | $allowed_bools = [ 'yes', 'no', 'true', 'false', '1', '0' ]; |
| 733 | 722 | |
| @@ -740,70 +729,12 @@ | ||
| 740 | 729 | false === in_array( $csv_row[ 'enabled' ], $allowed_bools ) ) { |
| 741 | 730 | return 'Skipped: Invalid "enabled" value. Must be "yes" or "no": ' . implode( ',', $csv_row ); |
| 742 | 731 | } |
| 743 | 732 | |
| 744 | - // Give any Premium-registered columns a chance to reject their own value, so dry-run | |
| 745 | - // mode surfaces bad input instead of it being silently coerced later on save. | |
| 746 | - foreach ( sh_cd_csv_columns() as $column_name => $column ) { | |
| 747 | - | |
| 748 | - if ( false === isset( $csv_row[ $column_name ] ) || false === isset( $column[ 'validate' ] ) ) { | |
| 749 | - continue; | |
| 750 | - } | |
| 751 | - | |
| 752 | - $validation_result = call_user_func( $column[ 'validate' ], $csv_row[ $column_name ] ); | |
| 753 | - | |
| 754 | - if ( true !== $validation_result ) { | |
| 755 | - return $validation_result; | |
| 756 | - } | |
| 757 | - } | |
| 758 | - | |
| 759 | 733 | return true; |
| 760 | 734 | } |
| 761 | 735 | |
| 762 | 736 | /** |
| 763 | - * Ordered map of CSV column name => column definition, used by both sh_cd_import_csv() and | |
| 764 | - * sh_cd_export_csv(). Extensible via the 'sh-cd-csv-columns' filter so Premium can add columns | |
| 765 | - * for its own fields without core knowing about them - column names must be unique (a later | |
| 766 | - * registration silently overwrites an earlier one of the same name, same as any other array-shaped | |
| 767 | - * filter in this plugin). | |
| 768 | - * | |
| 769 | - * Each column definition: | |
| 770 | - * 'required' => bool column must be present in the CSV header row | |
| 771 | - * 'export' => callable( array $shortcode ): string decoded shortcode row -> CSV cell value | |
| 772 | - * 'import' => callable( string $value ): array CSV cell value -> partial $shortcode to merge | |
| 773 | - * 'validate' => callable( string $value ): true|string optional; true, or an error message | |
| 774 | - * | |
| 775 | - * @return array | |
| 776 | - */ | |
| 777 | -function sh_cd_csv_columns() { | |
| 778 | - | |
| 779 | - $columns = [ | |
| 780 | - 'slug' => [ | |
| 781 | - 'required' => true, | |
| 782 | - 'export' => function( $shortcode ) { return $shortcode[ 'slug' ]; }, | |
| 783 | - 'import' => function( $value ) { return [ 'slug' => $value ]; }, | |
| 784 | - ], | |
| 785 | - 'content' => [ | |
| 786 | - 'required' => true, | |
| 787 | - 'export' => function( $shortcode ) { return stripslashes( $shortcode[ 'data' ] ); }, | |
| 788 | - 'import' => function( $value ) { return [ 'data' => $value ]; }, | |
| 789 | - ], | |
| 790 | - 'global' => [ | |
| 791 | - 'required' => true, | |
| 792 | - 'export' => function( $shortcode ) { return ( 1 === (int) $shortcode[ 'multisite' ] ) ? 'yes' : 'no'; }, | |
| 793 | - 'import' => function( $value ) { return [ 'multisite' => sh_cd_to_bool( $value ) ? 1 : 0 ]; }, | |
| 794 | - ], | |
| 795 | - 'enabled' => [ | |
| 796 | - 'required' => true, | |
| 797 | - 'export' => function( $shortcode ) { return ( 1 === (int) $shortcode[ 'disabled' ] ) ? 'no' : 'yes'; }, | |
| 798 | - 'import' => function( $value ) { return [ 'disabled' => sh_cd_to_bool( $value ) ? 0 : 1 ]; }, | |
| 799 | - ], | |
| 800 | - ]; | |
| 801 | - | |
| 802 | - return apply_filters( 'sh-cd-csv-columns', $columns ); | |
| 803 | -} | |
| 804 | - | |
| 805 | -/** | |
| 806 | 737 | * Convert string to bool |
| 807 | 738 | * @param $string |
| 808 | 739 | * @return mixed |
| 809 | 740 | */ |
| @@ -811,49 +742,8 @@ | ||
| 811 | 742 | return filter_var( $string, FILTER_VALIDATE_BOOLEAN ); |
| 812 | 743 | } |
| 813 | 744 | |
| 814 | 745 | /** |
| 815 | - * Export all shortcodes as a CSV string in the same column format sh_cd_import_csv() expects | |
| 816 | - * (see sh_cd_csv_columns()), so the output can be re-imported unchanged. | |
| 817 | - * | |
| 818 | - * @return string | |
| 819 | - */ | |
| 820 | -function sh_cd_export_csv() { | |
| 821 | - | |
| 822 | - sh_cd_permission_check(); | |
| 823 | - | |
| 824 | - $shortcodes = sh_cd_db_shortcodes_all(); | |
| 825 | - $columns = sh_cd_csv_columns(); | |
| 826 | - | |
| 827 | - $stream = fopen( 'php://temp', 'r+' ); | |
| 828 | - | |
| 829 | - fputcsv( $stream, array_keys( $columns ) ); | |
| 830 | - | |
| 831 | - foreach ( $shortcodes as $shortcode ) { | |
| 832 | - | |
| 833 | - // sh_cd_db_shortcodes_all() returns raw DB rows - run each through the same | |
| 834 | - // 'sh-cd-db-loaded-shortcode' filter used when loading a single shortcode, so | |
| 835 | - // Premium's JSON-encoded columns (device_type, roles, etc.) are decoded back into | |
| 836 | - // arrays before the column export callbacks below run. | |
| 837 | - $shortcode = sh_cd_db_filter_loaded_shortcode( $shortcode ); | |
| 838 | - | |
| 839 | - $row = []; | |
| 840 | - | |
| 841 | - foreach ( $columns as $column ) { | |
| 842 | - $row[] = call_user_func( $column[ 'export' ], $shortcode ); | |
| 843 | - } | |
| 844 | - | |
| 845 | - fputcsv( $stream, $row ); | |
| 846 | - } | |
| 847 | - | |
| 848 | - rewind( $stream ); | |
| 849 | - $csv = stream_get_contents( $stream ); | |
| 850 | - fclose( $stream ); | |
| 851 | - | |
| 852 | - return $csv; | |
| 853 | -} | |
| 854 | - | |
| 855 | -/** | |
| 856 | 746 | * Our version of kses and the HTML we are happy with |
| 857 | 747 | */ |
| 858 | 748 | function sh_cd_wp_kses( $value ) { |
| 859 | 749 | |
| @@ -912,54 +802,5 @@ | ||
| 912 | 802 | * Are tooltips enabled? |
| 913 | 803 | */ |
| 914 | 804 | function sh_cd_tooltips_is_enabled() { |
| 915 | 805 | return ( 'yes' === get_option( 'sh-cd-option-tool-tips-enabled', 'yes' ) ); |
| 916 | -} | |
| 917 | - | |
| 918 | -/** | |
| 919 | - * Fetch icons for given shortcode | |
| 920 | - * | |
| 921 | - * @param [type] $shortcode | |
| 922 | - * @param boolean $return_array | |
| 923 | - * @return void | |
| 924 | - */ | |
| 925 | -function sh_cd_icons_for_shortcode( $shortcode, $return_array = false ) { | |
| 926 | - | |
| 927 | - if ( true === empty( $shortcode ) ) { | |
| 928 | - return []; | |
| 929 | - } | |
| 930 | - | |
| 931 | - $icons = []; | |
| 932 | - | |
| 933 | - if ( false === empty( $shortcode[ 'header' ] ) ) { | |
| 934 | - $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 ) ) ); | |
| 935 | - } | |
| 936 | - | |
| 937 | - if ( false === empty( $shortcode[ 'footer' ] ) ) { | |
| 938 | - $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 ) ) ); | |
| 939 | - } | |
| 940 | - | |
| 941 | - if ( false === empty( $shortcode[ 'device_type' ] ) ) { | |
| 942 | - $shortcode[ 'device_type' ] = json_decode( $shortcode[ 'device_type' ] ); | |
| 943 | - } | |
| 944 | - | |
| 945 | - if ( true === is_array( $shortcode[ 'device_type' ] ) ) { | |
| 946 | - | |
| 947 | - if ( true === in_array( 'desktop', $shortcode[ 'device_type' ] ) ) { | |
| 948 | - $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 ) ) ); | |
| 949 | - } | |
| 950 | - | |
| 951 | - if ( true === in_array( 'mobile', $shortcode[ 'device_type' ] ) ) { | |
| 952 | - $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 ) ) ); | |
| 953 | - } | |
| 954 | - | |
| 955 | - if ( true === in_array( 'tablet', $shortcode[ 'device_type' ] ) ) { | |
| 956 | - $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 ) ) ); | |
| 957 | - } | |
| 958 | - } | |
| 959 | - | |
| 960 | - if ( true === $return_array ) { | |
| 961 | - return $icons; | |
| 962 | - } | |
| 963 | - | |
| 964 | - return ( false === empty( $icons ) ) ? implode( PHP_EOL, $icons ) : ''; | |
| 965 | 806 | } |