| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
add_shortcode( 'compare', 'wppb_toolbox_compare_shortcode' ); |
| 6 |
function wppb_toolbox_compare_shortcode( $atts, $content ){ |
| 7 |
|
| 8 |
$atts = shortcode_atts( array( |
| 9 |
'val1' => '', |
| 10 |
'val2' => '', |
| 11 |
'operator' => '', |
| 12 |
), $atts, 'compare' ); |
| 13 |
|
| 14 |
foreach($atts as $key => $value){ |
| 15 |
$atts[$key] = str_replace('”', '', $value ); |
| 16 |
} |
| 17 |
|
| 18 |
$l = $atts['val1']; |
| 19 |
$r = $atts['val2']; |
| 20 |
|
| 21 |
$operators = array( |
| 22 |
'==' => function($l, $r) { |
| 23 |
return $l == $r; |
| 24 |
}, |
| 25 |
'===' => function($l, $r) { |
| 26 |
return $l === $r; |
| 27 |
}, |
| 28 |
'!=' => function($l, $r) { |
| 29 |
return $l != $r; |
| 30 |
}, |
| 31 |
'<' => function($l, $r) { |
| 32 |
return $l < $r; |
| 33 |
}, |
| 34 |
'>' => function($l, $r) { |
| 35 |
return $l > $r; |
| 36 |
}, |
| 37 |
'<=' => function($l, $r) { |
| 38 |
return $l <= $r; |
| 39 |
}, |
| 40 |
'>=' => function($l, $r) { |
| 41 |
return $l >= $r; |
| 42 |
}, |
| 43 |
'' => function($l, $r) { |
| 44 |
return $l == $r; |
| 45 |
}, |
| 46 |
); |
| 47 |
|
| 48 |
if ( !array_key_exists($atts['operator'], $operators ) ) |
| 49 |
return '<p>The compare operator <strong style="padding:0 10px;">' . esc_html( $atts["operator"] ) . '</strong> is not recognized. Please try: == , ===, !=, <, >, <=, >='; |
| 50 |
|
| 51 |
$bool = $operators[$atts['operator']]($l, $r); |
| 52 |
|
| 53 |
if( $bool ) |
| 54 |
return do_shortcode( $content ); |
| 55 |
} |
| 56 |
|