| 1 |
<?php |
| 2 |
|
| 3 |
defined('ABSPATH') or die('Jog on!'); |
| 4 |
|
| 5 |
/** |
| 6 |
* Save / Insert a shortcode |
| 7 |
* |
| 8 |
* @return bool |
| 9 |
*/ |
| 10 |
function sh_cd_shortcodes_save_post() { |
| 11 |
|
| 12 |
// Capture the raw $_POST fields, the save functions will process and validate the data |
| 13 |
$shortcode = sh_cd_get_values_from_post( [ 'id', 'slug', 'data', 'disabled' ] ); |
| 14 |
|
| 15 |
return sh_cd_db_shortcodes_save( $shortcode ); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Replace user parameters within a shortcode e.g. look for %%parameter%% and replace |
| 20 |
* |
| 21 |
* @param $shortcode |
| 22 |
* @param $user_defined_parameters |
| 23 |
* |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
function sh_cd_apply_user_defined_parameters( $shortcode, $user_defined_parameters ){ |
| 27 |
|
| 28 |
// Ensure we have something to do! |
| 29 |
if ( true === empty( $user_defined_parameters ) || false === is_array( $user_defined_parameters ) ) { |
| 30 |
return $shortcode; |
| 31 |
} |
| 32 |
|
| 33 |
foreach ( $user_defined_parameters as $key => $value ) { |
| 34 |
$shortcode = str_replace( '%%' . $key . '%%', $value, $shortcode ); |
| 35 |
} |
| 36 |
|
| 37 |
return $shortcode; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Generate a unique slug |
| 42 |
* |
| 43 |
* @param $slug |
| 44 |
* |
| 45 |
* @return string |
| 46 |
*/ |
| 47 |
function sh_cd_slug_generate( $slug ) { |
| 48 |
|
| 49 |
if ( true === empty( $slug ) ) { |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
$slug = sanitize_title( $slug ); |
| 54 |
|
| 55 |
$original_slug = $slug; |
| 56 |
|
| 57 |
$try = 1; |
| 58 |
|
| 59 |
// Ensure the slug is unique |
| 60 |
while ( false === sh_cd_slug_is_unique( $slug ) ) { |
| 61 |
|
| 62 |
$slug = sprintf( '%s_%d', $original_slug, $try ); |
| 63 |
|
| 64 |
$try++; |
| 65 |
} |
| 66 |
|
| 67 |
return $slug; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Display message in admin UI |
| 72 |
* |
| 73 |
* @param $text |
| 74 |
* @param bool $error |
| 75 |
*/ |
| 76 |
function sh_cd_message_display( $text, $error = false ) { |
| 77 |
|
| 78 |
if ( true === empty( $text ) ) { |
| 79 |
return; |
| 80 |
} |
| 81 |
|
| 82 |
printf( '<div class="%s"><p>%s</p></div>', |
| 83 |
true === $error ? 'error' : 'updated', |
| 84 |
esc_html( $text ) |
| 85 |
); |
| 86 |
|
| 87 |
//TODO: Hook this to use admin_notices |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Fetch cache item |
| 92 |
* |
| 93 |
* @param $key |
| 94 |
* |
| 95 |
* @return mixed |
| 96 |
*/ |
| 97 |
function sh_cd_cache_get( $key ) { |
| 98 |
|
| 99 |
$key = sh_cd_cache_generate_key( $key ); |
| 100 |
|
| 101 |
return get_transient( $key ); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Set cache item |
| 106 |
* |
| 107 |
* @param $key |
| 108 |
* @param $data |
| 109 |
*/ |
| 110 |
function sh_cd_cache_set( $key, $data ) { |
| 111 |
|
| 112 |
$key = sh_cd_cache_generate_key( $key ); |
| 113 |
|
| 114 |
set_transient( $key, $data, 1 * HOUR_IN_SECONDS ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Delete cache for given shortcode slug / ID |
| 119 |
* |
| 120 |
* @param $slug_or_key |
| 121 |
*/ |
| 122 |
function sh_cd_cache_delete_by_slug_or_key( $slug_or_key ) { |
| 123 |
|
| 124 |
if ( true === is_numeric( $slug_or_key ) ) { |
| 125 |
|
| 126 |
sh_cd_cache_delete( sh_cd_db_shortcodes_get_slug_by_id( $slug_or_key ) ); |
| 127 |
|
| 128 |
} else { |
| 129 |
sh_cd_cache_delete( $slug_or_key ); |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Delete cache item |
| 135 |
* |
| 136 |
* @param $key |
| 137 |
* |
| 138 |
* @return mixed |
| 139 |
*/ |
| 140 |
function sh_cd_cache_delete( $key ) { |
| 141 |
|
| 142 |
$key = sh_cd_cache_generate_key( $key ); |
| 143 |
|
| 144 |
return delete_transient( $key ); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Generate cache key |
| 149 |
* |
| 150 |
* @param $key |
| 151 |
* |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
function sh_cd_cache_generate_key( $key ) { |
| 155 |
return SH_CD_SHORTCODE . $key; |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Return link to list own shortcodes |
| 160 |
* |
| 161 |
* @return mixed |
| 162 |
*/ |
| 163 |
function sh_cd_link_your_shortcodes() { |
| 164 |
|
| 165 |
$link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes'); |
| 166 |
|
| 167 |
return esc_url( $link ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Return link to add own shortcode |
| 172 |
* |
| 173 |
* @return mixed |
| 174 |
*/ |
| 175 |
function sh_cd_link_your_shortcodes_add() { |
| 176 |
|
| 177 |
$link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=add'); |
| 178 |
|
| 179 |
return esc_url( $link ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Return link to edit own shortcode |
| 184 |
* |
| 185 |
* @return mixed |
| 186 |
*/ |
| 187 |
function sh_cd_link_your_shortcodes_edit( $id ) { |
| 188 |
|
| 189 |
$link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=edit&id=' . (int) $id ); |
| 190 |
|
| 191 |
return esc_url( $link ); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Return link to delete own shortcode |
| 196 |
* |
| 197 |
* @return mixed |
| 198 |
*/ |
| 199 |
function sh_cd_link_your_shortcodes_delete( $id ) { |
| 200 |
|
| 201 |
$link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id ); |
| 202 |
|
| 203 |
return esc_url( $link ); |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Either fetch data from the $_POST object or from the array passed in! |
| 208 |
* |
| 209 |
* @param $object |
| 210 |
* @param $key |
| 211 |
* @return string |
| 212 |
*/ |
| 213 |
function sh_cd_get_value_from_post_or_obj( $object, $key ) { |
| 214 |
|
| 215 |
if ( true === isset( $_POST[ $key ] ) ) { |
| 216 |
return $_POST[ $key ]; |
| 217 |
} |
| 218 |
|
| 219 |
if ( true === isset( $object[ $key ] ) ) { |
| 220 |
return $object[ $key ]; |
| 221 |
} |
| 222 |
|
| 223 |
return ''; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Either fetch data from the $_POST object for the given object keys |
| 228 |
* |
| 229 |
* @param $meta_field |
| 230 |
* @return string |
| 231 |
*/ |
| 232 |
function sh_cd_get_values_from_post( $keys ) { |
| 233 |
|
| 234 |
$data = []; |
| 235 |
|
| 236 |
foreach ( $keys as $key ) { |
| 237 |
|
| 238 |
if ( true === isset( $_POST[ $key ] ) ) { |
| 239 |
$data[ $key ] = $_POST[ $key ]; |
| 240 |
} else { |
| 241 |
$data[ $key ] = ''; |
| 242 |
} |
| 243 |
|
| 244 |
} |
| 245 |
|
| 246 |
return $data; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* Toggle the status of a shortcode |
| 251 |
* |
| 252 |
* @param $id |
| 253 |
*/ |
| 254 |
function sh_cd_toggle_status( $id ) { |
| 255 |
|
| 256 |
$slug = sh_cd_db_shortcodes_by_id( (int) $id ); |
| 257 |
|
| 258 |
if ( false === empty( $slug ) ) { |
| 259 |
|
| 260 |
$status = ( 1 === (int) $slug['disabled'] ) ? 0 : 1 ; |
| 261 |
|
| 262 |
sh_cd_db_shortcodes_update_status( $id, $status ); |
| 263 |
|
| 264 |
return $status; |
| 265 |
} |
| 266 |
|
| 267 |
return NULL; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* Display a table of premade shortcodes |
| 273 |
* |
| 274 |
* @param string $display |
| 275 |
*/ |
| 276 |
function sh_cd_display_premade_shortcodes( $display = 'all' ) { |
| 277 |
|
| 278 |
$premium_user = sh_cd_license_is_premium(); |
| 279 |
$upgrade_link = sprintf( '<a class="button" href="%s"><i class="fas fa-check"></i> Upgrade now</a>', sh_cd_license_upgrade_link() ); |
| 280 |
|
| 281 |
switch ( $display ) { |
| 282 |
case 'free': |
| 283 |
$shortcodes = sh_cd_shortcode_presets_free_list(); |
| 284 |
$show_premium_col = false; |
| 285 |
break; |
| 286 |
case 'premium': |
| 287 |
$shortcodes = sh_cd_shortcode_presets_premium_list(); |
| 288 |
$show_premium_col = false; |
| 289 |
break; |
| 290 |
default: |
| 291 |
$shortcodes = sh_cd_presets_both_lists(); |
| 292 |
$show_premium_col = true; |
| 293 |
} |
| 294 |
|
| 295 |
$html = '<table class="widefat sh-cd-table" width="100%"> |
| 296 |
<tr class="row-title"> |
| 297 |
<th class="row-title" width="30%">Shortcode</th>'; |
| 298 |
|
| 299 |
if ( true === $show_premium_col) { |
| 300 |
$html .= '<th class="row-title">Premium</th>'; |
| 301 |
} |
| 302 |
|
| 303 |
$html .= '<th width="*">Description</th> |
| 304 |
</tr>'; |
| 305 |
|
| 306 |
$class = ''; |
| 307 |
|
| 308 |
foreach ( $shortcodes as $key => $data ) { |
| 309 |
|
| 310 |
$class = ($class == 'alternate') ? '' : 'alternate'; |
| 311 |
|
| 312 |
$shortcode = '[' . SH_CD_SHORTCODE. ' slug="' . $key . '"]'; |
| 313 |
|
| 314 |
$premium_shortcode = ( true === $data['premium'] ); |
| 315 |
|
| 316 |
$html .= sprintf( '<tr class="%s"><td>%s</td>', $class, esc_html( $shortcode ) ); |
| 317 |
|
| 318 |
|
| 319 |
if ( true === $show_premium_col) { |
| 320 |
|
| 321 |
$html .= sprintf( '<td align="middle">%s%s</td>', |
| 322 |
( true === $premium_shortcode && true === $premium_user ) ? '<i class="fas fa-check"></i>' : '', |
| 323 |
( true == $premium_shortcode && false === $premium_user ) ? $upgrade_link : '' |
| 324 |
); |
| 325 |
} |
| 326 |
|
| 327 |
$html .= sprintf( '<td>%s</td></tr>', esc_html( $data['description'] ) ); |
| 328 |
|
| 329 |
} |
| 330 |
|
| 331 |
$html .= '</table>'; |
| 332 |
|
| 333 |
return $html; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Display an upgrade button |
| 338 |
*/ |
| 339 |
function sh_cd_upgrade_button( $css_class = '', $link = NULL ) { |
| 340 |
|
| 341 |
$link = ( false === empty( $link ) ) ? $link : SH_CD_UPGRADE_LINK . '?hash=' . sh_cd_generate_site_hash() ; |
| 342 |
|
| 343 |
echo sprintf('<a href="%s" class="button-primary sh-cd-upgrade-button%s"><i class="far fa-credit-card"></i> %s £%d %s</a>', |
| 344 |
esc_url( $link ), |
| 345 |
esc_attr( ' ' . $css_class ), |
| 346 |
__('Upgrade to Premium for ', SH_CD_SLUG), |
| 347 |
SH_CD_PREMIUM_PRICE, |
| 348 |
__('a year ', SH_CD_SLUG) |
| 349 |
); |
| 350 |
} |