'rtl'. * @return string The (X)HTML markup to display on the page. */ function render($url, $options = null) { // Create a new SimplePie instance with this feed $feed = new SimplePie(); $feed->set_feed_url($url); $feed->init(); // Generate a unique identifier value for the URL(s) being used. HTML id attributes MUST begin with a letter. if (is_array($url)) { $t = ''; foreach ($url as $u) { $t .= $u; } $hash = 'a' . sha1($t); } else { $hash = 'a' . sha1($url); } // Open a
with a class of "block" (which we'll use for styling) and an id of some random value (for targetting via JavaScript) $html = '
' . "\n"; // Allow a custom title to be set. if (isset($options['title'])) { $feed_title = $options['title']; } else { if (is_array($url)) { $feed_title = array(); foreach ($url as $u) { $feed_title[] = newsblocks::name($u); } $feed_title = implode(', ', $feed_title); } else { $feed_title = newsblocks::name($feed->get_permalink()); } } // Allow a custom favicon to be set. if (isset($options['favicon'])) { $favicon = $options['favicon']; } else { if (is_array($url)) { $favicon = 'images/alternate_favicon.png'; } else { $favicon = $feed->get_favicon(); } } // Allow a custom link to be set. if (isset($options['permalink'])) { $permalink = $options['permalink']; } else { $permalink = $feed->get_permalink(); } // Here's the name of the feed, formatted the way we want. $html .= '

' . $feed_title . '

'; // Let's begin the primary list (the part we want to show when the page loads). if (isset($options['direction']) && $options['direction'] == 'rtl') { $direction = 'rtl'; } else { $direction = 'ltr'; } $html .= '' . "\n"; // Let's begin the secondary list (the part that will show when we click on "More" below). $html .= '' . "\n"; // If we have more than 10 items in the feed... if ($feed->get_item_quantity() > 10) { // Add a little "More" link for people to click on. $html .= '

More »

'; } // Close out of this
block. $html .= '
' . "\n"; // Return all of the HTML, so that we can display it as we choose or manipulate it further. return $html; } /** * Renders a simpler, wider display for media-oriented feeds containing thumbnails. * * @access public * @param mixed $url Either a single feed URL (as a string) or an array of feed URLs (as an array of strings). * @param array $options Options that the function should take into account when rendering the markup. Currently only accepts integer 'items'. Intended for syntactic parity with newsblocks::render(). * @return string The (X)HTML markup to display on the page. */ function render_wide($url, $options = null) { // Create a new SimplePie instance with this feed $feed = new SimplePie(); $feed->set_feed_url($url); $feed->init(); // Generate a unique identifier value for the URL(s) being used. HTML id attributes MUST begin with a letter. if (is_array($url)) { $t = ''; foreach ($url as $u) { $t .= $u; } $hash = 'a' . sha1($t); } else { $hash = 'a' . sha1($url); } // Open a
with a class of "block" (which we'll use for styling) and an id of some random value (for targetting via JavaScript) $html = '
' . "\n"; // Let's begin the primary list (the part we want to show when the page loads). $html .= '
    ' . "\n"; // Loop through the first 10 items. if (isset($options['items'])) { $items = intval($options['items']); } else { $items = 10; } foreach ($feed->get_items(0, $items) as $item) { // Check to see if we have an enclosure so we can add a special icon if ($enclosure = $item->get_enclosure()) { // Check to see if we have a thumbnail. We need it because this is going to display an image. if ($thumb = $enclosure->get_thumbnail()) { // Add each item: item title, linked back to the original posting, with a tooltip containing the description. $html .= '
  • ' . $item->get_title() . '
  • ' . "\n"; } } } // Close out of the primary list $html .= '
' . "\n"; // Close out of this
block. $html .= '
' . "\n"; // Return all of the HTML, so that we can display it as we choose or manipulate it further. return $html; } /** * Cleans up text for special output. * * Namely for use inside 'title' attributes. Strips HTML, removes double-quotes (since * that's what this demo uses for attributes), reduces all linebreaks and multiple * spaces into a single space, and can shorten a string to a number of characters. * * @access public * @param string $s The string of text to clean up. * @param integer $length The number of characters to return in the description. * @return string The cleaned up string. */ function cleanup($s, $length = 0) { // Strip out HTML tags. $s = strip_tags($s); // Get rid of double quotes so they don't interfere with the title tag. $s = str_replace('"', '', $s); // Strip out superfluous whitespace and line breaks. $s = preg_replace('/(\s+)/', ' ', $s); // Convert all HTML entities to their character counterparts. $s = html_entity_decode($s, ENT_QUOTES, 'UTF-8'); // Shorten the string to the number of characters requested, and strip wrapping whitespace. if ($length > 0 && strlen($s) > $length) { $s = trim(substr($s, 0, $length)) . '…'; } // Return the value. return $s; } /** * Generates a default name for a given feed URL. * * Takes the feed's permalink, and reduces that to its hostname. * * @access public * @param string $s The url to get the hostname for. * @return string The hostname. */ function name($s) { // Look at the feed homepage URLs and get rid of http://www. and anything after a slash. preg_match('/http(s)?:\/\/(www.)?([^\/]*)/i', $s, $d); // Return the value we want (i.e. just the domain name). return $d[3]; } } ?>