Plugin Directory

Changeset 1637819


Ignore:
Timestamp:
04/14/2017 05:11:07 PM (9 years ago)
Author:
TyB
Message:

Rewrite the replace-wp class to only make a maximum of 2 API calls per loop.

Location:
remote-post-swap/trunk
Files:
10 edited

Legend:

Unmodified
Added
Removed
  • remote-post-swap/trunk/inc/admin/class-rps-admin.php

    r1637292 r1637819  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.5.0
     7* @version 0.6.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc/admin
  • remote-post-swap/trunk/inc/class-rps-activator.php

    r1637292 r1637819  
    66*
    77* @author   Tyler Bailey
    8 * @version 0.5.0
     8* @version 0.6.0
    99* @package remote-post-swap
    1010* @subpackage remote-post-swap/inc
  • remote-post-swap/trunk/inc/class-rps-base.php

    r1637292 r1637819  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.5.0
     7* @version 0.6.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
  • remote-post-swap/trunk/inc/class-rps-deactivator.php

    r1637292 r1637819  
    66*
    77* @author   Tyler Bailey
    8 * @version 0.5.0
     8* @version 0.6.0
    99* @package remote-post-swap
    1010* @subpackage remote-post-swap/inc
  • remote-post-swap/trunk/inc/class-rps-replace-wp.php

    r1637298 r1637819  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.5.0
     7* @version 0.6.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
     
    8585            // Get the total post count queried
    8686            $post_count = count($posts);
    87             $npc = 0;
     87            // Array of already saved post IDs from the API
     88            $rps_retrieve = array();
    8889
    8990            foreach($posts as $post) {
    90                 $npc++;
    9191                $rps_id = $this->rps_get_post_meta($post->ID);
    9292
    9393                if($rps_id) {
    94 
    95                     $post = $this->rps_swap_single_post($post, $rps_id);
    96 
     94                    $rps_retrieve[] = $rps_id;
     95                }
     96            }
     97
     98            // Number of items in the rps_retrieve array
     99            $rps_count = count($rps_retrieve);
     100
     101            // Number of posts we need to query from the API
     102            // Total number of posts from wp_query - number of posts in rps array
     103            $num_posts = ($post_count - $rps_count);
     104
     105            // Default argument for API request
     106            $rps_args = array(
     107                'per_page' => $post_count
     108            );
     109
     110            if($num_posts > 0) {
     111                if($rps_count > 0) {
     112                    $rps_args = array(
     113                        'per_page' => $num_posts,
     114                        'exclude' => $rps_retrieve
     115                    );
     116
     117                    $rps_saved = $this->rps_get_posts(null, array('include' => $rps_retrieve));
    97118                } else {
    98 
    99                     $rpsp = $this->rps_get_posts(null, array('per_page' => $post_count));
    100 
    101                     if($rpsp) {
    102                         // Set the array for the API posts
    103                         $rps_posts = array();
    104 
    105                         // Set the new post count variable for the array
    106                         // Loop through returned API posts and assign new array data
    107                         foreach($rpsp as $rps_post) {
    108                             if($this->rps_ensure_unqiue_meta($rps_post->id)) {
    109                                 $rps_posts[$npc]['post_id'] = $rps_post->id;
    110                                 $rps_posts[$npc]['post_content'] = $this->rps_adjust_media_urls($rps_post->content->rendered);
    111                                 $rps_posts[$npc]['post_title'] = $rps_post->title->rendered;
    112                                 $rps_posts[$npc]['post_date'] = $rps_post->date;
    113                                 $rps_posts[$npc]['post_excerpt'] = $rps_post->excerpt->rendered;
    114                             }
    115                         }
    116 
    117                         // Reset the new post count variable for the second array
    118                         // Loop through original post object and replace data with returned API data
    119                         $post->post_title = $rps_posts[$npc]['post_title'];
    120                         $post->post_content = $rps_posts[$npc]['post_content'];
    121                         $post->post_date = $rps_posts[$npc]['post_date'];
    122                         $post->post_excerpt = $rps_posts[$npc]['post_excerpt'];
    123 
    124                         update_post_meta($post->ID, $this->rps_meta, $rps_posts[$npc]['post_id']);
    125                     }
     119                    $rps_args = array(
     120                        'per_page' => $num_posts,
     121                    );
     122                }
     123            } else {
     124                $rps_args = array(
     125                    'include' => $rps_retrieve
     126                );
     127            }
     128
     129            // Get the posts from the API
     130            $rpsp = $this->rps_get_posts(null, $rps_args);
     131
     132            // If we got new posts + retrieved saved posts, merge the array
     133            if(isset($rps_saved) && !empty($rps_saved))
     134                $rpsp = array_merge($rps_saved, $rpsp);
     135
     136            // If we have any new posts at all
     137            if($rpsp && !empty($rpsp)) {
     138                // Set the array for the API posts
     139                $rps_posts = array();
     140
     141                // Set the "New Post Counter" variable
     142                $npc = 0;
     143
     144                // Loop through returned API posts and assign new array data
     145                foreach($rpsp as $rps_post) {
     146
     147                    $rps_posts[$npc]['post_id'] = $rps_post->id;
     148                    $rps_posts[$npc]['post_content'] = $this->rps_adjust_media_urls($rps_post->content->rendered);
     149                    $rps_posts[$npc]['post_title'] = $rps_post->title->rendered;
     150                    $rps_posts[$npc]['post_date'] = $rps_post->date;
     151                    $rps_posts[$npc]['post_excerpt'] = $rps_post->excerpt->rendered;
     152
     153                    $npc++;
     154                }
     155
     156                // Set the "Original Post Counter" variable
     157                $opc = 0;
     158                foreach($posts as $post) {
     159
     160                    // Loop through original post object and replace data with returned API data
     161                    $post->post_title = $rps_posts[$opc]['post_title'];
     162                    $post->post_content = $rps_posts[$opc]['post_content'];
     163                    $post->post_date = $rps_posts[$opc]['post_date'];
     164                    $post->post_excerpt = $rps_posts[$opc]['post_excerpt'];
     165
     166                    if($this->rps_ensure_unqiue_meta($rps_posts[$opc]['post_id']))
     167                    update_post_meta($post->ID, $this->rps_meta, $rps_posts[$opc]['post_id']);
     168
     169                    $opc++;
    126170                }
    127171            }
     
    172216        */
    173217        private function rps_ensure_unqiue_meta($rps_id) {
     218
     219            $rps_id = (int) $rps_id;
     220
    174221            $args = array(
    175                 'posts_per_page' => -1,
    176                 'meta_key' => $this->rps_meta,
    177                 'meta_value' => $rps_id
     222                'posts_per_page' => 1,
     223                'meta_query' => array(
     224                    array(
     225                        'key' => $this->rps_meta,
     226                        'value' => $rps_id
     227                    )
     228                ),
     229                'fields' => 'ids'
    178230            );
     231
    179232            $rpsq = new WP_Query( $args );
    180233
    181             if($rpsq->have_posts()) {
     234            $rpsq_arr = $rpsq->posts;
     235
     236            if(!empty($rpsq_arr)) {
    182237                return false;
    183238            }
     239
     240            wp_reset_query();
    184241
    185242            return true;
  • remote-post-swap/trunk/inc/class-rps-retrieve-data.php

    r1637292 r1637819  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.5.0
     7* @version 0.6.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
  • remote-post-swap/trunk/inc/class-rps.php

    r1637292 r1637819  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.5.0
     7* @version 0.6.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
  • remote-post-swap/trunk/readme.txt

    r1637292 r1637819  
    44Requires at least: 4.7.0
    55Tested up to: 4.7.3
    6 Stable tag: 0.5.0
     6Stable tag: 0.6.0
     7License: GPLv2 or later
     8License URI: http://www.gnu.org/licenses/gpl-2.0.html
    79
    810Swap local development post data out with live/remote post data on the fly
     
    3537== Changelog ==
    3638
     39= 0.6.0 =
     40* Re-wrote the replace-wp class to only make a maximum of 2 API calls per loop. (Performance enhancement)
     41
    3742= 0.5.0 =
    3843* Initial Release
  • remote-post-swap/trunk/remote-post-swap.php

    r1637292 r1637819  
    1717* Plugin URI:           http://tylerb.me/plugins/remote-post-swap.zip
    1818* Description:          Swap local development post data out with live/remote post data on the fly
    19 * Version:               0.5.0
     19* Version:               0.6.0
    2020* Author:                Tyler Bailey
    2121* Author URI:          http://tylerb.me
  • remote-post-swap/trunk/uninstall.php

    r1637297 r1637819  
    33* Fired when the plugin is uninstalled.
    44*
    5 * @link         http://tylerb.me
    6 * @since        0.5.0
     5* @link       http://tylerb.me
     6* @since      0.5.0
    77* @package    rps
    88*/
     
    1111if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    1212    exit;
     13} else {
     14    delete_post_meta_by_key( 'rps_post_id' );
    1315}
    14 
    15 delete_post_meta_by_key( 'rps_post_id' );
Note: See TracChangeset for help on using the changeset viewer.