WordPressを読む 65-2 /blog/wp-includes/media.php 2
2015/03/06
目次
- 1 /blog/wp-includes/media.php 2
- 2 関数 image_make_intermediate_size()
- 3 関数 image_get_intermediate_size()
- 4 関数 get_intermediate_image_sizes()
- 5 関数 wp_get_attachment_image_src()
- 6 関数 wp_get_attachment_image()
- 7 関数 _wp_post_thumbnail_class_filter()
- 8 関数 _wp_post_thumbnail_class_filter_add()
- 9 関数 _wp_post_thumbnail_class_filter_remove()
- 10 関数間の処理 フックポイント
- 11 関数 img_caption_shortcode()
- 12 関数間の処理 フックポイント
- 13 関数 gallery_shortcode()
/blog/wp-includes/media.php 2
関数 image_make_intermediate_size()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | /** * Resize an image to make a thumbnail or intermediate size. * * The returned array has the file size, the image width, and image height. The * filter 'image_make_intermediate_size' can be used to hook in and change the * values of the returned array. The only parameter is the resized file path. * * @since 2.5.0 * * @param string $file File path. * @param int $width Image width. * @param int $height Image height. * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize. * @return bool|array False, if no image was created. Metadata array on success. */ function image_make_intermediate_size( $file, $width, $height, $crop = false ) { if ( $width || $height ) { $editor = wp_get_image_editor( $file ); if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) ) return false; $resized_file = $editor->save(); if ( ! is_wp_error( $resized_file ) && $resized_file ) { unset( $resized_file['path'] ); return $resized_file; } } return false; } |
関数 image_get_intermediate_size()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | /** * Retrieve the image's intermediate size (resized) path, width, and height. * * The $size parameter can be an array with the width and height respectively. * If the size matches the 'sizes' metadata array for width and height, then it * will be used. If there is no direct match, then the nearest image size larger * than the specified size will be used. If nothing is found, then the function * will break out and return false. * * The metadata 'sizes' is used for compatible sizes that can be used for the * parameter $size value. * * The url path will be given, when the $size parameter is a string. * * If you are passing an array for the $size, you should consider using * add_image_size() so that a cropped version is generated. It's much more * efficient than having to find the closest-sized image and then having the * browser scale down the image. * * @since 2.5.0 * @see add_image_size() * * @param int $post_id Attachment ID for image. * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string. * @return bool|array False on failure or array of file path, width, and height on success. */ function image_get_intermediate_size($post_id, $size='thumbnail') { if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) ) return false; // get the best one for a specified set of dimensions if ( is_array($size) && !empty($imagedata['sizes']) ) { foreach ( $imagedata['sizes'] as $_size => $data ) { // already cropped to width or height; so use this size if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) { $file = $data['file']; list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); return compact( 'file', 'width', 'height' ); } // add to lookup table: area => size $areas[$data['width'] * $data['height']] = $_size; } if ( !$size || !empty($areas) ) { // find for the smallest image not smaller than the desired size ksort($areas); foreach ( $areas as $_size ) { $data = $imagedata['sizes'][$_size]; if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) { // Skip images with unexpectedly divergent aspect ratios (crops) // First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop $maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false ); // If the size doesn't match within one pixel, then it is of a different aspect ratio, so we skip it, unless it's the thumbnail size if ( 'thumbnail' != $_size && ( !$maybe_cropped || ( $maybe_cropped[4] != $data['width'] && $maybe_cropped[4] + 1 != $data['width'] ) || ( $maybe_cropped[5] != $data['height'] && $maybe_cropped[5] + 1 != $data['height'] ) ) ) continue; // If we're still here, then we're going to use this size $file = $data['file']; list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size ); return compact( 'file', 'width', 'height' ); } } } } if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) ) return false; $data = $imagedata['sizes'][$size]; // include the full filesystem path of the intermediate file if ( empty($data['path']) && !empty($data['file']) ) { $file_url = wp_get_attachment_url($post_id); $data['path'] = path_join( dirname($imagedata['file']), $data['file'] ); $data['url'] = path_join( dirname($file_url), $data['file'] ); } return $data; } |
関数 get_intermediate_image_sizes()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Get the available image sizes * @since 3.0.0 * @return array Returns a filtered array of image size strings */ function get_intermediate_image_sizes() { global $_wp_additional_image_sizes; $image_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) $image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) ); /** * Filter the list of intermediate image sizes. * * @since 2.5.0 * * @param array $image_sizes An array of intermediate image sizes. Defaults * are 'thumbnail', 'medium', 'large'. */ return apply_filters( 'intermediate_image_sizes', $image_sizes ); } |
関数 wp_get_attachment_image_src()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /** * Retrieve an image to represent an attachment. * * A mime icon for files, thumbnail or intermediate size for images. * * @since 2.5.0 * * @param int $attachment_id Image attachment ID. * @param string $size Optional, default is 'thumbnail'. * @param bool $icon Optional, default is false. Whether it is an icon. * @return bool|array Returns an array (url, width, height), or false, if no image is available. */ function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) { // get a thumbnail or intermediate image if there is one if ( $image = image_downsize($attachment_id, $size) ) return $image; $src = false; if ( $icon && $src = wp_mime_type_icon($attachment_id) ) { /** This filter is documented in wp-includes/post.php */ $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); $src_file = $icon_dir . '/' . wp_basename($src); @list($width, $height) = getimagesize($src_file); } if ( $src && $width && $height ) return array( $src, $width, $height ); return false; } |
関数 wp_get_attachment_image()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | /** * Get an HTML img element representing an image attachment * * While $size will accept an array, it is better to register a size with * add_image_size() so that a cropped version is generated. It's much more * efficient than having to find the closest-sized image and then having the * browser scale down the image. * * @since 2.5.0 * * @see add_image_size() * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions * * @param int $attachment_id Image attachment ID. * @param string $size Optional, default is 'thumbnail'. * @param bool $icon Optional, default is false. Whether it is an icon. * @param mixed $attr Optional, attributes for the image markup. * @return string HTML img element or empty string on failure. */ function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { $html = ''; $image = wp_get_attachment_image_src($attachment_id, $size, $icon); if ( $image ) { list($src, $width, $height) = $image; $hwstring = image_hwstring($width, $height); if ( is_array($size) ) $size = join('x', $size); $attachment = get_post($attachment_id); $default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first ); if ( empty($default_attr['alt']) ) $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption if ( empty($default_attr['alt']) ) $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title $attr = wp_parse_args($attr, $default_attr); /** * Filter the list of attachment image attributes. * * @since 2.8.0 * * @param mixed $attr Attributes for the image markup. * @param int $attachment_id Image attachment ID. */ $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); $attr = array_map( 'esc_attr', $attr ); $html = rtrim("<img $hwstring"); foreach ( $attr as $name => $value ) { $html .= " $name=" . '"' . $value . '"'; } $html .= ' />'; } return $html; } |
関数 _wp_post_thumbnail_class_filter()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Adds a 'wp-post-image' class to post thumbnails * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to * dynamically add/remove itself so as to only filter post thumbnails * * @since 2.9.0 * @param array $attr Attributes including src, class, alt, title * @return array */ function _wp_post_thumbnail_class_filter( $attr ) { $attr['class'] .= ' wp-post-image'; return $attr; } |
関数 _wp_post_thumbnail_class_filter_add()
1 2 3 4 5 6 7 8 | /** * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter * * @since 2.9.0 */ function _wp_post_thumbnail_class_filter_add( $attr ) { add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); } |
関数 _wp_post_thumbnail_class_filter_remove()
1 2 3 4 5 6 7 8 | /** * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter * * @since 2.9.0 */ function _wp_post_thumbnail_class_filter_remove( $attr ) { remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' ); } |
関数間の処理 フックポイント
1 2 | add_shortcode('wp_caption', 'img_caption_shortcode'); add_shortcode('caption', 'img_caption_shortcode'); |
関数 img_caption_shortcode()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | /** * The Caption shortcode. * * Allows a plugin to replace the content that would otherwise be returned. The * filter is 'img_caption_shortcode' and passes an empty string, the attr * parameter and the content parameter values. * * The supported attributes for the shortcode are 'id', 'align', 'width', and * 'caption'. * * @since 2.6.0 * * @param array $attr { * Attributes of the caption shortcode. * * @type string $id ID of the div element for the caption. * @type string $align Class name that aligns the caption. Default 'alignnone'. Accepts 'alignleft', * 'aligncenter', alignright', 'alignnone'. * @type int $width The width of the caption, in pixels. * @type string $caption The caption text. * @type string $class Additional class name(s) added to the caption container. * } * @param string $content Optional. Shortcode content. * @return string HTML content to display the caption. */ function img_caption_shortcode( $attr, $content = null ) { // New-style shortcode with the caption inside the shortcode with the link and image tags. if ( ! isset( $attr['caption'] ) ) { if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) { $content = $matches[1]; $attr['caption'] = trim( $matches[2] ); } } /** * Filter the default caption shortcode output. * * If the filtered output isn't empty, it will be used instead of generating * the default caption template. * * @since 2.6.0 * * @see img_caption_shortcode() * * @param string $output The caption output. Default empty. * @param array $attr Attributes of the caption shortcode. * @param string $content The image element, possibly wrapped in a hyperlink. */ $output = apply_filters( 'img_caption_shortcode', '', $attr, $content ); if ( $output != '' ) return $output; $atts = shortcode_atts( array( 'id' => '', 'align' => 'alignnone', 'width' => '', 'caption' => '', 'class' => '', ), $attr, 'caption' ); $atts['width'] = (int) $atts['width']; if ( $atts['width'] < 1 || empty( $atts['caption'] ) ) return $content; if ( ! empty( $atts['id'] ) ) $atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" '; $class = trim( 'wp-caption ' . $atts['align'] . ' ' . $atts['class'] ); if ( current_theme_supports( 'html5', 'caption' ) ) { return '<figure ' . $atts['id'] . 'style="width: ' . (int) $atts['width'] . 'px;" class="' . esc_attr( $class ) . '">' . do_shortcode( $content ) . '<figcaption class="wp-caption-text">' . $atts['caption'] . '</figcaption></figure>'; } $caption_width = 10 + $atts['width']; /** * Filter the width of an image's caption. * * By default, the caption is 10 pixels greater than the width of the image, * to prevent post content from running up against a floated image. * * @since 3.7.0 * * @see img_caption_shortcode() * * @param int $caption_width Width of the caption in pixels. To remove this inline style, * return zero. * @param array $atts Attributes of the caption shortcode. * @param string $content The image element, possibly wrapped in a hyperlink. */ $caption_width = apply_filters( 'img_caption_shortcode_width', $caption_width, $atts, $content ); $style = ''; if ( $caption_width ) $style = 'style="width: ' . (int) $caption_width . 'px" '; return '<div ' . $atts['id'] . $style . 'class="' . esc_attr( $class ) . '">' . do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>'; } |
関数間の処理 フックポイント
1 | add_shortcode('gallery', 'gallery_shortcode'); |
関数 gallery_shortcode()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 | /** * The Gallery shortcode. * * This implements the functionality of the Gallery Shortcode for displaying * WordPress images on a post. * * @since 2.5.0 * * @param array $attr { * Attributes of the gallery shortcode. * * @type string $order Order of the images in the gallery. Default 'ASC'. Accepts 'ASC', 'DESC'. * @type string $orderby The field to use when ordering the images. Default 'menu_order ID'. * Accepts any valid SQL ORDERBY statement. * @type int $id Post ID. * @type string $itemtag HTML tag to use for each image in the gallery. * Default 'dl', or 'figure' when the theme registers HTML5 gallery support. * @type string $icontag HTML tag to use for each image's icon. * Default 'dt', or 'div' when the theme registers HTML5 gallery support. * @type string $captiontag HTML tag to use for each image's caption. * Default 'dd', or 'figcaption' when the theme registers HTML5 gallery support. * @type int $columns Number of columns of images to display. Default 3. * @type string $size Size of the images to display. Default 'thumbnail'. * @type string $ids A comma-separated list of IDs of attachments to display. Default empty. * @type string $include A comma-separated list of IDs of attachments to include. Default empty. * @type string $exclude A comma-separated list of IDs of attachments to exclude. Default empty. * @type string $link What to link each image to. Default empty (links to the attachment page). * Accepts 'file', 'none'. * } * @return string HTML content to display gallery. */ function gallery_shortcode( $attr ) { $post = get_post(); static $instance = 0; $instance++; if ( ! empty( $attr['ids'] ) ) { // 'ids' is explicitly ordered, unless you specify otherwise. if ( empty( $attr['orderby'] ) ) { $attr['orderby'] = 'post__in'; } $attr['include'] = $attr['ids']; } /** * Filter the default gallery shortcode output. * * If the filtered output isn't empty, it will be used instead of generating * the default gallery template. * * @since 2.5.0 * * @see gallery_shortcode() * * @param string $output The gallery output. Default empty. * @param array $attr Attributes of the gallery shortcode. */ $output = apply_filters( 'post_gallery', '', $attr ); if ( $output != '' ) { return $output; } // We're trusting author input, so let's at least make sure it looks like a valid orderby statement if ( isset( $attr['orderby'] ) ) { $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); if ( ! $attr['orderby'] ) { unset( $attr['orderby'] ); } } $html5 = current_theme_supports( 'html5', 'gallery' ); $atts = shortcode_atts( array( 'order' => 'ASC', 'orderby' => 'menu_order ID', 'id' => $post ? $post->ID : 0, 'itemtag' => $html5 ? 'figure' : 'dl', 'icontag' => $html5 ? 'div' : 'dt', 'captiontag' => $html5 ? 'figcaption' : 'dd', 'columns' => 3, 'size' => 'thumbnail', 'include' => '', 'exclude' => '', 'link' => '' ), $attr, 'gallery' ); $id = intval( $atts['id'] ); if ( 'RAND' == $atts['order'] ) { $atts['orderby'] = 'none'; } if ( ! empty( $atts['include'] ) ) { $_attachments = get_posts( array( 'include' => $atts['include'], 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); $attachments = array(); foreach ( $_attachments as $key => $val ) { $attachments[$val->ID] = $_attachments[$key]; } } elseif ( ! empty( $atts['exclude'] ) ) { $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'] ) ); } else { $attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $atts['order'], 'orderby' => $atts['orderby'] ) ); } if ( empty( $attachments ) ) { return ''; } if ( is_feed() ) { $output = "\n"; foreach ( $attachments as $att_id => $attachment ) { $output .= wp_get_attachment_link( $att_id, $atts['size'], true ) . "\n"; } return $output; } $itemtag = tag_escape( $atts['itemtag'] ); $captiontag = tag_escape( $atts['captiontag'] ); $icontag = tag_escape( $atts['icontag'] ); $valid_tags = wp_kses_allowed_html( 'post' ); if ( ! isset( $valid_tags[ $itemtag ] ) ) { $itemtag = 'dl'; } if ( ! isset( $valid_tags[ $captiontag ] ) ) { $captiontag = 'dd'; } if ( ! isset( $valid_tags[ $icontag ] ) ) { $icontag = 'dt'; } $columns = intval( $atts['columns'] ); $itemwidth = $columns > 0 ? floor(100/$columns) : 100; $float = is_rtl() ? 'right' : 'left'; $selector = "gallery-{$instance}"; $gallery_style = ''; /** * Filter whether to print default gallery styles. * * @since 3.1.0 * * @param bool $print Whether to print default gallery styles. * Defaults to false if the theme supports HTML5 galleries. * Otherwise, defaults to true. */ if ( apply_filters( 'use_default_gallery_style', ! $html5 ) ) { $gallery_style = " <style type='text/css'> #{$selector} { margin: auto; } #{$selector} .gallery-item { float: {$float}; margin-top: 10px; text-align: center; width: {$itemwidth}%; } #{$selector} img { border: 2px solid #cfcfcf; } #{$selector} .gallery-caption { margin-left: 0; } /* see gallery_shortcode() in wp-includes/media.php */ </style>\n\t\t"; } $size_class = sanitize_html_class( $atts['size'] ); $gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; /** * Filter the default gallery shortcode CSS styles. * * @since 2.5.0 * * @param string $gallery_style Default gallery shortcode CSS styles. * @param string $gallery_div Opening HTML div container for the gallery shortcode output. */ $output = apply_filters( 'gallery_style', $gallery_style . $gallery_div ); $i = 0; foreach ( $attachments as $id => $attachment ) { if ( ! empty( $atts['link'] ) && 'file' === $atts['link'] ) { $image_output = wp_get_attachment_link( $id, $atts['size'], false, false ); } elseif ( ! empty( $atts['link'] ) && 'none' === $atts['link'] ) { $image_output = wp_get_attachment_image( $id, $atts['size'], false ); } else { $image_output = wp_get_attachment_link( $id, $atts['size'], true, false ); } $image_meta = wp_get_attachment_metadata( $id ); $orientation = ''; if ( isset( $image_meta['height'], $image_meta['width'] ) ) { $orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape'; } $output .= "<{$itemtag} class='gallery-item'>"; $output .= " <{$icontag} class='gallery-icon {$orientation}'> $image_output </{$icontag}>"; if ( $captiontag && trim($attachment->post_excerpt) ) { $output .= " <{$captiontag} class='wp-caption-text gallery-caption'> " . wptexturize($attachment->post_excerpt) . " </{$captiontag}>"; } $output .= "</{$itemtag}>"; if ( ! $html5 && $columns > 0 && ++$i % $columns == 0 ) { $output .= '<br style="clear: both" />'; } } if ( ! $html5 && $columns > 0 && $i % $columns !== 0 ) { $output .= " <br style='clear: both' />"; } $output .= " </div>\n"; return $output; } |