Skip to content

Commit 6a582d1

Browse files
release: fixes
- Fixed an error for an edge case when image extraction failed to find a correct image in the feed entry.
2 parents ad9a9b3 + bd31de0 commit 6a582d1

File tree

3 files changed

+616
-79
lines changed

3 files changed

+616
-79
lines changed

includes/abstract/feedzy-rss-feeds-admin-abstract.php

Lines changed: 107 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ public function sanitize_attr( $sc, $feed_url ) {
11081108
* @since 3.0.0
11091109
* @access private
11101110
*
1111-
* @param array $sc The shorcode attributes array.
1111+
* @param array $sc The shortcode attributes array.
11121112
* @param object $feed The feed object.
11131113
* @param string $feed_url The feed url.
11141114
* @param string $content The original content.
@@ -1147,8 +1147,11 @@ private function render_content( $sc, $feed, $feed_url, $content = '' ) {
11471147
$class[] = $main_class;
11481148
$content .= '<div class="' . esc_attr( implode( ' ', $class ) ) . '">';
11491149
if ( $feed_title['use_title'] ) {
1150+
$feed_title = ! empty( $feed->get_title() ) ? $feed->get_title() : '';
1151+
$feed_description = ! empty( $feed->get_description() ) ? $feed->get_description() : '';
1152+
11501153
$content .= '<div class="rss_header">';
1151-
$content .= '<h2><a href="' . esc_url( $feed->get_permalink() ) . '" class="rss_title" rel="noopener">' . wp_kses_post( html_entity_decode( $feed->get_title() ) ) . '</a> <span class="rss_description"> ' . wp_kses_post( $feed->get_description() ) . '</span></h2>';
1154+
$content .= '<h2><a href="' . esc_url( $feed->get_permalink() ) . '" class="rss_title" rel="noopener">' . wp_kses_post( html_entity_decode( $feed_title ) ) . '</a> <span class="rss_description"> ' . wp_kses_post( $feed_description ) . '</span></h2>';
11521155
$content .= '</div>';
11531156
}
11541157
$content .= '<ul>';
@@ -1405,28 +1408,28 @@ private function get_feed_item_filter( $sc, $sizes, $item, $feed_url, $index, $i
14051408
// Fetch image thumbnail.
14061409
if ( 'yes' === $sc['thumb'] || 'auto' === $sc['thumb'] ) {
14071410
$the_thumbnail = $this->feedzy_retrieve_image( $item, $sc );
1408-
}
1409-
if ( 'yes' === $sc['thumb'] || 'auto' === $sc['thumb'] ) {
14101411
$content_thumb = '';
1411-
if ( (
1412-
! empty( $the_thumbnail ) &&
1413-
'auto' === $sc['thumb'] &&
1414-
! strpos( $the_thumbnail, 'img/feedzy.svg' )
1415-
) ||
1416-
'yes' === $sc['thumb']
1412+
if (
1413+
is_string( $the_thumbnail ) && ! empty( $the_thumbnail ) &&
1414+
(
1415+
'yes' === $sc['thumb'] ||
1416+
(
1417+
'auto' === $sc['thumb'] &&
1418+
! strpos( $the_thumbnail, 'img/feedzy.svg' )
1419+
)
1420+
)
14171421
) {
1418-
if ( ! empty( $the_thumbnail ) ) {
1419-
$the_thumbnail = $this->feedzy_image_encode( $the_thumbnail );
1420-
$content_thumb .= '<span class="fetched" style="background-image: url(\'' . $the_thumbnail . '\');" title="' . esc_attr( $item->get_title() ) . '"></span>';
1421-
if ( ! isset( $sc['amp'] ) || 'no' !== $sc['amp'] ) {
1422-
$content_thumb .= '<amp-img width="' . $sizes['width'] . '" height="' . $sizes['height'] . '" src="' . $the_thumbnail . '">';
1423-
}
1422+
$the_thumbnail = $this->feedzy_image_encode( $the_thumbnail );
1423+
$content_thumb .= '<span class="fetched" style="background-image: url(\'' . $the_thumbnail . '\');" title="' . esc_attr( $item->get_title() ) . '"></span>';
1424+
if ( ! isset( $sc['amp'] ) || 'no' !== $sc['amp'] ) {
1425+
$content_thumb .= '<amp-img width="' . $sizes['width'] . '" height="' . $sizes['height'] . '" src="' . $the_thumbnail . '">';
14241426
}
1425-
if ( empty( $the_thumbnail ) && 'yes' === $sc['thumb'] ) {
1426-
$content_thumb .= '<span class="default" style="background-image:url(' . $sc['default'] . ');" title="' . esc_attr( $item->get_title() ) . '"></span>';
1427-
if ( ! isset( $sc['amp'] ) || 'no' !== $sc['amp'] ) {
1428-
$content_thumb .= '<amp-img width="' . $sizes['width'] . '" height="' . $sizes['height'] . '" src="' . $sc['default'] . '">';
1429-
}
1427+
}
1428+
1429+
if ( empty( $the_thumbnail ) && 'yes' === $sc['thumb'] ) {
1430+
$content_thumb .= '<span class="default" style="background-image:url(' . $sc['default'] . ');" title="' . esc_attr( $item->get_title() ) . '"></span>';
1431+
if ( ! isset( $sc['amp'] ) || 'no' !== $sc['amp'] ) {
1432+
$content_thumb .= '<amp-img width="' . $sizes['width'] . '" height="' . $sizes['height'] . '" src="' . $sc['default'] . '">';
14301433
}
14311434
}
14321435
$content_thumb = apply_filters( 'feedzy_thumb_output', $content_thumb, $feed_url, $sizes, $item );
@@ -1642,13 +1645,13 @@ public function is_image_url( $url ) {
16421645
}
16431646

16441647
/**
1645-
* Retrive image from the item object
1648+
* Retrieve image from the item object
16461649
*
16471650
* @since 3.0.0
16481651
* @access public
16491652
*
1650-
* @param SimplePie\Item $item The item object.
1651-
* @param array $sc The shorcode attributes array.
1653+
* @param SimplePie\Item $item The item object.
1654+
* @param array<string, mixed>|null $sc The shortcode attributes array.
16521655
*
16531656
* @return string
16541657
*/
@@ -1666,42 +1669,9 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
16661669
$enclosures = $item->get_enclosures();
16671670
if ( $enclosures ) {
16681671
foreach ( $enclosures as $enclosure ) {
1669-
// Item thumbnail.
1670-
$single_thumbnail = $enclosure->get_thumbnail();
1671-
$medium = $enclosure->get_medium();
1672-
1673-
if ( in_array( $medium, array( 'video' ), true ) ) {
1674-
break;
1675-
}
1676-
1677-
if ( $single_thumbnail && $this->is_image_url( $single_thumbnail ) ) {
1678-
$the_thumbnail = $single_thumbnail;
1679-
}
1680-
1681-
$thumbnails = $enclosure->get_thumbnails();
1682-
if ( ! empty( $thumbnails ) ) {
1683-
foreach ( $thumbnails as $enclosure_thumbnail ) {
1684-
if ( ! $this->is_image_url( $enclosure_thumbnail ) ) {
1685-
continue;
1686-
}
1687-
$the_thumbnail = $enclosure_thumbnail;
1688-
}
1689-
}
1690-
1691-
$embedded_thumbnail = $enclosure->embed();
1692-
if ( $embedded_thumbnail ) {
1693-
$pattern = '/https?:\/\/.*\.(?:jpg|JPG|jpeg|JPEG|jpe|JPE|gif|GIF|png|PNG)/i';
1694-
if ( preg_match( $pattern, $embedded_thumbnail, $matches ) ) {
1695-
$the_thumbnail = $matches[0];
1696-
}
1697-
}
1698-
1699-
$enclosure_link = $enclosure->get_link();
1700-
if ( $this->is_image_url( $enclosure_link ) ) {
1701-
$the_thumbnail = $enclosure_link;
1702-
}
1703-
// Break loop if thumbnail is found.
1704-
if ( ! empty( $the_thumbnail ) ) {
1672+
$image_url = $this->extract_image_from_enclosure( $enclosure );
1673+
if ( $this->is_image_url( $image_url ) ) {
1674+
$the_thumbnail = $image_url;
17051675
break;
17061676
}
17071677
}
@@ -1715,17 +1685,28 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
17151685
}
17161686
// Content image.
17171687
if ( empty( $the_thumbnail ) ) {
1718-
$feed_description = $item->get_content();
1719-
$the_thumbnail = $this->feedzy_return_image( $feed_description );
1688+
$feed_description = $item->get_content();
1689+
$description_image = $this->feedzy_return_image( $feed_description );
1690+
1691+
if ( $this->is_image_url( $description_image ) ) {
1692+
$the_thumbnail = $description_image;
1693+
}
17201694
}
17211695
// Description image.
17221696
if ( empty( $the_thumbnail ) ) {
1723-
$feed_description = $item->get_description();
1724-
$the_thumbnail = $this->feedzy_return_image( $feed_description );
1697+
$feed_description = $item->get_description();
1698+
$description_image = $this->feedzy_return_image( $feed_description );
1699+
1700+
if ( $this->is_image_url( $description_image ) ) {
1701+
$the_thumbnail = $description_image;
1702+
}
17251703
}
17261704

17271705
// handle HTTP images.
1728-
if ( $sc && isset( $sc['http'] ) && 0 === strpos( $the_thumbnail, 'http://' ) ) {
1706+
if (
1707+
is_string( $the_thumbnail ) && ! empty( $the_thumbnail ) &&
1708+
$sc && isset( $sc['http'] ) && 0 === strpos( $the_thumbnail, 'http://' )
1709+
) {
17291710
switch ( $sc['http'] ) {
17301711
case 'https':
17311712
// fall-through.
@@ -1740,25 +1721,77 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
17401721

17411722
$the_thumbnail = html_entity_decode( $the_thumbnail, ENT_QUOTES, 'UTF-8' );
17421723

1743-
if ( isset( $sc['_dryrun_'] ) && 'yes' === $sc['_dryrun_'] ) {
1744-
return $the_thumbnail;
1724+
if ( is_array( $sc ) && ! empty( $sc ) ) {
1725+
if ( isset( $sc['_dryrun_'] ) && 'yes' === $sc['_dryrun_'] ) {
1726+
return $the_thumbnail;
1727+
}
1728+
1729+
if ( ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) && ! empty( $sc['feeds'] ) ) {
1730+
$feed_url = $this->normalize_urls( $sc['feeds'] );
1731+
$the_thumbnail = ! empty( $the_thumbnail ) ? $the_thumbnail : apply_filters( 'feedzy_default_image', $sc['default'], $feed_url );
1732+
}
17451733
}
17461734

1747-
if ( ! defined( 'REST_REQUEST' ) || ! REST_REQUEST ) {
1748-
$feed_url = $this->normalize_urls( $sc['feeds'] );
1749-
$the_thumbnail = ! empty( $the_thumbnail ) ? $the_thumbnail : apply_filters( 'feedzy_default_image', $sc['default'], $feed_url );
1750-
}
17511735
$the_thumbnail = apply_filters( 'feedzy_retrieve_image', $the_thumbnail, $item );
17521736
return $the_thumbnail;
17531737
}
17541738

1739+
/**
1740+
* Try to extract an image from the enclosure object.
1741+
*
1742+
* @param \SimplePie\Enclosure $enclosure The enclosure object.
1743+
* @return string|null
1744+
*
1745+
* @since 5.0.9
1746+
*/
1747+
public function extract_image_from_enclosure( $enclosure ) {
1748+
$image_url = null;
1749+
$medium = $enclosure->get_medium();
1750+
1751+
if ( in_array( $medium, array( 'video' ), true ) ) {
1752+
return $image_url;
1753+
}
1754+
1755+
$single_thumbnail = $enclosure->get_thumbnail();
1756+
if ( $single_thumbnail && $this->is_image_url( $single_thumbnail ) ) {
1757+
$image_url = $single_thumbnail;
1758+
}
1759+
1760+
$thumbnails = $enclosure->get_thumbnails();
1761+
if ( ! empty( $thumbnails ) ) {
1762+
foreach ( $thumbnails as $enclosure_thumbnail ) {
1763+
if ( ! $this->is_image_url( $enclosure_thumbnail ) ) {
1764+
continue;
1765+
}
1766+
$image_url = $enclosure_thumbnail;
1767+
}
1768+
}
1769+
1770+
if ( ! empty( $enclosure->get_real_type() ) ) {
1771+
$embedded_thumbnail = $enclosure->embed();
1772+
if ( $embedded_thumbnail ) {
1773+
$pattern = '/https?:\/\/.*\.(?:jpg|JPG|jpeg|JPEG|jpe|JPE|gif|GIF|png|PNG)/i';
1774+
if ( preg_match( $pattern, $embedded_thumbnail, $matches ) ) {
1775+
$image_url = $matches[0];
1776+
}
1777+
}
1778+
}
1779+
1780+
$enclosure_link = $enclosure->get_link();
1781+
if ( $this->is_image_url( $enclosure_link ) ) {
1782+
$image_url = $enclosure_link;
1783+
}
1784+
1785+
return $image_url;
1786+
}
1787+
17551788
/**
17561789
* Get an image from a string
17571790
*
17581791
* @since 3.0.0
17591792
* @access public
17601793
*
1761-
* @param string $img_html A string with an <img/> tag.
1794+
* @param string|null $img_html A string with an <img/> tag.
17621795
*
17631796
* @return string
17641797
*/
@@ -1801,7 +1834,7 @@ public function feedzy_return_image( $img_html ) {
18011834
* @return string
18021835
*/
18031836
public function feedzy_scrape_image( $img_html, $link = '' ) {
1804-
$pattern = '/< *img[^>]*src *= *["\']?([^"\']*)/';
1837+
$pattern = '/< *img[^>]*src *= *["\']?([^"\'>]+)/';
18051838
$match = $link;
18061839
preg_match( $pattern, $img_html, $link );
18071840
if ( ! empty( $link ) && isset( $link[1] ) ) {
@@ -1854,7 +1887,7 @@ public function feedzy_blacklist_images() {
18541887
}
18551888

18561889
/**
1857-
* Image name encoder and url retrive if in url param
1890+
* Image name encoder and url retrieve if in url param
18581891
*
18591892
* @since 3.0.0
18601893
* @access public

phpstan-baseline.neon

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,6 @@ parameters:
125125
count: 1
126126
path: includes/abstract/feedzy-rss-feeds-admin-abstract.php
127127

128-
-
129-
message: "#^Method Feedzy_Rss_Feeds_Admin_Abstract\\:\\:feedzy_retrieve_image\\(\\) has parameter \\$sc with no value type specified in iterable type array\\.$#"
130-
count: 1
131-
path: includes/abstract/feedzy-rss-feeds-admin-abstract.php
132-
133128
-
134129
message: "#^Method Feedzy_Rss_Feeds_Admin_Abstract\\:\\:feedzy_rss\\(\\) has parameter \\$atts with no value type specified in iterable type array\\.$#"
135130
count: 1

0 commit comments

Comments
 (0)