Completed
Push — master-stable ( 3751a6...e73511 )
by
unknown
40:58 queued 32:02
created

WPCOM_JSON_API_GET_Site_Endpoint::callback()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 8.6737
cc 5
eloc 13
nc 5
nop 2
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
		'jetpack_modules'   => '(array) A list of active Jetpack modules.',
22
		'meta'              => '(object) Meta data',
23
	);
24
25
	// /sites/mine
26
	// /sites/%s -> $blog_id
27
	function callback( $path = '', $blog_id = 0 ) {
28
		global $wpdb;
29
		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...
30
			$api = WPCOM_JSON_API::init();
31
			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...
32
				return new WP_Error( 'authorization_required', 'An active access token must be used to query information about the current blog.', 403 );
33
			}
34
			$blog_id = $api->token_details['blog_id'];
35
		}
36
37
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
38
		if ( is_wp_error( $blog_id ) ) {
39
			return $blog_id;
40
		}
41
42
		$response = $this->build_current_site_response();
43
44
		/** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */
45
		do_action( 'wpcom_json_api_objects', 'sites' );
46
47
		return $response;
48
	}
49
50
	/**
51
	 * Collects the necessary information to return for a site's response.
52
	 *
53
	 * @return (array)
54
	 */
55
	public function build_current_site_response( ) {
56
57
		global $wpdb, $wp_version;
58
59
		// Allow update in later versions
60
		/**
61
		 * Filter the structure of information about the site to return.
62
		 *
63
		 * @module json-api
64
		 *
65
		 * @since 3.9.3
66
		 *
67
		 * @param array $site_format Data structure.
68
		 */
69
		$response_format = apply_filters( 'sites_site_format', self::$site_format );
70
71
		$is_user_logged_in = is_user_logged_in();
72
73
		$visible = array();
74
75
		if ( $is_user_logged_in ) {
76
			$current_user = wp_get_current_user();
77
			$visible = get_user_meta( $current_user->ID, 'blog_visibility', true );
78
79
			if ( !is_array( $visible ) )
80
				$visible = array();
81
82
		}
83
84
		$blog_id = (int) $this->api->get_blog_id_for_output();
85
86
		/** This filter is documented in class.json-api-endpoints.php */
87
		$is_jetpack = true === apply_filters( 'is_jetpack_site', false, $blog_id );
88
		$site_url = get_option( 'siteurl' );
89
90 View Code Duplication
		if ( $is_jetpack ) {
91
			remove_filter( 'option_stylesheet', 'fix_theme_location' );
92
			if ( 'https' !== parse_url( $site_url, PHP_URL_SCHEME ) ) {
93
				add_filter( 'set_url_scheme', array( $this, 'force_http' ), 10, 3 );
94
			}
95
		}
96
		foreach ( array_keys( $response_format ) as $key ) {
97
98
			// refactoring to change parameter to locale in 1.2
99
			if ( $lang_or_locale = $this->process_locale( $key, $is_user_logged_in ) ) {
100
				$response[$key] = $lang_or_locale;
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...
101
				continue;
102
			}
103
104
			switch ( $key ) {
105
			case 'ID' :
106
				$response[$key] = $blog_id;
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...
107
				break;
108
			case 'name' :
109
				$response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
110
				break;
111
			case 'description' :
112
				$response[$key] = (string) htmlspecialchars_decode( get_bloginfo( 'description' ), ENT_QUOTES );
113
				break;
114
			case 'URL' :
115
				$response[$key] = (string) home_url();
116
				break;
117
			case 'jetpack' :
118
				$response[$key] = $is_jetpack; // jetpack magic affects this value
119
				break;
120
			case 'is_private' :
121
				if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
122
					$public_setting = get_option( 'blog_public' );
123
					if ( -1 == $public_setting )
124
						$response[$key] = true;
125
					else
126
						$response[$key] = false;
127
				} else {
128
					$response[$key] = false; // magic
129
				}
130
				break;
131
			case 'visible' :
132
				if ( $is_user_logged_in ){
133
					$is_visible = true;
134
					if ( isset( $visible[$blog_id] ) ) {
135
						$is_visible = (bool) $visible[$blog_id];
136
					}
137
					// null and true are visible
138
					$response[$key] = $is_visible;
139
				}
140
				break;
141
			case 'post_count' :
142
				if ( $is_user_logged_in )
143
					$response[$key] = (int) $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
144
				break;
145
			case 'icon' :
146
				if ( function_exists( 'blavatar_domain' ) && function_exists( 'blavatar_exists' ) && function_exists( 'blavatar_url' ) ) {
147
					$domain = blavatar_domain( home_url() );
148
					if ( blavatar_exists( $domain ) ) {
149
						$response[ $key ] = array(
150
							'img' => (string) remove_query_arg( 's', blavatar_url( $domain, 'img' ) ),
151
							'ico' => (string) remove_query_arg( 's', blavatar_url( $domain, 'ico' ) ),
152
						);
153
					} else {
154
                        // This is done so that we can access the updated blavatar on .com via the /me/sites endpoint
155
                        if( $is_jetpack ) {
156
157
							$site_icon_url = get_option( 'jetpack_site_icon_url' );
158
							if( $site_icon_url ) {
159
								$response[ $key ] = array(
160
									'img' => (string) jetpack_photon_url( $site_icon_url, array() , 'https' ),
161
									'ico' => (string) jetpack_photon_url( $site_icon_url, array( 'w' => 16 ), 'https' )
162
								);
163
							}
164
                        }
165
                   }
166
				} elseif ( function_exists( 'jetpack_site_icon_url' ) && function_exists( 'jetpack_photon_url' ) ) {
167
					$response[ $key ] = array(
168
						'img' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 80 ), array( 'w' => 80 ), 'https' ),
169
						'ico' => (string) jetpack_photon_url( jetpack_site_icon_url( get_current_blog_id() , 16 ), array( 'w' => 16 ), 'https' ),
170
					);
171
				}
172
				break;
173
			case 'logo' :
174
				// Set an empty response array.
175
				$response[$key] = array(
176
					'id'  => (int) 0,
177
					'sizes' => array(),
178
					'url' => '',
179
				);
180
181
				// Get current site logo values.
182
				$logo = get_option( 'site_logo' );
183
184
				// Update the response array if there's a site logo currenty active.
185
				if ( $logo && 0 != $logo['id'] ) {
186
					$response[$key]['id']  = $logo['id'];
187
					$response[$key]['url'] = $logo['url'];
188
189
					foreach ( $logo['sizes'] as $size => $properties ) {
190
						$response[$key]['sizes'][$size] = $properties;
191
					}
192
				}
193
				break;
194
			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...
195
196
				if ( function_exists( 'wpcom_subs_total_wpcom_subscribers' ) ) {
197
					$total_wpcom_subs = wpcom_subs_total_wpcom_subscribers(
198
						array(
199
							'blog_id' => $blog_id,
200
						)
201
					);
202
					$response[$key] = $total_wpcom_subs;
203
				} else {
204
					$response[$key] = 0; // magic
205
				}
206
				break;
207
			case 'is_following':
208
				$response[$key] = (bool) $this->api->is_following( $blog_id );
209
				break;
210
			case 'options':
211
				// Figure out if the blog supports VideoPress, have to do some extra checking for JP blogs
212
				$has_videopress = false;
213
				if ( get_option( 'video_upgrade' ) == '1' ) {
214
					$has_videopress = true;
215
				} else {
216
					if ( class_exists( 'Jetpack_Options' ) ) {
217
						$videopress = Jetpack_Options::get_option( 'videopress', array() );
218
						if ( isset( $videopress['blog_id'] ) && $videopress['blog_id'] > 0 ) {
219
							$has_videopress = true;
220
						}
221
					}
222
				}
223
224
				// deprecated - see separate endpoint. get a list of supported post formats
225
				$all_formats       = get_post_format_strings();
226
				$supported         = get_theme_support( 'post-formats' );
227
228
				$supported_formats = array();
229
230 View Code Duplication
				if ( isset( $supported[0] ) ) {
231
					foreach ( $supported[0] as $format ) {
232
						$supported_formats[ $format ] = $all_formats[ $format ];
233
					}
234
				}
235
236
				// determine if sharing buttons should be visible by default
237
				$default_sharing_status = false;
238
				if ( class_exists( 'Sharing_Service' ) ) {
239
					$ss                     = new Sharing_Service();
240
					$blog_services          = $ss->get_blog_services();
241
					$default_sharing_status = ! empty( $blog_services['visible'] );
242
				}
243
244
				$is_mapped_domain = false;
245
246
				if ( function_exists( 'get_primary_redirect' ) ) {
247
					$primary_redirect = strtolower( get_primary_redirect() );
248
					if ( false === strpos( $primary_redirect, '.wordpress.com' ) ) {
249
						$is_mapped_domain = true;
250
					}
251
				}
252
253
				$is_redirect = false;
254
255
				if ( function_exists( 'get_primary_domain_mapping_record' ) ) {
256
					if ( get_primary_domain_mapping_record()->type == 1 ) {
257
						$is_redirect = true;
258
					}
259
				}
260
261
				if ( function_exists( 'get_mime_types' ) ) {
262
					$allowed_file_types = get_mime_types();
263
				} else {
264
					// http://codex.wordpress.org/Uploading_Files
265
					$mime_types = get_allowed_mime_types();
266
					foreach ( $mime_types as $type => $mime_type ) {
267
						$extras = explode( '|', $type );
268
						foreach ( $extras as $extra ) {
269
							$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...
270
						}
271
					}
272
				}
273
274
				if ( function_exists( 'get_blog_details' ) ) {
275
					$blog_details = get_blog_details();
276
					if ( ! empty( $blog_details->registered ) ) {
277
						$registered_date = $blog_details->registered;
278
					}
279
				}
280
281
				$upgraded_filetypes_enabled = false;
282
				if ( $is_jetpack || get_option( 'use_upgraded_upload_filetypes' ) ) {
283
					$upgraded_filetypes_enabled = true;
284
				}
285
286
				$wordads = false;
287
				if ( function_exists( 'has_any_blog_stickers' ) ) {
288
					$wordads = has_any_blog_stickers( array( 'wordads-approved', 'wordads-approved-misfits' ), $blog_id );
289
				}
290
291
				$publicize_permanently_disabled = false;
292
				if ( function_exists( 'is_publicize_permanently_disabled' ) ) {
293
					$publicize_permanently_disabled = is_publicize_permanently_disabled( $blog_id );
294
				}
295
296
				$frame_nonce = false;
297
				if ( ! $is_jetpack ) {
298
					$frame_nonce = wpcom_get_frame_nonce();
299
				}
300
301
				$response[$key] = array(
302
					'timezone'                => (string) get_option( 'timezone_string' ),
303
					'gmt_offset'              => (float) get_option( 'gmt_offset' ),
304
					'videopress_enabled'      => $has_videopress,
305
					'upgraded_filetypes_enabled' =>  $upgraded_filetypes_enabled,
306
					'login_url'               => wp_login_url(),
307
					'admin_url'               => get_admin_url(),
308
					'is_mapped_domain'        => $is_mapped_domain,
309
					'is_redirect'             => $is_redirect,
310
					'unmapped_url'            => get_site_url( $blog_id ),
311
					'featured_images_enabled' => current_theme_supports( 'post-thumbnails' ),
312
					'theme_slug'              => get_option( 'stylesheet' ),
313
					'header_image'            => get_theme_mod( 'header_image_data' ),
314
					'background_color'        => get_theme_mod( 'background_color' ),
315
					'image_default_link_type' => get_option( 'image_default_link_type' ),
316
					'image_thumbnail_width'   => (int)  get_option( 'thumbnail_size_w' ),
317
					'image_thumbnail_height'  => (int)  get_option( 'thumbnail_size_h' ),
318
					'image_thumbnail_crop'    => get_option( 'thumbnail_crop' ),
319
					'image_medium_width'      => (int)  get_option( 'medium_size_w' ),
320
					'image_medium_height'     => (int)  get_option( 'medium_size_h' ),
321
					'image_large_width'       => (int)  get_option( 'large_size_w' ),
322
					'image_large_height'      => (int) get_option( 'large_size_h' ),
323
					'permalink_structure'     => get_option( 'permalink_structure' ),
324
					'post_formats'            => $supported_formats,
325
					'default_post_format'     => get_option( 'default_post_format' ),
326
					'default_category'        => (int) get_option( 'default_category' ),
327
					'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...
328
					'show_on_front'           => get_option( 'show_on_front' ),
329
					/** This filter is documented in modules/likes.php */
330
					'default_likes_enabled'   => (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ),
331
					'default_sharing_status'  => (bool) $default_sharing_status,
332
					'default_comment_status'  => ( 'closed' == get_option( 'default_comment_status' ) ? false : true ),
333
					'default_ping_status'     => ( 'closed' == get_option( 'default_ping_status' ) ? false : true ),
334
					'software_version'        => $wp_version,
335
					'created_at'              => ! empty( $registered_date ) ? $this->format_date( $registered_date ) : '0000-00-00T00:00:00+00:00',
336
					'wordads'                 => $wordads,
337
					'publicize_permanently_disabled' => $publicize_permanently_disabled,
338
					'frame_nonce'            => $frame_nonce,
339
				);
340
341
				if ( 'page' === get_option( 'show_on_front' ) ) {
342
					$response['options']['page_on_front'] = (int) get_option( 'page_on_front' );
343
					$response['options']['page_for_posts'] = (int) get_option( 'page_for_posts' );
344
				}
345
346
				if ( $is_jetpack ) {
347
					$response['options']['jetpack_version'] = get_option( 'jetpack_version' );
348
349
					if ( get_option( 'jetpack_main_network_site' ) ) {
350
						$response['options']['main_network_site'] = (string) rtrim( get_option( 'jetpack_main_network_site' ), '/' );
351
					}
352
353
					if ( is_array( Jetpack_Options::get_option( 'active_modules' ) ) ) {
354
						$response['options']['active_modules'] = (array) array_values( Jetpack_Options::get_option( 'active_modules' ) );
355
					}
356
357
					if ( $jetpack_wp_version = get_option( 'jetpack_wp_version' ) ) {
358
						$response['options']['software_version'] = (string) $jetpack_wp_version;
359
					} else if ( $jetpack_update = get_option( 'jetpack_updates' ) ) {
360
						if ( is_array( $jetpack_update ) && isset( $jetpack_update['wp_version'] ) ) {
361
							$response['options']['software_version'] = (string) $jetpack_update['wp_version'];
362
						} else {
363
							$response[ 'options' ][ 'software_version' ] = null;
364
						}
365
					} else {
366
						$response['options']['software_version'] = null;
367
					}
368
369
					$response['options']['max_upload_size'] = get_option( 'jetpack_max_upload_size', false );
370
371
					// Sites have to prove that they are not main_network site.
372
					// If the sync happends right then we should be able to see that we are not dealing with a network site
373
					$response['options']['is_multi_network'] = (bool) get_option( 'jetpack_is_main_network', true  );
374
					$response['options']['is_multi_site'] = (bool) get_option( 'jetpack_is_multi_site', true );
375
376
					$file_mod_denied_reason = array();
377
					$file_mod_denied_reason['automatic_updater_disabled'] = (bool) get_option( 'jetpack_constant_AUTOMATIC_UPDATER_DISABLED' );
378
379
					// WP AUTO UPDATE CORE defaults to minor, '1' if true and '0' if set to false.
380
					$file_mod_denied_reason['wp_auto_update_core_disabled'] =  ! ( (bool) get_option( 'jetpack_constant_WP_AUTO_UPDATE_CORE', 'minor' ) );
381
					$file_mod_denied_reason['is_version_controlled'] = (bool) get_option( 'jetpack_is_version_controlled' );
382
383
					// By default we assume that site does have system write access if the value is not set yet.
384
					$file_mod_denied_reason['has_no_file_system_write_access'] = ! (bool)( get_option( 'jetpack_has_file_system_write_access', true ) );
385
386
					$file_mod_denied_reason['disallow_file_mods'] = (bool) get_option( 'jetpack_constant_DISALLOW_FILE_MODS' );
387
388
					$file_mod_disabled_reasons = array();
389
					foreach( $file_mod_denied_reason as $reason => $set ) {
390
						if ( $set ) {
391
							$file_mod_disabled_reasons[] = $reason;
392
						}
393
					}
394
					$response['options']['file_mod_disabled'] = empty( $file_mod_disabled_reasons ) ? false : $file_mod_disabled_reasons;
395
				}
396
397
				if ( ! current_user_can( 'edit_posts' ) )
398
					unset( $response[$key] );
399
				break;
400
			case 'jetpack_modules':
401
				if ( ! $is_jetpack || ! is_user_member_of_blog() ) {
402
					break;
403
				}
404
				$response[$key] = array_values( Jetpack_Options::get_option( 'active_modules', array() ) );
405
				break;
406
			case 'meta':
407
				/**
408
				 * Filters the URL scheme used when querying your site's REST API endpoint.
409
				 *
410
				 * @module json-api
411
				 *
412
				 * @since 3.2.0
413
				 *
414
				 * @param string parse_url( get_option( 'home' ), PHP_URL_SCHEME ) URL scheme parsed from home URL.
415
				 */
416
				$xmlrpc_scheme = apply_filters( 'wpcom_json_api_xmlrpc_scheme', parse_url( get_option( 'home' ), PHP_URL_SCHEME ) );
417
				$xmlrpc_url = site_url( 'xmlrpc.php', $xmlrpc_scheme );
418
				$response[$key] = (object) array(
419
					'links' => (object) array(
420
						'self'     => (string) $this->get_site_link( $blog_id ),
421
						'help'     => (string) $this->get_site_link( $blog_id, 'help'      ),
422
						'posts'    => (string) $this->get_site_link( $blog_id, 'posts/'    ),
423
						'comments' => (string) $this->get_site_link( $blog_id, 'comments/' ),
424
						'xmlrpc'   => (string) $xmlrpc_url,
425
					),
426
				);
427
				break;
428
			}
429
		}
430
431
		if ( $is_jetpack ) {
432
			// Add the updates only make them visible if the user has manage options permission and the site is the main site of the network
433
			if ( current_user_can( 'manage_options' ) ) {
434
				if ( isset( $response['options']['main_network_site'], $response['options']['unmapped_url'] ) ) {
435
					$main_network_site_url = set_url_scheme( $response['options']['main_network_site'], 'http' );
436
					$unmapped_url = set_url_scheme( $response['options']['unmapped_url'], 'http' );
437
					if ( $unmapped_url === $main_network_site_url ) {
438
						$jetpack_update = (array) get_option( 'jetpack_updates' );
439
						if ( ! empty( $jetpack_update ) ) {
440
							if ( isset( $jetpack_update['wp_version'] ) ) {
441
								// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the wp_version into to jetpack_updates
442
								unset( $jetpack_update['wp_version'] );
443
							}
444
							if ( isset( $jetpack_update['site_is_version_controlled'] ) ) {
445
								// In previous version of Jetpack 3.4, 3.5, 3.6 we synced the site_is_version_controlled into to jetpack_updates
446
								unset( $jetpack_update['site_is_version_controlled'] );
447
							}
448
							$response['updates'] = (array) $jetpack_update;
449
						}
450
					}
451
				}
452
			}
453
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
454
				add_filter( 'option_stylesheet', 'fix_theme_location' );
455
			}
456 View Code Duplication
			if ( 'https' !== parse_url( $site_url, PHP_URL_SCHEME ) ) {
457
				remove_filter( 'set_url_scheme', array( $this, 'force_http' ), 10, 3 );
458
			}
459
		}
460
461
		return $response;
462
463
	}
464
465 View Code Duplication
	protected function process_locale( $key, $is_user_logged_in ) {
466
		if ( $is_user_logged_in && 'lang' == $key ) {
467
			if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
468
				if ( ! is_jetpack_site() ) {
469
					return (string) get_blog_lang_code();
470
				}
471
			}
472
			return (string) get_bloginfo( 'language' );
473
		}
474
		return false;
475
	}
476
477
	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...
478
		return preg_replace('/^https:\/\//', 'http://', $url, 1 );
479
	}
480
481
}
482
483
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...
484
	// /sites/%s/post-formats -> $blog_id
485
	function callback( $path = '', $blog_id = 0 ) {
486
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
487
		if ( is_wp_error( $blog_id ) ) {
488
			return $blog_id;
489
		}
490
491
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
492
			$this->load_theme_functions();
493
		}
494
495
		// Get a list of supported post formats.
496
		$all_formats = get_post_format_strings();
497
		$supported   = get_theme_support( 'post-formats' );
498
499
		$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...
500
501 View Code Duplication
		if ( isset( $supported[0] ) ) {
502
			foreach ( $supported[0] as $format ) {
503
				$supported_formats[ $format ] = $all_formats[ $format ];
504
			}
505
		}
506
507
		$response['formats'] = (object) $supported_formats;
508
509
		return $response;
510
	}
511
}
512
513
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...
514
	// /sites/%s/page-templates -> $blog_id
515
	function callback( $path = '', $blog_id = 0 ) {
516
		$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
517
		if ( is_wp_error( $blog_id ) ) {
518
			return $blog_id;
519
		}
520
521
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
522
			$this->load_theme_functions();
523
		}
524
525
		$response = array();
526
		$page_templates = array();
527
528
		$templates = get_page_templates();
529
		ksort( $templates );
530
531
		foreach ( array_keys( $templates ) as $label ) {
532
			$page_templates[] = array(
533
				'label' => $label,
534
				'file'  => $templates[ $label ]
535
			);
536
		}
537
538
		$response['templates'] = $page_templates;
539
540
		return $response;
541
	}
542
}
543