wp/wp-includes/media.php
author ymh <ymh.work@gmail.com>
Wed, 13 Nov 2013 17:59:35 +0000
changeset 3 6d22aaa62d12
parent 0 d970ebf37754
child 5 5e2f62d02dcd
permissions -rw-r--r--
correct portofolio item display
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * WordPress API for media display.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * @subpackage Media
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * Scale down the default size of an image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * This is so that the image is a better fit for the editor and theme.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 * The $size parameter accepts either an array or a string. The supported string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * values are 'thumb' or 'thumbnail' for the given thumbnail size or defaults at
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * 128 width and 96 height in pixels. Also supported for the string value is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * 'medium' and 'full'. The 'full' isn't actually supported, but any value other
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 * than the supported will result in the content_width size or 500 if that is
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
 * not set.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
 * Finally, there is a filter named 'editor_max_image_size', that will be called
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
 * on the calculated array for width and height, respectively. The second
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
 * parameter will be the value that was in the $size parameter. The returned
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
 * type for the hook is an array with the width as the first element and the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
 * height as the second element.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
 * @uses wp_constrain_dimensions() This function passes the widths and the heights.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
 * @param int $width Width of the image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 * @param int $height Height of the image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 * @param string|array $size Size of what the result image should be.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 * @param context Could be 'display' (like in a theme) or 'edit' (like inserting into an editor)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
 * @return array Width and height of what the result image should resize to.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
function image_constrain_size_for_editor($width, $height, $size = 'medium', $context = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
	global $content_width, $_wp_additional_image_sizes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
	if ( ! $context )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
		$context = is_admin() ? 'edit' : 'display';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
	if ( is_array($size) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
		$max_width = $size[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
		$max_height = $size[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
	elseif ( $size == 'thumb' || $size == 'thumbnail' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
		$max_width = intval(get_option('thumbnail_size_w'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
		$max_height = intval(get_option('thumbnail_size_h'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
		// last chance thumbnail size defaults
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
		if ( !$max_width && !$max_height ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
			$max_width = 128;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
			$max_height = 96;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	elseif ( $size == 'medium' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
		$max_width = intval(get_option('medium_size_w'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
		$max_height = intval(get_option('medium_size_h'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
		// if no width is set, default to the theme content width if available
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	elseif ( $size == 'large' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
		// We're inserting a large size image into the editor. If it's a really
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
		// big image we'll scale it down to fit reasonably within the editor
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
		// itself, and within the theme's content width if it's known. The user
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
		// can resize it in the editor if they wish.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
		$max_width = intval(get_option('large_size_w'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
		$max_height = intval(get_option('large_size_h'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
		if ( intval($content_width) > 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
			$max_width = min( intval($content_width), $max_width );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	} elseif ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) && in_array( $size, array_keys( $_wp_additional_image_sizes ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
		$max_width = intval( $_wp_additional_image_sizes[$size]['width'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
		$max_height = intval( $_wp_additional_image_sizes[$size]['height'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
		if ( intval($content_width) > 0 && 'edit' == $context ) // Only in admin. Assume that theme authors know what they're doing.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
			$max_width = min( intval($content_width), $max_width );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	// $size == 'full' has no constraint
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
		$max_width = $width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
		$max_height = $height;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
	list( $max_width, $max_height ) = apply_filters( 'editor_max_image_size', array( $max_width, $max_height ), $size, $context );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	return wp_constrain_dimensions( $width, $height, $max_width, $max_height );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
 * Retrieve width and height attributes using given width and height values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
 * Both attributes are required in the sense that both parameters must have a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
 * value, but are optional in that if you set them to false or null, then they
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
 * will not be added to the returned string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
 * You can set the value using a string, but it will only take numeric values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
 * If you wish to put 'px' after the numbers, then it will be stripped out of
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
 * the return.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
 * @param int|string $width Optional. Width attribute value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
 * @param int|string $height Optional. Height attribute value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
 * @return string HTML attributes for width and, or height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
function image_hwstring($width, $height) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	$out = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	if ($width)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
		$out .= 'width="'.intval($width).'" ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	if ($height)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
		$out .= 'height="'.intval($height).'" ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
	return $out;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
 * Scale an image to fit a particular size (such as 'thumb' or 'medium').
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
 * Array with image url, width, height, and whether is intermediate size, in
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
 * that order is returned on success is returned. $is_intermediate is true if
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
 * $url is a resized image, false if it is the original.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
 * The URL might be the original image, or it might be a resized version. This
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
 * function won't create a new resized copy, it will just return an already
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
 * resized one if it exists.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
 * A plugin may use the 'image_downsize' filter to hook into and offer image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
 * resizing services for images. The hook must return an array with the same
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
 * elements that are returned in the function. The first element being the URL
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
 * to the new image that was resized.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
 * @uses apply_filters() Calls 'image_downsize' on $id and $size to provide
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
 *		resize services.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
 * @param int $id Attachment ID for image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
 * @param array|string $size Optional, default is 'medium'. Size of image, either array or string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
 * @return bool|array False on failure, array on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
function image_downsize($id, $size = 'medium') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
	if ( !wp_attachment_is_image($id) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
	// plugins can use this to provide resize services
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
	if ( $out = apply_filters( 'image_downsize', false, $id, $size ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
		return $out;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
	$img_url = wp_get_attachment_url($id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
	$meta = wp_get_attachment_metadata($id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
	$width = $height = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
	$is_intermediate = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
	$img_url_basename = wp_basename($img_url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
	// try for a new style intermediate size
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
	if ( $intermediate = image_get_intermediate_size($id, $size) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
		$img_url = str_replace($img_url_basename, $intermediate['file'], $img_url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
		$width = $intermediate['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
		$height = $intermediate['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
		$is_intermediate = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
	elseif ( $size == 'thumbnail' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
		// fall back to the old thumbnail
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
		if ( ($thumb_file = wp_get_attachment_thumb_file($id)) && $info = getimagesize($thumb_file) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
			$img_url = str_replace($img_url_basename, wp_basename($thumb_file), $img_url);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
			$width = $info[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
			$height = $info[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
			$is_intermediate = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
	if ( !$width && !$height && isset( $meta['width'], $meta['height'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
		// any other type: use the real image
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		$width = $meta['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
		$height = $meta['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
	if ( $img_url) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
		// we have the actual image size, but might need to further constrain it if content_width is narrower
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
		list( $width, $height ) = image_constrain_size_for_editor( $width, $height, $size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
		return array( $img_url, $width, $height, $is_intermediate );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
 * Registers a new image size
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
function add_image_size( $name, $width = 0, $height = 0, $crop = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	global $_wp_additional_image_sizes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
	$_wp_additional_image_sizes[$name] = array( 'width' => absint( $width ), 'height' => absint( $height ), 'crop' => (bool) $crop );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
 * Registers an image size for the post thumbnail
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
function set_post_thumbnail_size( $width = 0, $height = 0, $crop = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
	add_image_size( 'post-thumbnail', $width, $height, $crop );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
 * An <img src /> tag for an image attachment, scaling it down if requested.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
 * The filter 'get_image_tag_class' allows for changing the class name for the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
 * image without having to use regular expressions on the HTML content. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
 * parameters are: what WordPress will use for the class, the Attachment ID,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
 * image align value, and the size the image should be.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
 * The second filter 'get_image_tag' has the HTML content, which can then be
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
 * further manipulated by a plugin to change all attribute values and even HTML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
 * content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
 * @uses apply_filters() The 'get_image_tag_class' filter is the IMG element
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
 *		class attribute.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
 * @uses apply_filters() The 'get_image_tag' filter is the full IMG element with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
 *		all attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
 * @param int $id Attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
 * @param string $alt Image Description for the alt attribute.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
 * @param string $title Image Description for the title attribute.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
 * @param string $align Part of the class name for aligning the image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
 * @param string $size Optional. Default is 'medium'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
 * @return string HTML IMG element for given image attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
function get_image_tag($id, $alt, $title, $align, $size='medium') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
	list( $img_src, $width, $height ) = image_downsize($id, $size);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
	$hwstring = image_hwstring($width, $height);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
	$title = $title ? 'title="' . esc_attr( $title ) . '" ' : '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
	$class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
	$class = apply_filters('get_image_tag_class', $class, $id, $align, $size);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   237
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
	$html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" ' . $title . $hwstring . 'class="' . $class . '" />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
	$html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
	return $html;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
 * Calculates the new dimensions for a downsampled image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
 * If either width or height are empty, no constraint is applied on
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
 * that dimension.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
 * @param int $current_width Current width of the image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
 * @param int $current_height Current height of the image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
 * @param int $max_width Optional. Maximum wanted width.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
 * @param int $max_height Optional. Maximum wanted height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
 * @return array First item is the width, the second item is the height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
function wp_constrain_dimensions( $current_width, $current_height, $max_width=0, $max_height=0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
	if ( !$max_width and !$max_height )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
		return array( $current_width, $current_height );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
	$width_ratio = $height_ratio = 1.0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
	$did_width = $did_height = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
	if ( $max_width > 0 && $current_width > 0 && $current_width > $max_width ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
		$width_ratio = $max_width / $current_width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
		$did_width = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
	if ( $max_height > 0 && $current_height > 0 && $current_height > $max_height ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
		$height_ratio = $max_height / $current_height;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
		$did_height = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	// Calculate the larger/smaller ratios
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
	$smaller_ratio = min( $width_ratio, $height_ratio );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
	$larger_ratio  = max( $width_ratio, $height_ratio );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
	if ( intval( $current_width * $larger_ratio ) > $max_width || intval( $current_height * $larger_ratio ) > $max_height )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
 		// The larger ratio is too big. It would result in an overflow.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
		$ratio = $smaller_ratio;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
		// The larger ratio fits, and is likely to be a more "snug" fit.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
		$ratio = $larger_ratio;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
	// Very small dimensions may result in 0, 1 should be the minimum.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
	$w = max ( 1, intval( $current_width  * $ratio ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
	$h = max ( 1, intval( $current_height * $ratio ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
	// Sometimes, due to rounding, we'll end up with a result like this: 465x700 in a 177x177 box is 117x176... a pixel short
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
	// 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.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
	// Thus we look for dimensions that are one pixel shy of the max value and bump them up
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
	if ( $did_width && $w == $max_width - 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
		$w = $max_width; // Round it up
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
	if ( $did_height && $h == $max_height - 1 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
		$h = $max_height; // Round it up
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
	return array( $w, $h );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
 * Retrieve calculated resized dimensions for use in WP_Image_Editor.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
 * Calculate dimensions and coordinates for a resized image that fits within a
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
 * specified width and height. If $crop is true, the largest matching central
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
 * portion of the image will be cropped out and resized to the required size.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
 * @uses apply_filters() Calls 'image_resize_dimensions' on $orig_w, $orig_h, $dest_w, $dest_h and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
 *		$crop to provide custom resize dimensions.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
 * @param int $orig_w Original width.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
 * @param int $orig_h Original height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
 * @param int $dest_w New width.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
 * @param int $dest_h New height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
 * @param bool $crop Optional, default is false. Whether to crop image or resize.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
 * @return bool|array False on failure. Returned array matches parameters for imagecopyresampled() PHP function.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
function image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
	if ($orig_w <= 0 || $orig_h <= 0)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
	// at least one of dest_w or dest_h must be specific
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
	if ($dest_w <= 0 && $dest_h <= 0)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
	// plugins can use this to provide custom resize dimensions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
	$output = apply_filters( 'image_resize_dimensions', null, $orig_w, $orig_h, $dest_w, $dest_h, $crop );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
	if ( null !== $output )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
	if ( $crop ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
		// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
		$aspect_ratio = $orig_w / $orig_h;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
		$new_w = min($dest_w, $orig_w);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		$new_h = min($dest_h, $orig_h);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
		if ( !$new_w ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
			$new_w = intval($new_h * $aspect_ratio);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
		if ( !$new_h ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
			$new_h = intval($new_w / $aspect_ratio);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
		$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
		$crop_w = round($new_w / $size_ratio);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
		$crop_h = round($new_h / $size_ratio);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		$s_x = floor( ($orig_w - $crop_w) / 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
		$s_y = floor( ($orig_h - $crop_h) / 2 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
		// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
		$crop_w = $orig_w;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
		$crop_h = $orig_h;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
		$s_x = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
		$s_y = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
		list( $new_w, $new_h ) = wp_constrain_dimensions( $orig_w, $orig_h, $dest_w, $dest_h );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   363
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
	// if the resulting image would be the same size or larger we don't want to resize it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
	if ( $new_w >= $orig_w && $new_h >= $orig_h )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
	// the return array matches the parameters to imagecopyresampled()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
	// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
	return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
 * Resize an image to make a thumbnail or intermediate size.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   377
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
 * The returned array has the file size, the image width, and image height. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   379
 * filter 'image_make_intermediate_size' can be used to hook in and change the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
 * values of the returned array. The only parameter is the resized file path.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   381
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
 * @param string $file File path.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
 * @param int $width Image width.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
 * @param int $height Image height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
 * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
 * @return bool|array False, if no image was created. Metadata array on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
function image_make_intermediate_size( $file, $width, $height, $crop = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
	if ( $width || $height ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		$editor = wp_get_image_editor( $file );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
		if ( is_wp_error( $editor ) || is_wp_error( $editor->resize( $width, $height, $crop ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
			return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
		$resized_file = $editor->save();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		if ( ! is_wp_error( $resized_file ) && $resized_file ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
			unset( $resized_file['path'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
			return $resized_file;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
 * Retrieve the image's intermediate size (resized) path, width, and height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
 * The $size parameter can be an array with the width and height respectively.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
 * If the size matches the 'sizes' metadata array for width and height, then it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
 * will be used. If there is no direct match, then the nearest image size larger
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
 * than the specified size will be used. If nothing is found, then the function
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
 * will break out and return false.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
 * The metadata 'sizes' is used for compatible sizes that can be used for the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   417
 * parameter $size value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
 * The url path will be given, when the $size parameter is a string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
 * If you are passing an array for the $size, you should consider using
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
 * add_image_size() so that a cropped version is generated. It's much more
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
 * efficient than having to find the closest-sized image and then having the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
 * browser scale down the image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   426
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
 * @see add_image_size()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
 * @param int $post_id Attachment ID for image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
 * @param array|string $size Optional, default is 'thumbnail'. Size of image, either array or string.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
 * @return bool|array False on failure or array of file path, width, and height on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
function image_get_intermediate_size($post_id, $size='thumbnail') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post_id ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
	// get the best one for a specified set of dimensions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
	if ( is_array($size) && !empty($imagedata['sizes']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
		foreach ( $imagedata['sizes'] as $_size => $data ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
			// already cropped to width or height; so use this size
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   441
			if ( ( $data['width'] == $size[0] && $data['height'] <= $size[1] ) || ( $data['height'] == $size[1] && $data['width'] <= $size[0] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
				$file = $data['file'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
				list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
				return compact( 'file', 'width', 'height' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
			// add to lookup table: area => size
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
			$areas[$data['width'] * $data['height']] = $_size;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   449
		if ( !$size || !empty($areas) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   450
			// find for the smallest image not smaller than the desired size
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   451
			ksort($areas);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   452
			foreach ( $areas as $_size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   453
				$data = $imagedata['sizes'][$_size];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   454
				if ( $data['width'] >= $size[0] || $data['height'] >= $size[1] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   455
					// Skip images with unexpectedly divergent aspect ratios (crops)
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   456
					// First, we calculate what size the original image would be if constrained to a box the size of the current image in the loop
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   457
					$maybe_cropped = image_resize_dimensions($imagedata['width'], $imagedata['height'], $data['width'], $data['height'], false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   458
					// 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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
					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'] ) ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
						continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
					// If we're still here, then we're going to use this size
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
					$file = $data['file'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
					list($width, $height) = image_constrain_size_for_editor( $data['width'], $data['height'], $size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
					return compact( 'file', 'width', 'height' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
	if ( is_array($size) || empty($size) || empty($imagedata['sizes'][$size]) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
		return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
	$data = $imagedata['sizes'][$size];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
	// include the full filesystem path of the intermediate file
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
	if ( empty($data['path']) && !empty($data['file']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
		$file_url = wp_get_attachment_url($post_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
		$data['path'] = path_join( dirname($imagedata['file']), $data['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
		$data['url'] = path_join( dirname($file_url), $data['file'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
	return $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
 * Get the available image sizes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
 * @since 3.0.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
 * @return array Returns a filtered array of image size strings
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   488
function get_intermediate_image_sizes() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
	global $_wp_additional_image_sizes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
	$image_sizes = array('thumbnail', 'medium', 'large'); // Standard sizes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
	if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
		$image_sizes = array_merge( $image_sizes, array_keys( $_wp_additional_image_sizes ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   494
	return apply_filters( 'intermediate_image_sizes', $image_sizes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   496
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   498
 * Retrieve an image to represent an attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
 * A mime icon for files, thumbnail or intermediate size for images.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
 * @param int $attachment_id Image attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
 * @param string $size Optional, default is 'thumbnail'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
 * @param bool $icon Optional, default is false. Whether it is an icon.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
 * @return bool|array Returns an array (url, width, height), or false, if no image is available.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   508
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
	// get a thumbnail or intermediate image if there is one
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
	if ( $image = image_downsize($attachment_id, $size) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   513
		return $image;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   515
	$src = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
	if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
		$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   519
		$src_file = $icon_dir . '/' . wp_basename($src);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
		@list($width, $height) = getimagesize($src_file);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
	if ( $src && $width && $height )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
		return array( $src, $width, $height );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
 * Get an HTML img element representing an image attachment
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
 * While $size will accept an array, it is better to register a size with
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
 * add_image_size() so that a cropped version is generated. It's much more
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
 * efficient than having to find the closest-sized image and then having the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
 * browser scale down the image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
 * @see add_image_size()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   536
 * @uses apply_filters() Calls 'wp_get_attachment_image_attributes' hook on attributes array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   537
 * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   538
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   539
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   540
 * @param int $attachment_id Image attachment ID.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   541
 * @param string $size Optional, default is 'thumbnail'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   542
 * @param bool $icon Optional, default is false. Whether it is an icon.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   543
 * @param mixed $attr Optional, attributes for the image markup.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   544
 * @return string HTML img element or empty string on failure.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   545
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   546
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   547
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   548
	$html = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   549
	$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   550
	if ( $image ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   551
		list($src, $width, $height) = $image;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   552
		$hwstring = image_hwstring($width, $height);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   553
		if ( is_array($size) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   554
			$size = join('x', $size);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   555
		$attachment = get_post($attachment_id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   556
		$default_attr = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   557
			'src'	=> $src,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   558
			'class'	=> "attachment-$size",
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   559
			'alt'	=> trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   560
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   561
		if ( empty($default_attr['alt']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   562
			$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   563
		if ( empty($default_attr['alt']) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   564
			$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   565
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   566
		$attr = wp_parse_args($attr, $default_attr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   567
		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   568
		$attr = array_map( 'esc_attr', $attr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   569
		$html = rtrim("<img $hwstring");
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   570
		foreach ( $attr as $name => $value ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   571
			$html .= " $name=" . '"' . $value . '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   572
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   573
		$html .= ' />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   574
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   575
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   576
	return $html;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   577
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   578
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   579
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   580
 * Adds a 'wp-post-image' class to post thumbnails
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   581
 * Uses the begin_fetch_post_thumbnail_html and end_fetch_post_thumbnail_html action hooks to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   582
 * dynamically add/remove itself so as to only filter post thumbnails
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   583
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   584
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   585
 * @param array $attr Attributes including src, class, alt, title
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   586
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   587
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   588
function _wp_post_thumbnail_class_filter( $attr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   589
	$attr['class'] .= ' wp-post-image';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   590
	return $attr;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   591
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   592
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   593
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   594
 * Adds _wp_post_thumbnail_class_filter to the wp_get_attachment_image_attributes filter
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   595
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   596
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   597
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   598
function _wp_post_thumbnail_class_filter_add( $attr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   599
	add_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   600
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   601
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   602
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   603
 * Removes _wp_post_thumbnail_class_filter from the wp_get_attachment_image_attributes filter
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   604
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   605
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   606
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   607
function _wp_post_thumbnail_class_filter_remove( $attr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   608
	remove_filter( 'wp_get_attachment_image_attributes', '_wp_post_thumbnail_class_filter' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   609
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   610
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   611
add_shortcode('wp_caption', 'img_caption_shortcode');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   612
add_shortcode('caption', 'img_caption_shortcode');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   614
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   615
 * The Caption shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   616
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   617
 * Allows a plugin to replace the content that would otherwise be returned. The
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   618
 * filter is 'img_caption_shortcode' and passes an empty string, the attr
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
 * parameter and the content parameter values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   620
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   621
 * The supported attributes for the shortcode are 'id', 'align', 'width', and
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   622
 * 'caption'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   623
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   624
 * @since 2.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   625
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   626
 * @param array $attr Attributes attributed to the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   627
 * @param string $content Optional. Shortcode content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   628
 * @return string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   629
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   630
function img_caption_shortcode($attr, $content = null) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   631
	// New-style shortcode with the caption inside the shortcode with the link and image tags.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   632
	if ( ! isset( $attr['caption'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   633
		if ( preg_match( '#((?:<a [^>]+>\s*)?<img [^>]+>(?:\s*</a>)?)(.*)#is', $content, $matches ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   634
			$content = $matches[1];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   635
			$attr['caption'] = trim( $matches[2] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   636
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   637
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   639
	// Allow plugins/themes to override the default caption template.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   640
	$output = apply_filters('img_caption_shortcode', '', $attr, $content);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   641
	if ( $output != '' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   642
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   643
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   644
	$atts = shortcode_atts( array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   645
		'id'	  => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   646
		'align'	  => 'alignnone',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   647
		'width'	  => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   648
		'caption' => ''
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   649
	), $attr, 'caption' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   650
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   651
	$atts['width'] = (int) $atts['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   652
	if ( $atts['width'] < 1 || empty( $atts['caption'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   653
		return $content;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   654
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   655
	if ( ! empty( $atts['id'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   656
		$atts['id'] = 'id="' . esc_attr( $atts['id'] ) . '" ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   657
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   658
	$caption_width = 10 + $atts['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   659
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   660
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   661
	 * Filter the width of an image's caption.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   662
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   663
	 * By default, the caption is 10 pixels greater than the width of the image,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   664
	 * to prevent post content from running up against a floated image.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   665
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   666
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   667
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   668
	 * @param int $caption_width Width in pixels. To remove this inline style, return zero.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   669
	 * @param array $atts {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   670
	 *     The attributes of the caption shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   671
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   672
	 *     @type string 'id'      The ID of the div element for the caption.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   673
	 *     @type string 'align'   The class name that aligns the caption. Default 'alignnone'.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   674
	 *     @type int    'width'   The width of the image being captioned.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   675
	 *     @type string 'caption' The image's caption.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   676
	 * }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   677
	 * @param string $content The image element, possibly wrapped in a hyperlink.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   678
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   679
	$caption_width = apply_filters( 'img_caption_shortcode_width', $caption_width, $atts, $content );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   681
	$style = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   682
	if ( $caption_width )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   683
		$style = 'style="width: ' . (int) $caption_width . 'px" ';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   684
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   685
	return '<div ' . $atts['id'] . $style . 'class="wp-caption ' . esc_attr( $atts['align'] ) . '">'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   686
	. do_shortcode( $content ) . '<p class="wp-caption-text">' . $atts['caption'] . '</p></div>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   687
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   688
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   689
add_shortcode('gallery', 'gallery_shortcode');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   690
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   691
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   692
 * The Gallery shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   693
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   694
 * This implements the functionality of the Gallery Shortcode for displaying
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   695
 * WordPress images on a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   696
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   697
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   698
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   699
 * @param array $attr Attributes of the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   700
 * @return string HTML content to display gallery.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   701
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   702
function gallery_shortcode($attr) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   703
	$post = get_post();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   704
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   705
	static $instance = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   706
	$instance++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   707
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   708
	if ( ! empty( $attr['ids'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   709
		// 'ids' is explicitly ordered, unless you specify otherwise.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   710
		if ( empty( $attr['orderby'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   711
			$attr['orderby'] = 'post__in';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   712
		$attr['include'] = $attr['ids'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   713
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   714
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   715
	// Allow plugins/themes to override the default gallery template.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   716
	$output = apply_filters('post_gallery', '', $attr);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   717
	if ( $output != '' )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   718
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   719
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   720
	// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   721
	if ( isset( $attr['orderby'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   722
		$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   723
		if ( !$attr['orderby'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   724
			unset( $attr['orderby'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   725
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   726
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   727
	extract(shortcode_atts(array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   728
		'order'      => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   729
		'orderby'    => 'menu_order ID',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   730
		'id'         => $post ? $post->ID : 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   731
		'itemtag'    => 'dl',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   732
		'icontag'    => 'dt',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   733
		'captiontag' => 'dd',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   734
		'columns'    => 3,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   735
		'size'       => 'thumbnail',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   736
		'include'    => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   737
		'exclude'    => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   738
		'link'       => ''
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   739
	), $attr, 'gallery'));
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   740
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   741
	$id = intval($id);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   742
	if ( 'RAND' == $order )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   743
		$orderby = 'none';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   744
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   745
	if ( !empty($include) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   746
		$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   747
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   748
		$attachments = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   749
		foreach ( $_attachments as $key => $val ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   750
			$attachments[$val->ID] = $_attachments[$key];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   751
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   752
	} elseif ( !empty($exclude) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   753
		$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   754
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   755
		$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   756
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   757
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   758
	if ( empty($attachments) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   759
		return '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   760
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   761
	if ( is_feed() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   762
		$output = "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   763
		foreach ( $attachments as $att_id => $attachment )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   764
			$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   765
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   766
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   767
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   768
	$itemtag = tag_escape($itemtag);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   769
	$captiontag = tag_escape($captiontag);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   770
	$icontag = tag_escape($icontag);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   771
	$valid_tags = wp_kses_allowed_html( 'post' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   772
	if ( ! isset( $valid_tags[ $itemtag ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   773
		$itemtag = 'dl';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   774
	if ( ! isset( $valid_tags[ $captiontag ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   775
		$captiontag = 'dd';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   776
	if ( ! isset( $valid_tags[ $icontag ] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   777
		$icontag = 'dt';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   778
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   779
	$columns = intval($columns);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   780
	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   781
	$float = is_rtl() ? 'right' : 'left';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   782
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   783
	$selector = "gallery-{$instance}";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   784
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   785
	$gallery_style = $gallery_div = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   786
	if ( apply_filters( 'use_default_gallery_style', true ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   787
		$gallery_style = "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   788
		<style type='text/css'>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   789
			#{$selector} {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   790
				margin: auto;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   791
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   792
			#{$selector} .gallery-item {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   793
				float: {$float};
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   794
				margin-top: 10px;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   795
				text-align: center;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   796
				width: {$itemwidth}%;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   797
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   798
			#{$selector} img {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   799
				border: 2px solid #cfcfcf;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   800
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   801
			#{$selector} .gallery-caption {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   802
				margin-left: 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   803
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   804
			/* see gallery_shortcode() in wp-includes/media.php */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   805
		</style>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   806
	$size_class = sanitize_html_class( $size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   807
	$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   808
	$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   809
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   810
	$i = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   811
	foreach ( $attachments as $id => $attachment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   812
		if ( ! empty( $link ) && 'file' === $link )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   813
			$image_output = wp_get_attachment_link( $id, $size, false, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   814
		elseif ( ! empty( $link ) && 'none' === $link )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   815
			$image_output = wp_get_attachment_image( $id, $size, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   816
		else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   817
			$image_output = wp_get_attachment_link( $id, $size, true, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   818
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   819
		$image_meta  = wp_get_attachment_metadata( $id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   820
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   821
		$orientation = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   822
		if ( isset( $image_meta['height'], $image_meta['width'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   823
			$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   824
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   825
		$output .= "<{$itemtag} class='gallery-item'>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   826
		$output .= "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   827
			<{$icontag} class='gallery-icon {$orientation}'>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   828
				$image_output
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   829
			</{$icontag}>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   830
		if ( $captiontag && trim($attachment->post_excerpt) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   831
			$output .= "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   832
				<{$captiontag} class='wp-caption-text gallery-caption'>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   833
				" . wptexturize($attachment->post_excerpt) . "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   834
				</{$captiontag}>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   835
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   836
		$output .= "</{$itemtag}>";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   837
		if ( $columns > 0 && ++$i % $columns == 0 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   838
			$output .= '<br style="clear: both" />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   839
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   840
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   841
	$output .= "
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   842
			<br style='clear: both;' />
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   843
		</div>\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   844
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   845
	return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   846
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   847
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   848
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   849
 * Provide a No-JS Flash fallback as a last resort for audio / video
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   850
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   851
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   852
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   853
 * @param string $url
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   854
 * @return string Fallback HTML
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   855
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   856
function wp_mediaelement_fallback( $url ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   857
	return apply_filters( 'wp_mediaelement_fallback', sprintf( '<a href="%1$s">%1$s</a>', esc_url( $url ) ), $url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   858
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   859
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   860
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   861
 * Return a filtered list of WP-supported audio formats
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   862
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   863
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   864
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   865
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   866
function wp_get_audio_extensions() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   867
	return apply_filters( 'wp_audio_extensions', array( 'mp3', 'ogg', 'wma', 'm4a', 'wav' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   868
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   869
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   870
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   871
 * The Audio shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   872
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   873
 * This implements the functionality of the Audio Shortcode for displaying
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   874
 * WordPress mp3s in a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   875
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   876
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   877
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   878
 * @param array  $attr    Attributes of the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   879
 * @param string $content Optional. Shortcode content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   880
 * @return string HTML content to display audio.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   881
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   882
function wp_audio_shortcode( $attr, $content = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   883
	$post_id = get_post() ? get_the_ID() : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   884
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   885
	static $instances = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   886
	$instances++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   887
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   888
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   889
	 * Override the default audio shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   890
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   891
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   892
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   893
	 * @param null              Empty variable to be replaced with shortcode markup.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   894
	 * @param array  $attr      Attributes of the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   895
	 * @param string $content   Shortcode content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   896
	 * @param int    $instances Unique numeric ID of this audio shortcode instance.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   897
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   898
	$html = apply_filters( 'wp_audio_shortcode_override', '', $attr, $content, $instances );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   899
	if ( '' !== $html )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   900
		return $html;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   901
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   902
	$audio = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   903
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   904
	$default_types = wp_get_audio_extensions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   905
	$defaults_atts = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   906
		'src'      => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   907
		'loop'     => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   908
		'autoplay' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   909
		'preload'  => 'none'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   910
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   911
	foreach ( $default_types as $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   912
		$defaults_atts[$type] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   913
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   914
	$atts = shortcode_atts( $defaults_atts, $attr, 'audio' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   915
	extract( $atts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   916
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   917
	$primary = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   918
	if ( ! empty( $src ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   919
		$type = wp_check_filetype( $src, wp_get_mime_types() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   920
		if ( ! in_array( strtolower( $type['ext'] ), $default_types ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   921
			return sprintf( '<a class="wp-embedded-audio" href="%s">%s</a>', esc_url( $src ), esc_html( $src ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   922
		$primary = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   923
		array_unshift( $default_types, 'src' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   924
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   925
		foreach ( $default_types as $ext ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   926
			if ( ! empty( $$ext ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   927
				$type = wp_check_filetype( $$ext, wp_get_mime_types() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   928
				if ( strtolower( $type['ext'] ) === $ext )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   929
					$primary = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   930
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   931
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   932
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   933
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   934
	if ( ! $primary ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   935
		$audios = get_attached_media( 'audio', $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   936
		if ( empty( $audios ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   937
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   938
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   939
		$audio = reset( $audios );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   940
		$src = wp_get_attachment_url( $audio->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   941
		if ( empty( $src ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   942
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   943
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   944
		array_unshift( $default_types, 'src' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   945
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   946
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   947
	$library = apply_filters( 'wp_audio_shortcode_library', 'mediaelement' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   948
	if ( 'mediaelement' === $library && did_action( 'init' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   949
		wp_enqueue_style( 'wp-mediaelement' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   950
		wp_enqueue_script( 'wp-mediaelement' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   951
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   952
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   953
	$atts = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   954
		'class'    => apply_filters( 'wp_audio_shortcode_class', 'wp-audio-shortcode' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   955
		'id'       => sprintf( 'audio-%d-%d', $post_id, $instances ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   956
		'loop'     => $loop,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   957
		'autoplay' => $autoplay,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   958
		'preload'  => $preload,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   959
		'style'    => 'width: 100%',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   960
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   961
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   962
	// These ones should just be omitted altogether if they are blank
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   963
	foreach ( array( 'loop', 'autoplay', 'preload' ) as $a ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   964
		if ( empty( $atts[$a] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   965
			unset( $atts[$a] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   966
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   967
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   968
	$attr_strings = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   969
	foreach ( $atts as $k => $v ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   970
		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   971
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   972
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   973
	$html = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   974
	if ( 'mediaelement' === $library && 1 === $instances )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   975
		$html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   976
	$html .= sprintf( '<audio %s controls="controls">', join( ' ', $attr_strings ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   978
	$fileurl = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   979
	$source = '<source type="%s" src="%s" />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   980
	foreach ( $default_types as $fallback ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   981
		if ( ! empty( $$fallback ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   982
			if ( empty( $fileurl ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   983
				$fileurl = $$fallback;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   984
			$type = wp_check_filetype( $$fallback, wp_get_mime_types() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   985
			$html .= sprintf( $source, $type['type'], esc_url( $$fallback ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   986
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   987
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   988
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   989
	if ( 'mediaelement' === $library )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   990
		$html .= wp_mediaelement_fallback( $fileurl );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   991
	$html .= '</audio>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   992
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   993
	return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   994
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   995
add_shortcode( 'audio', 'wp_audio_shortcode' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   996
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   997
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   998
 * Return a filtered list of WP-supported video formats
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   999
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1000
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1001
 * @return array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1002
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1003
function wp_get_video_extensions() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1004
	return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1005
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1006
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1007
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1008
 * The Video shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1009
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1010
 * This implements the functionality of the Video Shortcode for displaying
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1011
 * WordPress mp4s in a post.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1012
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1013
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1014
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1015
 * @param array  $attr    Attributes of the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1016
 * @param string $content Optional. Shortcode content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1017
 * @return string HTML content to display video.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1018
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1019
function wp_video_shortcode( $attr, $content = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1020
	global $content_width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1021
	$post_id = get_post() ? get_the_ID() : 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1022
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1023
	static $instances = 0;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1024
	$instances++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1025
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1026
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1027
	 * Override the default video shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1028
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1029
	 * @since 3.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1030
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1031
	 * @param null              Empty variable to be replaced with shortcode markup.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1032
	 * @param array  $attr      Attributes of the shortcode.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1033
	 * @param string $content   Shortcode content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1034
	 * @param int    $instances Unique numeric ID of this video shortcode instance.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1035
	 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1036
	$html = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instances );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1037
	if ( '' !== $html )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1038
		return $html;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1039
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1040
	$video = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1041
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1042
	$default_types = wp_get_video_extensions();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1043
	$defaults_atts = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1044
		'src'      => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1045
		'poster'   => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1046
		'loop'     => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1047
		'autoplay' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1048
		'preload'  => 'metadata',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1049
		'height'   => 360,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1050
		'width'    => empty( $content_width ) ? 640 : $content_width,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1051
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1052
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1053
	foreach ( $default_types as $type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1054
		$defaults_atts[$type] = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1055
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1056
	$atts = shortcode_atts( $defaults_atts, $attr, 'video' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1057
	extract( $atts );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1058
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1059
	$w = $width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1060
	$h = $height;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1061
	if ( is_admin() && $width > 600 )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1062
		$w = 600;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1063
	elseif ( ! is_admin() && $w > $defaults_atts['width'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1064
		$w = $defaults_atts['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1066
	if ( $w < $width )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1067
		$height = round( ( $h * $w ) / $width );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1068
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1069
	$width = $w;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1070
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1071
	$primary = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1072
	if ( ! empty( $src ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1073
		$type = wp_check_filetype( $src, wp_get_mime_types() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1074
		if ( ! in_array( strtolower( $type['ext'] ), $default_types ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1075
			return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $src ), esc_html( $src ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1076
		$primary = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1077
		array_unshift( $default_types, 'src' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1078
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1079
		foreach ( $default_types as $ext ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1080
			if ( ! empty( $$ext ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1081
				$type = wp_check_filetype( $$ext, wp_get_mime_types() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1082
				if ( strtolower( $type['ext'] ) === $ext )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1083
					$primary = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1084
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1085
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1086
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1087
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1088
	if ( ! $primary ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1089
		$videos = get_attached_media( 'video', $post_id );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1090
		if ( empty( $videos ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1091
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1092
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1093
		$video = reset( $videos );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1094
		$src = wp_get_attachment_url( $video->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1095
		if ( empty( $src ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1096
			return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1097
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1098
		array_unshift( $default_types, 'src' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1099
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1100
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1101
	$library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1102
	if ( 'mediaelement' === $library && did_action( 'init' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1103
		wp_enqueue_style( 'wp-mediaelement' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1104
		wp_enqueue_script( 'wp-mediaelement' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1105
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1106
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1107
	$atts = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1108
		'class'    => apply_filters( 'wp_video_shortcode_class', 'wp-video-shortcode' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1109
		'id'       => sprintf( 'video-%d-%d', $post_id, $instances ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1110
		'width'    => absint( $width ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1111
		'height'   => absint( $height ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1112
		'poster'   => esc_url( $poster ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1113
		'loop'     => $loop,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1114
		'autoplay' => $autoplay,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1115
		'preload'  => $preload,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1116
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1117
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1118
	// These ones should just be omitted altogether if they are blank
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1119
	foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1120
		if ( empty( $atts[$a] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1121
			unset( $atts[$a] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1122
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1123
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1124
	$attr_strings = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1125
	foreach ( $atts as $k => $v ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1126
		$attr_strings[] = $k . '="' . esc_attr( $v ) . '"';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1127
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1128
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1129
	$html = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1130
	if ( 'mediaelement' === $library && 1 === $instances )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1131
		$html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1132
	$html .= sprintf( '<video %s controls="controls">', join( ' ', $attr_strings ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1133
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1134
	$fileurl = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1135
	$source = '<source type="%s" src="%s" />';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1136
	foreach ( $default_types as $fallback ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1137
		if ( ! empty( $$fallback ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1138
			if ( empty( $fileurl ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1139
				$fileurl = $$fallback;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1140
			$type = wp_check_filetype( $$fallback, wp_get_mime_types() );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1141
			// m4v sometimes shows up as video/mpeg which collides with mp4
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1142
			if ( 'm4v' === $type['ext'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1143
				$type['type'] = 'video/m4v';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1144
			$html .= sprintf( $source, $type['type'], esc_url( $$fallback ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1145
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1146
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1147
	if ( 'mediaelement' === $library )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1148
		$html .= wp_mediaelement_fallback( $fileurl );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1149
	$html .= '</video>';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1150
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1151
	$html = sprintf( '<div style="width: %dpx; max-width: 100%%;">%s</div>', $width, $html );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1152
	return apply_filters( 'wp_video_shortcode', $html, $atts, $video, $post_id, $library );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1153
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1154
add_shortcode( 'video', 'wp_video_shortcode' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1155
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1156
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1157
 * Display previous image link that has the same post parent.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1158
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1159
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1160
 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1161
 * @param string $text Optional, default is false. If included, link will reflect $text variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1162
 * @return string HTML content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1163
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1164
function previous_image_link($size = 'thumbnail', $text = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1165
	adjacent_image_link(true, $size, $text);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1166
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1167
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1168
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1169
 * Display next image link that has the same post parent.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1170
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1171
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1172
 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1173
 * @param string $text Optional, default is false. If included, link will reflect $text variable.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1174
 * @return string HTML content.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1175
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1176
function next_image_link($size = 'thumbnail', $text = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1177
	adjacent_image_link(false, $size, $text);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1178
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1179
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1180
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1181
 * Display next or previous image link that has the same post parent.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1182
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1183
 * Retrieves the current attachment object from the $post global.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1184
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1185
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1186
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1187
 * @param bool $prev Optional. Default is true to display previous link, false for next.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1188
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1189
function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1190
	$post = get_post();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1191
	$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' ) ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1192
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1193
	foreach ( $attachments as $k => $attachment )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1194
		if ( $attachment->ID == $post->ID )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1195
			break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1196
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1197
	$k = $prev ? $k - 1 : $k + 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1198
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1199
	$output = $attachment_id = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1200
	if ( isset( $attachments[ $k ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1201
		$attachment_id = $attachments[ $k ]->ID;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1202
		$output = wp_get_attachment_link( $attachment_id, $size, true, false, $text );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1203
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1204
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1205
	$adjacent = $prev ? 'previous' : 'next';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1206
	echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1207
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1208
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1209
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1210
 * Retrieve taxonomies attached to the attachment.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1211
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1212
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1213
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1214
 * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1215
 * @return array Empty array on failure. List of taxonomies on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1216
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1217
function get_attachment_taxonomies($attachment) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1218
	if ( is_int( $attachment ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1219
		$attachment = get_post($attachment);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1220
	else if ( is_array($attachment) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1221
		$attachment = (object) $attachment;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1222
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1223
	if ( ! is_object($attachment) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1224
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1225
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1226
	$filename = basename($attachment->guid);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1227
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1228
	$objects = array('attachment');
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1229
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1230
	if ( false !== strpos($filename, '.') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1231
		$objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1232
	if ( !empty($attachment->post_mime_type) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1233
		$objects[] = 'attachment:' . $attachment->post_mime_type;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1234
		if ( false !== strpos($attachment->post_mime_type, '/') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1235
			foreach ( explode('/', $attachment->post_mime_type) as $token )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1236
				if ( !empty($token) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1237
					$objects[] = "attachment:$token";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1238
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1239
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1240
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1241
	foreach ( $objects as $object )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1242
		if ( $taxes = get_object_taxonomies($object) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1243
			$taxonomies = array_merge($taxonomies, $taxes);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1244
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1245
	return array_unique($taxonomies);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1246
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1247
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1248
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1249
 * Return all of the taxonomy names that are registered for attachments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1250
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1251
 * Handles mime-type-specific taxonomies such as attachment:image and attachment:video.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1252
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1253
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1254
 * @see get_attachment_taxonomies()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1255
 * @uses get_taxonomies()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1256
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1257
 * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1258
 * @return array The names of all taxonomy of $object_type.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1259
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1260
function get_taxonomies_for_attachments( $output = 'names' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1261
	$taxonomies = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1262
	foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1263
		foreach ( $taxonomy->object_type as $object_type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1264
			if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1265
				if ( 'names' == $output )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1266
					$taxonomies[] = $taxonomy->name;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1267
				else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1268
					$taxonomies[ $taxonomy->name ] = $taxonomy;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1269
				break;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1270
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1271
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1272
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1273
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1274
	return $taxonomies;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1275
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1276
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1277
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1278
 * Create new GD image resource with transparency support
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1279
 * @TODO: Deprecate if possible.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1280
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1281
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1282
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1283
 * @param int $width Image width
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1284
 * @param int $height Image height
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1285
 * @return image resource
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1286
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1287
function wp_imagecreatetruecolor($width, $height) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1288
	$img = imagecreatetruecolor($width, $height);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1289
	if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1290
		imagealphablending($img, false);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1291
		imagesavealpha($img, true);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1292
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1293
	return $img;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1294
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1295
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1296
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1297
 * Register an embed handler. This function should probably only be used for sites that do not support oEmbed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1298
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1299
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1300
 * @see WP_Embed::register_handler()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1301
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1302
function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1303
	global $wp_embed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1304
	$wp_embed->register_handler( $id, $regex, $callback, $priority );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1305
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1306
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1307
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1308
 * Unregister a previously registered embed handler.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1309
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1310
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1311
 * @see WP_Embed::unregister_handler()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1312
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1313
function wp_embed_unregister_handler( $id, $priority = 10 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1314
	global $wp_embed;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1315
	$wp_embed->unregister_handler( $id, $priority );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1316
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1317
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1318
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1319
 * Create default array of embed parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1320
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1321
 * The width defaults to the content width as specified by the theme. If the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1322
 * theme does not specify a content width, then 500px is used.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1323
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1324
 * The default height is 1.5 times the width, or 1000px, whichever is smaller.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1325
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1326
 * The 'embed_defaults' filter can be used to adjust either of these values.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1327
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1328
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1329
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1330
 * @return array Default embed parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1331
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1332
function wp_embed_defaults() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1333
	if ( ! empty( $GLOBALS['content_width'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1334
		$width = (int) $GLOBALS['content_width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1335
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1336
	if ( empty( $width ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1337
		$width = 500;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1338
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1339
	$height = min( ceil( $width * 1.5 ), 1000 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1340
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1341
	return apply_filters( 'embed_defaults', compact( 'width', 'height' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1342
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1343
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1344
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1345
 * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1346
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1347
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1348
 * @uses wp_constrain_dimensions() This function passes the widths and the heights.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1349
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1350
 * @param int $example_width The width of an example embed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1351
 * @param int $example_height The height of an example embed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1352
 * @param int $max_width The maximum allowed width.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1353
 * @param int $max_height The maximum allowed height.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1354
 * @return array The maximum possible width and height based on the example ratio.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1355
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1356
function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1357
	$example_width  = (int) $example_width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1358
	$example_height = (int) $example_height;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1359
	$max_width      = (int) $max_width;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1360
	$max_height     = (int) $max_height;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1361
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1362
	return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1363
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1364
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1365
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1366
 * Attempts to fetch the embed HTML for a provided URL using oEmbed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1367
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1368
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1369
 * @see WP_oEmbed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1370
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1371
 * @uses _wp_oembed_get_object()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1372
 * @uses WP_oEmbed::get_html()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1373
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1374
 * @param string $url The URL that should be embedded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1375
 * @param array $args Additional arguments and parameters.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1376
 * @return bool|string False on failure or the embed HTML on success.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1377
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1378
function wp_oembed_get( $url, $args = '' ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1379
	require_once( ABSPATH . WPINC . '/class-oembed.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1380
	$oembed = _wp_oembed_get_object();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1381
	return $oembed->get_html( $url, $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1382
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1383
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1384
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1385
 * Adds a URL format and oEmbed provider URL pair.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1386
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1387
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1388
 * @see WP_oEmbed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1389
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1390
 * @uses _wp_oembed_get_object()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1391
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1392
 * @param string $format The format of URL that this provider can handle. You can use asterisks as wildcards.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1393
 * @param string $provider The URL to the oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1394
 * @param boolean $regex Whether the $format parameter is in a regex format.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1395
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1396
function wp_oembed_add_provider( $format, $provider, $regex = false ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1397
	require_once( ABSPATH . WPINC . '/class-oembed.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1398
	$oembed = _wp_oembed_get_object();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1399
	$oembed->providers[$format] = array( $provider, $regex );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1400
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1401
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1402
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1403
 * Removes an oEmbed provider.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1404
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1405
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1406
 * @see WP_oEmbed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1407
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1408
 * @uses _wp_oembed_get_object()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1409
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1410
 * @param string $format The URL format for the oEmbed provider to remove.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1411
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1412
function wp_oembed_remove_provider( $format ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1413
	require_once( ABSPATH . WPINC . '/class-oembed.php' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1414
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1415
	$oembed = _wp_oembed_get_object();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1416
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1417
	if ( isset( $oembed->providers[ $format ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1418
		unset( $oembed->providers[ $format ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1419
		return true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1420
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1421
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1422
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1423
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1424
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1425
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1426
 * Determines if default embed handlers should be loaded.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1427
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1428
 * Checks to make sure that the embeds library hasn't already been loaded. If
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1429
 * it hasn't, then it will load the embeds library.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1430
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1431
 * @since 2.9.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1432
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1433
function wp_maybe_load_embeds() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1434
	if ( ! apply_filters( 'load_default_embeds', true ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1435
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1436
	wp_embed_register_handler( 'googlevideo', '#http://video\.google\.([A-Za-z.]{2,5})/videoplay\?docid=([\d-]+)(.*?)#i', 'wp_embed_handler_googlevideo' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1437
	wp_embed_register_handler( 'audio', '#^https?://.+?\.(' . join( '|', wp_get_audio_extensions() ) . ')$#i', apply_filters( 'wp_audio_embed_handler', 'wp_embed_handler_audio' ), 9999 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1438
	wp_embed_register_handler( 'video', '#^https?://.+?\.(' . join( '|', wp_get_video_extensions() ) . ')$#i', apply_filters( 'wp_video_embed_handler', 'wp_embed_handler_video' ), 9999 );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1439
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1440
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1441
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1442
 * The Google Video embed handler callback. Google Video does not support oEmbed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1443
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1444
 * @see WP_Embed::register_handler()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1445
 * @see WP_Embed::shortcode()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1446
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1447
 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1448
 * @param array $attr Embed attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1449
 * @param string $url The original URL that was matched by the regex.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1450
 * @param array $rawattr The original unmodified attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1451
 * @return string The embed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1452
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1453
function wp_embed_handler_googlevideo( $matches, $attr, $url, $rawattr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1454
	// If the user supplied a fixed width AND height, use it
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1455
	if ( !empty($rawattr['width']) && !empty($rawattr['height']) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1456
		$width  = (int) $rawattr['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1457
		$height = (int) $rawattr['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1458
	} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1459
		list( $width, $height ) = wp_expand_dimensions( 425, 344, $attr['width'], $attr['height'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1460
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1461
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1462
	return apply_filters( 'embed_googlevideo', '<embed type="application/x-shockwave-flash" src="http://video.google.com/googleplayer.swf?docid=' . esc_attr($matches[2]) . '&amp;hl=en&amp;fs=true" style="width:' . esc_attr($width) . 'px;height:' . esc_attr($height) . 'px" allowFullScreen="true" allowScriptAccess="always" />', $matches, $attr, $url, $rawattr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1463
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1464
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1465
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1466
 * Audio embed handler callback.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1467
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1468
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1469
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1470
 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1471
 * @param array $attr Embed attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1472
 * @param string $url The original URL that was matched by the regex.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1473
 * @param array $rawattr The original unmodified attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1474
 * @return string The embed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1475
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1476
function wp_embed_handler_audio( $matches, $attr, $url, $rawattr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1477
	$audio = sprintf( '[audio src="%s" /]', esc_url( $url ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1478
	return apply_filters( 'wp_embed_handler_audio', $audio, $attr, $url, $rawattr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1479
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1480
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1481
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1482
 * Video embed handler callback.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1483
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1484
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1485
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1486
 * @param array $matches The regex matches from the provided regex when calling {@link wp_embed_register_handler()}.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1487
 * @param array $attr Embed attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1488
 * @param string $url The original URL that was matched by the regex.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1489
 * @param array $rawattr The original unmodified attributes.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1490
 * @return string The embed HTML.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1491
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1492
function wp_embed_handler_video( $matches, $attr, $url, $rawattr ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1493
	$dimensions = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1494
	if ( ! empty( $rawattr['width'] ) && ! empty( $rawattr['height'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1495
		$dimensions .= sprintf( 'width="%d" ', (int) $rawattr['width'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1496
		$dimensions .= sprintf( 'height="%d" ', (int) $rawattr['height'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1497
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1498
	$video = sprintf( '[video %s src="%s" /]', $dimensions, esc_url( $url ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1499
	return apply_filters( 'wp_embed_handler_video', $video, $attr, $url, $rawattr );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1500
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1501
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1502
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1503
 * Converts a shorthand byte value to an integer byte value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1504
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1505
 * @since 2.3.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1506
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1507
 * @param string $size A shorthand byte value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1508
 * @return int An integer byte value.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1509
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1510
function wp_convert_hr_to_bytes( $size ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1511
	$size  = strtolower( $size );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1512
	$bytes = (int) $size;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1513
	if ( strpos( $size, 'k' ) !== false )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1514
		$bytes = intval( $size ) * 1024;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1515
	elseif ( strpos( $size, 'm' ) !== false )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1516
		$bytes = intval($size) * 1024 * 1024;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1517
	elseif ( strpos( $size, 'g' ) !== false )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1518
		$bytes = intval( $size ) * 1024 * 1024 * 1024;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1519
	return $bytes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1520
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1521
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1522
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1523
 * Determine the maximum upload size allowed in php.ini.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1524
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1525
 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1526
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1527
 * @return int Allowed upload size.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1528
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1529
function wp_max_upload_size() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1530
	$u_bytes = wp_convert_hr_to_bytes( ini_get( 'upload_max_filesize' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1531
	$p_bytes = wp_convert_hr_to_bytes( ini_get( 'post_max_size' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1532
	$bytes   = apply_filters( 'upload_size_limit', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1533
	return $bytes;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1534
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1535
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1536
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1537
 * Returns a WP_Image_Editor instance and loads file into it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1538
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1539
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1540
 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1541
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1542
 * @param string $path Path to file to load
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1543
 * @param array $args Additional data. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1544
 * @return WP_Image_Editor|WP_Error
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1545
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1546
function wp_get_image_editor( $path, $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1547
	$args['path'] = $path;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1548
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1549
	if ( ! isset( $args['mime_type'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1550
		$file_info = wp_check_filetype( $args['path'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1551
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1552
		// If $file_info['type'] is false, then we let the editor attempt to
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1553
		// figure out the file type, rather than forcing a failure based on extension.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1554
		if ( isset( $file_info ) && $file_info['type'] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1555
			$args['mime_type'] = $file_info['type'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1556
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1557
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1558
	$implementation = _wp_image_editor_choose( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1559
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1560
	if ( $implementation ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1561
		$editor = new $implementation( $path );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1562
		$loaded = $editor->load();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1563
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1564
		if ( is_wp_error( $loaded ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1565
			return $loaded;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1566
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1567
		return $editor;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1568
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1569
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1570
	return new WP_Error( 'image_no_editor', __('No editor could be selected.') );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1571
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1572
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1573
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1574
 * Tests whether there is an editor that supports a given mime type or methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1575
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1576
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1577
 * @access public
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1578
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1579
 * @param string|array $args Array of requirements. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1580
 * @return boolean true if an eligible editor is found; false otherwise
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1581
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1582
function wp_image_editor_supports( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1583
	return (bool) _wp_image_editor_choose( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1584
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1585
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1586
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1587
 * Tests which editors are capable of supporting the request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1588
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1589
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1590
 * @access private
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1591
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1592
 * @param array $args Additional data. Accepts { 'mime_type'=>string, 'methods'=>{string, string, ...} }
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1593
 * @return string|bool Class name for the first editor that claims to support the request. False if no editor claims to support the request.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1594
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1595
function _wp_image_editor_choose( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1596
	require_once ABSPATH . WPINC . '/class-wp-image-editor.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1597
	require_once ABSPATH . WPINC . '/class-wp-image-editor-gd.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1598
	require_once ABSPATH . WPINC . '/class-wp-image-editor-imagick.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1599
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1600
	$implementations = apply_filters( 'wp_image_editors',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1601
		array( 'WP_Image_Editor_Imagick', 'WP_Image_Editor_GD' ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1602
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1603
	foreach ( $implementations as $implementation ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1604
		if ( ! call_user_func( array( $implementation, 'test' ), $args ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1605
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1606
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1607
		if ( isset( $args['mime_type'] ) &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1608
			! call_user_func(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1609
				array( $implementation, 'supports_mime_type' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1610
				$args['mime_type'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1611
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1612
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1613
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1614
		if ( isset( $args['methods'] ) &&
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1615
			 array_diff( $args['methods'], get_class_methods( $implementation ) ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1616
			continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1617
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1618
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1619
		return $implementation;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1620
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1621
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1622
	return false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1623
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1624
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1625
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1626
 * Prints default plupload arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1627
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1628
 * @since 3.4.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1629
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1630
function wp_plupload_default_settings() {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1631
	global $wp_scripts;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1632
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1633
	$data = $wp_scripts->get_data( 'wp-plupload', 'data' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1634
	if ( $data && false !== strpos( $data, '_wpPluploadSettings' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1635
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1636
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1637
	$max_upload_size = wp_max_upload_size();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1638
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1639
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1640
		'runtimes'            => 'html5,silverlight,flash,html4',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1641
		'file_data_name'      => 'async-upload', // key passed to $_FILE.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1642
		'multiple_queues'     => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1643
		'max_file_size'       => $max_upload_size . 'b',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1644
		'url'                 => admin_url( 'async-upload.php', 'relative' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1645
		'flash_swf_url'       => includes_url( 'js/plupload/plupload.flash.swf' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1646
		'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1647
		'filters'             => array( array( 'title' => __( 'Allowed Files' ), 'extensions' => '*') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1648
		'multipart'           => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1649
		'urlstream_upload'    => true,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1650
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1651
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1652
	// Multi-file uploading doesn't currently work in iOS Safari,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1653
	// single-file allows the built-in camera to be used as source for images
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1654
	if ( wp_is_mobile() )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1655
		$defaults['multi_selection'] = false;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1656
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1657
	$defaults = apply_filters( 'plupload_default_settings', $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1658
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1659
	$params = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1660
		'action' => 'upload-attachment',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1661
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1662
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1663
	$params = apply_filters( 'plupload_default_params', $params );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1664
	$params['_wpnonce'] = wp_create_nonce( 'media-form' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1665
	$defaults['multipart_params'] = $params;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1666
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1667
	$settings = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1668
		'defaults' => $defaults,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1669
		'browser'  => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1670
			'mobile'    => wp_is_mobile(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1671
			'supported' => _device_can_upload(),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1672
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1673
		'limitExceeded' => is_multisite() && ! is_upload_space_available()
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1674
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1675
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1676
	$script = 'var _wpPluploadSettings = ' . json_encode( $settings ) . ';';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1677
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1678
	if ( $data )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1679
		$script = "$data\n$script";
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1680
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1681
	$wp_scripts->add_data( 'wp-plupload', 'data', $script );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1682
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1683
add_action( 'customize_controls_enqueue_scripts', 'wp_plupload_default_settings' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1684
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1685
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1686
 * Prepares an attachment post object for JS, where it is expected
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1687
 * to be JSON-encoded and fit into an Attachment model.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1688
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1689
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1690
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1691
 * @param mixed $attachment Attachment ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1692
 * @return array Array of attachment details.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1693
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1694
function wp_prepare_attachment_for_js( $attachment ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1695
	if ( ! $attachment = get_post( $attachment ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1696
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1697
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1698
	if ( 'attachment' != $attachment->post_type )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1699
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1700
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1701
	$meta = wp_get_attachment_metadata( $attachment->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1702
	if ( false !== strpos( $attachment->post_mime_type, '/' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1703
		list( $type, $subtype ) = explode( '/', $attachment->post_mime_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1704
	else
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1705
		list( $type, $subtype ) = array( $attachment->post_mime_type, '' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1706
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1707
	$attachment_url = wp_get_attachment_url( $attachment->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1708
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1709
	$response = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1710
		'id'          => $attachment->ID,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1711
		'title'       => $attachment->post_title,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1712
		'filename'    => wp_basename( $attachment->guid ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1713
		'url'         => $attachment_url,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1714
		'link'        => get_attachment_link( $attachment->ID ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1715
		'alt'         => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1716
		'author'      => $attachment->post_author,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1717
		'description' => $attachment->post_content,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1718
		'caption'     => $attachment->post_excerpt,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1719
		'name'        => $attachment->post_name,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1720
		'status'      => $attachment->post_status,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1721
		'uploadedTo'  => $attachment->post_parent,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1722
		'date'        => strtotime( $attachment->post_date_gmt ) * 1000,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1723
		'modified'    => strtotime( $attachment->post_modified_gmt ) * 1000,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1724
		'menuOrder'   => $attachment->menu_order,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1725
		'mime'        => $attachment->post_mime_type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1726
		'type'        => $type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1727
		'subtype'     => $subtype,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1728
		'icon'        => wp_mime_type_icon( $attachment->ID ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1729
		'dateFormatted' => mysql2date( get_option('date_format'), $attachment->post_date ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1730
		'nonces'      => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1731
			'update' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1732
			'delete' => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1733
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1734
		'editLink'   => false,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1735
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1736
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1737
	if ( current_user_can( 'edit_post', $attachment->ID ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1738
		$response['nonces']['update'] = wp_create_nonce( 'update-post_' . $attachment->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1739
		$response['editLink'] = get_edit_post_link( $attachment->ID, 'raw' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1740
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1741
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1742
	if ( current_user_can( 'delete_post', $attachment->ID ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1743
		$response['nonces']['delete'] = wp_create_nonce( 'delete-post_' . $attachment->ID );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1744
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1745
	if ( $meta && 'image' === $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1746
		$sizes = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1747
		/** This filter is documented in wp-admin/includes/media.php */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1748
		$possible_sizes = apply_filters( 'image_size_names_choose', array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1749
			'thumbnail' => __('Thumbnail'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1750
			'medium'    => __('Medium'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1751
			'large'     => __('Large'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1752
			'full'      => __('Full Size'),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1753
		) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1754
		unset( $possible_sizes['full'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1755
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1756
		// Loop through all potential sizes that may be chosen. Try to do this with some efficiency.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1757
		// First: run the image_downsize filter. If it returns something, we can use its data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1758
		// If the filter does not return something, then image_downsize() is just an expensive
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1759
		// way to check the image metadata, which we do second.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1760
		foreach ( $possible_sizes as $size => $label ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1761
			if ( $downsize = apply_filters( 'image_downsize', false, $attachment->ID, $size ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1762
				if ( ! $downsize[3] )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1763
					continue;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1764
				$sizes[ $size ] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1765
					'height'      => $downsize[2],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1766
					'width'       => $downsize[1],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1767
					'url'         => $downsize[0],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1768
					'orientation' => $downsize[2] > $downsize[1] ? 'portrait' : 'landscape',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1769
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1770
			} elseif ( isset( $meta['sizes'][ $size ] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1771
				if ( ! isset( $base_url ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1772
					$base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1773
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1774
				// Nothing from the filter, so consult image metadata if we have it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1775
				$size_meta = $meta['sizes'][ $size ];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1776
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1777
				// We have the actual image size, but might need to further constrain it if content_width is narrower.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1778
				// Thumbnail, medium, and full sizes are also checked against the site's height/width options.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1779
				list( $width, $height ) = image_constrain_size_for_editor( $size_meta['width'], $size_meta['height'], $size, 'edit' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1780
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1781
				$sizes[ $size ] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1782
					'height'      => $height,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1783
					'width'       => $width,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1784
					'url'         => $base_url . $size_meta['file'],
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1785
					'orientation' => $height > $width ? 'portrait' : 'landscape',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1786
				);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1787
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1788
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1789
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1790
		$sizes['full'] = array( 'url' => $attachment_url );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1791
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1792
		if ( isset( $meta['height'], $meta['width'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1793
			$sizes['full']['height'] = $meta['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1794
			$sizes['full']['width'] = $meta['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1795
			$sizes['full']['orientation'] = $meta['height'] > $meta['width'] ? 'portrait' : 'landscape';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1796
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1797
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1798
		$response = array_merge( $response, array( 'sizes' => $sizes ), $sizes['full'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1799
	} elseif ( $meta && 'video' === $type ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1800
		if ( isset( $meta['width'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1801
			$response['width'] = (int) $meta['width'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1802
		if ( isset( $meta['height'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1803
			$response['height'] = (int) $meta['height'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1804
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1805
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1806
	if ( $meta && ( 'audio' === $type || 'video' === $type ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1807
		if ( isset( $meta['length_formatted'] ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1808
			$response['fileLength'] = $meta['length_formatted'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1809
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1810
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1811
	if ( function_exists('get_compat_media_markup') )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1812
		$response['compat'] = get_compat_media_markup( $attachment->ID, array( 'in_modal' => true ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1813
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1814
	return apply_filters( 'wp_prepare_attachment_for_js', $response, $attachment, $meta );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1815
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1816
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1817
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1818
 * Enqueues all scripts, styles, settings, and templates necessary to use
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1819
 * all media JS APIs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1820
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1821
 * @since 3.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1822
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1823
function wp_enqueue_media( $args = array() ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1824
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1825
	// Enqueue me just once per page, please.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1826
	if ( did_action( 'wp_enqueue_media' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1827
		return;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1828
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1829
	$defaults = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1830
		'post' => null,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1831
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1832
	$args = wp_parse_args( $args, $defaults );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1833
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1834
	// We're going to pass the old thickbox media tabs to `media_upload_tabs`
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1835
	// to ensure plugins will work. We will then unset those tabs.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1836
	$tabs = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1837
		// handler action suffix => tab label
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1838
		'type'     => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1839
		'type_url' => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1840
		'gallery'  => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1841
		'library'  => '',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1842
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1843
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1844
	$tabs = apply_filters( 'media_upload_tabs', $tabs );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1845
	unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1846
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1847
	$props = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1848
		'link'  => get_option( 'image_default_link_type' ), // db default is 'file'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1849
		'align' => get_option( 'image_default_align' ), // empty default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1850
		'size'  => get_option( 'image_default_size' ),  // empty default
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1851
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1852
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1853
	$settings = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1854
		'tabs'      => $tabs,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1855
		'tabUrl'    => add_query_arg( array( 'chromeless' => true ), admin_url('media-upload.php') ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1856
		'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1857
		'captions'  => ! apply_filters( 'disable_captions', '' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1858
		'nonce'     => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1859
			'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1860
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1861
		'post'    => array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1862
			'id' => 0,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1863
		),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1864
		'defaultProps' => $props,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1865
		'embedExts'    => array_merge( wp_get_audio_extensions(), wp_get_video_extensions() ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1866
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1867
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1868
	$post = null;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1869
	if ( isset( $args['post'] ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1870
		$post = get_post( $args['post'] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1871
		$settings['post'] = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1872
			'id' => $post->ID,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1873
			'nonce' => wp_create_nonce( 'update-post_' . $post->ID ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1874
		);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1875
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1876
		if ( current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1877
			$featured_image_id = get_post_meta( $post->ID, '_thumbnail_id', true );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1878
			$settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1879
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1880
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1881
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1882
	$hier = $post && is_post_type_hierarchical( $post->post_type );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1883
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1884
	$strings = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1885
		// Generic
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1886
		'url'         => __( 'URL' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1887
		'addMedia'    => __( 'Add Media' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1888
		'search'      => __( 'Search' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1889
		'select'      => __( 'Select' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1890
		'cancel'      => __( 'Cancel' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1891
		/* translators: This is a would-be plural string used in the media manager.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1892
		   If there is not a word you can use in your language to avoid issues with the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1893
		   lack of plural support here, turn it into "selected: %d" then translate it.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1894
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1895
		'selected'    => __( '%d selected' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1896
		'dragInfo'    => __( 'Drag and drop to reorder images.' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1897
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1898
		// Upload
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1899
		'uploadFilesTitle'  => __( 'Upload Files' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1900
		'uploadImagesTitle' => __( 'Upload Images' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1901
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1902
		// Library
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1903
		'mediaLibraryTitle'  => __( 'Media Library' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1904
		'insertMediaTitle'   => __( 'Insert Media' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1905
		'createNewGallery'   => __( 'Create a new gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1906
		'returnToLibrary'    => __( '&#8592; Return to library' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1907
		'allMediaItems'      => __( 'All media items' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1908
		'noItemsFound'       => __( 'No items found.' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1909
		'insertIntoPost'     => $hier ? __( 'Insert into page' ) : __( 'Insert into post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1910
		'uploadedToThisPost' => $hier ? __( 'Uploaded to this page' ) : __( 'Uploaded to this post' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1911
		'warnDelete' =>      __( "You are about to permanently delete this item.\n  'Cancel' to stop, 'OK' to delete." ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1912
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1913
		// From URL
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1914
		'insertFromUrlTitle' => __( 'Insert from URL' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1915
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1916
		// Featured Images
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1917
		'setFeaturedImageTitle' => __( 'Set Featured Image' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1918
		'setFeaturedImage'    => __( 'Set featured image' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1919
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1920
		// Gallery
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1921
		'createGalleryTitle' => __( 'Create Gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1922
		'editGalleryTitle'   => __( 'Edit Gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1923
		'cancelGalleryTitle' => __( '&#8592; Cancel Gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1924
		'insertGallery'      => __( 'Insert gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1925
		'updateGallery'      => __( 'Update gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1926
		'addToGallery'       => __( 'Add to gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1927
		'addToGalleryTitle'  => __( 'Add to Gallery' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1928
		'reverseOrder'       => __( 'Reverse order' ),
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1929
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1930
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1931
	$settings = apply_filters( 'media_view_settings', $settings, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1932
	$strings  = apply_filters( 'media_view_strings',  $strings,  $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1933
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1934
	$strings['settings'] = $settings;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1935
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1936
	wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1937
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1938
	wp_enqueue_script( 'media-editor' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1939
	wp_enqueue_style( 'media-views' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1940
	wp_plupload_default_settings();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1941
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1942
	require_once ABSPATH . WPINC . '/media-template.php';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1943
	add_action( 'admin_footer', 'wp_print_media_templates' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1944
	add_action( 'wp_footer', 'wp_print_media_templates' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1945
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1946
	do_action( 'wp_enqueue_media' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1947
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1948
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1949
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1950
 * Retrieve media attached to the passed post
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1951
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1952
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1953
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1954
 * @param string $type (Mime) type of media desired
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1955
 * @param mixed $post Post ID or object
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1956
 * @return array Found attachments
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1957
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1958
function get_attached_media( $type, $post = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1959
	if ( ! $post = get_post( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1960
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1961
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1962
	$args = array(
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1963
		'post_parent' => $post->ID,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1964
		'post_type' => 'attachment',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1965
		'post_mime_type' => $type,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1966
		'posts_per_page' => -1,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1967
		'orderby' => 'menu_order',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1968
		'order' => 'ASC',
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1969
	);
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1970
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1971
	$args = apply_filters( 'get_attached_media_args', $args, $type, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1972
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1973
	$children = get_children( $args );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1974
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1975
	return (array) apply_filters( 'get_attached_media', $children, $type, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1976
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1977
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1978
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1979
 * Check the content blob for an <audio>, <video> <object>, <embed>, or <iframe>
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1980
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1981
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1982
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1983
 * @param string $content A string which might contain media data.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1984
 * @param array $types array of media types: 'audio', 'video', 'object', 'embed', or 'iframe'
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1985
 * @return array A list of found HTML media embeds
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1986
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1987
function get_media_embedded_in_content( $content, $types = null ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1988
	$html = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1989
	$allowed_media_types = array( 'audio', 'video', 'object', 'embed', 'iframe' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1990
	if ( ! empty( $types ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1991
		if ( ! is_array( $types ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1992
			$types = array( $types );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1993
		$allowed_media_types = array_intersect( $allowed_media_types, $types );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1994
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1995
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1996
	foreach ( $allowed_media_types as $tag ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1997
		if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1998
			$html[] = $matches[0];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  1999
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2000
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2001
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2002
	return $html;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2003
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2004
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2005
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2006
 * Retrieve galleries from the passed post's content
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2007
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2008
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2009
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2010
 * @param mixed $post Optional. Post ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2011
 * @param boolean $html Whether to return HTML or data in the array
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2012
 * @return array A list of arrays, each containing gallery data and srcs parsed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2013
 *		from the expanded shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2014
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2015
function get_post_galleries( $post, $html = true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2016
	if ( ! $post = get_post( $post ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2017
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2018
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2019
	if ( ! has_shortcode( $post->post_content, 'gallery' ) )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2020
		return array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2021
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2022
	$galleries = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2023
	if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2024
		foreach ( $matches as $shortcode ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2025
			if ( 'gallery' === $shortcode[2] ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2026
				$srcs = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2027
				$count = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2028
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2029
				$gallery = do_shortcode_tag( $shortcode );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2030
				if ( $html ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2031
					$galleries[] = $gallery;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2032
				} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2033
					preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2034
					if ( ! empty( $src ) ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2035
						foreach ( $src as $s )
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2036
							$srcs[] = $s[2];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2037
					}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2038
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2039
					$data = shortcode_parse_atts( $shortcode[3] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2040
					$data['src'] = array_values( array_unique( $srcs ) );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2041
					$galleries[] = $data;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2042
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2043
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2044
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2045
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2046
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2047
	return apply_filters( 'get_post_galleries', $galleries, $post );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2048
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2049
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2050
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2051
 * Check a specified post's content for gallery and, if present, return the first
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2052
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2053
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2054
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2055
 * @param mixed $post Optional. Post ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2056
 * @param boolean $html Whether to return HTML or data
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2057
 * @return string|array Gallery data and srcs parsed from the expanded shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2058
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2059
function get_post_gallery( $post = 0, $html = true ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2060
	$galleries = get_post_galleries( $post, $html );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2061
	$gallery = reset( $galleries );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2062
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2063
	return apply_filters( 'get_post_gallery', $gallery, $post, $galleries );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2064
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2065
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2066
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2067
 * Retrieve the image srcs from galleries from a post's content, if present
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2068
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2069
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2070
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2071
 * @param mixed $post Optional. Post ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2072
 * @return array A list of lists, each containing image srcs parsed
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2073
 *		from an expanded shortcode
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2074
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2075
function get_post_galleries_images( $post = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2076
	$galleries = get_post_galleries( $post, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2077
	return wp_list_pluck( $galleries, 'src' );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2078
}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2079
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2080
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2081
 * Check a post's content for galleries and return the image srcs for the first found gallery
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2082
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2083
 * @since 3.6.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2084
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2085
 * @param mixed $post Optional. Post ID or object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2086
 * @return array A list of a gallery's image srcs in order
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2087
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2088
function get_post_gallery_images( $post = 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2089
	$gallery = get_post_gallery( $post, false );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2090
	return empty( $gallery['src'] ) ? array() : $gallery['src'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
  2091
}