Plugin Directory

Changeset 1639382


Ignore:
Timestamp:
04/17/2017 11:30:10 PM (9 years ago)
Author:
TyB
Message:

Major performance enhancements + code organization & optimization

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

Legend:

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

    r1639319 r1639382  
    77*
    88* @author   Tyler Bailey
    9 * @version 0.7.0
     9* @version 0.8.0
    1010* @package remote-post-swap
    1111* @subpackage remote-post-swap/inc/admin
     
    1717defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1818
    19 if(!class_exists('RPS\Admin\RPS_Admin')) :
     19if(!class_exists('RPS_Admin')) :
    2020
    2121    class RPS_Admin {
  • remote-post-swap/trunk/inc/admin/class-rps-settings-display.php

    r1639346 r1639382  
    77*
    88* @author   Tyler Bailey
    9 * @version 0.7.0
     9* @version 0.8.0
    1010* @package remote-post-swap
    1111* @subpackage remote-post-swap/inc/admin
     
    1717defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1818
    19 if(!class_exists('RPS\Admin\RPS_Settings_Display')) :
     19if(!class_exists('RPS_Settings_Display')) :
    2020
    2121    class RPS_Settings_Display {
  • remote-post-swap/trunk/inc/admin/class-rps-settings.php

    r1639346 r1639382  
    77*
    88* @author   Tyler Bailey
    9 * @version 0.7.0
     9* @version 0.8.0
    1010* @package remote-post-swap
    1111* @subpackage remote-post-swap/inc/admin
     
    1717defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1818
    19 if(!class_exists('RPS\Admin\RPS_Settings')) :
     19if(!class_exists('RPS_Settings')) :
    2020
    2121    class RPS_Settings {
  • remote-post-swap/trunk/inc/class-rps-activator.php

    r1639319 r1639382  
    66*
    77* @author   Tyler Bailey
    8 * @version 0.7.0
     8* @version 0.8.0
    99* @package remote-post-swap
    1010* @subpackage remote-post-swap/inc
     
    1515defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1616
    17 if(!class_exists('RPS\RPS_Activator')) :
     17if(!class_exists('RPS_Activator')) :
    1818
    1919    class RPS_Activator {
  • remote-post-swap/trunk/inc/class-rps-autoloader.php

    r1639346 r1639382  
    99*
    1010* @author   Tyler Bailey
    11 * @version 0.7.0
     11* @version 0.8.0
    1212* @package remote-post-swap
    1313* @subpackage remote-post-swap/inc
     
    1616namespace RPS;
    1717
    18 if(!class_exists('RPS\RPS_Autoloader')) :
     18if(!class_exists('RPS_Autoloader')) :
    1919
    2020    class RPS_Autoloader {
  • remote-post-swap/trunk/inc/class-rps-base.php

    r1639319 r1639382  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.7.0
     7* @version 0.8.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
     
    1414defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1515
    16 if(!class_exists('RPS\RPS_Base')) :
     16if(!class_exists('RPS_Base')) :
    1717
    1818    class RPS_Base {
  • remote-post-swap/trunk/inc/class-rps-deactivator.php

    r1639319 r1639382  
    66*
    77* @author   Tyler Bailey
    8 * @version 0.7.0
     8* @version 0.8.0
    99* @package remote-post-swap
    1010* @subpackage remote-post-swap/inc
     
    1515defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1616
    17 if(!class_exists('RPS\RPS_Deactivator')) :
     17if(!class_exists('RPS_Deactivator')) :
    1818
    1919    class RPS_Deactivator {
  • remote-post-swap/trunk/inc/class-rps-post-media.php

    r1639321 r1639382  
    88*
    99* @author   Tyler Bailey
    10 * @version 0.7.0
     10* @version 0.8.0
    1111* @package remote-post-swap
    1212* @subpackage remote-post-swap/inc
     
    1717defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1818
    19 if(!class_exists('RPS\RPS_Post_Media')) :
     19if(!class_exists('RPS_Post_Media')) :
    2020
    2121    class RPS_Post_Media {
     
    5252            if($rps_pid) {
    5353                $rpsp_obj = new RPS_Retrieve_Data;
    54                 $rpsp = $rpsp_obj->rps_get_posts($rps_pid);
     54                $rpsp = $rpsp_obj->rps_get_api_data($rps_pid);
     55
    5556                $rpsp_img_id = $rpsp->featured_media;
    56                 $rpsp_img = $rpsp_obj->rps_get_media($rpsp_img_id);
     57                $rpsp_img = $rpsp_obj->rps_get_api_data($rpsp_img_id, 'media');
    5758
    5859                if(isset($rpsp_img->media_details)) {
     
    9899    }
    99100
    100     new RPS_Post_Media();
    101 
    102101endif;
  • remote-post-swap/trunk/inc/class-rps-replace-wp.php

    r1639319 r1639382  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.7.0
     7* @version 0.8.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
     
    1515defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1616
    17 if(!class_exists('RPS\RPS_Replace_WP')) :
     17if(!class_exists('RPS_Replace_WP')) :
    1818
    1919    class RPS_Replace_WP extends RPS_Retrieve_Data {
     
    4646            return $posts;
    4747
    48             if(is_single() && RPS_Base::rps_get_post_meta($posts[0]->ID)) {
    49                 $posts = $this->rps_swap_single_post($posts[0]);
    50             } else {
    51                 $posts = $this->rps_swap_loop_posts($posts);
    52             }
    53 
    54             return $posts;
    55         }
    56 
    57         /**
    58         * Swap a single posts content with it's API partner
    59         *
    60         * @param  $post - object - WP Post Object
    61         * @param  $rps_id - int - ID of the remote post to retrieve
    62         * @return  $posts - WP Post Object
    63         * @since    0.5.0
    64         */
    65         private function rps_swap_single_post($post, $rps_id = NULL) {
    66 
    67             if($rps_id === NULL)
    68             $rps_id = RPS_Base::rps_get_post_meta($post->ID);
    69 
    70             $rpsp = $this->rps_get_posts($rps_id);
    71 
    72             $post->post_content = RPS_Post_Media::rps_adjust_media_urls($rpsp->content->rendered);
    73             $post->post_title = $rpsp->title->rendered;
    74             $post->post_date = $rpsp->date;
    75 
    76             $posts[0] = $post;
    77 
    78             return $posts;
    79         }
    80 
    81         /**
    82         * Swap the post content within the loops
     48            return $this->rps_setup_api_data($posts);
     49        }
     50
     51        /**
     52        * Sets up the API & Original Post Data for Display
    8353        *
    8454        * @param  $posts - object - WP Posts Object
    8555        * @return  $posts - WP Post Object
    86         * @since    0.5.0
    87         */
    88         private function rps_swap_loop_posts($posts) {
     56        * @since    0.8.0
     57        */
     58        private function rps_setup_api_data($posts) {
    8959            // Get the total post count queried
    9060            $post_count = count($posts);
     
    9262            $rps_retrieve = array();
    9363
     64            // Default number of RPS posts to retrieve (0)
     65            $rps_count = 0;
     66
    9467            foreach($posts as $post) {
    9568                $rps_id = RPS_Base::rps_get_post_meta($post->ID);
     
    11386
    11487            if($num_posts > 0) {
     88                // If num_posts is greater than one, reset the default API request args
     89                $rps_args['per_page'] = $num_posts;
     90
    11591                if($rps_count > 0) {
    116                     $rps_args = array(
    117                         'per_page' => $num_posts,
    118                         'exclude' => $rps_retrieve
    119                     );
    120 
    121                     $rps_saved = $this->rps_get_posts(null, array('include' => $rps_retrieve));
    122                 } else {
    123                     $rps_args = array(
    124                         'per_page' => $num_posts,
    125                     );
     92
     93                    $rps_args['exclude'] = $rps_retrieve;
     94
     95                    // Get saved posts from API
     96                    $rps_saved = $this->rps_get_api_data(null, 'posts', array('include' => $rps_retrieve));
    12697                }
    12798            } else {
    128                 $rps_args = array(
    129                     'include' => $rps_retrieve
    130                 );
     99                $rps_args['include'] = $rps_retrieve;
    131100            }
    132101
    133102            // Get the posts from the API
    134             $rpsp = $this->rps_get_posts(null, $rps_args);
     103            $rpsp = $this->rps_get_api_data(null, 'posts', $rps_args);
    135104
    136105            // If we got new posts + retrieved saved posts, merge the array
     
    141110            if($rpsp && !empty($rpsp)) {
    142111                // Set the array for the API posts
    143                 $rps_posts = array();
    144 
    145                 // Set the "New Post Counter" variable
    146                 $npc = 0;
    147 
    148                 // Loop through returned API posts and assign new array data
    149                 foreach($rpsp as $rps_post) {
    150 
    151                     $rps_posts[$npc]['post_id'] = $rps_post->id;
    152                     $rps_posts[$npc]['post_title'] = $rps_post->title->rendered;
    153                     $rps_posts[$npc]['post_excerpt'] = $rps_post->excerpt->rendered;
    154                     $rps_posts[$npc]['post_content'] = RPS_Post_Media::rps_adjust_media_urls($rps_post->content->rendered);
    155                     $rps_posts[$npc]['post_date'] = $rps_post->date;
    156 
    157                     $npc++;
    158                 }
    159 
    160                 // Set the "Original Post Counter" variable
    161                 $opc = 0;
    162                 foreach($posts as $post) {
    163 
    164                     // Loop through original post object and replace data with returned API data
    165                     if(RPS_Base::rps_return_option('post_title'))
    166                     $post->post_title = $rps_posts[$opc]['post_title'];
    167 
    168                     if(RPS_Base::rps_return_option('post_content'))
    169                     $post->post_content = $rps_posts[$opc]['post_content'];
    170 
    171                     if(RPS_Base::rps_return_option('post_date'))
    172                     $post->post_date = $rps_posts[$opc]['post_date'];
    173 
    174                     if(RPS_Base::rps_return_option('post_excerpt'))
    175                     $post->post_excerpt = $rps_posts[$opc]['post_excerpt'];
    176 
    177                     if($this->rps_ensure_unqiue_meta($rps_posts[$opc]['post_id']))
    178                     update_post_meta($post->ID, RPS_Base::$rps_meta, $rps_posts[$opc]['post_id']);
    179 
    180                     $opc++;
    181                 }
     112                $rps_posts = $this->rps_setup_rps_posts($rpsp);
     113                // Swap the original post data with the API post data
     114                $posts = $this->rps_change_post_content($posts, $rps_posts);
     115            }
     116
     117            return $posts;
     118        }
     119
     120        /**
     121        * Create the array of post data from the API response
     122        *
     123        * @param  $rpsp - object - API Response
     124        * @return  $rps_posts - Array of post data returned from the API response
     125        * @since    0.8.0
     126        */
     127        private function rps_setup_rps_posts($rpsp) {
     128
     129            $rps_posts = array();
     130
     131            // Set the "New Post Counter" variable
     132            $npc = 0;
     133
     134            // Loop through returned API posts and assign new array data
     135            foreach($rpsp as $rps_post) {
     136
     137                $rps_posts[$npc]['post_id'] = $rps_post->id;
     138                $rps_posts[$npc]['post_title'] = $rps_post->title->rendered;
     139                $rps_posts[$npc]['post_excerpt'] = $rps_post->excerpt->rendered;
     140                $rps_posts[$npc]['post_content'] = RPS_Post_Media::rps_adjust_media_urls($rps_post->content->rendered);
     141                $rps_posts[$npc]['post_date'] = $rps_post->date;
     142
     143                $npc++;
     144            }
     145
     146            return $rps_posts;
     147        }
     148
     149        /**
     150        * Swap out the original post data with the API response post data
     151        *
     152        * @param  $posts - object - WP Post Object
     153        * @return  $rps_posts - Array of post data returned from the API response & formatted through rps_setup_api_data()
     154        * @since    0.8.0
     155        */
     156        private function rps_change_post_content($posts, $rps_posts) {
     157            // Set the "Original Post Counter" variable
     158            $opc = 0;
     159
     160            foreach($posts as $post) {
     161
     162                // Loop through original post object and replace data with returned API data
     163                if(RPS_Base::rps_return_option('post_title'))
     164                $post->post_title = $rps_posts[$opc]['post_title'];
     165
     166                if(RPS_Base::rps_return_option('post_content'))
     167                $post->post_content = $rps_posts[$opc]['post_content'];
     168
     169                if(RPS_Base::rps_return_option('post_date'))
     170                $post->post_date = $rps_posts[$opc]['post_date'];
     171
     172                if(RPS_Base::rps_return_option('post_excerpt'))
     173                $post->post_excerpt = $rps_posts[$opc]['post_excerpt'];
     174
     175                if($this->rps_ensure_unqiue_meta($rps_posts[$opc]['post_id']))
     176                update_post_meta($post->ID, RPS_Base::$rps_meta, $rps_posts[$opc]['post_id']);
     177
     178                $opc++;
    182179            }
    183180
     
    222219    }
    223220
    224     new RPS_Replace_WP();
    225 
    226221endif;
  • remote-post-swap/trunk/inc/class-rps-retrieve-data.php

    r1639319 r1639382  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.7.0
     7* @version 0.8.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
     
    1414defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1515
    16 if(!class_exists('RPS\RPS_Retrieve_Data')) :
     16if(!class_exists('RPS_Retrieve_Data')) :
    1717
    1818    class RPS_Retrieve_Data {
     
    2727
    2828        /**
    29         * The API endpoint for posts
    30         *
    31         * @var $rps_posts
    32         * @since 0.5.0
    33         */
    34         protected $rps_posts;
    35 
    36         /**
    37         * The API endpoint for users
    38         *
    39         * @var $rps_users
    40         * @since 0.5.0
    41         */
    42         protected $rps_users;
    43 
    44         /**
    45         * The API endpoint for media
    46         *
    47         * @var $rps_media
    48         * @since 0.5.0
    49         */
    50         protected $rps_media;
    51 
    52         /**
    5329        * Executed on class istantiation.
    5430        *
     
    6036
    6137            // Base URL from user entered options
    62             $this->rps_base_url = RPS_Base::rps_return_option('url');
    63 
    64             // Endpoint URLs
    65             $this->rps_posts = $this->rps_base_url . 'wp-json/wp/v2/posts';
    66             $this->rps_users = $this->rps_base_url . 'wp-json/wp/v2/users';
    67             $this->rps_media = $this->rps_base_url . 'wp-json/wp/v2/media';
     38            $this->rps_base_url = RPS_Base::rps_return_option('url') . 'wp-json/wp/v2/';
    6839        }
    6940
    7041        /**
    71         * Retrieves the posts from the target site API
     42        * Retrieves the API data from the target site
    7243        *
    73         * @param  $id - int - the ID of the post to retrieve from the API
     44        * @param  $id - int - the ID of the data to retrieve from the API
     45        * @param  $type - string - type of data to retrieve from the API
    7446        * @param  $filters - array - Array of API filters
    75         * @return   $posts - array - Array of post data returned from API
    76         * @since    0.5.0
     47        * @return  $api_return - array - Array of post data returned from API
     48        * @since    0.8.0
    7749        */
    78         public function rps_get_posts($id, $filters = array()) {
     50        public function rps_get_api_data($id = '', $type = 'posts', $filters = array()) {
    7951
    80             $posts = false;
     52            $resp = false;
    8153
    82             if($id != NULL && $id != FALSE) {
    83                 $resp = wp_remote_get($this->rps_posts . '/' . $id);
    84             } elseif(!empty($filters)) {
     54            $id = (strlen($id) > 0 ? '/' . $id : $id);
     55            $filter_str = $this->rps_process_filters($filters);
    8556
    86                 $fc = 0;
    87                 $filter_str = '';
     57            $url = $this->rps_base_url . $type . $id . $filter_str;
    8858
     59            $resp = wp_remote_get($url);
     60
     61            if(is_wp_error( $resp )) {
     62                return false;
     63            }
     64
     65            $api_return = json_decode( wp_remote_retrieve_body( $resp ));
     66
     67            if(empty($api_return) || isset($api_return->data->status) && $api_return->data->status === 404)
     68            return false;
     69
     70            return $api_return;
     71        }
     72
     73        /**
     74        * Processes the API filter arguments passed as an array to retrieve data
     75        *
     76        * @param  $filters - array - Array of API filters
     77        * @since    0.8.0
     78        */
     79        private function rps_process_filters($filters) {
     80
     81            $fc = 0;
     82            $filter_str = '';
     83
     84            if(is_array($filters)) {
    8985                foreach($filters as $key => $filter) {
    9086                    if(is_array($filter)) {
     
    9490                    $fc++;
    9591                }
    96 
    97                 $resp = wp_remote_get($this->rps_posts . $filter_str);
    98 
    99             } else {
    100                 $resp = wp_remote_get($this->rps_posts);
    10192            }
    10293
    103             if(is_wp_error( $resp )) {
    104                 return false;
    105             }
    106 
    107             $posts = json_decode( wp_remote_retrieve_body( $resp ));
    108 
    109             if(empty($posts) || isset($posts->data->status) && $posts->data->status === 404)
    110             return false;
    111 
    112             return $posts;
    113         }
    114 
    115         /**
    116         * Retrieves the users from the target site API
    117         *
    118         * @param  $id - int - the ID of the user to retrieve from the API
    119         * @return   $users - array - array of user data returned from API
    120         * @since    0.5.0
    121         */
    122         public function rps_get_users($id = NULL) {
    123 
    124             $users = false;
    125 
    126             if($id !== NULL) {
    127                 $resp = wp_remote_get($this->rps_users . $id);
    128             } else {
    129                 $resp = wp_remote_get($this->rps_users);
    130             }
    131 
    132             if(is_wp_error( $resp )) {
    133                 return false;
    134             }
    135 
    136             $users = json_decode( wp_remote_retrieve_body( $resp ));
    137 
    138             if(empty($users))
    139             return false;
    140 
    141             return $users;
    142         }
    143 
    144         /**
    145         * Retrieves featured image/media information from the API
    146         *
    147         * @param   $id - int - the ID of the media element to grab
    148         * @return   $media - array - array of media data returned from API
    149         * @since    0.5.0
    150         */
    151         public function rps_get_media($id) {
    152 
    153             $media = false;
    154 
    155             if($id !== NULL) {
    156                 $resp = wp_remote_get($this->rps_media . '/' . $id);
    157             }
    158 
    159             if(is_wp_error($resp)) {
    160                 return false;
    161             }
    162 
    163             $media = json_decode( wp_remote_retrieve_body( $resp ) );
    164 
    165             if(empty($media))
    166             return false;
    167 
    168             return $media;
     94            return $filter_str;
    16995        }
    17096    }
    171 
    172     new RPS_Retrieve_Data();
    173 
     97   
    17498endif;
  • remote-post-swap/trunk/inc/class-rps.php

    r1639319 r1639382  
    55*
    66* @author   Tyler Bailey
    7 * @version 0.7.0
     7* @version 0.8.0
    88* @package remote-post-swap
    99* @subpackage remote-post-swap/inc
     
    1414defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
    1515
    16 if(!class_exists('RPS\RPS')) :
     16if(!class_exists('RPS')) :
    1717
    1818    class RPS {
     
    2424        */
    2525        public function __construct() {
    26             $this->set_locale();
    27             $this->rps_init();
     26            self::set_locale();
     27            self::rps_init();
    2828        }
    2929
     
    3636        * @since    0.7.0
    3737        */
    38         private function rps_init() {
     38        private static function rps_init() {
    3939            new RPS_Replace_WP;
    4040
     
    4949        * @since   0.5.0
    5050        */
    51         private function set_locale() {
     51        private static function set_locale() {
    5252            load_plugin_textdomain( RPS_SLUG, false, RPS_GLOBAL_DIR . 'language' );
    5353        }
  • remote-post-swap/trunk/readme.txt

    r1639319 r1639382  
    44Requires at least: 4.7.0
    55Tested up to: 4.7.3
    6 Stable tag: 0.7.0
     6Stable tag: 0.8.0
    77License: GPLv2 or later
    88License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3737== Changelog ==
    3838
     39= 0.8.0 =
     40* Major performance enhancements & code optimization
     41
    3942= 0.7.0 =
    4043* Add in the ability to allow plugin users to choose which post data is swapped with remote post data (options page)
Note: See TracChangeset for help on using the changeset viewer.