Skip to content

Commit

Permalink
Merge pull request #69 from Codeinwp/v320
Browse files Browse the repository at this point in the history
Fix for image URL issue following some strange patterns. 
Added fallback for broken feed, now if one feed from the list is not working, others will will be used.
Added shortcode parameter for feed cache control.
  • Loading branch information
selul authored Aug 17, 2017
2 parents 750a696 + aabf0d7 commit 4e64b0f
Show file tree
Hide file tree
Showing 9 changed files with 375 additions and 207 deletions.
11 changes: 10 additions & 1 deletion css/feedzy-rss-feeds.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* feedzy-rss-feeds.css
* Feedzy RSS Feed
* Copyright: (c) 2016 Themeisle, themeisle.com
* Version: 3.1.10
* Version: 3.2.0
* Plugin Name: FEEDZY RSS Feeds
* Plugin URI: http://themeisle.com/plugins/feedzy-rss-feeds/
* Author: Themeisle
Expand Down Expand Up @@ -524,3 +524,12 @@ input:checked + .feedzy-track:before {
font-size: 17px;
vertical-align: -3px;
}

.post-type-feedzy_categories .postbox-container div#submitdiv,
.post-type-feedzy_categories #feedzy_category_feeds_rn {
display: block !important;
}

.post-type-feedzy_categories .postbox-container > div > .postbox:not(#feedzy_category_feeds) {
display: none;
}
2 changes: 1 addition & 1 deletion feedzy-rss-feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Plugin Name: Feedzy RSS Feeds Lite
* Plugin URI: https://themeisle.com/plugins/feedzy-rss-feeds-lite/
* Description: A small and lightweight RSS aggregator plugin. Fast and very easy to use, it allows you to aggregate multiple RSS feeds into your WordPress site through fully customizable shortcodes & widgets.
* Version: 3.1.10
* Version: 3.2.0
* Author: Themeisle
* Author URI: http://themeisle.com
* License: GPL-2.0+
Expand Down
101 changes: 86 additions & 15 deletions includes/abstract/feedzy-rss-feeds-admin-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public function feedzy_add_item_padding( $itemAttr, $sizes ) {
* @access public
*
* @param string $itemAttr The item attribute.
* @param string $sizes The item sizes.
* @param string $item The feed item.
* @param string $feedURL The feed URL.
* @param string $sc The short code attributes.
*
* @return string
*/
Expand Down Expand Up @@ -227,7 +231,8 @@ public function process_feed_source( $src ) {
public function feedzy_rss( $atts, $content = '' ) {
$sc = $this->get_short_code_attributes( $atts );
$feedURL = $this->normalize_urls( $sc['feeds'] );
$feed = $this->fetch_feed( $feedURL );
$cache = $sc['refresh'];
$feed = $this->fetch_feed( $feedURL, $cache );
if ( is_string( $feed ) ) {
return $feed;
}
Expand Down Expand Up @@ -274,6 +279,8 @@ public function get_short_code_attributes( $atts ) {
'size' => '',
// thumbs pixel size
'keywords_title' => '',
// cache refresh
'refresh' => '12_hours',
// only display item if title contains specific keywords (comma-separated list/case sensitive)
), $atts, 'feedzy_default'
);
Expand Down Expand Up @@ -314,13 +321,28 @@ public function normalize_urls( $raw ) {
* @since 3.1.7
* @access private
* @param string $feedURL The feed URL.
* @param string $cache The cache string (eg. 1_hour, 30_min etc.).
* @return SimplePie
*/
private function init_feed( $feedURL ) {
private function init_feed( $feedURL, $cache ) {
$unit_defaults = array(
'mins' => MINUTE_IN_SECONDS,
'hours' => HOUR_IN_SECONDS,
'days' => DAY_IN_SECONDS,
);
$cache_time = 12 * HOUR_IN_SECONDS;
if ( isset( $cache ) && $cache != '' ) {
list( $value, $unit ) = explode( '_', $cache );
if ( isset( $value ) && is_numeric( $value ) && $value >= 1 && $value <= 100 ) {
if ( isset( $unit ) && in_array( strtolower( $unit ), array( 'mins', 'hours', 'days' ) ) ) {
$cache_time = $value * $unit_defaults[ $unit ];
}
}
}
$feed = new SimplePie();
$feed->set_cache_class( 'WP_Feed_Cache' );
$feed->set_file_class( 'WP_SimplePie_File' );
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', 12 * HOUR_IN_SECONDS, $feedURL ) );
$feed->set_cache_duration( apply_filters( 'wp_feed_cache_transient_lifetime', $cache_time, $feedURL ) );
$feed->set_feed_url( $feedURL );
$feed->init();
$feed->handle_content_type();
Expand All @@ -331,11 +353,14 @@ private function init_feed( $feedURL ) {
/**
* Fetch the content feed from a group of urls.
*
* @param array $feedURL The feeds urls to fetch content from.
*
* @since 3.0.0
* @access public
* @updated 3.2.0
* @param array $feedURL The feeds urls to fetch content from.
* @param string $cache The cache string (eg. 1_hour, 30_min etc.).
* @return SimplePie|string|void|WP_Error The feed resource.
*/
public function fetch_feed( $feedURL ) {
public function fetch_feed( $feedURL, $cache = '12_hours' ) {
// Load SimplePie if not already
if ( ! class_exists( 'SimplePie' ) ) {
require_once( ABSPATH . WPINC . '/feed.php' );
Expand All @@ -351,17 +376,65 @@ public function fetch_feed( $feedURL ) {
} else {
$feedURL = html_entity_decode( $feedURL );
}
$feed = fetch_feed( $feedURL ); // Not used as log as #41304 is Opened.
if ( is_wp_error( $feed ) ) {
return __( 'An error occured for when trying to retrieve feeds! Check the URL\'s provided as feed sources.', 'feedzy-rss-feeds' );
}

$feedURL = $this->get_valid_feed_urls( $feedURL, $cache );

// $feed = fetch_feed( $validFeedURL ); // Not used as log as #41304 is Opened.
}

$feed = $this->init_feed( $feedURL ); // Added in 3.1.7 -- TODO: Remove this line when #41304 is fixed.
$feed = $this->init_feed( $feedURL, $cache ); // Added in 3.1.7 -- TODO: Remove this line when #41304 is fixed.

// var_dump( $feed );
return $feed;
}

/**
* Returns only valid URLs for fetching.
*
* @since 3.2.0
* @access private
* @param array|string $feedURL The feeds URL/s.
* @param string $cache The cache string (eg. 1_hour, 30_min etc.).
* @return array
*/
private function get_valid_feed_urls( $feedURL, $cache ) {
$validFeedURL = array();
if ( is_array( $feedURL ) ) {
foreach ( $feedURL as $url ) {
if ( $this->check_valid_xml( $url, $cache ) ) {
$validFeedURL[] = $url;
} else {
echo sprintf( __( 'Feed URL: %s not valid and removed from fetch.', 'feedzy-rss-feeds' ), '<b>' . $url . '</b>' );
}
}
} else {
if ( $this->check_valid_xml( $feedURL, $cache ) ) {
$validFeedURL[] = $feedURL;
} else {
echo sprintf( __( 'Feed URL: %s not valid and removed from fetch.', 'feedzy-rss-feeds' ), '<b>' . $feedURL . '</b>' );
}
}

return $validFeedURL;
}

/**
* Checks if a url is a valid feed.
*
* @since 3.2.0
* @access private
* @param string $url The URL to validate.
* @param string $cache The cache string (eg. 1_hour, 30_min etc.).
* @return bool
*/
private function check_valid_xml( $url, $cache ) {
$feed = $this->init_feed( $url, $cache );
if ( $feed->error() ) {
return false;
}
return true;
}

/**
* Sanitizes the shortcode array and sets the defaults
*
Expand Down Expand Up @@ -579,7 +652,7 @@ private function get_feed_item_filter( $sc, $sizes, $item, $feedURL ) {
if ( ( ! empty( $theThumbnail ) && $sc['thumb'] == 'auto' ) || $sc['thumb'] == 'yes' ) {
if ( ! empty( $theThumbnail ) ) {
$theThumbnail = $this->feedzy_image_encode( $theThumbnail );
$contentThumb .= '<span class="fetched" style="background-image: url(' . $theThumbnail . ');" title="' . $item->get_title() . '"></span>';
$contentThumb .= '<span class="fetched" style="background-image: url(\'' . $theThumbnail . '\');" title="' . $item->get_title() . '"></span>';
}
if ( $sc['thumb'] == 'yes' ) {
$contentThumb .= '<span class="default" style="background-image:url(' . $sc['default'] . ');" title="' . $item->get_title() . '"></span>';
Expand Down Expand Up @@ -725,8 +798,6 @@ public function feedzy_retrieve_image( $item ) {
$theThumbnail = $this->feedzy_return_image( $feedDescription );
}

$theThumbnail = preg_replace( '/\s+/', '-', trim( preg_replace( '/[\s-]+/', ' ', $theThumbnail ) ) );

return $theThumbnail;
}

Expand Down Expand Up @@ -783,7 +854,7 @@ public function feedzy_scrape_image( $string, $link = '' ) {
$match = $link;
preg_match( $pattern, $string, $link );
if ( ! empty( $link ) && isset( $link[1] ) ) {
$match = urldecode( $link[1] );
$match = $link[1];
}

return $match;
Expand Down
15 changes: 15 additions & 0 deletions includes/admin/feedzy-rss-feeds-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,21 @@ public function add_feedzy_post_type_metaboxes() {
'feedzy_category_feed',
), 'feedzy_categories', 'normal', 'high'
);
add_meta_box(
'feedzy_category_feeds_rn', __( 'Increase your social media presence', 'feedzy-rss-feeds' ), array(
$this,
'render_upsell_rn',
), 'feedzy_categories', 'side', 'low'
);
}

/**
* Render RN upsell metabox.
*/
public function render_upsell_rn() {
echo '<p>Learn how you can connect with people by sharing content from RSS feeds on your social media accounts. </p>';
echo '<a class="button button-primary " href="https://revive.social/plugins/revive-network/" target="_blank">View more details</a>';

}

/**
Expand Down
Loading

0 comments on commit 4e64b0f

Please sign in to comment.