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