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