Completed
Push — wpcom/json-endpoints-2017-01-1... ( fcbc0f...1d70aa )
by
unknown
35:14 queued 26:13
created

WPCOM_JSON_API_Update_Site_Homepage_Endpoint   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
D callback() 0 28 9
A get_current_settings() 0 11 1
1
<?php
2
3
class WPCOM_JSON_API_Update_Site_Homepage_Endpoint extends WPCOM_JSON_API_Endpoint {
4
5
	function callback( $path = '', $site_id = 0 ) {
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 homepage settings', 403 );
13
		}
14
15
		$args = $this->input();
16
		if ( empty( $args ) || ! is_array( $args ) ) {
17
			return $this->get_current_settings();
18
		}
19
20
		if ( isset( $args['is_page_on_front'] ) ) {
21
			$show_on_front = $args['is_page_on_front'] ? 'page' : 'posts';
22
			update_option( 'show_on_front', $show_on_front );
23
		}
24
		if ( isset( $args['page_on_front_id'] ) ) {
25
			update_option( 'page_on_front', $args['page_on_front_id'] );
26
		}
27
		if ( isset( $args['page_for_posts_id'] ) ) {
28
			update_option( 'page_for_posts', $args['page_for_posts_id'] );
29
		}
30
31
		return $this->get_current_settings();
32
	}
33
34
	function get_current_settings() {
35
		$is_page_on_front = ( get_option( 'show_on_front' ) === 'page' );
36
		$page_on_front_id = get_option( 'page_on_front' );
37
		$page_for_posts_id = get_option( 'page_for_posts' );
38
39
		return [
40
			'is_page_on_front' => $is_page_on_front,
41
			'page_on_front_id' => $page_on_front_id,
42
			'page_for_posts_id' => $page_for_posts_id,
43
		];
44
	}
45
}
46