| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handle the item image 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 image 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_Image extends WPUPG_Template_Shortcode { |
| 21 |
public static $shortcode = 'wpupg-item-image'; |
| 22 |
|
| 23 |
public static function init() { |
| 24 |
self::$attributes = array( |
| 25 |
'default' => array( |
| 26 |
'default' => '', |
| 27 |
), |
| 28 |
'style' => array( |
| 29 |
'default' => 'normal', |
| 30 |
'type' => 'dropdown', |
| 31 |
'options' => array( |
| 32 |
'normal' => 'Normal', |
| 33 |
'rounded' => 'Rounded', |
| 34 |
'circle' => 'Circle', |
| 35 |
), |
| 36 |
), |
| 37 |
'align' => array( |
| 38 |
'default' => 'center', |
| 39 |
'type' => 'dropdown', |
| 40 |
'options' => array( |
| 41 |
'left' => 'Left', |
| 42 |
'center' => 'Center', |
| 43 |
'right' => 'Right', |
| 44 |
), |
| 45 |
), |
| 46 |
'rounded_radius' => array( |
| 47 |
'default' => '5px', |
| 48 |
'type' => 'size', |
| 49 |
'dependency' => array( |
| 50 |
'id' => 'style', |
| 51 |
'value' => 'rounded', |
| 52 |
), |
| 53 |
), |
| 54 |
'size' => array( |
| 55 |
'default' => '', |
| 56 |
'type' => 'image_size' |
| 57 |
), |
| 58 |
'border_width' => array( |
| 59 |
'default' => '0px', |
| 60 |
'type' => 'size', |
| 61 |
), |
| 62 |
'border_style' => array( |
| 63 |
'default' => 'solid', |
| 64 |
'type' => 'dropdown', |
| 65 |
'options' => 'border_styles', |
| 66 |
), |
| 67 |
'border_color' => array( |
| 68 |
'default' => '#666666', |
| 69 |
'type' => 'color', |
| 70 |
), |
| 71 |
'link' => array( |
| 72 |
'default' => '0', |
| 73 |
'type' => 'toggle', |
| 74 |
), |
| 75 |
'link_target' => array( |
| 76 |
'default' => '_self', |
| 77 |
'type' => 'dropdown', |
| 78 |
'options' => array( |
| 79 |
'_self' => 'Open in same tab', |
| 80 |
'_blank' => 'Open in new tab', |
| 81 |
), |
| 82 |
'dependency' => array( |
| 83 |
'id' => 'link', |
| 84 |
'value' => '1', |
| 85 |
), |
| 86 |
), |
| 87 |
'prevent_pinning' => array( |
| 88 |
'default' => '0', |
| 89 |
'type' => 'toggle', |
| 90 |
), |
| 91 |
); |
| 92 |
parent::init(); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Output for the shortcode. |
| 97 |
* |
| 98 |
* @since 3.0.0 |
| 99 |
* @param array $atts Options passed along with the shortcode. |
| 100 |
*/ |
| 101 |
public static function shortcode( $atts ) { |
| 102 |
$atts = parent::get_attributes( $atts ); |
| 103 |
|
| 104 |
$item = WPUPG_Template_Shortcodes::get_item(); |
| 105 |
|
| 106 |
if ( ! $item || ! $item->has( 'image' ) ) { |
| 107 |
return ''; |
| 108 |
} |
| 109 |
|
| 110 |
// Use explicit size if set. |
| 111 |
$size = $atts['size'] ? $atts['size'] : $atts['default']; |
| 112 |
|
| 113 |
// Check if size should be handled as array. |
| 114 |
preg_match( '/^(\d+)x(\d+)$/i', $size, $match ); |
| 115 |
if ( ! empty( $match ) ) { |
| 116 |
$size = array( intval( $match[1] ), intval( $match[2] ) ); |
| 117 |
} |
| 118 |
|
| 119 |
// Output. |
| 120 |
$classes = array( |
| 121 |
'wpupg-item-image', |
| 122 |
'wpupg-block-image-' . $atts['style'], |
| 123 |
); |
| 124 |
|
| 125 |
// Alignment. |
| 126 |
if ( 'left' !== $atts['align'] ) { |
| 127 |
$classes[] = 'wpupg-align-' . $atts['align']; |
| 128 |
} |
| 129 |
|
| 130 |
$img = $item->image( $size ); |
| 131 |
|
| 132 |
// Image Style. |
| 133 |
$style = ''; |
| 134 |
$style .= 'border-width: ' . $atts['border_width'] . ';'; |
| 135 |
$style .= 'border-style: ' . $atts['border_style'] . ';'; |
| 136 |
$style .= 'border-color: ' . $atts['border_color'] . ';'; |
| 137 |
|
| 138 |
if ( 'rounded' === $atts['style'] ) { |
| 139 |
$style .= 'border-radius: ' . $atts['rounded_radius'] . ';'; |
| 140 |
} |
| 141 |
|
| 142 |
if ( $style ) { |
| 143 |
if ( false !== stripos( $img, ' style="' ) ) { |
| 144 |
$img = str_ireplace( ' style="', ' style="' . esc_attr( $style ), $img ); |
| 145 |
} else { |
| 146 |
$img = str_ireplace( '<img ', '<img style="' . esc_attr( $style ) . '" ', $img ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
// Prevent lazy image loading. |
| 151 |
if ( WPUPG_Settings::get( 'prevent_lazy_image_loading' ) ) { |
| 152 |
$img = str_ireplace( ' class="', ' class="skip-lazy disable-lazyload ', $img ); |
| 153 |
$img = str_ireplace( ' loading="lazy"', ' ', $img ); |
| 154 |
} |
| 155 |
|
| 156 |
// Prevent pinning. |
| 157 |
if ( (bool) $atts['prevent_pinning'] ) { |
| 158 |
$img = str_ireplace( '<img ', '<img data-pin-nopin="true" ', $img ); |
| 159 |
} |
| 160 |
|
| 161 |
// Link image. |
| 162 |
if ( $atts['link'] ) { |
| 163 |
$url = $item->url(); |
| 164 |
|
| 165 |
if ( false !== stripos( $img, ' href="' ) ) { |
| 166 |
$img = preg_replace( '/\shref=\"[^\"]*"/', ' href="' . esc_url( $url ) . '" target="' . esc_attr( $atts['link_target'] ). '"', $img ); |
| 167 |
} else { |
| 168 |
$img = '<a href="' . esc_url( $url ) . '" target="' . esc_attr( $atts['link_target'] ) . '">' . $img . '</a>'; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
$output = '<div class="' . esc_attr( implode( ' ', $classes ) ) . '">' . $img . '</div>'; |
| 173 |
return apply_filters( parent::get_hook(), $output, $atts, $item ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
WPUPG_SC_Image::init(); |