class.wpcom-json-api-add-widget-endpoint.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Activate a widget on a site.
  4. *
  5. * https://public-api.wordpress.com/rest/v1.1/sites/$site/widgets/new
  6. */
  7. new WPCOM_JSON_API_Add_Widgets_Endpoint( array (
  8. 'description' => 'Activate a widget on a site.',
  9. 'group' => 'sites',
  10. 'stat' => 'widgets:new',
  11. 'method' => 'POST',
  12. 'min_version' => '1.1',
  13. 'path' => '/sites/%s/widgets/new',
  14. 'path_labels' => array(
  15. '$site' => '(string) Site ID or domain.'
  16. ),
  17. 'request_format' => array(
  18. 'id_base' => '(string) The base ID of the widget.',
  19. 'sidebar' => '(string) Optional. The ID of the sidebar where this widget will be active. If empty, the widget will be added in the first sidebar available.',
  20. 'position' => '(int) Optional. The position of the widget in the sidebar.',
  21. 'settings' => '(object) Optional. The settings for the new widget.',
  22. ),
  23. 'response_format' => array(
  24. 'id' => '(string) The actual ID of the widget.',
  25. 'sidebar' => '(string) The ID of the sidebar where this widget will be active.',
  26. 'position' => '(int) The final position of the widget in the sidebar.',
  27. 'settings' => '(array) The settings for the new widget.',
  28. ),
  29. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/12345678/widgets/new',
  30. 'example_request_data' => array(
  31. 'headers' => array(
  32. 'authorization' => 'Bearer YOUR_API_TOKEN'
  33. ),
  34. 'body' => array(
  35. 'id_base' => 'text',
  36. 'sidebar' => 'sidebar-2',
  37. 'position' => '0',
  38. 'settings' => array( 'title' => 'hello world' ),
  39. )
  40. ),
  41. 'example_response' => '
  42. {
  43. "id": "text-3",
  44. "id_base": "text",
  45. "settings": {
  46. "title": "hello world"
  47. },
  48. "sidebar": "sidebar-2",
  49. "position": 0
  50. }'
  51. ) );
  52. class WPCOM_JSON_API_Add_Widgets_Endpoint extends WPCOM_JSON_API_Endpoint {
  53. /**
  54. * API callback.
  55. *
  56. * @param string $path
  57. * @param int $blog_id
  58. * @uses jetpack_require_lib
  59. * @uses Jetpack_Widgets
  60. *
  61. * @return array|WP_Error
  62. */
  63. function callback( $path = '', $blog_id = 0 ) {
  64. // Switch to the given blog.
  65. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  66. if ( is_wp_error( $blog_id ) ) {
  67. return $blog_id;
  68. }
  69. if ( ! current_user_can( 'edit_theme_options' ) ) {
  70. return new WP_Error( 'unauthorized', 'User is not authorized to access widgets', 403 );
  71. }
  72. jetpack_require_lib( 'widgets' );
  73. $args = $this->input( false, false ); // Don't filter the input
  74. if ( empty( $args ) || ! is_array( $args ) ) {
  75. return new WP_Error( 'no_data', 'No data was provided.', 400 );
  76. }
  77. if ( isset( $args['widgets'] ) || ! empty( $args['widgets'] ) ) {
  78. $widgets = Jetpack_Widgets::activate_widgets( $args['widgets'] );
  79. if ( is_wp_error( $widgets ) ) {
  80. return $widgets;
  81. }
  82. return array( 'widgets' => $widgets );
  83. }
  84. if ( ! isset( $args['id_base'] ) ) {
  85. return new WP_Error( 'missing_data', 'The data you provided was not accurate.', 400 );
  86. }
  87. if ( empty( $args['sidebar'] ) ) {
  88. $active_sidebars = Jetpack_Widgets::get_active_sidebars();
  89. reset( $active_sidebars );
  90. $args['sidebar'] = key( $active_sidebars );
  91. }
  92. return Jetpack_Widgets::activate_widget( $args['id_base'], $args['sidebar'], $args['position'], $args['settings'] );
  93. }
  94. }