WordPressを読む 65-6 /blog/wp-includes/media.php 6
2015/03/12
目次
- 1 /blog/wp-includes/media.php 6
- 2 関数 wp_enqueue_media()
- 3 関数 get_attached_media()
- 4 関数 get_media_embedded_in_content()
- 5 関数 get_post_galleries()
- 6 関数 get_post_gallery()
- 7 関数 get_post_galleries_images()
- 8 関数 get_post_gallery_images()
- 9 関数 wp_maybe_generate_attachment_metadata()
- 10 関数 attachment_url_to_postid()
- 11 関数 wpview_media_sandbox_styles()
/blog/wp-includes/media.php 6
関数 wp_enqueue_media()
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | /** * Enqueues all scripts, styles, settings, and templates necessary to use * all media JS APIs. * * @since 3.5.0 */ function wp_enqueue_media( $args = array() ) { // Enqueue me just once per page, please. if ( did_action( 'wp_enqueue_media' ) ) return; global $content_width, $wpdb, $wp_locale; $defaults = array( 'post' => null, ); $args = wp_parse_args( $args, $defaults ); // We're going to pass the old thickbox media tabs to `media_upload_tabs` // to ensure plugins will work. We will then unset those tabs. $tabs = array( // handler action suffix => tab label 'type' => '', 'type_url' => '', 'gallery' => '', 'library' => '', ); /** This filter is documented in wp-admin/includes/media.php */ $tabs = apply_filters( 'media_upload_tabs', $tabs ); unset( $tabs['type'], $tabs['type_url'], $tabs['gallery'], $tabs['library'] ); $props = array( 'link' => get_option( 'image_default_link_type' ), // db default is 'file' 'align' => get_option( 'image_default_align' ), // empty default 'size' => get_option( 'image_default_size' ), // empty default ); $exts = array_merge( wp_get_audio_extensions(), wp_get_video_extensions() ); $mimes = get_allowed_mime_types(); $ext_mimes = array(); foreach ( $exts as $ext ) { foreach ( $mimes as $ext_preg => $mime_match ) { if ( preg_match( '#' . $ext . '#i', $ext_preg ) ) { $ext_mimes[ $ext ] = $mime_match; break; } } } $has_audio = $wpdb->get_var( " SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'audio%' LIMIT 1 " ); $has_video = $wpdb->get_var( " SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND post_mime_type LIKE 'video%' LIMIT 1 " ); $months = $wpdb->get_results( $wpdb->prepare( " SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM $wpdb->posts WHERE post_type = %s ORDER BY post_date DESC ", 'attachment' ) ); foreach ( $months as $month_year ) { $month_year->text = sprintf( __( '%1$s %2$d' ), $wp_locale->get_month( $month_year->month ), $month_year->year ); } $settings = array( 'tabs' => $tabs, 'tabUrl' => add_query_arg( array( 'chromeless' => true ), admin_url('media-upload.php') ), 'mimeTypes' => wp_list_pluck( get_post_mime_types(), 0 ), /** This filter is documented in wp-admin/includes/media.php */ 'captions' => ! apply_filters( 'disable_captions', '' ), 'nonce' => array( 'sendToEditor' => wp_create_nonce( 'media-send-to-editor' ), ), 'post' => array( 'id' => 0, ), 'defaultProps' => $props, 'attachmentCounts' => array( 'audio' => ( $has_audio ) ? 1 : 0, 'video' => ( $has_video ) ? 1 : 0 ), 'embedExts' => $exts, 'embedMimes' => $ext_mimes, 'contentWidth' => $content_width, 'months' => $months, 'mediaTrash' => MEDIA_TRASH ? 1 : 0 ); $post = null; if ( isset( $args['post'] ) ) { $post = get_post( $args['post'] ); $settings['post'] = array( 'id' => $post->ID, 'nonce' => wp_create_nonce( 'update-post_' . $post->ID ), ); $thumbnail_support = current_theme_supports( 'post-thumbnails', $post->post_type ) && post_type_supports( $post->post_type, 'thumbnail' ); if ( ! $thumbnail_support && 'attachment' === $post->post_type && $post->post_mime_type ) { if ( 0 === strpos( $post->post_mime_type, 'audio/' ) ) { $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); } elseif ( 0 === strpos( $post->post_mime_type, 'video/' ) ) { $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); } } if ( $thumbnail_support ) { $featured_image_id = get_post_meta( $post->ID, '_thumbnail_id', true ); $settings['post']['featuredImageId'] = $featured_image_id ? $featured_image_id : -1; } } $hier = $post && is_post_type_hierarchical( $post->post_type ); $strings = array( // Generic 'url' => __( 'URL' ), 'addMedia' => __( 'Add Media' ), 'search' => __( 'Search' ), 'select' => __( 'Select' ), 'cancel' => __( 'Cancel' ), 'update' => __( 'Update' ), 'replace' => __( 'Replace' ), 'remove' => __( 'Remove' ), 'back' => __( 'Back' ), /* translators: This is a would-be plural string used in the media manager. If there is not a word you can use in your language to avoid issues with the lack of plural support here, turn it into "selected: %d" then translate it. */ 'selected' => __( '%d selected' ), 'dragInfo' => __( 'Drag and drop to reorder images.' ), // Upload 'uploadFilesTitle' => __( 'Upload Files' ), 'uploadImagesTitle' => __( 'Upload Images' ), // Library 'mediaLibraryTitle' => __( 'Media Library' ), 'insertMediaTitle' => __( 'Insert Media' ), 'createNewGallery' => __( 'Create a new gallery' ), 'createNewPlaylist' => __( 'Create a new playlist' ), 'createNewVideoPlaylist' => __( 'Create a new video playlist' ), 'returnToLibrary' => __( '← Return to library' ), 'allMediaItems' => __( 'All media items' ), 'allMediaTypes' => __( 'All media types' ), 'allDates' => __( 'All dates' ), 'noItemsFound' => __( 'No items found.' ), 'insertIntoPost' => $hier ? __( 'Insert into page' ) : __( 'Insert into post' ), 'unattached' => __( 'Unattached' ), 'trash' => __( 'Trash' ), 'uploadedToThisPost' => $hier ? __( 'Uploaded to this page' ) : __( 'Uploaded to this post' ), 'warnDelete' => __( "You are about to permanently delete this item.\n 'Cancel' to stop, 'OK' to delete." ), 'warnBulkDelete' => __( "You are about to permanently delete these items.\n 'Cancel' to stop, 'OK' to delete." ), 'warnBulkTrash' => __( "You are about to trash these items.\n 'Cancel' to stop, 'OK' to delete." ), 'bulkSelect' => __( 'Bulk Select' ), 'cancelSelection' => __( 'Cancel Selection' ), 'trashSelected' => __( 'Trash Selected' ), 'untrashSelected' => __( 'Untrash Selected' ), 'deleteSelected' => __( 'Delete Selected' ), 'deletePermanently' => __( 'Delete Permanently' ), 'apply' => __( 'Apply' ), 'filterByDate' => __( 'Filter by date' ), 'filterByType' => __( 'Filter by type' ), 'searchMediaLabel' => __( 'Search Media' ), // Library Details 'attachmentDetails' => __( 'Attachment Details' ), // From URL 'insertFromUrlTitle' => __( 'Insert from URL' ), // Featured Images 'setFeaturedImageTitle' => __( 'Set Featured Image' ), 'setFeaturedImage' => __( 'Set featured image' ), // Gallery 'createGalleryTitle' => __( 'Create Gallery' ), 'editGalleryTitle' => __( 'Edit Gallery' ), 'cancelGalleryTitle' => __( '← Cancel Gallery' ), 'insertGallery' => __( 'Insert gallery' ), 'updateGallery' => __( 'Update gallery' ), 'addToGallery' => __( 'Add to gallery' ), 'addToGalleryTitle' => __( 'Add to Gallery' ), 'reverseOrder' => __( 'Reverse order' ), // Edit Image 'imageDetailsTitle' => __( 'Image Details' ), 'imageReplaceTitle' => __( 'Replace Image' ), 'imageDetailsCancel' => __( 'Cancel Edit' ), 'editImage' => __( 'Edit Image' ), // Crop Image 'chooseImage' => __( 'Choose Image' ), 'selectAndCrop' => __( 'Select and Crop' ), 'skipCropping' => __( 'Skip Cropping' ), 'cropImage' => __( 'Crop Image' ), 'cropYourImage' => __( 'Crop your image' ), 'cropping' => __( 'Cropping…' ), 'suggestedDimensions' => __( 'Suggested image dimensions:' ), 'cropError' => __( 'There has been an error cropping your image.' ), // Edit Audio 'audioDetailsTitle' => __( 'Audio Details' ), 'audioReplaceTitle' => __( 'Replace Audio' ), 'audioAddSourceTitle' => __( 'Add Audio Source' ), 'audioDetailsCancel' => __( 'Cancel Edit' ), // Edit Video 'videoDetailsTitle' => __( 'Video Details' ), 'videoReplaceTitle' => __( 'Replace Video' ), 'videoAddSourceTitle' => __( 'Add Video Source' ), 'videoDetailsCancel' => __( 'Cancel Edit' ), 'videoSelectPosterImageTitle' => __( 'Select Poster Image' ), 'videoAddTrackTitle' => __( 'Add Subtitles' ), // Playlist 'playlistDragInfo' => __( 'Drag and drop to reorder tracks.' ), 'createPlaylistTitle' => __( 'Create Audio Playlist' ), 'editPlaylistTitle' => __( 'Edit Audio Playlist' ), 'cancelPlaylistTitle' => __( '← Cancel Audio Playlist' ), 'insertPlaylist' => __( 'Insert audio playlist' ), 'updatePlaylist' => __( 'Update audio playlist' ), 'addToPlaylist' => __( 'Add to audio playlist' ), 'addToPlaylistTitle' => __( 'Add to Audio Playlist' ), // Video Playlist 'videoPlaylistDragInfo' => __( 'Drag and drop to reorder videos.' ), 'createVideoPlaylistTitle' => __( 'Create Video Playlist' ), 'editVideoPlaylistTitle' => __( 'Edit Video Playlist' ), 'cancelVideoPlaylistTitle' => __( '← Cancel Video Playlist' ), 'insertVideoPlaylist' => __( 'Insert video playlist' ), 'updateVideoPlaylist' => __( 'Update video playlist' ), 'addToVideoPlaylist' => __( 'Add to video playlist' ), 'addToVideoPlaylistTitle' => __( 'Add to Video Playlist' ), // Media Library 'editMetadata' => __( 'Edit Metadata' ), 'noMedia' => __( 'No media attachments found.' ), ); /** * Filter the media view settings. * * @since 3.5.0 * * @param array $settings List of media view settings. * @param WP_Post $post Post object. */ $settings = apply_filters( 'media_view_settings', $settings, $post ); /** * Filter the media view strings. * * @since 3.5.0 * * @param array $strings List of media view strings. * @param WP_Post $post Post object. */ $strings = apply_filters( 'media_view_strings', $strings, $post ); $strings['settings'] = $settings; // Ensure we enqueue media-editor first, that way media-views is // registered internally before we try to localize it. see #24724. wp_enqueue_script( 'media-editor' ); wp_localize_script( 'media-views', '_wpMediaViewsL10n', $strings ); wp_enqueue_script( 'media-audiovideo' ); wp_enqueue_style( 'media-views' ); if ( is_admin() ) { wp_enqueue_script( 'mce-view' ); wp_enqueue_script( 'image-edit' ); } wp_enqueue_style( 'imgareaselect' ); wp_plupload_default_settings(); require_once ABSPATH . WPINC . '/media-template.php'; add_action( 'admin_footer', 'wp_print_media_templates' ); add_action( 'wp_footer', 'wp_print_media_templates' ); add_action( 'customize_controls_print_footer_scripts', 'wp_print_media_templates' ); /** * Fires at the conclusion of wp_enqueue_media(). * * @since 3.5.0 */ do_action( 'wp_enqueue_media' ); } |
関数 get_attached_media()
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 | /** * Retrieve media attached to the passed post. * * @since 3.6.0 * * @param string $type Mime type. * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @return array Found attachments. */ function get_attached_media( $type, $post = 0 ) { if ( ! $post = get_post( $post ) ) return array(); $args = array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => $type, 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC', ); /** * Filter arguments used to retrieve media attached to the given post. * * @since 3.6.0 * * @param array $args Post query arguments. * @param string $type Mime type of the desired media. * @param mixed $post Post ID or object. */ $args = apply_filters( 'get_attached_media_args', $args, $type, $post ); $children = get_children( $args ); /** * Filter the list of media attached to the given post. * * @since 3.6.0 * * @param array $children Associative array of media attached to the given post. * @param string $type Mime type of the media desired. * @param mixed $post Post ID or object. */ return (array) apply_filters( 'get_attached_media', $children, $type, $post ); } |
関数 get_media_embedded_in_content()
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 | /** * Check the content blob for an <audio>, <video> <object>, <embed>, or <iframe> * * @since 3.6.0 * * @param string $content A string which might contain media data. * @param array $types array of media types: 'audio', 'video', 'object', 'embed', or 'iframe' * @return array A list of found HTML media embeds */ function get_media_embedded_in_content( $content, $types = null ) { $html = array(); $allowed_media_types = array( 'audio', 'video', 'object', 'embed', 'iframe' ); if ( ! empty( $types ) ) { if ( ! is_array( $types ) ) $types = array( $types ); $allowed_media_types = array_intersect( $allowed_media_types, $types ); } foreach ( $allowed_media_types as $tag ) { if ( preg_match( '#' . get_tag_regex( $tag ) . '#', $content, $matches ) ) { $html[] = $matches[0]; } } return $html; } |
関数 get_post_galleries()
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 | /** * Retrieve galleries from the passed post's content. * * @since 3.6.0 * * @param int|WP_Post $post Optional. Post ID or object. * @param bool $html Whether to return HTML or data in the array. * @return array A list of arrays, each containing gallery data and srcs parsed * from the expanded shortcode. */ function get_post_galleries( $post, $html = true ) { if ( ! $post = get_post( $post ) ) return array(); if ( ! has_shortcode( $post->post_content, 'gallery' ) ) return array(); $galleries = array(); if ( preg_match_all( '/' . get_shortcode_regex() . '/s', $post->post_content, $matches, PREG_SET_ORDER ) ) { foreach ( $matches as $shortcode ) { if ( 'gallery' === $shortcode[2] ) { $srcs = array(); $gallery = do_shortcode_tag( $shortcode ); if ( $html ) { $galleries[] = $gallery; } else { preg_match_all( '#src=([\'"])(.+?)\1#is', $gallery, $src, PREG_SET_ORDER ); if ( ! empty( $src ) ) { foreach ( $src as $s ) $srcs[] = $s[2]; } $data = shortcode_parse_atts( $shortcode[3] ); $data['src'] = array_values( array_unique( $srcs ) ); $galleries[] = $data; } } } } /** * Filter the list of all found galleries in the given post. * * @since 3.6.0 * * @param array $galleries Associative array of all found post galleries. * @param WP_Post $post Post object. */ return apply_filters( 'get_post_galleries', $galleries, $post ); } |
関数 get_post_gallery()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Check a specified post's content for gallery and, if present, return the first * * @since 3.6.0 * * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @param bool $html Whether to return HTML or data. * @return string|array Gallery data and srcs parsed from the expanded shortcode. */ function get_post_gallery( $post = 0, $html = true ) { $galleries = get_post_galleries( $post, $html ); $gallery = reset( $galleries ); /** * Filter the first-found post gallery. * * @since 3.6.0 * * @param array $gallery The first-found post gallery. * @param int|WP_Post $post Post ID or object. * @param array $galleries Associative array of all found post galleries. */ return apply_filters( 'get_post_gallery', $gallery, $post, $galleries ); } |
関数 get_post_galleries_images()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Retrieve the image srcs from galleries from a post's content, if present * * @since 3.6.0 * * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @return array A list of lists, each containing image srcs parsed. * from an expanded shortcode */ function get_post_galleries_images( $post = 0 ) { $galleries = get_post_galleries( $post, false ); return wp_list_pluck( $galleries, 'src' ); } |
関数 get_post_gallery_images()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Check a post's content for galleries and return the image srcs for the first found gallery * * @since 3.6.0 * * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @return array A list of a gallery's image srcs in order. */ function get_post_gallery_images( $post = 0 ) { $gallery = get_post_gallery( $post, false ); return empty( $gallery['src'] ) ? array() : $gallery['src']; } |
関数 wp_maybe_generate_attachment_metadata()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** * Maybe attempt to generate attachment metadata, if missing. * * @since 3.9.0 * * @param WP_Post $attachment Attachment object. */ function wp_maybe_generate_attachment_metadata( $attachment ) { if ( empty( $attachment ) || ( empty( $attachment->ID ) || ! $attachment_id = (int) $attachment->ID ) ) { return; } $file = get_attached_file( $attachment_id ); $meta = wp_get_attachment_metadata( $attachment_id ); if ( empty( $meta ) && file_exists( $file ) ) { $_meta = get_post_meta( $attachment_id ); $regeneration_lock = 'wp_generating_att_' . $attachment_id; if ( ! array_key_exists( '_wp_attachment_metadata', $_meta ) && ! get_transient( $regeneration_lock ) ) { set_transient( $regeneration_lock, $file ); wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); delete_transient( $regeneration_lock ); } } } |
関数 attachment_url_to_postid()
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 | /** * Try to convert an attachment URL into a post ID. * * @since 4.0.0 * * @global wpdb $wpdb WordPress database access abstraction object. * * @param string $url The URL to resolve. * @return int The found post ID. */ function attachment_url_to_postid( $url ) { global $wpdb; $dir = wp_upload_dir(); $path = ltrim( $url, $dir['baseurl'] . '/' ); $sql = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attached_file' AND meta_value = %s", $path ); $post_id = $wpdb->get_var( $sql ); if ( ! empty( $post_id ) ) { return (int) $post_id; } } |
関数 wpview_media_sandbox_styles()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Return the URls for CSS files used in an <iframe>-sandbox'd TinyMCE media view * * @since 4.0.0 * * @global $wp_version * * @return array The relevant CSS file URLs. */ function wpview_media_sandbox_styles() { $version = 'ver=' . $GLOBALS['wp_version']; $mediaelement = includes_url( "js/mediaelement/mediaelementplayer.min.css?$version" ); $wpmediaelement = includes_url( "js/mediaelement/wp-mediaelement.css?$version" ); return array( $mediaelement, $wpmediaelement ); } |