Changeset 1181912
- Timestamp:
- 06/16/2015 03:03:25 PM (11 years ago)
- Location:
- content-staging/trunk
- Files:
-
- 1 added
- 4 edited
-
classes/apis/class-common-api.php (modified) (3 diffs)
-
classes/factories/class-xmlrpc-client-factory.php (added)
-
classes/xmlrpc/class-client.php (modified) (6 diffs)
-
content-staging.php (modified) (5 diffs)
-
readme.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
content-staging/trunk/classes/apis/class-common-api.php
r1167043 r1181912 148 148 ); 149 149 150 $this->client->request( 'smeContentStaging.verify', $request ); 151 $response = $this->client->get_response_data(); 150 $response = $this->client->request( 'smeContentStaging.verify', $request ); 152 151 153 152 // Hook in after batch has been transferred. … … 203 202 ); 204 203 205 $this->client->request( 'smeContentStaging.import', $request ); 206 $response = $this->client->get_response_data(); 204 $response = $this->client->request( 'smeContentStaging.import', $request ); 207 205 208 206 // Batch deploy in progress. … … 620 618 ); 621 619 622 $this->client->request( 'smeContentStaging.importStatus', $request ); 623 $response = $this->client->get_response_data(); 620 $response = $this->client->request( 'smeContentStaging.importStatus', $request ); 624 621 $response = apply_filters( 'sme_deploy_status', $response ); 625 622 -
content-staging/trunk/classes/xmlrpc/class-client.php
r1167043 r1181912 5 5 use \WP_HTTP_IXR_Client; 6 6 7 class Client extends WP_HTTP_IXR_Client { 7 class Client { 8 9 /** 10 * @var WP_HTTP_IXR_Client 11 */ 12 private $ixr_client; 13 14 /** 15 * Arguments to send with the XML-RPC request. 16 * 17 * @var array 18 */ 19 private $request_args; 20 21 /** 22 * Number of request attempts. 23 * 24 * @var int 25 */ 26 private $attempts; 8 27 9 28 private $secret_key; … … 11 30 private $filtered_response; 12 31 13 public function __construct() { 14 15 $endpoint = 'http://[YOUR_ENDPOINT_HERE]'; 16 $secret_key = 'YOUR_SECRET_KEY'; 17 18 if ( defined( 'CONTENT_STAGING_ENDPOINT' ) && CONTENT_STAGING_ENDPOINT ) { 19 $endpoint = CONTENT_STAGING_ENDPOINT; 20 } else if ( $endpoint_opt = get_option( 'sme_cs_endpoint' ) ) { 21 $endpoint = $endpoint_opt; 22 } 23 24 // Set secret key. 25 if ( defined( 'CONTENT_STAGING_SECRET_KEY' ) && CONTENT_STAGING_SECRET_KEY ) { 26 $secret_key = CONTENT_STAGING_SECRET_KEY; 27 } else if ( $secret_key_opt = get_option( 'sme_cs_secret_key' ) ) { 28 $secret_key = $secret_key_opt; 29 } 30 31 // Allow filtering of endpoint and secret key. 32 $endpoint = apply_filters( 'sme_endpoint', $endpoint ); 33 $secret_key = apply_filters( 'sme_secret_key', $secret_key ); 34 35 $this->secret_key = $secret_key; 36 37 parent::__construct( trailingslashit( $endpoint ) . 'xmlrpc.php', false, false, CONTENT_STAGING_TRANSFER_TIMEOUT ); 38 } 39 40 /** 41 * Perform the XML-RPC request and store the response. 32 /** 33 * Constructor. 34 */ 35 public function __construct( WP_HTTP_IXR_Client $ixr_client, $secret_key ) { 36 $this->ixr_client = $ixr_client; 37 $this->secret_key = $secret_key; 38 $this->request_args = array(); 39 $this->attempts = 0; 40 } 41 42 /************************************************************************ 43 * Perform Request. 44 * 45 * Operations carried out on Content Stage to handle sending a XML-RPC 46 * request to Production. 47 ************************************************************************/ 48 49 /** 50 * Prepare and perform the XML-RPC request and return the response. 42 51 * 43 52 * @param string $method 44 * @param array $data 53 * @param array $data 54 * 45 55 * @return array 46 56 */ 47 57 public function request( $method, $data = array() ) { 58 59 $this->request_args = $this->prepare_request_args( $method, $data ); 60 $this->ixr_client->path = $this->prepare_request_path( $this->ixr_client->path ); 61 $this->ixr_client->headers = $this->prepare_request_headers( $this->ixr_client->headers ); 62 63 // Send request. 64 $query_successful = $this->send(); 65 66 if ( ! $query_successful ) { 67 return $this->get_error_message(); 68 } 69 70 // Get the XML-RPC response data. 71 return unserialize( $this->decode( $this->filtered_response ) ); 72 } 73 74 /** 75 * Perform XML-RPC request. 76 * 77 * @return bool 78 */ 79 private function send() { 80 81 // Disable SSL verification (based on user settings). 82 $this->disable_ssl_verification(); 83 84 // Perform XML-RPC request. Returns true on success, false on failure. 85 $query_successful = call_user_func_array( array( $this->ixr_client, 'query' ), $this->request_args ); 86 87 // Enable SSL verification. 88 $this->enable_ssl_verification(); 89 90 // On failure, increment number of request and retry. 91 if ( ! $query_successful ) { 92 $this->attempts++; 93 return $this->retry(); 94 } 95 96 // Reset number of request attempts. 97 $this->attempts = 0; 98 99 // Get response. 100 $this->filtered_response = $this->ixr_client->getResponse(); 101 102 return $query_successful; 103 } 104 105 /** 106 * Retry sending the XML-RPC request. 107 * 108 * @return bool True on successful request, false on failure. 109 */ 110 private function retry() { 111 112 // Error message. 113 $msg = $this->ixr_client->getErrorMessage(); 114 115 // Log. 116 error_log( 117 sprintf( 118 '[SME] Request to host %s failed: %s (error code %s)', 119 $this->ixr_client->server, $msg, $this->ixr_client->getErrorCode() 120 ) 121 ); 122 123 // Error messages that should trigger request to be re-sent. 124 $retry_triggers = array( 125 'transport error - HTTP status code was not 200 (500)' 126 ); 127 128 // Anonymous function checking if a phrase from an error message is 129 // part of the actual error message. 130 $retry_callback = function( $carry, $item ) use ( $msg ) { 131 if ( $carry ) return $carry; 132 return strpos( $msg, $item ) !== false; 133 }; 134 135 // Check if request should be re-sent. 136 $should_retry = array_reduce( $retry_triggers, $retry_callback, false ); 137 138 // Request should not be re-sent, return error message. 139 if ( $this->attempts >= 3 || ! $should_retry ) { 140 $this->attempts = 0; 141 return false; 142 } 143 144 // Wait time until next request. 145 $seconds = 5 * $this->attempts; 146 147 // Log. 148 error_log( sprintf( '[SME] Re-send request in %d seconds...', $seconds ) ); 149 150 // Wait before trying to send the request again. 151 sleep( $seconds ); 152 153 return $this->send(); 154 } 155 156 /** 157 * Arguments to be sent with the XML-RPC request. 158 * 159 * @param string $method 160 * @param array $data 161 * 162 * @return array 163 */ 164 private function prepare_request_args( $method, $data = array() ) { 165 48 166 $data = $this->encode( serialize( $data ) ); 49 167 50 $args =array(168 return array( 51 169 $method, 52 170 $this->generate_access_token( $data ), 53 171 $data, 54 172 ); 55 56 // Allow custom path to send XML-RPC request to. 57 $this->path = apply_filters( 'sme_xmlrpc_path', $this->path ); 58 59 // Allow custom headers. 60 $this->headers = apply_filters( 'sme_client_headers', array() ); 61 62 // Disable SSL verification (based on user settings). 63 $this->disable_ssl_verification(); 64 65 /* 66 * Perform the XML-RPC request. A HTTP status code is returned indicating 67 * whether the request was successful (200) or not (any other code). 68 */ 69 $status = call_user_func_array( array( $this, 'query' ), $args ); 70 71 // Enable SSL verification. 72 $this->enable_ssl_verification(); 73 74 if ( ! $status ) { 75 76 if ( strpos( $this->getErrorMessage(), 'requested method smeContentStaging.verify does not exist' ) !== false ) { 77 $message = new Message(); 78 $message->set_level( 'error' ); 79 $message->set_message( 80 sprintf( 'Content Staging plugin not activated on host <strong>%s</strong>', $this->server ) 81 ); 82 83 $this->filtered_response = array( 84 'status' => 2, 85 'messages' => array( $message ), 86 ); 87 88 return; 89 } 90 91 if ( strpos( $this->getErrorMessage(), 'Could not resolve host' ) !== false ) { 92 $message = new Message(); 93 $message->set_level( 'error' ); 94 $message->set_message( 95 sprintf( 'Could not connect to host <strong>%s</strong>', $this->server ) 96 ); 97 98 $this->filtered_response = array( 99 'status' => 2, 100 'messages' => array( $message ), 101 ); 102 103 return; 104 } 105 106 $message = new Message(); 107 $message->set_level( 'error' ); 108 $message->set_message( 109 sprintf( 110 '%s - on host: %s (error code %s)', 111 $this->getErrorMessage(), 112 $this->server, 113 $this->getErrorCode() 114 ) 115 ); 116 117 $this->filtered_response = array( 118 'status' => 2, 119 'messages' => array( $message ), 120 ); 121 122 } else { 123 124 // Get the XML-RPC response data. 125 $this->filtered_response = unserialize( $this->decode( $this->getResponse() ) ); 126 } 127 } 173 } 174 175 /** 176 * Set custom path to send XML-RPC request to. 177 * 178 * @param string $path 179 * 180 * @return string 181 */ 182 private function prepare_request_path( $path ) { 183 return apply_filters( 'sme_xmlrpc_path', $path ); 184 } 185 186 /** 187 * Set custom request headers. 188 * 189 * @param array $headers 190 * 191 * @return array 192 */ 193 private function prepare_request_headers( $headers ) { 194 return apply_filters( 'sme_client_headers', $headers ); 195 } 196 197 /** 198 * Handle failed request. 199 * 200 * @return array 201 */ 202 private function get_error_message() { 203 204 if ( strpos( $this->ixr_client->getErrorMessage(), 'requested method smeContentStaging.verify does not exist' ) !== false ) { 205 return $this->error_plugin_inactive(); 206 } 207 208 if ( strpos( $this->ixr_client->getErrorMessage(), 'Could not resolve host' ) !== false ) { 209 return $this->error_host_not_found(); 210 } 211 212 return $this->error_general(); 213 } 214 215 /** 216 * Content Staging plugin is not active. 217 * 218 * @return array 219 */ 220 private function error_plugin_inactive() { 221 $message = new Message(); 222 $message->set_level( 'error' ); 223 $message->set_message( 224 sprintf( 'Content Staging plugin not activated on host <strong>%s</strong>', $this->ixr_client->server ) 225 ); 226 227 return array( 228 'status' => 2, 229 'messages' => array( $message ), 230 ); 231 } 232 233 /** 234 * Remote host could not be found. 235 * 236 * @return array 237 */ 238 private function error_host_not_found() { 239 $message = new Message(); 240 $message->set_level( 'error' ); 241 $message->set_message( 242 sprintf( 'Could not connect to host <strong>%s</strong>', $this->ixr_client->server ) 243 ); 244 245 return array( 246 'status' => 2, 247 'messages' => array( $message ), 248 ); 249 } 250 251 /** 252 * Error occurred during request. 253 * 254 * @return array 255 */ 256 private function error_general() { 257 $message = new Message(); 258 $message->set_level( 'error' ); 259 $message->set_message( 260 sprintf( 261 '%s - on host: %s (error code %s)', 262 $this->ixr_client->getErrorMessage(), 263 $this->ixr_client->server, 264 $this->ixr_client->getErrorCode() 265 ) 266 ); 267 268 return array( 269 'status' => 2, 270 'messages' => array( $message ), 271 ); 272 } 273 274 /************************************************************************ 275 * Handle Request. 276 * 277 * Operations carried out on Production to handle an incoming XML-RPC 278 * request from Content Stage. 279 ************************************************************************/ 128 280 129 281 /** … … 133 285 * 134 286 * @param array $args 287 * 135 288 * @return array 136 289 */ … … 204 357 205 358 /** 206 * Return the response data. 207 */ 208 public function get_response_data() { 209 return $this->filtered_response; 210 } 359 * Prepare response data. 360 * 361 * @param array $response 362 * 363 * @return string 364 */ 365 public function prepare_response( $response ) { 366 return $this->encode( serialize( $response ) ); 367 } 368 369 /************************************************************************ 370 * Common 371 ************************************************************************/ 211 372 212 373 /** … … 218 379 private function generate_access_token( $data ) { 219 380 return hash_hmac( 'sha1', $data, $this->secret_key ); 220 }221 222 /**223 * Prepare response data.224 *225 * @param array $response226 *227 * @return string228 */229 public function prepare_response( $response ) {230 return $this->encode( serialize( $response ) );231 381 } 232 382 … … 271 421 } 272 422 } 423 273 424 } -
content-staging/trunk/content-staging.php
r1167043 r1181912 4 4 * Plugin URI: https://github.com/stenberg/content-staging 5 5 * Description: Content Staging. 6 * Author: Joakim Stenberg , Fredrik Hörte7 * Version: 2.0. 06 * Author: Joakim Stenberg 7 * Version: 2.0.1 8 8 * License: GPLv2 9 9 */ … … 46 46 require_once( 'classes/db/class-user-dao.php' ); 47 47 require_once( 'classes/factories/class-dao-factory.php' ); 48 require_once( 'classes/factories/class-xmlrpc-client-factory.php' ); 48 49 require_once( 'classes/importers/class-batch-importer.php' ); 49 50 require_once( 'classes/importers/class-batch-ajax-importer.php' ); … … 80 81 use Me\Stenberg\Content\Staging\Controllers\Batch_History_Ctrl; 81 82 use Me\Stenberg\Content\Staging\Factories\DAO_Factory; 83 use Me\Stenberg\Content\Staging\Factories\XMLRPC_Client_Factory; 82 84 use Me\Stenberg\Content\Staging\Listeners\Common_Listener; 83 85 use Me\Stenberg\Content\Staging\Listeners\Import_Message_Listener; … … 87 89 use Me\Stenberg\Content\Staging\Controllers\Batch_Ctrl; 88 90 use Me\Stenberg\Content\Staging\Importers\Batch_Importer_Factory; 89 use Me\Stenberg\Content\Staging\XMLRPC\Client;90 91 91 92 /** … … 143 144 144 145 // XMLRPC client. 145 $xmlrpc_client = new Client(); 146 $xmlrpc_client_factory = new XMLRPC_Client_Factory(); 147 $xmlrpc_client = $xmlrpc_client_factory->create(); 146 148 147 149 /* -
content-staging/trunk/readme.txt
r1167043 r1181912 1 1 === Content Staging === 2 Contributors: stenberg.me , horte2 Contributors: stenberg.me 3 3 Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=6L9DXMHNE3A6Q 4 4 Tags: staging, stage, deploy, deploying, sync, syncing, environment, environments, database, databases, enterprise 5 5 Requires at least: 3.7 6 6 Tested up to: 4.2.2 7 Stable tag: 2.0. 07 Stable tag: 2.0.1 8 8 License: GPLv2 9 9 License URI: http://www.gnu.org/licenses/gpl-2.0.html … … 52 52 53 53 == Changelog == 54 55 = 2.0.1 = 56 * Fix undefined constant. 54 57 55 58 = 2.0.0 =
Note: See TracChangeset
for help on using the changeset viewer.