PluginProbe ʕ •ᴥ•ʔ
Strong Testimonials / 2.36
Strong Testimonials v2.36
3.3.1 trunk 1.0.1 2.30.9 2.31.10 2.32 2.32.1 2.32.2 2.32.3 2.32.4 2.33 2.34 2.35 2.36 2.37 2.38 2.38.1 2.39 2.39.1 2.39.2 2.39.3 2.40.0 2.40.1 2.40.2 2.40.3 2.40.4 2.40.5 2.40.6 2.40.7 2.41.0 2.41.1 2.50.0 2.50.1 2.50.2 2.50.3 2.50.4 2.51.0 2.51.1 2.51.2 2.51.3 2.51.4 2.51.5 2.51.6 2.51.7 2.51.8 2.51.9 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.17 3.2.18 3.2.19 3.2.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0
strong-testimonials / includes / functions.php
strong-testimonials / includes Last commit date
integrations 7 years ago class-strong-form.php 7 years ago class-strong-log.php 7 years ago class-strong-mail.php 7 years ago class-strong-templates.php 7 years ago class-strong-testimonials-order.php 7 years ago class-strong-testimonials-privacy.php 7 years ago class-strong-testimonials-render.php 7 years ago class-strong-testimonials-shortcode-average.php 7 years ago class-strong-testimonials-shortcode-count.php 7 years ago class-strong-testimonials-shortcode.php 7 years ago class-strong-view-display.php 7 years ago class-strong-view-form.php 7 years ago class-strong-view-slideshow.php 7 years ago class-strong-view.php 7 years ago class-walker-strong-category-checklist-front.php 7 years ago deprecated.php 7 years ago filters.php 7 years ago functions-activation.php 7 years ago functions-content.php 7 years ago functions-image.php 7 years ago functions-rating.php 7 years ago functions-template-form.php 7 years ago functions-template.php 7 years ago functions-views.php 7 years ago functions.php 7 years ago l10n-polylang.php 7 years ago l10n-wpml.php 7 years ago post-types.php 7 years ago retro.php 7 years ago scripts.php 7 years ago widget2.php 7 years ago
functions.php
864 lines
1 <?php
2 /**
3 * Functions
4 */
5
6 function wpmtst_support_url() {
7 return esc_url( '#' );
8 }
9
10 /**
11 * Return default translation from po/mo files if no active translation plugin.
12 *
13 * @since 2.23.2
14 * @param $string
15 *
16 * @return string
17 */
18 function wpmtst_l10n_default( $string ) {
19 return __( $string, 'strong-testimonials' );
20 }
21 add_filter( 'wpmtst_l10n', 'wpmtst_l10n_default' );
22
23 /**
24 * Append custom fields to post object.
25 * Add thumbnail if included in field group.
26 *
27 * @param $post
28 *
29 * @return mixed
30 */
31 function wpmtst_get_post( $post ) {
32 $custom = get_post_custom( $post->ID );
33 $fields = wpmtst_get_custom_fields();
34
35 foreach ( $fields as $key => $field ) {
36 $name = $field['name'];
37
38 if ( 'featured_image' == $name ) {
39 $post->thumbnail_id = get_post_thumbnail_id( $post->ID );
40 }
41 else {
42 if ( isset( $custom[ $name ] ) ) {
43 $post->$name = $custom[ $name ][0];
44 }
45 else {
46 $post->$name = '';
47 }
48 }
49 }
50
51 return $post;
52 }
53
54 /**
55 * Helper: Format URL
56 *
57 * @param $url
58 * @return string
59 */
60 function wpmtst_get_website( $url ) {
61 if ( !preg_match( "~^(?:f|ht)tps?://~i", $url ) )
62 $url = 'https://' . $url;
63
64 return $url;
65 }
66
67 /**
68 * Check whether a common script is already registered by file name
69 * instead of handle.
70 *
71 * === Used in older versions to check for Cycle script. ===
72 *
73 * Why? Plugins are loaded before themes. Our plugin includes the Cycle
74 * slider. Some themes include it too. We only want to load it once.
75 *
76 * Load jQuery Cycle plugin (http://jquery.malsup.com/cycle/) only if
77 * any version of Cycle is not already registered by a theme or another
78 * plugin. Both versions of Cycle use same function name so we can't load
79 * both but either version will work for our purposes.
80 * http://jquery.malsup.com/cycle2/faq/
81 *
82 * The WordPress function `wp_script_is` checks by *handle* within a plugin
83 * or theme but handles can be different so it misses it.
84 * wp_script_is( 'jquery-cycle', 'registered' )
85 * http://codex.wordpress.org/Function_Reference/wp_script_is
86 *
87 * Jetpack's slideshow shortcode simply enqueues its own version of Cycle
88 * without registering first if and when the shortcode is rendered. No way
89 * to check for that. It does not seem to create a conflict now. (1.16)
90 *
91 * @param array $filenames possible versions of one script,
92 * e.g. plugin.js, plugin-min.js, plugin-1.2.js
93 * @return string
94 */
95 function wpmtst_is_registered( $filenames ) {
96 global $wp_scripts;
97
98 // Bail if called too early.
99 if ( ! $wp_scripts ) return false;
100
101 $script_handle = '';
102
103 foreach ( $wp_scripts->registered as $handle => $script ) {
104 if ( in_array( basename( $script->src ), $filenames ) ) {
105 $script_handle = $handle;
106 break;
107 }
108 }
109
110 return $script_handle;
111 }
112
113 if ( ! function_exists( 'get_page_by_slug' ) ) {
114 /**
115 * Get page ID by slug.
116 *
117 * Thanks http://wordpress.stackexchange.com/a/102845/32076
118 * Does not require parent slug.
119 *
120 * @since 1.11.0
121 */
122 function get_page_by_slug( $page_slug, $output = OBJECT, $post_type = 'page' ) {
123 global $wpdb;
124 $page = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_name = %s AND post_type= %s AND post_status = 'publish'", $page_slug, $post_type ) );
125 if ( $page )
126 return get_post($page, $output);
127 else
128 return null;
129 }
130 }
131
132 /**
133 * Reverse auto-p wrap shortcodes that stand alone
134 *
135 * @since 1.11.0
136 */
137 if ( ! function_exists( 'reverse_wpautop' ) ) {
138 function reverse_wpautop( $s ) {
139 // remove any new lines already in there
140 $s = str_replace("\n", "", $s);
141
142 // remove all <p>
143 $s = str_replace("<p>", "", $s);
144
145 // remove <br>
146 $s = str_replace(array("<br />", "<br/>", "<br>"), "", $s);
147
148 // remove </p>
149 $s = str_replace("</p>", "", $s);
150
151 return $s;
152 }
153 }
154
155 /**
156 * Sort array based on 'order' element.
157 *
158 * @since 1.13
159 */
160 function wpmtst_uasort( $a, $b ) {
161 if ( $a['order'] == $b['order'] ) {
162 return 0;
163 }
164 return ( $a['order'] < $b['order'] ) ? -1 : 1;
165 }
166
167 function wpmtst_get_custom_form_count() {
168 $forms = get_option( 'wpmtst_custom_forms' );
169 return count( $forms );
170 }
171
172 function wpmtst_get_form_fields( $form_id = 1 ) {
173 $forms = get_option( 'wpmtst_custom_forms' );
174 if ( isset( $forms[ $form_id ] ) ) {
175 $form = $forms[ $form_id ];
176 } else {
177 $form = $forms[1];
178 }
179 $fields = $form['fields'];
180
181 return $fields;
182 }
183
184 /**
185 * Get only custom fields from all field groups.
186 *
187 * Used in post editor.
188 *
189 * @return array
190 */
191 function wpmtst_get_custom_fields() {
192 $all_fields = array();
193 $forms = get_option( 'wpmtst_custom_forms' );
194 if ( ! $forms ) {
195 return $all_fields;
196 }
197
198 // use default group as base
199 $fields = $forms[1]['fields'];
200 if ( ! $fields ) {
201 return $all_fields;
202 }
203
204 // replace key with field name
205 foreach ( $fields as $field ) {
206 if ( 'post' != $field['record_type'] ) {
207 $all_fields[ $field['name'] ] = $field;
208 }
209 }
210
211 // merge remaining form fields
212 foreach ( $forms as $form ) {
213 $custom_fields = array();
214 $fields = $form['fields'];
215 foreach ( $fields as $field ) {
216 if ( 'post' != $field['record_type'] ) {
217 $custom_fields[ $field['name'] ] = $field;
218 }
219 }
220 $all_fields = array_merge( $all_fields, $custom_fields );
221 }
222
223 return $all_fields;
224 }
225
226 /**
227 * Get all fields from all field groups.
228 *
229 * Used for admin list columns.
230 *
231 * @return array
232 */
233 function wpmtst_get_all_fields() {
234 $forms = get_option( 'wpmtst_custom_forms' );
235 $all_fields = array();
236
237 /**
238 * Use first custom form as the base because if we use 'default'
239 * and a field has 'admin_table' enabled in 'default'
240 * but not in any custom form, the column will still be shown.
241 */
242 $fields = $forms[1]['fields'];
243
244 // replace key with field name
245 foreach ( $fields as $field ) {
246 $all_fields[ $field['name'] ] = $field;
247 }
248
249 // merge remaining form fields
250 foreach ( $forms as $form ) {
251 $custom_fields = array();
252 $fields = $form['fields'];
253 foreach ( $fields as $field ) {
254 $custom_fields[ $field['name'] ] = $field;
255 }
256 $all_fields = array_merge( $all_fields, $custom_fields );
257 }
258
259 return $all_fields;
260 }
261
262 /**
263 * Get the built-in fields.
264 *
265 * @since 2.29.0
266 */
267 function wpmtst_get_builtin_fields() {
268 return array(
269 'post_date' => array(
270 'name' => 'post_date',
271 'label' => 'Post Date',
272 'input_type' => 'date',
273 'type' => 'date',
274 'record_type' => 'builtin',
275 ),
276 'submit_date' => array(
277 'name' => 'submit_date',
278 'label' => 'Submit Date',
279 'input_type' => 'date',
280 'type' => 'date',
281 'record_type' => 'builtin',
282 ),
283 'category' => array(
284 'name' => 'category',
285 'label' => 'Category',
286 'input_type' => 'category',
287 'type' => 'category',
288 'record_type' => 'builtin',
289 ),
290 );
291 }
292
293 /**
294 * Get defined images sizes.
295 *
296 * @link http://codex.wordpress.org/Function_Reference/get_intermediate_image_sizes
297 * @since 1.21.0
298 * @param string $size
299 *
300 * @return array|bool|mixed
301 */
302 /*
303 wpmtst_get_image_sizes = Array
304 (
305 [widget-thumbnail] => Array
306 (
307 [width] => 75
308 [height] => 75
309 [crop] =>
310 [label] => widget-thumbnail - 75 x 75
311 )
312 [thumbnail] => Array
313 (
314 [width] => 150
315 [height] => 150
316 [crop] => 1
317 [label] => thumbnail - 150 x 150
318 )
319 [medium] => Array
320 (
321 [width] => 300
322 [height] => 300
323 [crop] =>
324 [label] => medium - 300 x 300
325 )
326 [post-thumbnail] => Array
327 (
328 [width] => 825
329 [height] => 510
330 [crop] => 1
331 [label] => post-thumbnail - 825 x 510
332 )
333 [large] => Array
334 (
335 [width] => 1024
336 [height] => 1024
337 [crop] =>
338 [label] => large - 1024 x 1024
339 )
340 [full] => Array
341 (
342 [label] => original size uploaded
343 [width] => 0
344 [height] => 0
345 )
346 [custom] => Array
347 (
348 [label] => enter dimensions:
349 [width] => 0
350 [height] => 0
351 )
352 )
353 */
354 function wpmtst_get_image_sizes( $size = '' ) {
355
356 global $_wp_additional_image_sizes;
357
358 $sizes = array();
359 $get_intermediate_image_sizes = get_intermediate_image_sizes();
360
361 /**
362 * Catch possibility of missing standard sizes.
363 * @since 2.2.5
364 */
365 if ( $get_intermediate_image_sizes ) {
366 // Create the full array with sizes and crop info
367 foreach ( $get_intermediate_image_sizes as $_size ) {
368
369 if ( in_array( $_size, array( 'thumbnail', 'medium', 'large' ) ) ) {
370
371 $sizes[ $_size ]['width'] = get_option( $_size . '_size_w' );
372 $sizes[ $_size ]['height'] = get_option( $_size . '_size_h' );
373 $sizes[ $_size ]['crop'] = (bool)get_option( $_size . '_crop' );
374 }
375 elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
376
377 $sizes[ $_size ] = array(
378 'width' => $_wp_additional_image_sizes[ $_size ]['width'],
379 'height' => $_wp_additional_image_sizes[ $_size ]['height'],
380 'crop' => $_wp_additional_image_sizes[ $_size ]['crop']
381 );
382
383 }
384
385 }
386
387 // Sort by width
388 uasort( $sizes, 'wpmtst_compare_width' );
389
390 // Add option labels
391 foreach ( $sizes as $key => $dimensions ) {
392 $sizes[ $key ]['label'] = sprintf( '%s - %d x %d', $key, $dimensions['width'], $dimensions['height'] );
393 }
394 }
395
396 // Add extra options
397 $sizes['full'] = array( 'label' => 'original size uploaded', 'width' => 0, 'height' => 0 );
398 $sizes['custom'] = array( 'label' => 'custom size', 'width' => 0, 'height' => 0 );
399
400 // Get only one size if found
401 if ( $size ) {
402 if ( isset( $sizes[ $size ] ) ) {
403 return $sizes[ $size ];
404 } else {
405 return false;
406 }
407 }
408
409 return $sizes;
410 }
411
412 /**
413 * @param $a
414 * @param $b
415 *
416 * @return int
417 */
418 function wpmtst_compare_width( $a, $b ) {
419 if ( $a['width'] == $b['width'] ) {
420 return 0;
421 }
422 return ($a['width'] < $b['width']) ? -1 : 1;
423 }
424
425 /**
426 * @return int
427 */
428 function wpmtst_get_cat_count() {
429 return count( get_terms( 'wpm-testimonial-category', array( 'hide_empty' => false ) ) );
430 }
431
432 /**
433 * Return a list of categories after removing any orderby filters.
434 *
435 * @since 2.2.3 If WPML is active, will find corresponding term ID in current language.
436 *
437 * @param int $parent
438 *
439 * @return array|int|WP_Error
440 */
441 function wpmtst_get_cats( $parent = 0 ) {
442 return get_terms( 'wpm-testimonial-category', array(
443 'hide_empty' => false,
444 'parent' => $parent,
445 ) );
446 }
447
448 /**
449 * @param $value
450 * @param int $parent
451 * @param int $level
452 */
453 function wpmtst_nested_cats( $value, $parent = 0, $level = 0 ) {
454 $cats = wpmtst_get_cats( $parent );
455 if ( $cats ) {
456 foreach ( $cats as $cat ) {
457 $selected = in_array( $cat->term_id, $value ) ? ' selected' : '';
458 printf( '<option value="%s"%s>%s%s</option>', $cat->term_id, $selected, str_repeat( '&nbsp;&nbsp;&nbsp;', $level ), $cat->name );
459 wpmtst_nested_cats( $value, $cat->term_id, $level + 1 );
460 }
461 }
462 }
463
464 function wpmtst_sort_array_by_name( $a, $b ) {
465 if ( $a['name'] == $b['name'] )
466 return 0;
467
468 return ( $a['name'] < $b['name'] ) ? -1 : 1;
469 }
470
471 /**
472 * Allow disabling of client-side form validation via filter.
473 *
474 * @since 1.21.0
475 * @deprecated since 2.18.1
476 */
477 function wpmtst_using_form_validation_script() {
478 return true;
479 }
480
481 /**
482 * Set iframe width of embedded videos.
483 *
484 * @since 2.6.0
485 * @param $dimensions
486 * @param $url
487 *
488 * @return array
489 */
490 function wpmtst_embed_size( $dimensions, $url ) {
491 $options = get_option( 'wpmtst_options' );
492 $width = (int) $options['embed_width'];
493 if ( $width ) {
494 $dimensions = array(
495 'width' => $width,
496 'height' => min( ceil( $width * 1.5 ), 1000 )
497 );
498 }
499
500 return $dimensions;
501 }
502
503 /**
504 * Allow empty posts.
505 *
506 * @since 2.6.0
507 * @param $maybe_empty
508 * @param $postarr
509 *
510 * @return bool
511 */
512 function wpmtst_insert_post_empty_content( $maybe_empty, $postarr ) {
513 if ( 'wpm-testimonial' == $postarr['post_type'] )
514 return false;
515
516 return $maybe_empty;
517 }
518 add_filter( 'wp_insert_post_empty_content', 'wpmtst_insert_post_empty_content', 10, 2 );
519
520 /**
521 * Display submit_date in Publish meta box under Published date.
522 *
523 * @param $post @since WordPress 4.4
524 * @since 2.12.0
525 */
526 function wpmtst_post_submitbox_misc_actions( $post ) {
527 if ( ! $post ) {
528 global $post;
529 }
530
531 if ( 'wpm-testimonial' == $post->post_type ) {
532 echo '<div class="wpmtst-pub-section">';
533 echo '<span id="submit-timestamp">&nbsp;';
534 $submit_date = get_post_meta( $post->ID, 'submit_date', true );
535 if ( $submit_date ) {
536 echo 'Submitted on: <strong>' . date_i18n( __( 'M j, Y @ H:i' ), strtotime( $submit_date ) ) . '</strong>';
537 } else {
538 echo 'No submit date';
539 }
540 echo '</span>';
541 echo '</div>';
542 }
543 }
544 add_action( 'post_submitbox_misc_actions', 'wpmtst_post_submitbox_misc_actions' );
545
546 /**
547 * @return mixed
548 */
549 function wpmtst_get_background_defaults() {
550 return apply_filters( 'wpmtst_default_template_background', array(
551 'color' => '',
552 'type' => '',
553 'preset' => '',
554 'gradient1' => '',
555 'gradient2' => '',
556 ) );
557 }
558
559 /**
560 * @param null $preset
561 *
562 * TODO Move to options and add a filter.
563 * @return array|bool
564 */
565 function wpmtst_get_background_presets( $preset = null ) {
566 $presets = array(
567 'light-blue-gradient' => array(
568 'label' => __( 'light blue gradient', 'strong-testimonials' ),
569 'color' => '#E7EFFE',
570 'color2' => '#B8CFFB',
571 ),
572 'light-gray-gradient' => array(
573 'label' => __( 'light gray gradient', 'strong-testimonials' ),
574 'color' => '#FBFBFB',
575 'color2' => '#EDEDED',
576 ),
577 'light-green-mist-gradient' => array(
578 'label' => __( 'light green mist gradient', 'strong-testimonials' ),
579 'color' => '#F2FBE9',
580 'color2' => '#E0F7CC',
581 ),
582 'light-latte-gradient' => array(
583 'label' => __( 'light latte gradient', 'strong-testimonials' ),
584 'color' => '#F8F3EC',
585 'color2' => '#E0C8AB',
586 ),
587 'light-plum-gradient' => array(
588 'label' => __( 'light plum gradient', 'strong-testimonials' ),
589 'color' => '#F7EEF7',
590 'color2' => '#E9D0E9',
591 ),
592 'sky-blue-gradient' => array(
593 'label' => __( 'sky blue gradient', 'strong-testimonials' ),
594 'color' => '#E9F6FB',
595 'color2' => '#C8E9F6',
596 ),
597 );
598
599 if ( $preset ) {
600 if ( isset( $presets[ $preset ] ) ) {
601 return $presets[ $preset ];
602 } else {
603 return wpmtst_get_background_defaults();
604 }
605 }
606
607 return $presets;
608 }
609
610 /**
611 * Return the form success message.
612 *
613 * @since 2.18.0
614 *
615 * @return mixed
616 */
617 function wpmtst_get_success_message() {
618 $message = wpautop( do_shortcode( wpmtst_get_form_message( 'submission-success' ) ) );
619 $message = sprintf( '<div class="%s">%s</div>', 'testimonial-success', $message );
620
621 return apply_filters( 'wpmtst_form_success_message', $message );
622 }
623
624 /**
625 * Does callback exist?
626 *
627 * @param $callback
628 * @since 2.18.0
629 *
630 * @return bool
631 */
632 // TODO Move to Utils class
633 function wpmtst_callback_exists( $callback ) {
634 if ( is_array( $callback ) ) {
635 $exists = method_exists( $callback[0], $callback[1] );
636 } else {
637 $exists = function_exists( $callback );
638 }
639
640 return $exists;
641 }
642
643 /**
644 * Check for Divi Builder plugin.
645 *
646 * Its plugin version constant is inaccurate so get the version from the file header.
647 *
648 * @since 2.22.0
649 *
650 * @return bool
651 */
652 function wpmtst_divi_builder_active() {
653 $active = false;
654 if ( wpmtst_is_plugin_active( 'divi-builder/divi-builder.php' ) ) {
655 $plugin = get_file_data( WP_PLUGIN_DIR . '/divi-builder/divi-builder.php', array( 'version' => 'Version' ) );
656 if ( isset( $plugin['version'] ) && version_compare( $plugin['version'], '2' ) > 0 ) {
657 $active = true;
658 }
659 }
660
661 return $active;
662 }
663
664 /**
665 * Append custom fields to testimonial content in theme's single post template.
666 *
667 * @param $content
668 * @since 2.22.0
669 *
670 * @return string
671 */
672 function wpmtst_single_template_add_content( $content ) {
673 if ( is_singular( 'wpm-testimonial' ) || is_tax( 'wpm-testimonial-category' ) ) {
674 $content .= wpmtst_single_template_client();
675 }
676
677 return $content;
678 }
679 add_filter( 'the_content', 'wpmtst_single_template_add_content' );
680
681 /**
682 * Frequent plugin checks.
683 *
684 * A combination of an array of frequent plugin names, and core's is_plugin_active functions
685 * which are not available in front-end without loading plugin.php which is uncecessary.
686 *
687 * @param $plugin
688 *
689 * @return bool
690 */
691 function wpmtst_is_plugin_active( $plugin = '' ) {
692 if ( ! $plugin )
693 return false;
694
695 $plugins = array(
696 'wpml' => 'sitepress-multilingual-cms/sitepress.php',
697 'polylang' => 'polylang/polylang.php',
698 'lazy-loading-responsive-images' => 'lazy-loading-responsive-images/lazy-load-responsive-images.php',
699 );
700 if ( isset( $plugins[ $plugin ] ) ) {
701 $plugin = $plugins[ $plugin ];
702 }
703
704 if ( in_array( $plugin, (array) get_option( 'active_plugins', array() ) ) )
705 return true;
706
707 if ( ! is_multisite() )
708 return false;
709
710 $plugins = get_site_option( 'active_sitewide_plugins');
711 if ( isset( $plugins[ $plugin ] ) )
712 return true;
713
714 return false;
715 }
716
717 /**
718 * Sanitize a textarea from user input. Based on sanitize_text_field.
719 *
720 * Check for invalid UTF-8,
721 * Convert single < characters to entity,
722 * strip all tags,
723 * strip octets.
724 *
725 * @since 2.11.8
726 *
727 * @param string $text
728 *
729 * @return string
730 */
731 function wpmtst_sanitize_textarea( $text ) {
732 $filtered = wp_check_invalid_utf8( $text );
733
734 if ( strpos( $filtered, '<' ) !== false ) {
735 $filtered = wp_pre_kses_less_than( $filtered );
736 // This will NOT strip extra whitespace.
737 $filtered = wp_strip_all_tags( $filtered, false );
738 }
739
740 while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) {
741 $filtered = str_replace( $match[0], '', $filtered );
742 }
743
744 /**
745 * Filter a sanitized textarea string.
746 *
747 * @param string $filtered The sanitized string.
748 * @param string $str The string prior to being sanitized.
749 */
750 return apply_filters( 'wpmtst_sanitize_textarea', $filtered, $text );
751 }
752
753 /**
754 * Store values as 1 or 0 (never blank).
755 *
756 * Checked checkbox value is "on" but unchecked checkboxes are _not_ submitted.
757 *
758 * @param $input
759 * @param $key string Must be explicit. Do not simply loop through an input array.
760 *
761 * @return int
762 */
763 function wpmtst_sanitize_checkbox( $input, $key ) {
764 if ( isset( $input[ $key ] ) ) {
765 if ( 'on' == $input[ $key ] ) { // checked checkbox
766 return true;
767 } else { // hidden input
768 return $input[ $key ] ? true : false; // 0 or 1
769 }
770 } else { // unchecked checkbox
771 return false;
772 }
773 }
774
775 /**
776 * Trims a entire array recursively.
777 *
778 * @since 2.26.6
779 *
780 * @props Jonas John
781 * @version 0.2
782 * @link http://www.jonasjohn.de/snippets/php/trim-array.htm
783 * @param $input array|string
784 *
785 * @return array|string
786 */
787 function wpmtst_trim_array( $input ) {
788 if ( ! is_array( $input ) ) {
789 return trim( $input );
790 }
791
792 return array_map( 'wpmtst_trim_array', $input );
793 }
794
795 if ( ! function_exists( 'normalize_empty_atts' ) ) {
796 /**
797 * Normalize empty shortcode attributes.
798 *
799 * Turns atts into tags - brilliant!
800 * Thanks http://wordpress.stackexchange.com/a/123073/32076
801 *
802 * @param $atts
803 *
804 * @return mixed
805 */
806 function normalize_empty_atts( $atts ) {
807 if ( ! empty( $atts ) ) {
808 foreach ( $atts as $attribute => $value ) {
809 if ( is_int( $attribute ) ) {
810 $atts[ strtolower( $value ) ] = true;
811 unset( $atts[ $attribute ] );
812 }
813 }
814 }
815
816 return $atts;
817 }
818 }
819
820 if ( ! function_exists( 'wpmtst_round_to_half' ) ) {
821 /**
822 * Round to the nearest half.
823 *
824 * @param $value
825 *
826 * @since 2.31.0
827 * @return float|int
828 */
829 function wpmtst_round_to_half( $value ) {
830 if ( is_string( $value ) ) {
831 $value = (float) str_replace( ',', '.', $value );
832 }
833 return round( (float) $value * 2 ) / 2;
834 }
835 }
836
837
838 if ( ! function_exists( 'wpmtst_strip_whitespace' ) ) {
839 /**
840 * Remove whitespace from HTML output.
841 *
842 * @param $html
843 *
844 * @return string
845 */
846 function wpmtst_strip_whitespace( $html ) {
847 return preg_replace( '~>\s+<~', '><', trim( $html ) );
848 }
849 }
850
851 if ( ! function_exists( 'wpmtst_current_url' ) ) {
852 /**
853 * Assemble and return the current URL.
854 *
855 * @since 2.31.0
856 * @return string
857 */
858 function wpmtst_current_url() {
859 global $wp;
860
861 return home_url( add_query_arg( array(), $wp->request ) );
862 }
863 }
864