Plugin Directory

Changeset 871328


Ignore:
Timestamp:
03/07/2014 05:40:16 PM (12 years ago)
Author:
lightsystem
Message:

version 0.5.1

Location:
rss-synchronization
Files:
21 added
5 edited

Legend:

Unmodified
Added
Removed
  • rss-synchronization/trunk/README.txt

    r871189 r871328  
    44Requires at least: 3.8
    55Tested up to: 3.8.1
    6 Stable tag: 0.5.0
     6Stable tag: 0.5.1
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    1515
    1616You will find these settings in your administration page on the Settings->RSS Settings sub-menu.
     17
     18Check out the [project on GitHub](https://github.com/LightSystem/WordPress-Plugin-RSS-Sync).
    1719
    1820== Installation ==
     
    3840
    39411. Download `rss-sync.zip`
    40 2. Extract the `plugin-name` directory to your computer
    41 3. Upload the `plugin-name` directory to the `/wp-content/plugins/` directory
     422. Extract the `rss-sync` directory to your computer
     433. Upload the `rss-sync` directory to the `/wp-content/plugins/` directory
    42444. Activate the plugin in the Plugin dashboard
    4345
    4446== Changelog ==
     47
     48= 0.5.1 =
     49* Improved behaviour by instantly fetching feeds as soon as they are added in the settings.
    4550
    4651= 0.5.0 =
  • rss-synchronization/trunk/admin/class-rss-sync-admin.php

    r871161 r871328  
    2121     * Instance of this class.
    2222     *
    23      * @since    0.3.0
    24      *
    2523     * @var      object
    2624     */
     
    2927    /**
    3028     * Slug of the plugin screen.
    31      *
    32      * @since    0.3.0
    3329     *
    3430     * @var      string
  • rss-synchronization/trunk/admin/views/admin.php

    r871161 r871328  
    125125            return $options;
    126126
     127        if($existing['rss_feeds'] != $options['rss_feeds']){
     128            include_once( ABSPATH . 'wp-content/plugins/rss-sync/includes/class-rss-sync-tools.php' );
     129           
     130            $tools = RSS_Sync_Tools::get_instance();
     131            $tools->rss_sync_fetch($options['rss_feeds']);
     132        }
     133
    127134        if($existing['refresh'] != $options['refresh']){
    128135
  • rss-synchronization/trunk/public/class-rss-sync.php

    r871161 r871328  
    99 */
    1010
    11 include_once( ABSPATH . WPINC . '/feed.php' );
    12 include_once( ABSPATH . 'wp-admin/includes/image.php' );
     11include_once(ABSPATH . 'wp-content/plugins/rss-sync/includes/class-rss-sync-tools.php');
    1312
    1413/**
     
    3231     * @var     string
    3332     */
    34     const VERSION = '0.5.0';
     33    const VERSION = '0.5.1';
    3534
    3635    const RSS_ID_CUSTOM_FIELD = 'rss_id';
     
    4544     * plugin file.
    4645     *
    47      * @since    0.2.0
    48      *
    4946     * @var      string
    5047     */
     
    5350    /**
    5451     * Instance of this class.
    55      *
    56      * @since    0.2.0
    5752     *
    5853     * @var      object
     
    6459     * and styles.
    6560     *
    66      * @since     0.2.0
    6761     */
    6862    private function __construct() {
     
    8478        add_filter( 'wp_feed_cache_transient_lifetime', function ($seconds){ return 3600; } );
    8579
    86         add_action( 'rss_sync_event', array( $this, 'rss_sync_fetch' ) );
     80        $rss_sync_tools = RSS_Sync_Tools::get_instance();
     81        add_action( 'rss_sync_event', array( $rss_sync_tools, 'rss_sync_fetch' ) );
    8782    }
    8883
    8984    /**
    9085     * Return the plugin slug.
    91      *
    92      * @since    0.2.0
    9386     *
    9487     * @return    Plugin slug variable.
     
    10093    /**
    10194     * Return an instance of this class.
    102      *
    103      * @since     0.2.0
    10495     *
    10596     * @return    object    A single instance of this class.
     
    117108    /**
    118109     * Fired when the plugin is activated.
    119      *
    120      * @since    0.2.0
    121110     *
    122111     * @param    boolean    $network_wide    True if WPMU superadmin uses
     
    155144     * Fired when the plugin is deactivated.
    156145     *
    157      * @since    0.2.0
    158      *
    159146     * @param    boolean    $network_wide    True if WPMU superadmin uses
    160147     *                                       "Network Deactivate" action, false if
     
    193180     * Fired when a new site is activated with a WPMU environment.
    194181     *
    195      * @since    0.2.0
    196      *
    197182     * @param    int    $blog_id    ID of the new blog.
    198183     */
     
    215200     * - not deleted
    216201     *
    217      * @since    0.2.0
    218      *
    219202     * @return   array|false    The blog ids, false if no matches.
    220203     */
     
    243226        if($options){
    244227            $chosen_recurrence = $options['refresh'];
    245                 }
     228        }
    246229
    247230        if($chosen_recurrence){
    248231            wp_schedule_event( time(), $chosen_recurrence, 'rss_sync_event' );
    249                 } else {
     232        } else {
    250233            wp_schedule_event( time(), 'daily', 'rss_sync_event' );
    251                 }
     234        }
    252235    }
    253236
     
    296279    }
    297280
    298     /**
    299      * Does all the work of fetching specified RSS feeds, as well as create the associated posts.
    300      *
    301      *        Actions:    http://codex.wordpress.org/Plugin_API#Actions
    302      *        Reference:  http://codex.wordpress.org/Plugin_API/Action_Reference
    303      *
    304      * @since    0.2.0
    305      */
    306     public function rss_sync_fetch() {
    307 
    308         $options = get_option( 'rss_sync' );
    309 
    310         $rss_feeds_to_fetch = explode("\n", $options['rss_feeds']);
    311 
    312         if($rss_feeds_to_fetch){
    313             foreach ($rss_feeds_to_fetch as $rss_feed) {
    314                 $this->handle_RSS_feed($rss_feed);
    315             }
    316         }
    317     }
    318 
    319     /**
    320     * Fetch and process a single RSS feed.
    321     *
    322     * @since    0.3.0
    323     */
    324     private function handle_RSS_feed($rss_feed){
    325 
    326         // Get a SimplePie feed object from the specified feed source.
    327         $rss = fetch_feed( $rss_feed );
    328 
    329         if ( ! is_wp_error( $rss ) ) : // Checks that the object is created correctly
    330             $channel_title = $rss->get_title();
    331             $post_cat_id   = $this->cat_id_by_name($channel_title);
    332 
    333             $maxitems = $rss->get_item_quantity( 0 );
    334 
    335             // Build an array of all the items, starting with element 0 (first element).
    336             $rss_items = $rss->get_items( 0, $maxitems );
    337 
    338             //Loop through each feed item and create a post with the associated information
    339             foreach ( $rss_items as $item ) :
    340 
    341                 $item_id       = $item->get_id(false);
    342                 $item_pub_date = date($item->get_date('Y-m-d H:i:s'));
    343 
    344                 $item_categories = $item->get_categories();
    345                 $post_tags       = $this->extract_tags($item_categories);
    346 
    347                 $custom_field_query = new WP_Query(array( 'meta_key' => RSS_ID_CUSTOM_FIELD, 'meta_value' => $item_id ));
    348 
    349                 if($custom_field_query->have_posts()){
    350                     $post = $custom_field_query->next_post();
    351 
    352                     if (strtotime( $post->post_modified ) < strtotime( $item_pub_date )) {
    353                         $post->post_content  = $item->get_description(false);
    354                         $post->post_title    = $item->get_title();
    355                         $post->post_modified = $item_pub_date;
    356 
    357                         $updated_post_id = wp_update_post( $post );
    358 
    359                         if($updated_post_id != 0){
    360                             wp_set_object_terms( $updated_post_id, $post_cat_id, 'category', false );
    361                             wp_set_post_tags( $updated_post_id, $post_tags, false );
    362 
    363                             if($this->is_image_import()){
    364                                 //Image importing routines
    365                                 $post_data = array(
    366                                     'post_content' => $post->post_content,
    367                                     'post_date' => $post->post_modified
    368                                 );
    369 
    370                                 $processed_post_content = $this->process_image_tags($post_data, $updated_post_id);
    371 
    372                                 //Update post content
    373                                 if(!is_wp_error( $processed_post_content )){
    374                                     $this->update_post_content($processed_post_content, $updated_post_id);
    375                                 }
    376                             }
    377                         }
    378                     }
    379 
    380                 } else {
    381 
    382                     $post = array(
    383                       'post_content' => $item->get_description(false), // The full text of the post.
    384                       'post_title'   => $item->get_title(), // The title of the post.
    385                       'post_status'  => 'publish',
    386                       'post_date'    => $item_pub_date, // The time the post was made.
    387                       'tags_input'   => $post_tags
    388                     );
    389 
    390                     $inserted_post_id = wp_insert_post( $post );
    391 
    392                     if($inserted_post_id != 0){
    393                         wp_set_object_terms( $inserted_post_id, $post_cat_id, 'category', false );
    394                         update_post_meta($inserted_post_id, RSS_ID_CUSTOM_FIELD, $item_id);
    395 
    396                         if($this->is_image_import()){
    397                             //Import images to media library
    398                             $processed_post_content = $this->process_image_tags($post, $inserted_post_id);
    399 
    400                             //Update post content
    401                             if( !is_wp_error( $processed_post_content ) ){
    402                                 $this->update_post_content($processed_post_content, $inserted_post_id);
    403                             }
    404                         }
    405                     }
    406                 }
    407 
    408             endforeach;
    409         endif;
    410 
    411     }
    412 
    413     private function is_image_import(){
    414 
    415         $options = get_option( 'rss_sync' );
    416 
    417         return $options['img_storage'] == 'local_storage';
    418     }
    419 
    420     private function update_post_content($post_content, $post_id){
    421 
    422         $post = get_post( $post_id );
    423         $post->post_content = $post_content;
    424 
    425         return wp_update_post($post) != 0;
    426     }
    427 
    428     /**
    429     * Handles creation and/or resolution of a category ID. 
    430     *   
    431     * @since    0.4.0
    432     */
    433     private function cat_id_by_name($cat_name){
    434 
    435         $cat_id = get_cat_ID($cat_name);
    436 
    437         if($cat_id == 0){
    438             $cat_id = wp_insert_term( $cat_name, 'category' );
    439         }
    440 
    441         return $cat_id;
    442     }
    443 
    444     /**
    445     * Handles extraction of post tags from a list of RSS item categories.
    446     *
    447     * @since    0.4.0
    448     */
    449     private function extract_tags($rss_item_cats){
    450 
    451         $post_tags = array();
    452 
    453         foreach ($rss_item_cats as $category) {
    454 
    455             $raw_tag = $category->get_term();
    456 
    457             array_push($post_tags, str_replace(' ', '-', $raw_tag));
    458 
    459         }
    460 
    461         return $post_tags;
    462     }
    463 
    464     /**
    465     * Parses text content, looking for image tags. Handles fetching external image if needed.
    466     * Returns processed text with image tags now pointing to images locally stored.
    467     */
    468     private function process_image_tags($post, $post_id){
    469 
    470         if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post['post_content'], $matches)){
    471             $images_array = $matches [1];
    472 
    473             foreach ($images_array as $image) {
    474                 $upload = $this->get_img_attachment($post_id, $image);
    475 
    476                 if(!$upload){
    477                     $upload = $this->fetch_remote_image($image, $post, $post_id);
    478                 }
    479 
    480                 if ( is_wp_error( $upload ) ){
    481                     write_log('UPLOAD');
    482                     write_log($upload);
    483 
    484                     return $upload;
    485                 }
    486 
    487                 $post_content = str_replace($image, $upload['url'], $post['post_content']);
    488 
    489                 return $post_content;
    490             }
    491         }
    492 
    493         return $post['post_content'];
    494     }
    495 
    496     /**
    497     * Checks if image already exists in media library. Returns its URL if it does, returns false if it does not.
    498     */
    499     function get_img_attachment($post_id, $external_img_url){
    500 
    501         $attachments = new WP_Query( array( 'post_status' => 'any', 'post_type' => 'attachment', 'post_parent' => $post_id ) );
    502 
    503         while($attachments->have_posts()){
    504             $attachment = $attachments->next_post();
    505 
    506             $metadata = wp_get_attachment_metadata($attachment->ID);
    507 
    508             if($metadata['file'] == $external_img_url){
    509                 $upload = array(
    510                     'url' => wp_get_attachment_url( $attachment->ID )
    511                 );
    512 
    513                 return $upload;
    514             }
    515         }
    516 
    517         return false;
    518     }
    519 
    520     /**
    521      * Attempt to download a remote image attachment
    522      *
    523      * @param string $url URL of image to fetch
    524      * @param array $postdata Data about the post te image belongs to
    525      * @param string ID of the post
    526      * @return array|WP_Error Local file location details on success, WP_Error otherwise
    527      */
    528     function fetch_remote_image( $url, $postdata, $post_id ) {
    529 
    530         // extract the file name and extension from the url
    531         $file_name = rawurldecode(basename( $url ));
    532 
    533         // get placeholder file in the upload dir with a unique, sanitized filename
    534         $upload = wp_upload_bits( $file_name, 0, '', $postdata['post_date'] );
    535 
    536         //Append jpeg extension to file if Invalid file type error detected
    537         if($upload['error'] == 'Invalid file type'){
    538             //There must be some better way to do this
    539             $file_name = $file_name . '.jpeg';
    540 
    541             $upload = wp_upload_bits( $file_name, 0, '', $postdata['post_date'] );
    542         }
    543 
    544         if ( $upload['error'] ){
    545             return new WP_Error( 'upload_dir_error', $upload['error'] );
    546         }
    547 
    548         // fetch the remote url and write it to the placeholder file
    549         $headers = wp_get_http( $url, $upload['file'] );
    550 
    551         // request failed
    552         if ( ! $headers ) {
    553             @unlink( $upload['file'] );
    554             return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
    555         }
    556 
    557         // make sure the fetch was successful
    558         if ( $headers['response'] != '200' ) {
    559             @unlink( $upload['file'] );
    560             return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) );
    561         }
    562 
    563         $filesize = filesize( $upload['file'] );
    564 
    565         if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
    566             @unlink( $upload['file'] );
    567             return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
    568         }
    569 
    570         if ( 0 == $filesize ) {
    571             @unlink( $upload['file'] );
    572             return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
    573         }
    574 
    575         $max_size = (int) $this->max_attachment_size();
    576         if ( ! empty( $max_size ) && $filesize > $max_size ) {
    577             @unlink( $upload['file'] );
    578             return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
    579         }
    580 
    581         // keep track of the old and new urls so we can substitute them later
    582         $this->url_remap[$url] = $upload['url'];
    583         //$this->url_remap[$post['guid']] = $upload['url'];
    584         // keep track of the destination if the remote url is redirected somewhere else
    585         if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url ){
    586             $this->url_remap[$headers['x-final-location']] = $upload['url'];
    587                 }
    588                        
    589         //add to media library
    590         //Attachment options
    591         $attachment = array(
    592             'post_title'=> $file_name,
    593             'post_mime_type' => $headers['content-type']
    594         );
    595 
    596         $attach_id = wp_insert_attachment( $attachment, $upload['file'], $post_id );
    597         $attach_data = wp_generate_attachment_metadata( $attach_id, $url );
    598         wp_update_attachment_metadata( $attach_id, $attach_data );
    599 
    600         return $upload;
    601     }
    602 
    603     /**
    604      * Decide what the maximum file size for downloaded attachments is.
    605      * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
    606      *
    607      * @return int Maximum attachment file size to import
    608      */
    609     function max_attachment_size() {
    610         return apply_filters( 'import_attachment_size_limit', 0 );
    611     }
    612 
    613281}
  • rss-synchronization/trunk/rss-sync.php

    r871161 r871328  
    1313 * Plugin Name:       RSS Sync
    1414 * Description:       Synchronize posts with external RSS feed.
    15  * Version:           0.5.0
     15 * Version:           0.5.1
    1616 * Author:            Horta
    1717 * Text Domain:       rss-sync
Note: See TracChangeset for help on using the changeset viewer.