PluginProbe
Bricksable for Bricks Builder / 1.5.3
Bricksable for Bricks Builder v1.5.3
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.3, at includes/elements/random-image/element-random-image.php

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