| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elementor Property Image Widget. |
| 4 |
* |
| 5 |
* @since 1.0.0 |
| 6 |
*/ |
| 7 |
class Elementor_Property_Image_Widget extends \Elementor\Widget_Base { |
| 8 |
|
| 9 |
public function get_name() { |
| 10 |
return 'property-image'; |
| 11 |
} |
| 12 |
|
| 13 |
public function get_title() { |
| 14 |
return __( 'Image', 'propertyhive' ); |
| 15 |
} |
| 16 |
|
| 17 |
public function get_icon() { |
| 18 |
return 'eicon-image'; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_categories() { |
| 22 |
return [ 'property-hive' ]; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_keywords() { |
| 26 |
return [ 'property hive', 'propertyhive', 'property', 'images', 'photos', 'image', 'photo' ]; |
| 27 |
} |
| 28 |
|
| 29 |
protected function register_controls() { |
| 30 |
|
| 31 |
$this->start_controls_section( |
| 32 |
'content_section', |
| 33 |
[ |
| 34 |
'label' => __( 'Image', 'propertyhive' ), |
| 35 |
'tab' => \Elementor\Controls_Manager::TAB_CONTENT, |
| 36 |
] |
| 37 |
); |
| 38 |
|
| 39 |
$this->add_control( |
| 40 |
'image_link', |
| 41 |
[ |
| 42 |
'label' => __( 'Link', 'propertyhive' ), |
| 43 |
'type' => \Elementor\Controls_Manager::URL, // Control type |
| 44 |
'default' => [ |
| 45 |
'url' => '', |
| 46 |
'is_external' => false, |
| 47 |
'nofollow' => false, |
| 48 |
], |
| 49 |
'dynamic' => [ |
| 50 |
'active' => true, |
| 51 |
'settings' => [ |
| 52 |
'dynamic_tag' => \Elementor\Plugin::$instance->dynamic_tags->tag_data_to_tag_text( null, 'post-url' ) |
| 53 |
] |
| 54 |
], |
| 55 |
] |
| 56 |
); |
| 57 |
|
| 58 |
$this->add_control( |
| 59 |
'image_number', |
| 60 |
[ |
| 61 |
'label' => __( 'Image #', 'propertyhive' ), |
| 62 |
'type' => \Elementor\Controls_Manager::NUMBER, |
| 63 |
'default' => 1, |
| 64 |
] |
| 65 |
); |
| 66 |
|
| 67 |
$this->add_control( |
| 68 |
'image_size', |
| 69 |
[ |
| 70 |
'label' => __( 'Image Size', 'propertyhive' ), |
| 71 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 72 |
'options' => [ |
| 73 |
'thumbnail' => __( 'Thumbnail', 'propertyhive' ), |
| 74 |
'medium' => __( 'Medium', 'propertyhive' ), |
| 75 |
'large' => __( 'Large', 'propertyhive' ), |
| 76 |
'full' => __( 'Full', 'propertyhive' ), |
| 77 |
], |
| 78 |
'default' => 'large', |
| 79 |
] |
| 80 |
); |
| 81 |
|
| 82 |
$this->add_control( |
| 83 |
'output_ratio', |
| 84 |
[ |
| 85 |
'label' => __( 'Image Ratio', 'propertyhive' ), |
| 86 |
'type' => \Elementor\Controls_Manager::SELECT, |
| 87 |
'options' => [ |
| 88 |
'' => __( 'Uploaded Ratio', 'propertyhive' ), |
| 89 |
'3:2' => __( '3:2', 'propertyhive' ), |
| 90 |
'4:3' => __( '4:3', 'propertyhive' ), |
| 91 |
'16:9' => __( '16:9', 'propertyhive' ), |
| 92 |
'1:1' => __( 'Square', 'propertyhive' ), |
| 93 |
], |
| 94 |
'default' => '' |
| 95 |
] |
| 96 |
); |
| 97 |
|
| 98 |
do_action( 'propertyhive_elementor_widget_property_image_controls', $this ); |
| 99 |
|
| 100 |
$this->end_controls_section(); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
protected function render() { |
| 105 |
|
| 106 |
global $property; |
| 107 |
|
| 108 |
$settings = $this->get_settings_for_display(); |
| 109 |
|
| 110 |
if ( !isset($property->id) ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$image_number = 1; |
| 115 |
if ( isset($settings['image_number']) && $settings['image_number'] != '' && is_numeric($settings['image_number']) ) |
| 116 |
{ |
| 117 |
$image_number = (int)$settings['image_number']; |
| 118 |
} |
| 119 |
|
| 120 |
$output_ratio = isset($settings['output_ratio']) ? $settings['output_ratio'] : ''; |
| 121 |
|
| 122 |
if ( $output_ratio != '' ) |
| 123 |
{ |
| 124 |
// output div with image as background |
| 125 |
$url = ''; |
| 126 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 127 |
{ |
| 128 |
$photos = $property->_photo_urls; |
| 129 |
if ( isset($photos[$image_number-1]) ) |
| 130 |
{ |
| 131 |
$url = $photos[$image_number-1]['url']; |
| 132 |
} |
| 133 |
} |
| 134 |
else |
| 135 |
{ |
| 136 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 137 |
|
| 138 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 139 |
{ |
| 140 |
$url = wp_get_attachment_image_src( $gallery_attachment_ids[$image_number-1], $settings['image_size'] ); |
| 141 |
$url = $url[0]; |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// convert ratio to percentage |
| 146 |
$numbers = explode(':', $output_ratio); |
| 147 |
$percent = ( ( (int)$numbers[1] / (int)$numbers[0] ) * 100 ) . '%'; |
| 148 |
|
| 149 |
|
| 150 |
if ( ! empty( $settings['image_link']['url'] ) ) |
| 151 |
{ |
| 152 |
echo '<div style="background:url(' . esc_url($url) . ') no-repeat center center; background-size:cover;">'; |
| 153 |
$this->add_link_attributes( 'image_link', $settings['image_link'] ); |
| 154 |
?><a <?php $this->print_render_attribute_string( 'image_link' ); ?> style="display:block; <?php echo 'padding-bottom:' . esc_attr($percent); ?>"><?php |
| 155 |
echo '</a>'; |
| 156 |
echo '</div>'; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
echo '<div style="background:url(' . esc_url($url) . ') no-repeat center center; background-size:cover; padding-bottom:' . esc_attr($percent) . '"></div>'; |
| 161 |
} |
| 162 |
} |
| 163 |
else |
| 164 |
{ |
| 165 |
// output <img> |
| 166 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 167 |
{ |
| 168 |
$photos = $property->_photo_urls; |
| 169 |
if ( isset($photos[$image_number-1]) ) |
| 170 |
{ |
| 171 |
if ( ! empty( $settings['image_link']['url'] ) ) |
| 172 |
{ |
| 173 |
$this->add_link_attributes( 'image_link', $settings['image_link'] ); |
| 174 |
?><a <?php $this->print_render_attribute_string( 'image_link' ); ?>><?php |
| 175 |
} |
| 176 |
echo '<img src="' . esc_url($photos[$image_number-1]['url']) . '" alt="">'; |
| 177 |
if ( ! empty( $settings['image_link']['url'] ) ) { |
| 178 |
echo '</a>'; |
| 179 |
} |
| 180 |
} |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 185 |
|
| 186 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 187 |
{ |
| 188 |
if ( ! empty( $settings['image_link']['url'] ) ) |
| 189 |
{ |
| 190 |
$this->add_link_attributes( 'image_link', $settings['image_link'] ); |
| 191 |
?><a <?php $this->print_render_attribute_string( 'image_link' ); ?>><?php |
| 192 |
} |
| 193 |
echo wp_get_attachment_image( $gallery_attachment_ids[$image_number-1], $settings['image_size'] ); |
| 194 |
if ( ! empty( $settings['image_link']['url'] ) ) { |
| 195 |
echo '</a>'; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
do_action( 'propertyhive_elementor_widget_property_image_render_after', $settings, $property ); |
| 202 |
} |
| 203 |
|
| 204 |
} |