PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/jetpack/json-endpoints/class.wpcom-json-api-sharing-buttons-endpoint.php

https://gitlab.com/juanito.abelo/nlmobile
PHP | 385 lines | 269 code | 73 blank | 43 comment | 76 complexity | 6e5f6f2b8b36a812cca70894bb1ee6f7 MD5 | raw file
  1. <?php
  2. abstract class WPCOM_JSON_API_Sharing_Button_Endpoint extends WPCOM_JSON_API_Endpoint {
  3. public static $all_visibilities = array( 'visible', 'hidden' );
  4. protected $sharing_service;
  5. protected function setup() {
  6. $this->sharing_service = new Sharing_Service();
  7. if ( ! current_user_can( 'manage_options' ) ) {
  8. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
  9. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) ||
  10. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) {
  11. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 );
  12. }
  13. }
  14. public function format_sharing_button( $button ) {
  15. $response = array(
  16. 'ID' => $button->get_id(),
  17. 'name' => $button->get_name(),
  18. 'shortname' => $button->shortname,
  19. 'custom' => is_a( $button, 'Share_Custom' ),
  20. 'enabled' => $this->is_button_enabled( $button ),
  21. );
  22. if ( $response['enabled'] ) {
  23. // Status is either "disabled" or the visibility value
  24. $response['visibility'] = $this->get_button_visibility( $button );
  25. }
  26. if ( ! empty( $button->genericon ) ) {
  27. // Only pre-defined sharing buttons include genericon
  28. $response['genericon'] = $button->genericon;
  29. }
  30. if ( method_exists( $button, 'get_options' ) ) {
  31. // merge get_options() values into response, primarily to account
  32. // for custom sharing button values
  33. foreach ( $button->get_options() as $key => $value ) {
  34. // Capitalize URL property
  35. if ( 'url' === strtolower( $key ) ) {
  36. $key = strtoupper( $key );
  37. }
  38. $response[ $key ] = $value;
  39. }
  40. }
  41. return $response;
  42. }
  43. public function get_button_visibility( $button ) {
  44. $services = $this->sharing_service->get_blog_services();
  45. $visibilities = self::$all_visibilities;
  46. $button_id = $button->get_id();
  47. foreach ( $visibilities as $visibility ) {
  48. if ( isset( $services[ $visibility ][ $button_id ] ) ) {
  49. return $visibility;
  50. }
  51. }
  52. return false;
  53. }
  54. public function is_button_enabled( $button ) {
  55. return false !== $this->get_button_visibility( $button );
  56. }
  57. protected function is_button_input_for_custom( $button ) {
  58. return ( isset( $button['custom'] ) && $button['custom'] ) ||
  59. ( isset( $button['ID'] ) && 1 === preg_match( '/^custom-/', $button['ID'] ) ) ||
  60. ! empty( $button['name'] ) || ! empty( $button['URL'] ) || ! empty( $button['icon'] );
  61. }
  62. protected function validate_button_input( $button, $is_new = false ) {
  63. if ( ! empty( $button['visibility'] ) && ! in_array( $button['visibility'], self::$all_visibilities ) ) {
  64. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', self::$all_visibilities ) ), 400 );
  65. } else if ( $is_new && empty( $button['URL'] ) ) {
  66. return new WP_Error( 'invalid_request', 'The URL field is required', 400 );
  67. } else if ( $is_new && empty( $button['icon'] ) ) {
  68. return new WP_Error( 'invalid_request', 'The icon field is required', 400 );
  69. }
  70. }
  71. public function create_custom_button( $button ) {
  72. // Default visibility to 'visible' if enabled
  73. if ( empty( $button['visibility'] ) && true === $button['enabled'] ) {
  74. $button['visibility'] = 'visible';
  75. }
  76. $updated_service = $this->sharing_service->new_service( $button['name'], $button['URL'], $button['icon'] );
  77. if ( false !== $updated_service && ( true === $button['enabled'] || ! empty( $button['visibility'] ) ) ) {
  78. $blog_services = $this->sharing_service->get_blog_services();
  79. $blog_services[ $button['visibility'] ][ (string) $updated_service->get_id() ] = $updated_service;
  80. $this->sharing_service->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) );
  81. }
  82. return $updated_service;
  83. }
  84. public function update_button( $button_id, $button ) {
  85. $blog_services = $this->sharing_service->get_blog_services();
  86. // Find existing button
  87. $all_buttons = $this->sharing_service->get_all_services_blog();
  88. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  89. // Button doesn't exist
  90. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  91. }
  92. $updated_service = $all_buttons[ $button_id ];
  93. $service_id = $updated_service->get_id();
  94. if ( is_a( $all_buttons[ $button_id ], 'Share_Custom' ) ) {
  95. // Replace options for existing custom button
  96. $options = $updated_service->get_options();
  97. $name = isset( $button['name'] ) ? $button['name'] : $options['name'];
  98. $url = isset( $button['URL'] ) ? $button['URL'] : $options['url'];
  99. $icon = isset( $button['icon'] ) ? $button['icon'] : $options['icon'];
  100. $updated_service = new Share_Custom( $service_id, array( 'name' => $name, 'url' => $url, 'icon' => $icon ) );
  101. $this->sharing_service->set_service( $button_id, $updated_service );
  102. }
  103. // Default visibility to 'visible' if enabled
  104. if ( empty( $button['visibility'] ) && true === $button['enabled'] ) {
  105. $button['visibility'] = 'visible';
  106. } else if ( false === $button['enabled'] ) {
  107. unset( $button['visibility'] );
  108. }
  109. // Update button visibility and enabled status
  110. $visibility_changed = ( isset( $button['visibility'] ) || true === $button['enabled'] ) && ! array_key_exists( $service_id, $blog_services[ $button['visibility'] ] );
  111. $is_disabling = false === $button['enabled'];
  112. if ( $visibility_changed || $is_disabling ) {
  113. // Remove from all other visibilities
  114. foreach ( $blog_services as $service_visibility => $services ) {
  115. if ( $service_visibility !== $button['visibility'] || $is_disabling ) {
  116. unset( $blog_services[ $service_visibility ][ $service_id ] );
  117. }
  118. }
  119. if ( $visibility_changed ) {
  120. $blog_services[ $button['visibility'] ][ $service_id ] = $updated_service;
  121. }
  122. $this->sharing_service->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) );
  123. }
  124. return $updated_service;
  125. }
  126. }
  127. class WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  128. // GET /sites/%s/sharing-buttons -> $blog_id
  129. public function callback( $path = '', $blog_id = 0 ) {
  130. $args = $this->query_args();
  131. // Validate request
  132. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  133. if ( is_wp_error( $blog_id ) ) {
  134. return $blog_id;
  135. }
  136. $continue = $this->setup();
  137. if ( is_wp_error( $continue ) ) {
  138. return $continue;
  139. }
  140. if ( ! empty( $args['visibility'] ) && ! in_array( $args['visibility'], self::$all_visibilities ) ) {
  141. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', self::$all_visibilities ) ), 400 );
  142. }
  143. // Determine which visibilities to include based on request
  144. $visibilities = empty( $args['visibility'] ) ? self::$all_visibilities : array( $args['visibility'] );
  145. // Discover enabled services
  146. $buttons = array();
  147. $enabled_services = $this->sharing_service->get_blog_services();
  148. $all_services = $this->sharing_service->get_all_services_blog();
  149. // Include buttons of desired visibility
  150. foreach ( $visibilities as $visibility ) {
  151. $buttons = array_merge( $buttons, $enabled_services[ $visibility ] );
  152. }
  153. // Unless `enabled_only` or `visibility` is specified, append the
  154. // remaining buttons to the end of the array
  155. if ( ( ! isset( $args['enabled_only'] ) || ! $args['enabled_only'] ) && empty( $args['visibility'] ) ) {
  156. foreach ( $all_services as $id => $button ) {
  157. if ( ! array_key_exists( $id, $buttons ) ) {
  158. $buttons[ $id ] = $button;
  159. }
  160. }
  161. }
  162. // Format each button in the response
  163. $response = array();
  164. foreach ( $buttons as $button ) {
  165. $response[] = $this->format_sharing_button( $button );
  166. }
  167. return array(
  168. 'found' => count( $response ),
  169. 'sharing_buttons' => $response
  170. );
  171. }
  172. }
  173. class WPCOM_JSON_API_Get_Sharing_Button_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  174. // GET /sites/%s/sharing-buttons/%s -> $blog_id, $button_id
  175. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  176. // Validate request
  177. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  178. if ( is_wp_error( $blog_id ) ) {
  179. return $blog_id;
  180. }
  181. $continue = $this->setup();
  182. if ( is_wp_error( $continue ) ) {
  183. return $continue;
  184. }
  185. // Search existing services for button
  186. $all_buttons = $this->sharing_service->get_all_services_blog();
  187. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  188. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  189. } else {
  190. return $this->format_sharing_button( $all_buttons[ $button_id ] );
  191. }
  192. }
  193. }
  194. class WPCOM_JSON_API_Update_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  195. // POST /sites/%s/sharing-buttons -> $blog_id
  196. public function callback( $path = '', $blog_id = 0 ) {
  197. $input = $this->input();
  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. $continue = $this->setup();
  204. if ( is_wp_error( $continue ) ) {
  205. return $continue;
  206. }
  207. $all_buttons = $this->sharing_service->get_all_services_blog();
  208. // We do a first pass of all buttons to verify that no validation
  209. // issues exist before continuing to update
  210. foreach ( $input['sharing_buttons'] as $button ) {
  211. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons );
  212. $is_custom = $this->is_button_input_for_custom( $button );
  213. // If neither custom nor existing, bail
  214. if ( ! $button_exists && ! $is_custom ) {
  215. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  216. }
  217. // Validate input, only testing custom values if the button doesn't
  218. // already exist
  219. $validation_error = $this->validate_button_input( $button, ! $button_exists );
  220. if ( is_wp_error( $validation_error ) ) {
  221. return $validation_error;
  222. }
  223. }
  224. // Reset all existing buttons
  225. $this->sharing_service->set_blog_services( array(), array() );
  226. // Finally, we iterate over each button and update or create
  227. $success = true;
  228. $updated = array();
  229. foreach ( $input['sharing_buttons'] as $button ) {
  230. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons );
  231. if ( $button_exists ) {
  232. $updated_service = $this->update_button( $button['ID'], $button );
  233. } else {
  234. $updated_service = $this->create_custom_button( $button );
  235. }
  236. // We'll allow the request to continue if a failure occurred, but
  237. // log it for the response
  238. if ( false === $updated_service ) {
  239. $success = false;
  240. } else {
  241. $updated[] = $this->format_sharing_button( $updated_service );
  242. }
  243. }
  244. return array(
  245. 'success' => $success,
  246. 'updated' => $updated
  247. );
  248. }
  249. }
  250. class WPCOM_JSON_API_Update_Sharing_Button_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  251. // POST /sites/%s/sharing-buttons/new -> $blog_id
  252. // POST /sites/%s/sharing-buttons/%s -> $blog_id, $button_id
  253. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  254. $new = $this->api->ends_with( $path, '/new' );
  255. $input = $this->input();
  256. // Validate request
  257. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  258. if ( is_wp_error( $blog_id ) ) {
  259. return $blog_id;
  260. }
  261. $continue = $this->setup();
  262. if ( is_wp_error( $continue ) ) {
  263. return $continue;
  264. }
  265. $validation_error = $this->validate_button_input( $input, $new );
  266. if ( is_wp_error( $validation_error ) ) {
  267. return $validation_error;
  268. }
  269. // Update or create button
  270. if ( $new ) {
  271. $updated_service = $this->create_custom_button( $input );
  272. } else {
  273. $updated_service = $this->update_button( $button_id, $input );
  274. }
  275. if ( false === $updated_service ) {
  276. return new WP_Error( 'invalid_request', sprintf( 'The sharing button was not %s', $new ? 'created' : 'updated' ), 400 );
  277. } else if ( is_wp_error( $updated_service ) ) {
  278. return $updated_service;
  279. } else {
  280. return $this->format_sharing_button( $updated_service );
  281. }
  282. }
  283. }
  284. class WPCOM_JSON_API_Delete_Sharing_Button_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  285. // POST /sites/%s/sharing-buttons/%s/delete -> $blog_id, $button_id
  286. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  287. // Validate request
  288. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  289. if ( is_wp_error( $blog_id ) ) {
  290. return $blog_id;
  291. }
  292. $continue = $this->setup();
  293. if ( is_wp_error( $continue ) ) {
  294. return $continue;
  295. }
  296. // Find existing button
  297. $all_buttons = $this->sharing_service->get_all_services_blog();
  298. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  299. // Button doesn't exist
  300. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  301. }
  302. // Verify button is custom
  303. if ( ! is_a( $all_buttons[ $button_id ], 'Share_Custom' ) ) {
  304. return new WP_error( 'invalid_request', 'Only custom sharing buttons can be deleted', 400 );
  305. }
  306. $success = $this->sharing_service->delete_service( $button_id );
  307. return array(
  308. 'ID' => $button_id,
  309. 'success' => $success
  310. );
  311. }
  312. }