| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
add_shortcode( 'avada_property_image', function( $atts ) { |
| 9 |
$atts = shortcode_atts( array( |
| 10 |
'image_number' => '', |
| 11 |
'image_size' => '', |
| 12 |
'output_ratio' => '', |
| 13 |
), $atts ); |
| 14 |
|
| 15 |
if ( get_post_type( get_the_ID() ) != 'property' ) |
| 16 |
{ |
| 17 |
return ''; |
| 18 |
} |
| 19 |
|
| 20 |
fusion_element_rendering_elements( true ); |
| 21 |
|
| 22 |
global $property; |
| 23 |
|
| 24 |
if ( empty($property) ) |
| 25 |
{ |
| 26 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Shared frontend property global used by the Avada shortcode contract; changing $property would break the existing property context passed to these widgets. |
| 27 |
$property = new PH_Property(get_the_ID()); |
| 28 |
} |
| 29 |
|
| 30 |
ob_start(); |
| 31 |
|
| 32 |
$image_number = 1; |
| 33 |
if ( isset($atts['image_number']) && $atts['image_number'] != '' && is_numeric($atts['image_number']) ) |
| 34 |
{ |
| 35 |
$image_number = (int)$atts['image_number']; |
| 36 |
} |
| 37 |
|
| 38 |
$output_ratio = isset($atts['output_ratio']) ? $atts['output_ratio'] : ''; |
| 39 |
|
| 40 |
if ( $output_ratio != '' ) |
| 41 |
{ |
| 42 |
// output div with image as background |
| 43 |
$url = ''; |
| 44 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 45 |
{ |
| 46 |
$photos = $property->_photo_urls; |
| 47 |
if ( isset($photos[$image_number-1]) ) |
| 48 |
{ |
| 49 |
$url = $photos[$image_number-1]['url']; |
| 50 |
} |
| 51 |
} |
| 52 |
else |
| 53 |
{ |
| 54 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 55 |
|
| 56 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 57 |
{ |
| 58 |
$url = wp_get_attachment_image_src( $gallery_attachment_ids[$image_number-1], $atts['image_size'] ); |
| 59 |
$url = $url[0]; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
// convert ratio to percentage |
| 64 |
$numbers = explode(':', $output_ratio); |
| 65 |
$percent = ( ( (int)$numbers[1] / (int)$numbers[0] ) * 100 ) . '%'; |
| 66 |
|
| 67 |
|
| 68 |
if ( ! empty( $atts['image_link']['url'] ) ) |
| 69 |
{ |
| 70 |
echo '<div style="background:url(' . esc_url($url) . ') no-repeat center center; background-size:cover;">'; |
| 71 |
$this->add_link_attributes( 'image_link', $atts['image_link'] ); |
| 72 |
?><a <?php $this->print_render_attribute_string( 'image_link' ); ?> style="display:block; <?php echo 'padding-bottom:' . esc_attr($percent); ?>"><?php |
| 73 |
echo '</a>'; |
| 74 |
echo '</div>'; |
| 75 |
} |
| 76 |
else |
| 77 |
{ |
| 78 |
echo '<div style="background:url(' . esc_url($url) . ') no-repeat center center; background-size:cover; padding-bottom:' . esc_attr($percent) . '"></div>'; |
| 79 |
} |
| 80 |
} |
| 81 |
else |
| 82 |
{ |
| 83 |
// output <img> |
| 84 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 85 |
{ |
| 86 |
$photos = $property->_photo_urls; |
| 87 |
if ( isset($photos[$image_number-1]) ) |
| 88 |
{ |
| 89 |
if ( ! empty( $atts['image_link']['url'] ) ) |
| 90 |
{ |
| 91 |
$this->add_link_attributes( 'image_link', $atts['image_link'] ); |
| 92 |
?><a <?php $this->print_render_attribute_string( 'image_link' ); ?>><?php |
| 93 |
} |
| 94 |
echo '<img src="' . esc_url($photos[$image_number-1]['url']) . '" alt="">'; |
| 95 |
if ( ! empty( $atts['image_link']['url'] ) ) { |
| 96 |
echo '</a>'; |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
else |
| 101 |
{ |
| 102 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 103 |
|
| 104 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 105 |
{ |
| 106 |
if ( ! empty( $atts['image_link']['url'] ) ) |
| 107 |
{ |
| 108 |
$this->add_link_attributes( 'image_link', $atts['image_link'] ); |
| 109 |
?><a <?php $this->print_render_attribute_string( 'image_link' ); ?>><?php |
| 110 |
} |
| 111 |
echo wp_get_attachment_image( $gallery_attachment_ids[$image_number-1], $atts['image_size'] ); |
| 112 |
if ( ! empty( $atts['image_link']['url'] ) ) { |
| 113 |
echo '</a>'; |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
do_action( 'propertyhive_avada_widget_property_image_render_after', $atts, $property ); |
| 120 |
|
| 121 |
$html = ob_get_clean(); |
| 122 |
|
| 123 |
fusion_element_rendering_elements( false ); |
| 124 |
|
| 125 |
return $html; |
| 126 |
}); |