Skip to content

Commit bce84dd

Browse files
refactor: make a separated function for thumbnail fetch
1 parent e5e9d79 commit bce84dd

File tree

1 file changed

+47
-29
lines changed

1 file changed

+47
-29
lines changed

includes/gutenberg/feedzy-rss-feeds-loop-block.php

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -257,35 +257,7 @@ public function get_value( $key, $item, $attributes ) {
257257
case 'categories':
258258
return isset( $item['item_categories'] ) ? $item['item_categories'] : '';
259259
case 'image':
260-
$settings = apply_filters( 'feedzy_get_settings', array() );
261-
$thumb = 'auto';
262-
$default_thumbnail_img = '';
263-
264-
if ( isset( $attributes['thumb'] ) && ! empty( $attributes['thumb'] ) ) {
265-
$thumb = $attributes['thumb'];
266-
}
267-
268-
if ( 'no' === $thumb ) {
269-
return '';
270-
}
271-
272-
if ( isset( $item['item_img_path'] ) && ! empty( $item['item_img_path'] ) ) {
273-
$default_thumbnail_img = $item['item_img_path'];
274-
} elseif ( 'yes' === $thumb ) {
275-
if (
276-
isset( $attributes['fallbackImage'], $attributes['fallbackImage']['url'] ) &&
277-
! empty( $attributes['fallbackImage']['url'] )
278-
) {
279-
$default_thumbnail_img = $attributes['fallbackImage']['url'];
280-
} elseif ( $settings && ! empty( $settings['general']['default-thumbnail-id'] ) ) {
281-
$default_thumbnail_img = wp_get_attachment_image_src( $settings['general']['default-thumbnail-id'], 'full' );
282-
$default_thumbnail_img = ! empty( $default_thumbnail_img ) ? reset( $default_thumbnail_img ) : '';
283-
} else {
284-
$default_thumbnail_img = FEEDZY_ABSURL . 'img/feedzy.svg';
285-
}
286-
}
287-
288-
return $default_thumbnail_img;
260+
return $this->get_thumbnail( $item, $attributes );
289261
case 'media':
290262
return isset( $item['item_media']['src'] ) ? $item['item_media']['src'] : '';
291263
case 'price':
@@ -296,4 +268,50 @@ public function get_value( $key, $item, $attributes ) {
296268
return '';
297269
}
298270
}
271+
272+
/**
273+
* Get Thumbnail of feed item.
274+
*
275+
* Fallback to default thumbnail if not set. The Fallback image can be set in the block attributes or in the plugin settings.
276+
*
277+
* @param array<string, mixed> $item The feed item.
278+
* @param array<string, mixed> $attributes The block attributes.
279+
*
280+
* @return string The thumbnail URL.
281+
*/
282+
private function get_thumbnail( $item, $attributes ) {
283+
$settings = apply_filters( 'feedzy_get_settings', array() );
284+
$thumb = 'yes';
285+
286+
if ( isset( $attributes['thumb'] ) && ! empty( $attributes['thumb'] ) ) {
287+
$thumb = $attributes['thumb'];
288+
}
289+
290+
if ( 'no' === $thumb ) {
291+
return '';
292+
}
293+
294+
if ( isset( $item['item_img_path'] ) && ! empty( $item['item_img_path'] ) ) {
295+
return $item['item_img_path'];
296+
}
297+
298+
if ( 'yes' !== $thumb ) {
299+
return '';
300+
}
301+
302+
// Try to find the fallback image.
303+
if (
304+
isset( $attributes['fallbackImage'], $attributes['fallbackImage']['url'] ) &&
305+
! empty( $attributes['fallbackImage']['url'] )
306+
) {
307+
return $attributes['fallbackImage']['url'];
308+
} elseif (
309+
isset( $settings, $settings['general'], $settings['general']['default-thumbnail-id'] ) &&
310+
! empty( $settings['general']['default-thumbnail-id'] )
311+
) {
312+
return wp_get_attachment_image_src( $settings['general']['default-thumbnail-id'], 'full' );
313+
}
314+
315+
return FEEDZY_ABSURL . 'img/feedzy.svg';
316+
}
299317
}

0 commit comments

Comments
 (0)