| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the condition shortcode. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.2.1 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/shortcodes/special |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle the condition shortcode. |
| 14 |
* |
| 15 |
* @since 3.2.1 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/shortcodes/special |
| 18 |
* @author Brecht Vandersmissen <[email protected]> |
| 19 |
*/ |
| 20 |
class WPUPG_SC_Condition { |
| 21 |
public static function init() { |
| 22 |
add_shortcode( 'wpupg-condition', array( __CLASS__, 'shortcode' ) ); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Output for the shortcode. |
| 27 |
* |
| 28 |
* @since 3.2.1 |
| 29 |
* @param array $atts Options passed along with the shortcode. |
| 30 |
*/ |
| 31 |
public static function shortcode( $atts, $content ) { |
| 32 |
$atts = shortcode_atts( array( |
| 33 |
'type' => '', |
| 34 |
'key' => '', |
| 35 |
'values' => '', |
| 36 |
'match' => 'any', |
| 37 |
'inverse' => '0', |
| 38 |
), $atts, 'wpupg_condition' ); |
| 39 |
|
| 40 |
$item = WPUPG_Template_Shortcodes::get_item(); |
| 41 |
$type = in_array( $atts['type'], array( 'field', 'term_id', 'term_slug' ) ) ? $atts['type'] : false; |
| 42 |
if ( ! $item || ! $content || ! $type ) { |
| 43 |
return ''; |
| 44 |
} |
| 45 |
|
| 46 |
$key = trim( $atts['key'] ); |
| 47 |
$values = explode( ';', str_replace( ',', ';', trim( $atts['values'] ) ) ); |
| 48 |
$match = in_array( $atts['match'], array( 'any', 'all' ) ) ? $atts['match'] : 'any'; |
| 49 |
$inverse = (bool) $atts['inverse']; |
| 50 |
|
| 51 |
$matches_condition = false; |
| 52 |
|
| 53 |
switch ( $type ) { |
| 54 |
case 'field': |
| 55 |
$field_value = $item->custom_field( $key ); |
| 56 |
$matches_condition = false !== $field_value && in_array( $field_value, $values ); |
| 57 |
break; |
| 58 |
case 'term_id': |
| 59 |
case 'term_slug': |
| 60 |
$terms = $item->terms( $key ); |
| 61 |
if ( is_wp_error( $terms ) || ! $terms ) { |
| 62 |
$terms = array(); |
| 63 |
} |
| 64 |
|
| 65 |
if ( 'term_id' === $type ) { |
| 66 |
$terms_to_match = wp_list_pluck( $terms, 'term_id' ); |
| 67 |
$values = array_map( 'intval', $values ); |
| 68 |
} else { |
| 69 |
$terms_to_match = wp_list_pluck( $terms, 'slug' ); |
| 70 |
} |
| 71 |
|
| 72 |
$matches = array_intersect( $values, $terms_to_match ); |
| 73 |
|
| 74 |
if ( 'any' === $match ) { |
| 75 |
$matches_condition = 0 < count( $matches ); |
| 76 |
} else { |
| 77 |
$matches_condition = count( $values ) === count( $matches ); |
| 78 |
} |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
// Optional inverse match. |
| 83 |
if ( $inverse ) { |
| 84 |
$matches_condition = ! $matches_condition; |
| 85 |
} |
| 86 |
|
| 87 |
// Return content if it matches the condition, empty otherwise. |
| 88 |
if ( $matches_condition ) { |
| 89 |
return do_shortcode( $content ); |
| 90 |
} else { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
WPUPG_SC_Condition::init(); |