Jetpack_Client_Server

Client = Plugin Client Server = API Methods the Plugin must respond to.

Defined (1)

The class is defined in the following location(s).

/class.jetpack-client-server.php  
  1. class Jetpack_Client_Server { 
  2.  
  3. function authorize() { 
  4. $data = stripslashes_deep( $_GET ); 
  5. $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; 
  6.  
  7. $jetpack_unique_connection = Jetpack_Options::get_option( 'unique_connection' ); 
  8. // Checking if site has been active/connected previously before recording unique connection 
  9. if ( ! $jetpack_unique_connection ) { 
  10. // jetpack_unique_connection option has never been set 
  11. $jetpack_unique_connection = array( 
  12. 'connected' => 0,  
  13. 'disconnected' => 0,  
  14. 'version' => '3.6.1' 
  15. ); 
  16.  
  17. update_option( 'jetpack_unique_connection', $jetpack_unique_connection ); 
  18.  
  19. //track unique connection 
  20. $jetpack = Jetpack::init(); 
  21.  
  22. $jetpack->stat( 'connections', 'unique-connection' ); 
  23. $jetpack->do_stats( 'server_side' ); 
  24.  
  25. // increment number of times connected 
  26. $jetpack_unique_connection['connected'] += 1; 
  27. Jetpack_Options::update_option( 'unique_connection', $jetpack_unique_connection ); 
  28.  
  29. do { 
  30. $jetpack = $this->get_jetpack(); 
  31. $role = $jetpack->translate_current_user_to_role(); 
  32.  
  33. if ( !$role ) { 
  34. Jetpack::state( 'error', 'no_role' ); 
  35. break; 
  36.  
  37. $cap = $jetpack->translate_role_to_cap( $role ); 
  38. if ( !$cap ) { 
  39. Jetpack::state( 'error', 'no_cap' ); 
  40. break; 
  41.  
  42. $this->check_admin_referer( "jetpack-authorize_{$role}_{$redirect}" ); 
  43.  
  44. if ( !empty( $data['error'] ) ) { 
  45. Jetpack::state( 'error', $data['error'] ); 
  46. break; 
  47.  
  48. if ( empty( $data['state'] ) ) { 
  49. Jetpack::state( 'error', 'no_state' ); 
  50. break; 
  51.  
  52. if ( !ctype_digit( $data['state'] ) ) { 
  53. Jetpack::state( 'error', 'invalid_state' ); 
  54. break; 
  55.  
  56. $current_user_id = get_current_user_id(); 
  57. if ( $current_user_id != $data['state'] ) { 
  58. Jetpack::state( 'error', 'wrong_state' ); 
  59. break; 
  60.  
  61. if ( empty( $data['code'] ) ) { 
  62. Jetpack::state( 'error', 'no_code' ); 
  63. break; 
  64.  
  65. $token = $this->get_token( $data ); 
  66.  
  67. if ( is_wp_error( $token ) ) { 
  68. if ( $error = $token->get_error_code() ) 
  69. Jetpack::state( 'error', $error ); 
  70. else 
  71. Jetpack::state( 'error', 'invalid_token' ); 
  72.  
  73. Jetpack::state( 'error_description', $token->get_error_message() ); 
  74.  
  75. break; 
  76.  
  77. if ( !$token ) { 
  78. Jetpack::state( 'error', 'no_token' ); 
  79. break; 
  80.  
  81. $is_master_user = ! Jetpack::is_active(); 
  82.  
  83. Jetpack::update_user_token( $current_user_id, sprintf( '%s.%d', $token, $current_user_id ), $is_master_user ); 
  84.  
  85.  
  86. if ( $is_master_user ) { 
  87. Jetpack::state( 'message', 'authorized' ); 
  88. } else { 
  89. Jetpack::state( 'message', 'linked' ); 
  90. // Don't activate anything since we are just connecting a user. 
  91. break; 
  92.  
  93. if ( $active_modules = Jetpack_Options::get_option( 'active_modules' ) ) { 
  94. Jetpack_Options::delete_option( 'active_modules' ); 
  95.  
  96. Jetpack::activate_default_modules( 999, 1, $active_modules ); 
  97. } else { 
  98. Jetpack::activate_default_modules(); 
  99.  
  100. // Sync all registers options and constants 
  101. /** This action is documented in class.jetpack.php */ 
  102. do_action( 'jetpack_sync_all_registered_options' ); 
  103.  
  104. // Start nonce cleaner 
  105. wp_clear_scheduled_hook( 'jetpack_clean_nonces' ); 
  106. wp_schedule_event( time(), 'hourly', 'jetpack_clean_nonces' ); 
  107. } while ( false ); 
  108.  
  109. if ( wp_validate_redirect( $redirect ) ) { 
  110. $this->wp_safe_redirect( $redirect ); 
  111. } else { 
  112. $this->wp_safe_redirect( Jetpack::admin_url() ); 
  113.  
  114. $this->do_exit(); 
  115.  
  116. public static function deactivate_plugin( $probable_file, $probable_title ) { 
  117. include_once( ABSPATH . 'wp-admin/includes/plugin.php' ); 
  118. if ( is_plugin_active( $probable_file ) ) { 
  119. deactivate_plugins( $probable_file ); 
  120. return 1; 
  121. } else { 
  122. // If the plugin is not in the usual place, try looking through all active plugins. 
  123. $active_plugins = Jetpack::get_active_plugins(); 
  124. foreach ( $active_plugins as $plugin ) { 
  125. $data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ); 
  126. if ( $data['Name'] == $probable_title ) { 
  127. deactivate_plugins( $plugin ); 
  128. return 1; 
  129.  
  130. return 0; 
  131.  
  132. /** 
  133. * @return object|WP_Error 
  134. */ 
  135. function get_token( $data ) { 
  136. $jetpack = $this->get_jetpack(); 
  137. $role = $jetpack->translate_current_user_to_role(); 
  138.  
  139. if ( !$role ) { 
  140. return new Jetpack_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack' ) ); 
  141.  
  142. $client_secret = Jetpack_Data::get_access_token(); 
  143. if ( !$client_secret ) { 
  144. return new Jetpack_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack' ) ); 
  145.  
  146. $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : ''; 
  147.  
  148. $body = array( 
  149. 'client_id' => Jetpack_Options::get_option( 'id' ),  
  150. 'client_secret' => $client_secret->secret,  
  151. 'grant_type' => 'authorization_code',  
  152. 'code' => $data['code'],  
  153. 'redirect_uri' => add_query_arg( array( 
  154. 'action' => 'authorize',  
  155. '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),  
  156. 'redirect' => $redirect ? urlencode( $redirect ) : false,  
  157. ), menu_page_url( 'jetpack', false ) ),  
  158. ); 
  159.  
  160. $args = array( 
  161. 'method' => 'POST',  
  162. 'body' => $body,  
  163. 'headers' => array( 
  164. 'Accept' => 'application/json',  
  165. ),  
  166. ); 
  167. $response = Jetpack_Client::_wp_remote_request( Jetpack::fix_url_for_bad_hosts( Jetpack::api_url( 'token' ) ), $args ); 
  168.  
  169. if ( is_wp_error( $response ) ) { 
  170. return new Jetpack_Error( 'token_http_request_failed', $response->get_error_message() ); 
  171.  
  172. $code = wp_remote_retrieve_response_code( $response ); 
  173. $entity = wp_remote_retrieve_body( $response ); 
  174.  
  175. if ( $entity ) 
  176. $json = json_decode( $entity ); 
  177. else 
  178. $json = false; 
  179.  
  180. if ( 200 != $code || !empty( $json->error ) ) { 
  181. if ( empty( $json->error ) ) 
  182. return new Jetpack_Error( 'unknown', '', $code ); 
  183.  
  184. $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack' ), (string) $json->error_description ) : ''; 
  185.  
  186. return new Jetpack_Error( (string) $json->error, $error_description, $code ); 
  187.  
  188. if ( empty( $json->access_token ) || !is_scalar( $json->access_token ) ) { 
  189. return new Jetpack_Error( 'access_token', '', $code ); 
  190.  
  191. if ( empty( $json->token_type ) || 'X_JETPACK' != strtoupper( $json->token_type ) ) { 
  192. return new Jetpack_Error( 'token_type', '', $code ); 
  193.  
  194. if ( empty( $json->scope ) ) { 
  195. return new Jetpack_Error( 'scope', 'No Scope', $code ); 
  196. @list( $role, $hmac ) = explode( ':', $json->scope ); 
  197. if ( empty( $role ) || empty( $hmac ) ) { 
  198. return new Jetpack_Error( 'scope', 'Malformed Scope', $code ); 
  199. if ( $jetpack->sign_role( $role ) !== $json->scope ) { 
  200. return new Jetpack_Error( 'scope', 'Invalid Scope', $code ); 
  201.  
  202. if ( !$cap = $jetpack->translate_role_to_cap( $role ) ) 
  203. return new Jetpack_Error( 'scope', 'No Cap', $code ); 
  204. if ( !current_user_can( $cap ) ) 
  205. return new Jetpack_Error( 'scope', 'current_user_cannot', $code ); 
  206.  
  207. return (string) $json->access_token; 
  208.  
  209. public function get_jetpack() { 
  210. return Jetpack::init(); 
  211.  
  212. public function check_admin_referer( $action ) { 
  213. return check_admin_referer( $action ); 
  214.  
  215. public function wp_safe_redirect( $redirect ) { 
  216. return wp_safe_redirect( $redirect ); 
  217.  
  218. public function do_exit() { 
  219. exit; 
  220.