class.wpcom-json-api-sharing-buttons-endpoint.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. if ( class_exists( 'Sharing_Service' ) ) {
  7. $this->sharing_service = new Sharing_Service();
  8. }
  9. if ( ! current_user_can( 'manage_options' ) ) {
  10. return new WP_Error( 'forbidden', 'You do not have the capability to manage sharing buttons for this site', 403 );
  11. } else if ( ! class_exists( 'Sharing_Service' ) || ! class_exists( 'Sharing_Source' ) ||
  12. ( method_exists( 'Jetpack', 'is_module_active' ) && ! Jetpack::is_module_active( 'sharedaddy' ) ) ) {
  13. return new WP_Error( 'missing_jetpack_module', 'The Sharing module must be activated in order to use this endpoint', 400 );
  14. }
  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. if ( $response['enabled'] ) {
  25. // Status is either "disabled" or the visibility value
  26. $response['visibility'] = $this->get_button_visibility( $button );
  27. }
  28. if ( ! empty( $button->icon ) ) {
  29. // Only pre-defined sharing buttons include genericon
  30. $response['genericon'] = $button->icon;
  31. }
  32. if ( method_exists( $button, 'get_options' ) ) {
  33. // merge get_options() values into response, primarily to account
  34. // for custom sharing button values
  35. foreach ( $button->get_options() as $key => $value ) {
  36. // Capitalize URL property
  37. if ( 'url' === strtolower( $key ) ) {
  38. $key = strtoupper( $key );
  39. }
  40. $response[ $key ] = $value;
  41. }
  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. foreach ( $visibilities as $visibility ) {
  50. if ( isset( $services[ $visibility ][ $button_id ] ) ) {
  51. return $visibility;
  52. }
  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. }
  73. public function create_custom_button( $button ) {
  74. // Default visibility to 'visible' if enabled
  75. if ( empty( $button['visibility'] ) && true === $button['enabled'] ) {
  76. $button['visibility'] = 'visible';
  77. }
  78. $updated_service = $this->sharing_service->new_service( $button['name'], $button['URL'], $button['icon'] );
  79. if ( false !== $updated_service && ( true === $button['enabled'] || ! empty( $button['visibility'] ) ) ) {
  80. $blog_services = $this->sharing_service->get_blog_services();
  81. $blog_services[ $button['visibility'] ][ (string) $updated_service->get_id() ] = $updated_service;
  82. $this->sharing_service->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) );
  83. }
  84. return $updated_service;
  85. }
  86. public function update_button( $button_id, $button ) {
  87. $blog_services = $this->sharing_service->get_blog_services();
  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 ( $is_disabling || $service_visibility !== $button['visibility'] ) {
  118. unset( $blog_services[ $service_visibility ][ $service_id ] );
  119. }
  120. }
  121. if ( $visibility_changed ) {
  122. $blog_services[ $button['visibility'] ][ $service_id ] = $updated_service;
  123. }
  124. $this->sharing_service->set_blog_services( array_keys( $blog_services['visible'] ), array_keys( $blog_services['hidden'] ) );
  125. }
  126. return $updated_service;
  127. }
  128. }
  129. new WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint( array(
  130. 'description' => 'Get a list of a site\'s sharing buttons.',
  131. 'group' => 'sharing',
  132. 'stat' => 'sharing-buttons',
  133. 'method' => 'GET',
  134. 'path' => '/sites/%s/sharing-buttons/',
  135. 'path_labels' => array(
  136. '$site' => '(int|string) Site ID or domain',
  137. ),
  138. 'query_parameters' => array(
  139. 'enabled_only' => '(bool) If true, only enabled sharing buttons are included in the response',
  140. 'visibility' => '(string) The type of enabled sharing buttons to filter by, either "visible" or "hidden"',
  141. ),
  142. 'response_format' => array(
  143. 'found' => '(int) The total number of sharing buttons found that match the request.',
  144. 'sharing_buttons' => '(array:object) Array of sharing button objects',
  145. ),
  146. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/sharing-buttons/',
  147. 'example_request_data' => array(
  148. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  149. ),
  150. 'example_response' => '
  151. {
  152. "found": 2,
  153. "sharing_buttons": [
  154. {
  155. "ID": "twitter",
  156. "name": "Twitter",
  157. "shortname": "twitter",
  158. "custom": false,
  159. "enabled": true,
  160. "visibility": "visible",
  161. "genericon": "\\f202"
  162. },
  163. {
  164. "ID": "facebook",
  165. "name": "Facebook",
  166. "shortname": "facebook",
  167. "custom": false,
  168. "enabled": true,
  169. "visibility": "visible",
  170. "genericon": "\\f203"
  171. }
  172. ]
  173. }'
  174. ) );
  175. class WPCOM_JSON_API_Get_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  176. // GET /sites/%s/sharing-buttons -> $blog_id
  177. public function callback( $path = '', $blog_id = 0 ) {
  178. $args = $this->query_args();
  179. // Validate request
  180. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  181. if ( is_wp_error( $blog_id ) ) {
  182. return $blog_id;
  183. }
  184. $continue = $this->setup();
  185. if ( is_wp_error( $continue ) ) {
  186. return $continue;
  187. }
  188. if ( ! empty( $args['visibility'] ) && ! in_array( $args['visibility'], self::$all_visibilities ) ) {
  189. return new WP_Error( 'invalid_visibility', sprintf( 'The visibility field must be one of the following values: %s', implode( ', ', self::$all_visibilities ) ), 400 );
  190. }
  191. // Determine which visibilities to include based on request
  192. $visibilities = empty( $args['visibility'] ) ? self::$all_visibilities : array( $args['visibility'] );
  193. // Discover enabled services
  194. $buttons = array();
  195. $enabled_services = $this->sharing_service->get_blog_services();
  196. $all_services = $this->sharing_service->get_all_services_blog();
  197. // Include buttons of desired visibility
  198. foreach ( $visibilities as $visibility ) {
  199. $buttons = array_merge( $buttons, $enabled_services[ $visibility ] );
  200. }
  201. // Unless `enabled_only` or `visibility` is specified, append the
  202. // remaining buttons to the end of the array
  203. if ( ( ! isset( $args['enabled_only'] ) || ! $args['enabled_only'] ) && empty( $args['visibility'] ) ) {
  204. foreach ( $all_services as $id => $button ) {
  205. if ( ! array_key_exists( $id, $buttons ) ) {
  206. $buttons[ $id ] = $button;
  207. }
  208. }
  209. }
  210. // Format each button in the response
  211. $response = array();
  212. foreach ( $buttons as $button ) {
  213. $response[] = $this->format_sharing_button( $button );
  214. }
  215. return array(
  216. 'found' => count( $response ),
  217. 'sharing_buttons' => $response
  218. );
  219. }
  220. }
  221. new WPCOM_JSON_API_Get_Sharing_Button_Endpoint( array(
  222. 'description' => 'Get information about a single sharing button.',
  223. 'group' => '__do_not_document',
  224. 'stat' => 'sharing-buttons:1',
  225. 'method' => 'GET',
  226. 'path' => '/sites/%s/sharing-buttons/%s',
  227. 'path_labels' => array(
  228. '$site' => '(int|string) Site ID or domain',
  229. '$button_id' => '(string) The button ID',
  230. ),
  231. 'response_format' => array(
  232. 'ID' => '(int) Sharing button ID',
  233. 'name' => '(string) Sharing button name, used as a label on the button itself',
  234. 'shortname' => '(string) A generated short name for the sharing button',
  235. 'URL' => '(string) The URL pattern defined for a custom sharing button',
  236. 'icon' => '(string) URL to the 16x16 icon defined for a custom sharing button',
  237. 'genericon' => '(string) Icon character in Genericons icon set',
  238. 'custom' => '(bool) Is the button a user-created custom sharing button?',
  239. 'enabled' => '(bool) Is the button currently enabled for the site?',
  240. 'visibility' => '(string) If enabled, the current visibility of the sharing button, either "visible" or "hidden"',
  241. ),
  242. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/sharing-buttons/facebook',
  243. 'example_request_data' => array(
  244. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  245. ),
  246. 'example_response' => '{
  247. "ID": "facebook",
  248. "name": "Facebook",
  249. "shortname": "facebook",
  250. "custom": false,
  251. "enabled": true,
  252. "visibility": "visible",
  253. "genericon": "\\f203"
  254. }'
  255. ) );
  256. class WPCOM_JSON_API_Get_Sharing_Button_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  257. // GET /sites/%s/sharing-buttons/%s -> $blog_id, $button_id
  258. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  259. // Validate request
  260. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  261. if ( is_wp_error( $blog_id ) ) {
  262. return $blog_id;
  263. }
  264. $continue = $this->setup();
  265. if ( is_wp_error( $continue ) ) {
  266. return $continue;
  267. }
  268. // Search existing services for button
  269. $all_buttons = $this->sharing_service->get_all_services_blog();
  270. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  271. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  272. } else {
  273. return $this->format_sharing_button( $all_buttons[ $button_id ] );
  274. }
  275. }
  276. }
  277. new WPCOM_JSON_API_Update_Sharing_Buttons_Endpoint( array(
  278. 'description' => 'Edit all sharing buttons for a site.',
  279. 'group' => 'sharing',
  280. 'stat' => 'sharing-buttons:X:POST',
  281. 'method' => 'POST',
  282. 'path' => '/sites/%s/sharing-buttons',
  283. 'path_labels' => array(
  284. '$site' => '(int|string) Site ID or domain',
  285. ),
  286. 'request_format' => array(
  287. 'sharing_buttons' => '(array:sharing_button) An array of sharing button objects',
  288. ),
  289. 'response_format' => array(
  290. 'success' => '(bool) Confirmation that all sharing buttons were updated as specified',
  291. 'updated' => '(array) An array of updated sharing buttons',
  292. ),
  293. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/sharing-buttons',
  294. 'example_request_data' => array(
  295. 'headers' => array(
  296. 'authorization' => 'Bearer YOUR_API_TOKEN',
  297. ),
  298. 'body' => array(
  299. 'sharing_buttons' => array(
  300. array(
  301. 'ID' => 'facebook',
  302. 'visibility' => 'hidden',
  303. )
  304. )
  305. )
  306. ),
  307. 'example_response' => '{
  308. "success": true,
  309. "updated": [
  310. {
  311. "ID": "facebook",
  312. "name": "Facebook",
  313. "shortname": "facebook",
  314. "custom": false,
  315. "enabled": true,
  316. "visibility": "hidden",
  317. "genericon": "\\f204"
  318. }
  319. ]
  320. }'
  321. ) );
  322. class WPCOM_JSON_API_Update_Sharing_Buttons_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  323. // POST /sites/%s/sharing-buttons -> $blog_id
  324. public function callback( $path = '', $blog_id = 0 ) {
  325. $input = $this->input();
  326. // Validate request
  327. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  328. if ( is_wp_error( $blog_id ) ) {
  329. return $blog_id;
  330. }
  331. $continue = $this->setup();
  332. if ( is_wp_error( $continue ) ) {
  333. return $continue;
  334. }
  335. $all_buttons = $this->sharing_service->get_all_services_blog();
  336. if ( ! isset( $input['sharing_buttons'] ) ) {
  337. $input['sharing_buttons'] = array();
  338. }
  339. // We do a first pass of all buttons to verify that no validation
  340. // issues exist before continuing to update
  341. foreach ( $input['sharing_buttons'] as $button ) {
  342. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons );
  343. $is_custom = $this->is_button_input_for_custom( $button );
  344. // If neither custom nor existing, bail
  345. if ( ! $button_exists && ! $is_custom ) {
  346. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  347. }
  348. // Validate input, only testing custom values if the button doesn't
  349. // already exist
  350. $validation_error = $this->validate_button_input( $button, ! $button_exists );
  351. if ( is_wp_error( $validation_error ) ) {
  352. return $validation_error;
  353. }
  354. }
  355. // Reset all existing buttons
  356. $this->sharing_service->set_blog_services( array(), array() );
  357. // Finally, we iterate over each button and update or create
  358. $success = true;
  359. $updated = array();
  360. foreach ( $input['sharing_buttons'] as $button ) {
  361. $button_exists = isset( $button['ID'] ) && array_key_exists( $button['ID'], $all_buttons );
  362. if ( $button_exists ) {
  363. $updated_service = $this->update_button( $button['ID'], $button );
  364. } else {
  365. $updated_service = $this->create_custom_button( $button );
  366. }
  367. // We'll allow the request to continue if a failure occurred, but
  368. // log it for the response
  369. if ( false === $updated_service ) {
  370. $success = false;
  371. } else {
  372. $updated[] = $this->format_sharing_button( $updated_service );
  373. }
  374. }
  375. return array(
  376. 'success' => $success,
  377. 'updated' => $updated
  378. );
  379. }
  380. }
  381. new WPCOM_JSON_API_Update_Sharing_Button_Endpoint( array(
  382. 'description' => 'Create a new custom sharing button.',
  383. 'group' => '__do_not_document',
  384. 'stat' => 'sharing-buttons:new',
  385. 'method' => 'POST',
  386. 'path' => '/sites/%s/sharing-buttons/new',
  387. 'path_labels' => array(
  388. '$site' => '(int|string) Site ID or domain',
  389. ),
  390. 'request_format' => array(
  391. 'name' => '(string) The name for your custom sharing button, used as a label on the button itself',
  392. 'URL' => '(string) The URL to use for share links, including optional placeholders (%post_id%, %post_title%, %post_slug%, %post_url%, %post_full_url%, %post_excerpt%, %post_tags%, %home_url%)',
  393. 'icon' => '(string) The full URL to a 16x16 icon to display on the sharing button',
  394. 'enabled' => '(bool) Is the button currently enabled for the site?',
  395. 'visibility' => '(string) If enabled, the visibility of the sharing button, either "visible" (default) or "hidden"',
  396. ),
  397. 'response_format' => array(
  398. 'ID' => '(string) Sharing button ID',
  399. 'name' => '(string) Sharing button name, used as a label on the button itself',
  400. 'shortname' => '(string) A generated short name for the sharing button',
  401. 'URL' => '(string) The URL pattern defined for a custom sharing button',
  402. 'icon' => '(string) URL to the 16x16 icon defined for a custom sharing button',
  403. 'genericon' => '(string) Icon character in Genericons icon set',
  404. 'custom' => '(bool) Is the button a user-created custom sharing button?',
  405. 'enabled' => '(bool) Is the button currently enabled for the site?',
  406. 'visibility' => '(string) If enabled, the current visibility of the sharing button, either "visible" or "hidden"',
  407. ),
  408. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/sharing-buttons/new/',
  409. 'example_request_data' => array(
  410. 'headers' => array(
  411. 'authorization' => 'Bearer YOUR_API_TOKEN'
  412. ),
  413. 'body' => array(
  414. 'name' => 'Custom',
  415. 'URL' => 'https://www.wordpress.com/%post_name%',
  416. 'icon' => 'https://en.wordpress.com/i/stats-icon.gif',
  417. 'enabled' => true,
  418. 'visibility' => 'visible'
  419. )
  420. ),
  421. 'example_response' => '{
  422. "ID": "custom-123456789",
  423. "name": "Custom",
  424. "shortname": "custom",
  425. "url": "https://www.wordpress.com/%post_name%",
  426. "icon": "https://en.wordpress.com/i/stats-icon.gif",
  427. "custom": true,
  428. "enabled": true,
  429. "visibility": "visible"
  430. }'
  431. ) );
  432. new WPCOM_JSON_API_Update_Sharing_Button_Endpoint( array(
  433. 'description' => 'Edit a sharing button.',
  434. 'group' => '__do_not_document',
  435. 'stat' => 'sharing-buttons:1:POST',
  436. 'method' => 'POST',
  437. 'path' => '/sites/%s/sharing-buttons/%s',
  438. 'path_labels' => array(
  439. '$site' => '(int|string) Site ID or domain',
  440. '$button_id' => '(string) The button ID',
  441. ),
  442. 'request_format' => array(
  443. 'name' => '(string) Only if a custom sharing button, a new name used as a label on the button itself',
  444. 'URL' => '(string) Only if a custom sharing button, the URL to use for share links, including optional placeholders (%post_title%, %post_url%, %post_full_url%, %post_excerpt%, %post_tags%)',
  445. 'icon' => '(string) Only if a custom sharing button, the full URL to a 16x16 icon to display on the sharing button',
  446. 'enabled' => '(bool) Is the button currently enabled for the site?',
  447. 'visibility' => '(string) If enabled, the visibility of the sharing button, either "visible" (default) or "hidden"',
  448. ),
  449. 'response_format' => array(
  450. 'ID' => '(string) Sharing button ID',
  451. 'name' => '(string) Sharing button name, used as a label on the button itself',
  452. 'shortname' => '(string) A generated short name for the sharing button',
  453. 'URL' => '(string) The URL pattern defined for a custom sharing button',
  454. 'icon' => '(string) URL to the 16x16 icon defined for a custom sharing button',
  455. 'genericon' => '(string) Icon character in Genericons icon set',
  456. 'custom' => '(bool) Is the button a user-created custom sharing button?',
  457. 'enabled' => '(bool) Is the button currently enabled for the site?',
  458. 'visibility' => '(string) If enabled, the current visibility of the sharing button, either "visible" or "hidden"',
  459. ),
  460. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/sharing-buttons/custom-123456789/',
  461. 'example_request_data' => array(
  462. 'headers' => array(
  463. 'authorization' => 'Bearer YOUR_API_TOKEN'
  464. ),
  465. 'body' => array(
  466. 'enabled' => false,
  467. )
  468. ),
  469. 'example_response' => '{
  470. "ID": "custom-123456789",
  471. "name": "Custom",
  472. "shortname": "custom",
  473. "custom": true,
  474. "enabled": false,
  475. "icon": "https://en.wordpress.com/i/stats-icon.gif",
  476. "url": "https://www.wordpress.com/%post_name%"
  477. }'
  478. ) );
  479. class WPCOM_JSON_API_Update_Sharing_Button_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  480. // POST /sites/%s/sharing-buttons/new -> $blog_id
  481. // POST /sites/%s/sharing-buttons/%s -> $blog_id, $button_id
  482. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  483. $new = $this->api->ends_with( $path, '/new' );
  484. $input = $this->input();
  485. // Validate request
  486. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  487. if ( is_wp_error( $blog_id ) ) {
  488. return $blog_id;
  489. }
  490. $continue = $this->setup();
  491. if ( is_wp_error( $continue ) ) {
  492. return $continue;
  493. }
  494. $validation_error = $this->validate_button_input( $input, $new );
  495. if ( is_wp_error( $validation_error ) ) {
  496. return $validation_error;
  497. }
  498. // Update or create button
  499. if ( $new ) {
  500. $updated_service = $this->create_custom_button( $input );
  501. } else {
  502. $updated_service = $this->update_button( $button_id, $input );
  503. }
  504. if ( false === $updated_service ) {
  505. return new WP_Error( 'invalid_request', sprintf( 'The sharing button was not %s', $new ? 'created' : 'updated' ), 400 );
  506. } else if ( is_wp_error( $updated_service ) ) {
  507. return $updated_service;
  508. } else {
  509. return $this->format_sharing_button( $updated_service );
  510. }
  511. }
  512. }
  513. new WPCOM_JSON_API_Delete_Sharing_Button_Endpoint( array(
  514. 'description' => 'Delete a custom sharing button.',
  515. 'group' => '__do_not_document',
  516. 'stat' => 'sharing-buttons:1:delete',
  517. 'method' => 'POST',
  518. 'path' => '/sites/%s/sharing-buttons/%s/delete',
  519. 'path_labels' => array(
  520. '$site' => '(int|string) Site ID or domain',
  521. '$button_id' => '(string) The button ID',
  522. ),
  523. 'response_format' => array(
  524. 'ID' => '(int) The ID of the deleted sharing button',
  525. 'success' => '(bool) Confirmation that the sharing button has been removed'
  526. ),
  527. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/30434183/sharing-buttons/custom-123456789/delete',
  528. 'example_request_data' => array(
  529. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  530. ),
  531. 'example_response' => '{
  532. "ID": "custom-123456789",
  533. "success": "true"
  534. }'
  535. ) );
  536. class WPCOM_JSON_API_Delete_Sharing_Button_Endpoint extends WPCOM_JSON_API_Sharing_Button_Endpoint {
  537. // POST /sites/%s/sharing-buttons/%s/delete -> $blog_id, $button_id
  538. public function callback( $path = '', $blog_id = 0, $button_id = 0 ) {
  539. // Validate request
  540. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  541. if ( is_wp_error( $blog_id ) ) {
  542. return $blog_id;
  543. }
  544. $continue = $this->setup();
  545. if ( is_wp_error( $continue ) ) {
  546. return $continue;
  547. }
  548. // Find existing button
  549. $all_buttons = $this->sharing_service->get_all_services_blog();
  550. if ( ! array_key_exists( $button_id, $all_buttons ) ) {
  551. // Button doesn't exist
  552. return new WP_Error( 'not_found', 'The specified sharing button was not found', 404 );
  553. }
  554. // Verify button is custom
  555. if ( ! is_a( $all_buttons[ $button_id ], 'Share_Custom' ) ) {
  556. return new WP_error( 'invalid_request', 'Only custom sharing buttons can be deleted', 400 );
  557. }
  558. $success = $this->sharing_service->delete_service( $button_id );
  559. return array(
  560. 'ID' => $button_id,
  561. 'success' => $success
  562. );
  563. }
  564. }