/wp-includes/media.php

  1. <?php 
  2. /** 
  3. * WordPress API for media display. 
  4. * 
  5. * @package WordPress 
  6. * @subpackage Media 
  7. */ 
  8.  
  9. /** 
  10. * Retrieve additional image sizes. 
  11. * 
  12. * @since 4.7.0 
  13. * 
  14. * @global array $_wp_additional_image_sizes 
  15. * 
  16. * @return array Additional images size data. 
  17. */ 
  18. function wp_get_additional_image_sizes() { 
  19. global $_wp_additional_image_sizes; 
  20. if ( ! $_wp_additional_image_sizes ) { 
  21. $_wp_additional_image_sizes = array(); 
  22. return $_wp_additional_image_sizes; 
  23.  
  24. /** 
  25. * Scale down the default size of an image. 
  26. * 
  27. * This is so that the image is a better fit for the editor and theme. 
  28. * 
  29. * The `$size` parameter accepts either an array or a string. The supported string 
  30. * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at 
  31. * 128 width and 96 height in pixels. Also supported for the string value is 
  32. * 'medium', 'medium_large' and 'full'. The 'full' isn't actually supported, but any value other 
  33. * than the supported will result in the content_width size or 500 if that is 
  34. * not set. 
  35. * 
  36. * Finally, there is a filter named {@see 'editor_max_image_size'}, that will be 
  37. * called on the calculated array for width and height, respectively. The second 
  38. * parameter will be the value that was in the $size parameter. The returned 
  39. * type for the hook is an array with the width as the first element and the 
  40. * height as the second element. 
  41. * 
  42. * @since 2.5.0 
  43. * 
  44. * @global int $content_width 
  45. * 
  46. * @param int $width Width of the image in pixels. 
  47. * @param int $height Height of the image in pixels. 
  48. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array 
  49. * of width and height values in pixels (in that order). 
  50. * Default 'medium'. 
  51. * @param string $context Optional. Could be 'display' (like in a theme) or 'edit' 
  52. * (like inserting into an editor). Default null. 
  53. * @return array Width and height of what the result image should resize to. 
  54. */ 
  55. function image_constrain_size_for_editor( $width, $height, $size = 'medium', $context = null ) { 
  56. global $content_width; 
  57.  
  58. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  59.  
  60. if ( ! $context ) 
  61. $context = is_admin() ? 'edit' : 'display'; 
  62.  
  63. if ( is_array($size) ) { 
  64. $max_width = $size[0]; 
  65. $max_height = $size[1]; 
  66. elseif ( $size == 'thumb' || $size == 'thumbnail' ) { 
  67. $max_width = intval(get_option('thumbnail_size_w')); 
  68. $max_height = intval(get_option('thumbnail_size_h')); 
  69. // last chance thumbnail size defaults 
  70. if ( !$max_width && !$max_height ) { 
  71. $max_width = 128; 
  72. $max_height = 96; 
  73. elseif ( $size == 'medium' ) { 
  74. $max_width = intval(get_option('medium_size_w')); 
  75. $max_height = intval(get_option('medium_size_h')); 
  76.  
  77. elseif ( $size == 'medium_large' ) { 
  78. $max_width = intval( get_option( 'medium_large_size_w' ) ); 
  79. $max_height = intval( get_option( 'medium_large_size_h' ) ); 
  80.  
  81. if ( intval( $content_width ) > 0 ) { 
  82. $max_width = min( intval( $content_width ), $max_width ); 
  83. elseif ( $size == 'large' ) { 
  84. /** 
  85. * We're inserting a large size image into the editor. If it's a really 
  86. * big image we'll scale it down to fit reasonably within the editor 
  87. * itself, and within the theme's content width if it's known. The user 
  88. * can resize it in the editor if they wish. 
  89. */ 
  90. $max_width = intval(get_option('large_size_w')); 
  91. $max_height = intval(get_option('large_size_h')); 
  92. if ( intval($content_width) > 0 ) { 
  93. $max_width = min( intval($content_width), $max_width ); 
  94. } elseif ( ! empty( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) { 
  95. $max_width = intval( $_wp_additional_image_sizes[$size]['width'] ); 
  96. $max_height = intval( $_wp_additional_image_sizes[$size]['height'] ); 
  97. // Only in admin. Assume that theme authors know what they're doing. 
  98. if ( intval( $content_width ) > 0 && 'edit' === $context ) { 
  99. $max_width = min( intval( $content_width ), $max_width ); 
  100. // $size == 'full' has no constraint 
  101. else { 
  102. $max_width = $width; 
  103. $max_height = $height; 
  104.  
  105. /** 
  106. * Filters the maximum image size dimensions for the editor. 
  107. * 
  108. * @since 2.5.0 
  109. * 
  110. * @param array $max_image_size An array with the width as the first element,  
  111. * and the height as the second element. 
  112. * @param string|array $size Size of what the result image should be. 
  113. * @param string $context The context the image is being resized for. 
  114. * Possible values are 'display' (like in a theme) 
  115. * or 'edit' (like inserting into an editor). 
  116. */ 
  117. list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context ); 
  118.  
  119. return wp_constrain_dimensions( $width, $height, $max_width, $max_height ); 
  120.  
  121. /** 
  122. * Retrieve width and height attributes using given width and height values. 
  123. * 
  124. * Both attributes are required in the sense that both parameters must have a 
  125. * value, but are optional in that if you set them to false or null, then they 
  126. * will not be added to the returned string. 
  127. * 
  128. * You can set the value using a string, but it will only take numeric values. 
  129. * If you wish to put 'px' after the numbers, then it will be stripped out of 
  130. * the return. 
  131. * 
  132. * @since 2.5.0 
  133. * 
  134. * @param int|string $width Image width in pixels. 
  135. * @param int|string $height Image height in pixels. 
  136. * @return string HTML attributes for width and, or height. 
  137. */ 
  138. function image_hwstring( $width, $height ) { 
  139. $out = ''; 
  140. if ($width) 
  141. $out .= 'width="'.intval($width).'" '; 
  142. if ($height) 
  143. $out .= 'height="'.intval($height).'" '; 
  144. return $out; 
  145.  
  146. /** 
  147. * Scale an image to fit a particular size (such as 'thumb' or 'medium'). 
  148. * 
  149. * Array with image url, width, height, and whether is intermediate size, in 
  150. * that order is returned on success is returned. $is_intermediate is true if 
  151. * $url is a resized image, false if it is the original. 
  152. * 
  153. * The URL might be the original image, or it might be a resized version. This 
  154. * function won't create a new resized copy, it will just return an already 
  155. * resized one if it exists. 
  156. * 
  157. * A plugin may use the {@see 'image_downsize'} filter to hook into and offer image 
  158. * resizing services for images. The hook must return an array with the same 
  159. * elements that are returned in the function. The first element being the URL 
  160. * to the new image that was resized. 
  161. * 
  162. * @since 2.5.0 
  163. * 
  164. * @param int $id Attachment ID for image. 
  165. * @param array|string $size Optional. Image size to scale to. Accepts any valid image size,  
  166. * or an array of width and height values in pixels (in that order). 
  167. * Default 'medium'. 
  168. * @return false|array Array containing the image URL, width, height, and boolean for whether 
  169. * the image is an intermediate size. False on failure. 
  170. */ 
  171. function image_downsize( $id, $size = 'medium' ) { 
  172. $is_image = wp_attachment_is_image( $id ); 
  173.  
  174. /** 
  175. * Filters whether to preempt the output of image_downsize(). 
  176. * 
  177. * Passing a truthy value to the filter will effectively short-circuit 
  178. * down-sizing the image, returning that value as output instead. 
  179. * 
  180. * @since 2.5.0 
  181. * 
  182. * @param bool $downsize Whether to short-circuit the image downsize. Default false. 
  183. * @param int $id Attachment ID for image. 
  184. * @param array|string $size Size of image. Image size or array of width and height values (in that order). 
  185. * Default 'medium'. 
  186. */ 
  187. if ( $out = apply_filters( 'image_downsize', false, $id, $size ) ) { 
  188. return $out; 
  189.  
  190. $img_url = wp_get_attachment_url($id); 
  191. $meta = wp_get_attachment_metadata($id); 
  192. $width = $height = 0; 
  193. $is_intermediate = false; 
  194. $img_url_basename = wp_basename($img_url); 
  195.  
  196. // If the file isn't an image, attempt to replace its URL with a rendered image from its meta. 
  197. // Otherwise, a non-image type could be returned. 
  198. if ( ! $is_image ) { 
  199. if ( ! empty( $meta['sizes'] ) ) { 
  200. $img_url = str_replace( $img_url_basename, $meta['sizes']['full']['file'], $img_url ); 
  201. $img_url_basename = $meta['sizes']['full']['file']; 
  202. $width = $meta['sizes']['full']['width']; 
  203. $height = $meta['sizes']['full']['height']; 
  204. } else { 
  205. return false; 
  206.  
  207. // try for a new style intermediate size 
  208. if ( $intermediate = image_get_intermediate_size($id, $size) ) { 
  209. $img_url = str_replace($img_url_basename, $intermediate['file'], $img_url); 
  210. $width = $intermediate['width']; 
  211. $height = $intermediate['height']; 
  212. $is_intermediate = true; 
  213. elseif ( $size == 'thumbnail' ) { 
  214. // fall back to the old thumbnail 
  215. if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) { 
  216. $img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url); 
  217. $width = $info[0]; 
  218. $height = $info[1]; 
  219. $is_intermediate = true; 
  220. if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) { 
  221. // any other type: use the real image 
  222. $width = $meta['width']; 
  223. $height = $meta['height']; 
  224.  
  225. if ( $img_url) { 
  226. // we have the actual image size, but might need to further constrain it if content_width is narrower 
  227. list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size ); 
  228.  
  229. return array( $img_url, $width, $height, $is_intermediate ); 
  230. return false; 
  231.  
  232.  
  233. /** 
  234. * Register a new image size. 
  235. * 
  236. * Cropping behavior for the image size is dependent on the value of $crop: 
  237. * 1. If false (default), images will be scaled, not cropped. 
  238. * 2. If an array in the form of array( x_crop_position, y_crop_position ): 
  239. * - x_crop_position accepts 'left' 'center', or 'right'. 
  240. * - y_crop_position accepts 'top', 'center', or 'bottom'. 
  241. * Images will be cropped to the specified dimensions within the defined crop area. 
  242. * 3. If true, images will be cropped to the specified dimensions using center positions. 
  243. * 
  244. * @since 2.9.0 
  245. * 
  246. * @global array $_wp_additional_image_sizes Associative array of additional image sizes. 
  247. * 
  248. * @param string $name Image size identifier. 
  249. * @param int $width Image width in pixels. 
  250. * @param int $height Image height in pixels. 
  251. * @param bool|array $crop Optional. Whether to crop images to specified width and height or resize. 
  252. * An array can specify positioning of the crop area. Default false. 
  253. */ 
  254. function add_image_size( $name, $width = 0, $height = 0, $crop = false ) { 
  255. global $_wp_additional_image_sizes; 
  256.  
  257. $_wp_additional_image_sizes[ $name ] = array( 
  258. 'width' => absint( $width ),  
  259. 'height' => absint( $height ),  
  260. 'crop' => $crop,  
  261. ); 
  262.  
  263. /** 
  264. * Check if an image size exists. 
  265. * 
  266. * @since 3.9.0 
  267. * 
  268. * @param string $name The image size to check. 
  269. * @return bool True if the image size exists, false if not. 
  270. */ 
  271. function has_image_size( $name ) { 
  272. $sizes = wp_get_additional_image_sizes(); 
  273. return isset( $sizes[ $name ] ); 
  274.  
  275. /** 
  276. * Remove a new image size. 
  277. * 
  278. * @since 3.9.0 
  279. * 
  280. * @global array $_wp_additional_image_sizes 
  281. * 
  282. * @param string $name The image size to remove. 
  283. * @return bool True if the image size was successfully removed, false on failure. 
  284. */ 
  285. function remove_image_size( $name ) { 
  286. global $_wp_additional_image_sizes; 
  287.  
  288. if ( isset( $_wp_additional_image_sizes[ $name ] ) ) { 
  289. unset( $_wp_additional_image_sizes[ $name ] ); 
  290. return true; 
  291.  
  292. return false; 
  293.  
  294. /** 
  295. * Registers an image size for the post thumbnail. 
  296. * 
  297. * @since 2.9.0 
  298. * 
  299. * @see add_image_size() for details on cropping behavior. 
  300. * 
  301. * @param int $width Image width in pixels. 
  302. * @param int $height Image height in pixels. 
  303. * @param bool|array $crop Optional. Whether to crop images to specified width and height or resize. 
  304. * An array can specify positioning of the crop area. Default false. 
  305. */ 
  306. function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) { 
  307. add_image_size( 'post-thumbnail', $width, $height, $crop ); 
  308.  
  309. /** 
  310. * Gets an img tag for an image attachment, scaling it down if requested. 
  311. * 
  312. * The {@see 'get_image_tag_class'} filter allows for changing the class name for the 
  313. * image without having to use regular expressions on the HTML content. The 
  314. * parameters are: what WordPress will use for the class, the Attachment ID,  
  315. * image align value, and the size the image should be. 
  316. * 
  317. * The second filter, {@see 'get_image_tag'}, has the HTML content, which can then be 
  318. * further manipulated by a plugin to change all attribute values and even HTML 
  319. * content. 
  320. * 
  321. * @since 2.5.0 
  322. * 
  323. * @param int $id Attachment ID. 
  324. * @param string $alt Image Description for the alt attribute. 
  325. * @param string $title Image Description for the title attribute. 
  326. * @param string $align Part of the class name for aligning the image. 
  327. * @param string|array $size Optional. Registered image size to retrieve a tag for. Accepts any 
  328. * valid image size, or an array of width and height values in pixels 
  329. * (in that order). Default 'medium'. 
  330. * @return string HTML IMG element for given image attachment 
  331. */ 
  332. function get_image_tag( $id, $alt, $title, $align, $size = 'medium' ) { 
  333.  
  334. list( $img_src, $width, $height ) = image_downsize($id, $size); 
  335. $hwstring = image_hwstring($width, $height); 
  336.  
  337. $title = $title ? 'title="' . esc_attr( $title ) . '" ' : ''; 
  338.  
  339. $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; 
  340.  
  341. /** 
  342. * Filters the value of the attachment's image tag class attribute. 
  343. * 
  344. * @since 2.6.0 
  345. * 
  346. * @param string $class CSS class name or space-separated list of classes. 
  347. * @param int $id Attachment ID. 
  348. * @param string $align Part of the class name for aligning the image. 
  349. * @param string|array $size Size of image. Image size or array of width and height values (in that order). 
  350. * Default 'medium'. 
  351. */ 
  352. $class = apply_filters( 'get_image_tag_class', $class, $id, $align, $size ); 
  353.  
  354. $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />'; 
  355.  
  356. /** 
  357. * Filters the HTML content for the image tag. 
  358. * 
  359. * @since 2.6.0 
  360. * 
  361. * @param string $html HTML content for the image. 
  362. * @param int $id Attachment ID. 
  363. * @param string $alt Alternate text. 
  364. * @param string $title Attachment title. 
  365. * @param string $align Part of the class name for aligning the image. 
  366. * @param string|array $size Size of image. Image size or array of width and height values (in that order). 
  367. * Default 'medium'. 
  368. */ 
  369. return apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); 
  370.  
  371. /** 
  372. * Calculates the new dimensions for a down-sampled image. 
  373. * 
  374. * If either width or height are empty, no constraint is applied on 
  375. * that dimension. 
  376. * 
  377. * @since 2.5.0 
  378. * 
  379. * @param int $current_width Current width of the image. 
  380. * @param int $current_height Current height of the image. 
  381. * @param int $max_width Optional. Max width in pixels to constrain to. Default 0. 
  382. * @param int $max_height Optional. Max height in pixels to constrain to. Default 0. 
  383. * @return array First item is the width, the second item is the height. 
  384. */ 
  385. function wp_constrain_dimensions( $current_width, $current_height, $max_width = 0, $max_height = 0 ) { 
  386. if ( !$max_width && !$max_height ) 
  387. return array( $current_width, $current_height ); 
  388.  
  389. $width_ratio = $height_ratio = 1.0; 
  390. $did_width = $did_height = false; 
  391.  
  392. if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) { 
  393. $width_ratio = $max_width / $current_width; 
  394. $did_width = true; 
  395.  
  396. if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) { 
  397. $height_ratio = $max_height / $current_height; 
  398. $did_height = true; 
  399.  
  400. // Calculate the larger/smaller ratios 
  401. $smaller_ratio = min( $width_ratio, $height_ratio ); 
  402. $larger_ratio = max( $width_ratio, $height_ratio ); 
  403.  
  404. if ( (int) round( $current_width * $larger_ratio ) > $max_width || (int) round( $current_height * $larger_ratio ) > $max_height ) { 
  405. // The larger ratio is too big. It would result in an overflow. 
  406. $ratio = $smaller_ratio; 
  407. } else { 
  408. // The larger ratio fits, and is likely to be a more "snug" fit. 
  409. $ratio = $larger_ratio; 
  410.  
  411. // Very small dimensions may result in 0, 1 should be the minimum. 
  412. $w = max ( 1, (int) round( $current_width * $ratio ) ); 
  413. $h = max ( 1, (int) round( $current_height * $ratio ) ); 
  414.  
  415. // Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short 
  416. // 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. 
  417. // Thus we look for dimensions that are one pixel shy of the max value and bump them up 
  418.  
  419. // Note: $did_width means it is possible $smaller_ratio == $width_ratio. 
  420. if ( $did_width && $w == $max_width - 1 ) { 
  421. $w = $max_width; // Round it up 
  422.  
  423. // Note: $did_height means it is possible $smaller_ratio == $height_ratio. 
  424. if ( $did_height && $h == $max_height - 1 ) { 
  425. $h = $max_height; // Round it up 
  426.  
  427. /** 
  428. * Filters dimensions to constrain down-sampled images to. 
  429. * 
  430. * @since 4.1.0 
  431. * 
  432. * @param array $dimensions The image width and height. 
  433. * @param int $current_width The current width of the image. 
  434. * @param int $current_height The current height of the image. 
  435. * @param int $max_width The maximum width permitted. 
  436. * @param int $max_height The maximum height permitted. 
  437. */ 
  438. return apply_filters( 'wp_constrain_dimensions', array( $w, $h ), $current_width, $current_height, $max_width, $max_height ); 
  439.  
  440. /** 
  441. * Retrieves calculated resize dimensions for use in WP_Image_Editor. 
  442. * 
  443. * Calculates dimensions and coordinates for a resized image that fits 
  444. * within a specified width and height. 
  445. * 
  446. * Cropping behavior is dependent on the value of $crop: 
  447. * 1. If false (default), images will not be cropped. 
  448. * 2. If an array in the form of array( x_crop_position, y_crop_position ): 
  449. * - x_crop_position accepts 'left' 'center', or 'right'. 
  450. * - y_crop_position accepts 'top', 'center', or 'bottom'. 
  451. * Images will be cropped to the specified dimensions within the defined crop area. 
  452. * 3. If true, images will be cropped to the specified dimensions using center positions. 
  453. * 
  454. * @since 2.5.0 
  455. * 
  456. * @param int $orig_w Original width in pixels. 
  457. * @param int $orig_h Original height in pixels. 
  458. * @param int $dest_w New width in pixels. 
  459. * @param int $dest_h New height in pixels. 
  460. * @param bool|array $crop Optional. Whether to crop image to specified width and height or resize. 
  461. * An array can specify positioning of the crop area. Default false. 
  462. * @return false|array False on failure. Returned array matches parameters for `imagecopyresampled()`. 
  463. */ 
  464. function image_resize_dimensions( $orig_w, $orig_h, $dest_w, $dest_h, $crop = false ) { 
  465.  
  466. if ($orig_w <= 0 || $orig_h <= 0) 
  467. return false; 
  468. // at least one of dest_w or dest_h must be specific 
  469. if ($dest_w <= 0 && $dest_h <= 0) 
  470. return false; 
  471.  
  472. /** 
  473. * Filters whether to preempt calculating the image resize dimensions. 
  474. * 
  475. * Passing a non-null value to the filter will effectively short-circuit 
  476. * image_resize_dimensions(), returning that value instead. 
  477. * 
  478. * @since 3.4.0 
  479. * 
  480. * @param null|mixed $null Whether to preempt output of the resize dimensions. 
  481. * @param int $orig_w Original width in pixels. 
  482. * @param int $orig_h Original height in pixels. 
  483. * @param int $dest_w New width in pixels. 
  484. * @param int $dest_h New height in pixels. 
  485. * @param bool|array $crop Whether to crop image to specified width and height or resize. 
  486. * An array can specify positioning of the crop area. Default false. 
  487. */ 
  488. $output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop ); 
  489. if ( null !== $output ) 
  490. return $output; 
  491.  
  492. if ( $crop ) { 
  493. // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h 
  494. $aspect_ratio = $orig_w / $orig_h; 
  495. $new_w = min($dest_w, $orig_w); 
  496. $new_h = min($dest_h, $orig_h); 
  497.  
  498. if ( ! $new_w ) { 
  499. $new_w = (int) round( $new_h * $aspect_ratio ); 
  500.  
  501. if ( ! $new_h ) { 
  502. $new_h = (int) round( $new_w / $aspect_ratio ); 
  503.  
  504. $size_ratio = max($new_w / $orig_w, $new_h / $orig_h); 
  505.  
  506. $crop_w = round($new_w / $size_ratio); 
  507. $crop_h = round($new_h / $size_ratio); 
  508.  
  509. if ( ! is_array( $crop ) || count( $crop ) !== 2 ) { 
  510. $crop = array( 'center', 'center' ); 
  511.  
  512. list( $x, $y ) = $crop; 
  513.  
  514. if ( 'left' === $x ) { 
  515. $s_x = 0; 
  516. } elseif ( 'right' === $x ) { 
  517. $s_x = $orig_w - $crop_w; 
  518. } else { 
  519. $s_x = floor( ( $orig_w - $crop_w ) / 2 ); 
  520.  
  521. if ( 'top' === $y ) { 
  522. $s_y = 0; 
  523. } elseif ( 'bottom' === $y ) { 
  524. $s_y = $orig_h - $crop_h; 
  525. } else { 
  526. $s_y = floor( ( $orig_h - $crop_h ) / 2 ); 
  527. } else { 
  528. // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box 
  529. $crop_w = $orig_w; 
  530. $crop_h = $orig_h; 
  531.  
  532. $s_x = 0; 
  533. $s_y = 0; 
  534.  
  535. list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h ); 
  536.  
  537. // if the resulting image would be the same size or larger we don't want to resize it 
  538. if ( $new_w >= $orig_w && $new_h >= $orig_h && $dest_w != $orig_w && $dest_h != $orig_h ) { 
  539. return false; 
  540.  
  541. // the return array matches the parameters to imagecopyresampled() 
  542. // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h 
  543. return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h ); 
  544.  
  545.  
  546. /** 
  547. * Resizes an image to make a thumbnail or intermediate size. 
  548. * 
  549. * The returned array has the file size, the image width, and image height. The 
  550. * {@see 'image_make_intermediate_size'} filter can be used to hook in and change the 
  551. * values of the returned array. The only parameter is the resized file path. 
  552. * 
  553. * @since 2.5.0 
  554. * 
  555. * @param string $file File path. 
  556. * @param int $width Image width. 
  557. * @param int $height Image height. 
  558. * @param bool $crop Optional. Whether to crop image to specified width and height or resize. 
  559. * Default false. 
  560. * @return false|array False, if no image was created. Metadata array on success. 
  561. */ 
  562. function image_make_intermediate_size( $file, $width, $height, $crop = false ) { 
  563. if ( $width || $height ) { 
  564. $editor = wp_get_image_editor( $file ); 
  565.  
  566. if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) 
  567. return false; 
  568.  
  569. $resized_file = $editor->save(); 
  570.  
  571. if ( ! is_wp_error( $resized_file ) && $resized_file ) { 
  572. unset( $resized_file['path'] ); 
  573. return $resized_file; 
  574. return false; 
  575.  
  576. /** 
  577. * Helper function to test if aspect ratios for two images match. 
  578. * 
  579. * @since 4.6.0 
  580. * 
  581. * @param int $source_width Width of the first image in pixels. 
  582. * @param int $source_height Height of the first image in pixels. 
  583. * @param int $target_width Width of the second image in pixels. 
  584. * @param int $target_height Height of the second image in pixels. 
  585. * @return bool True if aspect ratios match within 1px. False if not. 
  586. */ 
  587. function wp_image_matches_ratio( $source_width, $source_height, $target_width, $target_height ) { 
  588. /** 
  589. * To test for varying crops, we constrain the dimensions of the larger image 
  590. * to the dimensions of the smaller image and see if they match. 
  591. */ 
  592. if ( $source_width > $target_width ) { 
  593. $constrained_size = wp_constrain_dimensions( $source_width, $source_height, $target_width ); 
  594. $expected_size = array( $target_width, $target_height ); 
  595. } else { 
  596. $constrained_size = wp_constrain_dimensions( $target_width, $target_height, $source_width ); 
  597. $expected_size = array( $source_width, $source_height ); 
  598.  
  599. // If the image dimensions are within 1px of the expected size, we consider it a match. 
  600. $matched = ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ); 
  601.  
  602. return $matched; 
  603.  
  604. /** 
  605. * Retrieves the image's intermediate size (resized) path, width, and height. 
  606. * 
  607. * The $size parameter can be an array with the width and height respectively. 
  608. * If the size matches the 'sizes' metadata array for width and height, then it 
  609. * will be used. If there is no direct match, then the nearest image size larger 
  610. * than the specified size will be used. If nothing is found, then the function 
  611. * will break out and return false. 
  612. * 
  613. * The metadata 'sizes' is used for compatible sizes that can be used for the 
  614. * parameter $size value. 
  615. * 
  616. * The url path will be given, when the $size parameter is a string. 
  617. * 
  618. * If you are passing an array for the $size, you should consider using 
  619. * add_image_size() so that a cropped version is generated. It's much more 
  620. * efficient than having to find the closest-sized image and then having the 
  621. * browser scale down the image. 
  622. * 
  623. * @since 2.5.0 
  624. * 
  625. * @param int $post_id Attachment ID. 
  626. * @param array|string $size Optional. Image size. Accepts any valid image size, or an array 
  627. * of width and height values in pixels (in that order). 
  628. * Default 'thumbnail'. 
  629. * @return false|array $data { 
  630. * Array of file relative path, width, and height on success. Additionally includes absolute 
  631. * path and URL if registered size is passed to $size parameter. False on failure. 
  632. * 
  633. * @type string $file Image's path relative to uploads directory 
  634. * @type int $width Width of image 
  635. * @type int $height Height of image 
  636. * @type string $path Image's absolute filesystem path. 
  637. * @type string $url Image's URL. 
  638. * } 
  639. */ 
  640. function image_get_intermediate_size( $post_id, $size = 'thumbnail' ) { 
  641. if ( ! $size || ! is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) || empty( $imagedata['sizes'] ) ) { 
  642. return false; 
  643.  
  644. $data = array(); 
  645.  
  646. // Find the best match when '$size' is an array. 
  647. if ( is_array( $size ) ) { 
  648. $candidates = array(); 
  649.  
  650. if ( ! isset( $imagedata['file'] ) && isset( $imagedata['sizes']['full'] ) ) { 
  651. $imagedata['height'] = $imagedata['sizes']['full']['height']; 
  652. $imagedata['width'] = $imagedata['sizes']['full']['width']; 
  653.  
  654. foreach ( $imagedata['sizes'] as $_size => $data ) { 
  655. // If there's an exact match to an existing image size, short circuit. 
  656. if ( $data['width'] == $size[0] && $data['height'] == $size[1] ) { 
  657. $candidates[ $data['width'] * $data['height'] ] = $data; 
  658. break; 
  659.  
  660. // If it's not an exact match, consider larger sizes with the same aspect ratio. 
  661. if ( $data['width'] >= $size[0] && $data['height'] >= $size[1] ) { 
  662. // If '0' is passed to either size, we test ratios against the original file. 
  663. if ( 0 === $size[0] || 0 === $size[1] ) { 
  664. $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $imagedata['width'], $imagedata['height'] ); 
  665. } else { 
  666. $same_ratio = wp_image_matches_ratio( $data['width'], $data['height'], $size[0], $size[1] ); 
  667.  
  668. if ( $same_ratio ) { 
  669. $candidates[ $data['width'] * $data['height'] ] = $data; 
  670.  
  671. if ( ! empty( $candidates ) ) { 
  672. // Sort the array by size if we have more than one candidate. 
  673. if ( 1 < count( $candidates ) ) { 
  674. ksort( $candidates ); 
  675.  
  676. $data = array_shift( $candidates ); 
  677. /** 
  678. * When the size requested is smaller than the thumbnail dimensions, we 
  679. * fall back to the thumbnail size to maintain backwards compatibility with 
  680. * pre 4.6 versions of WordPress. 
  681. */ 
  682. } elseif ( ! empty( $imagedata['sizes']['thumbnail'] ) && $imagedata['sizes']['thumbnail']['width'] >= $size[0] && $imagedata['sizes']['thumbnail']['width'] >= $size[1] ) { 
  683. $data = $imagedata['sizes']['thumbnail']; 
  684. } else { 
  685. return false; 
  686.  
  687. // Constrain the width and height attributes to the requested values. 
  688. list( $data['width'], $data['height'] ) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); 
  689.  
  690. } elseif ( ! empty( $imagedata['sizes'][ $size ] ) ) { 
  691. $data = $imagedata['sizes'][ $size ]; 
  692.  
  693. // If we still don't have a match at this point, return false. 
  694. if ( empty( $data ) ) { 
  695. return false; 
  696.  
  697. // include the full filesystem path of the intermediate file 
  698. if ( empty( $data['path'] ) && ! empty( $data['file'] ) && ! empty( $imagedata['file'] ) ) { 
  699. $file_url = wp_get_attachment_url($post_id); 
  700. $data['path'] = path_join( dirname($imagedata['file']), $data['file'] ); 
  701. $data['url'] = path_join( dirname($file_url), $data['file'] ); 
  702.  
  703. /** 
  704. * Filters the output of image_get_intermediate_size() 
  705. * 
  706. * @since 4.4.0 
  707. * 
  708. * @see image_get_intermediate_size() 
  709. * 
  710. * @param array $data Array of file relative path, width, and height on success. May also include 
  711. * file absolute path and URL. 
  712. * @param int $post_id The post_id of the image attachment 
  713. * @param string|array $size Registered image size or flat array of initially-requested height and width 
  714. * dimensions (in that order). 
  715. */ 
  716. return apply_filters( 'image_get_intermediate_size', $data, $post_id, $size ); 
  717.  
  718. /** 
  719. * Gets the available intermediate image sizes. 
  720. * 
  721. * @since 3.0.0 
  722. * 
  723. * @return array Returns a filtered array of image size strings. 
  724. */ 
  725. function get_intermediate_image_sizes() { 
  726. $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 
  727. $image_sizes = array('thumbnail', 'medium', 'medium_large', 'large'); // Standard sizes 
  728. if ( ! empty( $_wp_additional_image_sizes ) ) { 
  729. $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) ); 
  730.  
  731. /** 
  732. * Filters the list of intermediate image sizes. 
  733. * 
  734. * @since 2.5.0 
  735. * 
  736. * @param array $image_sizes An array of intermediate image sizes. Defaults 
  737. * are 'thumbnail', 'medium', 'medium_large', 'large'. 
  738. */ 
  739. return apply_filters( 'intermediate_image_sizes', $image_sizes ); 
  740.  
  741. /** 
  742. * Retrieve an image to represent an attachment. 
  743. * 
  744. * A mime icon for files, thumbnail or intermediate size for images. 
  745. * 
  746. * The returned array contains four values: the URL of the attachment image src,  
  747. * the width of the image file, the height of the image file, and a boolean 
  748. * representing whether the returned array describes an intermediate (generated) 
  749. * image size or the original, full-sized upload. 
  750. * 
  751. * @since 2.5.0 
  752. * 
  753. * @param int $attachment_id Image attachment ID. 
  754. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width 
  755. * and height values in pixels (in that order). Default 'thumbnail'. 
  756. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. 
  757. * @return false|array Returns an array (url, width, height, is_intermediate), or false, if no image is available. 
  758. */ 
  759. function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) { 
  760. // get a thumbnail or intermediate image if there is one 
  761. $image = image_downsize( $attachment_id, $size ); 
  762. if ( ! $image ) { 
  763. $src = false; 
  764.  
  765. if ( $icon && $src = wp_mime_type_icon( $attachment_id ) ) { 
  766. /** This filter is documented in wp-includes/post.php */ 
  767. $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); 
  768.  
  769. $src_file = $icon_dir . '/' . wp_basename( $src ); 
  770. @list( $width, $height ) = getimagesize( $src_file ); 
  771.  
  772. if ( $src && $width && $height ) { 
  773. $image = array( $src, $width, $height ); 
  774. /** 
  775. * Filters the image src result. 
  776. * 
  777. * @since 4.3.0 
  778. * 
  779. * @param array|false $image Either array with src, width & height, icon src, or false. 
  780. * @param int $attachment_id Image attachment ID. 
  781. * @param string|array $size Size of image. Image size or array of width and height values 
  782. * (in that order). Default 'thumbnail'. 
  783. * @param bool $icon Whether the image should be treated as an icon. Default false. 
  784. */ 
  785. return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon ); 
  786.  
  787. /** 
  788. * Get an HTML img element representing an image attachment 
  789. * 
  790. * While `$size` will accept an array, it is better to register a size with 
  791. * add_image_size() so that a cropped version is generated. It's much more 
  792. * efficient than having to find the closest-sized image and then having the 
  793. * browser scale down the image. 
  794. * 
  795. * @since 2.5.0 
  796. * 
  797. * @param int $attachment_id Image attachment ID. 
  798. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width 
  799. * and height values in pixels (in that order). Default 'thumbnail'. 
  800. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. 
  801. * @param string|array $attr Optional. Attributes for the image markup. Default empty. 
  802. * @return string HTML img element or empty string on failure. 
  803. */ 
  804. function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { 
  805. $html = ''; 
  806. $image = wp_get_attachment_image_src($attachment_id, $size, $icon); 
  807. if ( $image ) { 
  808. list($src, $width, $height) = $image; 
  809. $hwstring = image_hwstring($width, $height); 
  810. $size_class = $size; 
  811. if ( is_array( $size_class ) ) { 
  812. $size_class = join( 'x', $size_class ); 
  813. $attachment = get_post($attachment_id); 
  814. $default_attr = array( 
  815. 'src' => $src,  
  816. 'class' => "attachment-$size_class size-$size_class",  
  817. 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),  
  818. ); 
  819.  
  820. $attr = wp_parse_args( $attr, $default_attr ); 
  821.  
  822. // Generate 'srcset' and 'sizes' if not already present. 
  823. if ( empty( $attr['srcset'] ) ) { 
  824. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  825.  
  826. if ( is_array( $image_meta ) ) { 
  827. $size_array = array( absint( $width ), absint( $height ) ); 
  828. $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); 
  829. $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); 
  830.  
  831. if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { 
  832. $attr['srcset'] = $srcset; 
  833.  
  834. if ( empty( $attr['sizes'] ) ) { 
  835. $attr['sizes'] = $sizes; 
  836.  
  837. /** 
  838. * Filters the list of attachment image attributes. 
  839. * 
  840. * @since 2.8.0 
  841. * 
  842. * @param array $attr Attributes for the image markup. 
  843. * @param WP_Post $attachment Image attachment post. 
  844. * @param string|array $size Requested size. Image size or array of width and height values 
  845. * (in that order). Default 'thumbnail'. 
  846. */ 
  847. $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); 
  848. $attr = array_map( 'esc_attr', $attr ); 
  849. $html = rtrim("<img $hwstring"); 
  850. foreach ( $attr as $name => $value ) { 
  851. $html .= " $name=" . '"' . $value . '"'; 
  852. $html .= ' />'; 
  853.  
  854. return $html; 
  855.  
  856. /** 
  857. * Get the URL of an image attachment. 
  858. * 
  859. * @since 4.4.0 
  860. * 
  861. * @param int $attachment_id Image attachment ID. 
  862. * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array 
  863. * of width and height values in pixels (in that order). Default 'thumbnail'. 
  864. * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. 
  865. * @return string|false Attachment URL or false if no image is available. 
  866. */ 
  867. function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { 
  868. $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); 
  869. return isset( $image['0'] ) ? $image['0'] : false; 
  870.  
  871. /** 
  872. * Get the attachment path relative to the upload directory. 
  873. * 
  874. * @since 4.4.1 
  875. * @access private 
  876. * 
  877. * @param string $file Attachment file name. 
  878. * @return string Attachment path relative to the upload directory. 
  879. */ 
  880. function _wp_get_attachment_relative_path( $file ) { 
  881. $dirname = dirname( $file ); 
  882.  
  883. if ( '.' === $dirname ) { 
  884. return ''; 
  885.  
  886. if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) { 
  887. // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads) 
  888. $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); 
  889. $dirname = ltrim( $dirname, '/' ); 
  890.  
  891. return $dirname; 
  892.  
  893. /** 
  894. * Get the image size as array from its meta data. 
  895. * 
  896. * Used for responsive images. 
  897. * 
  898. * @since 4.4.0 
  899. * @access private 
  900. * 
  901. * @param string $size_name Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.). 
  902. * @param array $image_meta The image meta data. 
  903. * @return array|bool Array of width and height values in pixels (in that order) 
  904. * or false if the size doesn't exist. 
  905. */ 
  906. function _wp_get_image_size_from_meta( $size_name, $image_meta ) { 
  907. if ( $size_name === 'full' ) { 
  908. return array( 
  909. absint( $image_meta['width'] ),  
  910. absint( $image_meta['height'] ),  
  911. ); 
  912. } elseif ( ! empty( $image_meta['sizes'][$size_name] ) ) { 
  913. return array( 
  914. absint( $image_meta['sizes'][$size_name]['width'] ),  
  915. absint( $image_meta['sizes'][$size_name]['height'] ),  
  916. ); 
  917.  
  918. return false; 
  919.  
  920. /** 
  921. * Retrieves the value for an image attachment's 'srcset' attribute. 
  922. * 
  923. * @since 4.4.0 
  924. * 
  925. * @see wp_calculate_image_srcset() 
  926. * 
  927. * @param int $attachment_id Image attachment ID. 
  928. * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of 
  929. * width and height values in pixels (in that order). Default 'medium'. 
  930. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 
  931. * Default null. 
  932. * @return string|bool A 'srcset' value string or false. 
  933. */ 
  934. function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { 
  935. if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 
  936. return false; 
  937.  
  938. if ( ! is_array( $image_meta ) ) { 
  939. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  940.  
  941. $image_src = $image[0]; 
  942. $size_array = array( 
  943. absint( $image[1] ),  
  944. absint( $image[2] ) 
  945. ); 
  946.  
  947. return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 
  948.  
  949. /** 
  950. * A helper function to calculate the image sources to include in a 'srcset' attribute. 
  951. * 
  952. * @since 4.4.0 
  953. * 
  954. * @param array $size_array Array of width and height values in pixels (in that order). 
  955. * @param string $image_src The 'src' of the image. 
  956. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 
  957. * @param int $attachment_id Optional. The image attachment ID to pass to the filter. Default 0. 
  958. * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. 
  959. */ 
  960. function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { 
  961. /** 
  962. * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. 
  963. * 
  964. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 
  965. * @param array $size_array Array of width and height values in pixels (in that order). 
  966. * @param string $image_src The 'src' of the image. 
  967. * @param int $attachment_id The image attachment ID or 0 if not supplied. 
  968. */ 
  969. $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id ); 
  970.  
  971. if ( empty( $image_meta['sizes'] ) || ! isset( $image_meta['file'] ) || strlen( $image_meta['file'] ) < 4 ) { 
  972. return false; 
  973.  
  974. $image_sizes = $image_meta['sizes']; 
  975.  
  976. // Get the width and height of the image. 
  977. $image_width = (int) $size_array[0]; 
  978. $image_height = (int) $size_array[1]; 
  979.  
  980. // Bail early if error/no width. 
  981. if ( $image_width < 1 ) { 
  982. return false; 
  983.  
  984. $image_basename = wp_basename( $image_meta['file'] ); 
  985.  
  986. /** 
  987. * WordPress flattens animated GIFs into one frame when generating intermediate sizes. 
  988. * To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated. 
  989. * If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated. 
  990. */ 
  991. if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) { 
  992. $image_sizes[] = array( 
  993. 'width' => $image_meta['width'],  
  994. 'height' => $image_meta['height'],  
  995. 'file' => $image_basename,  
  996. ); 
  997. } elseif ( strpos( $image_src, $image_meta['file'] ) ) { 
  998. return false; 
  999.  
  1000. // Retrieve the uploads sub-directory from the full size image. 
  1001. $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); 
  1002.  
  1003. if ( $dirname ) { 
  1004. $dirname = trailingslashit( $dirname ); 
  1005.  
  1006. $upload_dir = wp_get_upload_dir(); 
  1007. $image_baseurl = trailingslashit( $upload_dir['baseurl'] ) . $dirname; 
  1008.  
  1009. /** 
  1010. * If currently on HTTPS, prefer HTTPS URLs when we know they're supported by the domain 
  1011. * (which is to say, when they share the domain name of the current request). 
  1012. */ 
  1013. if ( is_ssl() && 'https' !== substr( $image_baseurl, 0, 5 ) && parse_url( $image_baseurl, PHP_URL_HOST ) === $_SERVER['HTTP_HOST'] ) { 
  1014. $image_baseurl = set_url_scheme( $image_baseurl, 'https' ); 
  1015.  
  1016. /** 
  1017. * Images that have been edited in WordPress after being uploaded will 
  1018. * contain a unique hash. Look for that hash and use it later to filter 
  1019. * out images that are leftovers from previous versions. 
  1020. */ 
  1021. $image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash ); 
  1022.  
  1023. /** 
  1024. * Filters the maximum image width to be included in a 'srcset' attribute. 
  1025. * 
  1026. * @since 4.4.0 
  1027. * 
  1028. * @param int $max_width The maximum image width to be included in the 'srcset'. Default '1600'. 
  1029. * @param array $size_array Array of width and height values in pixels (in that order). 
  1030. */ 
  1031. $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); 
  1032.  
  1033. // Array to hold URL candidates. 
  1034. $sources = array(); 
  1035.  
  1036. /** 
  1037. * To make sure the ID matches our image src, we will check to see if any sizes in our attachment 
  1038. * meta match our $image_src. If no matches are found we don't return a srcset to avoid serving 
  1039. * an incorrect image. See #35045. 
  1040. */ 
  1041. $src_matched = false; 
  1042.  
  1043. /** 
  1044. * Loop through available images. Only use images that are resized 
  1045. * versions of the same edit. 
  1046. */ 
  1047. foreach ( $image_sizes as $image ) { 
  1048. $is_src = false; 
  1049.  
  1050. // Check if image meta isn't corrupted. 
  1051. if ( ! is_array( $image ) ) { 
  1052. continue; 
  1053.  
  1054. // If the file name is part of the `src`, we've confirmed a match. 
  1055. if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { 
  1056. $src_matched = $is_src = true; 
  1057.  
  1058. // Filter out images that are from previous edits. 
  1059. if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) { 
  1060. continue; 
  1061.  
  1062. /** 
  1063. * Filters out images that are wider than '$max_srcset_image_width' unless 
  1064. * that file is in the 'src' attribute. 
  1065. */ 
  1066. if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) { 
  1067. continue; 
  1068.  
  1069. // If the image dimensions are within 1px of the expected size, use it. 
  1070. if ( wp_image_matches_ratio( $image_width, $image_height, $image['width'], $image['height'] ) ) { 
  1071. // Add the URL, descriptor, and value to the sources array to be returned. 
  1072. $source = array( 
  1073. 'url' => $image_baseurl . $image['file'],  
  1074. 'descriptor' => 'w',  
  1075. 'value' => $image['width'],  
  1076. ); 
  1077.  
  1078. // The 'src' image has to be the first in the 'srcset', because of a bug in iOS8. See #35030. 
  1079. if ( $is_src ) { 
  1080. $sources = array( $image['width'] => $source ) + $sources; 
  1081. } else { 
  1082. $sources[ $image['width'] ] = $source; 
  1083.  
  1084. /** 
  1085. * Filters an image's 'srcset' sources. 
  1086. * 
  1087. * @since 4.4.0 
  1088. * 
  1089. * @param array $sources { 
  1090. * One or more arrays of source data to include in the 'srcset'. 
  1091. * 
  1092. * @type array $width { 
  1093. * @type string $url The URL of an image source. 
  1094. * @type string $descriptor The descriptor type used in the image candidate string,  
  1095. * either 'w' or 'x'. 
  1096. * @type int $value The source width if paired with a 'w' descriptor, or a 
  1097. * pixel density value if paired with an 'x' descriptor. 
  1098. * } 
  1099. * } 
  1100. * @param array $size_array Array of width and height values in pixels (in that order). 
  1101. * @param string $image_src The 'src' of the image. 
  1102. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 
  1103. * @param int $attachment_id Image attachment ID or 0. 
  1104. */ 
  1105. $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id ); 
  1106.  
  1107. // Only return a 'srcset' value if there is more than one source. 
  1108. if ( ! $src_matched || count( $sources ) < 2 ) { 
  1109. return false; 
  1110.  
  1111. $srcset = ''; 
  1112.  
  1113. foreach ( $sources as $source ) { 
  1114. $srcset .= str_replace( ' ', '%20', $source['url'] ) . ' ' . $source['value'] . $source['descriptor'] . ', '; 
  1115.  
  1116. return rtrim( $srcset, ', ' ); 
  1117.  
  1118. /** 
  1119. * Retrieves the value for an image attachment's 'sizes' attribute. 
  1120. * 
  1121. * @since 4.4.0 
  1122. * 
  1123. * @see wp_calculate_image_sizes() 
  1124. * 
  1125. * @param int $attachment_id Image attachment ID. 
  1126. * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of width 
  1127. * and height values in pixels (in that order). Default 'medium'. 
  1128. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 
  1129. * Default null. 
  1130. * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 
  1131. */ 
  1132. function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { 
  1133. if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 
  1134. return false; 
  1135.  
  1136. if ( ! is_array( $image_meta ) ) { 
  1137. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  1138.  
  1139. $image_src = $image[0]; 
  1140. $size_array = array( 
  1141. absint( $image[1] ),  
  1142. absint( $image[2] ) 
  1143. ); 
  1144.  
  1145. return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 
  1146.  
  1147. /** 
  1148. * Creates a 'sizes' attribute value for an image. 
  1149. * 
  1150. * @since 4.4.0 
  1151. * 
  1152. * @param array|string $size Image size to retrieve. Accepts any valid image size, or an array 
  1153. * of width and height values in pixels (in that order). Default 'medium'. 
  1154. * @param string $image_src Optional. The URL to the image file. Default null. 
  1155. * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 
  1156. * Default null. 
  1157. * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` 
  1158. * is needed when using the image size name as argument for `$size`. Default 0. 
  1159. * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 
  1160. */ 
  1161. function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) { 
  1162. $width = 0; 
  1163.  
  1164. if ( is_array( $size ) ) { 
  1165. $width = absint( $size[0] ); 
  1166. } elseif ( is_string( $size ) ) { 
  1167. if ( ! $image_meta && $attachment_id ) { 
  1168. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  1169.  
  1170. if ( is_array( $image_meta ) ) { 
  1171. $size_array = _wp_get_image_size_from_meta( $size, $image_meta ); 
  1172. if ( $size_array ) { 
  1173. $width = absint( $size_array[0] ); 
  1174.  
  1175. if ( ! $width ) { 
  1176. return false; 
  1177.  
  1178. // Setup the default 'sizes' attribute. 
  1179. $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); 
  1180.  
  1181. /** 
  1182. * Filters the output of 'wp_calculate_image_sizes()'. 
  1183. * 
  1184. * @since 4.4.0 
  1185. * 
  1186. * @param string $sizes A source size value for use in a 'sizes' attribute. 
  1187. * @param array|string $size Requested size. Image size or array of width and height values 
  1188. * in pixels (in that order). 
  1189. * @param string|null $image_src The URL to the image file or null. 
  1190. * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. 
  1191. * @param int $attachment_id Image attachment ID of the original image or 0. 
  1192. */ 
  1193. return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); 
  1194.  
  1195. /** 
  1196. * Filters 'img' elements in post content to add 'srcset' and 'sizes' attributes. 
  1197. * 
  1198. * @since 4.4.0 
  1199. * 
  1200. * @see wp_image_add_srcset_and_sizes() 
  1201. * 
  1202. * @param string $content The raw post content to be filtered. 
  1203. * @return string Converted content with 'srcset' and 'sizes' attributes added to images. 
  1204. */ 
  1205. function wp_make_content_images_responsive( $content ) { 
  1206. if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) { 
  1207. return $content; 
  1208.  
  1209. $selected_images = $attachment_ids = array(); 
  1210.  
  1211. foreach( $matches[0] as $image ) { 
  1212. if ( false === strpos( $image, ' srcset=' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && 
  1213. ( $attachment_id = absint( $class_id[1] ) ) ) { 
  1214.  
  1215. /** 
  1216. * If exactly the same image tag is used more than once, overwrite it. 
  1217. * All identical tags will be replaced later with 'str_replace()'. 
  1218. */ 
  1219. $selected_images[ $image ] = $attachment_id; 
  1220. // Overwrite the ID when the same image is included more than once. 
  1221. $attachment_ids[ $attachment_id ] = true; 
  1222.  
  1223. if ( count( $attachment_ids ) > 1 ) { 
  1224. /** 
  1225. * Warm object cache for use with 'get_post_meta()'. 
  1226. * 
  1227. * To avoid making a database call for each image, a single query 
  1228. * warms the object cache with the meta information for all images. 
  1229. */ 
  1230. update_meta_cache( 'post', array_keys( $attachment_ids ) ); 
  1231.  
  1232. foreach ( $selected_images as $image => $attachment_id ) { 
  1233. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  1234. $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content ); 
  1235.  
  1236. return $content; 
  1237.  
  1238. /** 
  1239. * Adds 'srcset' and 'sizes' attributes to an existing 'img' element. 
  1240. * 
  1241. * @since 4.4.0 
  1242. * 
  1243. * @see wp_calculate_image_srcset() 
  1244. * @see wp_calculate_image_sizes() 
  1245. * 
  1246. * @param string $image An HTML 'img' element to be filtered. 
  1247. * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 
  1248. * @param int $attachment_id Image attachment ID. 
  1249. * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added. 
  1250. */ 
  1251. function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
  1252. // Ensure the image meta exists. 
  1253. if ( empty( $image_meta['sizes'] ) ) { 
  1254. return $image; 
  1255.  
  1256. $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; 
  1257. list( $image_src ) = explode( '?', $image_src ); 
  1258.  
  1259. // Return early if we couldn't get the image source. 
  1260. if ( ! $image_src ) { 
  1261. return $image; 
  1262.  
  1263. // Bail early if an image has been inserted and later edited. 
  1264. if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && 
  1265. strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { 
  1266.  
  1267. return $image; 
  1268.  
  1269. $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; 
  1270. $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; 
  1271.  
  1272. if ( ! $width || ! $height ) { 
  1273. /** 
  1274. * If attempts to parse the size value failed, attempt to use the image meta data to match 
  1275. * the image file name from 'src' against the available sizes for an attachment. 
  1276. */ 
  1277. $image_filename = wp_basename( $image_src ); 
  1278.  
  1279. if ( $image_filename === wp_basename( $image_meta['file'] ) ) { 
  1280. $width = (int) $image_meta['width']; 
  1281. $height = (int) $image_meta['height']; 
  1282. } else { 
  1283. foreach( $image_meta['sizes'] as $image_size_data ) { 
  1284. if ( $image_filename === $image_size_data['file'] ) { 
  1285. $width = (int) $image_size_data['width']; 
  1286. $height = (int) $image_size_data['height']; 
  1287. break; 
  1288.  
  1289. if ( ! $width || ! $height ) { 
  1290. return $image; 
  1291.  
  1292. $size_array = array( $width, $height ); 
  1293. $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 
  1294.  
  1295. if ( $srcset ) { 
  1296. // Check if there is already a 'sizes' attribute. 
  1297. $sizes = strpos( $image, ' sizes=' ); 
  1298.  
  1299. if ( ! $sizes ) { 
  1300. $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 
  1301.  
  1302. if ( $srcset && $sizes ) { 
  1303. // Format the 'srcset' and 'sizes' string and escape attributes. 
  1304. $attr = sprintf( ' srcset="%s"', esc_attr( $srcset ) ); 
  1305.  
  1306. if ( is_string( $sizes ) ) { 
  1307. $attr .= sprintf( ' sizes="%s"', esc_attr( $sizes ) ); 
  1308.  
  1309. // Add 'srcset' and 'sizes' attributes to the image markup. 
  1310. $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $attr . ' />', $image ); 
  1311.  
  1312. return $image; 
  1313.  
  1314. /** 
  1315. * Adds a 'wp-post-image' class to post thumbnails. Internal use only. 
  1316. * 
  1317. * Uses the {@see 'begin_fetch_post_thumbnail_html'} and {@see 'end_fetch_post_thumbnail_html'} 
  1318. * action hooks to dynamically add/remove itself so as to only filter post thumbnails. 
  1319. * 
  1320. * @ignore 
  1321. * @since 2.9.0 
  1322. * 
  1323. * @param array $attr Thumbnail attributes including src, class, alt, title. 
  1324. * @return array Modified array of attributes including the new 'wp-post-image' class. 
  1325. */ 
  1326. function _wp_post_thumbnail_class_filter( $attr ) { 
  1327. $attr['class'] .= ' wp-post-image'; 
  1328. return $attr; 
  1329.  
  1330. /** 
  1331. * Adds '_wp_post_thumbnail_class_filter' callback to the 'wp_get_attachment_image_attributes' 
  1332. * filter hook. Internal use only. 
  1333. * 
  1334. * @ignore 
  1335. * @since 2.9.0 
  1336. * 
  1337. * @param array $attr Thumbnail attributes including src, class, alt, title. 
  1338. */ 
  1339. function _wp_post_thumbnail_class_filter_add( $attr ) { 
  1340. add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); 
  1341.  
  1342. /** 
  1343. * Removes the '_wp_post_thumbnail_class_filter' callback from the 'wp_get_attachment_image_attributes' 
  1344. * filter hook. Internal use only. 
  1345. * 
  1346. * @ignore 
  1347. * @since 2.9.0 
  1348. * 
  1349. * @param array $attr Thumbnail attributes including src, class, alt, title. 
  1350. */ 
  1351. function _wp_post_thumbnail_class_filter_remove( $attr ) { 
  1352. remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); 
  1353.  
  1354. add_shortcode('wp_caption', 'img_caption_shortcode'); 
  1355. add_shortcode('caption', 'img_caption_shortcode'); 
  1356.  
  1357. /** 
  1358. * Builds the Caption shortcode output. 
  1359. * 
  1360. * Allows a plugin to replace the content that would otherwise be returned. The 
  1361. * filter is {@see 'img_caption_shortcode'} and passes an empty string, the attr 
  1362. * parameter and the content parameter values. 
  1363. * 
  1364. * The supported attributes for the shortcode are 'id', 'align', 'width', and 
  1365. * 'caption'. 
  1366. * 
  1367. * @since 2.6.0 
  1368. * 
  1369. * @param array $attr { 
  1370. * Attributes of the caption shortcode. 
  1371. * 
  1372. * @type string $id ID of the div element for the caption. 
  1373. * @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft',  
  1374. * 'aligncenter', alignright', 'alignnone'. 
  1375. * @type int $width The width of the caption, in pixels. 
  1376. * @type string $caption The caption text. 
  1377. * @type string $class Additional class name(s) added to the caption container. 
  1378. * } 
  1379. * @param string $content Shortcode content. 
  1380. * @return string HTML content to display the caption. 
  1381. */ 
  1382. function img_caption_shortcode( $attr, $content = null ) { 
  1383. // New-style shortcode with the caption inside the shortcode with the link and image tags. 
  1384. if ( ! isset( $attr['caption'] ) ) { 
  1385. if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) { 
  1386. $content = $matches[1]; 
  1387. $attr['caption'] = trim( $matches[2] ); 
  1388. } elseif ( strpos( $attr['caption'], '<' ) !== false ) { 
  1389. $attr['caption'] = wp_kses( $attr['caption'], 'post' ); 
  1390.  
  1391. /** 
  1392. * Filters the default caption shortcode output. 
  1393. * 
  1394. * If the filtered output isn't empty, it will be used instead of generating 
  1395. * the default caption template. 
  1396. * 
  1397. * @since 2.6.0 
  1398. * 
  1399. * @see img_caption_shortcode() 
  1400. * 
  1401. * @param string $output The caption output. Default empty. 
  1402. * @param array $attr Attributes of the caption shortcode. 
  1403. * @param string $content The image element, possibly wrapped in a hyperlink. 
  1404. */ 
  1405. $output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); 
  1406. if ( $output != '' ) 
  1407. return $output; 
  1408.  
  1409. $atts = shortcode_atts( array( 
  1410. 'id' => '',  
  1411. 'align' => 'alignnone',  
  1412. 'width' => '',  
  1413. 'caption' => '',  
  1414. 'class' => '',  
  1415. ), $attr, 'caption' ); 
  1416.  
  1417. $atts['width'] = (int) $atts['width']; 
  1418. if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) 
  1419. return $content; 
  1420.  
  1421. if ( ! empty( $atts['id'] ) ) 
  1422. $atts['id'] = 'id="' . esc_attr( sanitize_html_class( $atts['id'] ) ) . '" '; 
  1423.  
  1424. $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); 
  1425.  
  1426. $html5 = current_theme_supports( 'html5', 'caption' ); 
  1427. // HTML5 captions never added the extra 10px to the image width 
  1428. $width = $html5 ? $atts['width'] : ( 10 + $atts['width'] ); 
  1429.  
  1430. /** 
  1431. * Filters the width of an image's caption. 
  1432. * 
  1433. * By default, the caption is 10 pixels greater than the width of the image,  
  1434. * to prevent post content from running up against a floated image. 
  1435. * 
  1436. * @since 3.7.0 
  1437. * 
  1438. * @see img_caption_shortcode() 
  1439. * 
  1440. * @param int $width Width of the caption in pixels. To remove this inline style,  
  1441. * return zero. 
  1442. * @param array $atts Attributes of the caption shortcode. 
  1443. * @param string $content The image element, possibly wrapped in a hyperlink. 
  1444. */ 
  1445. $caption_width = apply_filters( 'img_caption_shortcode_width', $width, $atts, $content ); 
  1446.  
  1447. $style = ''; 
  1448. if ( $caption_width ) { 
  1449. $style = 'style="width: ' . (int) $caption_width . 'px" '; 
  1450.  
  1451. if ( $html5 ) { 
  1452. $html = '<figure ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">' 
  1453. . do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption></figure>'; 
  1454. } else { 
  1455. $html = '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">' 
  1456. . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>'; 
  1457.  
  1458. return $html; 
  1459.  
  1460. add_shortcode('gallery', 'gallery_shortcode'); 
  1461.  
  1462. /** 
  1463. * Builds the Gallery shortcode output. 
  1464. * 
  1465. * This implements the functionality of the Gallery Shortcode for displaying 
  1466. * WordPress images on a post. 
  1467. * 
  1468. * @since 2.5.0 
  1469. * 
  1470. * @staticvar int $instance 
  1471. * 
  1472. * @param array $attr { 
  1473. * Attributes of the gallery shortcode. 
  1474. * 
  1475. * @type string $order Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'. 
  1476. * @type string $orderby The field to use when ordering the images. Default 'menu_order ID'. 
  1477. * Accepts any valid SQL ORDERBY statement. 
  1478. * @type int $id Post ID. 
  1479. * @type string $itemtag HTML tag to use for each image in the gallery. 
  1480. * Default 'dl', or 'figure' when the theme registers HTML5 gallery support. 
  1481. * @type string $icontag HTML tag to use for each image's icon. 
  1482. * Default 'dt', or 'div' when the theme registers HTML5 gallery support. 
  1483. * @type string $captiontag HTML tag to use for each image's caption. 
  1484. * Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support. 
  1485. * @type int $columns Number of columns of images to display. Default 3. 
  1486. * @type string|array $size Size of the images to display. Accepts any valid image size, or an array of width 
  1487. * and height values in pixels (in that order). Default 'thumbnail'. 
  1488. * @type string $ids A comma-separated list of IDs of attachments to display. Default empty. 
  1489. * @type string $include A comma-separated list of IDs of attachments to include. Default empty. 
  1490. * @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty. 
  1491. * @type string $link What to link each image to. Default empty (links to the attachment page). 
  1492. * Accepts 'file', 'none'. 
  1493. * } 
  1494. * @return string HTML content to display gallery. 
  1495. */ 
  1496. function gallery_shortcode( $attr ) { 
  1497. $post = get_post(); 
  1498.  
  1499. static $instance = 0; 
  1500. $instance++; 
  1501.  
  1502. if ( ! empty( $attr['ids'] ) ) { 
  1503. // 'ids' is explicitly ordered, unless you specify otherwise. 
  1504. if ( empty( $attr['orderby'] ) ) { 
  1505. $attr['orderby'] = 'post__in'; 
  1506. $attr['include'] = $attr['ids']; 
  1507.  
  1508. /** 
  1509. * Filters the default gallery shortcode output. 
  1510. * 
  1511. * If the filtered output isn't empty, it will be used instead of generating 
  1512. * the default gallery template. 
  1513. * 
  1514. * @since 2.5.0 
  1515. * @since 4.2.0 The `$instance` parameter was added. 
  1516. * 
  1517. * @see gallery_shortcode() 
  1518. * 
  1519. * @param string $output The gallery output. Default empty. 
  1520. * @param array $attr Attributes of the gallery shortcode. 
  1521. * @param int $instance Unique numeric ID of this gallery shortcode instance. 
  1522. */ 
  1523. $output = apply_filters( 'post_gallery', '', $attr, $instance ); 
  1524. if ( $output != '' ) { 
  1525. return $output; 
  1526.  
  1527. $html5 = current_theme_supports( 'html5', 'gallery' ); 
  1528. $atts = shortcode_atts( array( 
  1529. 'order' => 'ASC',  
  1530. 'orderby' => 'menu_order ID',  
  1531. 'id' => $post ? $post->ID : 0,  
  1532. 'itemtag' => $html5 ? 'figure' : 'dl',  
  1533. 'icontag' => $html5 ? 'div' : 'dt',  
  1534. 'captiontag' => $html5 ? 'figcaption' : 'dd',  
  1535. 'columns' => 3,  
  1536. 'size' => 'thumbnail',  
  1537. 'include' => '',  
  1538. 'exclude' => '',  
  1539. 'link' => '' 
  1540. ), $attr, 'gallery' ); 
  1541.  
  1542. $id = intval( $atts['id'] ); 
  1543.  
  1544. if ( ! empty( $atts['include'] ) ) { 
  1545. $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); 
  1546.  
  1547. $attachments = array(); 
  1548. foreach ( $_attachments as $key => $val ) { 
  1549. $attachments[$val->ID] = $_attachments[$key]; 
  1550. } elseif ( ! empty( $atts['exclude'] ) ) { 
  1551. $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'] ) ); 
  1552. } else { 
  1553. $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); 
  1554.  
  1555. if ( empty( $attachments ) ) { 
  1556. return ''; 
  1557.  
  1558. if ( is_feed() ) { 
  1559. $output = "\n"; 
  1560. foreach ( $attachments as $att_id => $attachment ) { 
  1561. $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n"; 
  1562. return $output; 
  1563.  
  1564. $itemtag = tag_escape( $atts['itemtag'] ); 
  1565. $captiontag = tag_escape( $atts['captiontag'] ); 
  1566. $icontag = tag_escape( $atts['icontag'] ); 
  1567. $valid_tags = wp_kses_allowed_html( 'post' ); 
  1568. if ( ! isset( $valid_tags[ $itemtag ] ) ) { 
  1569. $itemtag = 'dl'; 
  1570. if ( ! isset( $valid_tags[ $captiontag ] ) ) { 
  1571. $captiontag = 'dd'; 
  1572. if ( ! isset( $valid_tags[ $icontag ] ) ) { 
  1573. $icontag = 'dt'; 
  1574.  
  1575. $columns = intval( $atts['columns'] ); 
  1576. $itemwidth = $columns > 0 ? floor(100/$columns) : 100; 
  1577. $float = is_rtl() ? 'right' : 'left'; 
  1578.  
  1579. $selector = "gallery-{$instance}"; 
  1580.  
  1581. $gallery_style = ''; 
  1582.  
  1583. /** 
  1584. * Filters whether to print default gallery styles. 
  1585. * 
  1586. * @since 3.1.0 
  1587. * 
  1588. * @param bool $print Whether to print default gallery styles. 
  1589. * Defaults to false if the theme supports HTML5 galleries. 
  1590. * Otherwise, defaults to true. 
  1591. */ 
  1592. if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) { 
  1593. $gallery_style = " 
  1594. <style type='text/css'> 
  1595. #{$selector} { 
  1596. margin: auto; 
  1597. #{$selector} .gallery-item { 
  1598. float: {$float}; 
  1599. margin-top: 10px; 
  1600. text-align: center; 
  1601. width: {$itemwidth}%; 
  1602. #{$selector} img { 
  1603. border: 2px solid #cfcfcf; 
  1604. #{$selector} .gallery-caption { 
  1605. margin-left: 0; 
  1606. /* see gallery_shortcode() in wp-includes/media.php */ 
  1607. </style>\n\t\t"; 
  1608.  
  1609. $size_class = sanitize_html_class( $atts['size'] ); 
  1610. $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; 
  1611.  
  1612. /** 
  1613. * Filters the default gallery shortcode CSS styles. 
  1614. * 
  1615. * @since 2.5.0 
  1616. * 
  1617. * @param string $gallery_style Default CSS styles and opening HTML div container 
  1618. * for the gallery shortcode output. 
  1619. */ 
  1620. $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); 
  1621.  
  1622. $i = 0; 
  1623. foreach ( $attachments as $id => $attachment ) { 
  1624.  
  1625. $attr = ( trim( $attachment->post_excerpt ) ) ? array( 'aria-describedby' => "$selector-$id" ) : ''; 
  1626. if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { 
  1627. $image_output = wp_get_attachment_link( $id, $atts['size'], false, false, false, $attr ); 
  1628. } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { 
  1629. $image_output = wp_get_attachment_image( $id, $atts['size'], false, $attr ); 
  1630. } else { 
  1631. $image_output = wp_get_attachment_link( $id, $atts['size'], true, false, false, $attr ); 
  1632. $image_meta = wp_get_attachment_metadata( $id ); 
  1633.  
  1634. $orientation = ''; 
  1635. if ( isset( $image_meta['height'], $image_meta['width'] ) ) { 
  1636. $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; 
  1637. $output .= "<{$itemtag} class='gallery-item'>"; 
  1638. $output .= " 
  1639. <{$icontag} class='gallery-icon {$orientation}'> 
  1640. $image_output 
  1641. </{$icontag}>"; 
  1642. if ( $captiontag && trim($attachment->post_excerpt) ) { 
  1643. $output .= " 
  1644. <{$captiontag} class='wp-caption-text gallery-caption' id='$selector-$id'> 
  1645. " . wptexturize($attachment->post_excerpt) . " 
  1646. </{$captiontag}>"; 
  1647. $output .= "</{$itemtag}>"; 
  1648. if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) { 
  1649. $output .= '<br style="clear: both" />'; 
  1650.  
  1651. if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) { 
  1652. $output .= " 
  1653. <br style='clear: both' />"; 
  1654.  
  1655. $output .= " 
  1656. </div>\n"; 
  1657.  
  1658. return $output; 
  1659.  
  1660. /** 
  1661. * Outputs the templates used by playlists. 
  1662. * 
  1663. * @since 3.9.0 
  1664. */ 
  1665. function wp_underscore_playlist_templates() { 
  1666. ?> 
  1667. <script type="text/html" id="tmpl-wp-playlist-current-item"> 
  1668. <# if ( data.image ) { #> 
  1669. <img src="{{ data.thumb.src }}" alt="" /> 
  1670. <# } #> 
  1671. <div class="wp-playlist-caption"> 
  1672. <span class="wp-playlist-item-meta wp-playlist-item-title"><?php 
  1673. /** translators: playlist item title */ 
  1674. printf( _x( '“%s”', 'playlist item title' ), '{{ data.title }}' ); 
  1675. ?></span> 
  1676. <# if ( data.meta.album ) { #><span class="wp-playlist-item-meta wp-playlist-item-album">{{ data.meta.album }}</span><# } #> 
  1677. <# if ( data.meta.artist ) { #><span class="wp-playlist-item-meta wp-playlist-item-artist">{{ data.meta.artist }}</span><# } #> 
  1678. </div> 
  1679. </script> 
  1680. <script type="text/html" id="tmpl-wp-playlist-item"> 
  1681. <div class="wp-playlist-item"> 
  1682. <a class="wp-playlist-caption" href="{{ data.src }}"> 
  1683. {{ data.index ? ( data.index + '. ' ) : '' }} 
  1684. <# if ( data.caption ) { #> 
  1685. {{ data.caption }} 
  1686. <# } else { #> 
  1687. <span class="wp-playlist-item-title"><?php 
  1688. /** translators: playlist item title */ 
  1689. printf( _x( '“%s”', 'playlist item title' ), '{{{ data.title }}}' ); 
  1690. ?></span> 
  1691. <# if ( data.artists && data.meta.artist ) { #> 
  1692. <span class="wp-playlist-item-artist"> — {{ data.meta.artist }}</span> 
  1693. <# } #> 
  1694. <# } #> 
  1695. </a> 
  1696. <# if ( data.meta.length_formatted ) { #> 
  1697. <div class="wp-playlist-item-length">{{ data.meta.length_formatted }}</div> 
  1698. <# } #> 
  1699. </div> 
  1700. </script> 
  1701. <?php 
  1702.  
  1703. /** 
  1704. * Outputs and enqueue default scripts and styles for playlists. 
  1705. * 
  1706. * @since 3.9.0 
  1707. * 
  1708. * @param string $type Type of playlist. Accepts 'audio' or 'video'. 
  1709. */ 
  1710. function wp_playlist_scripts( $type ) { 
  1711. wp_enqueue_style( 'wp-mediaelement' ); 
  1712. wp_enqueue_script( 'wp-playlist' ); 
  1713. ?> 
  1714. <!--[if lt IE 9]><script>document.createElement('<?php echo esc_js( $type ) ?>');</script><![endif]--> 
  1715. <?php 
  1716. add_action( 'wp_footer', 'wp_underscore_playlist_templates', 0 ); 
  1717. add_action( 'admin_footer', 'wp_underscore_playlist_templates', 0 ); 
  1718.  
  1719. /** 
  1720. * Builds the Playlist shortcode output. 
  1721. * 
  1722. * This implements the functionality of the playlist shortcode for displaying 
  1723. * a collection of WordPress audio or video files in a post. 
  1724. * 
  1725. * @since 3.9.0 
  1726. * 
  1727. * @global int $content_width 
  1728. * @staticvar int $instance 
  1729. * 
  1730. * @param array $attr { 
  1731. * Array of default playlist attributes. 
  1732. * 
  1733. * @type string $type Type of playlist to display. Accepts 'audio' or 'video'. Default 'audio'. 
  1734. * @type string $order Designates ascending or descending order of items in the playlist. 
  1735. * Accepts 'ASC', 'DESC'. Default 'ASC'. 
  1736. * @type string $orderby Any column, or columns, to sort the playlist. If $ids are 
  1737. * passed, this defaults to the order of the $ids array ('post__in'). 
  1738. * Otherwise default is 'menu_order ID'. 
  1739. * @type int $id If an explicit $ids array is not present, this parameter 
  1740. * will determine which attachments are used for the playlist. 
  1741. * Default is the current post ID. 
  1742. * @type array $ids Create a playlist out of these explicit attachment IDs. If empty,  
  1743. * a playlist will be created from all $type attachments of $id. 
  1744. * Default empty. 
  1745. * @type array $exclude List of specific attachment IDs to exclude from the playlist. Default empty. 
  1746. * @type string $style Playlist style to use. Accepts 'light' or 'dark'. Default 'light'. 
  1747. * @type bool $tracklist Whether to show or hide the playlist. Default true. 
  1748. * @type bool $tracknumbers Whether to show or hide the numbers next to entries in the playlist. Default true. 
  1749. * @type bool $images Show or hide the video or audio thumbnail (Featured Image/post 
  1750. * thumbnail). Default true. 
  1751. * @type bool $artists Whether to show or hide artist name in the playlist. Default true. 
  1752. * } 
  1753. * 
  1754. * @return string Playlist output. Empty string if the passed type is unsupported. 
  1755. */ 
  1756. function wp_playlist_shortcode( $attr ) { 
  1757. global $content_width; 
  1758. $post = get_post(); 
  1759.  
  1760. static $instance = 0; 
  1761. $instance++; 
  1762.  
  1763. if ( ! empty( $attr['ids'] ) ) { 
  1764. // 'ids' is explicitly ordered, unless you specify otherwise. 
  1765. if ( empty( $attr['orderby'] ) ) { 
  1766. $attr['orderby'] = 'post__in'; 
  1767. $attr['include'] = $attr['ids']; 
  1768.  
  1769. /** 
  1770. * Filters the playlist output. 
  1771. * 
  1772. * Passing a non-empty value to the filter will short-circuit generation 
  1773. * of the default playlist output, returning the passed value instead. 
  1774. * 
  1775. * @since 3.9.0 
  1776. * @since 4.2.0 The `$instance` parameter was added. 
  1777. * 
  1778. * @param string $output Playlist output. Default empty. 
  1779. * @param array $attr An array of shortcode attributes. 
  1780. * @param int $instance Unique numeric ID of this playlist shortcode instance. 
  1781. */ 
  1782. $output = apply_filters( 'post_playlist', '', $attr, $instance ); 
  1783. if ( $output != '' ) { 
  1784. return $output; 
  1785.  
  1786. $atts = shortcode_atts( array( 
  1787. 'type' => 'audio',  
  1788. 'order' => 'ASC',  
  1789. 'orderby' => 'menu_order ID',  
  1790. 'id' => $post ? $post->ID : 0,  
  1791. 'include' => '',  
  1792. 'exclude' => '',  
  1793. 'style' => 'light',  
  1794. 'tracklist' => true,  
  1795. 'tracknumbers' => true,  
  1796. 'images' => true,  
  1797. 'artists' => true 
  1798. ), $attr, 'playlist' ); 
  1799.  
  1800. $id = intval( $atts['id'] ); 
  1801.  
  1802. if ( $atts['type'] !== 'audio' ) { 
  1803. $atts['type'] = 'video'; 
  1804.  
  1805. $args = array( 
  1806. 'post_status' => 'inherit',  
  1807. 'post_type' => 'attachment',  
  1808. 'post_mime_type' => $atts['type'],  
  1809. 'order' => $atts['order'],  
  1810. 'orderby' => $atts['orderby'] 
  1811. ); 
  1812.  
  1813. if ( ! empty( $atts['include'] ) ) { 
  1814. $args['include'] = $atts['include']; 
  1815. $_attachments = get_posts( $args ); 
  1816.  
  1817. $attachments = array(); 
  1818. foreach ( $_attachments as $key => $val ) { 
  1819. $attachments[$val->ID] = $_attachments[$key]; 
  1820. } elseif ( ! empty( $atts['exclude'] ) ) { 
  1821. $args['post_parent'] = $id; 
  1822. $args['exclude'] = $atts['exclude']; 
  1823. $attachments = get_children( $args ); 
  1824. } else { 
  1825. $args['post_parent'] = $id; 
  1826. $attachments = get_children( $args ); 
  1827.  
  1828. if ( empty( $attachments ) ) { 
  1829. return ''; 
  1830.  
  1831. if ( is_feed() ) { 
  1832. $output = "\n"; 
  1833. foreach ( $attachments as $att_id => $attachment ) { 
  1834. $output .= wp_get_attachment_link( $att_id ) . "\n"; 
  1835. return $output; 
  1836.  
  1837. $outer = 22; // default padding and border of wrapper 
  1838.  
  1839. $default_width = 640; 
  1840. $default_height = 360; 
  1841.  
  1842. $theme_width = empty( $content_width ) ? $default_width : ( $content_width - $outer ); 
  1843. $theme_height = empty( $content_width ) ? $default_height : round( ( $default_height * $theme_width ) / $default_width ); 
  1844.  
  1845. $data = array( 
  1846. 'type' => $atts['type'],  
  1847. // don't pass strings to JSON, will be truthy in JS 
  1848. 'tracklist' => wp_validate_boolean( $atts['tracklist'] ),  
  1849. 'tracknumbers' => wp_validate_boolean( $atts['tracknumbers'] ),  
  1850. 'images' => wp_validate_boolean( $atts['images'] ),  
  1851. 'artists' => wp_validate_boolean( $atts['artists'] ),  
  1852. ); 
  1853.  
  1854. $tracks = array(); 
  1855. foreach ( $attachments as $attachment ) { 
  1856. $url = wp_get_attachment_url( $attachment->ID ); 
  1857. $ftype = wp_check_filetype( $url, wp_get_mime_types() ); 
  1858. $track = array( 
  1859. 'src' => $url,  
  1860. 'type' => $ftype['type'],  
  1861. 'title' => $attachment->post_title,  
  1862. 'caption' => $attachment->post_excerpt,  
  1863. 'description' => $attachment->post_content 
  1864. ); 
  1865.  
  1866. $track['meta'] = array(); 
  1867. $meta = wp_get_attachment_metadata( $attachment->ID ); 
  1868. if ( ! empty( $meta ) ) { 
  1869.  
  1870. foreach ( wp_get_attachment_id3_keys( $attachment ) as $key => $label ) { 
  1871. if ( ! empty( $meta[ $key ] ) ) { 
  1872. $track['meta'][ $key ] = $meta[ $key ]; 
  1873.  
  1874. if ( 'video' === $atts['type'] ) { 
  1875. if ( ! empty( $meta['width'] ) && ! empty( $meta['height'] ) ) { 
  1876. $width = $meta['width']; 
  1877. $height = $meta['height']; 
  1878. $theme_height = round( ( $height * $theme_width ) / $width ); 
  1879. } else { 
  1880. $width = $default_width; 
  1881. $height = $default_height; 
  1882.  
  1883. $track['dimensions'] = array( 
  1884. 'original' => compact( 'width', 'height' ),  
  1885. 'resized' => array( 
  1886. 'width' => $theme_width,  
  1887. 'height' => $theme_height 
  1888. ); 
  1889.  
  1890. if ( $atts['images'] ) { 
  1891. $thumb_id = get_post_thumbnail_id( $attachment->ID ); 
  1892. if ( ! empty( $thumb_id ) ) { 
  1893. list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'full' ); 
  1894. $track['image'] = compact( 'src', 'width', 'height' ); 
  1895. list( $src, $width, $height ) = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); 
  1896. $track['thumb'] = compact( 'src', 'width', 'height' ); 
  1897. } else { 
  1898. $src = wp_mime_type_icon( $attachment->ID ); 
  1899. $width = 48; 
  1900. $height = 64; 
  1901. $track['image'] = compact( 'src', 'width', 'height' ); 
  1902. $track['thumb'] = compact( 'src', 'width', 'height' ); 
  1903.  
  1904. $tracks[] = $track; 
  1905. $data['tracks'] = $tracks; 
  1906.  
  1907. $safe_type = esc_attr( $atts['type'] ); 
  1908. $safe_style = esc_attr( $atts['style'] ); 
  1909.  
  1910. ob_start(); 
  1911.  
  1912. if ( 1 === $instance ) { 
  1913. /** 
  1914. * Prints and enqueues playlist scripts, styles, and JavaScript templates. 
  1915. * 
  1916. * @since 3.9.0 
  1917. * 
  1918. * @param string $type Type of playlist. Possible values are 'audio' or 'video'. 
  1919. * @param string $style The 'theme' for the playlist. Core provides 'light' and 'dark'. 
  1920. */ 
  1921. do_action( 'wp_playlist_scripts', $atts['type'], $atts['style'] ); 
  1922. } ?> 
  1923. <div class="wp-playlist wp-<?php echo $safe_type ?>-playlist wp-playlist-<?php echo $safe_style ?>"> 
  1924. <?php if ( 'audio' === $atts['type'] ): ?> 
  1925. <div class="wp-playlist-current-item"></div> 
  1926. <?php endif ?> 
  1927. <<?php echo $safe_type ?> controls="controls" preload="none" width="<?php 
  1928. echo (int) $theme_width; 
  1929. ?>"<?php if ( 'video' === $safe_type ): 
  1930. echo ' height="', (int) $theme_height, '"'; 
  1931. endif; ?>></<?php echo $safe_type ?>> 
  1932. <div class="wp-playlist-next"></div> 
  1933. <div class="wp-playlist-prev"></div> 
  1934. <noscript> 
  1935. <ol><?php 
  1936. foreach ( $attachments as $att_id => $attachment ) { 
  1937. printf( '<li>%s</li>', wp_get_attachment_link( $att_id ) ); 
  1938. ?></ol> 
  1939. </noscript> 
  1940. <script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ) ?></script> 
  1941. </div> 
  1942. <?php 
  1943. return ob_get_clean(); 
  1944. add_shortcode( 'playlist', 'wp_playlist_shortcode' ); 
  1945.  
  1946. /** 
  1947. * Provides a No-JS Flash fallback as a last resort for audio / video. 
  1948. * 
  1949. * @since 3.6.0 
  1950. * 
  1951. * @param string $url The media element URL. 
  1952. * @return string Fallback HTML. 
  1953. */ 
  1954. function wp_mediaelement_fallback( $url ) { 
  1955. /** 
  1956. * Filters the Mediaelement fallback output for no-JS. 
  1957. * 
  1958. * @since 3.6.0 
  1959. * 
  1960. * @param string $output Fallback output for no-JS. 
  1961. * @param string $url Media file URL. 
  1962. */ 
  1963. return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url ); 
  1964.  
  1965. /** 
  1966. * Returns a filtered list of WP-supported audio formats. 
  1967. * 
  1968. * @since 3.6.0 
  1969. * 
  1970. * @return array Supported audio formats. 
  1971. */ 
  1972. function wp_get_audio_extensions() { 
  1973. /** 
  1974. * Filters the list of supported audio formats. 
  1975. * 
  1976. * @since 3.6.0 
  1977. * 
  1978. * @param array $extensions An array of support audio formats. Defaults are 
  1979. * 'mp3', 'ogg', 'wma', 'm4a', 'wav'. 
  1980. */ 
  1981. return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'wma', 'm4a', 'wav' ) ); 
  1982.  
  1983. /** 
  1984. * Returns useful keys to use to lookup data from an attachment's stored metadata. 
  1985. * 
  1986. * @since 3.9.0 
  1987. * 
  1988. * @param WP_Post $attachment The current attachment, provided for context. 
  1989. * @param string $context Optional. The context. Accepts 'edit', 'display'. Default 'display'. 
  1990. * @return array Key/value pairs of field keys to labels. 
  1991. */ 
  1992. function wp_get_attachment_id3_keys( $attachment, $context = 'display' ) { 
  1993. $fields = array( 
  1994. 'artist' => __( 'Artist' ),  
  1995. 'album' => __( 'Album' ),  
  1996. ); 
  1997.  
  1998. if ( 'display' === $context ) { 
  1999. $fields['genre'] = __( 'Genre' ); 
  2000. $fields['year'] = __( 'Year' ); 
  2001. $fields['length_formatted'] = _x( 'Length', 'video or audio' ); 
  2002. } elseif ( 'js' === $context ) { 
  2003. $fields['bitrate'] = __( 'Bitrate' ); 
  2004. $fields['bitrate_mode'] = __( 'Bitrate Mode' ); 
  2005.  
  2006. /** 
  2007. * Filters the editable list of keys to look up data from an attachment's metadata. 
  2008. * 
  2009. * @since 3.9.0 
  2010. * 
  2011. * @param array $fields Key/value pairs of field keys to labels. 
  2012. * @param WP_Post $attachment Attachment object. 
  2013. * @param string $context The context. Accepts 'edit', 'display'. Default 'display'. 
  2014. */ 
  2015. return apply_filters( 'wp_get_attachment_id3_keys', $fields, $attachment, $context ); 
  2016. /** 
  2017. * Builds the Audio shortcode output. 
  2018. * 
  2019. * This implements the functionality of the Audio Shortcode for displaying 
  2020. * WordPress mp3s in a post. 
  2021. * 
  2022. * @since 3.6.0 
  2023. * 
  2024. * @staticvar int $instance 
  2025. * 
  2026. * @param array $attr { 
  2027. * Attributes of the audio shortcode. 
  2028. * 
  2029. * @type string $src URL to the source of the audio file. Default empty. 
  2030. * @type string $loop The 'loop' attribute for the `<audio>` element. Default empty. 
  2031. * @type string $autoplay The 'autoplay' attribute for the `<audio>` element. Default empty. 
  2032. * @type string $preload The 'preload' attribute for the `<audio>` element. Default 'none'. 
  2033. * @type string $class The 'class' attribute for the `<audio>` element. Default 'wp-audio-shortcode'. 
  2034. * @type string $style The 'style' attribute for the `<audio>` element. Default 'width: 100%;'. 
  2035. * } 
  2036. * @param string $content Shortcode content. 
  2037. * @return string|void HTML content to display audio. 
  2038. */ 
  2039. function wp_audio_shortcode( $attr, $content = '' ) { 
  2040. $post_id = get_post() ? get_the_ID() : 0; 
  2041.  
  2042. static $instance = 0; 
  2043. $instance++; 
  2044.  
  2045. /** 
  2046. * Filters the default audio shortcode output. 
  2047. * 
  2048. * If the filtered output isn't empty, it will be used instead of generating the default audio template. 
  2049. * 
  2050. * @since 3.6.0 
  2051. * 
  2052. * @param string $html Empty variable to be replaced with shortcode markup. 
  2053. * @param array $attr Attributes of the shortcode. @see wp_audio_shortcode() 
  2054. * @param string $content Shortcode content. 
  2055. * @param int $instance Unique numeric ID of this audio shortcode instance. 
  2056. */ 
  2057. $override = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instance ); 
  2058. if ( '' !== $override ) { 
  2059. return $override; 
  2060.  
  2061. $audio = null; 
  2062.  
  2063. $default_types = wp_get_audio_extensions(); 
  2064. $defaults_atts = array( 
  2065. 'src' => '',  
  2066. 'loop' => '',  
  2067. 'autoplay' => '',  
  2068. 'preload' => 'none',  
  2069. 'class' => 'wp-audio-shortcode',  
  2070. 'style' => 'width: 100%;' 
  2071. ); 
  2072. foreach ( $default_types as $type ) { 
  2073. $defaults_atts[$type] = ''; 
  2074.  
  2075. $atts = shortcode_atts( $defaults_atts, $attr, 'audio' ); 
  2076.  
  2077. $primary = false; 
  2078. if ( ! empty( $atts['src'] ) ) { 
  2079. $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 
  2080. if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { 
  2081. return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); 
  2082. $primary = true; 
  2083. array_unshift( $default_types, 'src' ); 
  2084. } else { 
  2085. foreach ( $default_types as $ext ) { 
  2086. if ( ! empty( $atts[ $ext ] ) ) { 
  2087. $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); 
  2088. if ( strtolower( $type['ext'] ) === $ext ) { 
  2089. $primary = true; 
  2090.  
  2091. if ( ! $primary ) { 
  2092. $audios = get_attached_media( 'audio', $post_id ); 
  2093. if ( empty( $audios ) ) { 
  2094. return; 
  2095.  
  2096. $audio = reset( $audios ); 
  2097. $atts['src'] = wp_get_attachment_url( $audio->ID ); 
  2098. if ( empty( $atts['src'] ) ) { 
  2099. return; 
  2100.  
  2101. array_unshift( $default_types, 'src' ); 
  2102.  
  2103. /** 
  2104. * Filters the media library used for the audio shortcode. 
  2105. * 
  2106. * @since 3.6.0 
  2107. * 
  2108. * @param string $library Media library used for the audio shortcode. 
  2109. */ 
  2110. $library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' ); 
  2111. if ( 'mediaelement' === $library && did_action( 'init' ) ) { 
  2112. wp_enqueue_style( 'wp-mediaelement' ); 
  2113. wp_enqueue_script( 'wp-mediaelement' ); 
  2114.  
  2115. /** 
  2116. * Filters the class attribute for the audio shortcode output container. 
  2117. * 
  2118. * @since 3.6.0 
  2119. * 
  2120. * @param string $class CSS class or list of space-separated classes. 
  2121. */ 
  2122. $atts['class'] = apply_filters( 'wp_audio_shortcode_class', $atts['class'] ); 
  2123.  
  2124. $html_atts = array( 
  2125. 'class' => $atts['class'],  
  2126. 'id' => sprintf( 'audio-%d-%d', $post_id, $instance ),  
  2127. 'loop' => wp_validate_boolean( $atts['loop'] ),  
  2128. 'autoplay' => wp_validate_boolean( $atts['autoplay'] ),  
  2129. 'preload' => $atts['preload'],  
  2130. 'style' => $atts['style'],  
  2131. ); 
  2132.  
  2133. // These ones should just be omitted altogether if they are blank 
  2134. foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) { 
  2135. if ( empty( $html_atts[$a] ) ) { 
  2136. unset( $html_atts[$a] ); 
  2137.  
  2138. $attr_strings = array(); 
  2139. foreach ( $html_atts as $k => $v ) { 
  2140. $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; 
  2141.  
  2142. $html = ''; 
  2143. if ( 'mediaelement' === $library && 1 === $instance ) { 
  2144. $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n"; 
  2145. $html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) ); 
  2146.  
  2147. $fileurl = ''; 
  2148. $source = '<source type="%s" src="%s" />'; 
  2149. foreach ( $default_types as $fallback ) { 
  2150. if ( ! empty( $atts[ $fallback ] ) ) { 
  2151. if ( empty( $fileurl ) ) { 
  2152. $fileurl = $atts[ $fallback ]; 
  2153. $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); 
  2154. $url = add_query_arg( '_', $instance, $atts[ $fallback ] ); 
  2155. $html .= sprintf( $source, $type['type'], esc_url( $url ) ); 
  2156.  
  2157. if ( 'mediaelement' === $library ) { 
  2158. $html .= wp_mediaelement_fallback( $fileurl ); 
  2159. $html .= '</audio>'; 
  2160.  
  2161. /** 
  2162. * Filters the audio shortcode output. 
  2163. * 
  2164. * @since 3.6.0 
  2165. * 
  2166. * @param string $html Audio shortcode HTML output. 
  2167. * @param array $atts Array of audio shortcode attributes. 
  2168. * @param string $audio Audio file. 
  2169. * @param int $post_id Post ID. 
  2170. * @param string $library Media library used for the audio shortcode. 
  2171. */ 
  2172. return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library ); 
  2173. add_shortcode( 'audio', 'wp_audio_shortcode' ); 
  2174.  
  2175. /** 
  2176. * Returns a filtered list of WP-supported video formats. 
  2177. * 
  2178. * @since 3.6.0 
  2179. * 
  2180. * @return array List of supported video formats. 
  2181. */ 
  2182. function wp_get_video_extensions() { 
  2183. /** 
  2184. * Filters the list of supported video formats. 
  2185. * 
  2186. * @since 3.6.0 
  2187. * 
  2188. * @param array $extensions An array of support video formats. Defaults are 
  2189. * 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv'. 
  2190. */ 
  2191. return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv' ) ); 
  2192.  
  2193. /** 
  2194. * Builds the Video shortcode output. 
  2195. * 
  2196. * This implements the functionality of the Video Shortcode for displaying 
  2197. * WordPress mp4s in a post. 
  2198. * 
  2199. * @since 3.6.0 
  2200. * 
  2201. * @global int $content_width 
  2202. * @staticvar int $instance 
  2203. * 
  2204. * @param array $attr { 
  2205. * Attributes of the shortcode. 
  2206. * 
  2207. * @type string $src URL to the source of the video file. Default empty. 
  2208. * @type int $height Height of the video embed in pixels. Default 360. 
  2209. * @type int $width Width of the video embed in pixels. Default $content_width or 640. 
  2210. * @type string $poster The 'poster' attribute for the `<video>` element. Default empty. 
  2211. * @type string $loop The 'loop' attribute for the `<video>` element. Default empty. 
  2212. * @type string $autoplay The 'autoplay' attribute for the `<video>` element. Default empty. 
  2213. * @type string $preload The 'preload' attribute for the `<video>` element. 
  2214. * Default 'metadata'. 
  2215. * @type string $class The 'class' attribute for the `<video>` element. 
  2216. * Default 'wp-video-shortcode'. 
  2217. * } 
  2218. * @param string $content Shortcode content. 
  2219. * @return string|void HTML content to display video. 
  2220. */ 
  2221. function wp_video_shortcode( $attr, $content = '' ) { 
  2222. global $content_width; 
  2223. $post_id = get_post() ? get_the_ID() : 0; 
  2224.  
  2225. static $instance = 0; 
  2226. $instance++; 
  2227.  
  2228. /** 
  2229. * Filters the default video shortcode output. 
  2230. * 
  2231. * If the filtered output isn't empty, it will be used instead of generating 
  2232. * the default video template. 
  2233. * 
  2234. * @since 3.6.0 
  2235. * 
  2236. * @see wp_video_shortcode() 
  2237. * 
  2238. * @param string $html Empty variable to be replaced with shortcode markup. 
  2239. * @param array $attr Attributes of the video shortcode. 
  2240. * @param string $content Video shortcode content. 
  2241. * @param int $instance Unique numeric ID of this video shortcode instance. 
  2242. */ 
  2243. $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance ); 
  2244. if ( '' !== $override ) { 
  2245. return $override; 
  2246.  
  2247. $video = null; 
  2248.  
  2249. $default_types = wp_get_video_extensions(); 
  2250. $defaults_atts = array( 
  2251. 'src' => '',  
  2252. 'poster' => '',  
  2253. 'loop' => '',  
  2254. 'autoplay' => '',  
  2255. 'preload' => 'metadata',  
  2256. 'width' => 640,  
  2257. 'height' => 360,  
  2258. 'class' => 'wp-video-shortcode',  
  2259. ); 
  2260.  
  2261. foreach ( $default_types as $type ) { 
  2262. $defaults_atts[$type] = ''; 
  2263.  
  2264. $atts = shortcode_atts( $defaults_atts, $attr, 'video' ); 
  2265.  
  2266. if ( is_admin() ) { 
  2267. // shrink the video so it isn't huge in the admin 
  2268. if ( $atts['width'] > $defaults_atts['width'] ) { 
  2269. $atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] ); 
  2270. $atts['width'] = $defaults_atts['width']; 
  2271. } else { 
  2272. // if the video is bigger than the theme 
  2273. if ( ! empty( $content_width ) && $atts['width'] > $content_width ) { 
  2274. $atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] ); 
  2275. $atts['width'] = $content_width; 
  2276.  
  2277. $is_vimeo = $is_youtube = false; 
  2278. $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; 
  2279. $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#'; 
  2280.  
  2281. $primary = false; 
  2282. if ( ! empty( $atts['src'] ) ) { 
  2283. $is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) ); 
  2284. $is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) ); 
  2285. if ( ! $is_youtube && ! $is_vimeo ) { 
  2286. $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); 
  2287. if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { 
  2288. return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); 
  2289.  
  2290. if ( $is_vimeo ) { 
  2291. wp_enqueue_script( 'froogaloop' ); 
  2292.  
  2293. $primary = true; 
  2294. array_unshift( $default_types, 'src' ); 
  2295. } else { 
  2296. foreach ( $default_types as $ext ) { 
  2297. if ( ! empty( $atts[ $ext ] ) ) { 
  2298. $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); 
  2299. if ( strtolower( $type['ext'] ) === $ext ) { 
  2300. $primary = true; 
  2301.  
  2302. if ( ! $primary ) { 
  2303. $videos = get_attached_media( 'video', $post_id ); 
  2304. if ( empty( $videos ) ) { 
  2305. return; 
  2306.  
  2307. $video = reset( $videos ); 
  2308. $atts['src'] = wp_get_attachment_url( $video->ID ); 
  2309. if ( empty( $atts['src'] ) ) { 
  2310. return; 
  2311.  
  2312. array_unshift( $default_types, 'src' ); 
  2313.  
  2314. /** 
  2315. * Filters the media library used for the video shortcode. 
  2316. * 
  2317. * @since 3.6.0 
  2318. * 
  2319. * @param string $library Media library used for the video shortcode. 
  2320. */ 
  2321. $library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); 
  2322. if ( 'mediaelement' === $library && did_action( 'init' ) ) { 
  2323. wp_enqueue_style( 'wp-mediaelement' ); 
  2324. wp_enqueue_script( 'wp-mediaelement' ); 
  2325.  
  2326. /** 
  2327. * Filters the class attribute for the video shortcode output container. 
  2328. * 
  2329. * @since 3.6.0 
  2330. * 
  2331. * @param string $class CSS class or list of space-separated classes. 
  2332. */ 
  2333. $atts['class'] = apply_filters( 'wp_video_shortcode_class', $atts['class'] ); 
  2334.  
  2335. $html_atts = array( 
  2336. 'class' => $atts['class'],  
  2337. 'id' => sprintf( 'video-%d-%d', $post_id, $instance ),  
  2338. 'width' => absint( $atts['width'] ),  
  2339. 'height' => absint( $atts['height'] ),  
  2340. 'poster' => esc_url( $atts['poster'] ),  
  2341. 'loop' => wp_validate_boolean( $atts['loop'] ),  
  2342. 'autoplay' => wp_validate_boolean( $atts['autoplay'] ),  
  2343. 'preload' => $atts['preload'],  
  2344. ); 
  2345.  
  2346. // These ones should just be omitted altogether if they are blank 
  2347. foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) { 
  2348. if ( empty( $html_atts[$a] ) ) { 
  2349. unset( $html_atts[$a] ); 
  2350.  
  2351. $attr_strings = array(); 
  2352. foreach ( $html_atts as $k => $v ) { 
  2353. $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; 
  2354.  
  2355. $html = ''; 
  2356. if ( 'mediaelement' === $library && 1 === $instance ) { 
  2357. $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n"; 
  2358. $html .= sprintf( '<video %s controls="controls">', join( ' ', $attr_strings ) ); 
  2359.  
  2360. $fileurl = ''; 
  2361. $source = '<source type="%s" src="%s" />'; 
  2362. foreach ( $default_types as $fallback ) { 
  2363. if ( ! empty( $atts[ $fallback ] ) ) { 
  2364. if ( empty( $fileurl ) ) { 
  2365. $fileurl = $atts[ $fallback ]; 
  2366. if ( 'src' === $fallback && $is_youtube ) { 
  2367. $type = array( 'type' => 'video/youtube' ); 
  2368. } elseif ( 'src' === $fallback && $is_vimeo ) { 
  2369. $type = array( 'type' => 'video/vimeo' ); 
  2370. } else { 
  2371. $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); 
  2372. $url = add_query_arg( '_', $instance, $atts[ $fallback ] ); 
  2373. $html .= sprintf( $source, $type['type'], esc_url( $url ) ); 
  2374.  
  2375. if ( ! empty( $content ) ) { 
  2376. if ( false !== strpos( $content, "\n" ) ) { 
  2377. $content = str_replace( array( "\r\n", "\n", "\t" ), '', $content ); 
  2378. $html .= trim( $content ); 
  2379.  
  2380. if ( 'mediaelement' === $library ) { 
  2381. $html .= wp_mediaelement_fallback( $fileurl ); 
  2382. $html .= '</video>'; 
  2383.  
  2384. $width_rule = ''; 
  2385. if ( ! empty( $atts['width'] ) ) { 
  2386. $width_rule = sprintf( 'width: %dpx;', $atts['width'] ); 
  2387. $output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_rule, $html ); 
  2388.  
  2389. /** 
  2390. * Filters the output of the video shortcode. 
  2391. * 
  2392. * @since 3.6.0 
  2393. * 
  2394. * @param string $output Video shortcode HTML output. 
  2395. * @param array $atts Array of video shortcode attributes. 
  2396. * @param string $video Video file. 
  2397. * @param int $post_id Post ID. 
  2398. * @param string $library Media library used for the video shortcode. 
  2399. */ 
  2400. return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); 
  2401. add_shortcode( 'video', 'wp_video_shortcode' ); 
  2402.  
  2403. /** 
  2404. * Displays previous image link that has the same post parent. 
  2405. * 
  2406. * @since 2.5.0 
  2407. * 
  2408. * @see adjacent_image_link() 
  2409. * 
  2410. * @param string|array $size Optional. Image size. Accepts any valid image size, an array of width and 
  2411. * height values in pixels (in that order), 0, or 'none'. 0 or 'none' will 
  2412. * default to 'post_title' or `$text`. Default 'thumbnail'. 
  2413. * @param string $text Optional. Link text. Default false. 
  2414. */ 
  2415. function previous_image_link( $size = 'thumbnail', $text = false ) { 
  2416. adjacent_image_link(true, $size, $text); 
  2417.  
  2418. /** 
  2419. * Displays next image link that has the same post parent. 
  2420. * 
  2421. * @since 2.5.0 
  2422. * 
  2423. * @see adjacent_image_link() 
  2424. * 
  2425. * @param string|array $size Optional. Image size. Accepts any valid image size, an array of width and 
  2426. * height values in pixels (in that order), 0, or 'none'. 0 or 'none' will 
  2427. * default to 'post_title' or `$text`. Default 'thumbnail'. 
  2428. * @param string $text Optional. Link text. Default false. 
  2429. */ 
  2430. function next_image_link( $size = 'thumbnail', $text = false ) { 
  2431. adjacent_image_link(false, $size, $text); 
  2432.  
  2433. /** 
  2434. * Displays next or previous image link that has the same post parent. 
  2435. * 
  2436. * Retrieves the current attachment object from the $post global. 
  2437. * 
  2438. * @since 2.5.0 
  2439. * 
  2440. * @param bool $prev Optional. Whether to display the next (false) or previous (true) link. Default true. 
  2441. * @param string|array $size Optional. Image size. Accepts any valid image size, or an array of width and height 
  2442. * values in pixels (in that order). Default 'thumbnail'. 
  2443. * @param bool $text Optional. Link text. Default false. 
  2444. */ 
  2445. function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false ) { 
  2446. $post = get_post(); 
  2447. $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) ); 
  2448.  
  2449. foreach ( $attachments as $k => $attachment ) { 
  2450. if ( $attachment->ID == $post->ID ) { 
  2451. break; 
  2452.  
  2453. $output = ''; 
  2454. $attachment_id = 0; 
  2455.  
  2456. if ( $attachments ) { 
  2457. $k = $prev ? $k - 1 : $k + 1; 
  2458.  
  2459. if ( isset( $attachments[ $k ] ) ) { 
  2460. $attachment_id = $attachments[ $k ]->ID; 
  2461. $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); 
  2462.  
  2463. $adjacent = $prev ? 'previous' : 'next'; 
  2464.  
  2465. /** 
  2466. * Filters the adjacent image link. 
  2467. * 
  2468. * The dynamic portion of the hook name, `$adjacent`, refers to the type of adjacency,  
  2469. * either 'next', or 'previous'. 
  2470. * 
  2471. * @since 3.5.0 
  2472. * 
  2473. * @param string $output Adjacent image HTML markup. 
  2474. * @param int $attachment_id Attachment ID 
  2475. * @param string $size Image size. 
  2476. * @param string $text Link text. 
  2477. */ 
  2478. echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); 
  2479.  
  2480. /** 
  2481. * Retrieves taxonomies attached to given the attachment. 
  2482. * 
  2483. * @since 2.5.0 
  2484. * @since 4.7.0 Introduced the `$output` parameter. 
  2485. * 
  2486. * @param int|array|object $attachment Attachment ID, data array, or data object. 
  2487. * @param string $output Output type. 'names' to return an array of taxonomy names,  
  2488. * or 'objects' to return an array of taxonomy objects. 
  2489. * Default is 'names'. 
  2490. * @return array Empty array on failure. List of taxonomies on success. 
  2491. */ 
  2492. function get_attachment_taxonomies( $attachment, $output = 'names' ) { 
  2493. if ( is_int( $attachment ) ) { 
  2494. $attachment = get_post( $attachment ); 
  2495. } elseif ( is_array( $attachment ) ) { 
  2496. $attachment = (object) $attachment; 
  2497. if ( ! is_object($attachment) ) 
  2498. return array(); 
  2499.  
  2500. $file = get_attached_file( $attachment->ID ); 
  2501. $filename = basename( $file ); 
  2502.  
  2503. $objects = array('attachment'); 
  2504.  
  2505. if ( false !== strpos($filename, '.') ) 
  2506. $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); 
  2507. if ( !empty($attachment->post_mime_type) ) { 
  2508. $objects[] = 'attachment:' . $attachment->post_mime_type; 
  2509. if ( false !== strpos($attachment->post_mime_type, '/') ) 
  2510. foreach ( explode('/', $attachment->post_mime_type) as $token ) 
  2511. if ( !empty($token) ) 
  2512. $objects[] = "attachment:$token"; 
  2513.  
  2514. $taxonomies = array(); 
  2515. foreach ( $objects as $object ) { 
  2516. if ( $taxes = get_object_taxonomies( $object, $output ) ) { 
  2517. $taxonomies = array_merge( $taxonomies, $taxes ); 
  2518.  
  2519. if ( 'names' === $output ) { 
  2520. $taxonomies = array_unique( $taxonomies ); 
  2521.  
  2522. return $taxonomies; 
  2523.  
  2524. /** 
  2525. * Retrieves all of the taxonomy names that are registered for attachments. 
  2526. * 
  2527. * Handles mime-type-specific taxonomies such as attachment:image and attachment:video. 
  2528. * 
  2529. * @since 3.5.0 
  2530. * 
  2531. * @see get_taxonomies() 
  2532. * 
  2533. * @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'. 
  2534. * Default 'names'. 
  2535. * @return array The names of all taxonomy of $object_type. 
  2536. */ 
  2537. function get_taxonomies_for_attachments( $output = 'names' ) { 
  2538. $taxonomies = array(); 
  2539. foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) { 
  2540. foreach ( $taxonomy->object_type as $object_type ) { 
  2541. if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) { 
  2542. if ( 'names' == $output ) 
  2543. $taxonomies[] = $taxonomy->name; 
  2544. else 
  2545. $taxonomies[ $taxonomy->name ] = $taxonomy; 
  2546. break; 
  2547.  
  2548. return $taxonomies; 
  2549.  
  2550. /** 
  2551. * Create new GD image resource with transparency support 
  2552. * 
  2553. * @todo: Deprecate if possible. 
  2554. * 
  2555. * @since 2.9.0 
  2556. * 
  2557. * @param int $width Image width in pixels. 
  2558. * @param int $height Image height in pixels.. 
  2559. * @return resource The GD image resource. 
  2560. */ 
  2561. function wp_imagecreatetruecolor($width, $height) { 
  2562. $img = imagecreatetruecolor($width, $height); 
  2563. if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) { 
  2564. imagealphablending($img, false); 
  2565. imagesavealpha($img, true); 
  2566. return $img; 
  2567.  
  2568. /** 
  2569. * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. 
  2570. * 
  2571. * @since 2.9.0 
  2572. * 
  2573. * @see wp_constrain_dimensions() 
  2574. * 
  2575. * @param int $example_width The width of an example embed. 
  2576. * @param int $example_height The height of an example embed. 
  2577. * @param int $max_width The maximum allowed width. 
  2578. * @param int $max_height The maximum allowed height. 
  2579. * @return array The maximum possible width and height based on the example ratio. 
  2580. */ 
  2581. function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) { 
  2582. $example_width = (int) $example_width; 
  2583. $example_height = (int) $example_height; 
  2584. $max_width = (int) $max_width; 
  2585. $max_height = (int) $max_height; 
  2586.  
  2587. return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); 
  2588.  
  2589. /** 
  2590. * Determines the maximum upload size allowed in php.ini. 
  2591. * 
  2592. * @since 2.5.0 
  2593. * 
  2594. * @return int Allowed upload size. 
  2595. */ 
  2596. function wp_max_upload_size() { 
  2597. $u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) ); 
  2598. $p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) ); 
  2599.  
  2600. /** 
  2601. * Filters the maximum upload size allowed in php.ini. 
  2602. * 
  2603. * @since 2.5.0 
  2604. * 
  2605. * @param int $size Max upload size limit in bytes. 
  2606. * @param int $u_bytes Maximum upload filesize in bytes. 
  2607. * @param int $p_bytes Maximum size of POST data in bytes. 
  2608. */ 
  2609. return apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes ); 
  2610.  
  2611. /** 
  2612. * Returns a WP_Image_Editor instance and loads file into it. 
  2613. * 
  2614. * @since 3.5.0 
  2615. * 
  2616. * @param string $path Path to the file to load. 
  2617. * @param array $args Optional. Additional arguments for retrieving the image editor. 
  2618. * Default empty array. 
  2619. * @return WP_Image_Editor|WP_Error The WP_Image_Editor object if successful, an WP_Error 
  2620. * object otherwise. 
  2621. */ 
  2622. function wp_get_image_editor( $path, $args = array() ) { 
  2623. $args['path'] = $path; 
  2624.  
  2625. if ( ! isset( $args['mime_type'] ) ) { 
  2626. $file_info = wp_check_filetype( $args['path'] ); 
  2627.  
  2628. // If $file_info['type'] is false, then we let the editor attempt to 
  2629. // figure out the file type, rather than forcing a failure based on extension. 
  2630. if ( isset( $file_info ) && $file_info['type'] ) 
  2631. $args['mime_type'] = $file_info['type']; 
  2632.  
  2633. $implementation = _wp_image_editor_choose( $args ); 
  2634.  
  2635. if ( $implementation ) { 
  2636. $editor = new $implementation( $path ); 
  2637. $loaded = $editor->load(); 
  2638.  
  2639. if ( is_wp_error( $loaded ) ) 
  2640. return $loaded; 
  2641.  
  2642. return $editor; 
  2643.  
  2644. return new WP_Error( 'image_no_editor', __('No editor could be selected.') ); 
  2645.  
  2646. /** 
  2647. * Tests whether there is an editor that supports a given mime type or methods. 
  2648. * 
  2649. * @since 3.5.0 
  2650. * 
  2651. * @param string|array $args Optional. Array of arguments to retrieve the image editor supports. 
  2652. * Default empty array. 
  2653. * @return bool True if an eligible editor is found; false otherwise. 
  2654. */ 
  2655. function wp_image_editor_supports( $args = array() ) { 
  2656. return (bool) _wp_image_editor_choose( $args ); 
  2657.  
  2658. /** 
  2659. * Tests which editors are capable of supporting the request. 
  2660. * 
  2661. * @ignore 
  2662. * @since 3.5.0 
  2663. * 
  2664. * @param array $args Optional. Array of arguments for choosing a capable editor. Default empty array. 
  2665. * @return string|false Class name for the first editor that claims to support the request. False if no 
  2666. * editor claims to support the request. 
  2667. */ 
  2668. function _wp_image_editor_choose( $args = array() ) { 
  2669. require_once ABSPATH . WPINC . '/class-wp-image-editor.php'; 
  2670. require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php'; 
  2671. require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php'; 
  2672. /** 
  2673. * Filters the list of image editing library classes. 
  2674. * 
  2675. * @since 3.5.0 
  2676. * 
  2677. * @param array $image_editors List of available image editors. Defaults are 
  2678. * 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'. 
  2679. */ 
  2680. $implementations = apply_filters( 'wp_image_editors', array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) ); 
  2681.  
  2682. foreach ( $implementations as $implementation ) { 
  2683. if ( ! call_user_func( array( $implementation, 'test' ), $args ) ) 
  2684. continue; 
  2685.  
  2686. if ( isset( $args['mime_type'] ) && 
  2687. ! call_user_func( 
  2688. array( $implementation, 'supports_mime_type' ),  
  2689. $args['mime_type'] ) ) { 
  2690. continue; 
  2691.  
  2692. if ( isset( $args['methods'] ) && 
  2693. array_diff( $args['methods'], get_class_methods( $implementation ) ) ) { 
  2694. continue; 
  2695.  
  2696. return $implementation; 
  2697.  
  2698. return false; 
  2699.  
  2700. /** 
  2701. * Prints default Plupload arguments. 
  2702. * 
  2703. * @since 3.4.0 
  2704. */ 
  2705. function wp_plupload_default_settings() { 
  2706. $wp_scripts = wp_scripts(); 
  2707.  
  2708. $data = $wp_scripts->get_data( 'wp-plupload', 'data' ); 
  2709. if ( $data && false !== strpos( $data, '_wpPluploadSettings' ) ) 
  2710. return; 
  2711.  
  2712. $max_upload_size = wp_max_upload_size(); 
  2713. $allowed_extensions = array_keys( get_allowed_mime_types() ); 
  2714. $extensions = array(); 
  2715. foreach ( $allowed_extensions as $extension ) { 
  2716. $extensions = array_merge( $extensions, explode( '|', $extension ) ); 
  2717.  
  2718. $defaults = array( 
  2719. 'runtimes' => 'html5, flash, silverlight, html4',  
  2720. 'file_data_name' => 'async-upload', // key passed to $_FILE. 
  2721. 'url' => admin_url( 'async-upload.php', 'relative' ),  
  2722. 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),  
  2723. 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),  
  2724. 'filters' => array( 
  2725. 'max_file_size' => $max_upload_size . 'b',  
  2726. 'mime_types' => array( array( 'extensions' => implode( ', ', $extensions ) ) ),  
  2727. ),  
  2728. ); 
  2729.  
  2730. // Currently only iOS Safari supports multiple files uploading but iOS 7.x has a bug that prevents uploading of videos 
  2731. // when enabled. See #29602. 
  2732. if ( wp_is_mobile() && strpos( $_SERVER['HTTP_USER_AGENT'], 'OS 7_' ) !== false && 
  2733. strpos( $_SERVER['HTTP_USER_AGENT'], 'like Mac OS X' ) !== false ) { 
  2734.  
  2735. $defaults['multi_selection'] = false; 
  2736.  
  2737. /** 
  2738. * Filters the Plupload default settings. 
  2739. * 
  2740. * @since 3.4.0 
  2741. * 
  2742. * @param array $defaults Default Plupload settings array. 
  2743. */ 
  2744. $defaults = apply_filters( 'plupload_default_settings', $defaults ); 
  2745.  
  2746. $params = array( 
  2747. 'action' => 'upload-attachment',  
  2748. ); 
  2749.  
  2750. /** 
  2751. * Filters the Plupload default parameters. 
  2752. * 
  2753. * @since 3.4.0 
  2754. * 
  2755. * @param array $params Default Plupload parameters array. 
  2756. */ 
  2757. $params = apply_filters( 'plupload_default_params', $params ); 
  2758. $params['_wpnonce'] = wp_create_nonce( 'media-form' ); 
  2759. $defaults['multipart_params'] = $params; 
  2760.  
  2761. $settings = array( 
  2762. 'defaults' => $defaults,  
  2763. 'browser' => array( 
  2764. 'mobile' => wp_is_mobile(),  
  2765. 'supported' => _device_can_upload(),  
  2766. ),  
  2767. 'limitExceeded' => is_multisite() && ! is_upload_space_available() 
  2768. ); 
  2769.  
  2770. $script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';'; 
  2771.  
  2772. if ( $data ) 
  2773. $script = "$data\n$script"; 
  2774.  
  2775. $wp_scripts->add_data( 'wp-plupload', 'data', $script ); 
  2776.  
  2777. /** 
  2778. * Prepares an attachment post object for JS, where it is expected 
  2779. * to be JSON-encoded and fit into an Attachment model. 
  2780. * 
  2781. * @since 3.5.0 
  2782. * 
  2783. * @param mixed $attachment Attachment ID or object. 
  2784. * @return array|void Array of attachment details. 
  2785. */ 
  2786. function wp_prepare_attachment_for_js( $attachment ) { 
  2787. if ( ! $attachment = get_post( $attachment ) ) 
  2788. return; 
  2789.  
  2790. if ( 'attachment' != $attachment->post_type ) 
  2791. return; 
  2792.  
  2793. $meta = wp_get_attachment_metadata( $attachment->ID ); 
  2794. if ( false !== strpos( $attachment->post_mime_type, '/' ) ) 
  2795. list( $type, $subtype ) = explode( '/', $attachment->post_mime_type ); 
  2796. else 
  2797. list( $type, $subtype ) = array( $attachment->post_mime_type, '' ); 
  2798.  
  2799. $attachment_url = wp_get_attachment_url( $attachment->ID ); 
  2800. $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); 
  2801.  
  2802. $response = array( 
  2803. 'id' => $attachment->ID,  
  2804. 'title' => $attachment->post_title,  
  2805. 'filename' => wp_basename( get_attached_file( $attachment->ID ) ),  
  2806. 'url' => $attachment_url,  
  2807. 'link' => get_attachment_link( $attachment->ID ),  
  2808. 'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),  
  2809. 'author' => $attachment->post_author,  
  2810. 'description' => $attachment->post_content,  
  2811. 'caption' => $attachment->post_excerpt,  
  2812. 'name' => $attachment->post_name,  
  2813. 'status' => $attachment->post_status,  
  2814. 'uploadedTo' => $attachment->post_parent,  
  2815. 'date' => strtotime( $attachment->post_date_gmt ) * 1000,  
  2816. 'modified' => strtotime( $attachment->post_modified_gmt ) * 1000,  
  2817. 'menuOrder' => $attachment->menu_order,  
  2818. 'mime' => $attachment->post_mime_type,  
  2819. 'type' => $type,  
  2820. 'subtype' => $subtype,  
  2821. 'icon' => wp_mime_type_icon( $attachment->ID ),  
  2822. 'dateFormatted' => mysql2date( __( 'F j, Y' ), $attachment->post_date ),  
  2823. 'nonces' => array( 
  2824. 'update' => false,  
  2825. 'delete' => false,  
  2826. 'edit' => false 
  2827. ),  
  2828. 'editLink' => false,  
  2829. 'meta' => false,  
  2830. ); 
  2831.  
  2832. $author = new WP_User( $attachment->post_author ); 
  2833. $response['authorName'] = $author->display_name; 
  2834.  
  2835. if ( $attachment->post_parent ) { 
  2836. $post_parent = get_post( $attachment->post_parent ); 
  2837. } else { 
  2838. $post_parent = false; 
  2839.  
  2840. if ( $post_parent ) { 
  2841. $parent_type = get_post_type_object( $post_parent->post_type ); 
  2842.  
  2843. if ( $parent_type && $parent_type->show_ui && current_user_can( 'edit_post', $attachment->post_parent ) ) { 
  2844. $response['uploadedToLink'] = get_edit_post_link( $attachment->post_parent, 'raw' ); 
  2845.  
  2846. if ( $parent_type && current_user_can( 'read_post', $attachment->post_parent ) ) { 
  2847. $response['uploadedToTitle'] = $post_parent->post_title ? $post_parent->post_title : __( '(no title)' ); 
  2848.  
  2849. $attached_file = get_attached_file( $attachment->ID ); 
  2850.  
  2851. if ( isset( $meta['filesize'] ) ) { 
  2852. $bytes = $meta['filesize']; 
  2853. } elseif ( file_exists( $attached_file ) ) { 
  2854. $bytes = filesize( $attached_file ); 
  2855. } else { 
  2856. $bytes = ''; 
  2857.  
  2858. if ( $bytes ) { 
  2859. $response['filesizeInBytes'] = $bytes; 
  2860. $response['filesizeHumanReadable'] = size_format( $bytes ); 
  2861.  
  2862. if ( current_user_can( 'edit_post', $attachment->ID ) ) { 
  2863. $response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID ); 
  2864. $response['nonces']['edit'] = wp_create_nonce( 'image_editor-' . $attachment->ID ); 
  2865. $response['editLink'] = get_edit_post_link( $attachment->ID, 'raw' ); 
  2866.  
  2867. if ( current_user_can( 'delete_post', $attachment->ID ) ) 
  2868. $response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID ); 
  2869.  
  2870. if ( $meta && ( 'image' === $type || ! empty( $meta['sizes'] ) ) ) { 
  2871. $sizes = array(); 
  2872.  
  2873. /** This filter is documented in wp-admin/includes/media.php */ 
  2874. $possible_sizes = apply_filters( 'image_size_names_choose', array( 
  2875. 'thumbnail' => __('Thumbnail'),  
  2876. 'medium' => __('Medium'),  
  2877. 'large' => __('Large'),  
  2878. 'full' => __('Full Size'),  
  2879. ) ); 
  2880. unset( $possible_sizes['full'] ); 
  2881.  
  2882. // Loop through all potential sizes that may be chosen. Try to do this with some efficiency. 
  2883. // First: run the image_downsize filter. If it returns something, we can use its data. 
  2884. // If the filter does not return something, then image_downsize() is just an expensive 
  2885. // way to check the image metadata, which we do second. 
  2886. foreach ( $possible_sizes as $size => $label ) { 
  2887.  
  2888. /** This filter is documented in wp-includes/media.php */ 
  2889. if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) { 
  2890. if ( empty( $downsize[3] ) ) { 
  2891. continue; 
  2892.  
  2893. $sizes[ $size ] = array( 
  2894. 'height' => $downsize[2],  
  2895. 'width' => $downsize[1],  
  2896. 'url' => $downsize[0],  
  2897. 'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape',  
  2898. ); 
  2899. } elseif ( isset( $meta['sizes'][ $size ] ) ) { 
  2900. // Nothing from the filter, so consult image metadata if we have it. 
  2901. $size_meta = $meta['sizes'][ $size ]; 
  2902.  
  2903. // We have the actual image size, but might need to further constrain it if content_width is narrower. 
  2904. // Thumbnail, medium, and full sizes are also checked against the site's height/width options. 
  2905. list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' ); 
  2906.  
  2907. $sizes[ $size ] = array( 
  2908. 'height' => $height,  
  2909. 'width' => $width,  
  2910. 'url' => $base_url . $size_meta['file'],  
  2911. 'orientation' => $height > $width ? 'portrait' : 'landscape',  
  2912. ); 
  2913.  
  2914. if ( 'image' === $type ) { 
  2915. $sizes['full'] = array( 'url' => $attachment_url ); 
  2916.  
  2917. if ( isset( $meta['height'], $meta['width'] ) ) { 
  2918. $sizes['full']['height'] = $meta['height']; 
  2919. $sizes['full']['width'] = $meta['width']; 
  2920. $sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape'; 
  2921.  
  2922. $response = array_merge( $response, $sizes['full'] ); 
  2923. } elseif ( $meta['sizes']['full']['file'] ) { 
  2924. $sizes['full'] = array( 
  2925. 'url' => $base_url . $meta['sizes']['full']['file'],  
  2926. 'height' => $meta['sizes']['full']['height'],  
  2927. 'width' => $meta['sizes']['full']['width'],  
  2928. 'orientation' => $meta['sizes']['full']['height'] > $meta['sizes']['full']['width'] ? 'portrait' : 'landscape' 
  2929. ); 
  2930.  
  2931. $response = array_merge( $response, array( 'sizes' => $sizes ) ); 
  2932.  
  2933. if ( $meta && 'video' === $type ) { 
  2934. if ( isset( $meta['width'] ) ) 
  2935. $response['width'] = (int) $meta['width']; 
  2936. if ( isset( $meta['height'] ) ) 
  2937. $response['height'] = (int) $meta['height']; 
  2938.  
  2939. if ( $meta && ( 'audio' === $type || 'video' === $type ) ) { 
  2940. if ( isset( $meta['length_formatted'] ) ) 
  2941. $response['fileLength'] = $meta['length_formatted']; 
  2942.  
  2943. $response['meta'] = array(); 
  2944. foreach ( wp_get_attachment_id3_keys( $attachment, 'js' ) as $key => $label ) { 
  2945. $response['meta'][ $key ] = false; 
  2946.  
  2947. if ( ! empty( $meta[ $key ] ) ) { 
  2948. $response['meta'][ $key ] = $meta[ $key ]; 
  2949.  
  2950. $id = get_post_thumbnail_id( $attachment->ID ); 
  2951. if ( ! empty( $id ) ) { 
  2952. list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'full' ); 
  2953. $response['image'] = compact( 'src', 'width', 'height' ); 
  2954. list( $src, $width, $height ) = wp_get_attachment_image_src( $id, 'thumbnail' ); 
  2955. $response['thumb'] = compact( 'src', 'width', 'height' ); 
  2956. } else { 
  2957. $src = wp_mime_type_icon( $attachment->ID ); 
  2958. $width = 48; 
  2959. $height = 64; 
  2960. $response['image'] = compact( 'src', 'width', 'height' ); 
  2961. $response['thumb'] = compact( 'src', 'width', 'height' ); 
  2962.  
  2963. if ( function_exists('get_compat_media_markup') ) 
  2964. $response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) ); 
  2965.  
  2966. /** 
  2967. * Filters the attachment data prepared for JavaScript. 
  2968. * 
  2969. * @since 3.5.0 
  2970. * 
  2971. * @param array $response Array of prepared attachment data. 
  2972. * @param int|object $attachment Attachment ID or object. 
  2973. * @param array $meta Array of attachment meta data. 
  2974. */ 
  2975. return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta ); 
  2976.  
  2977. /** 
  2978. * Enqueues all scripts, styles, settings, and templates necessary to use 
  2979. * all media JS APIs. 
  2980. * 
  2981. * @since 3.5.0 
  2982. * 
  2983. * @global int $content_width 
  2984. * @global wpdb $wpdb 
  2985. * @global WP_Locale $wp_locale 
  2986. * 
  2987. * @param array $args { 
  2988. * Arguments for enqueuing media scripts. 
  2989. * 
  2990. * @type int|WP_Post A post object or ID. 
  2991. * } 
  2992. */ 
  2993. function wp_enqueue_media( $args = array() ) { 
  2994. // Enqueue me just once per page, please. 
  2995. if ( did_action( 'wp_enqueue_media' ) ) 
  2996. return; 
  2997.  
  2998. global $content_width, $wpdb, $wp_locale; 
  2999.  
  3000. $defaults = array( 
  3001. 'post' => null,  
  3002. ); 
  3003. $args = wp_parse_args( $args, $defaults ); 
  3004.  
  3005. // We're going to pass the old thickbox media tabs to `media_upload_tabs` 
  3006. // to ensure plugins will work. We will then unset those tabs. 
  3007. $tabs = array( 
  3008. // handler action suffix => tab label 
  3009. 'type' => '',  
  3010. 'type_url' => '',  
  3011. 'gallery' => '',  
  3012. 'library' => '',  
  3013. ); 
  3014.  
  3015. /** This filter is documented in wp-admin/includes/media.php */ 
  3016. $tabs = apply_filters( 'media_upload_tabs', $tabs ); 
  3017. unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] ); 
  3018.  
  3019. $props = array( 
  3020. 'link' => get_option( 'image_default_link_type' ), // db default is 'file' 
  3021. 'align' => get_option( 'image_default_align' ), // empty default 
  3022. 'size' => get_option( 'image_default_size' ), // empty default 
  3023. ); 
  3024.  
  3025. $exts = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() ); 
  3026. $mimes = get_allowed_mime_types(); 
  3027. $ext_mimes = array(); 
  3028. foreach ( $exts as $ext ) { 
  3029. foreach ( $mimes as $ext_preg => $mime_match ) { 
  3030. if ( preg_match( '#' . $ext . '#i', $ext_preg ) ) { 
  3031. $ext_mimes[ $ext ] = $mime_match; 
  3032. break; 
  3033.  
  3034. /** 
  3035. * Allows showing or hiding the "Create Audio Playlist" button in the media library. 
  3036. * 
  3037. * By default (if this filter returns `null`), a query will be run to 
  3038. * determine whether the media library contains any audio items. This 
  3039. * query is expensive for large media libraries, so it may be desirable for 
  3040. * sites to override this behavior. 
  3041. * 
  3042. * @since 4.7.4 
  3043. * 
  3044. * @link https://core.trac.wordpress.org/ticket/31071 
  3045. * 
  3046. * @param bool|null Whether to show the button, or `null` for default behavior. 
  3047. */ 
  3048. $show_audio_playlist = apply_filters( 'media_library_show_audio_playlist', null ); 
  3049. if ( null === $show_audio_playlist ) { 
  3050. $show_audio_playlist = $wpdb->get_var( " 
  3051. SELECT ID 
  3052. FROM $wpdb->posts 
  3053. WHERE post_type = 'attachment' 
  3054. AND post_mime_type LIKE 'audio%' 
  3055. LIMIT 1 
  3056. " ); 
  3057.  
  3058. /** 
  3059. * Allows showing or hiding the "Create Video Playlist" button in the media library. 
  3060. * 
  3061. * By default (if this filter returns `null`), a query will be run to 
  3062. * determine whether the media library contains any video items. This 
  3063. * query is expensive for large media libraries, so it may be desirable for 
  3064. * sites to override this behavior. 
  3065. * 
  3066. * @since 4.7.4 
  3067. * 
  3068. * @link https://core.trac.wordpress.org/ticket/31071 
  3069. * 
  3070. * @param bool|null Whether to show the button, or `null` for default behavior. 
  3071. */ 
  3072. $show_video_playlist = apply_filters( 'media_library_show_video_playlist', null ); 
  3073. if ( null === $show_video_playlist ) { 
  3074. $show_video_playlist = $wpdb->get_var( " 
  3075. SELECT ID 
  3076. FROM $wpdb->posts 
  3077. WHERE post_type = 'attachment' 
  3078. AND post_mime_type LIKE 'video%' 
  3079. LIMIT 1 
  3080. " ); 
  3081.  
  3082. /** 
  3083. * Allows overriding the list of months displayed in the media library. 
  3084. * 
  3085. * By default (if this filter does not return an array), a query will be 
  3086. * run to determine the months that have media items. This query can be 
  3087. * expensive for large media libraries, so it may be desirable for sites to 
  3088. * override this behavior. 
  3089. * 
  3090. * @since 4.7.4 
  3091. * 
  3092. * @link https://core.trac.wordpress.org/ticket/31071 
  3093. * 
  3094. * @param array|null An array of objects with `month` and `year` 
  3095. * properties, or `null` (or any other non-array value) 
  3096. * for default behavior. 
  3097. */ 
  3098. $months = apply_filters( 'media_library_months_with_files', null ); 
  3099. if ( ! is_array( $months ) ) { 
  3100. $months = $wpdb->get_results( $wpdb->prepare( " 
  3101. SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month 
  3102. FROM $wpdb->posts 
  3103. WHERE post_type = %s 
  3104. ORDER BY post_date DESC 
  3105. ", 'attachment' ) ); 
  3106. foreach ( $months as $month_year ) { 
  3107. $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); 
  3108.  
  3109. $settings = array( 
  3110. 'tabs' => $tabs,  
  3111. 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url('media-upload.php') ),  
  3112. 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ),  
  3113. /** This filter is documented in wp-admin/includes/media.php */ 
  3114. 'captions' => ! apply_filters( 'disable_captions', '' ),  
  3115. 'nonce' => array( 
  3116. 'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ),  
  3117. ),  
  3118. 'post' => array( 
  3119. 'id' => 0,  
  3120. ),  
  3121. 'defaultProps' => $props,  
  3122. 'attachmentCounts' => array( 
  3123. 'audio' => ( $show_audio_playlist ) ? 1 : 0,  
  3124. 'video' => ( $show_video_playlist ) ? 1 : 0,  
  3125. ),  
  3126. 'embedExts' => $exts,  
  3127. 'embedMimes' => $ext_mimes,  
  3128. 'contentWidth' => $content_width,  
  3129. 'months' => $months,  
  3130. 'mediaTrash' => MEDIA_TRASH ? 1 : 0,  
  3131. ); 
  3132.  
  3133. $post = null; 
  3134. if ( isset( $args['post'] ) ) { 
  3135. $post = get_post( $args['post'] ); 
  3136. $settings['post'] = array( 
  3137. 'id' => $post->ID,  
  3138. 'nonce' => wp_create_nonce( 'update-post_' . $post->ID ),  
  3139. ); 
  3140.  
  3141. $thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ); 
  3142. if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) { 
  3143. if ( wp_attachment_is( 'audio', $post ) ) { 
  3144. $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); 
  3145. } elseif ( wp_attachment_is( 'video', $post ) ) { 
  3146. $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); 
  3147.  
  3148. if ( $thumbnail_support ) { 
  3149. $featured_image_id = get_post_meta( $post->ID, '_thumbnail_id', true ); 
  3150. $settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1; 
  3151.  
  3152. if ( $post ) { 
  3153. $post_type_object = get_post_type_object( $post->post_type ); 
  3154. } else { 
  3155. $post_type_object = get_post_type_object( 'post' ); 
  3156.  
  3157. $strings = array( 
  3158. // Generic 
  3159. 'url' => __( 'URL' ),  
  3160. 'addMedia' => __( 'Add Media' ),  
  3161. 'search' => __( 'Search' ),  
  3162. 'select' => __( 'Select' ),  
  3163. 'cancel' => __( 'Cancel' ),  
  3164. 'update' => __( 'Update' ),  
  3165. 'replace' => __( 'Replace' ),  
  3166. 'remove' => __( 'Remove' ),  
  3167. 'back' => __( 'Back' ),  
  3168. /** translators: This is a would-be plural string used in the media manager. 
  3169. If there is not a word you can use in your language to avoid issues with the 
  3170. lack of plural support here, turn it into "selected: %d" then translate it. 
  3171. */ 
  3172. 'selected' => __( '%d selected' ),  
  3173. 'dragInfo' => __( 'Drag and drop to reorder media files.' ),  
  3174.  
  3175. // Upload 
  3176. 'uploadFilesTitle' => __( 'Upload Files' ),  
  3177. 'uploadImagesTitle' => __( 'Upload Images' ),  
  3178.  
  3179. // Library 
  3180. 'mediaLibraryTitle' => __( 'Media Library' ),  
  3181. 'insertMediaTitle' => __( 'Insert Media' ),  
  3182. 'createNewGallery' => __( 'Create a new gallery' ),  
  3183. 'createNewPlaylist' => __( 'Create a new playlist' ),  
  3184. 'createNewVideoPlaylist' => __( 'Create a new video playlist' ),  
  3185. 'returnToLibrary' => __( '← Return to library' ),  
  3186. 'allMediaItems' => __( 'All media items' ),  
  3187. 'allDates' => __( 'All dates' ),  
  3188. 'noItemsFound' => __( 'No items found.' ),  
  3189. 'insertIntoPost' => $post_type_object->labels->insert_into_item,  
  3190. 'unattached' => __( 'Unattached' ),  
  3191. 'trash' => _x( 'Trash', 'noun' ),  
  3192. 'uploadedToThisPost' => $post_type_object->labels->uploaded_to_this_item,  
  3193. 'warnDelete' => __( "You are about to permanently delete this item.\n 'Cancel' to stop, 'OK' to delete." ),  
  3194. 'warnBulkDelete' => __( "You are about to permanently delete these items.\n 'Cancel' to stop, 'OK' to delete." ),  
  3195. 'warnBulkTrash' => __( "You are about to trash these items.\n 'Cancel' to stop, 'OK' to delete." ),  
  3196. 'bulkSelect' => __( 'Bulk Select' ),  
  3197. 'cancelSelection' => __( 'Cancel Selection' ),  
  3198. 'trashSelected' => __( 'Trash Selected' ),  
  3199. 'untrashSelected' => __( 'Untrash Selected' ),  
  3200. 'deleteSelected' => __( 'Delete Selected' ),  
  3201. 'deletePermanently' => __( 'Delete Permanently' ),  
  3202. 'apply' => __( 'Apply' ),  
  3203. 'filterByDate' => __( 'Filter by date' ),  
  3204. 'filterByType' => __( 'Filter by type' ),  
  3205. 'searchMediaLabel' => __( 'Search Media' ),  
  3206. 'searchMediaPlaceholder' => __( 'Search media items...' ), // placeholder (no ellipsis) 
  3207. 'noMedia' => __( 'No media files found.' ),  
  3208.  
  3209. // Library Details 
  3210. 'attachmentDetails' => __( 'Attachment Details' ),  
  3211.  
  3212. // From URL 
  3213. 'insertFromUrlTitle' => __( 'Insert from URL' ),  
  3214.  
  3215. // Featured Images 
  3216. 'setFeaturedImageTitle' => $post_type_object->labels->featured_image,  
  3217. 'setFeaturedImage' => $post_type_object->labels->set_featured_image,  
  3218.  
  3219. // Gallery 
  3220. 'createGalleryTitle' => __( 'Create Gallery' ),  
  3221. 'editGalleryTitle' => __( 'Edit Gallery' ),  
  3222. 'cancelGalleryTitle' => __( '← Cancel Gallery' ),  
  3223. 'insertGallery' => __( 'Insert gallery' ),  
  3224. 'updateGallery' => __( 'Update gallery' ),  
  3225. 'addToGallery' => __( 'Add to gallery' ),  
  3226. 'addToGalleryTitle' => __( 'Add to Gallery' ),  
  3227. 'reverseOrder' => __( 'Reverse order' ),  
  3228.  
  3229. // Edit Image 
  3230. 'imageDetailsTitle' => __( 'Image Details' ),  
  3231. 'imageReplaceTitle' => __( 'Replace Image' ),  
  3232. 'imageDetailsCancel' => __( 'Cancel Edit' ),  
  3233. 'editImage' => __( 'Edit Image' ),  
  3234.  
  3235. // Crop Image 
  3236. 'chooseImage' => __( 'Choose Image' ),  
  3237. 'selectAndCrop' => __( 'Select and Crop' ),  
  3238. 'skipCropping' => __( 'Skip Cropping' ),  
  3239. 'cropImage' => __( 'Crop Image' ),  
  3240. 'cropYourImage' => __( 'Crop your image' ),  
  3241. 'cropping' => __( 'Cropping…' ),  
  3242. 'suggestedDimensions' => __( 'Suggested image dimensions:' ),  
  3243. 'cropError' => __( 'There has been an error cropping your image.' ),  
  3244.  
  3245. // Edit Audio 
  3246. 'audioDetailsTitle' => __( 'Audio Details' ),  
  3247. 'audioReplaceTitle' => __( 'Replace Audio' ),  
  3248. 'audioAddSourceTitle' => __( 'Add Audio Source' ),  
  3249. 'audioDetailsCancel' => __( 'Cancel Edit' ),  
  3250.  
  3251. // Edit Video 
  3252. 'videoDetailsTitle' => __( 'Video Details' ),  
  3253. 'videoReplaceTitle' => __( 'Replace Video' ),  
  3254. 'videoAddSourceTitle' => __( 'Add Video Source' ),  
  3255. 'videoDetailsCancel' => __( 'Cancel Edit' ),  
  3256. 'videoSelectPosterImageTitle' => __( 'Select Poster Image' ),  
  3257. 'videoAddTrackTitle' => __( 'Add Subtitles' ),  
  3258.  
  3259. // Playlist 
  3260. 'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ),  
  3261. 'createPlaylistTitle' => __( 'Create Audio Playlist' ),  
  3262. 'editPlaylistTitle' => __( 'Edit Audio Playlist' ),  
  3263. 'cancelPlaylistTitle' => __( '← Cancel Audio Playlist' ),  
  3264. 'insertPlaylist' => __( 'Insert audio playlist' ),  
  3265. 'updatePlaylist' => __( 'Update audio playlist' ),  
  3266. 'addToPlaylist' => __( 'Add to audio playlist' ),  
  3267. 'addToPlaylistTitle' => __( 'Add to Audio Playlist' ),  
  3268.  
  3269. // Video Playlist 
  3270. 'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ),  
  3271. 'createVideoPlaylistTitle' => __( 'Create Video Playlist' ),  
  3272. 'editVideoPlaylistTitle' => __( 'Edit Video Playlist' ),  
  3273. 'cancelVideoPlaylistTitle' => __( '← Cancel Video Playlist' ),  
  3274. 'insertVideoPlaylist' => __( 'Insert video playlist' ),  
  3275. 'updateVideoPlaylist' => __( 'Update video playlist' ),  
  3276. 'addToVideoPlaylist' => __( 'Add to video playlist' ),  
  3277. 'addToVideoPlaylistTitle' => __( 'Add to Video Playlist' ),  
  3278. ); 
  3279.  
  3280. /** 
  3281. * Filters the media view settings. 
  3282. * 
  3283. * @since 3.5.0 
  3284. * 
  3285. * @param array $settings List of media view settings. 
  3286. * @param WP_Post $post Post object. 
  3287. */ 
  3288. $settings = apply_filters( 'media_view_settings', $settings, $post ); 
  3289.  
  3290. /** 
  3291. * Filters the media view strings. 
  3292. * 
  3293. * @since 3.5.0 
  3294. * 
  3295. * @param array $strings List of media view strings. 
  3296. * @param WP_Post $post Post object. 
  3297. */ 
  3298. $strings = apply_filters( 'media_view_strings', $strings, $post ); 
  3299.  
  3300. $strings['settings'] = $settings; 
  3301.  
  3302. // Ensure we enqueue media-editor first, that way media-views is 
  3303. // registered internally before we try to localize it. see #24724. 
  3304. wp_enqueue_script( 'media-editor' ); 
  3305. wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings ); 
  3306.  
  3307. wp_enqueue_script( 'media-audiovideo' ); 
  3308. wp_enqueue_style( 'media-views' ); 
  3309. if ( is_admin() ) { 
  3310. wp_enqueue_script( 'mce-view' ); 
  3311. wp_enqueue_script( 'image-edit' ); 
  3312. wp_enqueue_style( 'imgareaselect' ); 
  3313. wp_plupload_default_settings(); 
  3314.  
  3315. require_once ABSPATH . WPINC . '/media-template.php'; 
  3316. add_action( 'admin_footer', 'wp_print_media_templates' ); 
  3317. add_action( 'wp_footer', 'wp_print_media_templates' ); 
  3318. add_action( 'customize_controls_print_footer_scripts', 'wp_print_media_templates' ); 
  3319.  
  3320. /** 
  3321. * Fires at the conclusion of wp_enqueue_media(). 
  3322. * 
  3323. * @since 3.5.0 
  3324. */ 
  3325. do_action( 'wp_enqueue_media' ); 
  3326.  
  3327. /** 
  3328. * Retrieves media attached to the passed post. 
  3329. * 
  3330. * @since 3.6.0 
  3331. * 
  3332. * @param string $type Mime type. 
  3333. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 
  3334. * @return array Found attachments. 
  3335. */ 
  3336. function get_attached_media( $type, $post = 0 ) { 
  3337. if ( ! $post = get_post( $post ) ) 
  3338. return array(); 
  3339.  
  3340. $args = array( 
  3341. 'post_parent' => $post->ID,  
  3342. 'post_type' => 'attachment',  
  3343. 'post_mime_type' => $type,  
  3344. 'posts_per_page' => -1,  
  3345. 'orderby' => 'menu_order',  
  3346. 'order' => 'ASC',  
  3347. ); 
  3348.  
  3349. /** 
  3350. * Filters arguments used to retrieve media attached to the given post. 
  3351. * 
  3352. * @since 3.6.0 
  3353. * 
  3354. * @param array $args Post query arguments. 
  3355. * @param string $type Mime type of the desired media. 
  3356. * @param mixed $post Post ID or object. 
  3357. */ 
  3358. $args = apply_filters( 'get_attached_media_args', $args, $type, $post ); 
  3359.  
  3360. $children = get_children( $args ); 
  3361.  
  3362. /** 
  3363. * Filters the list of media attached to the given post. 
  3364. * 
  3365. * @since 3.6.0 
  3366. * 
  3367. * @param array $children Associative array of media attached to the given post. 
  3368. * @param string $type Mime type of the media desired. 
  3369. * @param mixed $post Post ID or object. 
  3370. */ 
  3371. return (array) apply_filters( 'get_attached_media', $children, $type, $post ); 
  3372.  
  3373. /** 
  3374. * Check the content blob for an audio, video, object, embed, or iframe tags. 
  3375. * 
  3376. * @since 3.6.0 
  3377. * 
  3378. * @param string $content A string which might contain media data. 
  3379. * @param array $types An array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'. 
  3380. * @return array A list of found HTML media embeds. 
  3381. */ 
  3382. function get_media_embedded_in_content( $content, $types = null ) { 
  3383. $html = array(); 
  3384.  
  3385. /** 
  3386. * Filters the embedded media types that are allowed to be returned from the content blob. 
  3387. * 
  3388. * @since 4.2.0 
  3389. * 
  3390. * @param array $allowed_media_types An array of allowed media types. Default media types are 
  3391. * 'audio', 'video', 'object', 'embed', and 'iframe'. 
  3392. */ 
  3393. $allowed_media_types = apply_filters( 'media_embedded_in_content_allowed_types', array( 'audio', 'video', 'object', 'embed', 'iframe' ) ); 
  3394.  
  3395. if ( ! empty( $types ) ) { 
  3396. if ( ! is_array( $types ) ) { 
  3397. $types = array( $types ); 
  3398.  
  3399. $allowed_media_types = array_intersect( $allowed_media_types, $types ); 
  3400.  
  3401. $tags = implode( '|', $allowed_media_types ); 
  3402.  
  3403. if ( preg_match_all( '#<(?P<tag>' . $tags . ')[^<]*?(?:>[\s\S]*?<\/(?P=tag)>|\s*\/>)#', $content, $matches ) ) { 
  3404. foreach ( $matches[0] as $match ) { 
  3405. $html[] = $match; 
  3406.  
  3407. return $html; 
  3408.  
  3409. /** 
  3410. * Retrieves galleries from the passed post's content. 
  3411. * 
  3412. * @since 3.6.0 
  3413. * 
  3414. * @param int|WP_Post $post Post ID or object. 
  3415. * @param bool $html Optional. Whether to return HTML or data in the array. Default true. 
  3416. * @return array A list of arrays, each containing gallery data and srcs parsed 
  3417. * from the expanded shortcode. 
  3418. */ 
  3419. function get_post_galleries( $post, $html = true ) { 
  3420. if ( ! $post = get_post( $post ) ) 
  3421. return array(); 
  3422.  
  3423. if ( ! has_shortcode( $post->post_content, 'gallery' ) ) 
  3424. return array(); 
  3425.  
  3426. $galleries = array(); 
  3427. if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) { 
  3428. foreach ( $matches as $shortcode ) { 
  3429. if ( 'gallery' === $shortcode[2] ) { 
  3430. $srcs = array(); 
  3431.  
  3432. $shortcode_attrs = shortcode_parse_atts( $shortcode[3] );  
  3433. if ( ! is_array( $shortcode_attrs ) ) { 
  3434. $shortcode_attrs = array(); 
  3435.  
  3436. // Specify the post id of the gallery we're viewing if the shortcode doesn't reference another post already. 
  3437. if ( ! isset( $shortcode_attrs['id'] ) ) { 
  3438. $shortcode[3] .= ' id="' . intval( $post->ID ) . '"'; 
  3439.  
  3440. $gallery = do_shortcode_tag( $shortcode ); 
  3441. if ( $html ) { 
  3442. $galleries[] = $gallery; 
  3443. } else { 
  3444. preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); 
  3445. if ( ! empty( $src ) ) { 
  3446. foreach ( $src as $s ) { 
  3447. $srcs[] = $s[2]; 
  3448.  
  3449. $galleries[] = array_merge( 
  3450. $shortcode_attrs,  
  3451. array( 
  3452. 'src' => array_values( array_unique( $srcs ) ) 
  3453. ); 
  3454.  
  3455. /** 
  3456. * Filters the list of all found galleries in the given post. 
  3457. * 
  3458. * @since 3.6.0 
  3459. * 
  3460. * @param array $galleries Associative array of all found post galleries. 
  3461. * @param WP_Post $post Post object. 
  3462. */ 
  3463. return apply_filters( 'get_post_galleries', $galleries, $post ); 
  3464.  
  3465. /** 
  3466. * Check a specified post's content for gallery and, if present, return the first 
  3467. * 
  3468. * @since 3.6.0 
  3469. * 
  3470. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. 
  3471. * @param bool $html Optional. Whether to return HTML or data. Default is true. 
  3472. * @return string|array Gallery data and srcs parsed from the expanded shortcode. 
  3473. */ 
  3474. function get_post_gallery( $post = 0, $html = true ) { 
  3475. $galleries = get_post_galleries( $post, $html ); 
  3476. $gallery = reset( $galleries ); 
  3477.  
  3478. /** 
  3479. * Filters the first-found post gallery. 
  3480. * 
  3481. * @since 3.6.0 
  3482. * 
  3483. * @param array $gallery The first-found post gallery. 
  3484. * @param int|WP_Post $post Post ID or object. 
  3485. * @param array $galleries Associative array of all found post galleries. 
  3486. */ 
  3487. return apply_filters( 'get_post_gallery', $gallery, $post, $galleries ); 
  3488.  
  3489. /** 
  3490. * Retrieve the image srcs from galleries from a post's content, if present 
  3491. * 
  3492. * @since 3.6.0 
  3493. * 
  3494. * @see get_post_galleries() 
  3495. * 
  3496. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  3497. * @return array A list of lists, each containing image srcs parsed. 
  3498. * from an expanded shortcode 
  3499. */ 
  3500. function get_post_galleries_images( $post = 0 ) { 
  3501. $galleries = get_post_galleries( $post, false ); 
  3502. return wp_list_pluck( $galleries, 'src' ); 
  3503.  
  3504. /** 
  3505. * Checks a post's content for galleries and return the image srcs for the first found gallery 
  3506. * 
  3507. * @since 3.6.0 
  3508. * 
  3509. * @see get_post_gallery() 
  3510. * 
  3511. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. 
  3512. * @return array A list of a gallery's image srcs in order. 
  3513. */ 
  3514. function get_post_gallery_images( $post = 0 ) { 
  3515. $gallery = get_post_gallery( $post, false ); 
  3516. return empty( $gallery['src'] ) ? array() : $gallery['src']; 
  3517.  
  3518. /** 
  3519. * Maybe attempts to generate attachment metadata, if missing. 
  3520. * 
  3521. * @since 3.9.0 
  3522. * 
  3523. * @param WP_Post $attachment Attachment object. 
  3524. */ 
  3525. function wp_maybe_generate_attachment_metadata( $attachment ) { 
  3526. if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) { 
  3527. return; 
  3528.  
  3529. $file = get_attached_file( $attachment_id ); 
  3530. $meta = wp_get_attachment_metadata( $attachment_id ); 
  3531. if ( empty( $meta ) && file_exists( $file ) ) { 
  3532. $_meta = get_post_meta( $attachment_id ); 
  3533. $regeneration_lock = 'wp_generating_att_' . $attachment_id; 
  3534. if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) { 
  3535. set_transient( $regeneration_lock, $file ); 
  3536. wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 
  3537. delete_transient( $regeneration_lock ); 
  3538.  
  3539. /** 
  3540. * Tries to convert an attachment URL into a post ID. 
  3541. * 
  3542. * @since 4.0.0 
  3543. * 
  3544. * @global wpdb $wpdb WordPress database abstraction object. 
  3545. * 
  3546. * @param string $url The URL to resolve. 
  3547. * @return int The found post ID, or 0 on failure. 
  3548. */ 
  3549. function attachment_url_to_postid( $url ) { 
  3550. global $wpdb; 
  3551.  
  3552. $dir = wp_get_upload_dir(); 
  3553. $path = $url; 
  3554.  
  3555. $site_url = parse_url( $dir['url'] ); 
  3556. $image_path = parse_url( $path ); 
  3557.  
  3558. //force the protocols to match if needed 
  3559. if ( isset( $image_path['scheme'] ) && ( $image_path['scheme'] !== $site_url['scheme'] ) ) { 
  3560. $path = str_replace( $image_path['scheme'], $site_url['scheme'], $path ); 
  3561.  
  3562. if ( 0 === strpos( $path, $dir['baseurl'] . '/' ) ) { 
  3563. $path = substr( $path, strlen( $dir['baseurl'] . '/' ) ); 
  3564.  
  3565. $sql = $wpdb->prepare( 
  3566. "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s",  
  3567. $path 
  3568. ); 
  3569. $post_id = $wpdb->get_var( $sql ); 
  3570.  
  3571. /** 
  3572. * Filters an attachment id found by URL. 
  3573. * 
  3574. * @since 4.2.0 
  3575. * 
  3576. * @param int|null $post_id The post_id (if any) found by the function. 
  3577. * @param string $url The URL being looked up. 
  3578. */ 
  3579. return (int) apply_filters( 'attachment_url_to_postid', $post_id, $url ); 
  3580.  
  3581. /** 
  3582. * Returns the URLs for CSS files used in an iframe-sandbox'd TinyMCE media view. 
  3583. * 
  3584. * @since 4.0.0 
  3585. * 
  3586. * @return array The relevant CSS file URLs. 
  3587. */ 
  3588. function wpview_media_sandbox_styles() { 
  3589. $version = 'ver=' . get_bloginfo( 'version' ); 
  3590. $mediaelement = includes_url( "js/mediaelement/mediaelementplayer.min.css?$version" ); 
  3591. $wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" ); 
  3592.  
  3593. return array( $mediaelement, $wpmediaelement ); 
.