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