Completed
Push — update/comments-ignore-author-... ( 4f8a13...0fe3a8 )
by
unknown
11:38
created

WPCOM_JSON_API_GET_Site_Endpoint   D

Complexity

Total Complexity 87

Size/Duplication

Total Lines 436
Duplicated Lines 3.21 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 87
lcom 1
cbo 4
dl 14
loc 436
rs 4.8717

3 Methods

Rating   Name   Duplication   Size   Complexity  
F build_current_site_response() 14 379 81
A force_http() 0 3 1
B callback() 0 22 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WPCOM_JSON_API_GET_Site_Endpoint often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use WPCOM_JSON_API_GET_Site_Endpoint, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
class WPCOM_JSON_API_GET_Site_Endpoint extends WPCOM_JSON_API_Endpoint {
4
5
	public static $site_format = array(
6
 		'ID'                => '(int) Site ID',
7
 		'name'              => '(string) Title of site',
8
 		'description'       => '(string) Tagline or description of site',
9
 		'URL'               => '(string) Full URL to the site',
10
 		'jetpack'           => '(bool)  Whether the site is a Jetpack site or not',
11
 		'post_count'        => '(int) The number of posts the site has',
12
		'subscribers_count' => '(int) The number of subscribers the site has',
13
		'lang'              => '(string) Primary language code of the site',
14
		'icon'              => '(array) An array of icon formats for the site',
15
		'logo'              => '(array) The site logo, set in the Customizer',
16
		'visible'           => '(bool) If this site is visible in the user\'s site list',
17
		'is_private'        => '(bool) If the site is a private site or not',
18
		'is_following'      => '(bool) If the current user is subscribed to this site in the reader',
19
		'options'           => '(array) An array of options/settings for the blog. Only viewable by users with post editing rights to the site. Note: Post formats is deprecated, please see /sites/$id/post-formats/',
20
		'updates'           => '(array) An array of available updates for plugins, themes, wordpress, and languages.',
21
		'meta'              => '(object) Meta data',
22
	);
23
24
	// /sites/mine
25
	// /sites/%s -> $blog_id
26
	function callback( $path = '', $blog_id = 0 ) {
27
		global $wpdb;
28
		if ( 'mine' === $blog_id ) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of 'mine' (string) and $blog_id (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
29
			$api = WPCOM_JSON_API::init();
30
			if ( !$api->token_details || empty( $api->token_details['blog_id'] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $api->token_details of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31
				return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current blog.', 403 );
32
			}
33
			$blog_id = $api->token_details['blog_id'];
34
		}
35
36
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
37
		if ( is_wp_error( $blog_id ) ) {
38
			return $blog_id;
39
		}
40
41
		$response = $this->build_current_site_response();
42
43
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
44
		do_action( 'wpcom_json_api_objects', 'sites' );
45
46
		return $response;
47
	}
48
49
	/**
50
	 * Collects the necessary information to return for a site's response.
51
	 *
52
	 * @return (array)
53
	 */
54
	public function build_current_site_response( ) {
55
56
		global $wpdb, $wp_version;
57
58
		$response_format = self::$site_format;
59
60
		$is_user_logged_in = is_user_logged_in();
61
62
		$visible = array();
63
64
		if ( $is_user_logged_in ) {
65
			$current_user = wp_get_current_user();
66
			$visible = get_user_meta( $current_user->ID, 'blog_visibility', true );
67
68
			if ( !is_array( $visible ) )
69
				$visible = array();
70
71
		}
72
73
		$blog_id = (int) $this->api->get_blog_id_for_output();
74
75
		/** This filter is documented in class.json-api-endpoints.php */
76
		$is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id );
77
		$site_url = get_option( 'siteurl' );
78
79 View Code Duplication
		if ( $is_jetpack ) {
80
			remove_filter( 'option_stylesheet', 'fix_theme_location' );
81
			if ( 'https' !== parse_url( $site_url, PHP_URL_SCHEME ) ) {
82
				add_filter( 'set_url_scheme', array( $this, 'force_http' ), 10, 3 );
83
			}
84
		}
85
		foreach ( array_keys( $response_format ) as $key ) {
86
			switch ( $key ) {
87
			case 'ID' :
88
				$response[$key] = $blog_id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
89
				break;
90
			case 'name' :
91
				$response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
0 ignored issues
show
Bug introduced by
The variable $response does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
92
				break;
93
			case 'description' :
94
				$response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES );
95
				break;
96
			case 'URL' :
97
				$response[$key] = (string) home_url();
98
				break;
99
			case 'jetpack' :
100
				$response[$key] = $is_jetpack; // jetpack magic affects this value
101
				break;
102
			case 'is_private' :
103
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
104
					$public_setting = get_option( 'blog_public' );
105
					if ( -1 == $public_setting )
106
						$response[$key] = true;
107
					else
108
						$response[$key] = false;
109
				} else {
110
					$response[$key] = false; // magic
111
				}
112
				break;
113
			case 'visible' :
114
				if ( $is_user_logged_in ){
115
					$is_visible = true;
116
					if ( isset( $visible[$blog_id] ) ) {
117
						$is_visible = (bool) $visible[$blog_id];
118
					}
119
					// null and true are visible
120
					$response[$key] = $is_visible;
121
				}
122
				break;
123
			case 'post_count' :
124
				if ( $is_user_logged_in )
125
					$response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
126
				break;
127
			case 'lang' :
128
				if ( $is_user_logged_in )
129
					$response[$key] = (string) get_bloginfo( 'language' );
130
				break;
131
			case 'icon' :
132
				if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
133
					$domain = blavatar_domain( home_url() );
134
					if ( blavatar_exists( $domain ) ) {
135
						$response[ $key ] = array(
136
							'img' => (string) remove_query_arg( 's', blavatar_url( $domain, 'img' ) ),
137
							'ico' => (string) remove_query_arg( 's', blavatar_url( $domain, 'ico' ) ),
138
						);
139
					} else {
140
                        // This is done so that we can access the updated blavatar on .com via the /me/sites endpoint
141
                        if( is_jetpack_site() ) {
142
143
							$site_icon_url = get_option( 'jetpack_site_icon_url' );
144
							if( $site_icon_url ) {
145
								$response[ $key ] = array(
146
									'img' => (string) jetpack_photon_url( $site_icon_url, array() , 'https' ),
147
									'ico' => (string) jetpack_photon_url( $site_icon_url, array( 'w' => 16 ), 'https' )
148
								);
149
							}
150
                        }
151
                   }
152
				} elseif ( function_exists( 'jetpack_site_icon_url' ) && function_exists( 'jetpack_photon_url' ) ) {
153
					$response[ $key ] = array(
154
						'img' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 80 ), array( 'w' => 80 ), 'https' ),
155
						'ico' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 16 ), array( 'w' => 16 ), 'https' ),
156
					);
157
				}
158
				break;
159
			case 'logo' :
160
				// Set an empty response array.
161
				$response[$key] = array(
162
					'id'  => (int) 0,
163
					'sizes' => array(),
164
					'url' => '',
165
				);
166
167
				// Get current site logo values.
168
				$logo = get_option( 'site_logo' );
169
170
				// Update the response array if there's a site logo currenty active.
171
				if ( $logo && 0 != $logo['id'] ) {
172
					$response[$key]['id']  = $logo['id'];
173
					$response[$key]['url'] = $logo['url'];
174
175
					foreach ( $logo['sizes'] as $size => $properties ) {
176
						$response[$key]['sizes'][$size] = $properties;
177
					}
178
				}
179
				break;
180
			case 'subscribers_count' :
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
181
182
				if ( function_exists( 'wpcom_subs_total_wpcom_subscribers' ) ) {
183
					$total_wpcom_subs = wpcom_subs_total_wpcom_subscribers(
184
						array(
185
							'blog_id' => $blog_id,
186
						)
187
					);
188
					$response[$key] = $total_wpcom_subs;
189
				} else {
190
					$response[$key] = 0; // magic
191
				}
192
				break;
193
			case 'is_following':
194
				$response[$key] = (bool) $this->api->is_following( $blog_id );
195
				break;
196
			case 'options':
197
				// Figure out if the blog supports VideoPress, have to do some extra checking for JP blogs
198
				$has_videopress = false;
199
				if ( get_option( 'video_upgrade' ) == '1' ) {
200
					$has_videopress = true;
201
				} else {
202
					if ( class_exists( 'Jetpack_Options' ) ) {
203
						$videopress = Jetpack_Options::get_option( 'videopress', array() );
204
						if ( isset( $videopress['blog_id'] ) && $videopress['blog_id'] > 0 ) {
205
							$has_videopress = true;
206
						}
207
					}
208
				}
209
210
				// deprecated - see separate endpoint. get a list of supported post formats
211
				$all_formats       = get_post_format_strings();
212
				$supported         = get_theme_support( 'post-formats' );
213
214
				$supported_formats = array();
215
216 View Code Duplication
				if ( isset( $supported[0] ) ) {
217
					foreach ( $supported[0] as $format ) {
218
						$supported_formats[ $format ] = $all_formats[ $format ];
219
					}
220
				}
221
222
				// determine if sharing buttons should be visible by default
223
				$default_sharing_status = false;
224
				if ( class_exists( 'Sharing_Service' ) ) {
225
					$ss                     = new Sharing_Service();
226
					$blog_services          = $ss->get_blog_services();
227
					$default_sharing_status = ! empty( $blog_services['visible'] );
228
				}
229
230
				$is_mapped_domain = false;
231
232
				if ( function_exists( 'get_primary_redirect' ) ) {
233
					$primary_redirect = strtolower( get_primary_redirect() );
234
					if ( false === strpos( $primary_redirect, '.wordpress.com' ) ) {
235
						$is_mapped_domain = true;
236
					}
237
				}
238
239
				$is_redirect = false;
240
241
				if ( function_exists( 'get_primary_domain_mapping_record' ) ) {
242
					if ( get_primary_domain_mapping_record()->type == 1 ) {
243
						$is_redirect = true;
244
					}
245
				}
246
247
				if ( function_exists( 'get_mime_types' ) ) {
248
					$allowed_file_types = get_mime_types();
249
				} else {
250
					// http://codex.wordpress.org/Uploading_Files
251
					$mime_types = get_allowed_mime_types();
252
					foreach ( $mime_types as $type => $mime_type ) {
253
						$extras = explode( '|', $type );
254
						foreach ( $extras as $extra ) {
255
							$allowed_file_types[] = $extra;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$allowed_file_types was never initialized. Although not strictly required by PHP, it is generally a good practice to add $allowed_file_types = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
256
						}
257
					}
258
				}
259
260
				if ( function_exists( 'get_blog_details' ) ) {
261
					$blog_details = get_blog_details();
262
					if ( ! empty( $blog_details->registered ) ) {
263
						$registered_date = $blog_details->registered;
264
					}
265
				}
266
267
				$upgraded_filetypes_enabled = false;
268
				if ( $is_jetpack || get_option( 'use_upgraded_upload_filetypes' ) ) {
269
					$upgraded_filetypes_enabled = true;
270
				}
271
272
				$wordads = false;
273
				if ( function_exists( 'has_any_blog_stickers' ) ) {
274
					$wordads = has_any_blog_stickers( array( 'wordads-approved', 'wordads-approved-misfits' ), $blog_id );
275
				}
276
277
				$publicize_permanently_disabled = false;
278
				if ( function_exists( 'is_publicize_permanently_disabled' ) ) {
279
					$publicize_permanently_disabled = is_publicize_permanently_disabled( $blog_id );
280
				}
281
282
				$response[$key] = array(
283
					'timezone'                => (string) get_option( 'timezone_string' ),
284
					'gmt_offset'              => (float) get_option( 'gmt_offset' ),
285
					'videopress_enabled'      => $has_videopress,
286
					'upgraded_filetypes_enabled' =>  $upgraded_filetypes_enabled,
287
					'login_url'               => wp_login_url(),
288
					'admin_url'               => get_admin_url(),
289
					'is_mapped_domain'        => $is_mapped_domain,
290
					'is_redirect'             => $is_redirect,
291
					'unmapped_url'            => get_site_url( $blog_id ),
292
					'featured_images_enabled' => current_theme_supports( 'post-thumbnails' ),
293
					'theme_slug'              => get_option( 'stylesheet' ),
294
					'header_image'            => get_theme_mod( 'header_image_data' ),
295
					'background_color'        => get_theme_mod( 'background_color' ),
296
					'image_default_link_type' => get_option( 'image_default_link_type' ),
297
					'image_thumbnail_width'   => (int)  get_option( 'thumbnail_size_w' ),
298
					'image_thumbnail_height'  => (int)  get_option( 'thumbnail_size_h' ),
299
					'image_thumbnail_crop'    => get_option( 'thumbnail_crop' ),
300
					'image_medium_width'      => (int)  get_option( 'medium_size_w' ),
301
					'image_medium_height'     => (int)  get_option( 'medium_size_h' ),
302
					'image_large_width'       => (int)  get_option( 'large_size_w' ),
303
					'image_large_height'      => (int) get_option( 'large_size_h' ),
304
					'permalink_structure'     => get_option( 'permalink_structure' ),
305
					'post_formats'            => $supported_formats,
306
					'default_post_format'     => get_option( 'default_post_format' ),
307
					'default_category'        => (int) get_option( 'default_category' ),
308
					'allowed_file_types'      => $allowed_file_types,
0 ignored issues
show
Bug introduced by
The variable $allowed_file_types does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
309
					'show_on_front'           => get_option( 'show_on_front' ),
310
					/** This filter is documented in modules/likes.php */
311
					'default_likes_enabled'   => (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ),
312
					'default_sharing_status'  => (bool) $default_sharing_status,
313
					'default_comment_status'  => ( 'closed' == get_option( 'default_comment_status' ) ? false : true ),
314
					'default_ping_status'     => ( 'closed' == get_option( 'default_ping_status' ) ? false : true ),
315
					'software_version'        => $wp_version,
316
					'created_at'              => ! empty( $registered_date ) ? $this->format_date( $registered_date ) : '0000-00-00T00:00:00+00:00',
317
					'wordads'                 => $wordads,
318
					'publicize_permanently_disabled' => $publicize_permanently_disabled,
319
				);
320
321
				if ( 'page' === get_option( 'show_on_front' ) ) {
322
					$response['options']['page_on_front'] = (int) get_option( 'page_on_front' );
323
					$response['options']['page_for_posts'] = (int) get_option( 'page_for_posts' );
324
				}
325
326
				if ( $is_jetpack ) {
327
					$response['options']['jetpack_version'] = get_option( 'jetpack_version' );
328
329
					if ( get_option( 'jetpack_main_network_site' ) ) {
330
						$response['options']['main_network_site'] = (string) rtrim( get_option( 'jetpack_main_network_site' ), '/' );
331
					}
332
333
					if ( is_array( Jetpack_Options::get_option( 'active_modules' ) ) ) {
334
						$response['options']['active_modules'] = (array) array_values( Jetpack_Options::get_option( 'active_modules' ) );
335
					}
336
337
					if ( $jetpack_wp_version = get_option( 'jetpack_wp_version' ) ) {
338
						$response['options']['software_version'] = (string) $jetpack_wp_version;
339
					} else if ( $jetpack_update = get_option( 'jetpack_updates' ) ) {
340
						if ( is_array( $jetpack_update ) && isset( $jetpack_update['wp_version'] ) ) {
341
							$response['options']['software_version'] = (string) $jetpack_update['wp_version'];
342
						} else {
343
							$response[ 'options' ][ 'software_version' ] = null;
344
						}
345
					} else {
346
						$response['options']['software_version'] = null;
347
					}
348
349
					$response['options']['max_upload_size'] = get_option( 'jetpack_max_upload_size', false );
350
351
					// Sites have to prove that they are not main_network site.
352
					// If the sync happends right then we should be able to see that we are not dealing with a network site
353
					$response['options']['is_multi_network'] = (bool) get_option( 'jetpack_is_main_network', true  );
354
					$response['options']['is_multi_site'] = (bool) get_option( 'jetpack_is_multi_site', true );
355
356
					$file_mod_denied_reason = array();
357
					$file_mod_denied_reason['automatic_updater_disabled'] = (bool) get_option( 'jetpack_constant_AUTOMATIC_UPDATER_DISABLED' );
358
359
					// WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
360
					$file_mod_denied_reason['wp_auto_update_core_disabled'] =  ! ( (bool) get_option( 'jetpack_constant_WP_AUTO_UPDATE_CORE', 'minor' ) );
361
					$file_mod_denied_reason['is_version_controlled'] = (bool) get_option( 'jetpack_is_version_controlled' );
362
363
					// By default we assume that site does have system write access if the value is not set yet.
364
					$file_mod_denied_reason['has_no_file_system_write_access'] = ! (bool)( get_option( 'jetpack_has_file_system_write_access', true ) );
365
366
					$file_mod_denied_reason['disallow_file_mods'] = (bool) get_option( 'jetpack_constant_DISALLOW_FILE_MODS' );
367
368
					$file_mod_disabled_reasons = array();
369
					foreach( $file_mod_denied_reason as $reason => $set ) {
370
						if ( $set ) {
371
							$file_mod_disabled_reasons[] = $reason;
372
						}
373
					}
374
					$response['options']['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
375
				}
376
377
				if ( ! current_user_can( 'edit_posts' ) )
378
					unset( $response[$key] );
379
				break;
380
			case 'meta' :
381
				/**
382
				 * Filters the URL scheme used when querying your site's REST API endpoint.
383
				 *
384
				 * @module json-api
385
				 *
386
				 * @since 3.2.0
387
				 *
388
				 * @param string parse_url( get_option( 'home' ), PHP_URL_SCHEME ) URL scheme parsed from home URL.
389
				 */
390
				$xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
391
				$xmlrpc_url = site_url( 'xmlrpc.php', $xmlrpc_scheme );
392
				$response[$key] = (object) array(
393
					'links' => (object) array(
394
						'self'     => (string) $this->get_site_link( $blog_id ),
395
						'help'     => (string) $this->get_site_link( $blog_id, 'help'      ),
396
						'posts'    => (string) $this->get_site_link( $blog_id, 'posts/'    ),
397
						'comments' => (string) $this->get_site_link( $blog_id, 'comments/' ),
398
						'xmlrpc'   => (string) $xmlrpc_url,
399
					),
400
				);
401
				break;
402
			}
403
		}
404
405
		if ( $is_jetpack ) {
406
407
			// Add the updates only make them visible if the user has manage options permission.
408
			$jetpack_update = (array) get_option( 'jetpack_updates' );
409
			if ( ! empty( $jetpack_update ) && current_user_can( 'manage_options' ) ) {
410
411
				if ( isset( $jetpack_update['wp_version'] ) ) {
412
					// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
413
					unset( $jetpack_update['wp_version'] );
414
				}
415
416
				if ( isset( $jetpack_update['site_is_version_controlled'] ) ) {
417
					// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
418
					unset( $jetpack_update['site_is_version_controlled'] );
419
				}
420
421
				$response['updates'] = (array) $jetpack_update;
422
			}
423
424
			add_filter( 'option_stylesheet', 'fix_theme_location' );
425 View Code Duplication
			if ( 'https' !== parse_url( $site_url, PHP_URL_SCHEME ) ) {
426
				remove_filter( 'set_url_scheme', array( $this, 'force_http' ), 10, 3 );
427
			}
428
		}
429
430
		return $response;
431
432
	}
433
434
	function force_http( $url, $scheme, $orig_scheme ) {
0 ignored issues
show
Unused Code introduced by
The parameter $scheme is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $orig_scheme is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
435
		return preg_replace('/^https:\/\//', 'http://', $url, 1 );
436
	}
437
438
}
439
440
class WPCOM_JSON_API_List_Post_Formats_Endpoint extends WPCOM_JSON_API_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
441
	// /sites/%s/post-formats -> $blog_id
442
	function callback( $path = '', $blog_id = 0 ) {
443
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
444
		if ( is_wp_error( $blog_id ) ) {
445
			return $blog_id;
446
		}
447
448
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
449
			$this->load_theme_functions();
450
		}
451
452
		// Get a list of supported post formats.
453
		$all_formats = get_post_format_strings();
454
		$supported   = get_theme_support( 'post-formats' );
455
456
		$supported_formats = $response['formats'] = array();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
457
458 View Code Duplication
		if ( isset( $supported[0] ) ) {
459
			foreach ( $supported[0] as $format ) {
460
				$supported_formats[ $format ] = $all_formats[ $format ];
461
			}
462
		}
463
464
		$response['formats'] = (object) $supported_formats;
465
466
		return $response;
467
	}
468
}
469
470
class WPCOM_JSON_API_List_Page_Templates_Endpoint extends WPCOM_JSON_API_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
471
	// /sites/%s/page-templates -> $blog_id
472
	function callback( $path = '', $blog_id = 0 ) {
473
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
474
		if ( is_wp_error( $blog_id ) ) {
475
			return $blog_id;
476
		}
477
478
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
479
			$this->load_theme_functions();
480
		}
481
482
		$response = array();
483
		$page_templates = array();
484
485
		$templates = get_page_templates();
486
		ksort( $templates );
487
488
		foreach ( array_keys( $templates ) as $label ) {
489
			$page_templates[] = array(
490
				'label' => $label,
491
				'file'  => $templates[ $label ]
492
			);
493
		}
494
495
		$response['templates'] = $page_templates;
496
497
		return $response;
498
	}
499
}
500
501
class WPCOM_JSON_API_List_Post_Types_Endpoint extends WPCOM_JSON_API_Endpoint {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
502
	static $post_type_keys_to_include = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $post_type_keys_to_include.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
503
		'name'         => 'name',
504
		'label'        => 'label',
505
		'labels'       => 'labels',
506
		'description'  => 'description',
507
		'map_meta_cap' => 'map_meta_cap',
508
		'cap'          => 'capabilities',
509
	);
510
511
	// /sites/%s/post-types -> $blog_id
512
	function callback( $path = '', $blog_id = 0 ) {
513
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
514
		if ( is_wp_error( $blog_id ) ) {
515
			return $blog_id;
516
		}
517
518
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
519
			$this->load_theme_functions();
520
		}
521
522
		$args = $this->query_args();
523
		$queryable_only = isset( $args['api_queryable'] ) && $args['api_queryable'];
524
525
		// Get a list of available post types
526
		$post_types = get_post_types( array( 'public' => true ) );
527
		$formatted_post_type_objects = array();
528
529
		// Retrieve post type object for each post type
530
		foreach ( $post_types as $post_type ) {
531
			// Skip non-queryable if filtering on queryable only
532
			$is_queryable = $this->is_post_type_allowed( $post_type );
533
			if ( $queryable_only && ! $is_queryable ) {
534
				continue;
535
			}
536
537
			$post_type_object = get_post_type_object( $post_type );
538
			$formatted_post_type_object = array();
539
540
			// Include only the desired keys in the response
541
			foreach ( self::$post_type_keys_to_include as $key => $value ) {
542
				$formatted_post_type_object[ $value ] = $post_type_object->{ $key };
543
			}
544
			$formatted_post_type_object['api_queryable'] = $is_queryable;
545
			$formatted_post_type_object['supports'] = get_all_post_type_supports( $post_type );
546
			if ( $this->post_type_supports_tags( $post_type ) ) {
547
				$formatted_post_type_object['supports']['tags'] = true;
548
			}
549
			$formatted_post_type_objects[] = $formatted_post_type_object;
550
		}
551
552
		return array(
553
			'found' => count( $formatted_post_type_objects ),
554
			'post_types' => $formatted_post_type_objects
555
		);
556
	}
557
558
	function post_type_supports_tags( $post_type ) {
559
		if ( in_array( 'post_tag', get_object_taxonomies( $post_type ) ) ) {
560
			return true;
561
		}
562
563
		// the featured content module adds post_tag support
564
		// to the post types that are registered for it
565
		// however it does so in a way that isn't available
566
		// to get_object_taxonomies
567
		$featured_content = get_theme_support( 'featured-content' );
568
		if ( ! $featured_content || empty( $featured_content[0] ) || empty( $featured_content[0]['post_types'] ) ) {
569
			return false;
570
		}
571
572
		return in_array( $post_type, $featured_content[0]['post_types'] );
573
	}
574
}
575