script_var = strtolower(__CLASS__); $this->handle = str_replace('_', '-', $this->script_var); $this->results = $this->handle . '-feed'; add_action( 'wp_ajax_'.$this->script_var, array($this, 'get_feeds_ajax') ); } function enqueue_scripts() { wp_enqueue_script($this->handle, plugins_url('scripts/jquery.news.js', dirname(__FILE__)), array('jquery'), $this->get_version(), true); wp_localize_script($this->handle, $this->script_var, array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'ajaxnonce' => wp_create_nonce( $this->script_var.'_nonce' ), 'ajaxaction' => $this->script_var, 'ajaxresults' => '.'.$this->results ) ); } function display_feeds($feeds = array()) { $feeds = apply_filters($this->script_var, $feeds); if (is_array($feeds) && (count($feeds) > 0)) { printf ('
', $this->results); for($index=0; $index < count($feeds); $index++ ) { wp_localize_script( $this->handle, $this->script_var.$index, array( 'feedurl' => $feeds[$index]) ); } } } function get_feeds_ajax() { check_ajax_referer( $this->script_var.'_nonce', 'security' ); $url = isset($_POST['url']) ? $_POST['url'] : ''; if (empty($url)) wp_send_json_error( array( 'error' => $this->get_message( 'feed_missing' ) ) ); $instance = array('url' => $url, 'show_summary' => false, 'show_featured' => true); $feed = $this->get_rss_feed_instance($instance); if( isset( $feed ) ) wp_send_json_success( $feed ); else wp_send_json_error( array( 'error' => sprintf($this->get_message('feed_failure'), $url ) ) ); } function get_rss_feed_instance( $instance ) { $url = ! empty( $instance['url'] ) ? $instance['url'] : ''; while ( stristr($url, 'http') != $url ) $url = substr($url, 1); if ( empty($url) ) return; if ( in_array( untrailingslashit( $url ), array( site_url(), home_url() ) ) ) return; $rss = fetch_feed($url); if ( is_wp_error($rss) ) { if ( is_admin() || current_user_can('manage_options') ) echo '
' . sprintf( __('%1$s: %2$s'), $this->get_message('feed_error') , $rss->get_error_message() ) . '
'; return; } $default_args = array( 'show_featured' => 0, 'show_summary' => 0, 'show_author' => 0, 'show_date' => 0, 'items' => 0 ); if (($parsed_url = parse_url($url)) && ($query = isset($parsed_url['query']) ? $parsed_url['query'] : '')) { $instance = wp_parse_args($query, $default_args); } $args = wp_parse_args( $instance, $default_args ); $items = (int) $args['items']; if ( $items < 1 || 20 < $items ) $items = 10; $show_featured = (int) $args['show_featured']; $show_summary = (int) $args['show_summary']; $show_author = (int) $args['show_author']; $show_date = (int) $args['show_date']; if ( !$rss->get_item_quantity() ) { return '
' .$this->message('feed_empty') . '
'; } $results = ''; foreach ( $rss->get_items( 0, $items ) as $item ) { $link = $item->get_link(); while ( stristr( $link, 'http' ) != $link ) { $link = substr( $link, 1 ); } $link = esc_url( strip_tags( $link ) ); $title = esc_html( trim( strip_tags( $item->get_title() ) ) ); $link_title = ''; $desc = @html_entity_decode( $item->get_description(), ENT_QUOTES, get_option( 'blog_charset' ) ); if (substr($desc,0,5) == ''); $link_title = sprintf(' title="%1$s"', $title); $title = substr($desc,0, $end_image+1); $desc = substr($desc, $end_image+1); } else { if ($show_featured) continue; //skip items with missing featured images } $desc = esc_attr( wp_trim_words( $desc, 55, ' […]' ) ); $summary = ''; if ( $show_summary ) { $summary = $desc; // Change existing [...] to […]. if ( '[...]' == substr( $summary, -5 ) ) { $summary = substr( $summary, 0, -5 ) . '[…]'; } $summary = '
' . esc_html( $summary ) . '
'; } $date = ''; if ( $show_date ) { $date = $item->get_date( 'U' ); if ( $date ) { $date = ' ' . date_i18n( get_option( 'date_format' ), $date ) . ''; } } $author = ''; if ( $show_author ) { $author = $item->get_author(); if ( is_object($author) ) { $author = $author->get_name(); $author = ' ' . esc_html( strip_tags( $author ) ) . ''; } } if ($link) $title = sprintf('%3$s', $link, $link_title, $title); $results .= sprintf('
%1$s
%2$s%3$s%4$s
', $title, $date, $summary, $author, $this->results ); } if ( ! is_wp_error($rss) ) $rss->__destruct(); unset($rss); return $results; } }