WPCOM_JSON_API_Sharing_Button_Endpoint

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

Defined (1)

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

/json-endpoints/class.wpcom-json-api-sharing-buttons-endpoint.php  
  1. abstract class WPCOM_JSON_API_Sharing_Button_Endpoint extends WPCOM_JSON_API_Endpoint { 
  2.  
  3. public static $all_visibilities = array( 'visible', 'hidden' ); 
  4.  
  5. protected $sharing_service; 
  6.  
  7. protected function setup() { 
  8. $this->sharing_service = new Sharing_Service(); 
  9.  
  10. if ( ! current_user_can( 'manage_options' ) ) { 
  11. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 ); 
  12. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) || 
  13. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) { 
  14. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 ); 
  15.  
  16. public function format_sharing_button( $button ) { 
  17. $response = array( 
  18. 'ID' => $button->get_id(),  
  19. 'name' => $button->get_name(),  
  20. 'shortname' => $button->shortname,  
  21. 'custom' => is_a( $button, 'Share_Custom' ),  
  22. 'enabled' => $this->is_button_enabled( $button ),  
  23. ); 
  24.  
  25. if ( $response['enabled'] ) { 
  26. // Status is either "disabled" or the visibility value 
  27. $response['visibility'] = $this->get_button_visibility( $button ); 
  28.  
  29. if ( ! empty( $button->genericon ) ) { 
  30. // Only pre-defined sharing buttons include genericon 
  31. $response['genericon'] = $button->genericon; 
  32.  
  33. if ( method_exists( $button, 'get_options' ) ) { 
  34. // merge get_options() values into response, primarily to account 
  35. // for custom sharing button values 
  36. foreach ( $button->get_options() as $key => $value ) { 
  37. // Capitalize URL property 
  38. if ( 'url' === strtolower( $key ) ) { 
  39. $key = strtoupper( $key ); 
  40.  
  41. $response[ $key ] = $value; 
  42.  
  43. return $response; 
  44.  
  45. public function get_button_visibility( $button ) { 
  46. $services = $this->sharing_service->get_blog_services(); 
  47. $visibilities = self::$all_visibilities; 
  48. $button_id = $button->get_id(); 
  49.  
  50. foreach ( $visibilities as $visibility ) { 
  51. if ( isset( $services[ $visibility ][ $button_id ] ) ) { 
  52. return $visibility; 
  53.  
  54. return false; 
  55.  
  56. public function is_button_enabled( $button ) { 
  57. return false !== $this->get_button_visibility( $button ); 
  58.  
  59. protected function is_button_input_for_custom( $button ) { 
  60. return ( isset( $button['custom'] ) && $button['custom'] ) || 
  61. ( isset( $button['ID'] ) && 1 === preg_match( '/^custom-/', $button['ID'] ) ) || 
  62. ! empty( $button['name'] ) || ! empty( $button['URL'] ) || ! empty( $button['icon'] ); 
  63.  
  64. protected function validate_button_input( $button, $is_new = false ) { 
  65. if ( ! empty( $button['visibility'] ) && ! in_array( $button['visibility'], self::$all_visibilities ) ) { 
  66. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', self::$all_visibilities ) ), 400 ); 
  67. } else if ( $is_new && empty( $button['URL'] ) ) { 
  68. return new WP_Error( 'invalid_request', 'The URL field is required', 400 ); 
  69. } else if ( $is_new && empty( $button['icon'] ) ) { 
  70. return new WP_Error( 'invalid_request', 'The icon field is required', 400 ); 
  71.  
  72. public function create_custom_button( $button ) { 
  73. // Default visibility to 'visible' if enabled 
  74. if ( empty( $button['visibility'] ) && true === $button['enabled'] ) { 
  75. $button['visibility'] = 'visible'; 
  76.  
  77. $updated_service = $this->sharing_service->new_service( $button['name'], $button['URL'], $button['icon'] ); 
  78. if ( false !== $updated_service && ( true === $button['enabled'] || ! empty( $button['visibility'] ) ) ) { 
  79. $blog_services = $this->sharing_service->get_blog_services(); 
  80. $blog_services[ $button['visibility'] ][ (string) $updated_service->get_id() ] = $updated_service; 
  81. $this->sharing_service->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) ); 
  82.  
  83. return $updated_service; 
  84.  
  85. public function update_button( $button_id, $button ) { 
  86. $blog_services = $this->sharing_service->get_blog_services(); 
  87.  
  88. // Find existing button 
  89. $all_buttons = $this->sharing_service->get_all_services_blog(); 
  90. if ( ! array_key_exists( $button_id, $all_buttons ) ) { 
  91. // Button doesn't exist 
  92. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 ); 
  93.  
  94. $updated_service = $all_buttons[ $button_id ]; 
  95. $service_id = $updated_service->get_id(); 
  96. if ( is_a( $all_buttons[ $button_id ], 'Share_Custom' ) ) { 
  97. // Replace options for existing custom button 
  98. $options = $updated_service->get_options(); 
  99. $name = isset( $button['name'] ) ? $button['name'] : $options['name']; 
  100. $url = isset( $button['URL'] ) ? $button['URL'] : $options['url']; 
  101. $icon = isset( $button['icon'] ) ? $button['icon'] : $options['icon']; 
  102. $updated_service = new Share_Custom( $service_id, array( 'name' => $name, 'url' => $url, 'icon' => $icon ) ); 
  103. $this->sharing_service->set_service( $button_id, $updated_service ); 
  104.  
  105. // Default visibility to 'visible' if enabled 
  106. if ( empty( $button['visibility'] ) && true === $button['enabled'] ) { 
  107. $button['visibility'] = 'visible'; 
  108. } else if ( false === $button['enabled'] ) { 
  109. unset( $button['visibility'] ); 
  110.  
  111. // Update button visibility and enabled status 
  112. $visibility_changed = ( isset( $button['visibility'] ) || true === $button['enabled'] ) && ! array_key_exists( $service_id, $blog_services[ $button['visibility'] ] ); 
  113. $is_disabling = false === $button['enabled']; 
  114. if ( $visibility_changed || $is_disabling ) { 
  115. // Remove from all other visibilities 
  116. foreach ( $blog_services as $service_visibility => $services ) { 
  117. if ( $service_visibility !== $button['visibility'] || $is_disabling ) { 
  118. unset( $blog_services[ $service_visibility ][ $service_id ] ); 
  119.  
  120. if ( $visibility_changed ) { 
  121. $blog_services[ $button['visibility'] ][ $service_id ] = $updated_service; 
  122.  
  123. $this->sharing_service->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) ); 
  124.  
  125. return $updated_service; 
  126.