PageRenderTime 63ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 1ms

/APP/wp-includes/media.php

https://bitbucket.org/AFelipeTrujillo/goblog
PHP | 3105 lines | 1512 code | 341 blank | 1252 comment | 314 complexity | 8fa567db388bf940d839361afc3cc6a4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * WordPress API for media display.
  4. *
  5. * @package WordPress
  6. * @subpackage Media
  7. */
  8. /**
  9. * Scale down the default size of an image.
  10. *
  11. * This is so that the image is a better fit for the editor and theme.
  12. *
  13. * The $size parameter accepts either an array or a string. The supported string
  14. * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
  15. * 128 width and 96 height in pixels. Also supported for the string value is
  16. * 'medium' and 'full'. The 'full' isn't actually supported, but any value other
  17. * than the supported will result in the content_width size or 500 if that is
  18. * not set.
  19. *
  20. * Finally, there is a filter named 'editor_max_image_size', that will be called
  21. * on the calculated array for width and height, respectively. The second
  22. * parameter will be the value that was in the $size parameter. The returned
  23. * type for the hook is an array with the width as the first element and the
  24. * height as the second element.
  25. *
  26. * @since 2.5.0
  27. * @uses wp_constrain_dimensions() This function passes the widths and the heights.
  28. *
  29. * @param int $width Width of the image
  30. * @param int $height Height of the image
  31. * @param string|array $size Size of what the result image should be.
  32. * @param context Could be 'display' (like in a theme) or 'edit' (like inserting into an editor)
  33. * @return array Width and height of what the result image should resize to.
  34. */
  35. function image_constrain_size_for_editor($width, $height, $size = 'medium', $context = null ) {
  36. global $content_width, $_wp_additional_image_sizes;
  37. if ( ! $context )
  38. $context = is_admin() ? 'edit' : 'display';
  39. if ( is_array($size) ) {
  40. $max_width = $size[0];
  41. $max_height = $size[1];
  42. }
  43. elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
  44. $max_width = intval(get_option('thumbnail_size_w'));
  45. $max_height = intval(get_option('thumbnail_size_h'));
  46. // last chance thumbnail size defaults
  47. if ( !$max_width && !$max_height ) {
  48. $max_width = 128;
  49. $max_height = 96;
  50. }
  51. }
  52. elseif ( $size == 'medium' ) {
  53. $max_width = intval(get_option('medium_size_w'));
  54. $max_height = intval(get_option('medium_size_h'));
  55. // if no width is set, default to the theme content width if available
  56. }
  57. elseif ( $size == 'large' ) {
  58. // We're inserting a large size image into the editor. If it's a really
  59. // big image we'll scale it down to fit reasonably within the editor
  60. // itself, and within the theme's content width if it's known. The user
  61. // can resize it in the editor if they wish.
  62. $max_width = intval(get_option('large_size_w'));
  63. $max_height = intval(get_option('large_size_h'));
  64. if ( intval($content_width) > 0 )
  65. $max_width = min( intval($content_width), $max_width );
  66. } elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
  67. $max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
  68. $max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
  69. if ( intval($content_width) > 0 && 'edit' == $context ) // Only in admin. Assume that theme authors know what they're doing.
  70. $max_width = min( intval($content_width), $max_width );
  71. }
  72. // $size == 'full' has no constraint
  73. else {
  74. $max_width = $width;
  75. $max_height = $height;
  76. }
  77. /**
  78. * Filter the maximum image size dimensions for the editor.
  79. *
  80. * @since 2.5.0
  81. *
  82. * @param array $max_image_size An array with the width as the first element,
  83. * and the height as the second element.
  84. * @param string|array $size Size of what the result image should be.
  85. * @param string $context The context the image is being resized for.
  86. * Possible values are 'display' (like in a theme)
  87. * or 'edit' (like inserting into an editor).
  88. */
  89. list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context );
  90. return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
  91. }
  92. /**
  93. * Retrieve width and height attributes using given width and height values.
  94. *
  95. * Both attributes are required in the sense that both parameters must have a
  96. * value, but are optional in that if you set them to false or null, then they
  97. * will not be added to the returned string.
  98. *
  99. * You can set the value using a string, but it will only take numeric values.
  100. * If you wish to put 'px' after the numbers, then it will be stripped out of
  101. * the return.
  102. *
  103. * @since 2.5.0
  104. *
  105. * @param int|string $width Optional. Width attribute value.
  106. * @param int|string $height Optional. Height attribute value.
  107. * @return string HTML attributes for width and, or height.
  108. */
  109. function image_hwstring($width, $height) {
  110. $out = '';
  111. if ($width)
  112. $out .= 'width="'.intval($width).'" ';
  113. if ($height)
  114. $out .= 'height="'.intval($height).'" ';
  115. return $out;
  116. }
  117. /**
  118. * Scale an image to fit a particular size (such as 'thumb' or 'medium').
  119. *
  120. * Array with image url, width, height, and whether is intermediate size, in
  121. * that order is returned on success is returned. $is_intermediate is true if
  122. * $url is a resized image, false if it is the original.
  123. *
  124. * The URL might be the original image, or it might be a resized version. This
  125. * function won't create a new resized copy, it will just return an already
  126. * resized one if it exists.
  127. *
  128. * A plugin may use the 'image_downsize' filter to hook into and offer image
  129. * resizing services for images. The hook must return an array with the same
  130. * elements that are returned in the function. The first element being the URL
  131. * to the new image that was resized.
  132. *
  133. * @since 2.5.0
  134. *
  135. * @param int $id Attachment ID for image.
  136. * @param array|string $size Optional, default is 'medium'. Size of image, either array or string.
  137. * @return bool|array False on failure, array on success.
  138. */
  139. function image_downsize($id, $size = 'medium') {
  140. if ( !wp_attachment_is_image($id) )
  141. return false;
  142. /**
  143. * Filter whether to preempt the output of image_downsize().
  144. *
  145. * Passing a truthy value to the filter will effectively short-circuit
  146. * down-sizing the image, returning that value as output instead.
  147. *
  148. * @since 2.5.0
  149. *
  150. * @param bool $downsize Whether to short-circuit the image downsize. Default false.
  151. * @param int $id Attachment ID for image.
  152. * @param array|string $size Size of image, either array or string. Default 'medium'.
  153. */
  154. if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) {
  155. return $out;
  156. }
  157. $img_url = wp_get_attachment_url($id);
  158. $meta = wp_get_attachment_metadata($id);
  159. $width = $height = 0;
  160. $is_intermediate = false;
  161. $img_url_basename = wp_basename($img_url);
  162. // try for a new style intermediate size
  163. if ( $intermediate = image_get_intermediate_size($id, $size) ) {
  164. $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
  165. $width = $intermediate['width'];
  166. $height = $intermediate['height'];
  167. $is_intermediate = true;
  168. }
  169. elseif ( $size == 'thumbnail' ) {
  170. // fall back to the old thumbnail
  171. if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
  172. $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
  173. $width = $info[0];
  174. $height = $info[1];
  175. $is_intermediate = true;
  176. }
  177. }
  178. if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) {
  179. // any other type: use the real image
  180. $width = $meta['width'];
  181. $height = $meta['height'];
  182. }
  183. if ( $img_url) {
  184. // we have the actual image size, but might need to further constrain it if content_width is narrower
  185. list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
  186. return array( $img_url, $width, $height, $is_intermediate );
  187. }
  188. return false;
  189. }
  190. /**
  191. * Register a new image size.
  192. *
  193. * Cropping behavior for the image size is dependent on the value of $crop:
  194. * 1. If false (default), images will be scaled, not cropped.
  195. * 2. If an array in the form of array( x_crop_position, y_crop_position ):
  196. * - x_crop_position accepts 'left' 'center', or 'right'.
  197. * - y_crop_position accepts 'top', 'center', or 'bottom'.
  198. * Images will be cropped to the specified dimensions within the defined crop area.
  199. * 3. If true, images will be cropped to the specified dimensions using center positions.
  200. *
  201. * @since 2.9.0
  202. *
  203. * @param string $name Image size identifier.
  204. * @param int $width Image width in pixels.
  205. * @param int $height Image height in pixels.
  206. * @param bool|array $crop Optional. Whether to crop images to specified height and width or resize.
  207. * An array can specify positioning of the crop area. Default false.
  208. * @return bool|array False, if no image was created. Metadata array on success.
  209. */
  210. function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
  211. global $_wp_additional_image_sizes;
  212. $_wp_additional_image_sizes[ $name ] = array(
  213. 'width' => absint( $width ),
  214. 'height' => absint( $height ),
  215. 'crop' => $crop,
  216. );
  217. }
  218. /**
  219. * Check if an image size exists.
  220. *
  221. * @since 3.9.0
  222. *
  223. * @param string $name The image size to check.
  224. * @return bool True if the image size exists, false if not.
  225. */
  226. function has_image_size( $name ) {
  227. global $_wp_additional_image_sizes;
  228. return isset( $_wp_additional_image_sizes[ $name ] );
  229. }
  230. /**
  231. * Remove a new image size.
  232. *
  233. * @since 3.9.0
  234. *
  235. * @param string $name The image size to remove.
  236. * @return bool True if the image size was successfully removed, false on failure.
  237. */
  238. function remove_image_size( $name ) {
  239. global $_wp_additional_image_sizes;
  240. if ( isset( $_wp_additional_image_sizes[ $name ] ) ) {
  241. unset( $_wp_additional_image_sizes[ $name ] );
  242. return true;
  243. }
  244. return false;
  245. }
  246. /**
  247. * Registers an image size for the post thumbnail.
  248. *
  249. * @since 2.9.0
  250. * @see add_image_size() for details on cropping behavior.
  251. *
  252. * @param int $width Image width in pixels.
  253. * @param int $height Image height in pixels.
  254. * @param bool|array $crop Optional. Whether to crop images to specified height and width or resize.
  255. * An array can specify positioning of the crop area. Default false.
  256. * @return bool|array False, if no image was created. Metadata array on success.
  257. */
  258. function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
  259. add_image_size( 'post-thumbnail', $width, $height, $crop );
  260. }
  261. /**
  262. * An <img src /> tag for an image attachment, scaling it down if requested.
  263. *
  264. * The filter 'get_image_tag_class' allows for changing the class name for the
  265. * image without having to use regular expressions on the HTML content. The
  266. * parameters are: what WordPress will use for the class, the Attachment ID,
  267. * image align value, and the size the image should be.
  268. *
  269. * The second filter 'get_image_tag' has the HTML content, which can then be
  270. * further manipulated by a plugin to change all attribute values and even HTML
  271. * content.
  272. *
  273. * @since 2.5.0
  274. *
  275. * @param int $id Attachment ID.
  276. * @param string $alt Image Description for the alt attribute.
  277. * @param string $title Image Description for the title attribute.
  278. * @param string $align Part of the class name for aligning the image.
  279. * @param string $size Optional. Default is 'medium'.
  280. * @return string HTML IMG element for given image attachment
  281. */
  282. function get_image_tag($id, $alt, $title, $align, $size='medium') {
  283. list( $img_src, $width, $height ) = image_downsize($id, $size);
  284. $hwstring = image_hwstring($width, $height);
  285. $title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';
  286. $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id;
  287. /**
  288. * Filter the value of the attachment's image tag class attribute.
  289. *
  290. * @since 2.6.0
  291. *
  292. * @param string $class CSS class name or space-separated list of classes.
  293. * @param int $id Attachment ID.
  294. * @param string $align Part of the class name for aligning the image.
  295. * @param string $size Optional. Default is 'medium'.
  296. */
  297. $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size );
  298. $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
  299. /**
  300. * Filter the HTML content for the image tag.
  301. *
  302. * @since 2.6.0
  303. *
  304. * @param string $html HTML content for the image.
  305. * @param int $id Attachment ID.
  306. * @param string $alt Alternate text.
  307. * @param string $title Attachment title.
  308. * @param string $align Part of the class name for aligning the image.
  309. * @param string $size Optional. Default is 'medium'.
  310. */
  311. $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
  312. return $html;
  313. }
  314. /**
  315. * Calculates the new dimensions for a downsampled image.
  316. *
  317. * If either width or height are empty, no constraint is applied on
  318. * that dimension.
  319. *
  320. * @since 2.5.0
  321. *
  322. * @param int $current_width Current width of the image.
  323. * @param int $current_height Current height of the image.
  324. * @param int $max_width Optional. Maximum wanted width.
  325. * @param int $max_height Optional. Maximum wanted height.
  326. * @return array First item is the width, the second item is the height.
  327. */
  328. function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
  329. if ( !$max_width and !$max_height )
  330. return array( $current_width, $current_height );
  331. $width_ratio = $height_ratio = 1.0;
  332. $did_width = $did_height = false;
  333. if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
  334. $width_ratio = $max_width / $current_width;
  335. $did_width = true;
  336. }
  337. if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
  338. $height_ratio = $max_height / $current_height;
  339. $did_height = true;
  340. }
  341. // Calculate the larger/smaller ratios
  342. $smaller_ratio = min( $width_ratio, $height_ratio );
  343. $larger_ratio = max( $width_ratio, $height_ratio );
  344. if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height )
  345. // The larger ratio is too big. It would result in an overflow.
  346. $ratio = $smaller_ratio;
  347. else
  348. // The larger ratio fits, and is likely to be a more "snug" fit.
  349. $ratio = $larger_ratio;
  350. // Very small dimensions may result in 0, 1 should be the minimum.
  351. $w = max ( 1, intval( $current_width * $ratio ) );
  352. $h = max ( 1, intval( $current_height * $ratio ) );
  353. // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
  354. // We also have issues with recursive calls resulting in an ever-changing result. Constraining to the result of a constraint should yield the original result.
  355. // Thus we look for dimensions that are one pixel shy of the max value and bump them up
  356. if ( $did_width && $w == $max_width - 1 )
  357. $w = $max_width; // Round it up
  358. if ( $did_height && $h == $max_height - 1 )
  359. $h = $max_height; // Round it up
  360. return array( $w, $h );
  361. }
  362. /**
  363. * Retrieve calculated resize dimensions for use in WP_Image_Editor.
  364. *
  365. * Calculates dimensions and coordinates for a resized image that fits
  366. * within a specified width and height.
  367. *
  368. * Cropping behavior is dependent on the value of $crop:
  369. * 1. If false (default), images will not be cropped.
  370. * 2. If an array in the form of array( x_crop_position, y_crop_position ):
  371. * - x_crop_position accepts 'left' 'center', or 'right'.
  372. * - y_crop_position accepts 'top', 'center', or 'bottom'.
  373. * Images will be cropped to the specified dimensions within the defined crop area.
  374. * 3. If true, images will be cropped to the specified dimensions using center positions.
  375. *
  376. * @since 2.5.0
  377. *
  378. * @param int $orig_w Original width in pixels.
  379. * @param int $orig_h Original height in pixels.
  380. * @param int $dest_w New width in pixels.
  381. * @param int $dest_h New height in pixels.
  382. * @param bool|array $crop Optional. Whether to crop image to specified height and width or resize.
  383. * An array can specify positioning of the crop area. Default false.
  384. * @return bool|array False on failure. Returned array matches parameters for `imagecopyresampled()`.
  385. */
  386. function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
  387. if ($orig_w <= 0 || $orig_h <= 0)
  388. return false;
  389. // at least one of dest_w or dest_h must be specific
  390. if ($dest_w <= 0 && $dest_h <= 0)
  391. return false;
  392. /**
  393. * Filter whether to preempt calculating the image resize dimensions.
  394. *
  395. * Passing a non-null value to the filter will effectively short-circuit
  396. * image_resize_dimensions(), returning that value instead.
  397. *
  398. * @since 3.4.0
  399. *
  400. * @param null|mixed $null Whether to preempt output of the resize dimensions.
  401. * @param int $orig_w Original width in pixels.
  402. * @param int $orig_h Original height in pixels.
  403. * @param int $dest_w New width in pixels.
  404. * @param int $dest_h New height in pixels.
  405. * @param bool|array $crop Whether to crop image to specified height and width or resize.
  406. * An array can specify positioning of the crop area. Default false.
  407. */
  408. $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
  409. if ( null !== $output )
  410. return $output;
  411. if ( $crop ) {
  412. // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
  413. $aspect_ratio = $orig_w / $orig_h;
  414. $new_w = min($dest_w, $orig_w);
  415. $new_h = min($dest_h, $orig_h);
  416. if ( !$new_w ) {
  417. $new_w = intval($new_h * $aspect_ratio);
  418. }
  419. if ( !$new_h ) {
  420. $new_h = intval($new_w / $aspect_ratio);
  421. }
  422. $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
  423. $crop_w = round($new_w / $size_ratio);
  424. $crop_h = round($new_h / $size_ratio);
  425. if ( ! is_array( $crop ) || count( $crop ) !== 2 ) {
  426. $crop = array( 'center', 'center' );
  427. }
  428. list( $x, $y ) = $crop;
  429. if ( 'left' === $x ) {
  430. $s_x = 0;
  431. } elseif ( 'right' === $x ) {
  432. $s_x = $orig_w - $crop_w;
  433. } else {
  434. $s_x = floor( ( $orig_w - $crop_w ) / 2 );
  435. }
  436. if ( 'top' === $y ) {
  437. $s_y = 0;
  438. } elseif ( 'bottom' === $y ) {
  439. $s_y = $orig_h - $crop_h;
  440. } else {
  441. $s_y = floor( ( $orig_h - $crop_h ) / 2 );
  442. }
  443. } else {
  444. // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
  445. $crop_w = $orig_w;
  446. $crop_h = $orig_h;
  447. $s_x = 0;
  448. $s_y = 0;
  449. list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
  450. }
  451. // if the resulting image would be the same size or larger we don't want to resize it
  452. if ( $new_w >= $orig_w && $new_h >= $orig_h )
  453. return false;
  454. // the return array matches the parameters to imagecopyresampled()
  455. // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
  456. return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
  457. }
  458. /**
  459. * Resize an image to make a thumbnail or intermediate size.
  460. *
  461. * The returned array has the file size, the image width, and image height. The
  462. * filter 'image_make_intermediate_size' can be used to hook in and change the
  463. * values of the returned array. The only parameter is the resized file path.
  464. *
  465. * @since 2.5.0
  466. *
  467. * @param string $file File path.
  468. * @param int $width Image width.
  469. * @param int $height Image height.
  470. * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize.
  471. * @return bool|array False, if no image was created. Metadata array on success.
  472. */
  473. function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
  474. if ( $width || $height ) {
  475. $editor = wp_get_image_editor( $file );
  476. if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
  477. return false;
  478. $resized_file = $editor->save();
  479. if ( ! is_wp_error( $resized_file ) && $resized_file ) {
  480. unset( $resized_file['path'] );
  481. return $resized_file;
  482. }
  483. }
  484. return false;
  485. }
  486. /**
  487. * Retrieve the image's intermediate size (resized) path, width, and height.
  488. *
  489. * The $size parameter can be an array with the width and height respectively.
  490. * If the size matches the 'sizes' metadata array for width and height, then it
  491. * will be used. If there is no direct match, then the nearest image size larger
  492. * than the specified size will be used. If nothing is found, then the function
  493. * will break out and return false.
  494. *
  495. * The metadata 'sizes' is used for compatible sizes that can be used for the
  496. * parameter $size value.
  497. *
  498. * The url path will be given, when the $size parameter is a string.
  499. *
  500. * If you are passing an array for the $size, you should consider using
  501. * add_image_size() so that a cropped version is generated. It's much more
  502. * efficient than having to find the closest-sized image and then having the
  503. * browser scale down the image.
  504. *
  505. * @since 2.5.0
  506. * @see add_image_size()
  507. *
  508. * @param int $post_id Attachment ID for image.
  509. * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string.
  510. * @return bool|array False on failure or array of file path, width, and height on success.
  511. */
  512. function image_get_intermediate_size($post_id, $size='thumbnail') {
  513. if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
  514. return false;
  515. // get the best one for a specified set of dimensions
  516. if ( is_array($size) && !empty($imagedata['sizes']) ) {
  517. foreach ( $imagedata['sizes'] as $_size => $data ) {
  518. // already cropped to width or height; so use this size
  519. if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
  520. $file = $data['file'];
  521. list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
  522. return compact( 'file', 'width', 'height' );
  523. }
  524. // add to lookup table: area => size
  525. $areas[$data['width'] * $data['height']] = $_size;
  526. }
  527. if ( !$size || !empty($areas) ) {
  528. // find for the smallest image not smaller than the desired size
  529. ksort($areas);
  530. foreach ( $areas as $_size ) {
  531. $data = $imagedata['sizes'][$_size];
  532. if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
  533. // Skip images with unexpectedly divergent aspect ratios (crops)
  534. // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
  535. $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
  536. // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size
  537. if ( 'thumbnail' != $_size && ( !$maybe_cropped || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) ) )
  538. continue;
  539. // If we're still here, then we're going to use this size
  540. $file = $data['file'];
  541. list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
  542. return compact( 'file', 'width', 'height' );
  543. }
  544. }
  545. }
  546. }
  547. if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
  548. return false;
  549. $data = $imagedata['sizes'][$size];
  550. // include the full filesystem path of the intermediate file
  551. if ( empty($data['path']) && !empty($data['file']) ) {
  552. $file_url = wp_get_attachment_url($post_id);
  553. $data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
  554. $data['url'] = path_join( dirname($file_url), $data['file'] );
  555. }
  556. return $data;
  557. }
  558. /**
  559. * Get the available image sizes
  560. * @since 3.0.0
  561. * @return array Returns a filtered array of image size strings
  562. */
  563. function get_intermediate_image_sizes() {
  564. global $_wp_additional_image_sizes;
  565. $image_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes
  566. if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
  567. $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
  568. /**
  569. * Filter the list of intermediate image sizes.
  570. *
  571. * @since 2.5.0
  572. *
  573. * @param array $image_sizes An array of intermediate image sizes. Defaults
  574. * are 'thumbnail', 'medium', 'large'.
  575. */
  576. return apply_filters( 'intermediate_image_sizes', $image_sizes );
  577. }
  578. /**
  579. * Retrieve an image to represent an attachment.
  580. *
  581. * A mime icon for files, thumbnail or intermediate size for images.
  582. *
  583. * @since 2.5.0
  584. *
  585. * @param int $attachment_id Image attachment ID.
  586. * @param string $size Optional, default is 'thumbnail'.
  587. * @param bool $icon Optional, default is false. Whether it is an icon.
  588. * @return bool|array Returns an array (url, width, height), or false, if no image is available.
  589. */
  590. function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
  591. // get a thumbnail or intermediate image if there is one
  592. if ( $image = image_downsize($attachment_id, $size) )
  593. return $image;
  594. $src = false;
  595. if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
  596. /** This filter is documented in wp-includes/post.php */
  597. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );
  598. $src_file = $icon_dir . '/' . wp_basename($src);
  599. @list($width, $height) = getimagesize($src_file);
  600. }
  601. if ( $src && $width && $height )
  602. return array( $src, $width, $height );
  603. return false;
  604. }
  605. /**
  606. * Get an HTML img element representing an image attachment
  607. *
  608. * While $size will accept an array, it is better to register a size with
  609. * add_image_size() so that a cropped version is generated. It's much more
  610. * efficient than having to find the closest-sized image and then having the
  611. * browser scale down the image.
  612. *
  613. * @since 2.5.0
  614. *
  615. * @see add_image_size()
  616. * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions
  617. *
  618. * @param int $attachment_id Image attachment ID.
  619. * @param string $size Optional, default is 'thumbnail'.
  620. * @param bool $icon Optional, default is false. Whether it is an icon.
  621. * @param mixed $attr Optional, attributes for the image markup.
  622. * @return string HTML img element or empty string on failure.
  623. */
  624. function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
  625. $html = '';
  626. $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
  627. if ( $image ) {
  628. list($src, $width, $height) = $image;
  629. $hwstring = image_hwstring($width, $height);
  630. if ( is_array($size) )
  631. $size = join('x', $size);
  632. $attachment = get_post($attachment_id);
  633. $default_attr = array(
  634. 'src' => $src,
  635. 'class' => "attachment-$size",
  636. 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
  637. );
  638. if ( empty($default_attr['alt']) )
  639. $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
  640. if ( empty($default_attr['alt']) )
  641. $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
  642. $attr = wp_parse_args($attr, $default_attr);
  643. /**
  644. * Filter the list of attachment image attributes.
  645. *
  646. * @since 2.8.0
  647. *
  648. * @param mixed $attr Attributes for the image markup.
  649. * @param int $attachment_id Image attachment ID.
  650. */
  651. $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
  652. $attr = array_map( 'esc_attr', $attr );
  653. $html = rtrim("<img $hwstring");
  654. foreach ( $attr as $name => $value ) {
  655. $html .= " $name=" . '"' . $value . '"';
  656. }
  657. $html .= ' />';
  658. }
  659. return $html;
  660. }
  661. /**
  662. * Adds a 'wp-post-image' class to post thumbnails
  663. * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to
  664. * dynamically add/remove itself so as to only filter post thumbnails
  665. *
  666. * @since 2.9.0
  667. * @param array $attr Attributes including src, class, alt, title
  668. * @return array
  669. */
  670. function _wp_post_thumbnail_class_filter( $attr ) {
  671. $attr['class'] .= ' wp-post-image';
  672. return $attr;
  673. }
  674. /**
  675. * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter
  676. *
  677. * @since 2.9.0
  678. */
  679. function _wp_post_thumbnail_class_filter_add( $attr ) {
  680. add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
  681. }
  682. /**
  683. * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter
  684. *
  685. * @since 2.9.0
  686. */
  687. function _wp_post_thumbnail_class_filter_remove( $attr ) {
  688. remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
  689. }
  690. add_shortcode('wp_caption', 'img_caption_shortcode');
  691. add_shortcode('caption', 'img_caption_shortcode');
  692. /**
  693. * The Caption shortcode.
  694. *
  695. * Allows a plugin to replace the content that would otherwise be returned. The
  696. * filter is 'img_caption_shortcode' and passes an empty string, the attr
  697. * parameter and the content parameter values.
  698. *
  699. * The supported attributes for the shortcode are 'id', 'align', 'width', and
  700. * 'caption'.
  701. *
  702. * @since 2.6.0
  703. *
  704. * @param array $attr {
  705. * Attributes of the caption shortcode.
  706. *
  707. * @type string $id ID of the div element for the caption.
  708. * @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft',
  709. * 'aligncenter', alignright', 'alignnone'.
  710. * @type int $width The width of the caption, in pixels.
  711. * @type string $caption The caption text.
  712. * @type string $class Additional class name(s) added to the caption container.
  713. * }
  714. * @param string $content Optional. Shortcode content.
  715. * @return string HTML content to display the caption.
  716. */
  717. function img_caption_shortcode( $attr, $content = null ) {
  718. // New-style shortcode with the caption inside the shortcode with the link and image tags.
  719. if ( ! isset( $attr['caption'] ) ) {
  720. if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
  721. $content = $matches[1];
  722. $attr['caption'] = trim( $matches[2] );
  723. }
  724. }
  725. /**
  726. * Filter the default caption shortcode output.
  727. *
  728. * If the filtered output isn't empty, it will be used instead of generating
  729. * the default caption template.
  730. *
  731. * @since 2.6.0
  732. *
  733. * @see img_caption_shortcode()
  734. *
  735. * @param string $output The caption output. Default empty.
  736. * @param array $attr Attributes of the caption shortcode.
  737. * @param string $content The image element, possibly wrapped in a hyperlink.
  738. */
  739. $output = apply_filters( 'img_caption_shortcode', '', $attr, $content );
  740. if ( $output != '' )
  741. return $output;
  742. $atts = shortcode_atts( array(
  743. 'id' => '',
  744. 'align' => 'alignnone',
  745. 'width' => '',
  746. 'caption' => '',
  747. 'class' => '',
  748. ), $attr, 'caption' );
  749. $atts['width'] = (int) $atts['width'];
  750. if ( $atts['width'] < 1 || empty( $atts['caption'] ) )
  751. return $content;
  752. if ( ! empty( $atts['id'] ) )
  753. $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
  754. $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] );
  755. if ( current_theme_supports( 'html5', 'caption' ) ) {
  756. return '<figure ' . $atts['id'] . 'style="width: ' . (int) $atts['width'] . 'px;" class="' . esc_attr( $class ) . '">'
  757. . do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption></figure>';
  758. }
  759. $caption_width = 10 + $atts['width'];
  760. /**
  761. * Filter the width of an image's caption.
  762. *
  763. * By default, the caption is 10 pixels greater than the width of the image,
  764. * to prevent post content from running up against a floated image.
  765. *
  766. * @since 3.7.0
  767. *
  768. * @see img_caption_shortcode()
  769. *
  770. * @param int $caption_width Width of the caption in pixels. To remove this inline style,
  771. * return zero.
  772. * @param array $atts Attributes of the caption shortcode.
  773. * @param string $content The image element, possibly wrapped in a hyperlink.
  774. */
  775. $caption_width = apply_filters( 'img_caption_shortcode_width', $caption_width, $atts, $content );
  776. $style = '';
  777. if ( $caption_width )
  778. $style = 'style="width: ' . (int) $caption_width . 'px" ';
  779. return '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">'
  780. . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>';
  781. }
  782. add_shortcode('gallery', 'gallery_shortcode');
  783. /**
  784. * The Gallery shortcode.
  785. *
  786. * This implements the functionality of the Gallery Shortcode for displaying
  787. * WordPress images on a post.
  788. *
  789. * @since 2.5.0
  790. *
  791. * @param array $attr {
  792. * Attributes of the gallery shortcode.
  793. *
  794. * @type string $order Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'.
  795. * @type string $orderby The field to use when ordering the images. Default 'menu_order ID'.
  796. * Accepts any valid SQL ORDERBY statement.
  797. * @type int $id Post ID.
  798. * @type string $itemtag HTML tag to use for each image in the gallery.
  799. * Default 'dl', or 'figure' when the theme registers HTML5 gallery support.
  800. * @type string $icontag HTML tag to use for each image's icon.
  801. * Default 'dt', or 'div' when the theme registers HTML5 gallery support.
  802. * @type string $captiontag HTML tag to use for each image's caption.
  803. * Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support.
  804. * @type int $columns Number of columns of images to display. Default 3.
  805. * @type string $size Size of the images to display. Default 'thumbnail'.
  806. * @type string $ids A comma-separated list of IDs of attachments to display. Default empty.
  807. * @type string $include A comma-separated list of IDs of attachments to include. Default empty.
  808. * @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty.
  809. * @type string $link What to link each image to. Default empty (links to the attachment page).
  810. * Accepts 'file', 'none'.
  811. * }
  812. * @return string HTML content to display gallery.
  813. */
  814. function gallery_shortcode( $attr ) {
  815. $post = get_post();
  816. static $instance = 0;
  817. $instance++;
  818. if ( ! empty( $attr['ids'] ) ) {
  819. // 'ids' is explicitly ordered, unless you specify otherwise.
  820. if ( empty( $attr['orderby'] ) )
  821. $attr['orderby'] = 'post__in';
  822. $attr['include'] = $attr['ids'];
  823. }
  824. /**
  825. * Filter the default gallery shortcode output.
  826. *
  827. * If the filtered output isn't empty, it will be used instead of generating
  828. * the default gallery template.
  829. *
  830. * @since 2.5.0
  831. *
  832. * @see gallery_shortcode()
  833. *
  834. * @param string $output The gallery output. Default empty.
  835. * @param array $attr Attributes of the gallery shortcode.
  836. */
  837. $output = apply_filters( 'post_gallery', '', $attr );
  838. if ( $output != '' )
  839. return $output;
  840. // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
  841. if ( isset( $attr['orderby'] ) ) {
  842. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  843. if ( !$attr['orderby'] )
  844. unset( $attr['orderby'] );
  845. }
  846. $html5 = current_theme_supports( 'html5', 'gallery' );
  847. extract(shortcode_atts(array(
  848. 'order' => 'ASC',
  849. 'orderby' => 'menu_order ID',
  850. 'id' => $post ? $post->ID : 0,
  851. 'itemtag' => $html5 ? 'figure' : 'dl',
  852. 'icontag' => $html5 ? 'div' : 'dt',
  853. 'captiontag' => $html5 ? 'figcaption' : 'dd',
  854. 'columns' => 3,
  855. 'size' => 'thumbnail',
  856. 'include' => '',
  857. 'exclude' => '',
  858. 'link' => ''
  859. ), $attr, 'gallery'));
  860. $id = intval($id);
  861. if ( 'RAND' == $order )
  862. $orderby = 'none';
  863. if ( !empty($include) ) {
  864. $_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  865. $attachments = array();
  866. foreach ( $_attachments as $key => $val ) {
  867. $attachments[$val->ID] = $_attachments[$key];
  868. }
  869. } elseif ( !empty($exclude) ) {
  870. $attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  871. } else {
  872. $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
  873. }
  874. if ( empty($attachments) )
  875. return '';
  876. if ( is_feed() ) {
  877. $output = "\n";
  878. foreach ( $attachments as $att_id => $attachment )
  879. $output .= wp_get_attachment_link($att_id, $size, true) . "\n";
  880. return $output;
  881. }
  882. $itemtag = tag_escape($itemtag);
  883. $captiontag = tag_escape($captiontag);
  884. $icontag = tag_escape($icontag);
  885. $valid_tags = wp_kses_allowed_html( 'post' );
  886. if ( ! isset( $valid_tags[ $itemtag ] ) )
  887. $itemtag = 'dl';
  888. if ( ! isset( $valid_tags[ $captiontag ] ) )
  889. $captiontag = 'dd';
  890. if ( ! isset( $valid_tags[ $icontag ] ) )
  891. $icontag = 'dt';
  892. $columns = intval($columns);
  893. $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
  894. $float = is_rtl() ? 'right' : 'left';
  895. $selector = "gallery-{$instance}";
  896. $gallery_style = $gallery_div = '';
  897. /**
  898. * Filter whether to print default gallery styles.
  899. *
  900. * @since 3.1.0
  901. *
  902. * @param bool $print Whether to print default gallery styles.
  903. * Defaults to false if the theme supports HTML5 galleries.
  904. * Otherwise, defaults to true.
  905. */
  906. if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) {
  907. $gallery_style = "
  908. <style type='text/css'>
  909. #{$selector} {
  910. margin: auto;
  911. }
  912. #{$selector} .gallery-item {
  913. float: {$float};
  914. margin-top: 10px;
  915. text-align: center;
  916. width: {$itemwidth}%;
  917. }
  918. #{$selector} img {
  919. border: 2px solid #cfcfcf;
  920. }
  921. #{$selector} .gallery-caption {
  922. margin-left: 0;
  923. }
  924. /* see gallery_shortcode() in wp-includes/media.php */
  925. </style>\n\t\t";
  926. }
  927. $size_class = sanitize_html_class( $size );
  928. $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
  929. /**
  930. * Filter the default gallery shortcode CSS styles.
  931. *
  932. * @since 2.5.0
  933. *
  934. * @param string $gallery_style Default gallery shortcode CSS styles.
  935. * @param string $gallery_div Opening HTML div container for the gallery shortcode output.
  936. */
  937. $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div );
  938. $i = 0;
  939. foreach ( $attachments as $id => $attachment ) {
  940. if ( ! empty( $link ) && 'file' === $link )
  941. $image_output = wp_get_attachment_link( $id, $size, false, false );
  942. elseif ( ! empty( $link ) && 'none' === $link )
  943. $image_output = wp_get_attachment_image( $id, $size, false );
  944. else
  945. $image_output = wp_get_attachment_link( $id, $size, true, false );
  946. $image_meta = wp_get_attachment_metadata( $id );
  947. $orientation = '';
  948. if ( isset( $image_meta['height'], $image_meta['width'] ) )
  949. $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
  950. $output .= "<{$itemtag} class='gallery-item'>";
  951. $output .= "
  952. <{$icontag} class='gallery-icon {$orientation}'>
  953. $image_output
  954. </{$icontag}>";
  955. if ( $captiontag && trim($attachment->post_excerpt) ) {
  956. $output .= "
  957. <{$captiontag} class='wp-caption-text gallery-caption'>
  958. " . wptexturize($attachment->post_excerpt) . "
  959. </{$captiontag}>";
  960. }
  961. $output .= "</{$itemtag}>";
  962. if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) {
  963. $output .= '<br style="clear: both" />';
  964. }
  965. }
  966. if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) {
  967. $output .= "
  968. <br style='clear: both' />";
  969. }
  970. $output .= "
  971. </div>\n";
  972. return $output;
  973. }
  974. /**
  975. * Output the templates used by playlists.
  976. *
  977. * @since 3.9.0
  978. */
  979. function wp_underscore_playlist_templates() {
  980. ?>
  981. <script type="text/html" id="tmpl-wp-playlist-current-item">
  982. <# if ( data.image ) { #>
  983. <img src="{{ data.thumb.src }}"/>
  984. <# } #>
  985. <div class="wp-playlist-caption">
  986. <span class="wp-playlist-item-meta wp-playlist-item-title">&#8220;{{ data.title }}&#8221;</span>
  987. <# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #>
  988. <# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #>
  989. </div>
  990. </script>
  991. <script type="text/html" id="tmpl-wp-playlist-item">
  992. <div class="wp-playlist-item">
  993. <a class="wp-playlist-caption" href="{{ data.src }}">
  994. {{ data.index ? ( data.index + '. ' ) : '' }}
  995. <# if ( data.caption ) { #>
  996. {{ data.caption }}
  997. <# } else { #>
  998. <span class="wp-playlist-item-title">&#8220;{{{ data.title }}}&#8221;</span>
  999. <# if ( data.artists && data.meta.artist ) { #>
  1000. <span class="wp-playlist-item-artist"> &mdash; {{ data.meta.artist }}</span>
  1001. <# } #>
  1002. <# } #>
  1003. </a>
  1004. <# if ( data.meta.length_formatted ) { #>
  1005. <div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div>
  1006. <# } #>
  1007. </div>
  1008. </script>
  1009. <?php
  1010. }
  1011. /**
  1012. * Output and enqueue default scripts and styles for playlists.
  1013. *
  1014. * @since 3.9.0
  1015. *
  1016. * @param string $type Type of playlist. Accepts 'audio' or 'video'.
  1017. */
  1018. function wp_playlist_scripts( $type ) {
  1019. wp_enqueue_style( 'wp-mediaelement' );
  1020. wp_enqueue_script( 'wp-playlist' );
  1021. ?>
  1022. <!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ) ?>');</script><![endif]-->
  1023. <?php
  1024. add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 );
  1025. add_action( 'admin_footer', 'wp_underscore_playlist_templates', 0 );
  1026. }
  1027. add_action( 'wp_playlist_scripts', 'wp_playlist_scripts' );
  1028. /**
  1029. * The playlist shortcode.
  1030. *
  1031. * This implements the functionality of the playlist shortcode for displaying
  1032. * a collection of WordPress audio or video files in a post.
  1033. *
  1034. * @since 3.9.0
  1035. *
  1036. * @param array $attr Playlist shortcode attributes.
  1037. * @return string Playlist output. Empty string if the passed type is unsupported.
  1038. */
  1039. function wp_playlist_shortcode( $attr ) {
  1040. global $content_width;
  1041. $post = get_post();
  1042. static $instance = 0;
  1043. $instance++;
  1044. if ( ! empty( $attr['ids'] ) ) {
  1045. // 'ids' is explicitly ordered, unless you specify otherwise.
  1046. if ( empty( $attr['orderby'] ) ) {
  1047. $attr['orderby'] = 'post__in';
  1048. }
  1049. $attr['include'] = $attr['ids'];
  1050. }
  1051. /**
  1052. * Filter the playlist output.
  1053. *
  1054. * Passing a non-empty value to the filter will short-circuit generation
  1055. * of the default playlist output, returning the passed value instead.
  1056. *
  1057. * @since 3.9.0
  1058. *
  1059. * @param string $output Playlist output. Default empty.
  1060. * @param array $attr An array of shortcode attributes.
  1061. */
  1062. $output = apply_filters( 'post_playlist', '', $attr );
  1063. if ( $output != '' ) {
  1064. return $output;
  1065. }
  1066. /*
  1067. * We're trusting author input, so let's at least make sure it looks
  1068. * like a valid orderby statement.
  1069. */
  1070. if ( isset( $attr['orderby'] ) ) {
  1071. $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
  1072. if ( ! $attr['orderby'] )
  1073. unset( $attr['orderby'] );
  1074. }
  1075. extract( shortcode_atts( array(
  1076. 'type' => 'audio',
  1077. 'order' => 'ASC',
  1078. 'orderby' => 'menu_order ID',
  1079. 'id' => $post ? $post->ID : 0,
  1080. 'include' => '',
  1081. 'exclude' => '',
  1082. 'style' => 'light',
  1083. 'tracklist' => true,
  1084. 'tracknumbers' => true,
  1085. 'images' => true,
  1086. 'artists' => true
  1087. ), $attr, 'playlist' ) );
  1088. $id = intval( $id );
  1089. if ( 'RAND' == $order ) {
  1090. $orderby = 'none';
  1091. }
  1092. $args = array(
  1093. 'post_status' => 'inherit',
  1094. 'post_type' => 'attachment',
  1095. 'post_mime_type' => $type,
  1096. 'order' => $order,
  1097. 'orderby' => $orderby
  1098. );
  1099. if ( ! empty( $include ) ) {
  1100. $args['include'] = $include;
  1101. $_attachments = get_posts( $args );
  1102. $attachments = array();
  1103. foreach ( $_attachments as $key => $val ) {
  1104. $attachments[$val->ID] = $_attachments[$key];
  1105. }
  1106. } elseif ( ! empty( $exclude ) ) {
  1107. $args['post_parent'] = $id;
  1108. $args['exclude'] = $exclude;
  1109. $attachments = get_children( $args );
  1110. } else {
  1111. $args['post_parent'] = $id;
  1112. $attachments = get_children( $args );
  1113. }
  1114. if ( empty( $attachments ) ) {
  1115. return '';
  1116. }
  1117. if ( is_feed() ) {
  1118. $output = "\n";
  1119. foreach ( $attachments as $att_id => $attachment ) {
  1120. $output .= wp_get_attachment_link( $att_id ) . "\n";
  1121. }
  1122. return $output;
  1123. }
  1124. $outer = 22; // default padding and border of wrapper
  1125. $default_width = 640;
  1126. $default_height = 360;
  1127. $theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer );
  1128. $theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width );
  1129. $data = compact( 'type' );
  1130. // don't pass strings to JSON, will be truthy in JS
  1131. foreach ( array( 'tracklist', 'tracknumbers', 'images', 'artists' ) as $key ) {
  1132. $data[$key] = filter_var( $$key, FILTER_VALIDATE_BOOLEAN );
  1133. }
  1134. $tracks = array();
  1135. foreach ( $attachments as $attachment ) {
  1136. $url = wp_get_attachment_url( $attachment->ID );
  1137. $ftype = wp_check_filetype( $url, wp_get_mime_types() );
  1138. $track = array(
  1139. 'src' => $url,
  1140. 'type' => $ftype['type'],
  1141. 'title' => $attachment->post_title,
  1142. 'caption' => $attachment->post_excerpt,
  1143. 'description' => $attachment->post_content
  1144. );
  1145. $track['meta'] = array();
  1146. $meta = wp_get_attachment_metadata( $attachment->ID );
  1147. if ( ! empty( $meta ) ) {
  1148. foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) {
  1149. if ( ! empty( $meta[ $key ] ) ) {
  1150. $track['meta'][ $key ] = $meta[ $key ];
  1151. }
  1152. }
  1153. if ( 'video' === $type ) {
  1154. if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) {
  1155. $width = $meta['width'];
  1156. $height = $meta['height'];
  1157. $theme_height = round( ( $height * $theme_width ) / $width );
  1158. } else {
  1159. $width = $default_width;
  1160. $height = $default_height;
  1161. }
  1162. $track['dimensions'] = array(
  1163. 'original' => compact( 'width', 'height' ),
  1164. 'resized' => array(
  1165. 'width' => $theme_width,
  1166. 'height' => $theme_height
  1167. )
  1168. );
  1169. }
  1170. }
  1171. if ( $images ) {
  1172. $id = get_post_thumbnail_id( $attachment->ID );
  1173. if ( ! empty( $id ) ) {
  1174. list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' );
  1175. $track['image'] = compact( 'src', 'width', 'height' );
  1176. list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' );
  1177. $track['thumb'] = compact( 'src', 'width', 'height' );
  1178. } else {
  1179. $src = wp_mime_type_icon( $attachment->ID );
  1180. $width = 48;
  1181. $height = 64;
  1182. $track['image'] = compact( 'src', 'width', 'height' );
  1183. $track['thumb'] = compact( 'src', 'width', 'height' );
  1184. }
  1185. }
  1186. $tracks[] = $track;
  1187. }
  1188. $data['tracks'] = $tracks;
  1189. $safe_type = esc_attr( $type );
  1190. $safe_style = esc_attr( $style );
  1191. ob_start();
  1192. if ( 1 === $instance ) {
  1193. /**
  1194. * Print and enqueue playlist scripts, styles, and JavaScript templates.
  1195. *
  1196. * @since 3.9.0
  1197. *
  1198. * @param string $type Type of playlist. Possible values are 'audio' or 'video'.
  1199. * @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'.
  1200. */
  1201. do_action( 'wp_playlist_scripts', $type, $style );
  1202. } ?>
  1203. <div class="wp-playlist wp-<?php echo $safe_type ?>-playlist wp-playlist-<?php echo $safe_style ?>">
  1204. <?php if ( 'audio' === $type ): ?>
  1205. <div class="wp-playlist-current-item"></div>
  1206. <?php endif ?>
  1207. <<?php echo $safe_type ?> controls="controls" preload="none" width="<?php
  1208. echo (int) $theme_width;
  1209. ?>"<?php if ( 'video' === $safe_type ):
  1210. echo ' height="', (int) $theme_height, '"';
  1211. endif; ?>></<?php echo $safe_type ?>>
  1212. <div class="wp-playlist-next"></div>
  1213. <div class="wp-playlist-prev"></div>
  1214. <noscript>
  1215. <ol><?php
  1216. foreach ( $attachments as $att_id => $attachment ) {
  1217. printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) );
  1218. }
  1219. ?></ol>
  1220. </noscript>
  1221. <script type="application/json"><?php echo json_encode( $data ) ?></script>
  1222. </div>
  1223. <?php
  1224. return ob_get_clean();
  1225. }
  1226. add_shortcode( 'playlist', 'wp_playlist_shortcode' );
  1227. /**
  1228. * Provide a No-JS Flash fallback as a last resort for audio / video
  1229. *
  1230. * @since 3.6.0
  1231. *
  1232. * @param string $url
  1233. * @return string Fallback HTML
  1234. */
  1235. function wp_mediaelement_fallback( $url ) {
  1236. /**
  1237. * Filter the Mediaelement fallback output for no-JS.
  1238. *
  1239. * @since 3.6.0
  1240. *
  1241. * @param string $output Fallback output for no-JS.
  1242. * @param string $url Media file URL.
  1243. */
  1244. return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url );
  1245. }
  1246. /**
  1247. * Return a filtered list of WP-supported audio formats.
  1248. *
  1249. * @since 3.6.0
  1250. * @return array
  1251. */
  1252. function wp_get_audio_extensions() {
  1253. /**
  1254. * Filter the list of supported audio formats.
  1255. *
  1256. * @since 3.6.0
  1257. *
  1258. * @param array $extensions An array of support audio formats. Defaults are
  1259. * 'mp3', 'ogg', 'wma', 'm4a', 'wav'.
  1260. */
  1261. return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'wma', 'm4a', 'wav' ) );
  1262. }
  1263. /**
  1264. * Return useful keys to use to lookup data from an attachment's stored metadata.
  1265. *
  1266. * @since 3.9.0
  1267. *
  1268. * @param WP_Post $attachment The current attachment, provided for context.
  1269. * @param string $context The context. Accepts 'edit', 'display'. Default 'display'.
  1270. * @return array Key/value pairs of field keys to labels.
  1271. */
  1272. function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) {
  1273. $fields = array(
  1274. 'artist' => __( 'Artist' ),
  1275. 'album' => __( 'Album' ),
  1276. );
  1277. if ( 'display' === $context ) {
  1278. $fields['genre'] = __( 'Genre' );
  1279. $fields['year'] = __( 'Year' );
  1280. $fields['length_formatted'] = _x( 'Length', 'video or audio' );
  1281. }
  1282. /**
  1283. * Filter the editable list of keys to look up data from an attachment's metadata.
  1284. *
  1285. * @since 3.9.0
  1286. *
  1287. * @param array $fields Key/value pairs of field keys to labels.
  1288. * @param WP_Post $attachment A…

Large files files are truncated, but you can click here to view the full file