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