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

callback()   D

Complexity

Conditions 9
Paths 15

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 17
nc 15
nop 2
dl 0
loc 28
rs 4.909
c 0
b 0
f 0
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