PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.2
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.2
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / blocks / includes / smart-image / Smart_Image.php

Smart_Image.php in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.2, at blocks/includes/smart-image/Smart_Image.php

248 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Smart image block class for Smart Post Show Blocks.
4 *
5 * @package Smart_Post_Show_Pro
6 * @subpackage Smart_Post_Show_Pro/blocks/includes
7 */
8
9 namespace SmartPostShow\Blocks;
10
11 use SmartPostShow\Blocks\Block_Base;
12 use WP_Error;
13 use WP_REST_Request;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Smart Image Block class
21 */
22 class Smart_Image extends Block_Base {
23
24 /**
25 * Set_block_properties
26 *
27 * @return void
28 */
29 protected function set_block_properties() {
30 $this->block_name = 'smart-image';
31 $this->styles = array(
32 'sp_smart_post_blocks_social_icons_style',
33 'pcp-font-awesome',
34 // 'sp_smart_post_blocks_css',
35 'pcp-likes',
36 'sp_smart_post_blocks_google_fonts',
37 // 'sp_smart_post_blocks_style',
38 );
39 }
40
41 /**
42 * Render Smart_Image block.
43 *
44 * @param array $attributes array $attributes - Block attributes.
45 * @param string $content string $content - Block content.
46 * @param array $blocks bocks.
47 *
48 * @return $content.
49 */
50 public function render_block( $attributes, $content = '', $blocks = array() ) {
51 $attributes['page_id'] = get_the_ID();
52 $sp_block_name = isset( $attributes['blockName'] ) ? $attributes['blockName'] : '';
53 $align = isset( $attributes['align'] ) ? $attributes['align'] : '';
54 $unique_id = isset( $attributes['uniqueId'] ) ? $attributes['uniqueId'] : '';
55 $hide_on_desktop = isset( $attributes['hideOnDesktop'] ) ? $attributes['hideOnDesktop'] : false;
56 $hide_on_tablet = isset( $attributes['hideOnTablet'] ) ? $attributes['hideOnTablet'] : false;
57 $hide_on_mobile = isset( $attributes['hideOnMobile'] ) ? $attributes['hideOnMobile'] : false;
58 $img_text_position = isset( $attributes['imgTextPosition'] ) ? $attributes['imgTextPosition'] : '';
59 $img_mask_enable = isset( $attributes['imgMaskingEnable'] ) ? $attributes['imgMaskingEnable'] : false;
60 $mask_shape_set = isset( $attributes['imageShapeSet'] ) ? $attributes['imageShapeSet'] : 'shape-set';
61 $select_mask_shape = isset( $attributes['selectImageShape'] ) ? $attributes['selectImageShape'] : 'original';
62 $select_image = isset( $attributes['selectImage'] ) ? $attributes['selectImage'] : array();
63 $select_img_size = isset( $attributes['imageSize'] ) ? $attributes['imageSize'] : 'full';
64 $lazy_load = isset( $attributes['lazyLoad'] ) ? $attributes['lazyLoad'] : false;
65 $aspect_ratio = isset( $attributes['aspectRatio'] ) ? $attributes['aspectRatio'] : 'original';
66 $img_animation = isset( $attributes['imgAnimationNormal'] ) ? $attributes['imgAnimationNormal'] : 'zoom-in';
67 $double_resolution_retina = isset( $attributes['doubleResolutionRetina'] ) ? $attributes['doubleResolutionRetina'] : false;
68 $enable_link = isset( $attributes['enableLink'] ) ? $attributes['enableLink'] : false;
69 $link_type = isset( $attributes['linkType'] ) ? $attributes['linkType'] : 'full-img';
70 $img_hover_overlay = isset( $attributes['imgHoverOverlayEnable'] ) ? $attributes['imgHoverOverlayEnable'] : false;
71 $link_btn_position = isset( $attributes['buttonPosition'] ) ? $attributes['buttonPosition'] : 'center-center';
72 $link_btn_label = isset( $attributes['buttonLabel'] ) ? $attributes['buttonLabel'] : '';
73 $btn_link_url = isset( $attributes['linkUrl'] ) ? $attributes['linkUrl'] : '';
74 $open_in_tab = ! empty( $attributes['openInTab'] ) ? '_blank' : '_self';
75 $img_alt_text = isset( $attributes['imageAltText'] ) ? $attributes['imageAltText'] : '';
76 $img_bg_enable = isset( $attributes['smartImgBgEnable'] ) ? $attributes['smartImgBgEnable'] : false;
77 $additional_class = isset( $attributes['additionalCssClass'] ) ? $attributes['additionalCssClass'] : false;
78 $img_alignment = isset( $attributes['imgAlignment'] ) ? $attributes['imgAlignment'] : 'left';
79
80 if ( empty( $select_image['url'] ) && empty( $select_image['insertUrl'] ) ) {
81 return;
82 }
83 $external_url = isset( $select_image['insertUrl'] ) ? $select_image['insertUrl'] : false;
84
85 unset( $attributes['dynamicCss'] );
86 unset( $attributes['fontListsEditPage'] );
87 unset( $attributes['fontLists'] );
88
89 $img_size_array = $select_image['media_details']['sizes'][ $select_img_size ] ?? array();
90
91 if ( ! isset( $img_size_array['source_url'] ) && ! isset( $select_image['url'] ) ) {
92 return '';
93 }
94
95 $visibility_class = ( $hide_on_desktop ? ' sp-hide-on-desktop ' : '' ) . ( $hide_on_tablet ? ' sp-hide-on-tablet ' : '' ) . ( $hide_on_mobile ? ' sp-hide-on-mobile' : '' );
96 $img_mask_class = $img_mask_enable ? ( 'custom' !== $mask_shape_set ? ' sp-' . $select_mask_shape : ' sp-custom-mask' ) : '';
97
98 $img_srcset_retina = null;
99
100 if ( ! empty( $select_image['srcset'] ) ) {
101 if ( ! $double_resolution_retina ) {
102 $img_srcset_retina = $select_image['srcset'];
103 } else {
104 // split into an array by commas.
105 $src_array = array_map( 'trim', explode( ',', $select_image['srcset'] ) );
106
107 // filter only sizes >= 600w.
108 $filtered = array_filter(
109 $src_array,
110 function ( $src ) {
111 preg_match( '/(\d+)w$/', $src, $matches );
112 $size = isset( $matches[1] ) ? (int) $matches[1] : 0;
113 return $size >= 600;
114 }
115 );
116
117 // join back into a string.
118 $img_srcset_retina = implode( ', ', $filtered );
119 }
120 }
121
122 $image_id = $select_image['id'] ?? '';
123 $image_width = $img_size_array['width'] ?? '';
124 $image_height = $img_size_array['height'] ?? '';
125 $image_source_url = $img_size_array['source_url'] ?? '';
126 $image_url = $select_image['url'] ?? '';
127
128 $img_wp_class = ! $external_url && count( $select_image ) > 0 ? 'wp-image-' . $image_id : '';
129 $img_class = 'sp-smart-image sp-image-ratio-' . $aspect_ratio . ' sp-' . $img_animation . ' ' . $img_wp_class;
130 $img_sizes_attr = ! $external_url ? '(max-width: ' . $image_width . 'px) 100vw, ' . $image_width . 'px' : '';
131
132 // Render the block content.
133 ob_start();
134 ?>
135 <div
136 id="<?php echo esc_attr( $unique_id ); ?>"
137 class="sp-smart-post-block-wrapper align<?php echo esc_attr( $align ); ?><?php echo esc_attr( $visibility_class ); ?> <?php echo esc_attr( $additional_class ); ?>"
138 >
139 <div class="sp-smart-post-smart-image">
140 <figure
141 class="sp-smart-image-wrapper sp-text-position-<?php echo esc_attr( $img_text_position ); ?>"
142 >
143 <div class="sp-smart-image-button sp-d-flex sp-justify-<?php echo esc_attr( $img_alignment ); ?>">
144 <div
145 class="sp-smart-post sp-smart-image-area<?php echo esc_attr( $img_bg_enable ? ' sp-has-bg' : '' ); ?> <?php echo esc_attr( $img_mask_class ); ?>"
146 >
147 <div class="sp-smart-image-wrapper sp-overflow-hidden">
148 <?php
149 if ( $enable_link && 'full-img' === $link_type ) {
150 echo '<a href="' . esc_url( $btn_link_url ) . '" rel="nofollow noreferrer" target="' . esc_attr( $open_in_tab ) . '">';
151 }
152 ?>
153 <img
154 fetchpriority="high"
155 decoding="async"
156 width="<?php echo ! $external_url ? esc_attr( $image_width ) : ''; ?>"
157 height="<?php echo ! $external_url ? esc_attr( $image_height ) : ''; ?>"
158 class="<?php echo esc_attr( $img_class ); ?>"
159 src="<?php echo esc_url( $image_source_url ?? $image_url ?? null ); ?>"
160 alt="<?php echo esc_attr( $img_alt_text ); ?>"
161 srcSet="<?php echo esc_attr( $img_srcset_retina ); ?>"
162 loading="<?php echo esc_attr( $lazy_load ? 'lazy' : 'eager' ); ?>"
163 sizes="<?php echo esc_attr( $img_sizes_attr ); ?>"
164 />
165 <?php if ( $img_hover_overlay ) : ?>
166 <div class="sp-hover-img-overlay sp-<?php echo esc_attr( $img_animation ); ?>"></div>
167 <?php endif; ?>
168 <?php
169 if ( $enable_link && 'full-img' === $link_type ) {
170 echo '</a>'; }
171 ?>
172 <?php if ( $enable_link && 'button' === $link_type ) : ?>
173 <div
174 class="sp-smart-image-link-btn-wrapper sp-position-<?php echo esc_attr( $link_btn_position ); ?>"
175 >
176 <a class="sp-smart-image-link-btn" rel="nofollow noreferrer" href="<?php echo esc_url( $btn_link_url ); ?>" target="<?php echo esc_attr( $open_in_tab ); ?>">
177 <?php echo esc_html( $link_btn_label ); ?>
178 </a>
179 </div>
180 <?php endif; ?>
181 <?php
182 if ( 'over-img' === $img_text_position ) {
183 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
184 echo self::image_caption( $attributes );
185 }
186 ?>
187 </div>
188 </div>
189 </div>
190 <?php
191 if ( 'over-img' !== $img_text_position ) {
192 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
193 echo self::image_caption( $attributes );
194 }
195 ?>
196 </figure>
197 </div>
198 </div>
199 <?php
200 return ob_get_clean();
201 }
202
203 /**
204 * Render Smart Image Caption.
205 *
206 * @param array $attributes array $attributes - Block attributes.
207 *
208 * @return string caption markup.
209 */
210 public function image_caption( $attributes ) {
211
212 $text_vertical_position = isset( $attributes['imgTextVertical'] ) ? $attributes['imgTextVertical'] : 'bottom';
213 $text_horizontal_position = isset( $attributes['imgTextHorizontal'] ) ? $attributes['imgTextHorizontal'] : 'center';
214 $img_text_position = isset( $attributes['imgTextPosition'] ) ? $attributes['imgTextPosition'] : 'over-img';
215 $img_text_visibility = isset( $attributes['imgTextVisibility'] ) ? $attributes['imgTextVisibility'] : '';
216 $img_title_enable = isset( $attributes['imgTitleEnable'] ) ? $attributes['imgTitleEnable'] : false;
217 $img_title_label = isset( $attributes['imgTitleLabel'] ) ? $attributes['imgTitleLabel'] : '';
218 $img_caption_enable = isset( $attributes['imgCaptionEnable'] ) ? $attributes['imgCaptionEnable'] : false;
219 $img_caption_label = isset( $attributes['imgCaptionLabel'] ) ? $attributes['imgCaptionLabel'] : '';
220
221 $text_position = 'over-img' === $img_text_position ? 'sp-position-' . $text_vertical_position . '-' . $text_horizontal_position : '';
222 $container_class = 'sp-visibility-' . $img_text_visibility . ' ' . $text_position . ' sp-text-' . $text_horizontal_position;
223
224 if ( ! $img_title_enable && ! $img_caption_enable ) {
225 return null;
226 }
227
228 ob_start();
229 ?>
230 <figcaption
231 class="sp-smart-image-text-container <?php echo esc_attr( $container_class ); ?>"
232 >
233 <?php if ( $img_title_enable ) : ?>
234 <div class="sp-smart-post sp-text sp-smart-image-title-area">
235 <h4 class="sp-smart-image-title"><?php echo esc_html( $img_title_label ); ?></h4>
236 </div>
237 <?php endif; ?>
238 <?php if ( $img_caption_enable ) : ?>
239 <div class="sp-smart-post sp-text sp-smart-image-caption-area">
240 <p class="sp-smart-image-caption"><?php echo esc_html( $img_caption_label ); ?></p>
241 </div>
242 <?php endif; ?>
243 </div>
244 <?php
245 return ob_get_clean();
246 }
247 }
248