| 1 |
<?php |
| 2 |
/** |
| 3 |
* Bricks Builder Property Image Widget. |
| 4 |
* |
| 5 |
* @since 1.0.0 |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 9 |
|
| 10 |
class Bricks_Builder_Property_Image_Widget extends \Bricks\Element { |
| 11 |
|
| 12 |
// Element properties |
| 13 |
public $category = 'propertyhive'; |
| 14 |
public $name = 'bricks-builder-property-image'; |
| 15 |
public $icon = 'fas fa-image'; |
| 16 |
|
| 17 |
public function get_label() |
| 18 |
{ |
| 19 |
return esc_html__( 'Image', 'propertyhive' ); |
| 20 |
} |
| 21 |
|
| 22 |
public function set_control_groups() |
| 23 |
{ |
| 24 |
/*$this->control_groups['form'] = [ |
| 25 |
'title' => esc_html__( 'Form', 'propertyhive' ), |
| 26 |
'tab' => 'content', // content / style |
| 27 |
];*/ |
| 28 |
} |
| 29 |
|
| 30 |
public function set_controls() |
| 31 |
{ |
| 32 |
$this->controls['image_number'] = [ // Unique control identifier (lowercase, no spaces) |
| 33 |
'tab' => 'content', // Control tab: content/style |
| 34 |
//'group' => 'form', // Show under control group |
| 35 |
'label' => esc_html__( 'Image #', 'propertyhive' ), // Control label |
| 36 |
'type' => 'text', // Control type |
| 37 |
'default' => 1 |
| 38 |
]; |
| 39 |
|
| 40 |
$this->controls['image_size'] = [ |
| 41 |
'tab' => 'content', |
| 42 |
//'group' => 'settings', |
| 43 |
'label' => esc_html__( 'Type', 'propertyhive' ), |
| 44 |
'type' => 'select', |
| 45 |
'options' => [ |
| 46 |
'thumbnail' => __( 'Thumbnail', 'propertyhive' ), |
| 47 |
'medium' => __( 'Medium', 'propertyhive' ), |
| 48 |
'large' => __( 'Large', 'propertyhive' ), |
| 49 |
'full' => __( 'Full', 'propertyhive' ), |
| 50 |
], |
| 51 |
//'inline' => true, |
| 52 |
//'clearable' => false, |
| 53 |
//'pasteStyles' => false, |
| 54 |
'default' => 'large', |
| 55 |
]; |
| 56 |
|
| 57 |
$this->controls['output_ratio'] = [ |
| 58 |
'tab' => 'content', |
| 59 |
//'group' => 'settings', |
| 60 |
'label' => esc_html__( 'Image Ratio', 'propertyhive' ), |
| 61 |
'type' => 'select', |
| 62 |
'options' => [ |
| 63 |
'' => __( 'Uploaded Ratio', 'propertyhive' ), |
| 64 |
'3:2' => __( '3:2', 'propertyhive' ), |
| 65 |
'4:3' => __( '4:3', 'propertyhive' ), |
| 66 |
'16:9' => __( '16:9', 'propertyhive' ), |
| 67 |
'1:1' => __( 'Square', 'propertyhive' ), |
| 68 |
], |
| 69 |
//'inline' => true, |
| 70 |
//'clearable' => false, |
| 71 |
//'pasteStyles' => false, |
| 72 |
'default' => 'large', |
| 73 |
]; |
| 74 |
|
| 75 |
do_action( 'propertyhive_bricks_builder_widget_property_image_controls', $this ); |
| 76 |
} |
| 77 |
|
| 78 |
public function render() |
| 79 |
{ |
| 80 |
global $property; |
| 81 |
|
| 82 |
if ( !isset($property->id) ) |
| 83 |
{ |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
$root_classes[] = $this->name; |
| 88 |
|
| 89 |
$settings = $this->settings; |
| 90 |
|
| 91 |
// Add 'class' attribute to element root tag |
| 92 |
$this->set_attribute( '_root', 'class', $root_classes ); |
| 93 |
|
| 94 |
echo "<div {$this->render_attributes( '_root' )}>"; |
| 95 |
|
| 96 |
$image_number = 1; |
| 97 |
if ( isset($settings['image_number']) && $settings['image_number'] != '' && is_numeric($settings['image_number']) ) |
| 98 |
{ |
| 99 |
$image_number = (int)$settings['image_number']; |
| 100 |
} |
| 101 |
|
| 102 |
$output_ratio = isset($settings['output_ratio']) ? $settings['output_ratio'] : ''; |
| 103 |
|
| 104 |
if ( $output_ratio != '' ) |
| 105 |
{ |
| 106 |
// output div with image as background |
| 107 |
$url = ''; |
| 108 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 109 |
{ |
| 110 |
$photos = $property->_photo_urls; |
| 111 |
if ( isset($photos[$image_number-1]) ) |
| 112 |
{ |
| 113 |
$url = $photos[$image_number-1]['url']; |
| 114 |
} |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 119 |
|
| 120 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 121 |
{ |
| 122 |
$url = wp_get_attachment_image_src( $gallery_attachment_ids[$image_number-1], $settings['image_size'] ); |
| 123 |
$url = $url[0]; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
// convert ratio to percentage |
| 128 |
$numbers = explode(':', $output_ratio); |
| 129 |
$percent = ( ( (int)$numbers[1] / (int)$numbers[0] ) * 100 ) . '%'; |
| 130 |
|
| 131 |
echo '<div style="background:url(' . esc_url($url) . ') no-repeat center center; background-size:cover; padding-bottom:' . esc_attr($percent) . '">'; |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
// output <img> |
| 136 |
if ( get_option('propertyhive_images_stored_as', '') == 'urls' ) |
| 137 |
{ |
| 138 |
$photos = $property->_photo_urls; |
| 139 |
if ( isset($photos[$image_number-1]) ) |
| 140 |
{ |
| 141 |
echo '<img src="' . esc_url($photos[$image_number-1]['url']) . '" alt="">'; |
| 142 |
} |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
$gallery_attachment_ids = $property->get_gallery_attachment_ids(); |
| 147 |
|
| 148 |
if ( isset($gallery_attachment_ids[$image_number-1]) ) |
| 149 |
{ |
| 150 |
echo wp_get_attachment_image( $gallery_attachment_ids[$image_number-1], $settings['image_size'] ); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
do_action( 'propertyhive_bricks_builder_widget_property_image_render_after', $settings, $property ); |
| 156 |
|
| 157 |
echo '</div>'; |
| 158 |
} |
| 159 |
} |