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