説明なし

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

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 (deprecated)',
  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. ),
  26. ),
  27. 'example_response' => '
  28. {
  29. "id": 12345,
  30. "url": "https:\/\/s.w.org\/about\/images\/logos\/codeispoetry-rgb.png"
  31. }'
  32. ) );
  33. new WPCOM_JSON_API_Update_Site_Logo_Endpoint( array (
  34. 'description' => 'Delete site logo settings',
  35. 'group' => '__do_not_document',
  36. 'stat' => 'sites:1:logo:delete',
  37. 'method' => 'POST',
  38. 'min_version' => '1.1',
  39. 'path' => '/sites/%s/logo/delete',
  40. 'path_labels' => array(
  41. '$site' => '(string) Site ID or domain.',
  42. ),
  43. 'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/logo/delete',
  44. 'example_request_data' => array(
  45. 'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
  46. ),
  47. ) );
  48. class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint {
  49. function callback( $path = '', $site_id = 0 ) {
  50. // Switch to the given blog.
  51. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
  52. if ( is_wp_error( $blog_id ) ) {
  53. return $blog_id;
  54. }
  55. if ( ! current_user_can( 'edit_theme_options' ) ) {
  56. return new WP_Error( 'unauthorized', 'User is not authorized to access logo settings', 403 );
  57. }
  58. if ( strpos( $path, '/delete' ) ) {
  59. delete_option( 'site_logo' );
  60. return array();
  61. }
  62. $args = $this->input();
  63. $logo_settings = $this->get_current_settings();
  64. if ( empty( $args ) || ! is_array( $args ) ) {
  65. return $logo_settings;
  66. }
  67. if ( isset( $args['id'] ) ) {
  68. update_option( 'site_logo', (int) $args['id'] );
  69. }
  70. return $this->get_current_settings();
  71. }
  72. function get_current_settings() {
  73. $logo_id = get_option( 'site_logo' );
  74. if ( ! $logo_id ) {
  75. return array();
  76. }
  77. return array(
  78. 'id' => $logo_id,
  79. 'url' => wp_get_attachment_url( $logo_id ),
  80. );
  81. }
  82. }