PageRenderTime 69ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/media.php

https://gitlab.com/geyson/geyson
PHP | 3474 lines | 1661 code | 362 blank | 1451 comment | 331 complexity | 8deb5434efed8a0ddc9ae25c26797401 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0

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

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