Skip to content

Commit 1008ea5

Browse files
fix: image handling in Feedzy RSS Feeds (#1107)
Add a simple verification function for valid image URLs.
1 parent ccfb1fd commit 1008ea5

File tree

3 files changed

+51
-55
lines changed

3 files changed

+51
-55
lines changed

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

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ private function render_content( $sc, $feed, $feed_url, $content = '' ) {
11731173
sprintf( $anchor2, esc_url( $item['item_url'] ), esc_attr( $item['item_url_target'] ), esc_attr( $item['item_url_follow'] ), wp_kses_post( $item['item_title'] ) ),
11741174
$details
11751175
);
1176-
} else {
1176+
} else {
11771177
$content .= sprintf(
11781178
$line_item,
11791179
wp_kses_post( $item['itemAttr'] ),
@@ -1616,14 +1616,38 @@ private function get_feed_item_filter( $sc, $sizes, $item, $feed_url, $index, $i
16161616
return $item_array;
16171617
}
16181618

1619+
/**
1620+
* Check if the URL is an image URL based on its extension.
1621+
*
1622+
* @param string|null $url The URL to check.
1623+
* @return bool
1624+
*/
1625+
public function is_image_url( $url ) {
1626+
if ( empty( $url ) || ! is_string( $url ) ) {
1627+
return false;
1628+
}
1629+
1630+
$image_extensions = array( 'jpg', 'jpeg', 'png', 'gif', 'webp', 'svg', 'bmp', 'tiff', 'tif', 'avif' );
1631+
$url_parts = wp_parse_url( $url );
1632+
if ( ! isset( $url_parts['path'] ) ) {
1633+
return false;
1634+
}
1635+
1636+
$path_info = pathinfo( $url_parts['path'] );
1637+
return (
1638+
isset( $path_info['extension'] ) &&
1639+
in_array( strtolower( $path_info['extension'] ), $image_extensions )
1640+
);
1641+
}
1642+
16191643
/**
16201644
* Retrive image from the item object
16211645
*
16221646
* @since 3.0.0
16231647
* @access public
16241648
*
1625-
* @param object $item The item object.
1626-
* @param array $sc The shorcode attributes array.
1649+
* @param SimplePie\Item $item The item object.
1650+
* @param array $sc The shorcode attributes array.
16271651
*
16281652
* @return string
16291653
*/
@@ -1640,38 +1664,40 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
16401664
$the_thumbnail = '';
16411665
$enclosures = $item->get_enclosures();
16421666
if ( $enclosures ) {
1643-
foreach ( (array) $enclosures as $enclosure ) {
1667+
foreach ( $enclosures as $enclosure ) {
16441668
// Item thumbnail.
1645-
$thumbnail = $enclosure->get_thumbnail();
1646-
$medium = $enclosure->get_medium();
1669+
$single_thumbnail = $enclosure->get_thumbnail();
1670+
$medium = $enclosure->get_medium();
1671+
16471672
if ( in_array( $medium, array( 'video' ), true ) ) {
16481673
break;
16491674
}
1650-
if ( $thumbnail ) {
1651-
$the_thumbnail = $thumbnail;
1675+
1676+
if ( $single_thumbnail && $this->is_image_url( $single_thumbnail ) ) {
1677+
$the_thumbnail = $single_thumbnail;
16521678
}
1653-
if ( isset( $enclosure->thumbnails ) ) {
1654-
foreach ( (array) $enclosure->thumbnails as $thumbnail ) {
1655-
$the_thumbnail = $thumbnail;
1679+
1680+
$thumbnails = $enclosure->get_thumbnails();
1681+
if ( ! empty( $thumbnails ) ) {
1682+
foreach ( $thumbnails as $enclosure_thumbnail ) {
1683+
if ( ! $this->is_image_url( $enclosure_thumbnail ) ) {
1684+
continue;
1685+
}
1686+
$the_thumbnail = $enclosure_thumbnail;
16561687
}
16571688
}
1658-
$thumbnail = $enclosure->embed();
1659-
if ( $thumbnail ) {
1689+
1690+
$embedded_thumbnail = $enclosure->embed();
1691+
if ( $embedded_thumbnail ) {
16601692
$pattern = '/https?:\/\/.*\.(?:jpg|JPG|jpeg|JPEG|jpe|JPE|gif|GIF|png|PNG)/i';
1661-
if ( preg_match( $pattern, $thumbnail, $matches ) ) {
1693+
if ( preg_match( $pattern, $embedded_thumbnail, $matches ) ) {
16621694
$the_thumbnail = $matches[0];
16631695
}
16641696
}
1665-
foreach ( (array) $enclosure->get_link() as $thumbnail ) {
1666-
$pattern = '/https?:\/\/.*\.(?:jpg|JPG|jpeg|JPEG|jpe|JPE|gif|GIF|png|PNG)/i';
1667-
$imgsrc = $thumbnail;
1668-
if ( preg_match( $pattern, $imgsrc, $matches ) ) {
1669-
$the_thumbnail = $thumbnail;
1670-
break;
1671-
} elseif ( in_array( $enclosure->type, $image_mime_types, true ) ) {
1672-
$the_thumbnail = $thumbnail;
1673-
break;
1674-
}
1697+
1698+
$enclosure_link = $enclosure->get_link();
1699+
if ( $this->is_image_url( $enclosure_link ) ) {
1700+
$the_thumbnail = $enclosure_link;
16751701
}
16761702
// Break loop if thumbnail is found.
16771703
if ( ! empty( $the_thumbnail ) ) {

includes/util/feedzy-rss-feeds-conditions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function is_condition_met( $condition, $value ): bool {
240240
*
241241
* @param bool $default_value The current return value.
242242
* @param array<string, string> $attrs The attributes of the feed.
243-
* @param array<string, string> $item The item to evaluate.
243+
* @param SimplePie\Item $item The item to evaluate.
244244
* @param string $feed_url The URL of the feed.
245245
* @param int $index The index of the item.
246246
*

phpstan-baseline.neon

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,31 +1705,6 @@ parameters:
17051705
count: 2
17061706
path: includes/layouts/setup-wizard.php
17071707

1708-
-
1709-
message: "#^Cannot call method get_author\\(\\) on array\\<string, string\\>\\.$#"
1710-
count: 1
1711-
path: includes/util/feedzy-rss-feeds-conditions.php
1712-
1713-
-
1714-
message: "#^Cannot call method get_content\\(\\) on array\\<string, string\\>\\.$#"
1715-
count: 1
1716-
path: includes/util/feedzy-rss-feeds-conditions.php
1717-
1718-
-
1719-
message: "#^Cannot call method get_date\\(\\) on array\\<string, string\\>\\.$#"
1720-
count: 1
1721-
path: includes/util/feedzy-rss-feeds-conditions.php
1722-
1723-
-
1724-
message: "#^Cannot call method get_item_tags\\(\\) on array\\<string, string\\>\\.$#"
1725-
count: 1
1726-
path: includes/util/feedzy-rss-feeds-conditions.php
1727-
1728-
-
1729-
message: "#^Cannot call method get_title\\(\\) on array\\<string, string\\>\\.$#"
1730-
count: 1
1731-
path: includes/util/feedzy-rss-feeds-conditions.php
1732-
17331708
-
17341709
message: "#^Constant SIMPLEPIE_NAMESPACE_ATOM_10 not found\\.$#"
17351710
count: 1
@@ -1740,11 +1715,6 @@ parameters:
17401715
count: 1
17411716
path: includes/util/feedzy-rss-feeds-conditions.php
17421717

1743-
-
1744-
message: "#^Parameter \\#1 \\$item of method Feedzy_Rss_Feeds_Admin_Abstract\\:\\:feedzy_retrieve_image\\(\\) expects object, array\\<string, string\\> given\\.$#"
1745-
count: 1
1746-
path: includes/util/feedzy-rss-feeds-conditions.php
1747-
17481718
-
17491719
message: "#^Unreachable statement \\- code above always terminates\\.$#"
17501720
count: 1

0 commit comments

Comments
 (0)