Completed
Push — fix/typo-pro-promo ( e5832e...fc698e )
by
unknown
29:20
created

....wpcom-json-api-site-settings-v1-3-endpoint.php (2 issues)

Labels
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
 * WARNING: This file is distributed verbatim in Jetpack.
4
 * There should be nothing WordPress.com specific in this file.
5
 *
6
 * @hide-in-jetpack
7
 * @autounit api-v1 site-settings
8
 */
9
10
new WPCOM_JSON_API_Site_Settings_V1_3_Endpoint( array(
11
	'description' => 'Get detailed settings information about a site.',
12
	'group'       => '__do_not_document',
13
	'stat'        => 'sites:X',
14
	'min_version' => '1.3',
15
	'method'      => 'GET',
16
	'path'        => '/sites/%s/settings',
17
	'path_labels' => array(
18
		'$site' => '(int|string) Site ID or domain',
19
	),
20
21
	'query_parameters' => array(
22
		'context' => false,
23
	),
24
25
	'response_format' => WPCOM_JSON_API_Site_Settings_Endpoint::$site_format,
26
27
	'example_request' => 'https://public-api.wordpress.com/rest/v1.3/sites/en.blog.wordpress.com/settings?pretty=1',
28
) );
29
30
new WPCOM_JSON_API_Site_Settings_V1_3_Endpoint( array(
31
	'description' => 'Update settings for a site.',
32
	'group'       => '__do_not_document',
33
	'stat'        => 'sites:X',
34
	'min_version' => '1.3',
35
	'method'      => 'POST',
36
	'path'        => '/sites/%s/settings',
37
	'path_labels' => array(
38
		'$site' => '(int|string) Site ID or domain',
39
	),
40
41
	'request_format'  => array(
42
		'blogname'                             => '(string) Blog name',
43
		'blogdescription'                      => '(string) Blog description',
44
		'default_pingback_flag'                => '(bool) Notify blogs linked from article?',
45
		'default_ping_status'                  => '(bool) Allow link notifications from other blogs?',
46
		'default_comment_status'               => '(bool) Allow comments on new articles?',
47
		'blog_public'                          => '(string) Site visibility; -1: private, 0: discourage search engines, 1: allow search engines',
48
		'jetpack_sync_non_public_post_stati'   => '(bool) allow sync of post and pages with non-public posts stati',
49
		'jetpack_relatedposts_enabled'         => '(bool) Enable related posts?',
50
		'jetpack_relatedposts_show_headline'   => '(bool) Show headline in related posts?',
51
		'jetpack_relatedposts_show_thumbnails' => '(bool) Show thumbnails in related posts?',
52
		'jetpack_protect_whitelist'            => '(array) List of IP addresses to whitelist',
53
		'infinite_scroll'                      => '(bool) Support infinite scroll of posts?',
54
		'default_category'                     => '(int) Default post category',
55
		'default_post_format'                  => '(string) Default post format',
56
		'require_name_email'                   => '(bool) Require comment authors to fill out name and email?',
57
		'comment_registration'                 => '(bool) Require users to be registered and logged in to comment?',
58
		'close_comments_for_old_posts'         => '(bool) Automatically close comments on old posts?',
59
		'close_comments_days_old'              => '(int) Age at which to close comments',
60
		'thread_comments'                      => '(bool) Enable threaded comments?',
61
		'thread_comments_depth'                => '(int) Depth to thread comments',
62
		'page_comments'                        => '(bool) Break comments into pages?',
63
		'comments_per_page'                    => '(int) Number of comments to display per page',
64
		'default_comments_page'                => '(string) newest|oldest Which page of comments to display first',
65
		'comment_order'                        => '(string) asc|desc Order to display comments within page',
66
		'comments_notify'                      => '(bool) Email me when someone comments?',
67
		'moderation_notify'                    => '(bool) Email me when a comment is helf for moderation?',
68
		'social_notifications_like'            => '(bool) Email me when someone likes my post?',
69
		'social_notifications_reblog'          => '(bool) Email me when someone reblogs my post?',
70
		'social_notifications_subscribe'       => '(bool) Email me when someone follows my blog?',
71
		'comment_moderation'                   => '(bool) Moderate comments for manual approval?',
72
		'comment_whitelist'                    => '(bool) Moderate comments unless author has a previously-approved comment?',
73
		'comment_max_links'                    => '(int) Moderate comments that contain X or more links',
74
		'moderation_keys'                      => '(string) Words or phrases that trigger comment moderation, one per line',
75
		'blacklist_keys'                       => '(string) Words or phrases that mark comment spam, one per line',
76
		'lang_id'                              => '(int) ID for language blog is written in',
77
		'locale'                               => '(string) locale code for language blog is written in',
78
		'wga'                                  => '(array) Google Analytics Settings',
79
		'disabled_likes'                       => '(bool) Are likes globally disabled (they can still be turned on per post)?',
80
		'disabled_reblogs'                     => '(bool) Are reblogs disabled on posts?',
81
		'jetpack_comment_likes_enabled'        => '(bool) Are comment likes enabled for all comments?',
82
		'sharing_button_style'                 => '(string) Style to use for sharing buttons (icon-text, icon, text, or official)',
83
		'sharing_label'                        => '(string) Label to use for sharing buttons, e.g. "Share this:"',
84
		'sharing_show'                         => '(string|array:string) Post type or array of types where sharing buttons are to be displayed',
85
		'sharing_open_links'                   => '(string) Link target for sharing buttons (same or new)',
86
		'twitter_via'                          => '(string) Twitter username to include in tweets when people share using the Twitter button',
87
		'jetpack-twitter-cards-site-tag'       => '(string) The Twitter username of the owner of the site\'s domain.',
88
		'eventbrite_api_token'                 => '(int) The Keyring token ID for an Eventbrite token to associate with the site',
89
		'timezone_string'                      => '(string) PHP-compatible timezone string like \'UTC-5\'',
90
		'gmt_offset'                           => '(int) Site offset from UTC in hours',
91
		'date_format'                          => '(string) PHP Date-compatible date format',
92
		'time_format'                          => '(string) PHP Date-compatible time format',
93
		'start_of_week'                        => '(int) Starting day of week (0 = Sunday, 6 = Saturday)',
94
		'jetpack_testimonial'                  => '(bool) Whether testimonial custom post type is enabled for the site',
95
		'jetpack_testimonial_posts_per_page'   => '(int) Number of testimonials to show per page',
96
		'jetpack_portfolio'                    => '(bool) Whether portfolio custom post type is enabled for the site',
97
		'jetpack_portfolio_posts_per_page'     => '(int) Number of portfolio projects to show per page',
98
		Jetpack_SEO_Utils::FRONT_PAGE_META_OPTION => '(string) The SEO meta description for the site.',
99
		Jetpack_SEO_Titles::TITLE_FORMATS_OPTION => '(array) SEO meta title formats. Allowed keys: front_page, posts, pages, groups, archives',
100
		'verification_services_codes'          => '(array) Website verification codes. Allowed keys: google, pinterest, bing, yandex',
101
		'amp_is_enabled'                       => '(bool) Whether AMP is enabled for this site',
102
		'podcasting_archive'                   => '(string) The post category, if any, used for publishing podcasts',
103
		'site_icon'                            => '(int) Media attachment ID to use as site icon. Set to zero or an otherwise empty value to clear',
104
		'api_cache'                            => '(bool) Turn on/off the Jetpack JSON API cache',
105
		'posts_per_page'                       => '(int) Number of posts to show on blog pages',
106
		'posts_per_rss'                        => '(int) Number of posts to show in the RSS feed',
107
		'rss_use_excerpt'                      => '(bool) Whether the RSS feed will use post excerpts',
108
	),
109
110
	'response_format' => array(
111
		'updated' => '(array)'
112
	),
113
114
	'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/settings?pretty=1',
115
) );
116
117
class WPCOM_JSON_API_Site_Settings_V1_3_Endpoint extends WPCOM_JSON_API_Site_Settings_V1_2_Endpoint {
118
	public static $wga_defaults = array(
119
		'code'                 => '',
120
		'anonymize_ip'         => false,
121
		'ec_track_purchases'   => false,
122
		'ec_track_add_to_cart' => false
123
	);
124
125
	function callback( $path = '', $blog_id = 0 ) {
126
		add_filter( 'site_settings_endpoint_get', array( $this, 'filter_site_settings_endpoint_get' ) );
127
		add_filter( 'site_settings_update_wga', array( $this, 'filter_update_google_analytics' ), 10, 2 );
128
		return parent::callback( $path, $blog_id );
129
	}
130
131
	/**
132
	 * Filter the parent's response to include the fields
133
	 * added to 1.3 (and their defaults)
134
	 */
135
	public function filter_site_settings_endpoint_get( $settings ) {
136
		$option_name = defined( 'IS_WPCOM' ) && IS_WPCOM ? 'wga' : 'jetpack_wga';
137
		$option = get_option( $option_name, array() );
138
		$settings[ 'wga' ] = wp_parse_args( $option, $this->get_defaults() );
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class WPCOM_JSON_API_Site_Settings_V1_3_Endpoint as the method get_defaults() does only exist in the following sub-classes of WPCOM_JSON_API_Site_Settings_V1_3_Endpoint: WPCOM_JSON_API_Site_Settings_V1_4_Endpoint. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
139
		return $settings;
140
	}
141
142
	/**
143
	 * Filter the parent's response to consume our new fields
144
	 */
145
	public function filter_update_google_analytics( $wga, $new_values ) {
146
		$wga_keys = array_keys( $this->get_defaults() );
0 ignored issues
show
It seems like you code against a specific sub-type and not the parent class WPCOM_JSON_API_Site_Settings_V1_3_Endpoint as the method get_defaults() does only exist in the following sub-classes of WPCOM_JSON_API_Site_Settings_V1_3_Endpoint: WPCOM_JSON_API_Site_Settings_V1_4_Endpoint. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
147
		foreach ( $wga_keys as $wga_key ) {
148
			// Skip code since the parent class has handled it
149
			if ( 'code' === $wga_key ) {
150
				continue;
151
			}
152
			// All our new keys are booleans, so let's coerce each key's value
153
			// before updating the value in settings
154
			if ( array_key_exists( $wga_key, $new_values ) ) {
155
				$wga[ $wga_key ] = WPCOM_JSON_API::is_truthy( $new_values[ $wga_key ] );
156
			}
157
		}
158
		return $wga;
159
	}
160
}
161