| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; // Exit if accessed directly. |
| 5 |
} |
| 6 |
class Bricksable_Image_Scroller extends \Bricks\Element { |
| 7 |
|
| 8 |
public $category = 'bricksable'; |
| 9 |
public $name = 'ba-image-scroller'; |
| 10 |
public $icon = 'ti-image'; |
| 11 |
public $css_selector = 'img'; |
| 12 |
public $scripts = array( 'bricksableImageScroller' ); |
| 13 |
|
| 14 |
|
| 15 |
public function get_label() { |
| 16 |
return esc_html__( 'Image Scroller', 'bricksable' ); |
| 17 |
} |
| 18 |
|
| 19 |
public function set_control_groups() { |
| 20 |
$this->control_groups['settings'] = array( |
| 21 |
'title' => esc_html__( 'Settings', 'bricksable' ), |
| 22 |
'tab' => 'content', |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
public function set_controls() { |
| 27 |
|
| 28 |
$this->controls['_margin']['css'][0]['selector'] = ''; |
| 29 |
$this->controls['_margin']['placeholder'] = array( |
| 30 |
'top' => 0, |
| 31 |
'right' => 'auto', |
| 32 |
'bottom' => 0, |
| 33 |
'left' => 'auto', |
| 34 |
); |
| 35 |
|
| 36 |
$this->controls['_typography']['css'][0]['selector'] = 'figure > *'; |
| 37 |
$this->controls['_gradient']['css'][0]['selector'] = '.image-wrapper'; |
| 38 |
|
| 39 |
$this->controls['image'] = array( |
| 40 |
'tab' => 'content', |
| 41 |
'type' => 'image', |
| 42 |
); |
| 43 |
|
| 44 |
$this->controls['altText'] = array( |
| 45 |
'tab' => 'content', |
| 46 |
'label' => esc_html__( 'Custom alt text', 'bricksable' ), |
| 47 |
'type' => 'text', |
| 48 |
'inline' => true, |
| 49 |
'required' => array( 'image', '!=', '' ), |
| 50 |
); |
| 51 |
|
| 52 |
$this->controls['stretch'] = array( |
| 53 |
'tab' => 'content', |
| 54 |
'label' => esc_html__( 'Stretch', 'bricksable' ), |
| 55 |
'type' => 'checkbox', |
| 56 |
); |
| 57 |
|
| 58 |
// Link To. |
| 59 |
|
| 60 |
$this->controls['linkToSeparator'] = array( |
| 61 |
'tab' => 'content', |
| 62 |
'type' => 'separator', |
| 63 |
'label' => esc_html__( 'Link To', 'bricksable' ), |
| 64 |
); |
| 65 |
|
| 66 |
$this->controls['link'] = array( |
| 67 |
'tab' => 'content', |
| 68 |
'type' => 'select', |
| 69 |
'options' => array( |
| 70 |
'lightbox' => esc_html__( 'Lightbox', 'bricksable' ), |
| 71 |
'attachment' => esc_html__( 'Attachment Page', 'bricksable' ), |
| 72 |
'media' => esc_html__( 'Media File', 'bricksable' ), |
| 73 |
'url' => esc_html__( 'Other (URL)', 'bricksable' ), |
| 74 |
), |
| 75 |
'placeholder' => esc_html__( 'None', 'bricksable' ), |
| 76 |
); |
| 77 |
|
| 78 |
$this->controls['url'] = array( |
| 79 |
'tab' => 'content', |
| 80 |
'label' => esc_html__( 'Link type', 'bricksable' ), |
| 81 |
'type' => 'link', |
| 82 |
'required' => array( 'link', '=', 'url' ), |
| 83 |
); |
| 84 |
|
| 85 |
$this->controls['popupOverlay'] = array( |
| 86 |
'tab' => 'content', |
| 87 |
'label' => esc_html__( 'Image overlay', 'bricksable' ), |
| 88 |
'type' => 'color', |
| 89 |
'css' => array( |
| 90 |
array( |
| 91 |
'property' => 'background-color', |
| 92 |
'selector' => '.image-overlay:after', |
| 93 |
), |
| 94 |
), |
| 95 |
'inline' => true, |
| 96 |
'small' => true, |
| 97 |
'required' => array( 'link', '!=', '' ), |
| 98 |
); |
| 99 |
|
| 100 |
// Icon |
| 101 |
|
| 102 |
$this->controls['popupSeparator'] = array( |
| 103 |
'tab' => 'content', |
| 104 |
'label' => esc_html__( 'Icon', 'bricksable' ), |
| 105 |
'type' => 'separator', |
| 106 |
'inline' => true, |
| 107 |
'small' => true, |
| 108 |
'required' => array( 'link', '!=', '' ), |
| 109 |
); |
| 110 |
|
| 111 |
// To hide icon for specific elements when image icon set in styles |
| 112 |
$this->controls['popupIconDisable'] = array( |
| 113 |
'tab' => 'content', |
| 114 |
'label' => esc_html__( 'Disable icon', 'bricksable' ), |
| 115 |
'type' => 'checkbox', |
| 116 |
'description' => esc_html__( 'Precedes: "Theme Styles > Element - Image"', 'bricksable' ), |
| 117 |
// 'required' => ['link', '!=', ''], |
| 118 |
); |
| 119 |
|
| 120 |
$this->controls['popupIcon'] = array( |
| 121 |
'tab' => 'content', |
| 122 |
'label' => esc_html__( 'Icon', 'bricksable' ), |
| 123 |
'type' => 'icon', |
| 124 |
'inline' => true, |
| 125 |
'small' => true, |
| 126 |
'required' => array( 'link', '!=', '' ), |
| 127 |
); |
| 128 |
|
| 129 |
// NOTE: Set popup CSS control outside of control 'link' (CSS is not applied to nested controls) |
| 130 |
$this->controls['popupIconBackgroundColor'] = array( |
| 131 |
'tab' => 'content', |
| 132 |
'label' => esc_html__( 'Icon background color', 'bricksable' ), |
| 133 |
'type' => 'color', |
| 134 |
'css' => array( |
| 135 |
array( |
| 136 |
'property' => 'background-color', |
| 137 |
'selector' => '.popup-icon', |
| 138 |
), |
| 139 |
), |
| 140 |
'inline' => true, |
| 141 |
'small' => true, |
| 142 |
'required' => array( 'link', '!=', '' ), |
| 143 |
); |
| 144 |
|
| 145 |
$this->controls['popupIconBorder'] = array( |
| 146 |
'tab' => 'content', |
| 147 |
'label' => esc_html__( 'Icon border', 'bricksable' ), |
| 148 |
'type' => 'border', |
| 149 |
'css' => array( |
| 150 |
array( |
| 151 |
'property' => 'border', |
| 152 |
'selector' => '.popup-icon', |
| 153 |
), |
| 154 |
), |
| 155 |
'inline' => true, |
| 156 |
'small' => true, |
| 157 |
'required' => array( 'link', '!=', '' ), |
| 158 |
); |
| 159 |
|
| 160 |
$this->controls['popupIconBoxShadow'] = array( |
| 161 |
'tab' => 'content', |
| 162 |
'label' => esc_html__( 'Icon box shadow', 'bricksable' ), |
| 163 |
'type' => 'box-shadow', |
| 164 |
'css' => array( |
| 165 |
array( |
| 166 |
'property' => 'box-shadow', |
| 167 |
'selector' => '.popup-icon', |
| 168 |
), |
| 169 |
), |
| 170 |
'inline' => true, |
| 171 |
'small' => true, |
| 172 |
'required' => array( 'link', '!=', '' ), |
| 173 |
); |
| 174 |
|
| 175 |
$this->controls['popupIconHeight'] = array( |
| 176 |
'tab' => 'content', |
| 177 |
'label' => esc_html__( 'Icon height', 'bricksable' ), |
| 178 |
'type' => 'number', |
| 179 |
'unit' => 'px', |
| 180 |
'css' => array( |
| 181 |
array( |
| 182 |
'property' => 'line-height', |
| 183 |
'selector' => '.popup-icon', |
| 184 |
), |
| 185 |
), |
| 186 |
'inline' => true, |
| 187 |
'small' => true, |
| 188 |
'required' => array( 'link', '!=', '' ), |
| 189 |
); |
| 190 |
|
| 191 |
$this->controls['popupIconWidth'] = array( |
| 192 |
'tab' => 'content', |
| 193 |
'label' => esc_html__( 'Icon width', 'bricksable' ), |
| 194 |
'type' => 'number', |
| 195 |
'unit' => 'px', |
| 196 |
'css' => array( |
| 197 |
array( |
| 198 |
'property' => 'width', |
| 199 |
'selector' => '.popup-icon', |
| 200 |
), |
| 201 |
), |
| 202 |
'inline' => true, |
| 203 |
'small' => true, |
| 204 |
'required' => array( 'link', '!=', '' ), |
| 205 |
); |
| 206 |
|
| 207 |
$this->controls['popupIconTypography'] = array( |
| 208 |
'tab' => 'content', |
| 209 |
'label' => esc_html__( 'Icon typography', 'bricksable' ), |
| 210 |
'type' => 'typography', |
| 211 |
'exclude' => array( |
| 212 |
'font-family', |
| 213 |
'font-weight', |
| 214 |
'text-align', |
| 215 |
'text-transform', |
| 216 |
'line-height', |
| 217 |
'letter-spacing', |
| 218 |
), |
| 219 |
'css' => array( |
| 220 |
array( |
| 221 |
'property' => 'font', |
| 222 |
'selector' => '.popup-icon', |
| 223 |
), |
| 224 |
), |
| 225 |
'inline' => true, |
| 226 |
'small' => true, |
| 227 |
'required' => array( 'link', '!=', '' ), |
| 228 |
); |
| 229 |
|
| 230 |
// Caption |
| 231 |
|
| 232 |
$this->controls['captionSeparator'] = array( |
| 233 |
'tab' => 'content', |
| 234 |
'type' => 'separator', |
| 235 |
'label' => esc_html__( 'Caption', 'bricksable' ), |
| 236 |
); |
| 237 |
|
| 238 |
$this->controls['caption'] = array( |
| 239 |
'tab' => 'content', |
| 240 |
'label' => esc_html__( 'Caption Type', 'bricksable' ), |
| 241 |
'type' => 'select', |
| 242 |
'options' => array( |
| 243 |
'none' => esc_html__( 'No caption', 'bricksable' ), |
| 244 |
'attachment' => esc_html__( 'Attachment', 'bricksable' ), |
| 245 |
'custom' => esc_html__( 'Custom', 'bricksable' ), |
| 246 |
), |
| 247 |
'inline' => true, |
| 248 |
'placeholder' => esc_html__( 'Select', 'bricksable' ), |
| 249 |
'description' => esc_html__( 'Default: "Theme Styles > Element - Image"', 'bricksable' ), |
| 250 |
); |
| 251 |
|
| 252 |
$this->controls['captionCustom'] = array( |
| 253 |
'tab' => 'content', |
| 254 |
'label' => esc_html__( 'Custom caption', 'bricksable' ), |
| 255 |
'type' => 'text', |
| 256 |
'placeholder' => esc_html__( 'Here goes your caption ...', 'bricksable' ), |
| 257 |
'required' => array( 'caption', '=', 'custom' ), |
| 258 |
); |
| 259 |
|
| 260 |
// Image Scroller Settings. |
| 261 |
$this->controls['scroller_height'] = array( |
| 262 |
'tab' => 'content', |
| 263 |
'group' => 'settings', |
| 264 |
'label' => esc_html__( 'Height', 'bricksable' ), |
| 265 |
'type' => 'slider', |
| 266 |
'css' => array( |
| 267 |
array( |
| 268 |
'property' => 'height', |
| 269 |
'selector' => '.image-wrapper', |
| 270 |
), |
| 271 |
array( |
| 272 |
'property' => 'transform', |
| 273 |
'selector' => ':hover .image-wrapper img', |
| 274 |
'value' => 'translateY(calc(-100% + ' . $this->settings['scroller_height'] . '))', |
| 275 |
), |
| 276 |
), |
| 277 |
'units' => array( |
| 278 |
'px' => array( |
| 279 |
'min' => 1, |
| 280 |
'max' => 1600, |
| 281 |
'step' => 1, |
| 282 |
), |
| 283 |
'vh' => array( |
| 284 |
'min' => 1, |
| 285 |
'max' => 100, |
| 286 |
'step' => 1, |
| 287 |
), |
| 288 |
), |
| 289 |
'default' => '300px', |
| 290 |
'placeholder' => esc_html__( '300px', 'bricksable' ), |
| 291 |
'description' => esc_html__( "Adjust the Scroller container's height.", 'bricksable' ), |
| 292 |
); |
| 293 |
|
| 294 |
$this->controls['scroll_direction'] = array( |
| 295 |
'tab' => 'content', |
| 296 |
'group' => 'settings', |
| 297 |
'label' => esc_html__( 'Scroll Direction', 'bricksable' ), |
| 298 |
'type' => 'select', |
| 299 |
'options' => array( |
| 300 |
'vertical' => esc_html__( 'Vertical', 'bricksable' ), |
| 301 |
'horizontal' => esc_html__( 'Horizontal', 'bricksable' ), |
| 302 |
), |
| 303 |
'inline' => true, |
| 304 |
'placeholder' => esc_html__( 'Vertical', 'bricksable' ), |
| 305 |
); |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
public function enqueue_scripts() { |
| 310 |
if ( '' === get_option( 'bricksable_combine_minify_js' ) || false === get_option( 'bricksable_combine_minify_js' ) ) { |
| 311 |
wp_enqueue_script( 'ba-image-scroller' ); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
public function get_normalized_image_settings( $settings ) { |
| 317 |
if ( ! isset( $settings['image'] ) ) { |
| 318 |
$settings['image'] = array( |
| 319 |
'id' => 0, |
| 320 |
'url' => '', |
| 321 |
); |
| 322 |
return $settings; |
| 323 |
} |
| 324 |
|
| 325 |
$image = $settings['image']; |
| 326 |
|
| 327 |
if ( isset( $image['useDynamicData']['name'] ) ) { |
| 328 |
$images = $this->render_dynamic_data_tag( $image['useDynamicData']['name'] ); |
| 329 |
$image['id'] = empty( $images ) ? 0 : $images[0]; |
| 330 |
} else { |
| 331 |
$image['id'] = isset( $image['id'] ) ? $image['id'] : 0; |
| 332 |
} |
| 333 |
|
| 334 |
// Image Size |
| 335 |
$image['size'] = isset( $image['size'] ) ? $settings['image']['size'] : BRICKS_DEFAULT_IMAGE_SIZE; |
| 336 |
|
| 337 |
// Image URL |
| 338 |
if ( ! isset( $image['url'] ) ) { |
| 339 |
$image['url'] = wp_get_attachment_image_url( $image['id'], $image['size'] ); |
| 340 |
} |
| 341 |
|
| 342 |
$settings['image'] = $image; |
| 343 |
|
| 344 |
return $settings; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
// NOTE: Use WP function 'wp_get_attachment_image' to render image (built-in responsive image implementation) |
| 349 |
public function render() { |
| 350 |
$settings = $this->settings; |
| 351 |
$settings = $this->get_normalized_image_settings( $settings ); |
| 352 |
|
| 353 |
//echo $this->generate_css_string_from_element(); |
| 354 |
|
| 355 |
// Dynamic Data is empty |
| 356 |
if ( isset( $settings['image']['useDynamicData']['name'] ) ) { |
| 357 |
|
| 358 |
if ( empty( $settings['image']['id'] ) ) { |
| 359 |
|
| 360 |
if ( 'ACF' === $settings['image']['useDynamicData']['group'] && ! class_exists( 'ACF' ) ) { |
| 361 |
$message = esc_html__( 'Can\'t render element, as the selected ACF field is not available. Please activate ACF or edit the element to select different data.', 'bricksable' ); |
| 362 |
} elseif ( '{featured_image}' == $settings['image']['useDynamicData']['name'] ) { |
| 363 |
$message = esc_html__( 'No featured image set.', 'bricksable' ); |
| 364 |
} else { |
| 365 |
$message = esc_html__( 'Dynamic Data %1$s (%2$s) is empty.', 'bricksable' ); |
| 366 |
} |
| 367 |
|
| 368 |
return $this->render_element_placeholder( |
| 369 |
array( |
| 370 |
'icon-class' => 'ti-image', |
| 371 |
'text' => sprintf( |
| 372 |
$message, |
| 373 |
$settings['image']['useDynamicData']['label'], |
| 374 |
$settings['image']['useDynamicData']['group'] |
| 375 |
), |
| 376 |
) |
| 377 |
); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
// Image id is empty or doesn't exist |
| 382 |
else { |
| 383 |
|
| 384 |
// No image |
| 385 |
if ( empty( $settings['image']['id'] ) ) { |
| 386 |
return $this->render_element_placeholder( |
| 387 |
array( |
| 388 |
'icon-class' => 'ti-image', |
| 389 |
'text' => esc_html__( 'No image selected.', 'bricksable' ), |
| 390 |
) |
| 391 |
); |
| 392 |
} |
| 393 |
|
| 394 |
// Return if image ID does not exist |
| 395 |
if ( ! wp_get_attachment_image_src( $settings['image']['id'] ) ) { |
| 396 |
return $this->render_element_placeholder( |
| 397 |
array( |
| 398 |
'icon-class' => 'ti-image', |
| 399 |
'text' => sprintf( esc_html__( 'Image ID (%s) no longer exist. Please select another image.', 'bricksable' ), $settings['image']['id'] ), |
| 400 |
) |
| 401 |
); |
| 402 |
} |
| 403 |
} |
| 404 |
|
| 405 |
// Render |
| 406 |
$image_atts = array(); |
| 407 |
$image_atts['id'] = 'image-' . $settings['image']['id']; |
| 408 |
|
| 409 |
// Check for alternartive "Alt Text" setting |
| 410 |
if ( isset( $settings['altText'] ) ) { |
| 411 |
$image_atts['alt'] = esc_attr( $settings['altText'] ); |
| 412 |
} |
| 413 |
|
| 414 |
$image_wrapper_classes = array( 'image-wrapper', 'ba-image-scroller-' . $settings['scroll_direction'] ); |
| 415 |
$img_classes = array( 'post-thumbnail', 'css-filter' ); |
| 416 |
|
| 417 |
if ( isset( $settings['stretch'] ) ) { |
| 418 |
$img_classes[] = 'stretch'; |
| 419 |
} |
| 420 |
|
| 421 |
if ( isset( $settings['link'] ) ) { |
| 422 |
$image_wrapper_classes[] = 'image-overlay'; |
| 423 |
} |
| 424 |
|
| 425 |
if ( isset( $settings['link'] ) && $settings['link'] === 'lightbox' ) { |
| 426 |
$img_classes[] = 'bricks-lightbox'; |
| 427 |
|
| 428 |
$image_src = $settings['image']['id'] ? wp_get_attachment_image_src( $settings['image']['id'], 'full' ) : array( |
| 429 |
\Bricks\Builder::get_template_placeholder_image(), |
| 430 |
800, |
| 431 |
600, |
| 432 |
); |
| 433 |
|
| 434 |
$image_atts['data-bricks-lightbox-source'] = $image_src[0]; |
| 435 |
$image_atts['data-bricks-lightbox-width'] = $image_src[1]; |
| 436 |
$image_atts['data-bricks-lightbox-height'] = $image_src[2]; |
| 437 |
} |
| 438 |
|
| 439 |
$img_classes[] = 'size-' . $settings['image']['size']; |
| 440 |
$image_atts['class'] = join( ' ', $img_classes ); |
| 441 |
|
| 442 |
$show_caption = isset( $settings['caption'] ) ? $settings['caption'] : 'attachment'; |
| 443 |
|
| 444 |
if ( $show_caption === 'none' ) { |
| 445 |
$image_caption = false; |
| 446 |
} elseif ( $show_caption === 'custom' && isset( $settings['captionCustom'] ) ) { |
| 447 |
$image_caption = trim( $settings['captionCustom'] ); |
| 448 |
} else { |
| 449 |
$image_data = get_post( $settings['image']['id'] ); |
| 450 |
$image_caption = $image_data ? $image_data->post_excerpt : ''; |
| 451 |
} |
| 452 |
|
| 453 |
if ( $image_caption ) { |
| 454 |
echo '<figure>'; |
| 455 |
} |
| 456 |
|
| 457 |
$this->set_attribute( 'image-wrapper', 'class', $image_wrapper_classes ); |
| 458 |
|
| 459 |
echo '<div ' . $this->render_attributes( 'image-wrapper' ) . '>'; |
| 460 |
|
| 461 |
$close_a_tag = false; |
| 462 |
|
| 463 |
if ( isset( $settings['link'] ) && $settings['link'] === 'media' ) { |
| 464 |
$close_a_tag = true; |
| 465 |
|
| 466 |
echo '<a href="' . wp_get_attachment_url( $settings['image']['id'] ) . '" target="_blank">'; |
| 467 |
} |
| 468 |
|
| 469 |
if ( isset( $settings['link'] ) && $settings['link'] === 'attachment' ) { |
| 470 |
echo '<a href="' . get_permalink( $settings['image']['id'] ) . '" target="_blank">'; |
| 471 |
} |
| 472 |
|
| 473 |
if ( isset( $settings['link'] ) && $settings['link'] === 'url' && isset( $settings['url'] ) ) { |
| 474 |
$close_a_tag = true; |
| 475 |
|
| 476 |
// Link |
| 477 |
$this->set_link_attributes( 'a', $settings['url'] ); |
| 478 |
|
| 479 |
echo '<a ' . $this->render_attributes( 'a' ) . '>'; |
| 480 |
} |
| 481 |
|
| 482 |
// Show icon only if 'Link to' is set |
| 483 |
if ( ! isset( $settings['popupIconDisable'] ) && isset( $settings['link'] ) && isset( $settings['popupIcon']['icon'] ) ) { |
| 484 |
$icon_classes = array( 'popup-icon', $settings['popupIcon']['icon'] ); |
| 485 |
|
| 486 |
if ( isset( $settings['link'] ) && $settings['link'] === 'lightbox' ) { |
| 487 |
$icon_classes[] = 'lightbox'; |
| 488 |
} |
| 489 |
|
| 490 |
$this->set_attribute( 'popup-icon', 'class', $icon_classes ); |
| 491 |
|
| 492 |
echo '<i ' . $this->render_attributes( 'popup-icon' ) . '></i>'; |
| 493 |
} |
| 494 |
|
| 495 |
// Lazy load atts set via 'wp_get_attachment_image_attributes' filter |
| 496 |
if ( isset( $settings['image']['id'] ) ) { |
| 497 |
echo wp_get_attachment_image( $settings['image']['id'], $settings['image']['size'], false, $image_atts ); |
| 498 |
} elseif ( ! empty( $settings['image']['url'] ) ) { |
| 499 |
echo '<img src="' . $settings['image']['url'] . '">'; |
| 500 |
} |
| 501 |
|
| 502 |
if ( $close_a_tag ) { |
| 503 |
echo '</a>'; |
| 504 |
} |
| 505 |
|
| 506 |
echo '</div>'; |
| 507 |
|
| 508 |
if ( $image_caption ) { |
| 509 |
echo '<figcaption class="bricks-image-caption">' . esc_html( $image_caption ) . '</figcaption></figure>'; |
| 510 |
} |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
public function get_block_html( $settings ) { |
| 516 |
if ( ! isset( $settings['image'] ) ) { |
| 517 |
return; |
| 518 |
} |
| 519 |
|
| 520 |
$figure_classes = array( 'wp-block-image', 'size-' . $settings['image']['size'] ); |
| 521 |
|
| 522 |
if ( isset( $settings['_typography']['text-align'] ) ) { |
| 523 |
$figure_classes[] = 'align' . $settings['_typography']['text-align']; |
| 524 |
} |
| 525 |
|
| 526 |
$this->set_attribute( 'figure', 'class', $figure_classes ); |
| 527 |
|
| 528 |
$this->set_attribute( 'image', 'src', $settings['image']['url'] ); |
| 529 |
$this->set_attribute( 'image', 'alt', '' ); |
| 530 |
$this->set_attribute( 'image', 'class', 'wp-image-' . $settings['image']['id'] ); |
| 531 |
|
| 532 |
if ( isset( $settings['_width'] ) && strpos( $settings['_width'], 'px' ) !== false ) { |
| 533 |
$this->set_attribute( 'image', 'width', str_replace( 'px', '', $settings['_width'] ) ); |
| 534 |
} |
| 535 |
|
| 536 |
if ( isset( $settings['_height'] ) && strpos( $settings['_height'], 'px' ) !== false ) { |
| 537 |
$this->set_attribute( 'image', 'height', str_replace( 'px', '', $settings['_height'] ) ); |
| 538 |
} |
| 539 |
|
| 540 |
$block_html = '<figure ' . $this->render_attributes( 'figure' ) . '>'; |
| 541 |
|
| 542 |
if ( isset( $settings['link'] ) ) { |
| 543 |
if ( $settings['link'] === 'media' ) { |
| 544 |
$this->set_link_attributes( 'a', 'href', wp_get_attachment_url( $settings['image']['id'] ) ); |
| 545 |
} elseif ( isset( $settings['url'] ) ) { |
| 546 |
$this->set_link_attributes( 'a', $settings['url'] ); |
| 547 |
} |
| 548 |
|
| 549 |
$this->remove_attribute( 'a', 'class' ); |
| 550 |
|
| 551 |
$block_html .= '<a ' . $this->render_attributes( 'a' ) . '>'; |
| 552 |
} |
| 553 |
|
| 554 |
$block_html .= '<img ' . $this->render_attributes( 'image' ) . '>'; |
| 555 |
|
| 556 |
if ( isset( $settings['link'] ) ) { |
| 557 |
$block_html .= '</a>'; |
| 558 |
} |
| 559 |
|
| 560 |
$block_html .= '</figure>'; |
| 561 |
|
| 562 |
return $block_html; |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
public function convert_element_settings_to_block( $settings ) { |
| 567 |
if ( ! isset( $settings['image'] ) ) { |
| 568 |
return; |
| 569 |
} |
| 570 |
|
| 571 |
$settings = $this->get_normalized_image_settings( $settings ); |
| 572 |
|
| 573 |
$block = array( |
| 574 |
'blockName' => $this->block, |
| 575 |
'attrs' => array( |
| 576 |
'id' => $settings['image']['id'], |
| 577 |
'sizeSlug' => $settings['image']['size'], |
| 578 |
), |
| 579 |
'innerContent' => array(), |
| 580 |
); |
| 581 |
|
| 582 |
if ( isset( $settings['_typography']['text-align'] ) ) { |
| 583 |
$block['attrs']['align'] = $settings['_typography']['text-align']; |
| 584 |
} |
| 585 |
|
| 586 |
if ( isset( $settings['_width'] ) && strpos( $settings['_width'], 'px' ) !== false ) { |
| 587 |
$block['attrs']['width'] = intval( str_replace( 'px', '', $settings['_width'] ) ); |
| 588 |
} |
| 589 |
|
| 590 |
if ( isset( $settings['_height'] ) && strpos( $settings['_height'], 'px' ) !== false ) { |
| 591 |
$block['attrs']['height'] = intval( str_replace( 'px', '', $settings['_height'] ) ); |
| 592 |
} |
| 593 |
|
| 594 |
if ( isset( $settings['link'] ) ) { |
| 595 |
if ( $settings['link'] === 'media' ) { |
| 596 |
$block['attrs']['linkDestination'] = 'media'; |
| 597 |
} else { |
| 598 |
$block['attrs']['linkDestination'] = 'custom'; |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
$block['innerContent'] = array( $this->get_block_html( $settings ) ); |
| 603 |
|
| 604 |
return $block; |
| 605 |
} |
| 606 |
|
| 607 |
public function convert_block_to_element_settings( $block, $attributes ) { |
| 608 |
$element_settings = array(); |
| 609 |
|
| 610 |
$image_id = isset( $attributes['id'] ) ? intval( $attributes['id'] ) : 0; |
| 611 |
$image_size = isset( $attributes['sizeSlug'] ) ? $attributes['sizeSlug'] : 'large'; |
| 612 |
$image_url = wp_get_attachment_image_src( $image_id, $image_size ); |
| 613 |
|
| 614 |
if ( is_array( $image_url ) && isset( $image_url[0] ) ) { |
| 615 |
$image_url = $image_url[0]; |
| 616 |
} |
| 617 |
|
| 618 |
if ( $image_id && $image_url ) { |
| 619 |
$element_settings['image'] = array( |
| 620 |
'filename' => basename( get_attached_file( $image_id ) ), |
| 621 |
'full' => wp_get_attachment_image_src( $image_id, 'full' ), |
| 622 |
'id' => $image_id, |
| 623 |
'size' => $image_size, |
| 624 |
'url' => $image_url, |
| 625 |
); |
| 626 |
} |
| 627 |
|
| 628 |
return $element_settings; |
| 629 |
} |
| 630 |
|
| 631 |
} |
| 632 |
|