. */ include_once(ABSPATH . WPINC . '/rss.php'); global $google_news_instance; if ( ! class_exists('google_news_plugin')) { class google_news_plugin { // So we don't have to query database on every replacement var $settings; var $regions = array( 'Australia' => 'au', 'India' => 'in', 'Israel' => 'en_il', 'Malaysia' => 'en_my', 'New Zealand' => 'nz', 'Pakistan' => 'en_pk', 'Philippines' => 'en_ph', 'Singapore' => 'en_sg', 'العالم العربي (Arabic)' => 'ar_me', '中国版 (China)' => 'cn', '香港版 (Hong Kong)' => 'hk', 'भारत (India)' => 'hi_in', 'தமிழ் (India)' => 'ta_in', 'മലയാളം (India)' => 'ml_in', 'తెలుగు (India)' => 'te_in', 'ישראל (Israel)' => 'iw_il', '日本 (Japan)' => 'jp', '한국 (Korea)' => 'kr', '台灣版 (Taiwan)' => 'tw', 'Việt Nam (Vietnam)' => 'vi_vn', '-------------' => 'us', 'België' => 'nl_be', 'Belgique' => 'fr_be', 'Botswana' => 'en_bw', 'Česká republika' => 'cs_cz', 'Deutschland' => 'de', 'España' => 'es', 'Ethiopia' => 'en_et', 'France' => 'fr', 'Ghana' => 'en_gh', 'Ireland' => 'en_ie', 'Italia' => 'it', 'Kenya' => 'en_ke', 'Magyarország' => 'hu_hu', 'Namibia' => 'en_na', 'Nederland' => 'nl_nl', 'Nigeria' => 'en_ng', 'Norge' => 'no_no', 'Österreich' => 'de_at', 'Polska' => 'pl_pl', 'Portugal' => 'pt:PT_pt', 'Schweiz' => 'de_ch', 'South Africa' => 'en_za', 'Suisse' => 'fr_ch', 'Sverige' => 'sv_se', 'Tanzania' => 'en_tz', 'Türkiye' => 'tr_tr', 'Uganda' => 'en_ug', 'U.K.' => 'uk', 'Zimbabwe' => 'en_zw', 'Ελλάδα (Greece)' => 'el_gr', 'Россия (Russia)' => 'ru_ru', 'Украина (Ukraine)' => 'ru_ua', 'Україна (Ukraine)' => 'uk_ua', '------------' => 'us', 'Argentina' => 'es_ar', 'Brasil' => 'pt:BR_br', 'Canada English' => 'ca', 'Canada Français' => 'fr_ca', 'Chile' => 'es_cl', 'Colombia' => 'es_co', 'Cuba' => 'es_cu', 'Estados Unidos' => 'es_us', 'México' => 'es_mx', 'Perú' => 'es_pe', 'U.S.' => 'us', 'Venezuela' => 'es_ve', ); var $newstypes = array( 'All' => '', 'Top News' => 'h', 'Foreign' => 'w', 'Domestic' => 'n', 'Business' => 'b', 'Sci/Tech' => 't', 'Health' => 'm', 'Sports' => 's', 'Entertainment' => 'e', ); var $outputtypes = array( 'Standard' => '', 'Text Only' => 't', 'With Images' => '&imv=1', ); var $desctypes = array( 'Short' => '', 'Long' => 'l', ); // Constructor function google_news_plugin() { // Form POSTs dealt with elsewhere if ( is_array($_POST) ) { if ( $_POST['google_news-widget-submit'] ) { $tmp = $_POST['google_news-widget-feed']; $alloptions = get_option('google_news'); if ( $alloptions['widget-1'] != $tmp ) { if ( $tmp == '*DEFAULT*' ) { $alloptions['widget-1'] = ''; } else { $alloptions['widget-1'] = $tmp; } update_option('google_news', $alloptions); } } else if ( $_POST['google_news-options-submit'] ) { // noop } else if ( $_POST['google_news-submit'] ) { // noop } } add_filter('the_content', array(&$this, 'insert_news')); add_action('admin_menu', array(&$this, 'admin_menu')); add_action('plugins_loaded', array(&$this, 'widget_init')); // Hook for theme coders/hackers add_action('google_news', array(&$this, 'display_feed')); // Makes it backwards compat pre-2.5 I hope if ( function_exists('add_shortcode') ) { add_shortcode('google-news', array(&$this, 'my_shortcode_handler')); } } // *************** Admin interface ****************** // Callback for admin menu function admin_menu() { add_options_page('Google News Options', 'Google News', 'administrator', __FILE__, array(&$this, 'plugin_options')); add_management_page('Google News', 'Google News', 'administrator', __FILE__, array(&$this, 'admin_manage')); } // Settings -> Google News function plugin_options() { if (get_bloginfo('version') >= '2.7') { $manage_page = 'tools.php'; } else { $manage_page = 'edit.php'; } print <<

Google News

This plugin allows you to define a number of Google News feeds and have them displayed anywhere in content, in a widget or in a theme. Any number of inline replacements or theme inserts can be made, but only one widget instance is permitted in this release. To use the feeds insert one or more of the following special html comments or Shortcodes anywhere in user content. Note that Shortcodes, i.e. the ones using square brackets, are only available in WordPress 2.5 and above.

To insert in a theme call do_action('google_news'); or alternatively do_action('google_news', 'feedname');

To manage feeds, go to Manage -> Google News, where you will also find more information.

Donations Page... ;-)

Widget Home Page, leave a comment if you have questions etc.

Google Terms Of Use

EOT; } // Manage -> Google News function admin_manage() { // Edit/delete links $mode = trim($_GET['mode']); $id = trim($_GET['id']); $this->upgrade_options(); $alloptions = get_option('google_news'); $flipregions = array_flip($this->regions); $flipnewstypes = array_flip($this->newstypes); $flipoutputtypes = array_flip($this->outputtypes); $flipdesctypes = array_flip($this->desctypes); if ( is_array($_POST) && $_POST['google_news-submit'] ) { $newoptions = array(); $id = $_POST['google_news-id']; $newoptions['name'] = $_POST['google_news-name']; $newoptions['title'] = $_POST['google_news-title']; $newoptions['region'] = $_POST['google_news-region']; $newoptions['newstype'] = $_POST['google_news-newstype']; $newoptions['outputtype'] = $_POST['google_news-outputtype']; $newoptions['desctype'] = $_POST['google_news-desctype']; $newoptions['numnews'] = $_POST['google_news-numnews']; $newoptions['query'] = $_POST['google_news-query']; $newoptions['feedtype'] = $flipregions[$newoptions['region']].' : '. $flipnewstypes[$newoptions['newstype']]; if ( $alloptions['feeds'][$id] == $newoptions ) { $text = 'No change...'; $mode = 'main'; } else { $alloptions['feeds'][$id] = $newoptions; update_option('google_news', $alloptions); $mode = 'save'; } } else if ( is_array($_POST) && $_POST['google_news-options-cachetime-submit'] ) { if ( $_POST['google_news-options-cachetime'] != $alloptions['cachetime'] ) { $alloptions['cachetime'] = $_POST['google_news-options-cachetime']; update_option('google_news', $alloptions); $text = "Cache time changed to {$alloptions[cachetime]} seconds."; } else { $text = "No change in cache time..."; } $mode = 'main'; } if ( $mode == 'newfeed' ) { $newfeed = 0; foreach ($alloptions['feeds'] as $k => $v) { if ( $k > $newfeed ) { $newfeed = $k; } } $newfeed += 1; $text = "Please configure new feed and press Save."; $mode = 'main'; } if ( $mode == 'save' ) { $text = "Saved feed {$alloptions[feeds][$id][name]} [$id]."; $mode = 'main'; } if ( $mode == 'edit' ) { if ( ! empty($text) ) { echo '

'.$text.'

'; } $text = "Editing feed {$alloptions[feeds][$id][name]} [$id]."; $edit_id = $id; $mode = 'main'; } if ( $mode == 'delete' ) { $text = "Deleted feed {$alloptions[feeds][$id][name]} [$id]."; unset($alloptions['feeds'][$id]); update_option('google_news', $alloptions); $mode = 'main'; } // main if ( empty($mode) or ($mode == 'main') ) { if ( ! empty($text) ) { echo '

'.$text.'

'; } print '
'; print '

'; print _e('Manage Google News Feeds','google_news'); print '

'; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; print ' '; if (get_bloginfo('version') >= '2.7') { $manage_page = 'tools.php'; } else { $manage_page = 'edit.php'; } if ( $alloptions['feeds'] || $newfeed ) { $i = 0; foreach ($alloptions['feeds'] as $key => $val) { if ( $i % 2 == 0 ) { print ''; } else { print ''; } if ( isset($edit_id) && $edit_id == $key ) { print ""; print ""; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '"; print ""; print ""; print ""; } else { print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print ""; print "\n"; print "\n"; } print ''; $i++; } if ( $newfeed ) { print ""; print ""; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '"; print ""; print ""; print ""; print ""; } else { print ""; } } else { print ''; print ""; } print '
'; print _e('Key','google_news'); print ''; print _e('Name','google_news'); print ''; print _e('Admin-defined title','google_news'); print ''; print _e('Region','google_news'); print ''; print _e('Type','google_news'); print ''; print _e('Output','google_news'); print ''; print _e('Item length','google_news'); print ''; print _e('Max items','google_news'); print ''; print _e('Optional query filter','google_news'); print ''; print _e('Action','google_news'); print '
".$key."'; print "".$key."".$val['name']."".$val['title']."".$flipregions[$val['region']]."".$flipnewstypes[$val['newstype']]."".$flipoutputtypes[$val['outputtype']]."".$flipdesctypes[$val['desctype']]."".$val['numnews']."".$val['query'].""; print __('Edit','google_news'); print ""; print __('Delete', 'google_news'); print "
".$newfeed."'; print "
"; print __('Add extra feed','google_news'); print "
'; print __('No feeds found(!)','google_news'); print '
"; print __('Add feed','google_news'); print "
'; print '

'; print _e('Global configuration parameters','google_news'); print '

'; print '
'; print ' '; print ''; print ''; print ''; print ''; print '
Cache time: seconds
'; print '
'; print '

'; print _e('Information','google_news'); print '

'; print ' '; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '
KeyUnique identifier used internally.
NameOptional name to be able to reference a specific feed as e.g. '; print ' <!--google_news#myname-->. '; print ' If more than one feed shares the same name, a random among these will be picked each time. '; print ' The one(s) without a name will be treated as the default feed(s), i.e. used for <!--google_news--> '; print ' or widget feed type *DEFAULT*. If you have Wordpress 2.5 '; print ' or above, you can also use Shortcodes on the form [google-news] '; print ' (for default feed) or [google-news name="feedname"]. And finally '; print ' you can use do_action(\'google_news\'); or do_action(\'google_news\', \'feedname\'); '; print ' in themes.
Admin-defined titleOptional feed title. If not set, a reasonable title based on '; print 'Region and Type will be used. Note Google Terms of Service require you to show that the feeds come from '; print 'Google News.
RegionThe region/language of the feed.
TypeThe type of news to present.
OutputText only, allow for images or images with most news items. Note that '; print 'there will be text in all three cases.
Item lengthSingle sentence news items or 2-3 lines of text.
Max itemsMaximum number of news items to show for this feed. If the feed contains '; print 'less than the requested items, only the number of items in the feed will obviously be displayed.
Optional query filterPass the requested news through a query filter for very '; print 'detailed control over the type of news to show. E.g. only sports news about the Yankees.
In all cases, output will depend on original news source and can and will '; print 'differ from source to source. Google hasn\'t really done a great job with respect to formatting. '; print 'Note specifically that a query filter will change the output slightly, as this is how Google wants it.
Cache timeMinimum number of seconds that WordPress should cache a Google News feed before fetching it again.
'; print '
'; } } // ************* Output ***************** // The function that gets called from themes function display_feed($data) { global $settings; $settings = get_option('google_news'); print $this->random_feed($data); unset($settings); } // Callback for inline replacement function insert_news($data) { global $settings; // Allow for multi-feed sites $tag = '//'; // We may have old style options $this->upgrade_options(); // Avoid getting this for each callback $settings = get_option('google_news'); $result = preg_replace_callback($tag, array(&$this, 'inline_replace_callback'), $data); unset($settings); return $result; } // *********** Widget support ************** function widget_init() { // Check for the required plugin functions. This will prevent fatal // errors occurring when you deactivate the dynamic-sidebar plugin. if ( !function_exists('register_sidebar_widget') ) return; register_widget_control('Google News', array(&$this, 'widget_control'), 200, 100); // wp_* has more features, presumably fixed at a later date register_sidebar_widget('Google News', array(&$this, 'widget_output')); } function widget_control() { // We may have old style options $this->upgrade_options(); $alloptions = get_option('google_news'); $thisfeed = $alloptions['widget-1']; print '

'; print '

'; print ''; } // Called every time we want to display ourselves as a sidebar widget function widget_output($args) { extract($args); // Gives us $before_ and $after_ I presume // We may have old style options $this->upgrade_options(); $alloptions = get_option('google_news'); $matching_feeds = array(); foreach ($alloptions['feeds'] as $k => $v) { if ( (string)$v['name'] == $alloptions['widget-1'] ) { $matching_feeds[] = $k; } } if ( ! count($matching_feeds) ) { if ( ! strlen($alloptions['widget-1']) ) { $content = '

'; } else { $content = ""; } echo $before_widget; echo $before_title . __('Google News
Error','google_news') . $after_title . '
'; echo $content; echo '
' . $after_widget; return; } $feed_id = $matching_feeds[rand(0, count($matching_feeds)-1)]; $options = $alloptions['feeds'][$feed_id]; $feedtype = $options['feedtype']; $cachetime = $alloptions['cachetime']; if ( strlen($options['title']) ) { $title = $options['title']; } else { $title = 'Google News
'.$feedtype; } echo $before_widget; echo $before_title . $title . $after_title . '
'; echo $this->get_feed($options, $cachetime); echo '
' . $after_widget; } // ************** The actual work **************** function get_feed(&$options, $cachetime) { if ( ! isset($options['region']) ) { return 'Options not set, visit plugin configuation screen.'; } $region = $options['region'] ? $options['region'] : 'us'; $newstype = $options['newstype']; $outputtype = $options['outputtype']; $query = $options['query']; $numnews = $options['numnews'] ? $options['numnews'] : 5; $desctype = $options['desctype']; $result = ''; } $rss->items = array_slice($rss->items, 0, $numnews); foreach ( $rss->items as $item ) { $description = $this->html_decode($item['description']); // All this is bound to break, but Google // doesn't know usable markup from squat // As per Google TOC, we need to retain related link preg_match('|(]+>[^<]+)|', $description, $related); // Try some tricks to lose useless markup $bloc = strpos($description, ']+>/','',$description); $patterns = array( '/<(td|tr|table|div|font|ul|li)[^>]*>/', '/<.(td|tr|table|div|font|ul|li)[^>]*>/', ); $replacements = array( '', '', ); $description = preg_replace($patterns, $replacements, $description); $description = preg_replace('|
|', '', $description, 1); $description = preg_replace('|(]+>)
([^<]+)|', '\\1\\2
', $description, 1); $description = preg_replace('|
html_decode($item['title']); $date = $item['pubdate']; $link = $item['link']; if ( strlen($desctype) ) { $result .= "
  • $description
  • "; } else { $result .= "
  • $title
    $related[1]
  • "; } } return $result.''; } // *********** Shortcode support ************** function my_shortcode_handler($atts, $content=null) { global $settings; $settings = get_option('google_news'); return $this->random_feed($atts['name']); unset($settings); } // *********** inline replacement callback support ************** function inline_replace_callback($matches) { if ( ! strlen($matches[1]) ) { // Default $feedname = ''; } else { $feedname = substr($matches[1], 1); // Skip # } return $this->random_feed($feedname); } // ************** Support functions **************** function random_feed($name) { global $settings; $matching_feeds = array(); foreach ($settings['feeds'] as $k => $v) { if ( (string)$v['name'] == $name ) { $matching_feeds[] = $k; } } if ( ! count($matching_feeds) ) { if ( ! strlen($name) ) { return '
      No default feed available
    '; } else { return "
      Unknown feed name $name used
    "; } } $feed_id = $matching_feeds[rand(0, count($matching_feeds)-1)]; $feed = $settings['feeds'][$feed_id]; if ( strlen($feed['title']) ) { $title = $feed['title']; } else { $title = 'Google News : '.$feed['feedtype']; } $result = ''; $result .= "

    $title

    "; $result .= $this->get_feed($feed, $settings['cachetime']); $result .= '
    '; return $result; } function html_decode($in) { $patterns = array( '/&/', '/"/', '/</', '/>/', ); $replacements = array( '&', '"', '<', '>', ); $tmp = preg_replace($patterns, $replacements, $in); return preg_replace('/'/','\'',$tmp); } // Unfortunately, we didn't finalize on a data structure // until version 2.1ish of the plugin so we need to upgrade // if needed function upgrade_options() { $options = get_option('google_news'); if ( !is_array($options) ) { // From 1.0 $oldoptions = get_option('widget_google_news_widget'); if ( is_array($oldoptions) ) { $flipregions = array_flip($this->regions); $flipnewstypes = array_flip($this->newstypes); $tmpfeed = array(); $tmpfeed['title'] = $oldoptions['title']; $tmpfeed['name'] = ''; $tmpfeed['numnews'] = $oldoptions['numnews']; $tmpfeed['region'] = $oldoptions['region']; $tmpfeed['newstype'] = $oldoptions['newstype']; $tmpfeed['outputtype'] = $oldoptions['outputtype']; $tmpfeed['query'] = $oldoptions['query']; $tmpfeed['feedtype'] = $flipregions[$tmpfeed['region']]. ' : '. $flipnewstypes[$tmpfeed['newstype']]; $options = array(); $options['feeds'] = array( $tmpfeed ); $options['widget-1'] = 0; $options['cachetime'] = 300; delete_option('widget_google_news_widget'); update_option('google_news', $options); } else { // First time ever $options = array(); $options['feeds'] = array( $this->default_feed() ); $options['widget-1'] = 0; $options['cachetime'] = 300; update_option('google_news', $options); } } else { // From 2.0/2.0.1 to 2.1 if ( array_key_exists('region', $options) ) { $newoptions = array('feeds' => array( $options)); $newoptions['feeds'][0]['name'] = ''; $newoptions['widget-1'] = 0; $newoptions['cachetime'] = 300; update_option('google_news', $newoptions); } else if ( 0 ) { // Messed up options, start from scratch $options = array(); $options['feeds'] = array( $this->default_feed() ); $options['widget-1'] = 0; $options['cachetime'] = 300; update_option('google_news', $options); } } } function default_feed() { return array( 'numnews' => 5, 'region' => 'us', 'name' => '', 'feedtype' => 'U.S. : All'); } } // Instantiate $google_news_instance &= new google_news_plugin(); } ?>