Completed
Push — update/dialogue-focus-on-conte... ( 9f1745...fa862f )
by
unknown
80:03 queued 71:18
created

...pcom-json-api-update-site-homepage-endpoint.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
new WPCOM_JSON_API_Update_Site_Homepage_Endpoint( array (
4
	'description'      => 'Set site homepage settings',
5
	'group'            => '__do_not_document',
6
	'stat'             => 'sites:1:homepage',
7
	'method'           => 'POST',
8
	'min_version'      => '1.1',
9
	'path'             => '/sites/%s/homepage',
10
	'path_labels'      => array(
11
		'$site' => '(string) Site ID or domain.',
12
	),
13
	'request_format'  => array(
14
		'is_page_on_front' => '(bool) True if we will use a page as the homepage; false to use a blog page as the homepage.',
15
		'page_on_front_id' => '(int) Optional. The ID of the page to use as the homepage if is_page_on_front is true.',
16
		'page_for_posts_id' => '(int) Optional. The ID of the page to use as the blog page if is_page_on_front is true.',
17
	),
18
	'response_format'  => array(
19
		'is_page_on_front' => '(bool) True if we will use a page as the homepage; false to use a blog page as the homepage.',
20
		'page_on_front_id' => '(int) The ID of the page to use as the homepage if is_page_on_front is true.',
21
		'page_for_posts_id' => '(int) The ID of the page to use as the blog page if is_page_on_front is true.',
22
	),
23
	'example_request'  => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/homepage',
24
	'example_request_data' => array(
25
		'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
26
		'body' => array(
27
			'is_page_on_front' => true,
28
			'page_on_front_id' => 1,
29
			'page_for_posts_id' => 0,
30
		),
31
	),
32
	'example_response' => '{"is_page_on_front":true,"page_on_front_id":1,"page_for_posts_id":0}',
33
) );
34
35
class WPCOM_JSON_API_Update_Site_Homepage_Endpoint extends WPCOM_JSON_API_Endpoint {
36
37
	function callback( $path = '', $site_id = 0 ) {
38
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $site_id ) );
39
		if ( is_wp_error( $blog_id ) ) {
40
			return $blog_id;
41
		}
42
43
		if ( ! current_user_can( 'edit_theme_options' ) ) {
44
			return new WP_Error( 'unauthorized', 'User is not authorized to access homepage settings', 403 );
0 ignored issues
show
The call to WP_Error::__construct() has too many arguments starting with 'unauthorized'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
45
		}
46
47
		$args = $this->input();
48
		if ( empty( $args ) || ! is_array( $args ) ) {
49
			return $this->get_current_settings();
50
		}
51
52
		if ( isset( $args['is_page_on_front'] ) ) {
53
			$show_on_front = $args['is_page_on_front'] ? 'page' : 'posts';
54
			update_option( 'show_on_front', $show_on_front );
55
		}
56
		if ( isset( $args['page_on_front_id'] ) ) {
57
			update_option( 'page_on_front', $args['page_on_front_id'] );
58
		}
59
		if ( isset( $args['page_for_posts_id'] ) ) {
60
			update_option( 'page_for_posts', $args['page_for_posts_id'] );
61
		}
62
63
		return $this->get_current_settings();
64
	}
65
66
	function get_current_settings() {
67
		$is_page_on_front = ( get_option( 'show_on_front' ) === 'page' );
68
		$page_on_front_id = get_option( 'page_on_front' );
69
		$page_for_posts_id = get_option( 'page_for_posts' );
70
71
		return array(
72
			'is_page_on_front' => $is_page_on_front,
73
			'page_on_front_id' => $page_on_front_id,
74
			'page_for_posts_id' => $page_for_posts_id,
75
		);
76
	}
77
}
78