summaryrefslogtreecommitdiff
blob: 2ccca0a98fd2d62005fba5ebe07efc2d5d4b7dc7 (plain)
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
<?php

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',
	),
	'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,
			'url' => 'https://s.w.org/about/images/logos/codeispoetry-rgb.png',
		),
	),
	'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' ),
	),
) );

class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint {
	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'] ) ) {
			$logo_settings['id'] = intval( $args['id'], 10 );
		}
		if ( isset( $args['url'] ) ) {
			$logo_settings['url'] = $args['url'];
		}
		if ( isset( $args['url'] ) || isset( $args['id'] ) ) {
			update_option( 'site_logo', $logo_settings );
		}

		return $this->get_current_settings();
	}

	function get_current_settings() {
		$logo_settings = get_option( 'site_logo' );
		if ( ! is_array( $logo_settings ) ) {
			$logo_settings = array();
		}
		return $logo_settings;
	}
}