+/**
+ * Determine the type of a string of data with the data formatted.
+ *
+ * Tell whether the type is text, html, or xhtml, per RFC 4287 section 3.1.
+ *
+ * In the case of WordPress, text is defined as containing no markup,
+ * xhtml is defined as "well formed", and html as tag soup (i.e., the rest).
+ *
+ * Container div tags are added to xhtml values, per section 3.1.1.3.
+ *
+ * @link http://www.atomenabled.org/developers/syndication/atom-format-spec.php#rfc.section.3.1
+ *
+ * @package WordPress
+ * @subpackage Feed
+ * @since 2.5
+ *
+ * @param string $data Input string
+ * @return array array(type, value)
+ */
+function prep_atom_text_construct($data) {
+ if (strpos($data, '<') === false && strpos($data, '&') === false) {
+ return array('text', $data);
+ }
+
+ $parser = xml_parser_create();
+ xml_parse($parser, '<div>' . $data . '</div>', true);
+ $code = xml_get_error_code($parser);
+ xml_parser_free($parser);
+
+ if (!$code) {
+ if (strpos($data, '<') === false) {
+ return array('text', $data);
+ } else {
+ $data = "<div xmlns='http://www.w3.org/1999/xhtml'>$data</div>";
+ return array('xhtml', $data);
+ }
+ }
+
+ if (strpos($data, ']]>') == false) {
+ return array('html', "<![CDATA[$data]]>");
+ } else {
+ return array('html', htmlspecialchars($data));
+ }
+}
+
+/**
+ * Display the link for the currently displayed feed in a XSS safe way.
+ *
+ * Generate a correct link for the atom:self element.
+ *
+ * @package WordPress
+ * @subpackage Feed
+ * @since 2.5
+ */
+function self_link() {
+ $host = @parse_url(get_option('home'));
+ $host = $host['host'];
+ echo esc_url(
+ 'http'
+ . ( (isset($_SERVER['https']) && $_SERVER['https'] == 'on') ? 's' : '' ) . '://'
+ . $host
+ . stripslashes($_SERVER['REQUEST_URI'])
+ );
+}
+
+/**
+ * Return the content type for specified feed type.
+ *
+ * @package WordPress
+ * @subpackage Feed
+ * @since 2.8.0
+ */
+function feed_content_type( $type = '' ) {
+ if ( empty($type) )
+ $type = get_default_feed();
+
+ $types = array(
+ 'rss' => 'application/rss+xml',
+ 'rss2' => 'application/rss+xml',
+ 'rss-http' => 'text/xml',
+ 'atom' => 'application/atom+xml',
+ 'rdf' => 'application/rdf+xml'
+ );
+
+ $content_type = ( !empty($types[$type]) ) ? $types[$type] : 'application/octet-stream';
+
+ return apply_filters( 'feed_content_type', $content_type, $type );
+}
+
+/**
+ * Build SimplePie object based on RSS or Atom feed from URL.
+ *
+ * @since 2.8
+ *
+ * @param string $url URL to retrieve feed
+ * @return WP_Error|SimplePie WP_Error object on failure or SimplePie object on success
+ */
+function fetch_feed($url) {
+ require_once (ABSPATH . WPINC . '/class-feed.php');
+
+ $feed = new SimplePie();
+ $feed->set_feed_url($url);
+ $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', 43200));
+ $feed->init();
+ $feed->handle_content_type();
+
+ if ( $feed->error() )
+ return new WP_Error('simplepie-error', $feed->error());
+
+ return $feed;
+}