PluginProbe
Snippet Shortcodes / 3.0.2
Snippet Shortcodes v3.0.2
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
← All changes | includes/functions.php +344 -241 1.83.0.2 View file →
@@ -1,352 +1,455 @@
1 1 <?php
2 2
3 3 defined('ABSPATH') or die('Jog on!');
4 4
5 -function sh_cd_get_user_defined_arguments($default, $specified) {
5 +/**
6 + * Save / Insert a shortcode
7 + *
8 + * @return bool
9 + */
10 +function sh_cd_shortcodes_save_post() {
6 11
7 - $user_defined = array();
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', 'previous_slug', 'data', 'disabled', 'multisite' ] );
8 14
9 - if(is_array($default) && !empty($default) && is_array($specified) && !empty($specified)) {
15 + return sh_cd_db_shortcodes_save( $shortcode );
16 +}
10 17
11 - foreach ($specified as $key => $value) {
12 - if(!isset($default[$key])) {
13 - $user_defined[$key] = $value;
14 - }
15 - }
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 ){
16 27
17 - }
28 + // Ensure we have something to do!
29 + if ( true === empty( $user_defined_parameters ) || false === is_array( $user_defined_parameters ) ) {
30 + return $shortcode;
31 + }
18 32
19 - return (is_array($user_defined) && !empty($user_defined)) ? $user_defined : false;
33 + foreach ( $user_defined_parameters as $key => $value ) {
34 + $shortcode = str_replace( '%%' . $key . '%%', $value, $shortcode );
35 + }
36 +
37 + return $shortcode;
20 38 }
21 39
22 -function sh_cd_apply_user_defined_parameters($shortcode, $user_defined_parameters){
40 +/**
41 + * Generate a unique slug
42 + *
43 + * @param $slug
44 + *
45 + * @return string
46 + */
47 +function sh_cd_slug_generate( $slug, $exising_id = NULL ) {
23 48
24 - if(!empty($shortcode) && is_array($user_defined_parameters) && !empty($user_defined_parameters)) {
49 + if ( true === empty( $slug ) ) {
50 + return NULL;
51 + }
25 52
26 - foreach ($user_defined_parameters as $key => $value) {
27 - $shortcode = str_replace('%%' . $key . '%%', $value, $shortcode);
28 - }
29 - }
53 + $slug = sanitize_title( $slug );
30 54
31 - return $shortcode;
55 + $original_slug = $slug;
56 +
57 + $try = 1;
58 +
59 + // Ensure the slug is unique
60 + while ( false === sh_cd_slug_is_unique( $slug, $exising_id ) ) {
61 +
62 + $slug = sprintf( '%s_%d', $original_slug, $try );
63 +
64 + $try++;
65 + }
66 +
67 + return $slug;
32 68 }
33 69
34 -function sh_cd_get_all_shortcodes()
35 -{
36 - global $wpdb;
70 +/**
71 + * Clone an existing shortcode!
72 + *
73 + * @param $id
74 + *
75 + * @return bool
76 + */
77 +function sh_cd_clone( $id ) {
37 78
38 - $sql = 'SELECT * FROM ' . $wpdb->prefix . SH_CD_TABLE . ' order by slug asc';
79 + if( false === sh_cd_license_is_premium() ) {
80 + return true;
81 + }
39 82
40 - $rows = $wpdb->get_results( $sql );
83 + if ( false === is_numeric( $id ) ) {
84 + return false;
85 + }
41 86
42 - if (!is_null($rows))
43 - return $rows;
87 + $to_be_cloned = sh_cd_db_shortcodes_by_id( $id );
44 88
45 - return false;
89 + if ( true === empty( $to_be_cloned ) ) {
90 + return false;
91 + }
46 92
93 + unset( $to_be_cloned['id'] );
94 +
95 + return sh_cd_db_shortcodes_save( $to_be_cloned );
47 96 }
48 97
49 -function sh_cd_get_shortcode($id)
50 -{
51 - if (!is_admin())
52 - return false;
98 +/**
99 + * Display message in admin UI
100 + *
101 + * @param $text
102 + * @param bool $error
103 + */
104 +function sh_cd_message_display( $text, $error = false ) {
53 105
54 - global $wpdb;
106 + if ( true === empty( $text ) ) {
107 + return;
108 + }
55 109
56 - $sql = $wpdb->prepare('SELECT slug, data, disabled FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
110 + printf( '<div class="%s"><p>%s</p></div>',
111 + true === $error ? 'error' : 'updated',
112 + esc_html( $text )
113 + );
57 114
58 - $row = $wpdb->get_row( $sql );
115 + //TODO: Hook this to use admin_notices
116 +}
59 117
60 - if (!is_null($row))
61 - return $row;
118 +/**
119 + * Fetch cache item
120 + *
121 + * @param $key
122 + *
123 + * @return mixed
124 + */
125 +function sh_cd_cache_get( $key ) {
62 126
63 - return false;
127 + $key = sh_cd_cache_generate_key( $key );
64 128
129 + return get_transient( $key );
65 130 }
66 -function sh_cd_get_shortcode_by_slug($slug)
67 -{
68 - global $wpdb;
69 131
70 - $sql = $wpdb->prepare('SELECT data FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s and disabled <> 1', $slug);
132 +/**
133 + * Set cache item
134 + *
135 + * @param $key
136 + * @param $data
137 + */
138 +function sh_cd_cache_set( $key, $data, $expire = NULL ) {
71 139
72 - $row = $wpdb->get_var( $sql );
73 140
74 - if (!is_null($row))
75 - return stripslashes($row);
141 + $expire = ( false === empty( $expire ) ) ? (int) $expire : 1 * HOUR_IN_SECONDS;
76 142
77 - return false;
143 + $key = sh_cd_cache_generate_key( $key );
78 144
145 + set_transient( $key, $data, $expire );
79 146 }
80 -function sh_cd_get_slug_by_id($id)
81 -{
82 - global $wpdb;
83 147
84 - $sql = $wpdb->prepare('SELECT slug FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
148 +/**
149 + * Delete cache for given shortcode slug / ID
150 + *
151 + * @param $slug_or_key
152 + */
153 +function sh_cd_cache_delete_by_slug_or_key( $slug_or_key ) {
85 154
86 - $row = $wpdb->get_var( $sql );
155 + if ( true === is_numeric( $slug_or_key ) ) {
87 156
88 - if (!is_null($row))
89 - return $row;
157 + $slug_or_key = sh_cd_db_shortcodes_get_slug_by_id( $slug_or_key );
90 158
91 - return false;
159 + sh_cd_cache_delete( $slug_or_key );
92 160
161 + } else {
162 + sh_cd_cache_delete( $slug_or_key );
163 + }
164 +
165 + // Delete site option
166 + $slug_or_key = SH_CD_PREFIX . $slug_or_key;
167 +
168 + delete_site_option( $slug_or_key );
169 +
93 170 }
94 171
95 -function sh_cd_save_shortcode($slug, $data, $disabled = false, $id = false)
96 -{
97 - if (!is_admin())
98 - return false;
172 +/**
173 + * Delete cache item
174 + *
175 + * @param $key
176 + *
177 + * @return mixed
178 + */
179 +function sh_cd_cache_delete( $key ) {
99 180
100 - if (false == $id && empty($slug))
101 - return false;
181 + $key = sh_cd_cache_generate_key( $key );
102 182
103 - global $wpdb;
183 + return delete_transient( $key );
184 +}
104 185
105 - $slug = sh_cd_get_slug($slug);
186 +/**
187 + * Generate cache key
188 + *
189 + * @param $key
190 + *
191 + * @return string
192 + */
193 +function sh_cd_cache_generate_key( $key ) {
194 + return SH_CD_SHORTCODE . SH_CD_PLUGIN_VERSION . $key;
195 +}
106 196
107 - $result = false;
197 +/**
198 + * Return link to list own shortcodes
199 + *
200 + * @return mixed
201 + */
202 +function sh_cd_link_your_shortcodes() {
108 203
109 - $disabled = (true === $disabled) ? 1: 0;
204 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes');
110 205
111 - if ($id)
112 - {
113 - //Update data (Slug can never be updated!)
114 - $result = $wpdb->update(
115 - $wpdb->prefix . SH_CD_TABLE,
116 - array(
117 - 'data' => $data,
118 - 'disabled' => $disabled
119 - ),
120 - array( 'id' => $id),
121 - array(
122 - '%s',
123 - '%d'
124 - ),
125 - array( '%d' )
126 - );
206 + return esc_url( $link );
207 +}
127 208
128 - sh_cd_delete_cache(sh_cd_get_slug_by_id($id));
129 - }
130 - else
131 - {
132 - $result = $wpdb->insert(
133 - $wpdb->prefix . SH_CD_TABLE,
134 - array(
135 - 'slug' => $slug,
136 - 'data' => $data,
137 - 'disabled' => $disabled
138 - ),
139 - array(
140 - '%s',
141 - '%s',
142 - '%d'
143 - )
144 - );
209 +/**
210 + * Return link to add own shortcode
211 + *
212 + * @return mixed
213 + */
214 +function sh_cd_link_your_shortcodes_add() {
145 215
146 - sh_cd_delete_cache($slug);
147 - }
216 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=add');
148 217
149 - if (false === $result) {
150 - return false;
151 - }
152 - else
153 - {
154 - return true;
155 - }
218 + return esc_url( $link );
219 +}
156 220
221 +/**
222 + * Return link to edit own shortcode
223 + *
224 + * @return mixed
225 + */
226 +function sh_cd_link_your_shortcodes_edit( $id ) {
227 +
228 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=edit&id=' . (int) $id );
229 +
230 + return esc_url( $link );
157 231 }
158 232
159 -function sh_cd_delete_shortcode($id)
160 -{
161 - if (!is_admin() || !is_numeric($id))
162 - return false;
233 +/**
234 + * Return link to delete own shortcode
235 + *
236 + * @return mixed
237 + */
238 +function sh_cd_link_your_shortcodes_delete( $id ) {
163 239
164 - global $wpdb;
240 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id );
165 241
166 - // Clear cached version
167 - sh_cd_delete_cache(sh_cd_get_slug_by_id($id));
242 + return esc_url( $link );
243 +}
168 244
169 - if (false === $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, array( 'id' => $id ) )) {
170 - return false;
171 - }
172 - else {
173 - return true;
174 - }
245 +/**
246 + * Either fetch data from the $_POST object or from the array passed in!
247 + *
248 + * @param $object
249 + * @param $key
250 + * @return string
251 + */
252 +function sh_cd_get_value_from_post_or_obj( $object, $key ) {
175 253
254 + if ( true === isset( $_POST[ $key ] ) ) {
255 + return $_POST[ $key ];
256 + }
257 +
258 + if ( true === isset( $object[ $key ] ) ) {
259 + return $object[ $key ];
260 + }
261 +
262 + return '';
176 263 }
177 264
178 -function sh_cd_get_slug($text)
179 -{
180 - if(!empty($text))
181 - {
182 - $text = sanitize_title($text);
265 +/**
266 + * Either fetch data from the $_POST object for the given object keys
267 + *
268 + * @param $meta_field
269 + * @return string
270 + */
271 +function sh_cd_get_values_from_post( $keys ) {
183 272
184 - $original_slug = $text;
273 + $data = [];
185 274
186 - $try = 1;
275 + foreach ( $keys as $key ) {
187 276
188 - // If slug exists, then fetch a unique one!
189 - // v1.1: and ensure slug isn't a preset
190 - while (!sh_cd_is_slug_unique($text))
191 - {
192 - $text = $original_slug . '_' . $try;
277 + if ( true === isset( $_POST[ $key ] ) ) {
278 + $data[ $key ] = $_POST[ $key ];
279 + } else {
280 + $data[ $key ] = '';
281 + }
193 282
194 - $try++;
195 - }
196 - }
283 + }
197 284
198 - return $text;
285 + return $data;
199 286 }
200 287
201 -function sh_cd_is_slug_unique($slug)
202 -{
203 - if (!is_admin() || empty($slug))
204 - return false;
288 +/**
289 + * Toggle the status of a shortcode
290 + *
291 + * @param $id
292 + */
293 +function sh_cd_toggle_status( $id ) {
205 294
206 - // 1.1 Ensure slug is not a prefix
207 - if (sh_cd_is_shortcode_preset($slug))
208 - return false;
295 + $slug = sh_cd_db_shortcodes_by_id( (int) $id );
209 296
210 - global $wpdb;
297 + if ( false === empty( $slug ) ) {
211 298
212 - $sql = $wpdb->prepare('SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug);
299 + $status = ( 1 === (int) $slug['disabled'] ) ? 0 : 1 ;
213 300
214 - $row = $wpdb->get_var( $sql );
301 + sh_cd_db_shortcodes_update_status( $id, $status );
215 302
216 - if(0 == $row)
217 - {
218 - return true;
303 + return $status;
219 304 }
220 - else
221 - {
222 - return false;
223 - }
224 305
225 -
306 + return NULL;
226 307 }
227 308
228 -function sh_cd_create_database_table()
229 -{
230 - global $wpdb;
309 +/**
310 + * Toggle the multisite of a shortcode
311 + *
312 + * @param $id
313 + */
314 +function sh_cd_toggle_multisite( $id ) {
231 315
232 - $table_name = $wpdb->prefix . SH_CD_TABLE;
316 + $slug = sh_cd_db_shortcodes_by_id( (int) $id );
233 317
234 - $charset_collate = $wpdb->get_charset_collate();
318 + if ( false === empty( $slug ) ) {
235 319
236 - $sql = "CREATE TABLE $table_name (
237 - id mediumint(9) NOT NULL AUTO_INCREMENT,
238 - slug varchar(100) NOT NULL,
239 - data text,
240 - disabled bit default 0,
241 - UNIQUE KEY id (id)
242 - ) $charset_collate;";
320 + $multisite = ( 1 === (int) $slug['multisite'] ) ? 0 : 1 ;
243 321
244 - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
245 - dbDelta( $sql );
322 + sh_cd_db_shortcodes_update_multisite( $id, $multisite );
246 323
247 - update_option('sh-cd-version', SH_CD_PLUGIN_VERSION);
324 + return $multisite;
325 + }
248 326
327 + return NULL;
249 328 }
250 329
251 -function sh_cd_display_message($text, $error = false)
252 -{
253 - if (!empty($text))
254 - {
255 - $class = (($error) ? 'error' : 'updated');
256 330
257 - echo '<div class="' . $class . '">
258 - <p>' . $text . '</p>
259 - </div>';
260 - }
261 -}
331 +/**
332 + * Display a table of premade shortcodes
333 + *
334 + * @param string $display
335 + */
336 +function sh_cd_display_premade_shortcodes( $display = 'all' ) {
262 337
263 -function sh_cd_create_dialog_jquery_code($title, $message, $class_used_to_prompt_confirmation, $js_call = false)
264 -{
265 - global $wp_scripts;
266 - $queryui = $wp_scripts->query('jquery-ui-core');
267 - $url = "//ajax.googleapis.com/ajax/libs/jqueryui/".$queryui->ver."/themes/smoothness/jquery-ui.css";
268 - wp_enqueue_script( 'jquery-ui-dialog' );
269 - wp_enqueue_style('jquery-ui-smoothness', $url, false, null);
338 + $premium_user = sh_cd_license_is_premium();
339 + $upgrade_link = sprintf( '<a class="button" href="%s"><i class="fas fa-check"></i> Upgrade now</a>', sh_cd_license_upgrade_link() );
270 340
271 - $id_hash = md5($title . $message . $class_used_to_prompt_confirmation);
341 + switch ( $display ) {
342 + case 'free':
343 + $shortcodes = sh_cd_shortcode_presets_free_list();
344 + $show_premium_col = false;
345 + break;
346 + case 'premium':
347 + $shortcodes = sh_cd_shortcode_presets_premium_list();
348 + $show_premium_col = false;
349 + break;
350 + default:
351 + $shortcodes = sh_cd_presets_both_lists();
352 + $show_premium_col = true;
353 + }
272 354
273 - ?>
274 - <div id='<?php echo $id_hash; ?>' title='<?php echo $title; ?>'>
275 - <p><?php echo $message; ?></p>
276 - </div>
277 - <script>
278 - jQuery(function($) {
355 + $html = '<table class="widefat sh-cd-table" width="100%">
356 + <tr class="row-title">
357 + <th class="row-title" width="30%">Shortcode</th>';
279 358
280 - var $info = $('#<?php echo $id_hash; ?>');
359 + if ( true === $show_premium_col) {
360 + $html .= '<th class="row-title">Premium</th>';
361 + }
281 362
282 - $info.dialog({
283 - 'dialogClass' : 'wp-dialog',
284 - 'modal' : true,
285 - 'autoOpen' : false
286 - });
363 + $html .= '<th width="*">Description</th>
364 + </tr>';
287 365
288 - $('.<?php echo $class_used_to_prompt_confirmation; ?>').click(function(event) {
366 + $class = '';
289 367
290 - event.preventDefault();
368 + foreach ( $shortcodes as $key => $data ) {
291 369
292 - target_url = $(this).attr('href');
370 + $class = ($class == 'alternate') ? '' : 'alternate';
293 371
294 - var $info = $('#<?php echo $id_hash; ?>');
372 + $shortcode = '[' . SH_CD_SHORTCODE. ' slug="' . $key . '"]';
295 373
296 - $info.dialog({
297 - 'dialogClass' : 'wp-dialog',
298 - 'modal' : true,
299 - 'autoOpen' : false,
300 - 'closeOnEscape' : true,
301 - 'buttons' : {
302 - 'Yes': function() {
374 + $premium_shortcode = ( true === $data['premium'] );
303 375
304 - <?php if ($js_call != false): ?>
305 - <?php echo $js_call; ?>
376 + $html .= sprintf( '<tr class="%s"><td>%s</td>', $class, esc_html( $shortcode ) );
306 377
307 - $(this).dialog('close');
308 - <?php else: ?>
309 - window.location.href = target_url;
310 - <?php endif; ?>
311 - },
312 - 'No': function() {
313 - $(this).dialog('close');
314 - }
315 - }
316 - });
317 378
318 - $info.dialog('open');
319 - });
379 + if ( true === $show_premium_col) {
320 380
321 - });
381 + $html .= sprintf( '<td align="middle">%s%s</td>',
382 + ( true === $premium_shortcode && true === $premium_user ) ? '<i class="fas fa-check"></i>' : '',
383 + ( true == $premium_shortcode && false === $premium_user ) ? $upgrade_link : ''
384 + );
385 + }
322 386
387 + $html .= sprintf( '<td>%s</td></tr>', wp_kses_post( $data['description'] ) );
323 388
389 + }
324 390
391 + $html .= '</table>';
325 392
326 - </script>
393 + return $html;
394 +}
327 395
328 - <?php
396 +/**
397 + * Display an upgrade button
398 + */
399 +function sh_cd_upgrade_button( $css_class = '', $link = NULL ) {
400 +
401 + $link = ( false === empty( $link ) ) ? $link : SH_CD_UPGRADE_LINK . '?hash=' . sh_cd_generate_site_hash() ;
402 +
403 + echo sprintf('<a href="%s" class="button-primary sh-cd-upgrade-button%s"><i class="far fa-credit-card"></i> %s £%d %s</a>',
404 + esc_url( $link ),
405 + esc_attr( ' ' . $css_class ),
406 + __('Upgrade to Premium for ', SH_CD_SLUG),
407 + SH_CD_PREMIUM_PRICE,
408 + __('a year ', SH_CD_SLUG)
409 + );
329 410 }
330 411
331 -function sh_cd_get_cache($key)
332 -{
333 - $key = sh_cd_generate_cache_key($key);
412 +/**
413 + * Is multsite functionality active for this install?
414 + *
415 + * @return bool
416 + */
417 +function sh_cd_is_multisite_enabled() {
334 418
335 - return get_transient($key);
419 + if ( false === is_multisite() ) {
420 + return false;
421 + }
422 +
423 + if ( false === sh_cd_license_is_premium() ) {
424 + return false;
425 + }
426 +
427 + return true;
336 428 }
337 -function sh_cd_set_cache($key, $data)
338 -{
339 - $key = sh_cd_generate_cache_key($key);
340 429
341 - set_transient($key, $data, SH_CD_CACHING_TIME);
342 -}
343 -function sh_cd_delete_cache($key)
344 -{
345 - $key = sh_cd_generate_cache_key($key);
430 +/**
431 + * Fetch all multisite slugs
432 + *
433 + * @return array|null
434 + */
435 +function sh_cd_multisite_slugs() {
346 436
347 - return delete_transient($key);
348 -}
349 -function sh_cd_generate_cache_key($key)
350 -{
351 - return SH_CD_SHORTCODE . $key;
352 -}
437 + if ( false === is_multisite() ) {
438 + return [];
439 + }
440 +
441 + $cache = sh_cd_cache_get( 'sh-cd-multisite-slugs' );
442 +
443 + if ( false !== $cache ) {
444 + return $cache;
445 + }
446 +
447 + $slugs = sh_cd_db_shortcodes_multisite_slugs();
448 +
449 + $slugs = ( false === empty( $slugs ) ) ? wp_list_pluck( $slugs, 'slug' ) : NULL;
450 +
451 + // Cache this for a short time
452 + sh_cd_cache_set( 'sh-cd-multisite-slugs', $slugs, 30 );
453 +
454 + return ( true === is_array( $slugs ) ) ? $slugs : [];
455 +}