/shop quần áo starloveshop.com/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-sharing-buttons-endpoint.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien · PHP · 270 lines · 190 code · 44 blank · 36 comment · 75 complexity · e29159e84e5ae94b1c363c2f7f229063 MD5 · raw file

  1. <?php
  2. /*
  3. * WARNING: This file is distributed verbatim in Jetpack.
  4. * There should be nothing WordPress.com specific in this file.
  5. *
  6. * @hide-in-jetpack
  7. */
  8. class WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Endpoint {
  9. public static $all_visibilities = array( 'visible', 'hidden' );
  10. // GET /sites/%s/sharing-buttons -> $blog_id
  11. public function callback( $path = '', $blog_id = 0 ) {
  12. $args = $this->query_args();
  13. // Validate request
  14. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  15. if ( is_wp_error( $blog_id ) ) {
  16. return $blog_id;
  17. }
  18. if ( ! current_user_can( 'manage_options' ) ) {
  19. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
  20. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) ||
  21. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) {
  22. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 );
  23. } else if ( ! empty( $args['visibility'] ) && ! in_array( $args['visibility'], self::$all_visibilities ) ) {
  24. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', self::$all_visibilities ) ), 400 );
  25. }
  26. // Determine which visibilities to include based on request
  27. $visibilities = empty( $args['visibility'] ) || ! in_array( $args['visibility'], self::$all_visibilities ) ? self::$all_visibilities : array( $args['visibility'] );
  28. // Discover enabled services
  29. $ss = new Sharing_Service();
  30. $buttons = array();
  31. $all_services = $ss->get_all_services_blog();
  32. foreach( $all_services as $button ) {
  33. // Filter enabled buttons
  34. if ( isset( $args['enabled_only'] ) && $args['enabled_only'] && ! WPCOM_JSON_API_Get_Sharing_Button_Endpoint::is_button_enabled( $ss, $button ) ) {
  35. continue;
  36. }
  37. // Filter visibility
  38. if ( isset( $args['visibility'] ) && ! in_array( WPCOM_JSON_API_Get_Sharing_Button_Endpoint::get_button_visibility( $ss, $button ), $visibilities ) ) {
  39. continue;
  40. }
  41. $buttons[] = WPCOM_JSON_API_Get_Sharing_Button_Endpoint::format_sharing_button( $ss, $button );
  42. }
  43. return array(
  44. 'found' => count( $buttons ),
  45. 'sharing_buttons' => $buttons
  46. );
  47. }
  48. }
  49. class WPCOM_JSON_API_Get_Sharing_Button_Endpoint extends WPCOM_JSON_API_Endpoint {
  50. public static function format_sharing_button( $sharing_service, $button ) {
  51. $response = array(
  52. 'ID' => $button->get_id(),
  53. 'name' => $button->get_name(),
  54. 'shortname' => $button->shortname,
  55. 'custom' => is_a( $button, 'Share_Custom' ),
  56. 'enabled' => self::is_button_enabled( $sharing_service, $button ),
  57. );
  58. if ( $response['enabled'] ) {
  59. // Status is either "disabled" or the visibility value
  60. $response['visibility'] = self::get_button_visibility( $sharing_service, $button );
  61. }
  62. if ( ! empty( $button->genericon ) ) {
  63. // Only pre-defined sharing buttons include genericon
  64. $response['genericon'] = $button->genericon;
  65. }
  66. if ( method_exists( $button, 'get_options' ) ) {
  67. // merge get_options() values into response, primarily to account
  68. // for custom sharing button values
  69. foreach ( $button->get_options() as $key => $value ) {
  70. // Capitalize URL property
  71. if ( 'url' === strtolower( $key ) ) {
  72. $key = strtoupper( $key );
  73. }
  74. $response[ $key ] = $value;
  75. }
  76. }
  77. return $response;
  78. }
  79. public static function get_button_visibility( $sharing_service, $button ) {
  80. $services = $sharing_service->get_blog_services();
  81. $visibilities = WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint::$all_visibilities;
  82. $button_id = $button->get_id();
  83. foreach ( $visibilities as $visibility ) {
  84. if ( isset( $services[ $visibility ][ $button_id ] ) ) {
  85. return $visibility;
  86. }
  87. }
  88. return false;
  89. }
  90. public static function is_button_enabled( $sharing_service, $button ) {
  91. return false !== self::get_button_visibility( $sharing_service, $button );
  92. }
  93. // GET /sites/%s/sharing-buttons/%s -> $blog_id, $button_id
  94. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  95. // Validate request
  96. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  97. if ( is_wp_error( $blog_id ) ) {
  98. return $blog_id;
  99. }
  100. if ( ! current_user_can( 'manage_options' ) ) {
  101. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
  102. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) ||
  103. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) {
  104. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 );
  105. }
  106. // Search existing services for button
  107. $ss = new Sharing_Service();
  108. $all_buttons = $ss->get_all_services_blog();
  109. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  110. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  111. } else {
  112. return self::format_sharing_button( $ss, $all_buttons[ $button_id ] );
  113. }
  114. }
  115. }
  116. class WPCOM_JSON_API_Update_Sharing_Button_Endpoint extends WPCOM_JSON_API_Endpoint {
  117. // POST /sites/%s/sharing-buttons/new -> $blog_id
  118. // POST /sites/%s/sharing-buttons/%s -> $blog_id, $button_id
  119. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  120. $new = $this->api->ends_with( $path, '/new' );
  121. $input = $this->input();
  122. // Validate request
  123. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  124. if ( is_wp_error( $blog_id ) ) {
  125. return $blog_id;
  126. }
  127. if ( ! current_user_can( 'manage_options' ) ) {
  128. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
  129. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) ||
  130. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) {
  131. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 );
  132. } else if ( ! empty( $input['visibility'] ) && ! in_array( $input['visibility'], WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint::$all_visibilities ) ) {
  133. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint::$all_visibilities ) ), 400 );
  134. } else if ( $new && empty( $input['URL'] ) ) {
  135. return new WP_Error( 'invalid_request', 'The URL field is required', 400 );
  136. } else if ( $new && empty( $input['icon'] ) ) {
  137. return new WP_Error( 'invalid_request', 'The icon field is required', 400 );
  138. }
  139. // Assign default values
  140. $visibility = $input['visibility'];
  141. if ( empty( $visibility ) || ( ! isset( $input['visibility'] ) && true === $input['enabled'] ) ) {
  142. $visibility = 'visible';
  143. }
  144. // Update or create button
  145. $ss = new Sharing_Service();
  146. $blog_services = $ss->get_blog_services();
  147. if ( $new ) {
  148. // Attempt to create new button
  149. $updated_service = $ss->new_service( $input['name'], $input['URL'], $input['icon'] );
  150. if ( false !== $updated_service && ( ( isset( $input['enabled'] ) && true === $input['enabled'] ) || isset( $input['visibility'] ) ) ) {
  151. $blog_services[ $visibility ][ (string) $updated_service->get_id() ] = $updated_service;
  152. $ss->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) );
  153. }
  154. } else {
  155. // Find existing button
  156. $all_buttons = $ss->get_all_services_blog();
  157. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  158. // Button doesn't exist
  159. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  160. }
  161. $updated_service = $all_buttons[ $button_id ];
  162. $service_id = $updated_service->get_id();
  163. if ( is_a( $all_buttons[ $button_id ], 'Share_Custom' ) ) {
  164. // Replace options for existing custom button
  165. $options = $updated_service->get_options();
  166. $name = isset( $input['name'] ) ? $input['name'] : $options['name'];
  167. $url = isset( $input['URL'] ) ? $input['URL'] : $options['url'];
  168. $icon = isset( $input['icon'] ) ? $input['icon'] : $options['icon'];
  169. $updated_service = new Share_Custom( $service_id, array( 'name' => $name, 'url' => $url, 'icon' => $icon ) );
  170. $ss->set_service( $button_id, $updated_service );
  171. }
  172. // Update button visibility
  173. $visibility_changed = ( isset( $input['visibility'] ) || true === $input['enabled'] ) && ! array_key_exists( $service_id, $blog_services[ $visibility ] );
  174. $is_disabling = false === $input['enabled'];
  175. if ( $visibility_changed || $is_disabling ) {
  176. // Remove from all other visibilities
  177. foreach ( $blog_services as $service_visibility => $services ) {
  178. if ( $service_visibility !== $visibility || $is_disabling ) {
  179. unset( $blog_services[ $service_visibility ][ $service_id ] );
  180. }
  181. }
  182. if ( $visibility_changed ) {
  183. $blog_services[ $visibility ][ $service_id ] = $updated_service;
  184. }
  185. $ss->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) );
  186. }
  187. }
  188. if ( false === $updated_service ) {
  189. return new WP_Error( 'invalid_request', sprintf( 'The sharing button was not %s', $new ? 'created' : 'updated' ), 400 );
  190. } else {
  191. return WPCOM_JSON_API_Get_Sharing_Button_Endpoint::format_sharing_button( $ss, $updated_service );
  192. }
  193. }
  194. }
  195. class WPCOM_JSON_API_Delete_Sharing_Button_Endpoint extends WPCOM_JSON_API_Endpoint {
  196. // POST /sites/%s/sharing-buttons/%s/delete -> $blog_id, $button_id
  197. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  198. // Validate request
  199. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  200. if ( is_wp_error( $blog_id ) ) {
  201. return $blog_id;
  202. }
  203. if ( ! current_user_can( 'manage_options' ) ) {
  204. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
  205. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) ||
  206. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) {
  207. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 );
  208. }
  209. // Find existing button
  210. $ss = new Sharing_Service();
  211. $all_buttons = $ss->get_all_services_blog();
  212. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  213. // Button doesn't exist
  214. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  215. }
  216. // Verify button is custom
  217. if ( ! is_a( $all_buttons[ $button_id ], 'Share_Custom' ) ) {
  218. return new WP_error( 'invalid_request', 'Only custom sharing buttons can be deleted', 400 );
  219. }
  220. $success = $ss->delete_service( $button_id );
  221. return array(
  222. 'ID' => $button_id,
  223. 'success' => $success
  224. );
  225. }
  226. }