/>

/>

themeidol_array_obj_string( $error ) ); //Display the error message return '
themeidol_array_obj_string( $error ) ) . '">

' . __('Sorry, this feed is currently unavailable or does not exists anymore.', 'themeidol-all-widget') . '

'; } /*************************************************************** * Convert array or object into string ***************************************************************/ function themeidol_array_obj_string ( $error ){ if ( is_array( $error ) || is_object( $error ) ) { return print_r( $error, true ); } else { return $error; } } /*************************************************************** * Padding ratio based on image size ***************************************************************/ function themeidol_add_item_padding( $itemAttr, $sizes ){ $paddinTop = number_format( ( 15 / 150 ) * $sizes[ 'height' ], 0 ); $paddinBottom = number_format( ( 25 / 150 ) * $sizes[ 'height' ], 0 ); $stylePadding = ' style="padding: ' . $paddinTop . 'px 0 ' . $paddinBottom . 'px"'; return $itemAttr . $stylePadding; } /*************************************************************** * Feed item container class ***************************************************************/ function themeidol_classes_item( $itemAttr ){ $classes = array( 'rss_item' ); $classes = apply_filters( 'themeidol_add_classes_item', $classes ); $classes = ' class="' . implode( ' ', $classes ) . '"'; return $itemAttr . $classes; } /*************************************************************** * Main Rss Function function ***************************************************************/ function themeidol_rss( $atts, $content = '' ) { global $feedzyStyle; $feedzyStyle = true; $count = 0; //Load SimplePie if not already if ( !class_exists( 'SimplePie' ) ){ require_once( ABSPATH . WPINC . '/class-feed.php' ); } //Retrieve & extract shorcode parameters extract( shortcode_atts( array( "feeds" => '', //comma separated feeds url "max" => '5', //number of feeds items (0 for unlimited) "feed_title" => 'yes', //display feed title yes/no "target" => '_blank', //_blank, _self "title" => '', //strip title after X char "meta" => 'yes', //yes, no "summary" => 'yes', //strip title "summarylength" => '', //strip summary after X char "thumb" => 'yes', //yes, no, auto "default" => '', //default thumb URL if no image found (only if thumb is set to yes or auto) "size" => '', //thumbs pixel size "keywords_title" => '' //only display item if title contains specific keywords (comma-separated list/case sensitive) ), $atts, 'themeidol_default' ) ); //Use "shortcode_atts_feedzy_default" filter to edit shortcode parameters default values or add your owns. if ( !empty( $feeds ) ) { $feeds = rtrim( $feeds, ',' ); $feeds = explode( ',', $feeds ); //Remove SSL from HTTP request to prevent fetching errors foreach( $feeds as $feed ){ $feedURL[] = preg_replace("/^https:/i", "http:", $feed); } if ( count( $feedURL ) === 1 ) { $feedURL = $feedURL[0]; } } if ( $max == '0' ) { $max = '999'; } else if ( empty( $max ) || !ctype_digit( $max ) ) { $max = '5'; } if ( empty( $size ) || !ctype_digit( $size ) ){ $size = '150'; } $sizes = array( 'width' => $size, 'height' => $size ); $sizes = apply_filters( 'themeidol_thumb_sizes', $sizes, $feedURL ); if ( !empty( $title ) && !ctype_digit( $title ) ){ $title = ''; } if ( !empty($keywords_title)){ $keywords_title = rtrim( $keywords_title, ',' ); $keywords_title = array_map( 'trim', explode( ',', $keywords_title ) ); } if ( !empty( $summarylength ) && !ctype_digit( $summarylength ) ){ $summarylength = ''; } if ( !empty( $default ) ) { $default = $default; } else { $default = apply_filters( 'themeidol_default_image', $default, $feedURL ); } //Load SimplePie Instance $feed = new SimplePie(); $feed -> set_feed_url( $feedURL ); $feed -> enable_cache( true ); $feed -> enable_order_by_date( true ); $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', 7200, $feedURL ) ); do_action_ref_array( 'wp_feed_options', array( $feed, $feedURL ) ); $feed -> strip_comments( true ); $feed -> strip_htmltags( array( 'base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style' ) ); $feed -> init(); $feed -> handle_content_type(); // Display the error message if ( $feed -> error() ) { $content .= apply_filters( 'themeidol_default_error', $feed -> error(), $feedURL ); } $content .= '
'; if ( $feed_title == 'yes' ) { $content .= '
'; $content .= '

' . html_entity_decode( $feed->get_title() ) . ' ' . $feed->get_description() . '

'; $content .= '
'; } $content .= ''; $content .= '
'; return apply_filters( 'themeidol_global_output', $content, $feedURL ); }//end of themeidol_rss /*************************************************************** * Retrive image from the item object ***************************************************************/ function themeidol_retrieve_image( $item ) { $thethumbnail = ""; if ( $enclosures = $item->get_enclosures() ) { foreach( (array) $enclosures as $enclosure ){ //item thumb if ( $thumbnail = $enclosure->get_thumbnail() ) { $thethumbnail = $thumbnail; } //media:thumbnail if ( isset( $enclosure->thumbnails ) ) { foreach ( (array) $enclosure->thumbnails as $thumbnail ) { $thethumbnail = $thumbnail; } } //enclosure if ( $thumbnail = $enclosure->embed() ) { $pattern = '/https?:\/\/.*\.(?:jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/iU'; if ( preg_match( $pattern, $thumbnail, $matches ) ) { $thethumbnail = $matches[0]; } } //media:content && strpos( $enclosure->type, 'image' ) !== false foreach ( (array) $enclosure->get_link() as $thumbnail ) { $pattern = '/https?:\/\/.*\.(?:jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/iU'; $imgsrc = $thumbnail; if ( preg_match( $pattern, $imgsrc, $matches ) ) { $thethumbnail = $matches[0]; break; } } //break loop if thumbnail found if ( ! empty( $thethumbnail ) ) { break; } } } //xmlns:itunes podcast if ( empty( $thethumbnail ) ) { $data = $item->get_item_tags('http://www.itunes.com/dtds/podcast-1.0.dtd', 'image'); if ( isset( $data['0']['attribs']['']['href'] ) && !empty( $data['0']['attribs']['']['href'] ) ){ $thethumbnail = $data['0']['attribs']['']['href']; } } //content image if ( empty( $thethumbnail ) ) { $feedDescription = $item->get_content(); $thethumbnail = $this->themeidol_returnImage( $feedDescription ); } //description image if ( empty( $thethumbnail ) ) { $feedDescription = $item->get_description(); $thethumbnail = $this->themeidol_returnImage( $feedDescription ); } return $thethumbnail; } /*************************************************************** * Get an image from a string ***************************************************************/ function themeidol_returnImage( $string ) { $img = html_entity_decode($string, ENT_QUOTES, 'UTF-8'); $pattern = "/]+\>/i"; preg_match( $pattern, $img, $matches ); if( isset( $matches[0] ) ){ $blacklistCount = 0; foreach( $matches as $matche){ $link = $this->themeidol_scrapeImage( $matche ); $blacklist = array(); $blacklist = apply_filters( 'themeidol_feed_blacklist_images', $this->themeidol_blacklist_images( $blacklist ) ); foreach( $blacklist as $string ) { if ( strpos( (string) $link, $string ) !== false) { $blacklistCount++; } } if( $blacklistCount == 0) break; } if( $blacklistCount == 0) return $link; } return; } function themeidol_scrapeImage( $string, $link = '' ) { $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/'; preg_match( $pattern, $string, $link ); if( isset( $link[1] ) ){ $link = urldecode( $link[1] ); } return $link; } /*************************************************************** * List blacklisted images to prevent fetching emoticons ***************************************************************/ function themeidol_blacklist_images( $blacklist ) { $blacklist = array( 'frownie.png', 'icon_arrow.gif', 'icon_biggrin.gif', 'icon_confused.gif', 'icon_cool.gif', 'icon_cry.gif', 'icon_eek.gif', 'icon_evil.gif', 'icon_exclaim.gif', 'icon_idea.gif', 'icon_lol.gif', 'icon_mad.gif', 'icon_mrgreen.gif', 'icon_neutral.gif', 'icon_question.gif', 'icon_razz.gif', 'icon_redface.gif', 'icon_rolleyes.gif', 'icon_sad.gif', 'icon_smile.gif', 'icon_surprised.gif', 'icon_twisted.gif', 'icon_wink.gif', 'mrgreen.png', 'rolleyes.png', 'simple-smile.png', ); return $blacklist; } /*************************************************************** * Image name encode + get image url if in url param ***************************************************************/ function themeidol_image_encode( $string ) { //Check if img url is set as an URL parameter $url_tab = parse_url( $string ); if( isset( $url_tab['query'] ) ){ preg_match_all( '/(http|https):\/\/[^ ]+(\.gif|\.GIF|\.jpg|\.JPG|\.jpeg|\.JPEG|\.png|\.PNG)/', $url_tab['query'], $imgUrl ); if( isset( $imgUrl[0][0] ) ){ $string = $imgUrl[0][0]; } } //Encode image name only en keep extra parameters $query = $extention = ''; $url_tab = parse_url( $string ); if( isset( $url_tab['query'] ) ){ $query = '?' . $url_tab['query']; } $path_parts = pathinfo( $string ); $path = $path_parts['dirname']; $file = rawurldecode( $path_parts['filename'] ); $extention = pathinfo( $url_tab['path'], PATHINFO_EXTENSION ); if( !empty( $extention ) ){ $extention = '.' . $extention; } //Return a well encoded image url return $path . '/' . rawurlencode( $file ) . $extention . $query; } public function flush_widget_cache() { wp_cache_delete( 'themeidol-rssfeed', 'widget' ); } //Display widget function widget( $args, $instance ) { $cache = (array) wp_cache_get( 'themeidol-rssfeed', 'widget' ); if(!is_array($cache)) $cache = array(); if(isset($cache[$args['widget_id']])){ echo $cache[$args['widget_id']]; return; } ob_start(); extract( $args ); wp_enqueue_style('rss-feed-style'); $title = apply_filters( 'widget_title', $instance[ 'title' ] ); if (strpos($before_widget, 'widget ') !== false) { $before_widget = preg_replace('/widget /', "idol-widget ", $before_widget, 1); } //Display the widget body echo $before_widget; //Check if title is set if ( $title ) echo $before_title . $title . $after_title; $items = array( 'meta', 'summary' ); foreach( $items as $item ){ if( $instance[ $item ] == true ){ $instance[ $item ] = 'yes'; } else { $instance[ $item ] = 'no'; } } //Fix for versions before 2.3.1 if ( $instance[ 'thumb' ] == '1' ){ $instance[ 'thumb' ] = 'yes'; } else if ( $instance[ 'thumb' ] == '0' ) { $instance[ 'thumb' ] = 'no'; } //Call the shortcode function echo $this->themeidol_rss( array( "feeds" => $instance[ 'feeds' ], "max" => $instance[ 'max' ], "feed_title" => 'no', "target" => $instance[ 'target' ], "title" => $instance[ 'titlelength' ], "meta" => $instance[ 'meta' ], "summary" => $instance[ 'summary' ], "summarylength" => $instance[ 'summarylength' ], "thumb" => $instance[ 'thumb' ], "default" => $instance[ 'default' ], "size" => $instance[ 'size' ], "keywords_title" => $instance[ 'keywords_title' ] ) ); echo $after_widget; $widget_string = ob_get_flush(); $cache[$args['widget_id']] = $widget_string; wp_cache_add('themeidol-rssfeed', $cache, 'widget'); } } add_action( 'widgets_init', create_function( '', 'return register_widget("Themeidol_rss_feed_widget");' ) );