@@ -1108,7 +1108,7 @@ public function sanitize_attr( $sc, $feed_url ) {
1108
1108
* @since 3.0.0
1109
1109
* @access private
1110
1110
*
1111
- * @param array $sc The shorcode attributes array.
1111
+ * @param array $sc The shortcode attributes array.
1112
1112
* @param object $feed The feed object.
1113
1113
* @param string $feed_url The feed url.
1114
1114
* @param string $content The original content.
@@ -1147,8 +1147,11 @@ private function render_content( $sc, $feed, $feed_url, $content = '' ) {
1147
1147
$ class [] = $ main_class ;
1148
1148
$ content .= '<div class=" ' . esc_attr ( implode ( ' ' , $ class ) ) . '"> ' ;
1149
1149
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
+
1150
1153
$ 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> ' ;
1152
1155
$ content .= '</div> ' ;
1153
1156
}
1154
1157
$ content .= '<ul> ' ;
@@ -1405,28 +1408,28 @@ private function get_feed_item_filter( $sc, $sizes, $item, $feed_url, $index, $i
1405
1408
// Fetch image thumbnail.
1406
1409
if ( 'yes ' === $ sc ['thumb ' ] || 'auto ' === $ sc ['thumb ' ] ) {
1407
1410
$ the_thumbnail = $ this ->feedzy_retrieve_image ( $ item , $ sc );
1408
- }
1409
- if ( 'yes ' === $ sc ['thumb ' ] || 'auto ' === $ sc ['thumb ' ] ) {
1410
1411
$ 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
+ )
1417
1421
) {
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 . '"> ' ;
1424
1426
}
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 ' ] . '"> ' ;
1430
1433
}
1431
1434
}
1432
1435
$ content_thumb = apply_filters ( 'feedzy_thumb_output ' , $ content_thumb , $ feed_url , $ sizes , $ item );
@@ -1642,13 +1645,13 @@ public function is_image_url( $url ) {
1642
1645
}
1643
1646
1644
1647
/**
1645
- * Retrive image from the item object
1648
+ * Retrieve image from the item object
1646
1649
*
1647
1650
* @since 3.0.0
1648
1651
* @access public
1649
1652
*
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.
1652
1655
*
1653
1656
* @return string
1654
1657
*/
@@ -1666,42 +1669,9 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
1666
1669
$ enclosures = $ item ->get_enclosures ();
1667
1670
if ( $ enclosures ) {
1668
1671
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 ;
1705
1675
break ;
1706
1676
}
1707
1677
}
@@ -1715,17 +1685,28 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
1715
1685
}
1716
1686
// Content image.
1717
1687
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
+ }
1720
1694
}
1721
1695
// Description image.
1722
1696
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
+ }
1725
1703
}
1726
1704
1727
1705
// 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
+ ) {
1729
1710
switch ( $ sc ['http ' ] ) {
1730
1711
case 'https ' :
1731
1712
// fall-through.
@@ -1740,25 +1721,77 @@ public function feedzy_retrieve_image( $item, $sc = null ) {
1740
1721
1741
1722
$ the_thumbnail = html_entity_decode ( $ the_thumbnail , ENT_QUOTES , 'UTF-8 ' );
1742
1723
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
+ }
1745
1733
}
1746
1734
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
- }
1751
1735
$ the_thumbnail = apply_filters ( 'feedzy_retrieve_image ' , $ the_thumbnail , $ item );
1752
1736
return $ the_thumbnail ;
1753
1737
}
1754
1738
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
+
1755
1788
/**
1756
1789
* Get an image from a string
1757
1790
*
1758
1791
* @since 3.0.0
1759
1792
* @access public
1760
1793
*
1761
- * @param string $img_html A string with an <img/> tag.
1794
+ * @param string|null $img_html A string with an <img/> tag.
1762
1795
*
1763
1796
* @return string
1764
1797
*/
@@ -1801,7 +1834,7 @@ public function feedzy_return_image( $img_html ) {
1801
1834
* @return string
1802
1835
*/
1803
1836
public function feedzy_scrape_image ( $ img_html , $ link = '' ) {
1804
- $ pattern = '/< *img[^>]*src *= *[" \']?([^" \']* )/ ' ;
1837
+ $ pattern = '/< *img[^>]*src *= *[" \']?([^" \'>]+ )/ ' ;
1805
1838
$ match = $ link ;
1806
1839
preg_match ( $ pattern , $ img_html , $ link );
1807
1840
if ( ! empty ( $ link ) && isset ( $ link [1 ] ) ) {
@@ -1854,7 +1887,7 @@ public function feedzy_blacklist_images() {
1854
1887
}
1855
1888
1856
1889
/**
1857
- * Image name encoder and url retrive if in url param
1890
+ * Image name encoder and url retrieve if in url param
1858
1891
*
1859
1892
* @since 3.0.0
1860
1893
* @access public
0 commit comments