| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die("Jog on!"); |
| 4 |
|
| 5 |
/** |
| 6 |
* Determine if the slug belongs to a preset |
| 7 |
* @param $slug |
| 8 |
*/ |
| 9 |
function sh_cd_is_preset( $slug ) { |
| 10 |
|
| 11 |
$slug = sh_cd_prep_slug( $slug ); |
| 12 |
|
| 13 |
// Free preset? |
| 14 |
if ( true === array_key_exists( $slug, sh_cd_shortcode_presets_free_list() ) ) { |
| 15 |
return 'free'; |
| 16 |
} |
| 17 |
|
| 18 |
// Premium preset? |
| 19 |
if ( true === array_key_exists( $slug, sh_cd_shortcode_presets_premium_list() ) ) { |
| 20 |
return 'premium'; |
| 21 |
} |
| 22 |
|
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Remove old references to the "sc-" prefix |
| 28 |
*/ |
| 29 |
function sh_cd_prep_slug( $slug ) { |
| 30 |
|
| 31 |
$slug = str_replace( 'sc-', '', $slug ); |
| 32 |
|
| 33 |
return $slug; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Return all free and premium premade shortcodes |
| 38 |
* |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
function sh_cd_presets_both_lists() { |
| 42 |
|
| 43 |
$both = array_merge( sh_cd_shortcode_presets_free_list(), sh_cd_shortcode_presets_premium_list() ); |
| 44 |
|
| 45 |
ksort( $both ); |
| 46 |
|
| 47 |
return $both; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* For a given preset, look up the class information, if valid, initiate the class and render |
| 52 |
* |
| 53 |
* @param $args |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
function sh_cd_shortcode_presets_render( $args ) { |
| 58 |
|
| 59 |
$preset = sh_cd_shortcode_presets_fetch( $args[ 'slug' ] ); |
| 60 |
|
| 61 |
// Not a preset? |
| 62 |
if ( false === $preset ) { |
| 63 |
return ''; |
| 64 |
} |
| 65 |
|
| 66 |
// Is this a premium shortcode but no license? |
| 67 |
if ( 'premium' === $preset['sh-cd-type'] && false === sh_cd_is_premium() ) { |
| 68 |
return sprintf( '<p><strong>%s</strong> %s. <a href="%s">%s</a>.<p>', |
| 69 |
__( 'Ooops!', SH_CD_SLUG ), |
| 70 |
__('Unfortunately this is a Premium shortcode. You need to upgrade "Snippet Shortcodes" to use it', SH_CD_SLUG ), |
| 71 |
sh_cd_license_upgrade_link(), |
| 72 |
__('Upgrade now', SH_CD_SLUG ) |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
$class_name = 'SV_' . $preset[ 'class' ]; |
| 77 |
|
| 78 |
if ( true === class_exists( $class_name ) ) { |
| 79 |
|
| 80 |
$shortcode = new $class_name(); |
| 81 |
|
| 82 |
// Any plugin arguments? |
| 83 |
if ( false === empty( $preset['args'] ) ) { |
| 84 |
$args = array_merge( $args, $preset['args'] ); |
| 85 |
} |
| 86 |
|
| 87 |
$shortcode->set_arguments( $args ); |
| 88 |
|
| 89 |
$shortcode->init(); |
| 90 |
|
| 91 |
return $shortcode->sanitised(); |
| 92 |
} |
| 93 |
|
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* For a given slug, fetch the preset information regarding it. |
| 99 |
* |
| 100 |
* @param $slug |
| 101 |
* |
| 102 |
* @return bool |
| 103 |
*/ |
| 104 |
function sh_cd_shortcode_presets_fetch( $slug ) { |
| 105 |
|
| 106 |
$slug = sh_cd_prep_slug( $slug ); |
| 107 |
|
| 108 |
$free_presets = sh_cd_shortcode_presets_free_list(); |
| 109 |
|
| 110 |
// Free preset? |
| 111 |
if ( true === array_key_exists( $slug, $free_presets ) ) { |
| 112 |
|
| 113 |
$preset = $free_presets[ $slug ]; |
| 114 |
$preset['sh-cd-type'] = 'free'; |
| 115 |
|
| 116 |
return $preset; |
| 117 |
} |
| 118 |
|
| 119 |
$premium_presents = sh_cd_shortcode_presets_premium_list(); |
| 120 |
|
| 121 |
// Premium preset? |
| 122 |
if ( true === array_key_exists( $slug, $premium_presents ) ) { |
| 123 |
|
| 124 |
$preset = $premium_presents[ $slug ]; |
| 125 |
$preset['sh-cd-type'] = 'premium'; |
| 126 |
|
| 127 |
return $preset; |
| 128 |
} |
| 129 |
|
| 130 |
return false; |
| 131 |
|
| 132 |
} |