| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the image shortcode. |
| 4 |
* |
| 5 |
* @link https://bootstrapped.ventures |
| 6 |
* @since 3.8.0 |
| 7 |
* |
| 8 |
* @package WP_Ultimate_Post_Grid |
| 9 |
* @subpackage WP_Ultimate_Post_Grid/includes/public/shortcodes/item |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle the image shortcode. |
| 14 |
* |
| 15 |
* @since 3.8.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_Regular_Image extends WPUPG_Template_Shortcode { |
| 21 |
public static $shortcode = 'wpupg-image'; |
| 22 |
|
| 23 |
public static function init() { |
| 24 |
self::$attributes = array( |
| 25 |
'image_id' => array( |
| 26 |
'default' => '0', |
| 27 |
'type' => 'image', |
| 28 |
), |
| 29 |
'style' => array( |
| 30 |
'default' => 'normal', |
| 31 |
'type' => 'dropdown', |
| 32 |
'options' => array( |
| 33 |
'normal' => 'Normal', |
| 34 |
'rounded' => 'Rounded', |
| 35 |
'circle' => 'Circle', |
| 36 |
), |
| 37 |
), |
| 38 |
'rounded_radius' => array( |
| 39 |
'default' => '5px', |
| 40 |
'type' => 'size', |
| 41 |
'dependency' => array( |
| 42 |
'id' => 'style', |
| 43 |
'value' => 'rounded', |
| 44 |
), |
| 45 |
), |
| 46 |
'size' => array( |
| 47 |
'default' => 'medium', |
| 48 |
'type' => 'image_size' |
| 49 |
), |
| 50 |
'border_width' => array( |
| 51 |
'default' => '0px', |
| 52 |
'type' => 'size', |
| 53 |
), |
| 54 |
'border_style' => array( |
| 55 |
'default' => 'solid', |
| 56 |
'type' => 'dropdown', |
| 57 |
'options' => 'border_styles', |
| 58 |
), |
| 59 |
'border_color' => array( |
| 60 |
'default' => '#666666', |
| 61 |
'type' => 'color', |
| 62 |
), |
| 63 |
'align' => array( |
| 64 |
'default' => 'left', |
| 65 |
'type' => 'dropdown', |
| 66 |
'options' => array( |
| 67 |
'left' => 'Left', |
| 68 |
'center' => 'Center', |
| 69 |
'right' => 'Right', |
| 70 |
), |
| 71 |
), |
| 72 |
); |
| 73 |
parent::init(); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Output for the shortcode. |
| 78 |
* |
| 79 |
* @since 3.8.0 |
| 80 |
* @param array $atts Options passed along with the shortcode. |
| 81 |
*/ |
| 82 |
public static function shortcode( $atts ) { |
| 83 |
$atts = parent::get_attributes( $atts ); |
| 84 |
|
| 85 |
$image_id = intval( $atts['image_id'] ); |
| 86 |
if ( ! $image_id ) { |
| 87 |
return ''; |
| 88 |
} |
| 89 |
|
| 90 |
$size = $atts['size']; |
| 91 |
|
| 92 |
// Check if size should be handled as array. |
| 93 |
preg_match( '/^(\d+)x(\d+)$/i', $size, $match ); |
| 94 |
if ( ! empty( $match ) ) { |
| 95 |
$size = array( intval( $match[1] ), intval( $match[2] ) ); |
| 96 |
} |
| 97 |
|
| 98 |
// Get image. |
| 99 |
$img = wp_get_attachment_image( $image_id, $size ); |
| 100 |
|
| 101 |
if ( ! $img ) { |
| 102 |
return ''; |
| 103 |
} |
| 104 |
|
| 105 |
// Output. |
| 106 |
$classes = array( |
| 107 |
'wpupg-image', |
| 108 |
'wpupg-block-image-' . $atts['style'], |
| 109 |
); |
| 110 |
|
| 111 |
// Align. |
| 112 |
if ( 'left' !== $atts['align'] ) { |
| 113 |
$classes[] = 'wpupg-align-' . esc_attr( $atts['align'] ); |
| 114 |
} |
| 115 |
|
| 116 |
// Image Style. |
| 117 |
$style = ''; |
| 118 |
$style .= 'border-width: ' . $atts['border_width'] . ';'; |
| 119 |
$style .= 'border-style: ' . $atts['border_style'] . ';'; |
| 120 |
$style .= 'border-color: ' . $atts['border_color'] . ';'; |
| 121 |
|
| 122 |
if ( 'rounded' === $atts['style'] ) { |
| 123 |
$style .= 'border-radius: ' . $atts['rounded_radius'] . ';'; |
| 124 |
} |
| 125 |
|
| 126 |
if ( $style ) { |
| 127 |
if ( false !== stripos( $img, ' style="' ) ) { |
| 128 |
$img = str_ireplace( ' style="', ' style="' . esc_attr( $style ), $img ); |
| 129 |
} else { |
| 130 |
$img = str_ireplace( '<img ', '<img style="' . esc_attr( $style ) . '" ', $img ); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
$output = '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $img . '</div>'; |
| 135 |
return apply_filters( parent::get_hook(), $output, $atts ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
WPUPG_SC_Regular_Image::init(); |