| 1 |
<?php |
| 2 |
if (!defined('ABSPATH')) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
class Divi_Property_Image_Widget extends ET_Builder_Module |
| 7 |
{ |
| 8 |
public $slug = 'et_pb_property_image_widget'; |
| 9 |
public $vb_support = 'partial'; |
| 10 |
public $icon = ''; |
| 11 |
|
| 12 |
public function init() { |
| 13 |
$this->name = esc_html__( 'Property Image', 'propertyhive' ); |
| 14 |
$this->icon = '&'; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_fields() |
| 18 |
{ |
| 19 |
$fields = array( |
| 20 |
'image_number' => array( |
| 21 |
'label' => __( 'Image #', 'propertyhive' ), |
| 22 |
'type' => 'number', |
| 23 |
'toggle_slug' => 'main_content', |
| 24 |
), |
| 25 |
'image_size' => array( |
| 26 |
'label' => __( 'Image Size', 'propertyhive' ), |
| 27 |
'type' => 'select', |
| 28 |
'options' => [ |
| 29 |
'thumbnail' => __( 'Thumbnail', 'propertyhive' ), |
| 30 |
'medium' => __( 'Medium', 'propertyhive' ), |
| 31 |
'large' => __( 'Large', 'propertyhive' ), |
| 32 |
'full' => __( 'Full', 'propertyhive' ), |
| 33 |
], |
| 34 |
'default_on_front' => 'large', |
| 35 |
'toggle_slug' => 'main_content', |
| 36 |
), |
| 37 |
'output_ratio' => array( |
| 38 |
'label' => __( 'Image Ratio', 'propertyhive' ), |
| 39 |
'type' => 'select', |
| 40 |
'options' => [ |
| 41 |
'' => __( 'Uploaded Ratio', 'propertyhive' ), |
| 42 |
'3:2' => __( '3:2', 'propertyhive' ), |
| 43 |
'4:3' => __( '4:3', 'propertyhive' ), |
| 44 |
'16:9' => __( '16:9', 'propertyhive' ), |
| 45 |
'1:1' => __( 'Square', 'propertyhive' ), |
| 46 |
], |
| 47 |
'default_on_front' => '', |
| 48 |
'toggle_slug' => 'main_content', |
| 49 |
), |
| 50 |
); |
| 51 |
|
| 52 |
return $fields; |
| 53 |
} |
| 54 |
|
| 55 |
public function render( $attrs, $content, $render_slug ) |
| 56 |
{ |
| 57 |
$post_id = get_the_ID(); |
| 58 |
|
| 59 |
$property = new PH_Property($post_id); |
| 60 |
|
| 61 |
if ( !isset($property->id) ) { |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
$return = ''; |
| 66 |
|
| 67 |
$image_number = 1; |
| 68 |
if ( isset($this->props['image_number']) && $this->props['image_number'] != '' && is_numeric($this->props['image_number']) ) |
| 69 |
{ |
| 70 |
$image_number = (int)$this->props['image_number']; |
| 71 |
} |
| 72 |
|
| 73 |
$output_ratio = isset($this->props['output_ratio']) ? $this->props['output_ratio'] : ''; |
| 74 |
|
| 75 |
if ( $output_ratio != '' ) |
| 76 |
{ |
| 77 |
// output div with image as background |
| 78 |
$url = ''; |
| 79 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 80 |
{ |
| 81 |
$photos = $property->_photo_urls; |
| 82 |
if ( isset($photos[$image_number-1]) ) |
| 83 |
{ |
| 84 |
$url = $photos[$image_number-1]['url']; |
| 85 |
} |
| 86 |
} |
| 87 |
else |
| 88 |
{ |
| 89 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 90 |
|
| 91 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 92 |
{ |
| 93 |
$url = wp_get_attachment_image_src( $gallery_attachment_ids[$image_number-1], $this->props['image_size'] ); |
| 94 |
$url = $url[0]; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// convert ratio to percentage |
| 99 |
$numbers = explode(':', $output_ratio); |
| 100 |
$percent = ( ( (int)$numbers[1] / (int)$numbers[0] ) * 100 ) . '%'; |
| 101 |
|
| 102 |
$return .= '<div style="background:url(' . esc_url($url) . ') no-repeat center center; background-size:cover; padding-bottom:' . esc_attr($percent) . '">'; |
| 103 |
} |
| 104 |
else |
| 105 |
{ |
| 106 |
// output <img> |
| 107 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 108 |
{ |
| 109 |
$photos = $property->_photo_urls; |
| 110 |
if ( isset($photos[$image_number-1]) ) |
| 111 |
{ |
| 112 |
$return .= '<img src="' . esc_url($photos[$image_number-1]['url']) . '" alt="">'; |
| 113 |
} |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 118 |
|
| 119 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 120 |
{ |
| 121 |
$return .= wp_get_attachment_image( $gallery_attachment_ids[$image_number-1], $this->props['image_size'] ); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $this->_render_module_wrapper( $return, $render_slug ); |
| 127 |
} |
| 128 |
} |