Advertisement
drtanz

Untitled

Mar 5th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.38 KB | None | 0 0
  1. <?php
  2. /*
  3.     Plugin Name: WP RSS Aggregator
  4.     Plugin URI: http://www.jeangalea.com
  5.     Description: Imports and merges multiple RSS Feeds using SimplePie
  6.     Version: 1.0
  7.     Author: Jean Galea
  8.     Author URI: http://www.jeangalea.com
  9.     License: GPLv2
  10. */
  11.  
  12. /*  Copyright 2011 Jean Galea (email : jean@jpgalea.com)
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20.     GNU General Public License for more details.
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  24. */
  25.  
  26.  
  27.     /**
  28.      * Plugin activation procedure
  29.      */      
  30.    
  31.     register_activation_hook( __FILE__, 'wprss_install' );    
  32.    
  33.     function wprss_install() {
  34.         // Activates the plugin and checks for compatible version of WordPress
  35.         if ( version_compare( get_bloginfo( 'version' ), '2.9', '<' ) ) {
  36.             deactivate_plugins ( basename( __FILE__ ));     // Deactivate plugin
  37.             wp_die( "This plugin requires WordPress version 2.9 or higher." );
  38.         }
  39.          
  40.         if ( !wp_next_scheduled( 'wprss_generate_hook' ) ) {            
  41.             // Schedule to run hourly
  42.             wp_schedule_event( time(), 'hourly', 'wprss_generate_hook' );
  43.         }
  44.        
  45.         add_action( 'wprss_generate_hook', 'wp_rss_aggregator' );                
  46.     }
  47.    
  48.    
  49.     /**
  50.      * Plugin deactivation procedure
  51.      */    
  52.    
  53.     register_deactivation_hook( __FILE__, 'wprss_deactivate' );
  54.    
  55.     function wprss_deactivate() {
  56.         // on deactivation remove the cron job
  57.         if ( wp_next_scheduled( 'wprss_generate_hook' ) )
  58.         wp_clear_scheduled_hook( 'wprss_generate_hook' );
  59.     }
  60.      
  61.    
  62.     /**
  63.      * Actions for plugin's options page
  64.      */
  65.    
  66.     // Only load scripts and CSS if we are on this plugin's options page (admin)
  67.     if ( isset( $_GET['page'] ) && $_GET['page'] == 'wprss_aggregator' ) {
  68.         add_action( 'admin_print_scripts', 'wprss_register_scripts' );
  69.         add_action( 'admin_print_styles', 'wprss_header' );
  70.     }    
  71.  
  72.  
  73.     /**
  74.      * Include scripts in plugin page header (admin)
  75.      */    
  76.    
  77.     function wprss_register_scripts() {
  78.          wp_enqueue_script( 'jquery' );
  79.          wp_enqueue_script( 'add-remove', plugins_url( 'scripts/add-remove.js', __FILE__) );
  80.     }
  81.    
  82.    
  83.     /**
  84.      * Include Colorbox-related script and CSS in WordPress head on frontend
  85.      */      
  86.    
  87.     add_action( 'wp_enqueue_scripts', 'wprss_frontend_scripts' );
  88.    
  89.     function wprss_frontend_scripts() {
  90.          wp_enqueue_style( 'styles', plugins_url( 'css/colorbox.css', __FILE__) );
  91.          wp_enqueue_script( 'jquery.colorbox-min', plugins_url( 'scripts/jquery.colorbox-min.js', __FILE__) );        
  92.     }    
  93.    
  94.    
  95.     /**
  96.      * Output JQuery command to trigger Colorbox for links in the <head>
  97.      */  
  98.    
  99.     add_action ( 'wp_head', 'wprss_head_output' );
  100.    
  101.     function wprss_head_output() {
  102.         echo "<script type='text/javascript'>jQuery(document).ready(function(){ jQuery('a.colorbox').colorbox({iframe:true, width:'80%', height:'80%'});});</script>";
  103.     }
  104.  
  105.  
  106.     /**
  107.      * Include CSS in plugin page header
  108.      */
  109.    
  110.     function wprss_header() {        
  111.         wp_enqueue_style( 'styles', plugins_url( 'css/styles.css', __FILE__) );
  112.     }  
  113.    
  114.    
  115.     /**
  116.      * Plugin administration page
  117.      */
  118.    
  119.     // Add the admin options page    
  120.     add_action( 'admin_menu', 'wprss_add_page' );
  121.    
  122.     function wprss_add_page() {
  123.         add_options_page( 'RSS Aggregator', 'RSS Aggregator', 'manage_options', 'wprss_aggregator', 'wprss_options_page' );
  124.     }    
  125.    
  126.     /**
  127.      * Draw options page
  128.      */
  129.    
  130.     function wprss_options_page() {
  131.         ?>
  132.         <div class="wrap">
  133.         <?php screen_icon(); ?>
  134.        
  135.          
  136.         <h2>WordPress RSS Aggregator</h2>
  137.         <div id="options">
  138.         <form action="options.php" method="post">            
  139.         <?php
  140.        
  141.         settings_fields( 'wprss_options' );
  142.         do_settings_sections( 'wprss' );
  143.         $options = get_option( 'wprss_options' );        
  144.         if ( !empty($options) ) {
  145.             $size = count($options);
  146.             for ( $i=1; $i<=$size; $i++ ) {            
  147.                 if( $i % 2 == 0 ) continue;
  148.                 echo "<div class='wprss-input'>";
  149.                
  150.                 $key = key( $options );
  151.                
  152.                 echo "<p><label class='textinput' for='$key'>" . wprss_convert_key( $key ) . "</label>
  153.                <input id='$key' class='wprss-input' size='100' name='wprss_options[$key]' type='text' value='$options[$key]' /></p>";
  154.                
  155.                 next( $options );
  156.                
  157.                 $key = key( $options );
  158.                
  159.                 echo "<p><label class='textinput' for='$key'>" . wprss_convert_key( $key ) . "</label>
  160.                <input id='$key' class='wprss-input' size='100' name='wprss_options[$key]' type='text' value='$options[$key]' /></p>";
  161.                
  162.                 next( $options );
  163.                 echo "</div>";
  164.                
  165.             }
  166.         }
  167.        
  168.         $siteurl = get_option('siteurl');
  169.         $images_url = $siteurl . '/wp-content/plugins/' . basename(dirname(__FILE__)) . '/images';
  170.         ?>
  171.         <div id="buttons"><a href="#" id="add"><img src="<?php echo $images_url; ?>/add.png"></a>  
  172.         <a href="#" id="remove"><img src="<?php echo $images_url; ?>/remove.png"></a></div>  
  173.         <p class="submit"><input type="submit" value="Save Settings" name="submit" class="button-primary"></p>
  174.        
  175.         </form>
  176.         </div>
  177.         </div>
  178.         <?php
  179.     }
  180.    
  181.    
  182.     /**
  183.      * Register and define settings
  184.      */
  185.    
  186.     add_action( 'admin_init', 'wprss_admin_init' );
  187.    
  188.     function wprss_admin_init() {
  189.         register_setting( 'wprss_options', 'wprss_options' );
  190.         add_settings_section( 'wprss_main', 'WP RSS Aggregator settings', 'wprss_section_text', 'wprss' );      
  191.     }    
  192.  
  193.  
  194.     /**
  195.      * Explain the options section
  196.      */
  197.    
  198.     function wprss_section_text() {
  199.         echo '<p>Enter a name and URL for each of your feeds. The name is used just for your reference.</p>';
  200.     }
  201.    
  202.    
  203.     /**
  204.      * Convert from field name to user-friendly name
  205.      */
  206.    
  207.     function wprss_convert_key( $key ) {
  208.         if ( strpos( $key, 'feed_name_' ) === 0 ) {
  209.             $label = str_replace( 'feed_name_', 'Feed name ', $key );
  210.         }
  211.        
  212.         else if ( strpos( $key, 'feed_url_' ) === 0 ) {
  213.             $label = str_replace( 'feed_url_', 'Feed URL ', $key );
  214.         }
  215.         return $label;
  216.     }
  217.    
  218.    
  219.     /**
  220.      * Set up shortcodes and call the main function for output
  221.      */    
  222.    
  223.     // Register a new shortcode
  224.     add_shortcode( 'wp_rss_aggregator', 'wprss_shortcode');
  225.    
  226.     function wprss_shortcode( $atts ) {    
  227.         if ( !empty ($atts) ) {
  228.             foreach ( $atts as $key => &$val ) {
  229.                 $val = html_entity_decode($val);
  230.             }
  231.         }
  232.         wp_rss_aggregator( $atts );      
  233.     }
  234.  
  235.  
  236.     /**
  237.      * Get feeds and output the aggregation
  238.      */    
  239.        
  240.     function wp_rss_aggregator( $args = array() ) {
  241.        
  242.         $defaults = array(
  243.                           'date_before' => '<h3>',
  244.                           'date_after' => '</h3>',
  245.                           'links_before' => '<ul>',
  246.                           'links_after' => '</ul>',
  247.                           'link_before' => '<li>',
  248.                           'link_after' => '</li>'                          
  249.                     );
  250.        
  251.         // Parse incoming $args into an array and merge it with $defaults          
  252.     $args = wp_parse_args( $args, $defaults );
  253.         // Declare each item in $args as its own variable
  254.         extract( $args, EXTR_SKIP );        
  255.        
  256.         $wprss_options = get_option( 'wprss_options', 'option not found' );  
  257.    
  258.         foreach ( $wprss_options as $key => $value ) {            
  259.             if ( strpos( $key, 'feed_url_' ) === 0 ) {                
  260.                 $feed_uris[] = $value;
  261.             }
  262.         }        
  263.        
  264.         if ( !empty( $feed_uris ) ) {          
  265.             // update feeds every hour else serve from cache
  266.             function wprss_hourly_feed() { return 3600; }
  267.             add_filter( 'wp_feed_cache_transient_lifetime', 'wprss_hourly_feed' );
  268.             $feed = fetch_feed( $feed_uris );    
  269.         }
  270.         else echo 'No feed defined';
  271.         remove_filter( 'wp_feed_cache_transient_lifetime', 'wprss_hourly_feed' );
  272.        
  273.         $items = $feed->get_items();        
  274.         $items_today = array();
  275.         $items_yesterday = array();
  276.         $items_two_days_ago = array();
  277.         $items_older = array();
  278.        
  279.        
  280.         foreach ( $items as $item ):        
  281.             $item_date = $item->get_date('l jS F (Y-m-d)');
  282.             if ( $item_date == date('l jS F (Y-m-d)', strtotime('today') ) ) {
  283.                 $items_today[] = $item;
  284.             }
  285.             else if ( $item_date == date('l jS F (Y-m-d)', strtotime('yesterday') ) ) {
  286.                 $items_yesterday[] = $item;
  287.             }
  288.             else if ( $item_date == date('l jS F (Y-m-d)', strtotime('-2 days') ) ) {
  289.                 $items_two_days_ago[] = $item;
  290.             }
  291.             else {
  292.                 $items_older[] = $item;
  293.             }                  
  294.         endforeach;
  295.        
  296.         if ( !empty( $items_today ) ) {
  297.             echo $date_before . 'Today' . $date_after;
  298.             echo $links_before;
  299.             foreach ( $items_today as $item ) {
  300.                
  301.                 echo $link_before . '<a class="colorbox" href="' . $item->get_permalink() .'">'. $item->get_title(). ' '. '</a>';
  302.                 echo '<br><span class="feed-source">Source: '.$item->get_feed()->get_title()/* . ' | ' . $item->get_date('l jS F').''*/ . '</span>';
  303.                 echo $link_after;
  304.                
  305.             }
  306.             echo $links_after;
  307.         }
  308.        
  309.         if ( !empty( $items_yesterday ) ) {
  310.             echo $date_before . 'Yesterday' . $date_after;
  311.             echo $links_before;
  312.             foreach ( $items_yesterday as $item ) {
  313.                 echo '<li><a class="colorbox" href="' . $item->get_permalink() .'">'. $item->get_title(). ' '. '</a>';
  314.                 echo '<br><span class="feed-source">Source: '.$item->get_feed()->get_title()/* . ' | ' . $item->get_date('l jS F').''*/ . '</span>';
  315.                 echo $link_after;
  316.             }
  317.             echo $links_after;
  318.         }
  319.        
  320.         if ( !empty( $items_two_days_ago ) ) {
  321.             echo $date_before . '2 days ago' . $date_after;
  322.             echo $links_before;
  323.             foreach ( $items_two_days_ago as $item ) {
  324.                 echo '<li><a class="colorbox" href="' . $item->get_permalink() .'">'. $item->get_title(). ' '. '</a>';
  325.                 echo '<br><span class="feed-source">Source: '.$item->get_feed()->get_title()/* . ' | ' . $item->get_date('l jS F').''*/ . '</span>';
  326.                 echo $link_after;
  327.             }
  328.             echo $links_after;
  329.         }
  330.         if ( !empty( $items_older ) ) {
  331.             echo $date_before . 'More than 2 days ago' . $date_after;
  332.             echo $links_before;
  333.             foreach ( $items_older as $item ) {
  334.                 echo '<li><a class="colorbox" href="' . $item->get_permalink() .'">'. $item->get_title(). ' '. '</a>';
  335.                 echo '<br><span class="feed-source">Source: '.$item->get_feed()->get_title() . ' | ' . $item->get_date('l jS F').'</span>';
  336.                 echo $link_after;
  337.             }          
  338.             echo $links_after;
  339.         }
  340.     }
  341.    
  342.     // use just for testing - runs on each wp load
  343.     //add_action( 'wp_loaded', 'wp_rss_aggregator' );
  344.        
  345. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement