+ $atts = shortcode_atts( $defaults_atts, $attr, 'audio' );
+
+ $primary = false;
+ if ( ! empty( $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-audio" 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 ) {
+ $audios = get_attached_media( 'audio', $post_id );
+ if ( empty( $audios ) ) {
+ return;
+ }
+
+ $audio = reset( $audios );
+ $atts['src'] = wp_get_attachment_url( $audio->ID );
+ if ( empty( $atts['src'] ) ) {
+ return;
+ }
+
+ array_unshift( $default_types, 'src' );
+ }
+
+ /**
+ * Filter the media library used for the audio shortcode.
+ *
+ * @since 3.6.0
+ *
+ * @param string $library Media library used for the audio shortcode.
+ */
+ $library = apply_filters( 'wp_audio_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 audio 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_audio_shortcode_class', 'wp-audio-shortcode' ),
+ 'id' => sprintf( 'audio-%d-%d', $post_id, $instance ),
+ 'loop' => wp_validate_boolean( $atts['loop'] ),
+ 'autoplay' => wp_validate_boolean( $atts['autoplay'] ),
+ 'preload' => $atts['preload'],
+ 'style' => 'width: 100%; visibility: hidden;',
+ );
+
+ // These ones should just be omitted altogether if they are blank
+ foreach ( array( '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 === $instance ) {
+ $html .= "<!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->\n";
+ }
+ $html .= sprintf( '<audio %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 ];
+ }
+ $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
+ $url = add_query_arg( '_', $instance, $atts[ $fallback ] );
+ $html .= sprintf( $source, $type['type'], esc_url( $url ) );
+ }
+ }
+
+ if ( 'mediaelement' === $library ) {
+ $html .= wp_mediaelement_fallback( $fileurl );
+ }
+ $html .= '</audio>';
+
+ /**
+ * Filter the audio shortcode output.
+ *
+ * @since 3.6.0
+ *
+ * @param string $html Audio shortcode HTML output.
+ * @param array $atts Array of audio shortcode attributes.
+ * @param string $audio Audio file.
+ * @param int $post_id Post ID.
+ * @param string $library Media library used for the audio shortcode.
+ */
+ return apply_filters( 'wp_audio_shortcode', $html, $atts, $audio, $post_id, $library );
+}
+add_shortcode( 'audio', 'wp_audio_shortcode' );
+
+/**
+ * Returns a filtered list of WP-supported video formats.
+ *
+ * @since 3.6.0
+ *
+ * @return array List of supported video formats.
+ */
+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' ) );
+}
+
+/**
+ * Builds the Video shortcode output.
+ *
+ * This implements the functionality of the Video Shortcode for displaying
+ * WordPress mp4s in a post.
+ *
+ * @since 3.6.0
+ *
+ * @global int $content_width
+ * @staticvar int $instance
+ *
+ * @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'.
+ * }
+ * @param string $content Shortcode content.
+ * @return string|void HTML content to display video.
+ */
+function wp_video_shortcode( $attr, $content = '' ) {
+ global $content_width;
+ $post_id = get_post() ? get_the_ID() : 0;
+
+ static $instance = 0;
+ $instance++;
+
+ /**
+ * 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 $instance Unique numeric ID of this video shortcode instance.
+ */
+ $override = apply_filters( 'wp_video_shortcode_override', '', $attr, $content, $instance );
+ 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;
+ }
+ }
+
+ $is_vimeo = $is_youtube = false;
+ $yt_pattern = '#^https?://(?:www\.)?(?:youtube\.com/watch|youtu\.be/)#';
+ $vimeo_pattern = '#^https?://(.+\.)?vimeo\.com/.*#';
+
+ $primary = false;
+ if ( ! empty( $atts['src'] ) ) {
+ $is_vimeo = ( preg_match( $vimeo_pattern, $atts['src'] ) );
+ $is_youtube = ( preg_match( $yt_pattern, $atts['src'] ) );
+ if ( ! $is_youtube && ! $is_vimeo ) {
+ $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'] ) );
+ }
+ }
+
+ if ( $is_vimeo ) {
+ wp_enqueue_script( 'froogaloop' );
+ }
+
+ $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, $instance ),
+ 'width' => absint( $atts['width'] ),
+ 'height' => absint( $atts['height'] ),
+ 'poster' => esc_url( $atts['poster'] ),
+ 'loop' => wp_validate_boolean( $atts['loop'] ),
+ 'autoplay' => wp_validate_boolean( $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 === $instance ) {
+ $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 && $is_youtube ) {
+ $type = array( 'type' => 'video/youtube' );
+ } elseif ( 'src' === $fallback && $is_vimeo ) {
+ $type = array( 'type' => 'video/vimeo' );
+ } else {
+ $type = wp_check_filetype( $atts[ $fallback ], wp_get_mime_types() );
+ }
+ $url = add_query_arg( '_', $instance, $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 = '';
+ if ( ! empty( $atts['width'] ) ) {
+ $width_rule = sprintf( 'width: %dpx; ', $atts['width'] );
+ }
+ $output = sprintf( '<div style="%s" class="wp-video">%s</div>', $width_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' );
+
+/**
+ * Displays previous image link that has the same post parent.
+ *
+ * @since 2.5.0
+ *
+ * @see adjacent_image_link()
+ *
+ * @param string|array $size Optional. Registered image size or flat array of height and width dimensions.
+ * 0 or 'none' will default to 'post_title' or `$text`. Default 'thumbnail'.
+ * @param string $text Optional. Link text. Default false.
+ */
+function previous_image_link( $size = 'thumbnail', $text = false ) {
+ adjacent_image_link(true, $size, $text);
+}
+
+/**
+ * Displays next image link that has the same post parent.
+ *
+ * @since 2.5.0
+ *
+ * @see adjacent_image_link()
+ *
+ * @param string|array $size Optional. Registered image size or flat array of height and width dimensions.
+ * 0 or 'none' will default to 'post_title' or `$text`. Default 'thumbnail'.
+ * @param string $text Optional. Link text. Default false.
+ */
+function next_image_link($size = 'thumbnail', $text = false) {
+ adjacent_image_link(false, $size, $text);
+}
+
+/**
+ * Displays 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. Whether to display the next (false) or previous (true) link. Default true.
+ * @param string|array $size Optional. Registered image size or flat array of height and width dimensions.
+ * Default 'thumbnail'.
+ * @param bool $text Optional. Link text. Default false.
+ */
+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 );
+}
+
+/**
+ * Retrieves taxonomies attached to given the attachment.
+ *
+ * @since 2.5.0
+ *
+ * @param int|array|object $attachment Attachment ID, data array, or 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 );
+ } elseif ( 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);
+}
+
+/**
+ * Retrieves 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_taxonomies()
+ *
+ * @param string $output Optional. The type of taxonomy output to return. Accepts 'names' or 'objects'.
+ * Default 'names'.
+ * @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;
+}
+
+/**
+ * Create new GD image resource with transparency support
+ *
+ * @todo: Deprecate if possible.
+ *
+ * @since 2.9.0
+ *
+ * @param int $width Image width in pixels.
+ * @param int $height Image height in pixels..
+ * @return resource The GD 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;
+}
+
+/**
+ * Registers an embed handler.
+ *
+ * Should probably only be used for sites that do not support oEmbed.
+ *
+ * @since 2.9.0
+ *
+ * @global WP_Embed $wp_embed
+ *
+ * @param string $id An internal ID/name for the handler. Needs to be unique.
+ * @param string $regex The regex that will be used to see if this handler should be used for a URL.
+ * @param callback $callback The callback function that will be called if the regex is matched.
+ * @param int $priority Optional. Used to specify the order in which the registered handlers will
+ * be tested. Default 10.
+ */
+function wp_embed_register_handler( $id, $regex, $callback, $priority = 10 ) {
+ global $wp_embed;
+ $wp_embed->register_handler( $id, $regex, $callback, $priority );
+}
+
+/**
+ * Unregisters a previously-registered embed handler.
+ *
+ * @since 2.9.0
+ *
+ * @global WP_Embed $wp_embed
+ *
+ * @param string $id The handler ID that should be removed.
+ * @param int $priority Optional. The priority of the handler to be removed. Default 10.
+ */
+function wp_embed_unregister_handler( $id, $priority = 10 ) {
+ global $wp_embed;
+ $wp_embed->unregister_handler( $id, $priority );
+}
+
+/**
+ * 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
+ *
+ * @global int $content_width
+ *
+ * @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 );
+}
+
+/**
+ * Based on a supplied width/height example, return the biggest possible dimensions based on the max width/height.
+ *
+ * @since 2.9.0
+ *
+ * @see wp_constrain_dimensions()
+ *
+ * @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 );
+}