class.wpcom-json-api-update-site-logo-endpoint.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. new WPCOM_JSON_API_Update_Site_Logo_Endpoint( array (
  3. 'description' => 'Set site logo settings',
  4. 'group' => '__do_not_document',
  5. 'stat' => 'sites:1:logo',
  6. 'method' => 'POST',
  7. 'min_version' => '1.1',
  8. 'path' => '/sites/%s/logo',
  9. 'path_labels' => array(
  10. '$site' => '(string) Site ID or domain.',
  11. ),
  12. 'request_format' => array(
  13. 'id' => '(int) The ID of the logo post',
  14. 'url' => '(string) The URL of the logo post',
  15. ),
  16. 'response_format' => array(
  17. 'id' => '(int) The ID of the logo post',
  18. 'url' => '(string) The URL of the logo post',
  19. ),
  20. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/logo',
  21. 'example_request_data' => array(
  22. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  23. 'body' => array(
  24. 'id' => 12345,
  25. 'url' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
  26. ),
  27. ),
  28. 'example_response' => '
  29. {
  30. "id": 12345,
  31. "url": "https:\/\/s.w.org\/about\/images\/logos\/codeispoetry-rgb.png"
  32. }'
  33. ) );
  34. new WPCOM_JSON_API_Update_Site_Logo_Endpoint( array (
  35. 'description' => 'Delete site logo settings',
  36. 'group' => '__do_not_document',
  37. 'stat' => 'sites:1:logo:delete',
  38. 'method' => 'POST',
  39. 'min_version' => '1.1',
  40. 'path' => '/sites/%s/logo/delete',
  41. 'path_labels' => array(
  42. '$site' => '(string) Site ID or domain.',
  43. ),
  44. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/logo/delete',
  45. 'example_request_data' => array(
  46. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  47. ),
  48. ) );
  49. class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint {
  50. function callback( $path = '', $site_id = 0 ) {
  51. // Switch to the given blog.
  52. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
  53. if ( is_wp_error( $blog_id ) ) {
  54. return $blog_id;
  55. }
  56. if ( ! current_user_can( 'edit_theme_options' ) ) {
  57. return new WP_Error( 'unauthorized', 'User is not authorized to access logo settings', 403 );
  58. }
  59. if ( strpos( $path, '/delete' ) ) {
  60. delete_option( 'site_logo' );
  61. return array();
  62. }
  63. $args = $this->input();
  64. $logo_settings = $this->get_current_settings();
  65. if ( empty( $args ) || ! is_array( $args ) ) {
  66. return $logo_settings;
  67. }
  68. if ( isset( $args['id'] ) ) {
  69. $logo_settings['id'] = intval( $args['id'], 10 );
  70. }
  71. if ( isset( $args['url'] ) ) {
  72. $logo_settings['url'] = $args['url'];
  73. }
  74. if ( isset( $args['url'] ) || isset( $args['id'] ) ) {
  75. update_option( 'site_logo', $logo_settings );
  76. }
  77. return $this->get_current_settings();
  78. }
  79. function get_current_settings() {
  80. $logo_settings = get_option( 'site_logo' );
  81. if ( ! is_array( $logo_settings ) ) {
  82. $logo_settings = array();
  83. }
  84. return $logo_settings;
  85. }
  86. }