| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the item terms shortcode. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.0.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/shortcodes/item |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle the item terms shortcode. |
| 14 |
* |
| 15 |
* @since 3.0.0 |
| 16 |
* @package WP_Ultimate_Post_Grid |
| 17 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/shortcodes/item |
| 18 |
* @author Brecht Vandersmissen <[email protected]> |
| 19 |
*/ |
| 20 |
class WPUPG_SC_Terms extends WPUPG_Template_Shortcode { |
| 21 |
public static $shortcode = 'wpupg-item-terms'; |
| 22 |
|
| 23 |
public static function init() { |
| 24 |
$atts = array( |
| 25 |
'key' => array( |
| 26 |
'default' => '', |
| 27 |
'type' => 'text', |
| 28 |
'help' => 'Key of the taxonomy you want to display. Use "category" for regular categories and "post_tag" for regular tags.', |
| 29 |
), |
| 30 |
'orderby' => array( |
| 31 |
'default' => 'name', |
| 32 |
'type' => 'dropdown', |
| 33 |
'options' => array( |
| 34 |
'id' => 'ID', |
| 35 |
'name' => 'Name', |
| 36 |
'slug' => 'Slug', |
| 37 |
'count' => 'Count', |
| 38 |
'description' => 'Description', |
| 39 |
'parent' => 'Parent', |
| 40 |
'term_group' => 'Term Group', |
| 41 |
'term_id' => 'Term ID', |
| 42 |
), |
| 43 |
), |
| 44 |
'order' => array( |
| 45 |
'default' => 'asc', |
| 46 |
'type' => 'dropdown', |
| 47 |
'options' => array( |
| 48 |
'asc' => 'Ascending', |
| 49 |
'desc' => 'Descending', |
| 50 |
), |
| 51 |
), |
| 52 |
'links' => array( |
| 53 |
'type' => 'toggle', |
| 54 |
'default' => '0', |
| 55 |
), |
| 56 |
'display' => array( |
| 57 |
'default' => 'inline', |
| 58 |
'type' => 'dropdown', |
| 59 |
'options' => 'display_options', |
| 60 |
), |
| 61 |
'align' => array( |
| 62 |
'default' => 'left', |
| 63 |
'type' => 'dropdown', |
| 64 |
'options' => array( |
| 65 |
'left' => 'Left', |
| 66 |
'center' => 'Center', |
| 67 |
'right' => 'Right', |
| 68 |
), |
| 69 |
'dependency' => array( |
| 70 |
'id' => 'display', |
| 71 |
'value' => 'block', |
| 72 |
), |
| 73 |
), |
| 74 |
'term_style' => array( |
| 75 |
'default' => 'text', |
| 76 |
'type' => 'dropdown', |
| 77 |
'options' => array( |
| 78 |
'text' => 'Text', |
| 79 |
'block' => 'Block', |
| 80 |
), |
| 81 |
), |
| 82 |
'block_horizontal_padding' => array( |
| 83 |
'default' => '5px', |
| 84 |
'type' => 'size', |
| 85 |
'dependency' => array( |
| 86 |
'id' => 'term_style', |
| 87 |
'value' => 'block', |
| 88 |
), |
| 89 |
), |
| 90 |
'block_vertical_padding' => array( |
| 91 |
'default' => '2px', |
| 92 |
'type' => 'size', |
| 93 |
'dependency' => array( |
| 94 |
'id' => 'term_style', |
| 95 |
'value' => 'block', |
| 96 |
), |
| 97 |
), |
| 98 |
'block_color' => array( |
| 99 |
'default' => '#333333', |
| 100 |
'type' => 'color', |
| 101 |
'dependency' => array( |
| 102 |
'id' => 'term_style', |
| 103 |
'value' => 'block', |
| 104 |
), |
| 105 |
), |
| 106 |
'block_text_color' => array( |
| 107 |
'default' => '#ffffff', |
| 108 |
'type' => 'color', |
| 109 |
'dependency' => array( |
| 110 |
'id' => 'term_style', |
| 111 |
'value' => 'block', |
| 112 |
), |
| 113 |
), |
| 114 |
'text_style' => array( |
| 115 |
'default' => 'normal', |
| 116 |
'type' => 'dropdown', |
| 117 |
'options' => 'text_styles', |
| 118 |
), |
| 119 |
'term_separator' => array( |
| 120 |
'default' => ', ', |
| 121 |
'type' => 'text', |
| 122 |
), |
| 123 |
); |
| 124 |
|
| 125 |
$atts = array_merge( $atts, WPUPG_Template_Helper::get_label_container_atts() ); |
| 126 |
|
| 127 |
self::$attributes = $atts; |
| 128 |
parent::init(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Output for the shortcode. |
| 133 |
* |
| 134 |
* @since 3.0.0 |
| 135 |
* @param array $atts Options passed along with the shortcode. |
| 136 |
*/ |
| 137 |
public static function shortcode( $atts ) { |
| 138 |
$atts = parent::get_attributes( $atts ); |
| 139 |
|
| 140 |
$item = WPUPG_Template_Shortcodes::get_item(); |
| 141 |
|
| 142 |
$args = array( |
| 143 |
'orderby' => $atts['orderby'], |
| 144 |
'order' => $atts['order'], |
| 145 |
); |
| 146 |
|
| 147 |
$terms = $item->terms( $atts['key'], $args ); |
| 148 |
if ( ! $item || ! $terms || ! is_array( $terms ) ) { |
| 149 |
return ''; |
| 150 |
} |
| 151 |
|
| 152 |
// Output. |
| 153 |
$classes = array( |
| 154 |
'wpupg-item-terms', |
| 155 |
'wpupg-block-text-' . $atts['text_style'], |
| 156 |
); |
| 157 |
|
| 158 |
// Alignment. |
| 159 |
if ( 'block' === $atts['display'] && 'left' !== $atts['align'] ) { |
| 160 |
$classes[] = 'wpupg-align-' . $atts['align']; |
| 161 |
} |
| 162 |
|
| 163 |
// Term Output. |
| 164 |
$term_output = ''; |
| 165 |
foreach ( $terms as $index => $term ) { |
| 166 |
if ( 0 !== $index ) { |
| 167 |
$term_output .= $atts['term_separator']; |
| 168 |
} |
| 169 |
|
| 170 |
$style = ''; |
| 171 |
|
| 172 |
if ( 'block' === $atts['term_style'] ) { |
| 173 |
$style = ' style="display: inline-block;padding:' . esc_attr( $atts['block_vertical_padding'] ) . ' ' . esc_attr( $atts['block_horizontal_padding'] ) . ';background-color:' . esc_attr( $atts['block_color'] ) . ';color:' . esc_attr( $atts['block_text_color'] ) . ';"'; |
| 174 |
} |
| 175 |
|
| 176 |
if ( is_object( $term ) ) { |
| 177 |
$link = (bool) $atts['links'] ? get_term_link( $term ) : false; |
| 178 |
$name = apply_filters( 'wpupg_term_name', $term->name, $term->term_id, $atts['key'] ); |
| 179 |
|
| 180 |
if ( $link && ! is_wp_error( $link ) ) { |
| 181 |
$term_output .= '<a href="' . esc_attr( $link ) . '" class="wpupg-item-term"' . $style . '>' . WPUPG_Shortcode::sanitize_html( $name ) . '</a>'; |
| 182 |
} else { |
| 183 |
$term_output .= '<span class="wpupg-item-term"' . $style . '>' . WPUPG_Shortcode::sanitize_html( $name ) . '</span>'; |
| 184 |
} |
| 185 |
} else { |
| 186 |
$term_output .= '<span class="wpupg-item-term"' . $style . '>' . WPUPG_Shortcode::sanitize_html( $term ) . '</span>'; |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
$label_container = WPUPG_Template_Helper::get_label_container( $atts, 'terms' ); |
| 191 |
$tag = 'block' === $atts['display'] ? 'div' : 'span'; |
| 192 |
$output = '<' . esc_attr( $tag ) . ' class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $label_container . $term_output . '</' . esc_attr( $tag ) . '>'; |
| 193 |
return apply_filters( parent::get_hook(), $output, $atts, $item ); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
WPUPG_SC_Terms::init(); |