WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint

The Jetpack by WordPress.com WPCOM JSON API Get 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_Get_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint { 
  2.  
  3. // GET /sites/%s/sharing-buttons -> $blog_id 
  4. public function callback( $path = '', $blog_id = 0 ) { 
  5. $args = $this->query_args(); 
  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. if ( ! empty( $args['visibility'] ) && ! in_array( $args['visibility'], self::$all_visibilities ) ) { 
  17. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', self::$all_visibilities ) ), 400 ); 
  18.  
  19. // Determine which visibilities to include based on request 
  20. $visibilities = empty( $args['visibility'] ) ? self::$all_visibilities : array( $args['visibility'] ); 
  21.  
  22. // Discover enabled services 
  23. $buttons = array(); 
  24. $enabled_services = $this->sharing_service->get_blog_services(); 
  25. $all_services = $this->sharing_service->get_all_services_blog(); 
  26.  
  27. // Include buttons of desired visibility 
  28. foreach ( $visibilities as $visibility ) { 
  29. $buttons = array_merge( $buttons, $enabled_services[ $visibility ] ); 
  30.  
  31. // Unless `enabled_only` or `visibility` is specified, append the 
  32. // remaining buttons to the end of the array 
  33. if ( ( ! isset( $args['enabled_only'] ) || ! $args['enabled_only'] ) && empty( $args['visibility'] ) ) { 
  34. foreach ( $all_services as $id => $button ) { 
  35. if ( ! array_key_exists( $id, $buttons ) ) { 
  36. $buttons[ $id ] = $button; 
  37.  
  38. // Format each button in the response 
  39. $response = array(); 
  40. foreach ( $buttons as $button ) { 
  41. $response[] = $this->format_sharing_button( $button ); 
  42.  
  43. return array( 
  44. 'found' => count( $response ),  
  45. 'sharing_buttons' => $response 
  46. );