wp_prepare_attachment_for_js

Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model.

Description

(array|void) wp_prepare_attachment_for_js( (mixed) $attachment ); 

Returns (array|void)

Array of attachment details.

Parameters (1)

0. $attachment (mixed)
Attachment ID or object.

Usage

  1. if ( !function_exists( 'wp_prepare_attachment_for_js' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Attachment ID or object. 
  5. $attachment = null; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_prepare_attachment_for_js($attachment); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function wp_prepare_attachment_for_js( $attachment ) { 
  2. if ( ! $attachment = get_post( $attachment ) ) 
  3. return; 
  4.  
  5. if ( 'attachment' != $attachment->post_type ) 
  6. return; 
  7.  
  8. $meta = wp_get_attachment_metadata( $attachment->ID ); 
  9. if ( false !== strpos( $attachment->post_mime_type, '/' ) ) 
  10. list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); 
  11. else 
  12. list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); 
  13.  
  14. $attachment_url = wp_get_attachment_url( $attachment->ID ); 
  15. $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); 
  16.  
  17. $response = array( 
  18. 'id' => $attachment->ID,  
  19. 'title' => $attachment->post_title,  
  20. 'filename' => wp_basename( get_attached_file( $attachment->ID ) ),  
  21. 'url' => $attachment_url,  
  22. 'link' => get_attachment_link( $attachment->ID ),  
  23. 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),  
  24. 'author' => $attachment->post_author,  
  25. 'description' => $attachment->post_content,  
  26. 'caption' => $attachment->post_excerpt,  
  27. 'name' => $attachment->post_name,  
  28. 'status' => $attachment->post_status,  
  29. 'uploadedTo' => $attachment->post_parent,  
  30. 'date' => strtotime( $attachment->post_date_gmt ) * 1000,  
  31. 'modified' => strtotime( $attachment->post_modified_gmt ) * 1000,  
  32. 'menuOrder' => $attachment->menu_order,  
  33. 'mime' => $attachment->post_mime_type,  
  34. 'type' => $type,  
  35. 'subtype' => $subtype,  
  36. 'icon' => wp_mime_type_icon( $attachment->ID ),  
  37. 'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),  
  38. 'nonces' => array( 
  39. 'update' => false,  
  40. 'delete' => false,  
  41. 'edit' => false 
  42. ),  
  43. 'editLink' => false,  
  44. 'meta' => false,  
  45. ); 
  46.  
  47. $author = new WP_User( $attachment->post_author ); 
  48. $response['authorName'] = $author->display_name; 
  49.  
  50. if ( $attachment->post_parent ) { 
  51. $post_parent = get_post( $attachment->post_parent ); 
  52. } else { 
  53. $post_parent = false; 
  54.  
  55. if ( $post_parent ) { 
  56. $parent_type = get_post_type_object( $post_parent->post_type ); 
  57.  
  58. if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $attachment->post_parent ) ) { 
  59. $response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' ); 
  60.  
  61. if ( $parent_type && current_user_can( 'read_post', $attachment->post_parent ) ) { 
  62. $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); 
  63.  
  64. $attached_file = get_attached_file( $attachment->ID ); 
  65.  
  66. if ( isset( $meta['filesize'] ) ) { 
  67. $bytes = $meta['filesize']; 
  68. } elseif ( file_exists( $attached_file ) ) { 
  69. $bytes = filesize( $attached_file ); 
  70. } else { 
  71. $bytes = ''; 
  72.  
  73. if ( $bytes ) { 
  74. $response['filesizeInBytes'] = $bytes; 
  75. $response['filesizeHumanReadable'] = size_format( $bytes ); 
  76.  
  77. if ( current_user_can( 'edit_post', $attachment->ID ) ) { 
  78. $response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID ); 
  79. $response['nonces']['edit'] = wp_create_nonce( 'image_editor-' . $attachment->ID ); 
  80. $response['editLink'] = get_edit_post_link( $attachment->ID, 'raw' ); 
  81.  
  82. if ( current_user_can( 'delete_post', $attachment->ID ) ) 
  83. $response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID ); 
  84.  
  85. if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) { 
  86. $sizes = array(); 
  87.  
  88. /** This filter is documented in wp-admin/includes/media.php */ 
  89. $possible_sizes = apply_filters( 'image_size_names_choose', array( 
  90. 'thumbnail' => __('Thumbnail'),  
  91. 'medium' => __('Medium'),  
  92. 'large' => __('Large'),  
  93. 'full' => __('Full Size'),  
  94. ) ); 
  95. unset( $possible_sizes['full'] ); 
  96.  
  97. // Loop through all potential sizes that may be chosen. Try to do this with some efficiency. 
  98. // First: run the image_downsize filter. If it returns something, we can use its data. 
  99. // If the filter does not return something, then image_downsize() is just an expensive 
  100. // way to check the image metadata, which we do second. 
  101. foreach ( $possible_sizes as $size => $label ) { 
  102.  
  103. /** This filter is documented in wp-includes/media.php */ 
  104. if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) { 
  105. if ( empty( $downsize[3] ) ) { 
  106. continue; 
  107.  
  108. $sizes[ $size ] = array( 
  109. 'height' => $downsize[2],  
  110. 'width' => $downsize[1],  
  111. 'url' => $downsize[0],  
  112. 'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape',  
  113. ); 
  114. } elseif ( isset( $meta['sizes'][ $size ] ) ) { 
  115. // Nothing from the filter, so consult image metadata if we have it. 
  116. $size_meta = $meta['sizes'][ $size ]; 
  117.  
  118. // We have the actual image size, but might need to further constrain it if content_width is narrower. 
  119. // Thumbnail, medium, and full sizes are also checked against the site's height/width options. 
  120. list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' ); 
  121.  
  122. $sizes[ $size ] = array( 
  123. 'height' => $height,  
  124. 'width' => $width,  
  125. 'url' => $base_url . $size_meta['file'],  
  126. 'orientation' => $height > $width ? 'portrait' : 'landscape',  
  127. ); 
  128.  
  129. if ( 'image' === $type ) { 
  130. $sizes['full'] = array( 'url' => $attachment_url ); 
  131.  
  132. if ( isset( $meta['height'], $meta['width'] ) ) { 
  133. $sizes['full']['height'] = $meta['height']; 
  134. $sizes['full']['width'] = $meta['width']; 
  135. $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape'; 
  136.  
  137. $response = array_merge( $response, $sizes['full'] ); 
  138. } elseif ( $meta['sizes']['full']['file'] ) { 
  139. $sizes['full'] = array( 
  140. 'url' => $base_url . $meta['sizes']['full']['file'],  
  141. 'height' => $meta['sizes']['full']['height'],  
  142. 'width' => $meta['sizes']['full']['width'],  
  143. 'orientation' => $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape' 
  144. ); 
  145.  
  146. $response = array_merge( $response, array( 'sizes' => $sizes ) ); 
  147.  
  148. if ( $meta && 'video' === $type ) { 
  149. if ( isset( $meta['width'] ) ) 
  150. $response['width'] = (int) $meta['width']; 
  151. if ( isset( $meta['height'] ) ) 
  152. $response['height'] = (int) $meta['height']; 
  153.  
  154. if ( $meta && ( 'audio' === $type || 'video' === $type ) ) { 
  155. if ( isset( $meta['length_formatted'] ) ) 
  156. $response['fileLength'] = $meta['length_formatted']; 
  157.  
  158. $response['meta'] = array(); 
  159. foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) { 
  160. $response['meta'][ $key ] = false; 
  161.  
  162. if ( ! empty( $meta[ $key ] ) ) { 
  163. $response['meta'][ $key ] = $meta[ $key ]; 
  164.  
  165. $id = get_post_thumbnail_id( $attachment->ID ); 
  166. if ( ! empty( $id ) ) { 
  167. list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' ); 
  168. $response['image'] = compact( 'src', 'width', 'height' ); 
  169. list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' ); 
  170. $response['thumb'] = compact( 'src', 'width', 'height' ); 
  171. } else { 
  172. $src = wp_mime_type_icon( $attachment->ID ); 
  173. $width = 48; 
  174. $height = 64; 
  175. $response['image'] = compact( 'src', 'width', 'height' ); 
  176. $response['thumb'] = compact( 'src', 'width', 'height' ); 
  177.  
  178. if ( function_exists('get_compat_media_markup') ) 
  179. $response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) ); 
  180.  
  181. /** 
  182. * Filters the attachment data prepared for JavaScript. 
  183. * @since 3.5.0 
  184. * @param array $response Array of prepared attachment data. 
  185. * @param int|object $attachment Attachment ID or object. 
  186. * @param array $meta Array of attachment meta data. 
  187. */ 
  188. return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );