/wp-includes/media.php

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