-
+ 1E55B2B55D1E3CE37443404E32E6479104D6A37528CF2360BB2209FDDDBDBAE20133C1A64E1DAB1B0EBAD76D517DB07EBA003FE2EFED8576F35CEB8D15A2E08C
mp-wp/wp-includes/media.php
(0 . 0)(1 . 769)
136740 <?php
136741 /**
136742 * WordPress API for media display.
136743 *
136744 * @package WordPress
136745 */
136746
136747 /**
136748 * Scale down the default size of an image.
136749 *
136750 * This is so that the image is a better fit for the editor and theme.
136751 *
136752 * The $size parameter accepts either an array or a string. The supported string
136753 * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
136754 * 128 width and 96 height in pixels. Also supported for the string value is
136755 * 'medium' and 'full'. The 'full' isn't actually supported, but any value other
136756 * than the supported will result in the content_width size or 500 if that is
136757 * not set.
136758 *
136759 * Finally, there is a filter named, 'editor_max_image_size' that will be called
136760 * on the calculated array for width and height, respectively. The second
136761 * parameter will be the value that was in the $size parameter. The returned
136762 * type for the hook is an array with the width as the first element and the
136763 * height as the second element.
136764 *
136765 * @since 2.5.0
136766 * @uses wp_constrain_dimensions() This function passes the widths and the heights.
136767 *
136768 * @param int $width Width of the image
136769 * @param int $height Height of the image
136770 * @param string|array $size Size of what the result image should be.
136771 * @return array Width and height of what the result image should resize to.
136772 */
136773 function image_constrain_size_for_editor($width, $height, $size = 'medium') {
136774 global $content_width;
136775
136776 if ( is_array($size) ) {
136777 $max_width = $size[0];
136778 $max_height = $size[1];
136779 }
136780 elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
136781 $max_width = intval(get_option('thumbnail_size_w'));
136782 $max_height = intval(get_option('thumbnail_size_h'));
136783 // last chance thumbnail size defaults
136784 if ( !$max_width && !$max_height ) {
136785 $max_width = 128;
136786 $max_height = 96;
136787 }
136788 }
136789 elseif ( $size == 'medium' ) {
136790 $max_width = intval(get_option('medium_size_w'));
136791 $max_height = intval(get_option('medium_size_h'));
136792 // if no width is set, default to the theme content width if available
136793 }
136794 elseif ( $size == 'large' ) {
136795 // we're inserting a large size image into the editor. if it's a really
136796 // big image we'll scale it down to fit reasonably within the editor
136797 // itself, and within the theme's content width if it's known. the user
136798 // can resize it in the editor if they wish.
136799 $max_width = intval(get_option('large_size_w'));
136800 $max_height = intval(get_option('large_size_h'));
136801 if ( intval($content_width) > 0 )
136802 $max_width = min( intval($content_width), $max_width );
136803 }
136804 // $size == 'full' has no constraint
136805 else {
136806 $max_width = $width;
136807 $max_height = $height;
136808 }
136809
136810 list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size );
136811
136812 return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
136813 }
136814
136815 /**
136816 * Retrieve width and height attributes using given width and height values.
136817 *
136818 * Both attributes are required in the sense that both parameters must have a
136819 * value, but are optional in that if you set them to false or null, then they
136820 * will not be added to the returned string.
136821 *
136822 * You can set the value using a string, but it will only take numeric values.
136823 * If you wish to put 'px' after the numbers, then it will be stripped out of
136824 * the return.
136825 *
136826 * @since 2.5.0
136827 *
136828 * @param int|string $width Optional. Width attribute value.
136829 * @param int|string $height Optional. Height attribute value.
136830 * @return string HTML attributes for width and, or height.
136831 */
136832 function image_hwstring($width, $height) {
136833 $out = '';
136834 if ($width)
136835 $out .= 'width="'.intval($width).'" ';
136836 if ($height)
136837 $out .= 'height="'.intval($height).'" ';
136838 return $out;
136839 }
136840
136841 /**
136842 * Scale an image to fit a particular size (such as 'thumb' or 'medium').
136843 *
136844 * Array with image url, width, height, and whether is intermediate size, in
136845 * that order is returned on success is returned. $is_intermediate is true if
136846 * $url is a resized image, false if it is the original.
136847 *
136848 * The URL might be the original image, or it might be a resized version. This
136849 * function won't create a new resized copy, it will just return an already
136850 * resized one if it exists.
136851 *
136852 * A plugin may use the 'image_downsize' filter to hook into and offer image
136853 * resizing services for images. The hook must return an array with the same
136854 * elements that are returned in the function. The first element being the URL
136855 * to the new image that was resized.
136856 *
136857 * @since 2.5.0
136858 * @uses apply_filters() Calls 'image_downsize' on $id and $size to provide
136859 * resize services.
136860 *
136861 * @param int $id Attachment ID for image.
136862 * @param string $size Optional, default is 'medium'. Size of image, can be 'thumbnail'.
136863 * @return bool|array False on failure, array on success.
136864 */
136865 function image_downsize($id, $size = 'medium') {
136866
136867 if ( !wp_attachment_is_image($id) )
136868 return false;
136869
136870 $img_url = wp_get_attachment_url($id);
136871 $meta = wp_get_attachment_metadata($id);
136872 $width = $height = 0;
136873 $is_intermediate = false;
136874
136875 // plugins can use this to provide resize services
136876 if ( $out = apply_filters('image_downsize', false, $id, $size) )
136877 return $out;
136878
136879 // try for a new style intermediate size
136880 if ( $intermediate = image_get_intermediate_size($id, $size) ) {
136881 $img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
136882 $width = $intermediate['width'];
136883 $height = $intermediate['height'];
136884 $is_intermediate = true;
136885 }
136886 elseif ( $size == 'thumbnail' ) {
136887 // fall back to the old thumbnail
136888 if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
136889 $img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
136890 $width = $info[0];
136891 $height = $info[1];
136892 $is_intermediate = true;
136893 }
136894 }
136895 if ( !$width && !$height && isset($meta['width'], $meta['height']) ) {
136896 // any other type: use the real image
136897 $width = $meta['width'];
136898 $height = $meta['height'];
136899 }
136900
136901 if ( $img_url) {
136902 // we have the actual image size, but might need to further constrain it if content_width is narrower
136903 list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
136904
136905 return array( $img_url, $width, $height, $is_intermediate );
136906 }
136907 return false;
136908
136909 }
136910
136911 /**
136912 * An <img src /> tag for an image attachment, scaling it down if requested.
136913 *
136914 * The filter 'get_image_tag_class' allows for changing the class name for the
136915 * image without having to use regular expressions on the HTML content. The
136916 * parameters are: what WordPress will use for the class, the Attachment ID,
136917 * image align value, and the size the image should be.
136918 *
136919 * The second filter 'get_image_tag' has the HTML content, which can then be
136920 * further manipulated by a plugin to change all attribute values and even HTML
136921 * content.
136922 *
136923 * @since 2.5.0
136924 *
136925 * @uses apply_filters() The 'get_image_tag_class' filter is the IMG element
136926 * class attribute.
136927 * @uses apply_filters() The 'get_image_tag' filter is the full IMG element with
136928 * all attributes.
136929 *
136930 * @param int $id Attachment ID.
136931 * @param string $alt Image Description for the alt attribute.
136932 * @param string $title Image Description for the title attribute.
136933 * @param string $align Part of the class name for aligning the image.
136934 * @param string $size Optional. Default is 'medium'.
136935 * @return string HTML IMG element for given image attachment
136936 */
136937 function get_image_tag($id, $alt, $title, $align, $size='medium') {
136938
136939 list( $img_src, $width, $height ) = image_downsize($id, $size);
136940 $hwstring = image_hwstring($width, $height);
136941
136942 $class = 'align'.attribute_escape($align).' size-'.attribute_escape($size).' wp-image-'.$id;
136943 $class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
136944
136945 $html = '<img src="'.attribute_escape($img_src).'" alt="'.attribute_escape($alt).'" title="'.attribute_escape($title).'" '.$hwstring.'class="'.$class.'" />';
136946
136947 $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
136948
136949 return $html;
136950 }
136951
136952 /**
136953 * Calculates the new dimentions for a downsampled image.
136954 *
136955 * Same as {@link wp_shrink_dimensions()}, except the max parameters are
136956 * optional. If either width or height are empty, no constraint is applied on
136957 * that dimension.
136958 *
136959 * @since 2.5.0
136960 *
136961 * @param int $current_width Current width of the image.
136962 * @param int $current_height Current height of the image.
136963 * @param int $max_width Optional. Maximum wanted width.
136964 * @param int $max_height Optional. Maximum wanted height.
136965 * @return array First item is the width, the second item is the height.
136966 */
136967 function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
136968 if ( !$max_width and !$max_height )
136969 return array( $current_width, $current_height );
136970
136971 $width_ratio = $height_ratio = 1.0;
136972
136973 if ( $max_width > 0 && $current_width > $max_width )
136974 $width_ratio = $max_width / $current_width;
136975
136976 if ( $max_height > 0 && $current_height > $max_height )
136977 $height_ratio = $max_height / $current_height;
136978
136979 // the smaller ratio is the one we need to fit it to the constraining box
136980 $ratio = min( $width_ratio, $height_ratio );
136981
136982 return array( intval($current_width * $ratio), intval($current_height * $ratio) );
136983 }
136984
136985 /**
136986 * Retrieve calculated resized dimensions for use in imagecopyresampled().
136987 *
136988 * Calculate dimensions and coordinates for a resized image that fits within a
136989 * specified width and height. If $crop is true, the largest matching central
136990 * portion of the image will be cropped out and resized to the required size.
136991 *
136992 * @since 2.5.0
136993 *
136994 * @param int $orig_w Original width.
136995 * @param int $orig_h Original height.
136996 * @param int $dest_w New width.
136997 * @param int $dest_h New height.
136998 * @param bool $crop Optional, default is false. Whether to crop image or resize.
136999 * @return bool|array False, on failure. Returned array matches parameters for imagecopyresampled() PHP function.
137000 */
137001 function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop=false) {
137002
137003 if ($orig_w <= 0 || $orig_h <= 0)
137004 return false;
137005 // at least one of dest_w or dest_h must be specific
137006 if ($dest_w <= 0 && $dest_h <= 0)
137007 return false;
137008
137009 if ( $crop ) {
137010 // crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
137011 $aspect_ratio = $orig_w / $orig_h;
137012 $new_w = min($dest_w, $orig_w);
137013 $new_h = min($dest_h, $orig_h);
137014 if (!$new_w) {
137015 $new_w = intval($new_h * $aspect_ratio);
137016 }
137017 if (!$new_h) {
137018 $new_h = intval($new_w / $aspect_ratio);
137019 }
137020
137021 $size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
137022
137023 $crop_w = ceil($new_w / $size_ratio);
137024 $crop_h = ceil($new_h / $size_ratio);
137025
137026 $s_x = floor(($orig_w - $crop_w)/2);
137027 $s_y = floor(($orig_h - $crop_h)/2);
137028 }
137029 else {
137030 // don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
137031 $crop_w = $orig_w;
137032 $crop_h = $orig_h;
137033
137034 $s_x = 0;
137035 $s_y = 0;
137036
137037 list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
137038 }
137039
137040 // if the resulting image would be the same size or larger we don't want to resize it
137041 if ($new_w >= $orig_w && $new_h >= $orig_h)
137042 return false;
137043
137044 // the return array matches the parameters to imagecopyresampled()
137045 // int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
137046 return array(0, 0, $s_x, $s_y, $new_w, $new_h, $crop_w, $crop_h);
137047
137048 }
137049
137050 /**
137051 * Scale down an image to fit a particular size and save a new copy of the image.
137052 *
137053 * The PNG transparency will be preserved using the function, as well as the
137054 * image type. If the file going in is PNG, then the resized image is going to
137055 * be PNG. The only supported image types are PNG, GIF, and JPEG.
137056 *
137057 * Some functionality requires API to exist, so some PHP version may lose out
137058 * support. This is not the fault of WordPress (where functionality is
137059 * downgraded, not actual defects), but of your PHP version.
137060 *
137061 * @since 2.5.0
137062 *
137063 * @param string $file Image file path.
137064 * @param int $max_w Maximum width to resize to.
137065 * @param int $max_h Maximum height to resize to.
137066 * @param bool $crop Optional. Whether to crop image or resize.
137067 * @param string $suffix Optional. File Suffix.
137068 * @param string $dest_path Optional. New image file path.
137069 * @param int $jpeg_quality Optional, default is 90. Image quality percentage.
137070 * @return mixed WP_Error on failure. String with new destination path. Array of dimensions from {@link image_resize_dimensions()}
137071 */
137072 function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) {
137073
137074 $image = wp_load_image( $file );
137075 if ( !is_resource( $image ) )
137076 return new WP_Error('error_loading_image', $image);
137077
137078 list($orig_w, $orig_h, $orig_type) = getimagesize( $file );
137079 $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
137080 if (!$dims)
137081 return $dims;
137082 list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
137083
137084 $newimage = imagecreatetruecolor( $dst_w, $dst_h);
137085
137086 // preserve PNG transparency
137087 if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) {
137088 imagealphablending( $newimage, false);
137089 imagesavealpha( $newimage, true);
137090 }
137091
137092 imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
137093
137094 // we don't need the original in memory anymore
137095 imagedestroy( $image );
137096
137097 // $suffix will be appended to the destination filename, just before the extension
137098 if ( !$suffix )
137099 $suffix = "{$dst_w}x{$dst_h}";
137100
137101 $info = pathinfo($file);
137102 $dir = $info['dirname'];
137103 $ext = $info['extension'];
137104 $name = basename($file, ".{$ext}");
137105 if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
137106 $dir = $_dest_path;
137107 $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
137108
137109 if ( $orig_type == IMAGETYPE_GIF ) {
137110 if (!imagegif( $newimage, $destfilename ) )
137111 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
137112 }
137113 elseif ( $orig_type == IMAGETYPE_PNG ) {
137114 if (!imagepng( $newimage, $destfilename ) )
137115 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
137116 }
137117 else {
137118 // all other formats are converted to jpg
137119 $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
137120 if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality ) ) )
137121 return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
137122 }
137123
137124 imagedestroy( $newimage );
137125
137126 // Set correct file permissions
137127 $stat = stat( dirname( $destfilename ));
137128 $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
137129 @ chmod( $destfilename, $perms );
137130
137131 return $destfilename;
137132 }
137133
137134 /**
137135 * Resize an image to make a thumbnail or intermediate size.
137136 *
137137 * The returned array has the file size, the image width, and image height. The
137138 * filter 'image_make_intermediate_size' can be used to hook in and change the
137139 * values of the returned array. The only parameter is the resized file path.
137140 *
137141 * @since 2.5.0
137142 *
137143 * @param string $file File path.
137144 * @param int $width Image width.
137145 * @param int $height Image height.
137146 * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize.
137147 * @return bool|array False, if no image was created. Metadata array on success.
137148 */
137149 function image_make_intermediate_size($file, $width, $height, $crop=false) {
137150 if ( $width || $height ) {
137151 $resized_file = image_resize($file, $width, $height, $crop);
137152 if ( !is_wp_error($resized_file) && $resized_file && $info = getimagesize($resized_file) ) {
137153 $resized_file = apply_filters('image_make_intermediate_size', $resized_file);
137154 return array(
137155 'file' => basename( $resized_file ),
137156 'width' => $info[0],
137157 'height' => $info[1],
137158 );
137159 }
137160 }
137161 return false;
137162 }
137163
137164 /**
137165 * Retrieve the image's intermediate size (resized) path, width, and height.
137166 *
137167 * The $size parameter can be an array with the width and height respectively.
137168 * If the size matches the 'sizes' metadata array for width and height, then it
137169 * will be used. If there is no direct match, then the nearest image size larger
137170 * than the specified size will be used. If nothing is found, then the function
137171 * will break out and return false.
137172 *
137173 * The metadata 'sizes' is used for compatible sizes that can be used for the
137174 * parameter $size value.
137175 *
137176 * The url path will be given, when the $size parameter is a string.
137177 *
137178 * @since 2.5.0
137179 *
137180 * @param int $post_id Attachment ID for image.
137181 * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string.
137182 * @return bool|array False on failure or array of file path, width, and height on success.
137183 */
137184 function image_get_intermediate_size($post_id, $size='thumbnail') {
137185 if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
137186 return false;
137187
137188 // get the best one for a specified set of dimensions
137189 if ( is_array($size) && !empty($imagedata['sizes']) ) {
137190 foreach ( $imagedata['sizes'] as $_size => $data ) {
137191 // already cropped to width or height; so use this size
137192 if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
137193 $file = $data['file'];
137194 list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
137195 return compact( 'file', 'width', 'height' );
137196 }
137197 // add to lookup table: area => size
137198 $areas[$data['width'] * $data['height']] = $_size;
137199 }
137200 if ( !$size || !empty($areas) ) {
137201 // find for the smallest image not smaller than the desired size
137202 ksort($areas);
137203 foreach ( $areas as $_size ) {
137204 $data = $imagedata['sizes'][$_size];
137205 if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
137206 $file = $data['file'];
137207 list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
137208 return compact( 'file', 'width', 'height' );
137209 }
137210 }
137211 }
137212 }
137213
137214 if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
137215 return false;
137216
137217 $data = $imagedata['sizes'][$size];
137218 // include the full filesystem path of the intermediate file
137219 if ( empty($data['path']) && !empty($data['file']) ) {
137220 $file_url = wp_get_attachment_url($post_id);
137221 $data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
137222 $data['url'] = path_join( dirname($file_url), $data['file'] );
137223 }
137224 return $data;
137225 }
137226
137227 /**
137228 * Retrieve an image to represent an attachment.
137229 *
137230 * A mime icon for files, thumbnail or intermediate size for images.
137231 *
137232 * @since 2.5.0
137233 *
137234 * @param int $attachment_id Image attachment ID.
137235 * @param string $size Optional, default is 'thumbnail'.
137236 * @param bool $icon Optional, default is false. Whether it is an icon.
137237 * @return bool|array Returns an array (url, width, height), or false, if no image is available.
137238 */
137239 function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
137240
137241 // get a thumbnail or intermediate image if there is one
137242 if ( $image = image_downsize($attachment_id, $size) )
137243 return $image;
137244
137245 if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
137246 $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
137247 $src_file = $icon_dir . '/' . basename($src);
137248 @list($width, $height) = getimagesize($src_file);
137249 }
137250 if ( $src && $width && $height )
137251 return array( $src, $width, $height );
137252 return false;
137253 }
137254
137255 /**
137256 * Retrieve img HTML content for an image to represent an attachment.
137257 *
137258 * @see wp_get_attachment_image_src() Returns img HTML element based on array.
137259 * @since 2.5.0
137260 *
137261 * @param int $attachment_id Image attachment ID.
137262 * @param string $size Optional, default is 'thumbnail'.
137263 * @param bool $icon Optional, default is false. Whether it is an icon.
137264 * @return string HTML img element or empty string on failure.
137265 */
137266 function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false) {
137267
137268 $html = '';
137269 $image = wp_get_attachment_image_src($attachment_id, $size, $icon);
137270 if ( $image ) {
137271 list($src, $width, $height) = $image;
137272 $hwstring = image_hwstring($width, $height);
137273 if ( is_array($size) )
137274 $size = join('x', $size);
137275 $html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />';
137276 }
137277
137278 return $html;
137279 }
137280
137281 add_shortcode('wp_caption', 'img_caption_shortcode');
137282 add_shortcode('caption', 'img_caption_shortcode');
137283
137284 /**
137285 * The Caption shortcode.
137286 *
137287 * Allows a plugin to replace the content that would otherwise be returned. The
137288 * filter is 'img_caption_shortcode' and passes an empty string, the attr
137289 * parameter and the content parameter values.
137290 *
137291 * The supported attributes for the shortcode are 'id', 'align', 'width', and
137292 * 'caption'.
137293 *
137294 * @since 2.6.0
137295 *
137296 * @param array $attr Attributes attributed to the shortcode.
137297 * @param string $content Optional. Shortcode content.
137298 * @return string
137299 */
137300 function img_caption_shortcode($attr, $content = null) {
137301
137302 // Allow plugins/themes to override the default caption template.
137303 $output = apply_filters('img_caption_shortcode', '', $attr, $content);
137304 if ( $output != '' )
137305 return $output;
137306
137307 extract(shortcode_atts(array(
137308 'id' => '',
137309 'align' => 'alignnone',
137310 'width' => '',
137311 'caption' => ''
137312 ), $attr));
137313
137314 if ( 1 > (int) $width || empty($caption) )
137315 return $content;
137316
137317 if ( $id ) $id = 'id="' . $id . '" ';
137318
137319 return '<div ' . $id . 'class="wp-caption ' . $align . '" style="width: ' . (10 + (int) $width) . 'px">'
137320 . $content . '<p class="wp-caption-text">' . $caption . '</p></div>';
137321 }
137322
137323 add_shortcode('gallery', 'gallery_shortcode');
137324
137325 /**
137326 * The Gallery shortcode.
137327 *
137328 * This implements the functionality of the Gallery Shortcode for displaying
137329 * WordPress images on a post.
137330 *
137331 * @since 2.5.0
137332 *
137333 * @param array $attr Attributes attributed to the shortcode.
137334 * @return string HTML content to display gallery.
137335 */
137336 function gallery_shortcode($attr) {
137337 global $post;
137338
137339 // Allow plugins/themes to override the default gallery template.
137340 $output = apply_filters('post_gallery', '', $attr);
137341 if ( $output != '' )
137342 return $output;
137343
137344 // We're trusting author input, so let's at least make sure it looks like a valid orderby statement
137345 if ( isset( $attr['orderby'] ) ) {
137346 $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
137347 if ( !$attr['orderby'] )
137348 unset( $attr['orderby'] );
137349 }
137350
137351 extract(shortcode_atts(array(
137352 'order' => 'ASC',
137353 'orderby' => 'menu_order ID',
137354 'id' => $post->ID,
137355 'itemtag' => 'dl',
137356 'icontag' => 'dt',
137357 'captiontag' => 'dd',
137358 'columns' => 3,
137359 'size' => 'thumbnail'
137360 ), $attr));
137361
137362 $id = intval($id);
137363 $attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
137364
137365 if ( empty($attachments) )
137366 return '';
137367
137368 if ( is_feed() ) {
137369 $output = "\n";
137370 foreach ( $attachments as $id => $attachment )
137371 $output .= wp_get_attachment_link($id, $size, true) . "\n";
137372 return $output;
137373 }
137374
137375 $itemtag = tag_escape($itemtag);
137376 $captiontag = tag_escape($captiontag);
137377 $columns = intval($columns);
137378 $itemwidth = $columns > 0 ? floor(100/$columns) : 100;
137379
137380 $output = apply_filters('gallery_style', "
137381 <style type='text/css'>
137382 .gallery {
137383 margin: auto;
137384 }
137385 .gallery-item {
137386 float: left;
137387 margin-top: 10px;
137388 text-align: center;
137389 width: {$itemwidth}%; }
137390 .gallery img {
137391 border: 2px solid #cfcfcf;
137392 }
137393 .gallery-caption {
137394 margin-left: 0;
137395 }
137396 </style>
137397 <!-- see gallery_shortcode() in wp-includes/media.php -->
137398 <div class='gallery'>");
137399
137400 $i = 0;
137401 foreach ( $attachments as $id => $attachment ) {
137402 $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
137403
137404 $output .= "<{$itemtag} class='gallery-item'>";
137405 $output .= "
137406 <{$icontag} class='gallery-icon'>
137407 $link
137408 </{$icontag}>";
137409 if ( $captiontag && trim($attachment->post_excerpt) ) {
137410 $output .= "
137411 <{$captiontag} class='gallery-caption'>
137412 {$attachment->post_excerpt}
137413 </{$captiontag}>";
137414 }
137415 $output .= "</{$itemtag}>";
137416 if ( $columns > 0 && ++$i % $columns == 0 )
137417 $output .= '<br style="clear: both" />';
137418 }
137419
137420 $output .= "
137421 <br style='clear: both;' />
137422 </div>\n";
137423
137424 return $output;
137425 }
137426
137427 /**
137428 * Display previous image link that has the same post parent.
137429 *
137430 * @since 2.5.0
137431 */
137432 function previous_image_link() {
137433 adjacent_image_link(true);
137434 }
137435
137436 /**
137437 * Display next image link that has the same post parent.
137438 *
137439 * @since 2.5.0
137440 */
137441 function next_image_link() {
137442 adjacent_image_link(false);
137443 }
137444
137445 /**
137446 * Display next or previous image link that has the same post parent.
137447 *
137448 * Retrieves the current attachment object from the $post global.
137449 *
137450 * @since 2.5.0
137451 *
137452 * @param bool $prev Optional. Default is true to display previous link, true for next.
137453 */
137454 function adjacent_image_link($prev = true) {
137455 global $post;
137456 $post = get_post($post);
137457 $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') ));
137458
137459 foreach ( $attachments as $k => $attachment )
137460 if ( $attachment->ID == $post->ID )
137461 break;
137462
137463 $k = $prev ? $k - 1 : $k + 1;
137464
137465 if ( isset($attachments[$k]) )
137466 echo wp_get_attachment_link($attachments[$k]->ID, 'thumbnail', true);
137467 }
137468
137469 /**
137470 * Retrieve taxonomies attached to the attachment.
137471 *
137472 * @since 2.5.0
137473 *
137474 * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object.
137475 * @return array Empty array on failure. List of taxonomies on success.
137476 */
137477 function get_attachment_taxonomies($attachment) {
137478 if ( is_int( $attachment ) )
137479 $attachment = get_post($attachment);
137480 else if ( is_array($attachment) )
137481 $attachment = (object) $attachment;
137482
137483 if ( ! is_object($attachment) )
137484 return array();
137485
137486 $filename = basename($attachment->guid);
137487
137488 $objects = array('attachment');
137489
137490 if ( false !== strpos($filename, '.') )
137491 $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
137492 if ( !empty($attachment->post_mime_type) ) {
137493 $objects[] = 'attachment:' . $attachment->post_mime_type;
137494 if ( false !== strpos($attachment->post_mime_type, '/') )
137495 foreach ( explode('/', $attachment->post_mime_type) as $token )
137496 if ( !empty($token) )
137497 $objects[] = "attachment:$token";
137498 }
137499
137500 $taxonomies = array();
137501 foreach ( $objects as $object )
137502 if ( $taxes = get_object_taxonomies($object) )
137503 $taxonomies = array_merge($taxonomies, $taxes);
137504
137505 return array_unique($taxonomies);
137506 }
137507
137508 ?>