Changeset 871328
- Timestamp:
- 03/07/2014 05:40:16 PM (12 years ago)
- Location:
- rss-synchronization
- Files:
-
- 21 added
- 5 edited
-
tags/0.5.1 (added)
-
tags/0.5.1/LICENSE.txt (added)
-
tags/0.5.1/README.txt (added)
-
tags/0.5.1/admin (added)
-
tags/0.5.1/admin/class-rss-sync-admin.php (added)
-
tags/0.5.1/admin/views (added)
-
tags/0.5.1/admin/views/admin.php (added)
-
tags/0.5.1/admin/views/index.php (added)
-
tags/0.5.1/assets (added)
-
tags/0.5.1/includes (added)
-
tags/0.5.1/includes/class-rss-sync-tools.php (added)
-
tags/0.5.1/index.php (added)
-
tags/0.5.1/languages (added)
-
tags/0.5.1/languages/index.php (added)
-
tags/0.5.1/languages/rss-sync.pot (added)
-
tags/0.5.1/public (added)
-
tags/0.5.1/public/class-rss-sync.php (added)
-
tags/0.5.1/rss-sync.php (added)
-
tags/0.5.1/uninstall.php (added)
-
trunk/README.txt (modified) (3 diffs)
-
trunk/admin/class-rss-sync-admin.php (modified) (2 diffs)
-
trunk/admin/views/admin.php (modified) (1 diff)
-
trunk/includes (added)
-
trunk/includes/class-rss-sync-tools.php (added)
-
trunk/public/class-rss-sync.php (modified) (13 diffs)
-
trunk/rss-sync.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
rss-synchronization/trunk/README.txt
r871189 r871328 4 4 Requires at least: 3.8 5 5 Tested up to: 3.8.1 6 Stable tag: 0.5. 06 Stable tag: 0.5.1 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 15 15 16 16 You will find these settings in your administration page on the Settings->RSS Settings sub-menu. 17 18 Check out the [project on GitHub](https://github.com/LightSystem/WordPress-Plugin-RSS-Sync). 17 19 18 20 == Installation == … … 38 40 39 41 1. Download `rss-sync.zip` 40 2. Extract the ` plugin-name` directory to your computer41 3. Upload the ` plugin-name` directory to the `/wp-content/plugins/` directory42 2. Extract the `rss-sync` directory to your computer 43 3. Upload the `rss-sync` directory to the `/wp-content/plugins/` directory 42 44 4. Activate the plugin in the Plugin dashboard 43 45 44 46 == Changelog == 47 48 = 0.5.1 = 49 * Improved behaviour by instantly fetching feeds as soon as they are added in the settings. 45 50 46 51 = 0.5.0 = -
rss-synchronization/trunk/admin/class-rss-sync-admin.php
r871161 r871328 21 21 * Instance of this class. 22 22 * 23 * @since 0.3.024 *25 23 * @var object 26 24 */ … … 29 27 /** 30 28 * Slug of the plugin screen. 31 *32 * @since 0.3.033 29 * 34 30 * @var string -
rss-synchronization/trunk/admin/views/admin.php
r871161 r871328 125 125 return $options; 126 126 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 127 134 if($existing['refresh'] != $options['refresh']){ 128 135 -
rss-synchronization/trunk/public/class-rss-sync.php
r871161 r871328 9 9 */ 10 10 11 include_once( ABSPATH . WPINC . '/feed.php' ); 12 include_once( ABSPATH . 'wp-admin/includes/image.php' ); 11 include_once(ABSPATH . 'wp-content/plugins/rss-sync/includes/class-rss-sync-tools.php'); 13 12 14 13 /** … … 32 31 * @var string 33 32 */ 34 const VERSION = '0.5. 0';33 const VERSION = '0.5.1'; 35 34 36 35 const RSS_ID_CUSTOM_FIELD = 'rss_id'; … … 45 44 * plugin file. 46 45 * 47 * @since 0.2.048 *49 46 * @var string 50 47 */ … … 53 50 /** 54 51 * Instance of this class. 55 *56 * @since 0.2.057 52 * 58 53 * @var object … … 64 59 * and styles. 65 60 * 66 * @since 0.2.067 61 */ 68 62 private function __construct() { … … 84 78 add_filter( 'wp_feed_cache_transient_lifetime', function ($seconds){ return 3600; } ); 85 79 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' ) ); 87 82 } 88 83 89 84 /** 90 85 * Return the plugin slug. 91 *92 * @since 0.2.093 86 * 94 87 * @return Plugin slug variable. … … 100 93 /** 101 94 * Return an instance of this class. 102 *103 * @since 0.2.0104 95 * 105 96 * @return object A single instance of this class. … … 117 108 /** 118 109 * Fired when the plugin is activated. 119 *120 * @since 0.2.0121 110 * 122 111 * @param boolean $network_wide True if WPMU superadmin uses … … 155 144 * Fired when the plugin is deactivated. 156 145 * 157 * @since 0.2.0158 *159 146 * @param boolean $network_wide True if WPMU superadmin uses 160 147 * "Network Deactivate" action, false if … … 193 180 * Fired when a new site is activated with a WPMU environment. 194 181 * 195 * @since 0.2.0196 *197 182 * @param int $blog_id ID of the new blog. 198 183 */ … … 215 200 * - not deleted 216 201 * 217 * @since 0.2.0218 *219 202 * @return array|false The blog ids, false if no matches. 220 203 */ … … 243 226 if($options){ 244 227 $chosen_recurrence = $options['refresh']; 245 }228 } 246 229 247 230 if($chosen_recurrence){ 248 231 wp_schedule_event( time(), $chosen_recurrence, 'rss_sync_event' ); 249 } else {232 } else { 250 233 wp_schedule_event( time(), 'daily', 'rss_sync_event' ); 251 }234 } 252 235 } 253 236 … … 296 279 } 297 280 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#Actions302 * Reference: http://codex.wordpress.org/Plugin_API/Action_Reference303 *304 * @since 0.2.0305 */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.0323 */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 correctly330 $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 information339 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 routines365 $post_data = array(366 'post_content' => $post->post_content,367 'post_date' => $post->post_modified368 );369 370 $processed_post_content = $this->process_image_tags($post_data, $updated_post_id);371 372 //Update post content373 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_tags388 );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 library398 $processed_post_content = $this->process_image_tags($post, $inserted_post_id);399 400 //Update post content401 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.0432 */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.0448 */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 attachment522 *523 * @param string $url URL of image to fetch524 * @param array $postdata Data about the post te image belongs to525 * @param string ID of the post526 * @return array|WP_Error Local file location details on success, WP_Error otherwise527 */528 function fetch_remote_image( $url, $postdata, $post_id ) {529 530 // extract the file name and extension from the url531 $file_name = rawurldecode(basename( $url ));532 533 // get placeholder file in the upload dir with a unique, sanitized filename534 $upload = wp_upload_bits( $file_name, 0, '', $postdata['post_date'] );535 536 //Append jpeg extension to file if Invalid file type error detected537 if($upload['error'] == 'Invalid file type'){538 //There must be some better way to do this539 $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 file549 $headers = wp_get_http( $url, $upload['file'] );550 551 // request failed552 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 successful558 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 later582 $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 else585 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 library590 //Attachment options591 $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_limit606 *607 * @return int Maximum attachment file size to import608 */609 function max_attachment_size() {610 return apply_filters( 'import_attachment_size_limit', 0 );611 }612 613 281 } -
rss-synchronization/trunk/rss-sync.php
r871161 r871328 13 13 * Plugin Name: RSS Sync 14 14 * Description: Synchronize posts with external RSS feed. 15 * Version: 0.5. 015 * Version: 0.5.1 16 16 * Author: Horta 17 17 * Text Domain: rss-sync
Note: See TracChangeset
for help on using the changeset viewer.