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 +298 -225 1.12.2.1 View file →
@@ -1,305 +1,378 @@
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' ] );
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 ) {
77 139
78 - $result = false;
140 + $key = sh_cd_cache_generate_key( $key );
79 141
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 - );
142 + set_transient( $key, $data, 1 * HOUR_IN_SECONDS );
143 +}
94 144
95 - sh_cd_delete_cache(sh_cd_get_slug_by_id($id));
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 ) {
151 +
152 + if ( true === is_numeric( $slug_or_key ) ) {
153 +
154 + sh_cd_cache_delete( sh_cd_db_shortcodes_get_slug_by_id( $slug_or_key ) );
155 +
156 + } else {
157 + sh_cd_cache_delete( $slug_or_key );
96 158 }
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 - );
159 +}
110 160
111 - sh_cd_delete_cache($slug);
112 - }
161 +/**
162 + * Delete cache item
163 + *
164 + * @param $key
165 + *
166 + * @return mixed
167 + */
168 +function sh_cd_cache_delete( $key ) {
113 169
114 - if (false === $result) {
115 - return false;
116 - }
117 - else
118 - {
119 - return true;
120 - }
121 -
170 + $key = sh_cd_cache_generate_key( $key );
171 +
172 + return delete_transient( $key );
122 173 }
123 174
124 -function sh_cd_delete_shortcode($id)
125 -{
126 - if (!is_admin() || !is_numeric($id))
127 - 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 +}
128 185
129 - global $wpdb;
186 +/**
187 + * Return link to list own shortcodes
188 + *
189 + * @return mixed
190 + */
191 +function sh_cd_link_your_shortcodes() {
130 192
131 - if (false === $wpdb->delete( $wpdb->prefix . SH_CD_TABLE, array( 'id' => $id ) )) {
132 - return false;
133 - }
134 - else {
135 - return true;
136 - }
137 -
193 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes');
194 +
195 + return esc_url( $link );
138 196 }
139 197
140 -function sh_cd_get_slug($text)
141 -{
142 - if(!empty($text))
143 - {
144 - $text = sanitize_title($text);
198 +/**
199 + * Return link to add own shortcode
200 + *
201 + * @return mixed
202 + */
203 +function sh_cd_link_your_shortcodes_add() {
145 204
146 - $original_slug = $text;
205 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=add');
147 206
148 - $try = 1;
207 + return esc_url( $link );
208 +}
149 209
150 - // If slug exists, then fetch a unique one!
151 - while (!sh_cd_is_slug_unique($text))
152 - {
153 - $text = $original_slug . '_' . $try;
154 -
155 - $try++;
156 - }
157 - }
210 +/**
211 + * Return link to edit own shortcode
212 + *
213 + * @return mixed
214 + */
215 +function sh_cd_link_your_shortcodes_edit( $id ) {
158 216
159 - return $text;
217 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=edit&id=' . (int) $id );
218 +
219 + return esc_url( $link );
160 220 }
161 221
162 -function sh_cd_is_slug_unique($slug)
163 -{
164 - if (!is_admin() || empty($slug))
165 - return false;
222 +/**
223 + * Return link to delete own shortcode
224 + *
225 + * @return mixed
226 + */
227 +function sh_cd_link_your_shortcodes_delete( $id ) {
166 228
167 - global $wpdb;
229 + $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id );
168 230
169 - $sql = $wpdb->prepare('SELECT count(slug) FROM ' . $wpdb->prefix . SH_CD_TABLE . ' where slug = %s', $slug);
231 + return esc_url( $link );
232 +}
170 233
171 - $row = $wpdb->get_var( $sql );
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 ) {
172 242
173 - if(0 == $row)
174 - {
175 - return true;
176 - }
177 - else
178 - {
179 - return false;
180 - }
243 + if ( true === isset( $_POST[ $key ] ) ) {
244 + return $_POST[ $key ];
245 + }
181 246
247 + if ( true === isset( $object[ $key ] ) ) {
248 + return $object[ $key ];
249 + }
182 250
251 + return '';
183 252 }
184 253
185 -function sh_cd_create_database_table()
186 -{
187 - global $wpdb;
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 ) {
188 261
189 - $table_name = $wpdb->prefix . SH_CD_TABLE;
190 -
191 - $charset_collate = $wpdb->get_charset_collate();
262 + $data = [];
192 263
193 - $sql = "CREATE TABLE $table_name (
194 - id mediumint(9) NOT NULL AUTO_INCREMENT,
195 - slug varchar(100) NOT NULL,
196 - data text,
197 - UNIQUE KEY id (id)
198 - ) $charset_collate;";
264 + foreach ( $keys as $key ) {
199 265
200 - $wpdb->query($sql);
266 + if ( true === isset( $_POST[ $key ] ) ) {
267 + $data[ $key ] = $_POST[ $key ];
268 + } else {
269 + $data[ $key ] = '';
270 + }
201 271
272 + }
273 +
274 + return $data;
202 275 }
203 276
204 -function sh_cd_display_message($text, $error = false)
205 -{
206 - if (!empty($text))
207 - {
208 - $class = (($error) ? 'error' : 'updated');
277 +/**
278 + * Toggle the status of a shortcode
279 + *
280 + * @param $id
281 + */
282 +function sh_cd_toggle_status( $id ) {
209 283
210 - echo '<div class="' . $class . '">
211 - <p>' . $text . '</p>
212 - </div>';
284 + $slug = sh_cd_db_shortcodes_by_id( (int) $id );
285 +
286 + if ( false === empty( $slug ) ) {
287 +
288 + $status = ( 1 === (int) $slug['disabled'] ) ? 0 : 1 ;
289 +
290 + sh_cd_db_shortcodes_update_status( $id, $status );
291 +
292 + return $status;
213 293 }
294 +
295 + return NULL;
214 296 }
215 297
216 -function sh_cd_create_dialog_jquery_code($title, $message, $class_used_to_prompt_confirmation, $js_call = false)
217 -{
218 - global $wp_scripts;
219 - $queryui = $wp_scripts->query('jquery-ui-core');
220 - $url = "//ajax.googleapis.com/ajax/libs/jqueryui/".$queryui->ver."/themes/smoothness/jquery-ui.css";
221 - wp_enqueue_script( 'jquery-ui-dialog' );
222 - wp_enqueue_style('jquery-ui-smoothness', $url, false, null);
223 298
224 - $id_hash = md5($title . $message . $class_used_to_prompt_confirmation);
299 +/**
300 + * Display a table of premade shortcodes
301 + *
302 + * @param string $display
303 + */
304 +function sh_cd_display_premade_shortcodes( $display = 'all' ) {
225 305
226 - ?>
227 - <div id='<?php echo $id_hash; ?>' title='<?php echo $title; ?>'>
228 - <p><?php echo $message; ?></p>
229 - </div>
230 - <script>
231 - jQuery(function($) {
232 -
233 - var $info = $('#<?php echo $id_hash; ?>');
234 -
235 - $info.dialog({
236 - 'dialogClass' : 'wp-dialog',
237 - 'modal' : true,
238 - 'autoOpen' : false
239 - });
240 -
241 - $('.<?php echo $class_used_to_prompt_confirmation; ?>').click(function(event) {
242 -
243 - event.preventDefault();
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() );
244 308
245 - target_url = $(this).attr('href');
246 -
247 - var $info = $('#<?php echo $id_hash; ?>');
248 -
249 - $info.dialog({
250 - 'dialogClass' : 'wp-dialog',
251 - 'modal' : true,
252 - 'autoOpen' : false,
253 - 'closeOnEscape' : true,
254 - 'buttons' : {
255 - 'Yes': function() {
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 + }
256 322
257 - <?php if ($js_call != false): ?>
258 - <?php echo $js_call; ?>
323 + $html = '<table class="widefat sh-cd-table" width="100%">
324 + <tr class="row-title">
325 + <th class="row-title" width="30%">Shortcode</th>';
259 326
260 - $(this).dialog('close');
261 - <?php else: ?>
262 - window.location.href = target_url;
263 - <?php endif; ?>
264 - },
265 - 'No': function() {
266 - $(this).dialog('close');
267 - }
268 - }
269 - });
327 + if ( true === $show_premium_col) {
328 + $html .= '<th class="row-title">Premium</th>';
329 + }
270 330
271 - $info.dialog('open');
272 - });
331 + $html .= '<th width="*">Description</th>
332 + </tr>';
273 333
274 - });
334 + $class = '';
275 335
336 + foreach ( $shortcodes as $key => $data ) {
276 337
338 + $class = ($class == 'alternate') ? '' : 'alternate';
277 339
340 + $shortcode = '[' . SH_CD_SHORTCODE. ' slug="' . $key . '"]';
278 341
279 - </script>
342 + $premium_shortcode = ( true === $data['premium'] );
280 343
281 - <?php
282 -}
344 + $html .= sprintf( '<tr class="%s"><td>%s</td>', $class, esc_html( $shortcode ) );
283 345
284 -function sh_cd_get_cache($key)
285 -{
286 - $key = sh_cd_generate_cache_key($key);
287 346
288 - return get_transient($key);
347 + if ( true === $show_premium_col) {
348 +
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 + }
354 +
355 + $html .= sprintf( '<td>%s</td></tr>', esc_html( $data['description'] ) );
356 +
357 + }
358 +
359 + $html .= '</table>';
360 +
361 + return $html;
289 362 }
290 -function sh_cd_set_cache($key, $data)
291 -{
292 - $key = sh_cd_generate_cache_key($key);
293 363
294 - set_transient($key, $data, SH_CD_CACHING_TIME);
295 -}
296 -function sh_cd_delete_cache($key)
297 -{
298 - $key = sh_cd_generate_cache_key($key);
364 +/**
365 + * Display an upgrade button
366 + */
367 +function sh_cd_upgrade_button( $css_class = '', $link = NULL ) {
299 368
300 - return delete_transient($key);
301 -}
302 -function sh_cd_generate_cache_key($key)
303 -{
304 - return SH_CD_SHORTCODE . $key;
369 + $link = ( false === empty( $link ) ) ? $link : SH_CD_UPGRADE_LINK . '?hash=' . sh_cd_generate_site_hash() ;
370 +
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 + );
305 378 }