/** * Generate a preview of the latest 5 posts from the feed source being added/edited * * @since 2.0 */ function wprss_preview_meta_box_callback() { global $post; $feed_url = get_post_meta($post->ID, 'wprss_url', true); if (!empty($feed_url)) { $feed = wprss_fetch_feed($feed_url, $post->ID); if (!is_wp_error($feed)) { $items = $feed->get_items(); // Figure out how many total items there are, but limit it to 5. $maxitems = $feed->get_item_quantity(5); // Build an array of all the items, starting with element 0 (first element). $items = $feed->get_items(0, $maxitems); echo '<h4>Latest 5 feed items available from ' . get_the_title() . '</h4>'; echo '<ul>'; foreach ($items as $item) { // Get human date (comment if you want to use non human date) $item_date = human_time_diff($item->get_date('U'), current_time('timestamp')) . ' ' . __('ago', 'rc_mdm'); // Start displaying item content within a <li> tag echo '<li>'; // create item link //echo '<a href="'.esc_url( $item->get_permalink() ).'" title="'.$item_date.'">'; // Get item title echo esc_html($item->get_title()); //echo '</a>'; // Display date echo ' <div class="rss-date"><small>' . $item_date . '</small></div>'; // End <li> tag echo '</li>'; } echo '</ul>'; } else { ?> <span class="invalid-feed-url"> <strong><?php _e('Invalid feed URL', 'wprss'); ?> </strong> - <?php _e('Double check the feed source URL setting above.', 'wprss'); ?> </span> <p><?php _e('Not sure where to find the RSS feed on a website?', 'wprss'); ?> <a target="_blank" href="http://webtrends.about.com/od/webfeedsyndicationrss/ss/rss_howto.htm"> <?php _e('Click here'); ?> </a> <?php _e('for a visual guide', 'wprss'); ?> </p> <?php } $force_feed = get_post_meta($post->ID, 'wprss_force_feed', TRUE); ?> <hr/> <p> <label for="wprss-force-feed"> <strong><?php _e('Are you seeing an error, but sure the feed URL is correct?', 'wprss'); ?> </strong> </label> </p> <p> <label for="wprss-force-feed">Force the feed</label> <input type="hidden" name="wprss_force_feed" value="false" /> <input type="checkbox" name="wprss_force_feed" id="wprss-force-feed" value="true" <?php echo checked($force_feed, 'true'); ?> /> </p> <p class="description"> <strong>Note:</strong> This will disable auto discovery of the RSS feed, meaning you will have to use the feed's URL. Using the site's URL will not work. </p> <?php } else { _e('No feed URL defined yet', 'wprss'); } }
/** * Fetches the feed items from a feed at the given URL. * * Called from 'wprss_fetch_insert_single_feed_items' * * @since 3.0 */ function wprss_get_feed_items($feed_url, $source) { // Add filters and actions prior to fetching the feed items add_filter('wp_feed_cache_transient_lifetime', 'wprss_feed_cache_lifetime'); add_action('wp_feed_options', 'wprss_do_not_cache_feeds'); /* Fetch the feed from the soure URL specified */ $feed = wprss_fetch_feed($feed_url, $source); // Remove previously added filters and actions remove_action('wp_feed_options', 'wprss_do_not_cache_feeds'); remove_filter('wp_feed_cache_transient_lifetime', 'wprss_feed_cache_lifetime'); if (!is_wp_error($feed)) { // Return the items in the feed. return $feed->get_items(); } else { wprss_log('Failed to fetch feed "' . $feed_url . '". ' . $feed->get_error_message()); return NULL; } }
/** * Checks if the feed source uses the force full content option or meta option, and * returns the fulltextrss url if so. * * @since 1.0 */ public static function check_force_full_content( $feed_url, $feed_ID ) { if ( wprss_ftp_using_feed_items( $feed_ID ) ) { return $feed_url; } // Get the computed settings / meta options for the feed source $options = WPRSS_FTP_Settings::get_instance()->get_computed_options( $feed_ID ); // If using force full content option / meta if ( WPRSS_FTP_Utils::multiboolean( $options['force_full_content'] ) === TRUE ) { $service = WPRSS_FTP_Settings::get_instance()->get('full_text_rss_service'); $service = apply_filters( 'wprss_ftp_service_before_full_text_feed_url', $service ); switch( $service ) { case 'free': $key = WPRSS_FTP_FULL_TEXT_RSS_KEY; $API_HASH = sha1( $key . $feed_url ); $encoded_url = urlencode( $feed_url ); // Prepare the fulltext sources $full_text_sources = apply_filters( 'wprss_ftp_full_text_sources', array( "http://fulltext.wprssaggregator.com/makefulltextfeed.php?key=1&hash=$API_HASH&links=preserve&exc=1&url=", "http://ftr-premium.fivefilters.org/makefulltextfeed.php?key=1920&hash=$API_HASH&max=10&links=preserve&exc=1&url=", ) ); // Start with no feed to use $feed_url_to_use = NULL; // Load SimplePie require_once ( ABSPATH . WPINC . '/class-feed.php' ); // For each source ... foreach ( $full_text_sources as $full_text_source ) { // Prepare the feed $full_text_feed_url = $full_text_source . $encoded_url; $feed = wprss_fetch_feed( $full_text_feed_url, $feed_ID ); // If the feed has no errors, the we will use this feed if ( !is_wp_error( $feed ) && !$feed->error() ) { $feed_url_to_use = $full_text_source . $encoded_url; break; } } // If after trying all the sources, the feed to use is still NULL, then no source was valid. // Return the same url passed as parameter, Otherwise, return the full text rss feed url if ( $feed_url_to_use === NULL ) { WPRSS_FTP_Utils::log( __( 'Failed to find a working full text rss service.', WPRSS_TEXT_DOMAIN ), 'check_force_full_content' ); } return ( $feed_url_to_use === NULL )? $feed_url : $feed_url_to_use; case 'feeds_api': $api_key = WPRSS_FTP_Settings::get_instance()->get( 'feeds_api_key' ); $encoded_url = urlencode( $feed_url ); $feeds_api_feed_url = WPRSS_FTP_Utils::template( WPRSS_FTP_FEEDS_API_REQUEST_FORMAT, array( 'url' => $encoded_url, 'key' => $api_key, ) ); // Attempt to fetch the feed $feed = wprss_fetch_feed( $feeds_api_feed_url, $feed_ID ); // If an error was encountered if ( is_wp_error( $feed ) || $feed->error() ) { // Request the error message and log it $response = wp_remote_get( $feeds_api_feed_url ); WPRSS_FTP_Utils::log( "FeedsAPI failed to return a feed, and responded with: \"{$response['body']}\"" ); // Return the original parameter url return $feed_url; } // Return the feeds api if no error was encountered. return $feeds_api_feed_url; // For other services default: return apply_filters( 'wprss_ftp_misc_full_text_url', $feed_url, $feed_ID, $service ); } } // Otherwise, return back the given url else return $feed_url; }
/** * Generate a preview of the latest 5 posts from the feed source being added/edited * * @since 2.0 */ function wprss_preview_meta_box_callback() { global $post; $feed_url = get_post_meta($post->ID, 'wprss_url', true); $help = WPRSS_Help::get_instance(); /* @var $help WPRSS_Help */ echo '<div id="feed-preview-container">'; if (!empty($feed_url)) { $feed = wprss_fetch_feed($feed_url, $post->ID); if (!is_wp_error($feed)) { $items = $feed->get_items(); // Figure out how many total items there are $total = $feed->get_item_quantity(); // Get the number of items again, but limit it to 5. $maxitems = $feed->get_item_quantity(5); // Build an array of all the items, starting with element 0 (first element). $items = $feed->get_items(0, $maxitems); ?> <h4><?php echo sprintf(__('Latest %1$s feed items out of %2$s available from %3$s'), $maxitems, $total, get_the_title()); ?> </h4> <ul> <?php foreach ($items as $item) { // Get human date (comment if you want to use non human date) $has_date = $item->get_date('U') ? TRUE : FALSE; if ($has_date) { $item_date = human_time_diff($item->get_date('U'), current_time('timestamp')) . ' ' . __('ago', 'rc_mdm'); } else { $item_date = '<em>[' . __('No Date', WPRSS_TEXT_DOMAIN) . ']</em>'; } // Start displaying item content within a <li> tag echo '<li>'; // create item link //echo '<a href="'.esc_url( $item->get_permalink() ).'" title="'.$item_date.'">'; // Get item title echo esc_html($item->get_title()); //echo '</a>'; // Display date echo ' <div class="rss-date"><small>' . $item_date . '</small></div>'; // End <li> tag echo '</li>'; } ?> </ul> <?php } else { ?> <span class="invalid-feed-url"> <?php _e('<strong>Invalid feed URL</strong> - Double check the feed source URL setting above.', WPRSS_TEXT_DOMAIN); ?> <?php wprss_log_obj('Failed to preview feed.', $feed->get_error_message(), NULL, WPRSS_LOG_LEVEL_INFO); ?> </span> <?php echo wpautop(sprintf(__('Not sure where to find the RSS feed on a website? <a target="_blank" href="%1$s">Click here</a> for a visual guide. ', WPRSS_TEXT_DOMAIN), 'http://webtrends.about.com/od/webfeedsyndicationrss/ss/rss_howto.htm')); } } else { echo '<p>' . __('No feed URL defined yet', WPRSS_TEXT_DOMAIN) . '</p>'; } echo '</div>'; echo '<div id="force-feed-container">'; wprss_render_force_feed_option($post->ID, TRUE); echo '</div>'; }
/** * This function is triggered just after a post is updated. * It checks if the updated post is a feed source, and carries out any * updating necassary. * * @since 3.3 */ function wprss_updated_feed_source($post_ID, $post_after, $post_before) { // Check if the post is a feed source and is published if ($post_after->post_type == 'wprss_feed' && $post_after->post_status == 'publish') { if (isset($_POST['wprss_url']) && !empty($_POST['wprss_url'])) { $url = $_POST['wprss_url']; $feed = wprss_fetch_feed($url); if ($feed !== NULL && !is_wp_error($feed)) { update_post_meta($post_ID, 'wprss_site_url', $feed->get_permalink()); update_post_meta($post_ID, 'wprss_feed_image', $feed->get_image_url()); } } if (isset($_POST['wprss_limit']) && !empty($_POST['wprss_limit'])) { // Checking feed limit change // Get the limit currently saved in db, and limit in POST request //$limit = get_post_meta( $post_ID, 'wprss_limit', true ); $limit = $_POST['wprss_limit']; // Get all feed items for this source $feed_sources = new WP_Query(array('post_type' => 'wprss_feed_item', 'post_status' => 'publish', 'cache_results' => false, 'no_found_rows' => true, 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'ASC', 'meta_query' => array(array('key' => 'wprss_feed_id', 'value' => $post_ID, 'compare' => 'LIKE')))); // If the limit is smaller than the number of found posts, delete the feed items // and re-import, to ensure that most recent feed items are present. $difference = intval($feed_sources->post_count) - intval($limit); if ($difference > 0) { // Loop and delete the excess feed items while ($feed_sources->have_posts() && $difference > 0) { $feed_sources->the_post(); wp_delete_post(get_the_ID(), true); $difference--; } } } } }