WordPressを読む 65-4 /blog/wp-includes/media.php 4
2015/03/09
目次
- 1 /blog/wp-includes/media.php 4
- 2 関数 wp_get_video_extensions()
- 3 関数 wp_video_shortcode()
- 4 関数 previous_image_link()
- 5 関数 next_image_link()
- 6 関数 adjacent_image_link()
- 7 関数 get_attachment_taxonomies()
- 8 関数 get_taxonomies_for_attachments()
- 9 関数 wp_imagecreatetruecolor()
- 10 関数 wp_embed_register_handler()
- 11 関数 wp_embed_unregister_handler()
- 12 関数 wp_embed_defaults()
- 13 関数 wp_expand_dimensions()
- 14 関数 wp_oembed_get()
- 15 関数 wp_oembed_add_provider()
- 16 関数 wp_oembed_remove_provider()
/blog/wp-includes/media.php 4
関数 wp_get_video_extensions()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Return a filtered list of WP-supported video formats * * @since 3.6.0 * @return array */ function wp_get_video_extensions() { /** * Filter the list of supported video formats. * * @since 3.6.0 * * @param array $extensions An array of support video formats. Defaults are * 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv'. */ return apply_filters( 'wp_video_extensions', array( 'mp4', 'm4v', 'webm', 'ogv', 'wmv', 'flv' ) ); } |
関数 wp_video_shortcode()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | /** * The Video shortcode. * * This implements the functionality of the Video Shortcode for displaying * WordPress mp4s in a post. * * @since 3.6.0 * * @param array $attr { * Attributes of the shortcode. * * @type string $src URL to the source of the video file. Default empty. * @type int $height Height of the video embed in pixels. Default 360. * @type int $width Width of the video embed in pixels. Default $content_width or 640. * @type string $poster The 'poster' attribute for the `<video>` element. Default empty. * @type string $loop The 'loop' attribute for the `<video>` element. Default empty. * @type string $autoplay The 'autoplay' attribute for the `<video>` element. Default empty. * @type string $preload The 'preload' attribute for the `<video>` element. * Default 'metadata'. * @type string $class The 'class' attribute for the `<video>` element. * Default 'wp-video-shortcode'. * @type string $id The 'id' attribute for the `<video>` element. * Default 'video-{$post_id}-{$instances}'. * } * @param string $content Optional. Shortcode content. * @return string HTML content to display video. */ function wp_video_shortcode( $attr, $content = '' ) { global $content_width; $post_id = get_post() ? get_the_ID() : 0; static $instances = 0; $instances++; /** * Filter the default video shortcode output. * * If the filtered output isn't empty, it will be used instead of generating * the default video template. * * @since 3.6.0 * * @see wp_video_shortcode() * * @param string $html Empty variable to be replaced with shortcode markup. * @param array $attr Attributes of the video shortcode. * @param string $content Video shortcode content. * @param int $instances Unique numeric ID of this video shortcode instance. */ $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instances ); if ( '' !== $override ) { return $override; } $video = null; $default_types = wp_get_video_extensions(); $defaults_atts = array( 'src' => '', 'poster' => '', 'loop' => '', 'autoplay' => '', 'preload' => 'metadata', 'width' => 640, 'height' => 360, ); foreach ( $default_types as $type ) { $defaults_atts[$type] = ''; } $atts = shortcode_atts( $defaults_atts, $attr, 'video' ); if ( is_admin() ) { // shrink the video so it isn't huge in the admin if ( $atts['width'] > $defaults_atts['width'] ) { $atts['height'] = round( ( $atts['height'] * $defaults_atts['width'] ) / $atts['width'] ); $atts['width'] = $defaults_atts['width']; } } else { // if the video is bigger than the theme if ( ! empty( $content_width ) && $atts['width'] > $content_width ) { $atts['height'] = round( ( $atts['height'] * $content_width ) / $atts['width'] ); $atts['width'] = $content_width; } } $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#'; $primary = false; if ( ! empty( $atts['src'] ) ) { if ( ! preg_match( $yt_pattern, $atts['src'] ) ) { $type = wp_check_filetype( $atts['src'], wp_get_mime_types() ); if ( ! in_array( strtolower( $type['ext'] ), $default_types ) ) { return sprintf( '<a class="wp-embedded-video" href="%s">%s</a>', esc_url( $atts['src'] ), esc_html( $atts['src'] ) ); } } $primary = true; array_unshift( $default_types, 'src' ); } else { foreach ( $default_types as $ext ) { if ( ! empty( $atts[ $ext ] ) ) { $type = wp_check_filetype( $atts[ $ext ], wp_get_mime_types() ); if ( strtolower( $type['ext'] ) === $ext ) { $primary = true; } } } } if ( ! $primary ) { $videos = get_attached_media( 'video', $post_id ); if ( empty( $videos ) ) { return; } $video = reset( $videos ); $atts['src'] = wp_get_attachment_url( $video->ID ); if ( empty( $atts['src'] ) ) { return; } array_unshift( $default_types, 'src' ); } /** * Filter the media library used for the video shortcode. * * @since 3.6.0 * * @param string $library Media library used for the video shortcode. */ $library = apply_filters( 'wp_video_shortcode_library', 'mediaelement' ); if ( 'mediaelement' === $library && did_action( 'init' ) ) { wp_enqueue_style( 'wp-mediaelement' ); wp_enqueue_script( 'wp-mediaelement' ); } /** * Filter the class attribute for the video shortcode output container. * * @since 3.6.0 * * @param string $class CSS class or list of space-separated classes. */ $html_atts = array( 'class' => apply_filters( 'wp_video_shortcode_class', 'wp-video-shortcode' ), 'id' => sprintf( 'video-%d-%d', $post_id, $instances ), 'width' => absint( $atts['width'] ), 'height' => absint( $atts['height'] ), 'poster' => esc_url( $atts['poster'] ), 'loop' => $atts['loop'], 'autoplay' => $atts['autoplay'], 'preload' => $atts['preload'], ); // These ones should just be omitted altogether if they are blank foreach ( array( 'poster', 'loop', 'autoplay', 'preload' ) as $a ) { if ( empty( $html_atts[$a] ) ) { unset( $html_atts[$a] ); } } $attr_strings = array(); foreach ( $html_atts as $k => $v ) { $attr_strings[] = $k . '="' . esc_attr( $v ) . '"'; } $html = ''; if ( 'mediaelement' === $library && 1 === $instances ) { $html .= "<!--[if lt IE 9]><script>document.createElement('video');</script><![endif]-->\n"; } $html .= sprintf( '<video %s controls="controls">', join( ' ', $attr_strings ) ); $fileurl = ''; $source = '<source type="%s" src="%s" />'; foreach ( $default_types as $fallback ) { if ( ! empty( $atts[ $fallback ] ) ) { if ( empty( $fileurl ) ) { $fileurl = $atts[ $fallback ]; } if ( 'src' === $fallback && preg_match( $yt_pattern, $atts['src'] ) ) { $type = array( 'type' => 'video/youtube' ); } else { $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() ); } $url = add_query_arg( '_', $instances, $atts[ $fallback ] ); $html .= sprintf( $source, $type['type'], esc_url( $url ) ); } } if ( ! empty( $content ) ) { if ( false !== strpos( $content, "\n" ) ) { $content = str_replace( array( "\r\n", "\n", "\t" ), '', $content ); } $html .= trim( $content ); } if ( 'mediaelement' === $library ) { $html .= wp_mediaelement_fallback( $fileurl ); } $html .= '</video>'; $width_rule = $height_rule = ''; if ( ! empty( $atts['width'] ) ) { $width_rule = sprintf( 'width: %dpx; ', $atts['width'] ); } if ( ! empty( $atts['height'] ) ) { $height_rule = sprintf( 'height: %dpx; ', $atts['height'] ); } $output = sprintf( '<div style="%s%s" class="wp-video">%s</div>', $width_rule, $height_rule, $html ); /** * Filter the output of the video shortcode. * * @since 3.6.0 * * @param string $output Video shortcode HTML output. * @param array $atts Array of video shortcode attributes. * @param string $video Video file. * @param int $post_id Post ID. * @param string $library Media library used for the video shortcode. */ return apply_filters( 'wp_video_shortcode', $output, $atts, $video, $post_id, $library ); } add_shortcode( 'video', 'wp_video_shortcode' ); |
関数 previous_image_link()
1 2 3 4 5 6 7 8 9 10 11 | /** * Display previous image link that has the same post parent. * * @since 2.5.0 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text; * @param string $text Optional, default is false. If included, link will reflect $text variable. * @return string HTML content. */ function previous_image_link($size = 'thumbnail', $text = false) { adjacent_image_link(true, $size, $text); } |
関数 next_image_link()
1 2 3 4 5 6 7 8 9 10 11 | /** * Display next image link that has the same post parent. * * @since 2.5.0 * @param string $size Optional, default is 'thumbnail'. Size of image, either array or string. 0 or 'none' will default to post_title or $text; * @param string $text Optional, default is false. If included, link will reflect $text variable. * @return string HTML content. */ function next_image_link($size = 'thumbnail', $text = false) { adjacent_image_link(false, $size, $text); } |
関数 adjacent_image_link()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | /** * Display next or previous image link that has the same post parent. * * Retrieves the current attachment object from the $post global. * * @since 2.5.0 * * @param bool $prev Optional. Default is true to display previous link, false for next. */ function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) { $post = get_post(); $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' ) ) ); foreach ( $attachments as $k => $attachment ) { if ( $attachment->ID == $post->ID ) { break; } } $output = ''; $attachment_id = 0; if ( $attachments ) { $k = $prev ? $k - 1 : $k + 1; if ( isset( $attachments[ $k ] ) ) { $attachment_id = $attachments[ $k ]->ID; $output = wp_get_attachment_link( $attachment_id, $size, true, false, $text ); } } $adjacent = $prev ? 'previous' : 'next'; /** * Filter the adjacent image link. * * The dynamic portion of the hook name, $adjacent, refers to the type of adjacency, * either 'next', or 'previous'. * * @since 3.5.0 * * @param string $output Adjacent image HTML markup. * @param int $attachment_id Attachment ID * @param string $size Image size. * @param string $text Link text. */ echo apply_filters( "{$adjacent}_image_link", $output, $attachment_id, $size, $text ); } |
関数 get_attachment_taxonomies()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * Retrieve taxonomies attached to the attachment. * * @since 2.5.0 * * @param int|array|object $attachment Attachment ID, Attachment data array, or Attachment data object. * @return array Empty array on failure. List of taxonomies on success. */ function get_attachment_taxonomies($attachment) { if ( is_int( $attachment ) ) $attachment = get_post($attachment); else if ( is_array($attachment) ) $attachment = (object) $attachment; if ( ! is_object($attachment) ) return array(); $filename = basename($attachment->guid); $objects = array('attachment'); if ( false !== strpos($filename, '.') ) $objects[] = 'attachment:' . substr($filename, strrpos($filename, '.') + 1); if ( !empty($attachment->post_mime_type) ) { $objects[] = 'attachment:' . $attachment->post_mime_type; if ( false !== strpos($attachment->post_mime_type, '/') ) foreach ( explode('/', $attachment->post_mime_type) as $token ) if ( !empty($token) ) $objects[] = "attachment:$token"; } $taxonomies = array(); foreach ( $objects as $object ) if ( $taxes = get_object_taxonomies($object) ) $taxonomies = array_merge($taxonomies, $taxes); return array_unique($taxonomies); } |
関数 get_taxonomies_for_attachments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | /** * Return all of the taxonomy names that are registered for attachments. * * Handles mime-type-specific taxonomies such as attachment:image and attachment:video. * * @since 3.5.0 * @see get_attachment_taxonomies() * @uses get_taxonomies() * * @param string $output The type of output to return, either taxonomy 'names' or 'objects'. 'names' is the default. * @return array The names of all taxonomy of $object_type. */ function get_taxonomies_for_attachments( $output = 'names' ) { $taxonomies = array(); foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy ) { foreach ( $taxonomy->object_type as $object_type ) { if ( 'attachment' == $object_type || 0 === strpos( $object_type, 'attachment:' ) ) { if ( 'names' == $output ) $taxonomies[] = $taxonomy->name; else $taxonomies[ $taxonomy->name ] = $taxonomy; break; } } } return $taxonomies; } |
関数 wp_imagecreatetruecolor()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Create new GD image resource with transparency support * @TODO: Deprecate if possible. * * @since 2.9.0 * * @param int $width Image width * @param int $height Image height * @return image resource */ function wp_imagecreatetruecolor($width, $height) { $img = imagecreatetruecolor($width, $height); if ( is_resource($img) && function_exists('imagealphablending') && function_exists('imagesavealpha') ) { imagealphablending($img, false); imagesavealpha($img, true); } return $img; } |
関数 wp_embed_register_handler()
1 2 3 4 5 6 7 8 9 10 | /** * Register an embed handler. This function should probably only be used for sites that do not support oEmbed. * * @since 2.9.0 * @see WP_Embed::register_handler() */ function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) { global $wp_embed; $wp_embed->register_handler( $id, $regex, $callback, $priority ); } |
関数 wp_embed_unregister_handler()
1 2 3 4 5 6 7 8 9 10 | /** * Unregister a previously registered embed handler. * * @since 2.9.0 * @see WP_Embed::unregister_handler() */ function wp_embed_unregister_handler( $id, $priority = 10 ) { global $wp_embed; $wp_embed->unregister_handler( $id, $priority ); } |
関数 wp_embed_defaults()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * Create default array of embed parameters. * * The width defaults to the content width as specified by the theme. If the * theme does not specify a content width, then 500px is used. * * The default height is 1.5 times the width, or 1000px, whichever is smaller. * * The 'embed_defaults' filter can be used to adjust either of these values. * * @since 2.9.0 * * @param string $url Optional. The URL that should be embedded. Default empty. * * @return array Default embed parameters. */ function wp_embed_defaults( $url = '' ) { if ( ! empty( $GLOBALS['content_width'] ) ) $width = (int) $GLOBALS['content_width']; if ( empty( $width ) ) $width = 500; $height = min( ceil( $width * 1.5 ), 1000 ); /** * Filter the default array of embed dimensions. * * @since 2.9.0 * * @param int $width Width of the embed in pixels. * @param int $height Height of the embed in pixels. * @param string $url The URL that should be embedded. */ return apply_filters( 'embed_defaults', compact( 'width', 'height' ), $url ); } |
関数 wp_expand_dimensions()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height. * * @since 2.9.0 * @uses wp_constrain_dimensions() This function passes the widths and the heights. * * @param int $example_width The width of an example embed. * @param int $example_height The height of an example embed. * @param int $max_width The maximum allowed width. * @param int $max_height The maximum allowed height. * @return array The maximum possible width and height based on the example ratio. */ function wp_expand_dimensions( $example_width, $example_height, $max_width, $max_height ) { $example_width = (int) $example_width; $example_height = (int) $example_height; $max_width = (int) $max_width; $max_height = (int) $max_height; return wp_constrain_dimensions( $example_width * 1000000, $example_height * 1000000, $max_width, $max_height ); } |
関数 wp_oembed_get()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Attempts to fetch the embed HTML for a provided URL using oEmbed. * * @since 2.9.0 * @see WP_oEmbed * * @uses _wp_oembed_get_object() * @uses WP_oEmbed::get_html() * * @param string $url The URL that should be embedded. * @param array $args Additional arguments and parameters. * @return bool|string False on failure or the embed HTML on success. */ function wp_oembed_get( $url, $args = '' ) { require_once( ABSPATH . WPINC . '/class-oembed.php' ); $oembed = _wp_oembed_get_object(); return $oembed->get_html( $url, $args ); } |
関数 wp_oembed_add_provider()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Adds a URL format and oEmbed provider URL pair. * * @since 2.9.0 * @see WP_oEmbed * * @uses _wp_oembed_get_object() * * @param string $format The format of URL that this provider can handle. You can use asterisks as wildcards. * @param string $provider The URL to the oEmbed provider. * @param boolean $regex Whether the $format parameter is in a regex format. */ function wp_oembed_add_provider( $format, $provider, $regex = false ) { require_once( ABSPATH . WPINC . '/class-oembed.php' ); if ( did_action( 'plugins_loaded' ) ) { $oembed = _wp_oembed_get_object(); $oembed->providers[$format] = array( $provider, $regex ); } else { WP_oEmbed::_add_provider_early( $format, $provider, $regex ); } } |
関数 wp_oembed_remove_provider()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * Removes an oEmbed provider. * * @since 3.5.0 * @see WP_oEmbed * * @uses _wp_oembed_get_object() * * @param string $format The URL format for the oEmbed provider to remove. */ function wp_oembed_remove_provider( $format ) { require_once( ABSPATH . WPINC . '/class-oembed.php' ); if ( did_action( 'plugins_loaded' ) ) { $oembed = _wp_oembed_get_object(); if ( isset( $oembed->providers[ $format ] ) ) { unset( $oembed->providers[ $format ] ); return true; } } else { WP_oEmbed::_remove_provider_early( $format ); } return false; } |