PluginProbe
Snippet Shortcodes / 3.3.3
Snippet Shortcodes v3.3.3
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
shortcode-variables / includes / functions.php

functions.php in Snippet Shortcodes 3.3.3, at includes/functions.php

459 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 defined('ABSPATH') or die('Jog on!');
4
5 /**
6 * Save / Insert a shortcode
7 *
8 * @return bool
9 */
10 function sh_cd_shortcodes_save_post() {
11
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' ] );
14
15 return sh_cd_db_shortcodes_save( $shortcode );
16 }
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;
38 }
39
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 ) {
48
49 if ( true === empty( $slug ) ) {
50 return NULL;
51 }
52
53 $slug = sanitize_title( $slug );
54
55 $original_slug = $slug;
56
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;
68 }
69
70 /**
71 * Clone an existing shortcode!
72 *
73 * @param $id
74 *
75 * @return bool
76 */
77 function sh_cd_clone( $id ) {
78
79 if( false === sh_cd_license_is_premium() ) {
80 return true;
81 }
82
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 );
96 }
97
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 ) {
105
106 if ( true === empty( $text ) ) {
107 return;
108 }
109
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
116 }
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 );
130 }
131
132 /**
133 * Set cache item
134 *
135 * @param $key
136 * @param $data
137 */
138 function sh_cd_cache_set( $key, $data, $expire = NULL ) {
139
140
141 $expire = ( false === empty( $expire ) ) ? (int) $expire : 1 * HOUR_IN_SECONDS;
142
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 );
163 }
164
165 // Delete site option
166 $slug_or_key = SH_CD_PREFIX . $slug_or_key;
167
168 delete_site_option( $slug_or_key );
169
170 }
171
172 /**
173 * Delete cache item
174 *
175 * @param $key
176 *
177 * @return mixed
178 */
179 function sh_cd_cache_delete( $key ) {
180
181 $key = sh_cd_cache_generate_key( $key );
182
183 return delete_transient( $key );
184 }
185
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 }
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 );
207 }
208
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 );
219 }
220
221 /**
222 * Return link to edit own shortcode
223 *
224 * @return mixed
225 */
226 function sh_cd_link_your_shortcodes_edit( $id ) {
227
228 $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=edit&id=' . (int) $id );
229
230 return esc_url( $link );
231 }
232
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 ) {
240
241 $link = admin_url('admin.php?page=sh-cd-shortcode-variables-your-shortcodes&action=delete&id=' . (int) $id );
242
243 return esc_url( $link );
244 }
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 ) {
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 '';
264 }
265
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 ) {
273
274 $data = [];
275
276 foreach ( $keys as $key ) {
277
278 if ( true === isset( $_POST[ $key ] ) ) {
279 $data[ $key ] = $_POST[ $key ];
280 } else {
281 $data[ $key ] = '';
282 }
283
284 }
285
286 return $data;
287 }
288
289 /**
290 * Toggle the status of a shortcode
291 *
292 * @param $id
293 */
294 function sh_cd_toggle_status( $id ) {
295
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;
305 }
306
307 return NULL;
308 }
309
310 /**
311 * Toggle the multisite of a shortcode
312 *
313 * @param $id
314 * @return int|null
315 */
316 function sh_cd_toggle_multisite( $id ) {
317
318 $slug = sh_cd_db_shortcodes_by_id( (int) $id );
319
320 if ( false === empty( $slug ) ) {
321
322 $multisite = ( 1 === (int) $slug['multisite'] ) ? 0 : 1 ;
323
324 sh_cd_db_shortcodes_update_multisite( $id, $multisite );
325
326 return $multisite;
327 }
328
329 return NULL;
330 }
331
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' ) {
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 ) );
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 }
357
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 ) );
361
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;
397 }
398
399 /**
400 * Display an upgrade button
401 */
402 function sh_cd_upgrade_button( $css_class = '', $link = NULL ) {
403
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 );
413 }
414
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;
431 }
432
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 : [];
458 }
459