get_channel_tags() public method

This method allows you to get access to ANY element/attribute in the channel/header section of the feed. See {@see \SimplePie::get_feed_tags()} for a description of the return value
See also: http://simplepie.org/wiki/faq/supported_xml_namespaces
Since: 1.0
public get_channel_tags ( string $namespace, string $tag ) : array
$namespace string The URL of the XML namespace of the elements you're trying to access
$tag string Tag name
return array
 /**
  * Find the feed's icon
  *
  * @param SimplePie $feed SimplePie object to retrieve logo for
  * @return string URL to feed icon
  */
 protected static function discover_favicon($feed, $id)
 {
     if ($return = $feed->get_channel_tags(SIMPLEPIE_NAMESPACE_ATOM_10, 'icon')) {
         $favicon = SimplePie_Misc::absolutize_url($return[0]['data'], $feed->get_base($return[0]));
     } elseif (($url = $feed->get_link()) !== null && preg_match('/^http(s)?:\\/\\//i', $url)) {
         $filename = $id . '.ico';
         $favicon = SimplePie_Misc::absolutize_url('/favicon.ico', $url);
     } else {
         return false;
     }
     $cache = new DataHandler(get_option('cachedir'));
     $request = new HTTPRequest();
     $file = $request->get($favicon, array('X-Forwarded-For' => $_SERVER['REMOTE_ADDR']));
     if ($file->success && strlen($file->body) > 0) {
         $sniffer = new $feed->content_type_sniffer_class($file);
         if (substr($sniffer->get_type(), 0, 6) === 'image/') {
             $body = array('type' => $sniffer->get_type(), 'body' => $file->body);
             return $cache->save($filename, serialize($body));
         } else {
             return false;
         }
     }
     return false;
 }
 function generate_post()
 {
     if ($this->o["feed_url"] == "") {
         return;
     }
     $rss = new SimplePie();
     $rss->set_feed_url($this->o["feed_url"]);
     $rss->enable_cache(false);
     $rss->enable_order_by_date(false);
     $rss->init();
     $updated = $rss->get_channel_tags("http://www.w3.org/2005/Atom", "updated");
     $last_update = strtotime(str_replace(array("T", "Z"), " ", $updated[0]["data"]));
     $new_items = array();
     $post_content = "";
     $newtime = 0;
     if ($this->o["last_refresh_feed"] < $last_update) {
         foreach ($rss->get_items() as $item) {
             $entry_time = strtotime($item->get_local_date());
             $crawl_time = $item->data["attribs"]["http://www.google.com/schemas/reader/atom/"]["crawl-timestamp-msec"];
             if ($newtime == 0) {
                 $newtime = $crawl_time;
             }
             if ($this->o["last_crawl"] < $crawl_time) {
                 $new_item["crawl_time"] = $crawl_time;
                 $new_item["entry_time"] = $entry_time;
                 $new_item["title"] = $item->get_title();
                 $new_item["link"] = $this->get_origin_url($item->get_link());
                 if ($source = $item->get_source()) {
                     $new_item['site_url'] = $source->get_link(0);
                     $new_item['site_name'] = $source->get_title();
                 }
                 $annotation = $item->get_item_tags("http://www.google.com/schemas/reader/atom/", "annotation");
                 if (isset($annotation)) {
                     $note = html_entity_decode($this->o["post_note_template"]);
                     $note = str_replace(array_keys($this->annotation_elements), array($annotation[0]["child"]["http://www.w3.org/2005/Atom"]["content"][0]["data"], $annotation[0]["child"]["http://www.w3.org/2005/Atom"]["author"][0]["child"]["http://www.w3.org/2005/Atom"]["name"][0]["data"]), $note);
                 } else {
                     $note = "";
                 }
                 $new_item["note"] = $note;
                 $new_items[] = $new_item;
             }
         }
         if (count($new_items) > 0) {
             foreach ($new_items as $item) {
                 $item_date = date(get_option("date_format"), $item["entry_time"]);
                 $import = html_entity_decode($this->o["post_item_template"]);
                 $import = str_replace(array_keys($this->item_elements), array($item["title"], $item["link"], $item_date, $item["note"], $item["site_name"], $item["site_url"]), $import);
                 $post_content .= $import;
             }
         }
     }
     if ($post_content != "") {
         $post_title = html_entity_decode($this->o["post_title"]);
         $post_title = str_replace(array_keys($this->title_elements), date(get_option("date_format")), $post_title);
         $post_header = html_entity_decode($this->o["post_header_template"]);
         $post_footer = html_entity_decode($this->o["post_footer_template"]);
         $new_post = array();
         $new_post['comment_status'] = $this->o["post_comments"] == 1 ? 'open' : 'closed';
         $new_post['post_author'] = $this->o["post_author"];
         $new_post['post_content'] = $post_header . $post_content . $post_footer;
         $new_post['post_status'] = 'publish';
         $new_post['post_title'] = $post_title;
         $new_post['post_type'] = 'post';
         $new_post['post_category'] = array($this->o["post_category"]);
         $new_post['tags_input'] = $this->o["post_tags"];
         wp_insert_post($new_post);
         $this->o["last_refresh_feed"] = $last_update;
         $this->o["last_refresh"] = mktime();
         $this->o["last_crawl"] = $newtime;
         update_option($this->options_key, $this->o);
     }
 }
        function get_total_results($url)
        {
            $response = $this->oauth_get($url,array('max-results' => 1, 'start-index' => 2));

            $feed = new SimplePie();
            $feed->set_raw_data($response);
            $feed->init();
            $results = $feed->get_channel_tags('http://a9.com/-/spec/opensearchrss/1.0/', 'totalResults');

            $total_results = $results[0]['data'];
            unset($feed);
            return (int)$total_results;
        }
Beispiel #4
0
 /**
  * called by do_ajax())
  * takes action when ajax request is made with URL from the comment form
  * send back 1 or 10 last posts depending on rules
  */
 function fetch_feed()
 {
     // check nonce
     //debugbreak();
     $options = $this->get_options();
     if (isset($options['use_nonce'])) {
         $checknonce = check_ajax_referer('fetch', false, false);
         if (!$checknonce) {
             die(' error! not authorized ' . strip_tags($_REQUEST['_ajax_nonce']));
         }
     }
     if (!$_POST['url']) {
         die('no url');
     }
     if (!defined('DOING_AJAX')) {
         define('DOING_AJAX', true);
     }
     // try to prevent deprecated notices
     @ini_set('display_errors', 0);
     @error_reporting(0);
     include_once ABSPATH . WPINC . '/class-simplepie.php';
     $num = 1;
     $url = esc_url($_POST['url']);
     $orig_url = $url;
     // add trailing slash (can help with some blogs)
     if (!strpos($url, '?')) {
         $url = trailingslashit($url);
     }
     // fetch 10 last posts?
     if (is_user_logged_in() && $options['whogets'] == 'registered' || !is_user_logged_in() && $options['whogets'] == 'everybody') {
         $num = 10;
     } elseif ($options['whogets'] == 'everybody') {
         $num = 10;
     } elseif (current_user_can('manage_options')) {
         $num = 10;
     }
     // check if request is for the blog we're on
     if (strstr($url, home_url())) {
         //DebugBreak();
         $posts = get_posts(array('numberposts' => 10));
         $return = array();
         $error = '';
         if ($posts) {
             foreach ($posts as $post) {
                 $return[] = array('type' => 'blog', 'title' => htmlspecialchars_decode(strip_tags($post->post_title)), 'link' => get_permalink($post->ID), 'p' => 'u');
             }
         } else {
             $error = __('Could not get posts for home blog', $this->plugin_domain);
         }
         // check for admin only notices to add
         $canreg = get_option('users_can_register');
         $whogets = $options['whogets'];
         if (!$canreg && $whogets == 'registered') {
             $return[] = array('type' => 'message', 'title' => __('Warning! You have set to show 10 posts for registered users but you have not enabled user registrations on your site. You should change the operational settings in the CommentLuv settings page to show 10 posts for everyone or enable user registrations', $this->plugin_domain), 'link' => '');
         }
         $response = json_encode(array('error' => $error, 'items' => $return));
         header("Content-Type: application/json");
         echo $response;
         exit;
     }
     // get simple pie ready
     $rss = new SimplePie();
     if (!$rss) {
         die(' error! no simplepie');
     }
     $rss->set_useragent('Commentluv /' . $this->version . ' (Feed Parser; http://www.commentluv.com; Allow like Gecko) Build/20110502');
     $rss->set_feed_url(add_query_arg(array('commentluv' => 'true'), $url));
     $rss->enable_cache(FALSE);
     // fetch the feed
     $rss->init();
     $su = $rss->subscribe_url();
     $ferror = $rss->error();
     // try a fall back and add /?feed=rss2 to the end of url if the found subscribe url hasn't already got it
     // also try known blogspot feed location if this is a blogspot url
     if ($ferror || strstr($ferror, 'could not be found') && !strstr($su, 'feed')) {
         unset($rss);
         $rss = new SimplePie();
         $rss->set_useragent('Commentluv /' . $this->version . ' (Feed Parser; http://www.commentluv.com; Allow like Gecko) Build/20110502');
         $rss->enable_cache(FALSE);
         // construct alternate feed url
         if (strstr($url, 'blogspot')) {
             $url = trailingslashit($url) . 'feeds/posts/default/';
         } else {
             $url = add_query_arg(array('feed' => 'atom'), $url);
         }
         $rss->set_feed_url($url);
         $rss->init();
         $ferror = $rss->error();
         if ($ferror || stripos($ferror, 'invalid')) {
             $suburl = $rss->subscribe_url() ? $rss->subscribe_url() : $orig_url;
             unset($rss);
             $rss = new SimplePie();
             $rss->set_useragent('Commentluv /' . $this->version . ' (Feed Parser; http://www.commentluv.com; Allow like Gecko) Build/20110502');
             $rss->enable_cache(FALSE);
             $rss->set_feed_url($orig_url);
             $rss->init();
             $ferror = $rss->error();
             // go back to original URL if error persisted
             if (stripos($ferror, 'invalid')) {
                 //get raw file to show any errors
                 @(include_once ABSPATH . WPINC . '/SimplePie/File.php');
                 if (class_exists('SimplePie_File')) {
                     $rawfile = new SimplePie_File($suburl, $rss->timeout, 5, null, $rss->useragent, $rss->force_fsockopen);
                 } elseif (class_exists($rss->file_class)) {
                     $rawfile = new $rss->file_class($suburl, $rss->timeout, 5, null, $rss->useragent, $rss->force_fsockopen);
                 }
                 if (isset($rawfile->body)) {
                     $rawfile = $rawfile->body;
                 } else {
                     $rawfile = __('Raw file could not be found', $this->plugin_domain);
                 }
             }
         }
     }
     $rss->handle_content_type();
     $gen = $rss->get_channel_tags('', 'generator');
     $prem_msg = $rss->get_channel_tags('', 'prem_msg');
     $g = $num;
     $p = 'u';
     $meta = array();
     //DebugBreak();
     if ($gen && strstr($gen[0]['data'], 'commentluv')) {
         $generator = $gen[0]['data'];
         $meta['generator'] = $generator;
         $pos = stripos($generator, 'v=');
         if (substr($generator, $pos + 2, 1) == '3') {
             $g = 15;
             $p = 'p';
         }
     }
     if ($prem_msg) {
         $prem_msg = $prem_msg[0]['data'];
     }
     //DebugBreak();
     $error = $rss->error();
     $meta['used_feed'] = $rss->subscribe_url();
     //DebugBreak();
     // no error, construct return json
     if (!$error) {
         $arr = array();
         // save meta
         $meta['used_feed'] = $rss->subscribe_url();
         $feed_items = $rss->get_items();
         foreach ($feed_items as $item) {
             //debugbreak();
             $type = 'blog';
             $itemtags = $item->get_item_tags('', 'type');
             if ($itemtags) {
                 $type = $itemtags[0]['data'];
             }
             $arr[] = array('type' => $type, 'title' => htmlspecialchars_decode(strip_tags($item->get_title())), 'link' => $item->get_permalink(), 'p' => $p);
             $g--;
             if ($g < 1) {
                 break;
             }
         }
         // add message to unregistered user if set
         if (!is_user_logged_in() && $options['unreg_user_text'] && $options['whogets'] != 'everybody' && $p == 'u') {
             if (get_option('users_can_register')) {
                 $arr[] = array('type' => 'message', 'title' => $options['unreg_user_text'], 'link' => '');
                 if (!strstr($options['unreg_user_text'], 'action=register')) {
                     $register_link = apply_filters('register', '<a href="' . site_url('wp-login.php?action=register', 'login') . '">' . __('Register') . '</a>');
                     $arr[] = array('type' => 'message', 'title' => $register_link, 'link' => '');
                 }
             }
             if ($options['whogets'] == 'registered' && get_option('users_can_regsiter')) {
                 $arr[] = array('type' => 'message', 'title' => __('If you are registered, you need to log in to get 10 posts to choose from', $this->plugin_domain), 'link' => '');
             }
         }
         if ($prem_msg) {
             $arr[] = array('type' => 'alert', 'title' => $prem_msg, 'link' => '');
         }
         $response = json_encode(array('error' => '', 'items' => $arr, 'meta' => $meta));
     } else {
         // had an error trying to read the feed
         $response = json_encode(array('error' => $error, 'meta' => $meta, 'rawfile' => htmlspecialchars($rawfile)));
     }
     unset($rss);
     header("Content-Type: application/json");
     echo $response;
     exit;
 }