diff --git a/includes/class-wp-push-syndication-server.php b/includes/class-wp-push-syndication-server.php index 02d756936ee0da704c12f03583743e39700ba22b..a2117a518e7396dac09d92bdc41ba7001d0dff5f 100644 --- a/includes/class-wp-push-syndication-server.php +++ b/includes/class-wp-push-syndication-server.php @@ -8,7 +8,7 @@ class WP_Push_Syndication_Server { public $push_syndicate_default_settings; public $push_syndicate_transports; - private $version = '2.0'; + private $version; function __construct() { @@ -132,6 +132,8 @@ class WP_Push_Syndication_Server { $this->push_syndicate_settings = wp_parse_args( (array) get_option( 'push_syndicate_settings' ), $this->push_syndicate_default_settings ); + $this->version = get_option( 'syn_version' ); + } public function register_syndicate_actions() { @@ -190,6 +192,8 @@ class WP_Push_Syndication_Server { // register settings register_setting( 'push_syndicate_settings', 'push_syndicate_settings', array( $this, 'push_syndicate_settings_validate' ) ); + // Maybe run upgrade + $this->upgrade(); } public function load_scripts_and_styles( $hook ) { @@ -1198,14 +1202,14 @@ class WP_Push_Syndication_Server { public function pull_content( $sites ) { foreach( $sites as $site ) { + $site_id = $site->ID; - $site_enabled = get_post_meta( $site->ID, 'syn_site_enabled', true ); + $site_enabled = get_post_meta( $site_id, 'syn_site_enabled', true ); if( $site_enabled != 'on' ) continue; - $inserted_posts = get_post_meta( $site->ID, 'syn_inserted_posts', true ); - $transport_type = get_post_meta( $site->ID, 'syn_transport_type', true ); - $client = Syndication_Client_Factory::get_client( $transport_type, $site->ID ); + $transport_type = get_post_meta( $site_id, 'syn_transport_type', true ); + $client = Syndication_Client_Factory::get_client( $transport_type, $site_id ); $posts = $client->get_posts(); if( empty( $posts ) ) @@ -1220,7 +1224,8 @@ class WP_Push_Syndication_Server { $post_types_processed[] = $post->post_type; } - if( in_array( $post['post_guid'], $inserted_posts ) ) { + $post_id = $this->find_post_by_guid( $post['post_guid'], $post, $site ); + if( $post_id ) { $pull_edit_shortcircuit = apply_filters( 'syn_pre_pull_edit_post_shortcircuit', false, $post, $site, $transport_type, $client ); if ( true === $pull_edit_shortcircuit ) continue; @@ -1229,7 +1234,7 @@ class WP_Push_Syndication_Server { if( $this->push_syndicate_settings['update_pulled_posts'] != 'on' ) continue; - $post['ID'] = array_search( $post['post_guid'], $inserted_posts ); + $post['ID'] = $post_id; $result = wp_update_post( $post, true ); do_action( 'syn_post_pull_edit_post', $result, $post, $site, $transport_type, $client ); @@ -1243,21 +1248,61 @@ class WP_Push_Syndication_Server { do_action( 'syn_post_pull_new_post', $result, $post, $site, $transport_type, $client ); - if( !is_wp_error( $result ) ) - $inserted_posts[ $result ] = $post['post_guid']; + if( !is_wp_error( $result ) ) { + update_post_meta( $result, 'syn_post_guid', $post['post_guid'] ); + update_post_meta( $result, 'syn_source_site_id', $site_id ); + } } - } foreach ( $post_types_processed as $post_type ) { add_post_type_support( $post_type, 'revisions' ); } + } + + } + + function find_post_by_guid( $guid, $post, $site ) { + global $wpdb; + + $post_id = apply_filters( 'syn_pre_find_post_by_guid', false, $guid, $post, $site ); + if ( false !== $post_id ) + return $post_id; + + // A direct query here is way more efficient than WP_Query, because we don't have to do all the extra processing, filters, and JOIN. + $post_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'syn_post_guid' LIMIT 1", $guid ) ); + + if ( $post_id ) + return $post_id; - update_post_meta( $site->ID, 'syn_inserted_posts', $inserted_posts ); + return false; + } + + private function upgrade() { + global $wpdb; + + if ( version_compare( $this->version, SYNDICATION_VERSION, '>=' ) ) + return; + + // upgrade to 2.1 + if ( version_compare( $this->version, '2.0', '<=' ) ) { + $inserted_posts_by_site = $wpdb->get_col( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'syn_inserted_posts'" ); + foreach ( $inserted_posts_by_site as $site_id ) { + $inserted_posts = get_post_meta( $site_id, 'syn_inserted_posts', true ); + + foreach ( $inserted_posts as $inserted_post_id => $inserted_post_guid ) { + update_post_meta( $inserted_post_id, 'syn_post_guid', $inserted_post_guid ); + update_post_meta( $inserted_post_id, 'syn_source_site_id', $site_id ); + } + + delete_post_meta( $site_id, 'syn_inserted_posts' ); + } + update_option( 'syn_version', '2.1' ); } + update_option( 'syn_version', SYNDICATION_VERSION ); } } diff --git a/push-syndication.php b/push-syndication.php index fc67de444f6808e8c4a9daf7b54569af70b84cda..82a30828a72976d6df59890d8e02c3754ea36bd3 100644 --- a/push-syndication.php +++ b/push-syndication.php @@ -9,6 +9,8 @@ * License: GPLv2 or later */ +define( 'SYNDICATION_VERSION', 2.0 ); + if ( ! defined( 'PUSH_SYNDICATE_KEY' ) ) define( 'PUSH_SYNDICATE_KEY', 'PUSH_SYNDICATE_KEY' );