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