Completed
Push — add/sync-rest-comments ( 1da85c )
by
unknown
09:46
created

WPCOM_JSON_API_GET_Site_Endpoint::process_locale()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

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