Completed
Push — wpcom/json-endpoints-2017-01-1... ( 87c71c...1c58b3 )
by
unknown
12:29 queued 03:48
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
/**
4
 * @autounit api logo
5
 */
6
class WPCOM_JSON_API_Update_Site_Logo_Endpoint extends WPCOM_JSON_API_Endpoint {
7
	function callback( $path = '', $site_id = 0 ) {
8
		// Switch to the given blog.
9
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
10
		if ( is_wp_error( $blog_id ) ) {
11
			return $blog_id;
12
		}
13
14
		if ( ! current_user_can( 'edit_theme_options' ) ) {
15
			return new WP_Error( 'unauthorized', 'User is not authorized to access logo settings', 403 );
16
		}
17
18
		if ( strpos( $path, '/delete' ) ) {
19
			delete_option( 'site_logo' );
20
			return array();
21
		}
22
23
		$args = $this->input();
24
		$logo_settings = $this->get_current_settings();
25
		if ( empty( $args ) || ! is_array( $args ) ) {
26
			return $logo_settings;
27
		}
28
29
		if ( isset( $args['id'] ) ) {
30
			$logo_settings['id'] = intval( $args['id'], 10 );
31
		}
32
		if ( isset( $args['url'] ) ) {
33
			$logo_settings['url'] = $args['url'];
34
		}
35
		if ( isset( $args['url'] ) || isset( $args['id'] ) ) {
36
			update_option( 'site_logo', $logo_settings );
37
		}
38
39
		return $this->get_current_settings();
40
	}
41
42
	function get_current_settings() {
43
		$logo_settings = get_option( 'site_logo' );
44
		if ( ! is_array( $logo_settings ) ) {
45
			$logo_settings = array();
46
		}
47
		return $logo_settings;
48
	}
49
}
50
51