Changeset 1639382
- Timestamp:
- 04/17/2017 11:30:10 PM (9 years ago)
- Location:
- remote-post-swap/trunk
- Files:
-
- 12 edited
-
inc/admin/class-rps-admin.php (modified) (2 diffs)
-
inc/admin/class-rps-settings-display.php (modified) (2 diffs)
-
inc/admin/class-rps-settings.php (modified) (2 diffs)
-
inc/class-rps-activator.php (modified) (2 diffs)
-
inc/class-rps-autoloader.php (modified) (2 diffs)
-
inc/class-rps-base.php (modified) (2 diffs)
-
inc/class-rps-deactivator.php (modified) (2 diffs)
-
inc/class-rps-post-media.php (modified) (4 diffs)
-
inc/class-rps-replace-wp.php (modified) (7 diffs)
-
inc/class-rps-retrieve-data.php (modified) (5 diffs)
-
inc/class-rps.php (modified) (5 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
remote-post-swap/trunk/inc/admin/class-rps-admin.php
r1639319 r1639382 7 7 * 8 8 * @author Tyler Bailey 9 * @version 0. 7.09 * @version 0.8.0 10 10 * @package remote-post-swap 11 11 * @subpackage remote-post-swap/inc/admin … … 17 17 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 18 18 19 if(!class_exists('RPS \Admin\RPS_Admin')) :19 if(!class_exists('RPS_Admin')) : 20 20 21 21 class RPS_Admin { -
remote-post-swap/trunk/inc/admin/class-rps-settings-display.php
r1639346 r1639382 7 7 * 8 8 * @author Tyler Bailey 9 * @version 0. 7.09 * @version 0.8.0 10 10 * @package remote-post-swap 11 11 * @subpackage remote-post-swap/inc/admin … … 17 17 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 18 18 19 if(!class_exists('RPS \Admin\RPS_Settings_Display')) :19 if(!class_exists('RPS_Settings_Display')) : 20 20 21 21 class RPS_Settings_Display { -
remote-post-swap/trunk/inc/admin/class-rps-settings.php
r1639346 r1639382 7 7 * 8 8 * @author Tyler Bailey 9 * @version 0. 7.09 * @version 0.8.0 10 10 * @package remote-post-swap 11 11 * @subpackage remote-post-swap/inc/admin … … 17 17 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 18 18 19 if(!class_exists('RPS \Admin\RPS_Settings')) :19 if(!class_exists('RPS_Settings')) : 20 20 21 21 class RPS_Settings { -
remote-post-swap/trunk/inc/class-rps-activator.php
r1639319 r1639382 6 6 * 7 7 * @author Tyler Bailey 8 * @version 0. 7.08 * @version 0.8.0 9 9 * @package remote-post-swap 10 10 * @subpackage remote-post-swap/inc … … 15 15 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 16 16 17 if(!class_exists('RPS \RPS_Activator')) :17 if(!class_exists('RPS_Activator')) : 18 18 19 19 class RPS_Activator { -
remote-post-swap/trunk/inc/class-rps-autoloader.php
r1639346 r1639382 9 9 * 10 10 * @author Tyler Bailey 11 * @version 0. 7.011 * @version 0.8.0 12 12 * @package remote-post-swap 13 13 * @subpackage remote-post-swap/inc … … 16 16 namespace RPS; 17 17 18 if(!class_exists('RPS \RPS_Autoloader')) :18 if(!class_exists('RPS_Autoloader')) : 19 19 20 20 class RPS_Autoloader { -
remote-post-swap/trunk/inc/class-rps-base.php
r1639319 r1639382 5 5 * 6 6 * @author Tyler Bailey 7 * @version 0. 7.07 * @version 0.8.0 8 8 * @package remote-post-swap 9 9 * @subpackage remote-post-swap/inc … … 14 14 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 15 15 16 if(!class_exists('RPS \RPS_Base')) :16 if(!class_exists('RPS_Base')) : 17 17 18 18 class RPS_Base { -
remote-post-swap/trunk/inc/class-rps-deactivator.php
r1639319 r1639382 6 6 * 7 7 * @author Tyler Bailey 8 * @version 0. 7.08 * @version 0.8.0 9 9 * @package remote-post-swap 10 10 * @subpackage remote-post-swap/inc … … 15 15 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 16 16 17 if(!class_exists('RPS \RPS_Deactivator')) :17 if(!class_exists('RPS_Deactivator')) : 18 18 19 19 class RPS_Deactivator { -
remote-post-swap/trunk/inc/class-rps-post-media.php
r1639321 r1639382 8 8 * 9 9 * @author Tyler Bailey 10 * @version 0. 7.010 * @version 0.8.0 11 11 * @package remote-post-swap 12 12 * @subpackage remote-post-swap/inc … … 17 17 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 18 18 19 if(!class_exists('RPS \RPS_Post_Media')) :19 if(!class_exists('RPS_Post_Media')) : 20 20 21 21 class RPS_Post_Media { … … 52 52 if($rps_pid) { 53 53 $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 55 56 $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'); 57 58 58 59 if(isset($rpsp_img->media_details)) { … … 98 99 } 99 100 100 new RPS_Post_Media();101 102 101 endif; -
remote-post-swap/trunk/inc/class-rps-replace-wp.php
r1639319 r1639382 5 5 * 6 6 * @author Tyler Bailey 7 * @version 0. 7.07 * @version 0.8.0 8 8 * @package remote-post-swap 9 9 * @subpackage remote-post-swap/inc … … 15 15 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 16 16 17 if(!class_exists('RPS \RPS_Replace_WP')) :17 if(!class_exists('RPS_Replace_WP')) : 18 18 19 19 class RPS_Replace_WP extends RPS_Retrieve_Data { … … 46 46 return $posts; 47 47 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 83 53 * 84 54 * @param $posts - object - WP Posts Object 85 55 * @return $posts - WP Post Object 86 * @since 0. 5.087 */ 88 private function rps_s wap_loop_posts($posts) {56 * @since 0.8.0 57 */ 58 private function rps_setup_api_data($posts) { 89 59 // Get the total post count queried 90 60 $post_count = count($posts); … … 92 62 $rps_retrieve = array(); 93 63 64 // Default number of RPS posts to retrieve (0) 65 $rps_count = 0; 66 94 67 foreach($posts as $post) { 95 68 $rps_id = RPS_Base::rps_get_post_meta($post->ID); … … 113 86 114 87 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 115 91 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)); 126 97 } 127 98 } else { 128 $rps_args = array( 129 'include' => $rps_retrieve 130 ); 99 $rps_args['include'] = $rps_retrieve; 131 100 } 132 101 133 102 // 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); 135 104 136 105 // If we got new posts + retrieved saved posts, merge the array … … 141 110 if($rpsp && !empty($rpsp)) { 142 111 // 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++; 182 179 } 183 180 … … 222 219 } 223 220 224 new RPS_Replace_WP();225 226 221 endif; -
remote-post-swap/trunk/inc/class-rps-retrieve-data.php
r1639319 r1639382 5 5 * 6 6 * @author Tyler Bailey 7 * @version 0. 7.07 * @version 0.8.0 8 8 * @package remote-post-swap 9 9 * @subpackage remote-post-swap/inc … … 14 14 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 15 15 16 if(!class_exists('RPS \RPS_Retrieve_Data')) :16 if(!class_exists('RPS_Retrieve_Data')) : 17 17 18 18 class RPS_Retrieve_Data { … … 27 27 28 28 /** 29 * The API endpoint for posts30 *31 * @var $rps_posts32 * @since 0.5.033 */34 protected $rps_posts;35 36 /**37 * The API endpoint for users38 *39 * @var $rps_users40 * @since 0.5.041 */42 protected $rps_users;43 44 /**45 * The API endpoint for media46 *47 * @var $rps_media48 * @since 0.5.049 */50 protected $rps_media;51 52 /**53 29 * Executed on class istantiation. 54 30 * … … 60 36 61 37 // 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/'; 68 39 } 69 40 70 41 /** 71 * Retrieves the posts from the target site API42 * Retrieves the API data from the target site 72 43 * 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 74 46 * @param $filters - array - Array of API filters 75 * @return $posts- array - Array of post data returned from API76 * @since 0. 5.047 * @return $api_return - array - Array of post data returned from API 48 * @since 0.8.0 77 49 */ 78 public function rps_get_ posts($id, $filters = array()) {50 public function rps_get_api_data($id = '', $type = 'posts', $filters = array()) { 79 51 80 $ posts= false;52 $resp = false; 81 53 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); 85 56 86 $fc = 0; 87 $filter_str = ''; 57 $url = $this->rps_base_url . $type . $id . $filter_str; 88 58 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)) { 89 85 foreach($filters as $key => $filter) { 90 86 if(is_array($filter)) { … … 94 90 $fc++; 95 91 } 96 97 $resp = wp_remote_get($this->rps_posts . $filter_str);98 99 } else {100 $resp = wp_remote_get($this->rps_posts);101 92 } 102 93 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; 169 95 } 170 96 } 171 172 new RPS_Retrieve_Data(); 173 97 174 98 endif; -
remote-post-swap/trunk/inc/class-rps.php
r1639319 r1639382 5 5 * 6 6 * @author Tyler Bailey 7 * @version 0. 7.07 * @version 0.8.0 8 8 * @package remote-post-swap 9 9 * @subpackage remote-post-swap/inc … … 14 14 defined( 'ABSPATH' ) or die( 'No script kiddies please!' ); 15 15 16 if(!class_exists('RPS \RPS')) :16 if(!class_exists('RPS')) : 17 17 18 18 class RPS { … … 24 24 */ 25 25 public function __construct() { 26 $this->set_locale();27 $this->rps_init();26 self::set_locale(); 27 self::rps_init(); 28 28 } 29 29 … … 36 36 * @since 0.7.0 37 37 */ 38 private function rps_init() {38 private static function rps_init() { 39 39 new RPS_Replace_WP; 40 40 … … 49 49 * @since 0.5.0 50 50 */ 51 private function set_locale() {51 private static function set_locale() { 52 52 load_plugin_textdomain( RPS_SLUG, false, RPS_GLOBAL_DIR . 'language' ); 53 53 } -
remote-post-swap/trunk/readme.txt
r1639319 r1639382 4 4 Requires at least: 4.7.0 5 5 Tested up to: 4.7.3 6 Stable tag: 0. 7.06 Stable tag: 0.8.0 7 7 License: GPLv2 or later 8 8 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 37 37 == Changelog == 38 38 39 = 0.8.0 = 40 * Major performance enhancements & code optimization 41 39 42 = 0.7.0 = 40 43 * 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.