WPCOM_JSON_API_Update_Sharing_Buttons_Endpoint

The WordPress Core 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. if ( ! isset( $input['sharing_buttons'] ) ) { 
  19. $input['sharing_buttons'] = array(); 
  20.  
  21. // We do a first pass of all buttons to verify that no validation 
  22. // issues exist before continuing to update 
  23. foreach ( $input['sharing_buttons'] as $button ) { 
  24. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons ); 
  25. $is_custom = $this->is_button_input_for_custom( $button ); 
  26.  
  27. // If neither custom nor existing, bail 
  28. if ( ! $button_exists && ! $is_custom ) { 
  29. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 ); 
  30.  
  31. // Validate input, only testing custom values if the button doesn't 
  32. // already exist 
  33. $validation_error = $this->validate_button_input( $button, ! $button_exists ); 
  34. if ( is_wp_error( $validation_error ) ) { 
  35. return $validation_error; 
  36.  
  37. // Reset all existing buttons 
  38. $this->sharing_service->set_blog_services( array(), array() ); 
  39.  
  40. // Finally, we iterate over each button and update or create 
  41. $success = true; 
  42. $updated = array(); 
  43. foreach ( $input['sharing_buttons'] as $button ) { 
  44. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons ); 
  45. if ( $button_exists ) { 
  46. $updated_service = $this->update_button( $button['ID'], $button ); 
  47. } else { 
  48. $updated_service = $this->create_custom_button( $button ); 
  49.  
  50. // We'll allow the request to continue if a failure occurred, but 
  51. // log it for the response 
  52. if ( false === $updated_service ) { 
  53. $success = false; 
  54. } else { 
  55. $updated[] = $this->format_sharing_button( $updated_service ); 
  56.  
  57. return array( 
  58. 'success' => $success,  
  59. 'updated' => $updated 
  60. ); 
  61.