| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the item author 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 author 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_Author extends WPUPG_Template_Shortcode { |
| 21 |
public static $shortcode = 'wpupg-item-author'; |
| 22 |
|
| 23 |
public static function init() { |
| 24 |
$atts = array( |
| 25 |
'display' => array( |
| 26 |
'default' => 'inline', |
| 27 |
'type' => 'dropdown', |
| 28 |
'options' => 'display_options', |
| 29 |
), |
| 30 |
'align' => array( |
| 31 |
'default' => 'left', |
| 32 |
'type' => 'dropdown', |
| 33 |
'options' => array( |
| 34 |
'left' => 'Left', |
| 35 |
'center' => 'Center', |
| 36 |
'right' => 'Right', |
| 37 |
), |
| 38 |
'dependency' => array( |
| 39 |
'id' => 'display', |
| 40 |
'value' => 'block', |
| 41 |
), |
| 42 |
), |
| 43 |
'text_style' => array( |
| 44 |
'default' => 'normal', |
| 45 |
'type' => 'dropdown', |
| 46 |
'options' => 'text_styles', |
| 47 |
), |
| 48 |
'author_image' => array( |
| 49 |
'default' => '0', |
| 50 |
'type' => 'toggle', |
| 51 |
), |
| 52 |
'image_style' => array( |
| 53 |
'default' => 'circle', |
| 54 |
'type' => 'dropdown', |
| 55 |
'options' => array( |
| 56 |
'normal' => 'Normal', |
| 57 |
'rounded' => 'Rounded', |
| 58 |
'circle' => 'Circle', |
| 59 |
), |
| 60 |
'dependency' => array( |
| 61 |
'id' => 'author_image', |
| 62 |
'value' => '1', |
| 63 |
), |
| 64 |
), |
| 65 |
'rounded_radius' => array( |
| 66 |
'default' => '5px', |
| 67 |
'type' => 'size', |
| 68 |
'dependency' => array( |
| 69 |
array( |
| 70 |
'id' => 'author_image', |
| 71 |
'value' => '1', |
| 72 |
), |
| 73 |
array( |
| 74 |
'id' => 'image_style', |
| 75 |
'value' => 'rounded', |
| 76 |
), |
| 77 |
), |
| 78 |
), |
| 79 |
'image_size' => array( |
| 80 |
'default' => '30px', |
| 81 |
'type' => 'size', |
| 82 |
'dependency' => array( |
| 83 |
'id' => 'author_image', |
| 84 |
'value' => '1', |
| 85 |
), |
| 86 |
), |
| 87 |
'image_border_width' => array( |
| 88 |
'default' => '0px', |
| 89 |
'type' => 'size', |
| 90 |
'dependency' => array( |
| 91 |
'id' => 'author_image', |
| 92 |
'value' => '1', |
| 93 |
), |
| 94 |
), |
| 95 |
'image_border_style' => array( |
| 96 |
'default' => 'solid', |
| 97 |
'type' => 'dropdown', |
| 98 |
'options' => 'border_styles', |
| 99 |
'dependency' => array( |
| 100 |
array( |
| 101 |
'id' => 'author_image', |
| 102 |
'value' => '1', |
| 103 |
), |
| 104 |
array( |
| 105 |
'id' => 'image_border_width', |
| 106 |
'value' => '0px', |
| 107 |
'type' => 'inverse', |
| 108 |
), |
| 109 |
), |
| 110 |
), |
| 111 |
'image_border_color' => array( |
| 112 |
'default' => '#666666', |
| 113 |
'type' => 'color', |
| 114 |
'dependency' => array( |
| 115 |
array( |
| 116 |
'id' => 'author_image', |
| 117 |
'value' => '1', |
| 118 |
), |
| 119 |
array( |
| 120 |
'id' => 'image_border_width', |
| 121 |
'value' => '0px', |
| 122 |
'type' => 'inverse', |
| 123 |
), |
| 124 |
), |
| 125 |
), |
| 126 |
); |
| 127 |
|
| 128 |
$atts = array_merge( $atts, WPUPG_Template_Helper::get_label_container_atts() ); |
| 129 |
|
| 130 |
self::$attributes = $atts; |
| 131 |
parent::init(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Output for the shortcode. |
| 136 |
* |
| 137 |
* @since 3.0.0 |
| 138 |
* @param array $atts Options passed along with the shortcode. |
| 139 |
*/ |
| 140 |
public static function shortcode( $atts ) { |
| 141 |
$atts = parent::get_attributes( $atts ); |
| 142 |
|
| 143 |
$item = WPUPG_Template_Shortcodes::get_item(); |
| 144 |
if ( ! $item ) { |
| 145 |
return ''; |
| 146 |
} |
| 147 |
|
| 148 |
// Output. |
| 149 |
$classes = array( |
| 150 |
'wpupg-item-author', |
| 151 |
'wpupg-block-text-' . $atts['text_style'], |
| 152 |
); |
| 153 |
|
| 154 |
// Alignment. |
| 155 |
if ( 'block' === $atts['display'] && 'left' !== $atts['align'] ) { |
| 156 |
$classes[] = 'wpupg-align-' . $atts['align']; |
| 157 |
} |
| 158 |
|
| 159 |
$author_output = $item->author(); |
| 160 |
|
| 161 |
// Optional author image. |
| 162 |
$img = ''; |
| 163 |
if ( (bool) $atts['author_image'] ) { |
| 164 |
$author_id = $item->author_id(); |
| 165 |
|
| 166 |
if ( $author_id ) { |
| 167 |
$img = get_avatar( $author_id, $atts['image_size'] ); |
| 168 |
|
| 169 |
// Image Style. |
| 170 |
$style = ''; |
| 171 |
$style .= 'border-width: ' . $atts['image_border_width'] . ';'; |
| 172 |
$style .= 'border-style: ' . $atts['image_border_style'] . ';'; |
| 173 |
$style .= 'border-color: ' . $atts['image_border_color'] . ';'; |
| 174 |
|
| 175 |
if ( 'rounded' === $atts['image_style'] ) { |
| 176 |
$style .= 'border-radius: ' . $atts['rounded_radius'] . ';'; |
| 177 |
} elseif ( 'circle' === $atts['image_style'] ) { |
| 178 |
$style .= 'border-radius: 50%;'; |
| 179 |
} |
| 180 |
|
| 181 |
if ( $style ) { |
| 182 |
if ( false !== stripos( $img, ' style="' ) ) { |
| 183 |
$img = str_ireplace( ' style="', ' style="' . esc_attr( $style ), $img ); |
| 184 |
} else { |
| 185 |
$img = str_ireplace( '<img ', '<img style="' . esc_attr( $style ) . '" ', $img ); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if ( $img ) { |
| 192 |
$author_output = '<span class="wpupg-item-author-with-image"><span class="wpupg-item-author-image">' . $img . '</span>' . WPUPG_Shortcode::sanitize_html( $author_output ) . '</span>'; |
| 193 |
} |
| 194 |
|
| 195 |
$label_container = WPUPG_Template_Helper::get_label_container( $atts, 'author' ); |
| 196 |
$tag = 'block' === $atts['display'] ? 'div' : 'span'; |
| 197 |
$output = '<' . esc_attr( $tag ) . ' class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $label_container . $author_output . '</' . esc_attr( $tag ) . '>'; |
| 198 |
return apply_filters( parent::get_hook(), $output, $atts, $item ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
WPUPG_SC_Author::init(); |