Completed
Push — fix/twitter-cards-3778 ( 2ceea6...e05f35 )
by Jeremy
09:08
created

SAL_Post::get_shortlink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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_tags() {
69
		$tags = array();
70
		$terms = wp_get_post_tags( $this->post->ID );
71
		foreach ( $terms as $term ) {
72
			if ( !empty( $term->name ) ) {
73
				$tags[$term->name] = $this->format_taxonomy( $term, 'post_tag', 'display' );
74
			}
75
		}
76
		return (object) $tags;
77
	}
78
79
	public function get_categories() {
80
		$categories = array();
81
		$terms = wp_get_object_terms( $this->post->ID, 'category', array( 'fields' => 'all' ) );
82 View Code Duplication
		foreach ( $terms as $term ) {
83
			if ( !empty( $term->name ) ) {
84
				$categories[$term->name] = $this->format_taxonomy( $term, 'category', 'display' );
85
			}
86
		}
87
		return (object) $categories;
88
	}
89
90
	public function get_attachments_and_count() {
91
		$attachments = array();
92
		$_attachments = new WP_Query( array( 'post_parent' => $this->post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'posts_per_page' => '20' ) );
93
		foreach ( $_attachments->posts as $attachment ) {
94
			$attachments[$attachment->ID] = $this->get_media_item_v1_1( $attachment->ID );
95
		}
96
		return array( (object) $attachments, (int) $_attachments->found_posts );
97
	}
98
99
	public function get_metadata() {
100
		$metadata = array();
101
		foreach ( (array) has_meta( $this->post->ID ) as $meta ) {
102
			// Don't expose protected fields.
103
			$meta_key = $meta['meta_key'];
104
			
105
			$show = !( WPCOM_JSON_API_Metadata::is_internal_only( $meta_key ) )
106
				&&
107
					( 
108
						WPCOM_JSON_API_Metadata::is_public( $meta_key ) 
109
					|| 
110
						current_user_can( 'edit_post_meta', $this->post->ID , $meta_key )
111
					);
112
			
113
			if ( $show ) {
114
				$metadata[] = array(
115
					'id'    => $meta['meta_id'],
116
					'key'   => $meta['meta_key'],
117
					'value' => maybe_unserialize( $meta['meta_value'] ),
118
				);
119
			}
120
		}
121
122
		if ( ! empty( $metadata ) ) {
123
			return $metadata;
124
		} else {
125
			return false;
126
		}
127
	}
128
129
	public function get_meta() {
130
		$meta = (object) array(
131
			'links' => (object) array(
132
				'self'    => (string) $this->get_post_link(),
133
				'help'    => (string) $this->get_post_link( 'help' ),
134
				'site'    => (string) $this->get_site_link(),
135
				'replies' => (string) $this->get_post_link( 'replies/' ),
136
				'likes'   => (string) $this->get_post_link( 'likes/' ),
137
			),
138
		);
139
140
		// add autosave link if a more recent autosave exists
141
		if ( 'edit' === $this->context ) {
142
			$autosave = wp_get_post_autosave( $this->post->ID );
143
			if ( $autosave && $autosave->post_modified > $this->post->post_modified )
144
				$meta->links->autosave = (string) $this->get_post_link() . '/autosave';
145
		}
146
147
		return $meta;
148
	}
149
150
	public function get_current_user_capabilities() {
151
		return array(
152
			'publish_post' => current_user_can( 'publish_post', $this->post ),
153
			'delete_post'  => current_user_can( 'delete_post', $this->post ),
154
			'edit_post'    => current_user_can( 'edit_post', $this->post )
155
		);
156
	}
157
158
	public function get_revisions() {
159
		if ( 'edit' !== $this->context ) {
160
			return false;
161
		}
162
163
		$revisions = array();
164
		$post_revisions = wp_get_post_revisions( $this->post->ID );
165
166
		foreach ( $post_revisions as $_post ) {
167
			$revisions[] = $_post->ID;
168
		}
169
170
		return $revisions;
171
	}
172
173
	public function get_other_urls() {
174
		$other_urls = array();
175
176
		if ( 'publish' !== $this->post->post_status ) {
177
			$other_urls = $this->get_permalink_suggestions( $this->post->post_title );
178
		}
179
180
		return (object) $other_urls;
181
	}
182
183
	protected function get_site_link() {
184
		return $this->links->get_site_link( $this->site->blog_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...
185
	}
186
187
	protected function get_post_link( $path = null ) {
188
		return $this->links->get_post_link( $this->site->blog_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...
189
	}
190
191
	public function get_publicize_urls() {
192
		$publicize_URLs = array();
193
		$publicize      = get_post_meta( $this->post->ID, 'publicize_results', true );
194 View Code Duplication
		if ( $publicize ) {
195
			foreach ( $publicize as $service => $data ) {
196
				switch ( $service ) {
197
				case 'twitter' :
198
					foreach ( $data as $datum ) {
199
						$publicize_URLs[] = esc_url_raw( "https://twitter.com/{$datum['user_id']}/status/{$datum['post_id']}" );
200
					}
201
					break;
202
				case 'fb' :
203
					foreach ( $data as $datum ) {
204
						$publicize_URLs[] = esc_url_raw( "https://www.facebook.com/permalink.php?story_fbid={$datum['post_id']}&id={$datum['user_id']}" );
205
					}
206
					break;
207
				}
208
			}
209
		}
210
		return (array) $publicize_URLs;
211
	}
212
213
	public function get_page_template() {
214
		return (string) get_post_meta( $this->post->ID, '_wp_page_template', true );
215
	}
216
217
	// note this is overridden in jetpack-shadow
218
	public function get_featured_image() {
219
		$image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $this->post->ID ), 'full' );
220
		if ( is_array( $image_attributes ) && isset( $image_attributes[0] ) ) {
221
			return (string) $image_attributes[0];
222
		} else {
223
			return '';
224
		}
225
	}
226
227
	public function get_post_thumbnail() {
228
		$thumb = null;
229
230
		$thumb_id = get_post_thumbnail_id( $this->post->ID );
231
232 View Code Duplication
		if ( ! empty( $thumb_id ) ) {
233
			$attachment = get_post( $thumb_id );
234
			if ( ! empty( $attachment ) )
235
				$featured_image_object = $this->get_attachment( $attachment );
236
237
			if ( ! empty( $featured_image_object ) ) {
238
				$thumb = (object) $featured_image_object;
239
			}
240
		}
241
242
		return $thumb;
243
	}
244
245
	public function get_format() {
246
		$format = (string) get_post_format( $this->post->ID );
247
		if ( !$format ) {
248
			$format = 'standard';
249
		}
250
251
		return $format;
252
	}
253
254 View Code Duplication
	private function get_attachment( $attachment ) {
255
		$metadata = wp_get_attachment_metadata( $attachment->ID );
256
257
		$result = array(
258
			'ID'		=> (int) $attachment->ID,
259
			'URL'           => (string) wp_get_attachment_url( $attachment->ID ),
260
			'guid'		=> (string) $attachment->guid,
261
			'mime_type'	=> (string) $attachment->post_mime_type,
262
			'width'		=> (int) isset( $metadata['width']  ) ? $metadata['width']  : 0,
263
			'height'	=> (int) isset( $metadata['height'] ) ? $metadata['height'] : 0,
264
		);
265
266
		if ( isset( $metadata['duration'] ) ) {
267
			$result['duration'] = (int) $metadata['duration'];
268
		}
269
270
		/** This filter is documented in class.jetpack-sync.php */
271
		return (object) apply_filters( 'get_attachment', $result );
272
	}
273
274
	public function get_date() {
275
		return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_date_gmt, $this->post->post_date );
276
	}
277
278
	public function get_modified_date() {
279
		return (string) WPCOM_JSON_API_Date::format_date( $this->post->post_modified_gmt, $this->post->post_modified );
280
	}
281
282
	public function get_title() {
283
		if ( 'display' === $this->context ) {
284
			return (string) get_the_title( $this->post->ID );
285
		} else {
286
			return (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
287
		}
288
	}
289
290
	public function get_url() {
291
		if ( 'revision' === $this->post->post_type ) {
292
			return (string) esc_url_raw( get_permalink( $this->post->post_parent ) );
293
		} else {
294
			return (string) esc_url_raw( get_permalink( $this->post->ID ) );
295
		}
296
	}
297
298
	public function get_shortlink() {
299
		return (string) esc_url_raw( wp_get_shortlink( $this->post->ID ) );
300
	}
301
302
	public function get_content() {
303
		if ( 'display' === $this->context ) {
304
			// 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...
305
			add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
306
			$content = (string) $this->get_the_post_content_for_display();
307
			remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
308
			return $content;
309
		} else {
310
			return (string) $this->post->post_content;
311
		}
312
	}
313
314
	public function get_excerpt() {
315
		if ( 'display' === $this->context ) {
316
			add_filter( 'the_password_form', array( $this, 'the_password_form' ) );
317
			ob_start();
318
			the_excerpt();
319
			$response = (string) ob_get_clean();
320
			remove_filter( 'the_password_form', array( $this, 'the_password_form' ) );
321
		} else {
322
			$response = htmlspecialchars_decode( (string) $this->post->post_excerpt, ENT_QUOTES );
323
		}
324
		return $response;
325
	}
326
327
	public function get_status() {
328
		return (string) get_post_status( $this->post->ID );
329
	}
330
331
	public function is_sticky() {
332
		return (bool) is_sticky( $this->post->ID );
333
	}
334
335
	public function get_slug() {
336
		return (string) $this->post->post_name;
337
	}
338
339
	public function get_password() {
340
		$password = (string) $this->post->post_password;
341
		if ( 'edit' === $this->context ) {
342
			$password = htmlspecialchars_decode( (string) $password, ENT_QUOTES );
343
		}
344
		return $password;
345
	}
346
347
	public function get_parent() {
348
		if ( $this->post->post_parent ) {
349
			$parent = get_post( $this->post->post_parent );
350
			if ( 'display' === $this->context ) {
351
				$parent_title = (string) get_the_title( $parent->ID );
352
			} else {
353
				$parent_title = (string) htmlspecialchars_decode( $this->post->post_title, ENT_QUOTES );
354
			}
355
			return (object) array(
356
				'ID'   => (int) $parent->ID,
357
				'type' => (string) $parent->post_type,
358
				'link' => (string) $this->links->get_post_link( $this->site->blog_id, $parent->ID ),
359
				'title' => $parent_title,
360
			);
361
		} else {
362
			return false;
363
		}
364
	}
365
366
	function the_password_form() {
367
		return __( 'This post is password protected.', 'jetpack' );
368
	}
369
370
	public function get_discussion() {
371
		return array(
372
			'comments_open'  => (bool) comments_open( $this->post->ID ),
373
			'comment_status' => (string) $this->post->comment_status,
374
			'pings_open'     => (bool) pings_open( $this->post->ID ),
375
			'ping_status'    => (string) $this->post->ping_status,
376
			'comment_count'  => (int) $this->post->comment_count,
377
		);
378
	}
379
380
	public function is_likes_enabled() {
381
		/** This filter is documented in modules/likes.php */
382
		$sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) );
383
		$post_likes_switched    = (bool) get_post_meta( $this->post->ID, 'switch_like_status', true );
384
		$post_likes_enabled = $sitewide_likes_enabled;
385
		if ( $post_likes_switched ) {
386
			$post_likes_enabled = ! $post_likes_enabled;
387
		}
388
		return (bool) $post_likes_enabled;
389
	}
390
391
	public function is_sharing_enabled() {
392
		$show = true;
393
		/** This filter is documented in modules/sharedaddy/sharing-service.php */
394
		$show = apply_filters( 'sharing_show', $show, $this->post );
395
396
		$switched_status = get_post_meta( $this->post->ID, 'sharing_disabled', false );
397
398
		if ( !empty( $switched_status ) )
399
			$show = false;
400
401
		return (bool) $show;
402
	}
403
404
	// No Blog ID parameter.  No Post ID parameter.  Depends on globals.
405
	// Expects setup_postdata() to already have been run
406 View Code Duplication
	function get_the_post_content_for_display() {
407
		global $pages, $page;
408
409
		$old_pages = $pages;
410
		$old_page  = $page;
411
412
		$content = join( "\n\n", $pages );
413
		$content = preg_replace( '/<!--more(.*?)?-->/', '', $content );
414
		$pages   = array( $content );
415
		$page    = 1;
416
417
		ob_start();
418
		the_content();
419
		$return = ob_get_clean();
420
421
		$pages = $old_pages;
422
		$page  = $old_page;
423
424
		return $return;
425
	}
426
427
	public function get_author() {
428
		if ( 0 == $this->post->post_author )
429
			return null;
430
431
		$show_email = $this->context === 'edit' && current_user_can( 'edit_post', $this->post );
432
433
		$user = get_user_by( 'id', $this->post->post_author );
434
435
		if ( ! $user || is_wp_error( $user ) ) {
436
			trigger_error( 'Unknown user', E_USER_WARNING );
437
438
			return null;
439
		}
440
441
		// 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...
442
		if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
443
			$active_blog = get_active_blog_for_user( $user->ID );
444
			$site_id     = $active_blog->blog_id;
445
			$profile_URL = "http://en.gravatar.com/{$user->user_login}";
446
		} else {
447
			$profile_URL = 'http://en.gravatar.com/' . md5( strtolower( trim( $user->user_email ) ) );
448
			$site_id     = -1;
449
		}
450
451
		$author = array(
452
			'ID'          => (int) $user->ID,
453
			'login'       => (string) $user->user_login,
454
			'email'       => $show_email ? (string) $user->user_email : false, // (string|bool)
455
			'name'        => (string) $user->display_name,
456
			'first_name'  => (string) $user->first_name,
457
			'last_name'   => (string) $user->last_name,
458
			'nice_name'   => (string) $user->user_nicename,
459
			'URL'         => (string) esc_url_raw( $user->user_url ),
460
			'avatar_URL'  => (string) esc_url_raw( $this->get_avatar_url( $user->user_email ) ),
461
			'profile_URL' => (string) esc_url_raw( $profile_URL )
462
		);
463
464
		if ($site_id > -1) {
465
			$author['site_ID'] = (int) $site_id;
466
		}
467
468
		return (object) $author;
469
	}
470
471
	private function get_avatar_url( $email, $avatar_size = 96 ) {
472
		$avatar_url = wpcom_get_avatar_url( $email, $avatar_size, '', true );
473
		if ( !$avatar_url || is_wp_error( $avatar_url ) ) {
474
			return '';
475
		}
476
477
		return esc_url_raw( htmlspecialchars_decode( $avatar_url[0] ) );
478
	}
479
480
	/**
481
 	 * Get extra post permalink suggestions
482
 	 * @return array	array of permalink suggestions: 'permalink_URL', 'suggested_slug'
483
 	 */
484
	public function get_permalink_suggestions( $title ) {
485
		$suggestions = array();
486
		list( $suggestions['permalink_URL'], $suggestions['suggested_slug'] ) = get_sample_permalink( $this->post->ID, $title );
487
		return $suggestions;
488
	}
489
490
	private function format_taxonomy( $taxonomy, $taxonomy_type, $context ) {
491
		// Permissions
492 View Code Duplication
		switch ( $context ) {
493
		case 'edit' :
494
			$tax = get_taxonomy( $taxonomy_type );
495
			if ( !current_user_can( $tax->cap->edit_terms ) )
496
				return new WP_Error( 'unauthorized', 'User cannot edit taxonomy', 403 );
497
			break;
498
		case 'display' :
499
			if ( -1 == get_option( 'blog_public' ) && ! current_user_can( 'read' ) ) {
500
				return new WP_Error( 'unauthorized', 'User cannot view taxonomy', 403 );
501
			}
502
			break;
503
		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...
504
			return new WP_Error( 'invalid_context', 'Invalid API CONTEXT', 400 );
505
		}
506
507
		$response                = array();
508
		$response['ID']          = (int) $taxonomy->term_id;
509
		$response['name']        = (string) $taxonomy->name;
510
		$response['slug']        = (string) $taxonomy->slug;
511
		$response['description'] = (string) $taxonomy->description;
512
		$response['post_count']  = (int) $taxonomy->count;
513
514
		if ( 'category' === $taxonomy_type )
515
			$response['parent'] = (int) $taxonomy->parent;
516
517
		$response['meta'] = (object) array(
518
			'links' => (object) array(
519
				'self' => (string) $this->links->get_taxonomy_link( $this->site->blog_id, $taxonomy->slug, $taxonomy_type ),
520
				'help' => (string) $this->links->get_taxonomy_link( $this->site->blog_id, $taxonomy->slug, $taxonomy_type, 'help' ),
521
				'site' => (string) $this->links->get_site_link( $this->site->blog_id ),
522
			),
523
		);
524
525
		return (object) $response;
526
	}
527
528
	// 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...
529
	private function get_media_item_v1_1( $media_id ) {
530
		$media_item = get_post( $media_id );
531
532
		if ( ! $media_item || is_wp_error( $media_item ) )
533
			return new WP_Error( 'unknown_media', 'Unknown Media', 404 );
534
535
		$file = basename( wp_get_attachment_url( $media_item->ID ) );
536
		$file_info = pathinfo( $file );
537
		$ext  = $file_info['extension'];
538
539
		$response = array(
540
			'ID'           => $media_item->ID,
541
			'URL'          => wp_get_attachment_url( $media_item->ID ),
542
			'guid'         => $media_item->guid,
543
			'date'         => (string) WPCOM_JSON_API_Date::format_date( $media_item->post_date_gmt, $media_item->post_date ),
544
			'post_ID'      => $media_item->post_parent,
545
			'author_ID'    => (int) $media_item->post_author,
546
			'file'         => $file,
547
			'mime_type'    => $media_item->post_mime_type,
548
			'extension'    => $ext,
549
			'title'        => $media_item->post_title,
550
			'caption'      => $media_item->post_excerpt,
551
			'description'  => $media_item->post_content,
552
			'alt'          => get_post_meta( $media_item->ID, '_wp_attachment_image_alt', true ),
553
			'thumbnails'   => array()
554
		);
555
556 View Code Duplication
		if ( in_array( $ext, array( 'jpg', 'jpeg', 'png', 'gif' ) ) ) {
557
			$metadata = wp_get_attachment_metadata( $media_item->ID );
558
			if ( isset( $metadata['height'], $metadata['width'] ) ) {
559
				$response['height'] = $metadata['height'];
560
				$response['width'] = $metadata['width'];
561
			}
562
563
			if ( isset( $metadata['sizes'] ) ) {
564
				/**
565
				 * Filter the thumbnail sizes available for each attachment ID.
566
				 *
567
				 * @module json-api
568
				 *
569
				 * @since 3.9.0
570
				 *
571
				 * @param array $metadata['sizes'] Array of thumbnail sizes available for a given attachment ID.
572
				 * @param string $media_id Attachment ID.
573
				 */
574
				$sizes = apply_filters( 'rest_api_thumbnail_sizes', $metadata['sizes'], $media_id );
575
				if ( is_array( $sizes ) ) {
576
					foreach ( $sizes as $size => $size_details ) {
577
						$response['thumbnails'][ $size ] = dirname( $response['URL'] ) . '/' . $size_details['file'];
578
					}
579
				}
580
			}
581
582
			if ( isset( $metadata['image_meta'] ) ) {
583
				$response['exif'] = $metadata['image_meta'];
584
			}
585
		}
586
587 View Code Duplication
		if ( in_array( $ext, array( 'mp3', 'm4a', 'wav', 'ogg' ) ) ) {
588
			$metadata = wp_get_attachment_metadata( $media_item->ID );
589
			$response['length'] = $metadata['length'];
590
			$response['exif']   = $metadata;
591
		}
592
593 View Code Duplication
		if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) {
594
			$metadata = wp_get_attachment_metadata( $media_item->ID );
595
			if ( isset( $metadata['height'], $metadata['width'] ) ) {
596
				$response['height'] = $metadata['height'];
597
				$response['width']  = $metadata['width'];
598
			}
599
600
			if ( isset( $metadata['length'] ) ) {
601
				$response['length'] = $metadata['length'];
602
			}
603
604
			// add VideoPress info
605
			if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
606
				$info = video_get_info_by_blogpostid( $this->site->blog_id, $media_id );
607
608
				// Thumbnails
609
				if ( function_exists( 'video_format_done' ) && function_exists( 'video_image_url_by_guid' ) ) {
610
					$response['thumbnails'] = array( 'fmt_hd' => '', 'fmt_dvd' => '', 'fmt_std' => '' );
611
					foreach ( $response['thumbnails'] as $size => $thumbnail_url ) {
612
						if ( video_format_done( $info, $size ) ) {
613
							$response['thumbnails'][ $size ] = video_image_url_by_guid( $info->guid, $size );
614
						} else {
615
							unset( $response['thumbnails'][ $size ] );
616
						}
617
					}
618
				}
619
620
				$response['videopress_guid'] = $info->guid;
621
				$response['videopress_processing_done'] = true;
622
				if ( '0000-00-00 00:00:00' == $info->finish_date_gmt ) {
623
					$response['videopress_processing_done'] = false;
624
				}
625
			}
626
		}
627
628
		$response['thumbnails'] = (object) $response['thumbnails'];
629
630
		$response['meta'] = (object) array(
631
			'links' => (object) array(
632
				'self' => (string) $this->links->get_media_link( $this->site->blog_id, $media_id ),
633
				'help' => (string) $this->links->get_media_link( $this->site->blog_id, $media_id, 'help' ),
634
				'site' => (string) $this->links->get_site_link( $this->site->blog_id ),
635
			),
636
		);
637
638
		// add VideoPress link to the meta
639 View Code Duplication
		if ( in_array( $ext, array( 'ogv', 'mp4', 'mov', 'wmv', 'avi', 'mpg', '3gp', '3g2', 'm4v' ) ) ) {
640
			if ( function_exists( 'video_get_info_by_blogpostid' ) ) {
641
				$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...
642
			}
643
		}
644
645 View Code Duplication
		if ( $media_item->post_parent > 0 ) {
646
			$response['meta']->links->parent = (string) $this->links->get_post_link( $this->site->blog_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...
647
		}
648
649
		return (object) $response;
650
	}
651
}