PluginProbe
Bricksable for Bricks Builder / 1.5.2
Bricksable for Bricks Builder v1.5.2
1.2.9 1.3.0 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.2 1.4.3 1.4.31 1.4.32 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 All 132 releases
bricksable / includes / elements / random-image / element-random-image.php

element-random-image.php in Bricksable for Bricks Builder 1.5.2, at includes/elements/random-image/element-random-image.php

353 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 class Bricksable_Random_Image extends \Bricks\Element {
8
9 public $category = 'bricksable';
10 public $name = 'random-image';
11 public $icon = 'ti-image';
12 public $custom_attributes = false;
13
14 public function get_label() {
15 return esc_html__( 'Random Image', 'bricksable' );
16 }
17 public function set_control_groups() {
18 $this->control_groups['image'] = array(
19 'title' => esc_html__( 'Image', 'bricksable' ),
20 'tab' => 'content',
21 );
22 unset( $this->control_groups['_typography'] );
23 }
24
25 public function set_controls() {
26 // Apply CSS filters only to img tag.
27 $this->controls['_cssFilters']['css'] = array(
28 array(
29 'selector' => '&:is(img)',
30 'property' => 'filter',
31 ),
32 array(
33 'selector' => 'img',
34 'property' => 'filter',
35 ),
36 );
37
38 $this->controls['_typography']['css'][0]['selector'] = 'figcaption';
39
40 // Images.
41 $this->controls['randomImageGallery'] = array(
42 'tab' => 'content',
43 'label' => esc_html__( 'Random Image gallery', 'bricksable' ),
44 'type' => 'image-gallery',
45 );
46
47 // @since 1.4.
48 $this->controls['tag'] = array(
49 'tab' => 'content',
50 'label' => esc_html__( 'HTML tag', 'bricksable' ),
51 'type' => 'select',
52 'options' => array(
53 'figure' => 'figure',
54 'picture' => 'picture',
55 'div' => 'div',
56 'custom' => esc_html__( 'Custom', 'bricksable' ),
57 ),
58 'lowercase' => true,
59 'inline' => true,
60 'placeholder' => '-',
61 );
62
63 $this->controls['customTag'] = array(
64 'tab' => 'content',
65 'label' => esc_html__( 'Custom tag', 'bricksable' ),
66 'type' => 'text',
67 'inline' => true,
68 'hasDynamicData' => false,
69 'placeholder' => 'div',
70 'required' => array( 'tag', '=', 'custom' ),
71 );
72
73 $this->controls['stretch'] = array(
74 'tab' => 'content',
75 'group' => 'image',
76 'label' => esc_html__( 'Stretch', 'bricksable' ),
77 'type' => 'checkbox',
78 'css' => array(
79 array(
80 'property' => 'width',
81 'value' => '100%',
82 ),
83 ),
84 );
85
86 // Link To.
87 $this->controls['linkToSeparator'] = array(
88 'tab' => 'content',
89 'group' => 'image',
90 'type' => 'separator',
91 'label' => esc_html__( 'Link To', 'bricksable' ),
92 );
93
94 $this->controls['link'] = array(
95 'tab' => 'content',
96 'group' => 'image',
97 'type' => 'select',
98 'options' => array(
99 'lightbox' => esc_html__( 'Lightbox', 'bricksable' ),
100 'attachment' => esc_html__( 'Attachment Page', 'bricksable' ),
101 'media' => esc_html__( 'Media File', 'bricksable' ),
102 'custom' => esc_html__( 'Custom URL', 'bricksable' ),
103 ),
104 'placeholder' => esc_html__( 'None', 'bricksable' ),
105 );
106
107 $this->controls['newTab'] = array(
108 'tab' => 'content',
109 'group' => 'image',
110
111 'label' => esc_html__( 'Open in new tab', 'bricksable' ),
112 'type' => 'checkbox',
113 'required' => array( 'link', '=', array( 'attachment', 'media' ) ),
114 );
115
116 $this->controls['url'] = array(
117 'tab' => 'content',
118 'group' => 'image',
119 'label' => esc_html__( 'Link type', 'bricksable' ),
120 'type' => 'link',
121 'required' => array( 'link', '=', 'url' ),
122 );
123
124 $this->controls['linkCustom'] = array(
125 'tab' => 'content',
126 'group' => 'image',
127 'label' => esc_html__( 'Custom links', 'bricksable' ),
128 'type' => 'repeater',
129 'fields' => array(
130 'link' => array(
131 'label' => esc_html__( 'Link', 'bricksable' ),
132 'type' => 'link',
133 'exclude' => array(
134 'lightboxImage',
135 'lightboxVideo',
136 ),
137 ),
138 ),
139 'placeholder' => esc_html__( 'Custom link', 'bricksable' ),
140 'required' => array( 'link', '=', 'custom' ),
141 );
142 }
143
144 public function enqueue_scripts() {
145 if ( isset( $this->settings['link'] ) && 'lightbox' === $this->settings['link'] ) {
146 wp_enqueue_script( 'bricks-photoswipe' );
147 wp_enqueue_style( 'bricks-photoswipe' );
148 }
149 }
150
151 public function get_random_image_settings( $settings ) {
152 $items = isset( $settings['randomImageGallery'] ) ? $settings['randomImageGallery'] : array();
153
154 $size = ! empty( $items['size'] ) ? $items['size'] : BRICKS_DEFAULT_IMAGE_SIZE;
155
156 // Dynamic Data.
157 if ( ! empty( $items['useDynamicData'] ) ) {
158 $items['images'] = array();
159
160 $images = $this->render_dynamic_data_tag( $items['useDynamicData'], 'image' );
161
162 if ( is_array( $images ) ) {
163 foreach ( $images as $image_id ) {
164 $items['images'][] = array(
165 'id' => $image_id,
166 'full' => wp_get_attachment_image_url( $image_id, 'full' ),
167 'url' => wp_get_attachment_image_url( $image_id, $size ),
168 );
169 }
170 }
171 }
172
173 // Either empty OR old data structure used before 1.0 (images were saved as one array directly on $items).
174 if ( ! isset( $items['images'] ) ) {
175 $images = ! empty( $items ) ? $items : array();
176
177 unset( $items );
178
179 $items['images'] = $images;
180 }
181
182 // Get 'size' from first image if not set (previous to 1.4-RC).
183 $first_image_size = ! empty( $items['images'][0]['size'] ) ? $items['images'][0]['size'] : false;
184 $size = empty( $items['size'] ) && $first_image_size ? $first_image_size : $size;
185
186 // Calculate new image URL if size is not the same as from the Media Library.
187 if ( $first_image_size && $first_image_size !== $size ) {
188 foreach ( $items['images'] as $key => $image ) {
189 $items['images'][ $key ]['size'] = $size;
190 $items['images'][ $key ]['url'] = wp_get_attachment_image_url( $image['id'], $size );
191 }
192 }
193
194 $settings['items'] = $items;
195
196 $settings['items']['size'] = $size;
197
198 return $settings;
199 }
200
201 public function render() {
202 $settings = $this->settings;
203 $image_settings = $this->get_random_image_settings( $this->settings );
204 $images = ! empty( $image_settings['items']['images'] ) ? $image_settings['items']['images'] : false;
205 $size = ! empty( $image_settings['items']['size'] ) ? $image_settings['items']['size'] : BRICKS_DEFAULT_IMAGE_SIZE;
206 $link_to = ! empty( $settings['link'] ) ? $settings['link'] : false;
207 $has_html_tag = isset( $settings['tag'] );
208
209 // Return placeholder.
210 if ( ! $images ) {
211 if ( ! empty( $settings['items']['useDynamicData'] ) ) {
212 if ( BRICKS_DB_TEMPLATE_SLUG !== get_post_type( $this->post_id ) ) {
213 return $this->render_element_placeholder(
214 array(
215 'title' => esc_html__( 'Dynamic data is empty.', 'bricks' ),
216 )
217 );
218 }
219 } else {
220 return $this->render_element_placeholder(
221 array(
222 'title' => esc_html__( 'No image selected.', 'bricks' ),
223 )
224 );
225 }
226 }
227
228 // Check: Global classes on Image element for '_gradient' setting.
229 $element_global_classes = ! empty( $settings['_cssGlobalClasses'] ) ? $settings['_cssGlobalClasses'] : false;
230
231 if ( ! $has_html_tag && is_array( $element_global_classes ) ) {
232 $all_global_classes = Database::$global_data['globalClasses'];
233
234 foreach ( $element_global_classes as $element_global_class ) {
235 $index = array_search( $element_global_class, array_column( $all_global_classes, 'id' ), true );
236 $global_class = ! empty( $all_global_classes[ $index ] ) ? $all_global_classes[ $index ] : false;
237
238 // Global class has 'gradient' setting: Add HTML tag to Image element to make ::before work.
239 if ( $global_class && strpos( wp_json_encode( $global_class ), '_gradient' ) ) {
240 $has_html_tag = true;
241 }
242 }
243 }
244
245 /**
246 * Render: Wrap 'img' HTML tag in HTML tag (if 'tag' set) or anchor tag (if 'link' set)
247 */
248 $output = '';
249
250 // Add _root attributes to outermost tag.
251 if ( $has_html_tag ) {
252 $this->set_attribute( '_root', 'class', 'tag brxe-image ' );
253
254 $output .= "<{$this->tag} {$this->render_attributes( '_root' )}>";
255 }
256
257 if ( $images ) {
258 foreach ( $images as $index => $item ) {
259 $rand_keys = array_rand( $images, 1 );
260 $image = $images[ $rand_keys ];
261 $image_atts['id'] = 'image-' . $image['id'];
262 $close_a_tag = false;
263
264 if ( $link_to ) {
265 if ( isset( $settings['newTab'] ) ) {
266 $this->set_attribute( 'link', 'target', '_blank' );
267 }
268 if ( 'attachment' === $link_to && isset( $image['id'] ) ) {
269 $close_a_tag = true;
270 $this->set_attribute( 'link', 'href', get_permalink( $image['id'] ) );
271 } elseif ( 'media' === $link_to ) {
272 $close_a_tag = true;
273 $this->set_attribute( 'link', 'href', wp_get_attachment_url( $image['id'] ) );
274 } elseif ( 'custom' === $link_to && isset( $settings['linkCustom'][ $index ]['link'] ) ) {
275 $close_a_tag = true;
276 $this->set_link_attributes( "a-$index", $settings['linkCustom'][ $index ]['link'] );
277 }
278 if ( 'custom' === $link_to ) {
279 $output .= "<a {$this->render_attributes( "a-$index" )}>";
280 } else {
281 $output .= "<a {$this->render_attributes( 'link' )}>";
282 }
283 }
284
285 // Render.
286 $image_atts = array();
287 $img_classes = array( 'post-thumbnail', 'image' );
288 if ( isset( $size ) ) {
289 $img_classes[] = 'size-' . $size;
290 }
291 if ( ! $has_html_tag ) {
292 $img_classes[] = $this->attributes['_root']['class'][0];
293 }
294
295 if ( isset( $settings['link'] ) && 'lightbox' === $settings['link'] ) {
296 $img_classes[] = 'bricks-lightbox';
297 $image_src = $image['id'] ? wp_get_attachment_image_src( $image['id'], $size ) : array(
298 \Bricks\Builder::get_template_placeholder_image(),
299 800,
300 600,
301 );
302 $image_atts['data-bricks-lightbox-source'] = $image_src[0];
303 $image_atts['data-bricks-lightbox-width'] = $image_src[1];
304 $image_atts['data-bricks-lightbox-height'] = $image_src[2];
305 $image_atts['data-bricks-lightbox-index'] = $index;
306 $image_atts['data-bricks-lightbox-id'] = $this->id;
307 }
308 $image_atts['class'] = join( ' ', $img_classes );
309
310 if ( ! $has_html_tag ) {
311
312 $this->set_attribute( 'img', 'class', 'css-filter' );
313
314 foreach ( $this->attributes['_root'] as $key => $value ) {
315 $image_attributes[ $key ] = is_array( $value ) ? join( ' ', $value ) : $value;
316 }
317
318 foreach ( $this->attributes['img'] as $key => $value ) {
319 if ( isset( $image_attributes[ $key ] ) ) {
320 $image_attributes[ $key ] .= ' ' . ( is_array( $value ) ? join( ' ', $value ) : $value );
321 } else {
322 $image_attributes[ $key ] = is_array( $value ) ? join( ' ', $value ) : $value;
323 }
324 }
325 // Merge custom attributes with img attributes.
326 $custom_attributes = $this->get_custom_attributes( $settings );
327 $image_attributes = array_merge( $image_attributes, $custom_attributes, $image_atts );
328 $output .= wp_get_attachment_image( $image['id'], $size, false, $image_attributes );
329 } else {
330 $output .= wp_get_attachment_image(
331 $image['id'],
332 $size,
333 false,
334 $image_atts
335 );
336 }
337 if ( $close_a_tag ) {
338 $output .= '</a>';
339 }
340 break;
341 }
342 } else {
343 esc_html_e( 'No image(s) selected.', 'bricksable' );
344 }
345
346 if ( $has_html_tag ) {
347 $output .= "</{$this->tag}>";
348 }
349 //phpcs:ignore
350 echo $output;
351 }
352 }
353