Completed
Push — feature/sync-json-endpoints ( ca493e...d48054 )
by
unknown
20:01
created

WPCOM_JSON_API_Update_Site_Logo_Endpoint   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
D callback() 0 34 10
A get_current_settings() 0 7 2
1
<?php
2
3
class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint {
4
	function callback( $path = '', $site_id = 0 ) {
5
		// Switch to the given blog.
6
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
7
		if ( is_wp_error( $blog_id ) ) {
8
			return $blog_id;
9
		}
10
11
		if ( ! current_user_can( 'edit_theme_options' ) ) {
12
			return new WP_Error( 'unauthorized', 'User is not authorized to access logo settings', 403 );
13
		}
14
15
		if ( strpos( $path, '/delete' ) ) {
16
			delete_option( 'site_logo' );
17
			return array();
18
		}
19
20
		$args = $this->input();
21
		$logo_settings = $this->get_current_settings();
22
		if ( empty( $args ) || ! is_array( $args ) ) {
23
			return $logo_settings;
24
		}
25
26
		if ( isset( $args['id'] ) ) {
27
			$logo_settings['id'] = intval( $args['id'], 10 );
28
		}
29
		if ( isset( $args['url'] ) ) {
30
			$logo_settings['url'] = $args['url'];
31
		}
32
		if ( isset( $args['url'] ) || isset( $args['id'] ) ) {
33
			update_option( 'site_logo', $logo_settings );
34
		}
35
36
		return $this->get_current_settings();
37
	}
38
39
	function get_current_settings() {
40
		$logo_settings = get_option( 'site_logo' );
41
		if ( ! is_array( $logo_settings ) ) {
42
			$logo_settings = array();
43
		}
44
		return $logo_settings;
45
	}
46
}
47
48