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 -249 1.82.2.1 View file →
@@ -1,352 +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, disabled 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 and disabled <> 1', $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;
115 + //TODO: Hook this to use admin_notices
116 +}
78 117
79 -}
80 -function sh_cd_get_slug_by_id($id)
81 -{
82 - global $wpdb;
118 +/**
119 + * Fetch cache item
120 + *
121 + * @param $key
122 + *
123 + * @return mixed
124 + */
125 +function sh_cd_cache_get( $key ) {
83 126
84 - $sql = $wpdb->prepare('SELECT slug FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where id = %d', $id);
127 + $key = sh_cd_cache_generate_key( $key );
85 128
86 - $row = $wpdb->get_var( $sql );
129 + return get_transient( $key );
130 +}
87 131
88 - if (!is_null($row))
89 - return $row;
132 +/**
133 + * Set cache item
134 + *
135 + * @param $key
136 + * @param $data
137 + */
138 +function sh_cd_cache_set( $key, $data ) {
90 139
91 - return false;
140 + $key = sh_cd_cache_generate_key( $key );
92 141
142 + set_transient( $key, $data, 1 * HOUR_IN_SECONDS );
93 143 }
94 144
95 -function sh_cd_save_shortcode($slug, $data, $disabled = false, $id = false)
96 -{
97 - if (!is_admin())
98 - return false;
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 ) {
99 151
100 - if (false == $id && empty($slug))
101 - return false;
152 + if ( true === is_numeric( $slug_or_key ) ) {
102 153
103 - global $wpdb;
154 + sh_cd_cache_delete( sh_cd_db_shortcodes_get_slug_by_id( $slug_or_key ) );
104 155
105 - $slug = sh_cd_get_slug($slug);
156 + } else {
157 + sh_cd_cache_delete( $slug_or_key );
158 + }
159 +}
106 160
107 - $result = false;
161 +/**
162 + * Delete cache item
163 + *
164 + * @param $key
165 + *
166 + * @return mixed
167 + */
168 +function sh_cd_cache_delete( $key ) {
108 169
109 - $disabled = (true === $disabled) ? 1: 0;
170 + $key = sh_cd_cache_generate_key( $key );
110 171
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 - );
172 + return delete_transient( $key );
173 +}
127 174
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 - );
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 +}
145 185
146 - sh_cd_delete_cache($slug);
147 - }
186 +/**
187 + * Return link to list own shortcodes
188 + *
189 + * @return mixed
190 + */
191 +function sh_cd_link_your_shortcodes() {
148 192
149 - if (false === $result) {
150 - return false;
151 - }
152 - else
153 - {
154 - return true;
155 - }
193 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes');
156 194
195 + return esc_url( $link );
157 196 }
158 197
159 -function sh_cd_delete_shortcode($id)
160 -{
161 - if (!is_admin() || !is_numeric($id))
162 - return false;
198 +/**
199 + * Return link to add own shortcode
200 + *
201 + * @return mixed
202 + */
203 +function sh_cd_link_your_shortcodes_add() {
163 204
164 - global $wpdb;
205 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=add');
165 206
166 - // Clear cached version
167 - sh_cd_delete_cache(sh_cd_get_slug_by_id($id));
207 + return esc_url( $link );
208 +}
168 209
169 - if (false === $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, array( 'id' => $id ) )) {
170 - return false;
171 - }
172 - else {
173 - return true;
174 - }
210 +/**
211 + * Return link to edit own shortcode
212 + *
213 + * @return mixed
214 + */
215 +function sh_cd_link_your_shortcodes_edit( $id ) {
175 216
217 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=edit&id=' . (int) $id );
218 +
219 + return esc_url( $link );
176 220 }
177 221
178 -function sh_cd_get_slug($text)
179 -{
180 - if(!empty($text))
181 - {
182 - $text = sanitize_title($text);
222 +/**
223 + * Return link to delete own shortcode
224 + *
225 + * @return mixed
226 + */
227 +function sh_cd_link_your_shortcodes_delete( $id ) {
183 228
184 - $original_slug = $text;
229 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id );
185 230
186 - $try = 1;
231 + return esc_url( $link );
232 +}
187 233
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;
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 ) {
193 242
194 - $try++;
195 - }
196 - }
243 + if ( true === isset( $_POST[ $key ] ) ) {
244 + return $_POST[ $key ];
245 + }
197 246
198 - return $text;
247 + if ( true === isset( $object[ $key ] ) ) {
248 + return $object[ $key ];
249 + }
250 +
251 + return '';
199 252 }
200 253
201 -function sh_cd_is_slug_unique($slug)
202 -{
203 - if (!is_admin() || empty($slug))
204 - return false;
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 ) {
205 261
206 - // 1.1 Ensure slug is not a prefix
207 - if (sh_cd_is_shortcode_preset($slug))
208 - return false;
262 + $data = [];
209 263
210 - global $wpdb;
264 + foreach ( $keys as $key ) {
211 265
212 - $sql = $wpdb->prepare('SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug);
266 + if ( true === isset( $_POST[ $key ] ) ) {
267 + $data[ $key ] = $_POST[ $key ];
268 + } else {
269 + $data[ $key ] = '';
270 + }
213 271
214 - $row = $wpdb->get_var( $sql );
272 + }
215 273
216 - if(0 == $row)
217 - {
218 - return true;
219 - }
220 - else
221 - {
222 - return false;
223 - }
224 -
225 -
274 + return $data;
226 275 }
227 276
228 -function sh_cd_create_database_table()
229 -{
230 - global $wpdb;
277 +/**
278 + * Toggle the status of a shortcode
279 + *
280 + * @param $id
281 + */
282 +function sh_cd_toggle_status( $id ) {
231 283
232 - $table_name = $wpdb->prefix . SH_CD_TABLE;
284 + $slug = sh_cd_db_shortcodes_by_id( (int) $id );
233 285
234 - $charset_collate = $wpdb->get_charset_collate();
286 + if ( false === empty( $slug ) ) {
235 287
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;";
288 + $status = ( 1 === (int) $slug['disabled'] ) ? 0 : 1 ;
243 289
244 - require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
245 - dbDelta( $sql );
290 + sh_cd_db_shortcodes_update_status( $id, $status );
246 291
247 - update_option('sh-cd-version', SH_CD_PLUGIN_VERSION);
292 + return $status;
293 + }
248 294
295 + return NULL;
249 296 }
250 297
251 -function sh_cd_display_message($text, $error = false)
252 -{
253 - if (!empty($text))
254 - {
255 - $class = (($error) ? 'error' : 'updated');
256 298
257 - echo '<div class="' . $class . '">
258 - <p>' . $text . '</p>
259 - </div>';
260 - }
261 -}
299 +/**
300 + * Display a table of premade shortcodes
301 + *
302 + * @param string $display
303 + */
304 +function sh_cd_display_premade_shortcodes( $display = 'all' ) {
262 305
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);
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() );
270 308
271 - $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 + }
272 322
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($) {
323 + $html = '<table class="widefat sh-cd-table" width="100%">
324 + <tr class="row-title">
325 + <th class="row-title" width="30%">Shortcode</th>';
279 326
280 - var $info = $('#<?php echo $id_hash; ?>');
327 + if ( true === $show_premium_col) {
328 + $html .= '<th class="row-title">Premium</th>';
329 + }
281 330
282 - $info.dialog({
283 - 'dialogClass' : 'wp-dialog',
284 - 'modal' : true,
285 - 'autoOpen' : false
286 - });
331 + $html .= '<th width="*">Description</th>
332 + </tr>';
287 333
288 - $('.<?php echo $class_used_to_prompt_confirmation; ?>').click(function(event) {
334 + $class = '';
289 335
290 - event.preventDefault();
336 + foreach ( $shortcodes as $key => $data ) {
291 337
292 - target_url = $(this).attr('href');
338 + $class = ($class == 'alternate') ? '' : 'alternate';
293 339
294 - var $info = $('#<?php echo $id_hash; ?>');
340 + $shortcode = '[' . SH_CD_SHORTCODE. ' slug="' . $key . '"]';
295 341
296 - $info.dialog({
297 - 'dialogClass' : 'wp-dialog',
298 - 'modal' : true,
299 - 'autoOpen' : false,
300 - 'closeOnEscape' : true,
301 - 'buttons' : {
302 - 'Yes': function() {
342 + $premium_shortcode = ( true === $data['premium'] );
303 343
304 - <?php if ($js_call != false): ?>
305 - <?php echo $js_call; ?>
344 + $html .= sprintf( '<tr class="%s"><td>%s</td>', $class, esc_html( $shortcode ) );
306 345
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 346
318 - $info.dialog('open');
319 - });
347 + if ( true === $show_premium_col) {
320 348
321 - });
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 + }
322 354
355 + $html .= sprintf( '<td>%s</td></tr>', esc_html( $data['description'] ) );
323 356
357 + }
324 358
359 + $html .= '</table>';
325 360
326 - </script>
327 -
328 - <?php
361 + return $html;
329 362 }
330 363
331 -function sh_cd_get_cache($key)
332 -{
333 - $key = sh_cd_generate_cache_key($key);
364 +/**
365 + * Display an upgrade button
366 + */
367 +function sh_cd_upgrade_button( $css_class = '', $link = NULL ) {
334 368
335 - return get_transient($key);
336 -}
337 -function sh_cd_set_cache($key, $data)
338 -{
339 - $key = sh_cd_generate_cache_key($key);
369 + $link = ( false === empty( $link ) ) ? $link : SH_CD_UPGRADE_LINK . '?hash=' . sh_cd_generate_site_hash() ;
340 370
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);
346 -
347 - return delete_transient($key);
348 -}
349 -function sh_cd_generate_cache_key($key)
350 -{
351 - return SH_CD_SHORTCODE . $key;
352 -}
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 +}