Completed
Push — fix/site-id-in-api-links ( faf259 )
by
unknown
46:23 queued 35:17
created

SAL_Post   D

Complexity

Total Complexity 130

Size/Duplication

Total Lines 661
Duplicated Lines 35.85 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 237
loc 661
rs 4.6193
c 1
b 0
f 0
wmc 130
lcom 1
cbo 3

53 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __set() 0 3 1
A __get() 0 7 2
A __call() 0 7 2
A __isset() 0 3 1
get_like_count() 0 1 ?
is_liked() 0 1 ?
is_reblogged() 0 1 ?
is_following() 0 1 ?
get_global_id() 0 1 ?
get_geo() 0 1 ?
A get_menu_order() 0 3 1
A get_guid() 0 3 1
A get_type() 0 3 1
B get_terms() 0 21 5
A get_tags() 0 10 3
A get_categories() 5 10 3
A get_attachments_and_count() 0 8 2
B get_metadata() 0 29 6
A get_meta() 0 20 4
A get_current_user_capabilities() 0 7 1
A get_other_urls() 0 9 2
B get_publicize_urls() 16 21 7
A get_page_template() 0 3 1
A get_featured_image() 0 8 3
A get_post_thumbnail() 9 17 4
A get_format() 0 8 2
A get_revisions() 0 14 3
A get_site_link() 0 3 1
A get_post_link() 0 3 1
A get_attachment() 19 19 4
A get_date() 0 3 1
A get_modified_date() 0 3 1
A get_title() 0 7 2
A get_url() 0 7 2
A get_shortlink() 0 3 1
A get_content() 0 11 2
A get_excerpt() 0 12 2
A get_status() 0 3 1
A is_sticky() 0 3 1
A get_slug() 0 3 1
A get_password() 0 7 2
A get_parent() 0 18 3
A the_password_form() 0 3 1
A get_discussion() 0 9 1
A is_likes_enabled() 0 10 2
A is_sharing_enabled() 0 12 2
A get_the_post_content_for_display() 20 20 1
D get_author() 8 43 9
A get_avatar_url() 0 8 3
A get_permalink_suggestions() 0 5 1
C format_taxonomy() 38 38 7
F get_media_item_v1_1() 122 122 22

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 SAL_Post 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 SAL_Post, and based on these observations, apply Extract Interface, too.

1
<?php 
2
/**
3
 * This class wraps a WP_Post and proxies any undefined attributes
4
 * and methods to the wrapped class. We need to do this because at present
5
 * the WP_Post class is marked as final (in 4.5 this will change, though it's
6
 * not clear if there will be a mechanism to retrieve from the DB into the over-
7
 * ridden class dynamically).
8
 **/
9
10
require_once dirname( __FILE__ ) . '/class.json-api-metadata.php';
11
require_once dirname( __FILE__ ) . '/class.json-api-date.php';
12
require_once ( ABSPATH . "wp-includes/post.php" );
13
14
abstract class SAL_Post {
15
	public $post;
16
	public $context;
17
	public $site;
18
19
	function __construct( $site, $post, $context ) {
20
		$this->post = $post;
21
		$this->context = $context;
22
		$this->site = $site;
23
	}
24
25
	public function __set( $key, $value ) {
26
		$this->post->{ $key } = $value;
27
	}
28
29
	public function __get( $key ) {
30
		if ( $key === 'links' ) {
31
			require_once dirname( __FILE__ ) . '/class.json-api-links.php';
32
			return WPCOM_JSON_API_Links::getInstance();
33
		}
34
		return $this->post->{ $key };
35
	}
36
37
	public function __call( $name, $arguments ) {
38
		if ( is_callable( array( $this->post, $name ) ) ) {
39
			return call_user_func_array( array( $this->post, $name ), $arguments );
40
		} else {
41
			trigger_error("Call to undefined method '{$name}'");
42
		}
43
	}
44
45
	public function __isset ( $name ) {
46
		return isset( $this->post->{ $name } );
47
	}
48
49
	abstract public function get_like_count();
50
	abstract public function is_liked();
51
	abstract public function is_reblogged();
52
	abstract public function is_following();
53
	abstract public function get_global_id();
54
	abstract public function get_geo();
55
	
56
	public function get_menu_order() {
57
		return (int) $this->post->menu_order;
58
	}
59
60
	public function get_guid() {
61
		return (string) $this->post->guid;
62
	}
63
64
	public function get_type() {
65
		return (string) $this->post->post_type;
66
	}
67
68
	public function get_terms() {
69
		$taxonomies = get_object_taxonomies( $this->post, 'objects' );
70
		$terms = array();
71
		foreach ( $taxonomies as $taxonomy ) {
72
			if ( ! $taxonomy->public && ! current_user_can( $taxonomy->cap->assign_terms ) ) {
73
				continue;
74
			}
75
76
			$terms[ $taxonomy->name ] = array();
77
78
			$taxonomy_terms = wp_get_object_terms( $this->post->ID, $taxonomy->name, array( 'fields' => 'all' ) );
79
			foreach ( $taxonomy_terms as $term ) {
80
				$formatted_term = $this->format_taxonomy( $term, $taxonomy->name, 'display' );
81
				$terms[ $taxonomy->name ][ $term->name ] = $formatted_term;
82
			}
83
84
			$terms[ $taxonomy->name ] = (object) $terms[ $taxonomy->name ];
85
		}
86
87
		return (object) $terms;
88
	}
89
90
	public function get_tags() {
91
		$tags = array();
92
		$terms = wp_get_post_tags( $this->post->ID );
93
		foreach ( $terms as $term ) {
94
			if ( !empty( $term->name ) ) {
95
				$tags[$term->name] = $this->format_taxonomy( $term, 'post_tag', 'display' );
96
			}
97
		}
98
		return (object) $tags;
99
	}
100
101
	public function get_categories() {
102
		$categories = array();
103
		$terms = wp_get_object_terms( $this->post->ID, 'category', array( 'fields' => 'all' ) );
104 View Code Duplication
		foreach ( $terms as $term ) {
105
			if ( !empty( $term->name ) ) {
106
				$categories[$term->name] = $this->format_taxonomy( $term, 'category', 'display' );
107
			}
108
		}
109
		return (object) $categories;
110
	}
111
112
	public function get_attachments_and_count() {
113
		$attachments = array();
114
		$_attachments = new WP_Query( array( 'post_parent' => $this->post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => '20' ) );
115
		foreach ( $_attachments->posts as $attachment ) {
116
			$attachments[$attachment->ID] = $this->get_media_item_v1_1( $attachment->ID );
117
		}
118
		return array( (object) $attachments, (int) $_attachments->found_posts );
119
	}
120
121
	public function get_metadata() {
122
		$metadata = array();
123
		foreach ( (array) has_meta( $this->post->ID ) as $meta ) {
124
			// Don't expose protected fields.
125
			$meta_key = $meta['meta_key'];
126
			
127
			$show = !( WPCOM_JSON_API_Metadata::is_internal_only( $meta_key ) )
128
				&&
129
					( 
130
						WPCOM_JSON_API_Metadata::is_public( $meta_key ) 
131
					|| 
132
						current_user_can( 'edit_post_meta', $this->post->ID , $meta_key )
133
					);
134
			
135
			if ( $show ) {
136
				$metadata[] = array(
137
					'id'    => $meta['meta_id'],
138
					'key'   => $meta['meta_key'],
139
					'value' => maybe_unserialize( $meta['meta_value'] ),
140
				);
141
			}
142
		}
143
144
		if ( ! empty( $metadata ) ) {
145
			return $metadata;
146
		} else {
147
			return false;
148
		}
149
	}
150
151
	public function get_meta() {
152
		$meta = (object) array(
153
			'links' => (object) array(
154
				'self'    => (string) $this->get_post_link(),
155
				'help'    => (string) $this->get_post_link( 'help' ),
156
				'site'    => (string) $this->get_site_link(),
157
				'replies' => (string) $this->get_post_link( 'replies/' ),
158
				'likes'   => (string) $this->get_post_link( 'likes/' ),
159
			),
160
		);
161
162
		// add autosave link if a more recent autosave exists
163
		if ( 'edit' === $this->context ) {
164
			$autosave = wp_get_post_autosave( $this->post->ID );
165
			if ( $autosave && $autosave->post_modified > $this->post->post_modified )
166
				$meta->links->autosave = (string) $this->get_post_link() . '/autosave';
167
		}
168
169
		return $meta;
170
	}
171
172
	public function get_current_user_capabilities() {
173
		return array(
174
			'publish_post' => current_user_can( 'publish_post', $this->post ),
175
			'delete_post'  => current_user_can( 'delete_post', $this->post ),
176
			'edit_post'    => current_user_can( 'edit_post', $this->post )
177
		);
178
	}
179
180
	public function get_revisions() {
181
		if ( 'edit' !== $this->context ) {
182
			return false;
183
		}
184
185
		$revisions = array();
186
		$post_revisions = wp_get_post_revisions( $this->post->ID );
187
188
		foreach ( $post_revisions as $_post ) {
189
			$revisions[] = $_post->ID;
190
		}
191
192
		return $revisions;
193
	}
194
195
	public function get_other_urls() {
196
		$other_urls = array();
197
198
		if ( 'publish' !== $this->post->post_status ) {
199
			$other_urls = $this->get_permalink_suggestions( $this->post->post_title );
200
		}
201
202
		return (object) $other_urls;
203
	}
204
205
	protected function get_site_link() {
206
		return $this->links->get_site_link( $this->site->get_id() );
0 ignored issues
show
Documentation introduced by
The property links does not exist on object<SAL_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
207
	}
208
209
	protected function get_post_link( $path = null ) {
210
		return $this->links->get_post_link( $this->site->get_id(), $this->post->ID, $path );
0 ignored issues
show
Documentation introduced by
The property links does not exist on object<SAL_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
211
	}
212
213
	public function get_publicize_urls() {
214
		$publicize_URLs = array();
215
		$publicize      = get_post_meta( $this->post->ID, 'publicize_results', true );
216 View Code Duplication
		if ( $publicize ) {
217
			foreach ( $publicize as $service => $data ) {
218
				switch ( $service ) {
219
				case 'twitter' :
220
					foreach ( $data as $datum ) {
221
						$publicize_URLs[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
222
					}
223
					break;
224
				case 'fb' :
225
					foreach ( $data as $datum ) {
226
						$publicize_URLs[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
227
					}
228
					break;
229
				}
230
			}
231
		}
232
		return (array) $publicize_URLs;
233
	}
234
235
	public function get_page_template() {
236
		return (string) get_post_meta( $this->post->ID, '_wp_page_template', true );
237
	}
238
239
	// note this is overridden in jetpack-shadow
240
	public function get_featured_image() {
241
		$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $this->post->ID ), 'full' );
242
		if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
243
			return (string) $image_attributes[0];
244
		} else {
245
			return '';
246
		}
247
	}
248
249
	public function get_post_thumbnail() {
250
		$thumb = null;
251
252
		$thumb_id = get_post_thumbnail_id( $this->post->ID );
253
254 View Code Duplication
		if ( ! empty( $thumb_id ) ) {
255
			$attachment = get_post( $thumb_id );
256
			if ( ! empty( $attachment ) )
257
				$featured_image_object = $this->get_attachment( $attachment );
258
259
			if ( ! empty( $featured_image_object ) ) {
260
				$thumb = (object) $featured_image_object;
261
			}
262
		}
263
264
		return $thumb;
265
	}
266
267
	public function get_format() {
268
		$format = (string) get_post_format( $this->post->ID );
269
		if ( !$format ) {
270
			$format = 'standard';
271
		}
272
273
		return $format;
274
	}
275
276 View Code Duplication
	private function get_attachment( $attachment ) {
277
		$metadata = wp_get_attachment_metadata( $attachment->ID );
278
279
		$result = array(
280
			'ID'		=> (int) $attachment->ID,
281
			'URL'           => (string) wp_get_attachment_url( $attachment->ID ),
282
			'guid'		=> (string) $attachment->guid,
283
			'mime_type'	=> (string) $attachment->post_mime_type,
284
			'width'		=> (int) isset( $metadata['width']  ) ? $metadata['width']  : 0,
285
			'height'	=> (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
286
		);
287
288
		if ( isset( $metadata['duration'] ) ) {
289
			$result['duration'] = (int) $metadata['duration'];
290
		}
291
292
		/** This filter is documented in class.jetpack-sync.php */
293
		return (object) apply_filters( 'get_attachment', $result );
294
	}
295
296
	public function get_date() {
297
		return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_date_gmt, $this->post->post_date );
298
	}
299
300
	public function get_modified_date() {
301
		return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_modified_gmt, $this->post->post_modified );
302
	}
303
304
	public function get_title() {
305
		if ( 'display' === $this->context ) {
306
			return (string) get_the_title( $this->post->ID );
307
		} else {
308
			return (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
309
		}
310
	}
311
312
	public function get_url() {
313
		if ( 'revision' === $this->post->post_type ) {
314
			return (string) esc_url_raw( get_permalink( $this->post->post_parent ) );
315
		} else {
316
			return (string) esc_url_raw( get_permalink( $this->post->ID ) );
317
		}
318
	}
319
320
	public function get_shortlink() {
321
		return (string) esc_url_raw( wp_get_shortlink( $this->post->ID ) );
322
	}
323
324
	public function get_content() {
325
		if ( 'display' === $this->context ) {
326
			// TODO: move this WPCOM-specific hack
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
327
			add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
328
			$content = (string) $this->get_the_post_content_for_display();
329
			remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
330
			return $content;
331
		} else {
332
			return (string) $this->post->post_content;
333
		}
334
	}
335
336
	public function get_excerpt() {
337
		if ( 'display' === $this->context ) {
338
			add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
339
			ob_start();
340
			the_excerpt();
341
			$response = (string) ob_get_clean();
342
			remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
343
		} else {
344
			$response = htmlspecialchars_decode( (string) $this->post->post_excerpt, ENT_QUOTES );
345
		}
346
		return $response;
347
	}
348
349
	public function get_status() {
350
		return (string) get_post_status( $this->post->ID );
351
	}
352
353
	public function is_sticky() {
354
		return (bool) is_sticky( $this->post->ID );
355
	}
356
357
	public function get_slug() {
358
		return (string) $this->post->post_name;
359
	}
360
361
	public function get_password() {
362
		$password = (string) $this->post->post_password;
363
		if ( 'edit' === $this->context ) {
364
			$password = htmlspecialchars_decode( (string) $password, ENT_QUOTES );
365
		}
366
		return $password;
367
	}
368
369
	public function get_parent() {
370
		if ( $this->post->post_parent ) {
371
			$parent = get_post( $this->post->post_parent );
372
			if ( 'display' === $this->context ) {
373
				$parent_title = (string) get_the_title( $parent->ID );
374
			} else {
375
				$parent_title = (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
376
			}
377
			return (object) array(
378
				'ID'   => (int) $parent->ID,
379
				'type' => (string) $parent->post_type,
380
				'link' => (string) $this->links->get_post_link( $this->site->get_id(), $parent->ID ),
381
				'title' => $parent_title,
382
			);
383
		} else {
384
			return false;
385
		}
386
	}
387
388
	function the_password_form() {
389
		return __( 'This post is password protected.', 'jetpack' );
390
	}
391
392
	public function get_discussion() {
393
		return array(
394
			'comments_open'  => (bool) comments_open( $this->post->ID ),
395
			'comment_status' => (string) $this->post->comment_status,
396
			'pings_open'     => (bool) pings_open( $this->post->ID ),
397
			'ping_status'    => (string) $this->post->ping_status,
398
			'comment_count'  => (int) $this->post->comment_count,
399
		);
400
	}
401
402
	public function is_likes_enabled() {
403
		/** This filter is documented in modules/likes.php */
404
		$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
405
		$post_likes_switched    = (bool) get_post_meta( $this->post->ID, 'switch_like_status', true );
406
		$post_likes_enabled = $sitewide_likes_enabled;
407
		if ( $post_likes_switched ) {
408
			$post_likes_enabled = ! $post_likes_enabled;
409
		}
410
		return (bool) $post_likes_enabled;
411
	}
412
413
	public function is_sharing_enabled() {
414
		$show = true;
415
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
416
		$show = apply_filters( 'sharing_show', $show, $this->post );
417
418
		$switched_status = get_post_meta( $this->post->ID, 'sharing_disabled', false );
419
420
		if ( !empty( $switched_status ) )
421
			$show = false;
422
423
		return (bool) $show;
424
	}
425
426
	// No Blog ID parameter.  No Post ID parameter.  Depends on globals.
427
	// Expects setup_postdata() to already have been run
428 View Code Duplication
	function get_the_post_content_for_display() {
429
		global $pages, $page;
430
431
		$old_pages = $pages;
432
		$old_page  = $page;
433
434
		$content = join( "\n\n", $pages );
435
		$content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
436
		$pages   = array( $content );
437
		$page    = 1;
438
439
		ob_start();
440
		the_content();
441
		$return = ob_get_clean();
442
443
		$pages = $old_pages;
444
		$page  = $old_page;
445
446
		return $return;
447
	}
448
449
	public function get_author() {
450
		if ( 0 == $this->post->post_author )
451
			return null;
452
453
		$show_email = $this->context === 'edit' && current_user_can( 'edit_post', $this->post );
454
455
		$user = get_user_by( 'id', $this->post->post_author );
456
457
		if ( ! $user || is_wp_error( $user ) ) {
458
			trigger_error( 'Unknown user', E_USER_WARNING );
459
460
			return null;
461
		}
462
463
		// TODO factor this out
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
464 View Code Duplication
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
465
			$active_blog = get_active_blog_for_user( $user->ID );
466
			$site_id     = $active_blog->get_id();
467
			$profile_URL = "http://en.gravatar.com/{$user->user_login}";
468
		} else {
469
			$profile_URL = 'http://en.gravatar.com/' . md5( strtolower( trim( $user->user_email ) ) );
470
			$site_id     = -1;
471
		}
472
473
		$author = array(
474
			'ID'          => (int) $user->ID,
475
			'login'       => (string) $user->user_login,
476
			'email'       => $show_email ? (string) $user->user_email : false, // (string|bool)
477
			'name'        => (string) $user->display_name,
478
			'first_name'  => (string) $user->first_name,
479
			'last_name'   => (string) $user->last_name,
480
			'nice_name'   => (string) $user->user_nicename,
481
			'URL'         => (string) esc_url_raw( $user->user_url ),
482
			'avatar_URL'  => (string) esc_url_raw( $this->get_avatar_url( $user->user_email ) ),
483
			'profile_URL' => (string) esc_url_raw( $profile_URL )
484
		);
485
486
		if ($site_id > -1) {
487
			$author['site_ID'] = (int) $site_id;
488
		}
489
490
		return (object) $author;
491
	}
492
493
	protected function get_avatar_url( $email, $avatar_size = 96 ) {
494
		$avatar_url = wpcom_get_avatar_url( $email, $avatar_size, '', true );
495
		if ( !$avatar_url || is_wp_error( $avatar_url ) ) {
496
			return '';
497
		}
498
499
		return esc_url_raw( htmlspecialchars_decode( $avatar_url[0] ) );
500
	}
501
502
	/**
503
 	 * Get extra post permalink suggestions
504
 	 * @return array	array of permalink suggestions: 'permalink_URL', 'suggested_slug'
505
 	 */
506
	public function get_permalink_suggestions( $title ) {
507
		$suggestions = array();
508
		list( $suggestions['permalink_URL'], $suggestions['suggested_slug'] ) = get_sample_permalink( $this->post->ID, $title );
509
		return $suggestions;
510
	}
511
512 View Code Duplication
	private function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
513
		// Permissions
514
		switch ( $context ) {
515
		case 'edit' :
516
			$tax = get_taxonomy( $taxonomy_type );
517
			if ( !current_user_can( $tax->cap->edit_terms ) )
518
				return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
519
			break;
520
		case 'display' :
521
			if ( -1 == get_option( 'blog_public' ) && ! current_user_can( 'read' ) ) {
522
				return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
523
			}
524
			break;
525
		default :
0 ignored issues
show
Coding Style introduced by
There must be no space before the colon in a DEFAULT statement

As per the PSR-2 coding standard, there must not be a space in front of the colon in the default statement.

switch ($expr) {
    default : //wrong
        doSomething();
        break;
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

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

Loading history...
526
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
527
		}
528
529
		$response                = array();
530
		$response['ID']          = (int) $taxonomy->term_id;
531
		$response['name']        = (string) $taxonomy->name;
532
		$response['slug']        = (string) $taxonomy->slug;
533
		$response['description'] = (string) $taxonomy->description;
534
		$response['post_count']  = (int) $taxonomy->count;
535
536
		if ( is_taxonomy_hierarchical( $taxonomy_type ) ) {
537
			$response['parent'] = (int) $taxonomy->parent;
538
		}
539
540
		$response['meta'] = (object) array(
541
			'links' => (object) array(
542
				'self' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type ),
543
				'help' => (string) $this->links->get_taxonomy_link( $this->site->get_id(), $taxonomy->slug, $taxonomy_type, 'help' ),
544
				'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
545
			),
546
		);
547
548
		return (object) $response;
549
	}
550
551
	// TODO: factor this out into site
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
552 View Code Duplication
	private function get_media_item_v1_1( $media_id ) {
553
		$media_item = get_post( $media_id );
554
555
		if ( ! $media_item || is_wp_error( $media_item ) )
556
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
557
558
		$file = basename( wp_get_attachment_url( $media_item->ID ) );
559
		$file_info = pathinfo( $file );
560
		$ext  = $file_info['extension'];
561
562
		$response = array(
563
			'ID'           => $media_item->ID,
564
			'URL'          => wp_get_attachment_url( $media_item->ID ),
565
			'guid'         => $media_item->guid,
566
			'date'         => (string) WPCOM_JSON_API_Date::format_date( $media_item->post_date_gmt, $media_item->post_date ),
567
			'post_ID'      => $media_item->post_parent,
568
			'author_ID'    => (int) $media_item->post_author,
569
			'file'         => $file,
570
			'mime_type'    => $media_item->post_mime_type,
571
			'extension'    => $ext,
572
			'title'        => $media_item->post_title,
573
			'caption'      => $media_item->post_excerpt,
574
			'description'  => $media_item->post_content,
575
			'alt'          => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
576
			'thumbnails'   => array()
577
		);
578
579
		if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) {
580
			$metadata = wp_get_attachment_metadata( $media_item->ID );
581
			if ( isset( $metadata['height'], $metadata['width'] ) ) {
582
				$response['height'] = $metadata['height'];
583
				$response['width'] = $metadata['width'];
584
			}
585
586
			if ( isset( $metadata['sizes'] ) ) {
587
				/**
588
				 * Filter the thumbnail sizes available for each attachment ID.
589
				 *
590
				 * @module json-api
591
				 *
592
				 * @since 3.9.0
593
				 *
594
				 * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
595
				 * @param string $media_id Attachment ID.
596
				 */
597
				$sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_id );
598
				if ( is_array( $sizes ) ) {
599
					foreach ( $sizes as $size => $size_details ) {
600
						$response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
601
					}
602
				}
603
			}
604
605
			if ( isset( $metadata['image_meta'] ) ) {
606
				$response['exif'] = $metadata['image_meta'];
607
			}
608
		}
609
610
		if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ) ) ) {
611
			$metadata = wp_get_attachment_metadata( $media_item->ID );
612
			$response['length'] = $metadata['length'];
613
			$response['exif']   = $metadata;
614
		}
615
616
		if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) {
617
			$metadata = wp_get_attachment_metadata( $media_item->ID );
618
			if ( isset( $metadata['height'], $metadata['width'] ) ) {
619
				$response['height'] = $metadata['height'];
620
				$response['width']  = $metadata['width'];
621
			}
622
623
			if ( isset( $metadata['length'] ) ) {
624
				$response['length'] = $metadata['length'];
625
			}
626
627
			// add VideoPress info
628
			if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
629
				$info = video_get_info_by_blogpostid( $this->site->get_id(), $media_id );
630
631
				// Thumbnails
632
				if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
633
					$response['thumbnails'] = array( 'fmt_hd' => '', 'fmt_dvd' => '', 'fmt_std' => '' );
634
					foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
635
						if ( video_format_done( $info, $size ) ) {
636
							$response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
637
						} else {
638
							unset( $response['thumbnails'][ $size ] );
639
						}
640
					}
641
				}
642
643
				$response['videopress_guid'] = $info->guid;
644
				$response['videopress_processing_done'] = true;
645
				if ( '0000-00-00 00:00:00' == $info->finish_date_gmt ) {
646
					$response['videopress_processing_done'] = false;
647
				}
648
			}
649
		}
650
651
		$response['thumbnails'] = (object) $response['thumbnails'];
652
653
		$response['meta'] = (object) array(
654
			'links' => (object) array(
655
				'self' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id ),
656
				'help' => (string) $this->links->get_media_link( $this->site->get_id(), $media_id, 'help' ),
657
				'site' => (string) $this->links->get_site_link( $this->site->get_id() ),
658
			),
659
		);
660
661
		// add VideoPress link to the meta
662
		if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) {
663
			if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
664
				$response['meta']->links->videopress = (string) $this->links->get_link( '/videos/%s', $response['videopress_guid'], '' );
0 ignored issues
show
Documentation introduced by
The property links does not exist on object<SAL_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
665
			}
666
		}
667
668
		if ( $media_item->post_parent > 0 ) {
669
			$response['meta']->links->parent = (string) $this->links->get_post_link( $this->site->get_id(), $media_item->post_parent );
0 ignored issues
show
Documentation introduced by
The property links does not exist on object<SAL_Post>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
670
		}
671
672
		return (object) $response;
673
	}
674
}