WPCOM_JSON_API_Update_Sharing_Buttons_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Update Sharing Buttons Endpoint class.

Defined (1)

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

/json-endpoints/class.wpcom-json-api-sharing-buttons-endpoint.php  
  1. class WPCOM_JSON_API_Update_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint { 
  2.  
  3. // POST /sites/%s/sharing-buttons -> $blog_id 
  4. public function callback( $path = '', $blog_id = 0 ) { 
  5. $input = $this->input(); 
  6.  
  7. // Validate request 
  8. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); 
  9. if ( is_wp_error( $blog_id ) ) { 
  10. return $blog_id; 
  11.  
  12. $continue = $this->setup(); 
  13. if ( is_wp_error( $continue ) ) { 
  14. return $continue; 
  15.  
  16. $all_buttons = $this->sharing_service->get_all_services_blog(); 
  17.  
  18. // We do a first pass of all buttons to verify that no validation 
  19. // issues exist before continuing to update 
  20. foreach ( $input['sharing_buttons'] as $button ) { 
  21. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons ); 
  22. $is_custom = $this->is_button_input_for_custom( $button ); 
  23.  
  24. // If neither custom nor existing, bail 
  25. if ( ! $button_exists && ! $is_custom ) { 
  26. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 ); 
  27.  
  28. // Validate input, only testing custom values if the button doesn't 
  29. // already exist 
  30. $validation_error = $this->validate_button_input( $button, ! $button_exists ); 
  31. if ( is_wp_error( $validation_error ) ) { 
  32. return $validation_error; 
  33.  
  34. // Reset all existing buttons 
  35. $this->sharing_service->set_blog_services( array(), array() ); 
  36.  
  37. // Finally, we iterate over each button and update or create 
  38. $success = true; 
  39. $updated = array(); 
  40. foreach ( $input['sharing_buttons'] as $button ) { 
  41. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons ); 
  42. if ( $button_exists ) { 
  43. $updated_service = $this->update_button( $button['ID'], $button ); 
  44. } else { 
  45. $updated_service = $this->create_custom_button( $button ); 
  46.  
  47. // We'll allow the request to continue if a failure occurred, but 
  48. // log it for the response 
  49. if ( false === $updated_service ) { 
  50. $success = false; 
  51. } else { 
  52. $updated[] = $this->format_sharing_button( $updated_service ); 
  53.  
  54. return array( 
  55. 'success' => $success,  
  56. 'updated' => $updated 
  57. ); 
  58.