WordPressを読む 38-11 /blog/wp-includes/post.php 11
2014/12/25
目次
- 1 /blog/wp-includes/post.php 11
- 2 関数 is_local_attachment()
- 3 関数 wp_insert_attachment()
- 4 関数 wp_delete_attachment()
- 5 関数 wp_get_attachment_metadata()
- 6 関数 wp_update_attachment_metadata()
- 7 関数 wp_get_attachment_url()
- 8 関数 wp_get_attachment_thumb_file()
- 9 関数 wp_get_attachment_thumb_url()
- 10 関数 wp_attachment_is_image()
- 11 関数 wp_mime_type_icon()
/blog/wp-includes/post.php 11
関数 is_local_attachment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // // Attachment functions // /** * Check if the attachment URI is local one and is really an attachment. * * @since 2.0.0 * * @param string $url URL to check * @return bool True on success, false on failure. */ function is_local_attachment($url) { if (strpos($url, home_url()) === false) return false; if (strpos($url, home_url('/?attachment_id=')) !== false) return true; if ( $id = url_to_postid($url) ) { $post = get_post($id); if ( 'attachment' == $post->post_type ) return true; } return false; } |
関数 wp_insert_attachment()
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 | /** * Insert an attachment. * * If you set the 'ID' in the $args parameter, it will mean that you are * updating and attempt to update the attachment. You can also set the * attachment name or title by setting the key 'post_name' or 'post_title'. * * You can set the dates for the attachment manually by setting the 'post_date' * and 'post_date_gmt' keys' values. * * By default, the comments will use the default settings for whether the * comments are allowed. You can close them manually or keep them open by * setting the value for the 'comment_status' key. * * @since 2.0.0 * * @see wp_insert_post() * * @param string|array $args Arguments for inserting an attachment. * @param string $file Optional. Filename. * @param int $parent Optional. Parent post ID. * @return int Attachment ID. */ function wp_insert_attachment( $args, $file = false, $parent = 0 ) { $defaults = array( 'file' => $file, 'post_parent' => $parent ); $data = wp_parse_args( $args, $defaults ); $data['post_type'] = 'attachment'; return wp_insert_post( $data ); } |
関数 wp_delete_attachment()
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 | /** * Trash or delete an attachment. * * When an attachment is permanently deleted, the file will also be removed. * Deletion removes all post meta fields, taxonomy, comments, etc. associated * with the attachment (except the main post). * * The attachment is moved to the trash instead of permanently deleted unless trash * for media is disabled, item is already in the trash, or $force_delete is true. * * @since 2.0.0 * * @global wpdb $wpdb WordPress database access abstraction object. * * @param int $post_id Attachment ID. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. * Default false. * @return mixed False on failure. Post data on success. */ function wp_delete_attachment( $post_id, $force_delete = false ) { global $wpdb; if ( !$post = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id) ) ) return $post; if ( 'attachment' != $post->post_type ) return false; if ( !$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status ) return wp_trash_post( $post_id ); delete_post_meta($post_id, '_wp_trash_meta_status'); delete_post_meta($post_id, '_wp_trash_meta_time'); $meta = wp_get_attachment_metadata( $post_id ); $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); $file = get_attached_file( $post_id ); $intermediate_sizes = array(); foreach ( get_intermediate_image_sizes() as $size ) { if ( $intermediate = image_get_intermediate_size( $post_id, $size ) ) $intermediate_sizes[] = $intermediate; } if ( is_multisite() ) delete_transient( 'dirsize_cache' ); /** * Fires before an attachment is deleted, at the start of wp_delete_attachment(). * * @since 2.0.0 * * @param int $post_id Attachment ID. */ do_action( 'delete_attachment', $post_id ); wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); // Delete all for any posts. delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id )); foreach ( $comment_ids as $comment_id ) wp_delete_comment( $comment_id, true ); $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id )); foreach ( $post_meta_ids as $mid ) delete_metadata_by_mid( 'post', $mid ); /** This action is documented in wp-includes/post.php */ do_action( 'delete_post', $post_id ); $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); if ( ! $result ) { return false; } /** This action is documented in wp-includes/post.php */ do_action( 'deleted_post', $post_id ); $uploadpath = wp_upload_dir(); if ( ! empty($meta['thumb']) ) { // Don't delete the thumb if another attachment uses it. if (! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id)) ) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); /** This filter is documented in wp-admin/custom-header.php */ $thumbfile = apply_filters( 'wp_delete_file', $thumbfile ); @ unlink( path_join($uploadpath['basedir'], $thumbfile) ); } } // Remove intermediate and backup images if there are any. foreach ( $intermediate_sizes as $intermediate ) { /** This filter is documented in wp-admin/custom-header.php */ $intermediate_file = apply_filters( 'wp_delete_file', $intermediate['path'] ); @ unlink( path_join($uploadpath['basedir'], $intermediate_file) ); } if ( is_array($backup_sizes) ) { foreach ( $backup_sizes as $size ) { $del_file = path_join( dirname($meta['file']), $size['file'] ); /** This filter is documented in wp-admin/custom-header.php */ $del_file = apply_filters( 'wp_delete_file', $del_file ); @ unlink( path_join($uploadpath['basedir'], $del_file) ); } } /** This filter is documented in wp-admin/custom-header.php */ $file = apply_filters( 'wp_delete_file', $file ); if ( ! empty($file) ) @ unlink($file); clean_post_cache( $post ); return $post; } |
関数 wp_get_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 25 26 27 28 29 30 | /** * Retrieve attachment meta field for attachment ID. * * @since 2.1.0 * * @param int $post_id Attachment ID. Default 0. * @param bool $unfiltered Optional. If true, filters are not run. Default false. * @return string|bool Attachment meta field. False on failure. */ function wp_get_attachment_metadata( $post_id = 0, $unfiltered = false ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); if ( $unfiltered ) return $data; /** * Filter the attachment meta data. * * @since 2.1.0 * * @param array|bool $data Array of meta data for the given attachment, or false * if the object does not exist. * @param int $post_id Attachment ID. */ return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); } |
関数 wp_update_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 25 26 27 | /** * Update metadata for an attachment. * * @since 2.1.0 * * @param int $post_id Attachment ID. * @param array $data Attachment data. * @return int|bool False if $post is invalid. */ function wp_update_attachment_metadata( $post_id, $data ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; /** * Filter the updated attachment meta data. * * @since 2.1.0 * * @param array $data Array of updated attachment meta data. * @param int $post_id Attachment ID. */ if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) ) return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); else return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); } |
関数 wp_get_attachment_url()
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 | /** * Retrieve the URL for an attachment. * * @since 2.1.0 * * @param int $post_id Optional. Attachment ID. Default 0. * @return string|bool Attachment URL, otherwise false. */ function wp_get_attachment_url( $post_id = 0 ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; if ( 'attachment' != $post->post_type ) return false; $url = ''; // Get attached file. if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { // Get upload directory. if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Check that the upload base exists in the file location. if ( 0 === strpos( $file, $uploads['basedir'] ) ) { // Replace file location with url location. $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); } elseif ( false !== strpos($file, 'wp-content/uploads') ) { $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 ); } else { // It's a newly-uploaded file, therefore $file is relative to the basedir. $url = $uploads['baseurl'] . "/$file"; } } } /* * If any of the above options failed, Fallback on the GUID as used pre-2.7, * not recommended to rely upon this. */ if ( empty($url) ) { $url = get_the_guid( $post->ID ); } /** * Filter the attachment URL. * * @since 2.1.0 * * @param string $url URL for the given attachment. * @param int $post_id Attachment ID. */ $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); if ( empty( $url ) ) return false; return $url; } |
関数 wp_get_attachment_thumb_file()
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 | /** * Retrieve thumbnail for an attachment. * * @since 2.1.0 * * @param int $post_id Optional. Attachment ID. Default 0. * @return mixed False on failure. Thumbnail file path on success. */ function wp_get_attachment_thumb_file( $post_id = 0 ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) return false; $file = get_attached_file( $post->ID ); if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) ) { /** * Filter the attachment thumbnail file path. * * @since 2.1.0 * * @param string $thumbfile File path to the attachment thumbnail. * @param int $post_id Attachment ID. */ return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); } return false; } |
関数 wp_get_attachment_thumb_url()
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 | /** * Retrieve URL for an attachment thumbnail. * * @since 2.1.0 * * @param int $post_id Optional. Attachment ID. Default 0. * @return string|bool False on failure. Thumbnail URL on success. */ function wp_get_attachment_thumb_url( $post_id = 0 ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; if ( !$url = wp_get_attachment_url( $post->ID ) ) return false; $sized = image_downsize( $post_id, 'thumbnail' ); if ( $sized ) return $sized[0]; if ( !$thumb = wp_get_attachment_thumb_file( $post->ID ) ) return false; $url = str_replace(basename($url), basename($thumb), $url); /** * Filter the attachment thumbnail URL. * * @since 2.1.0 * * @param string $url URL for the attachment thumbnail. * @param int $post_id Attachment ID. */ return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); } |
関数 wp_attachment_is_image()
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 if the attachment is an image. * * @since 2.1.0 * * @param int $post_id Optional. Attachment ID. Default 0. * @return bool Whether the attachment is an image. */ function wp_attachment_is_image( $post_id = 0 ) { $post_id = (int) $post_id; if ( !$post = get_post( $post_id ) ) return false; if ( !$file = get_attached_file( $post->ID ) ) return false; $ext = preg_match('/\.([^.]+)$/', $file, $matches) ? strtolower($matches[1]) : false; $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); if ( 'image/' == substr($post->post_mime_type, 0, 6) || $ext && 'import' == $post->post_mime_type && in_array($ext, $image_exts) ) return true; return false; } |
関数 wp_mime_type_icon()
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 | /** * Retrieve the icon for a MIME type. * * @since 2.1.0 * * @param string|int $mime MIME type or attachment ID. * @return string|bool Icon, false otherwise. */ function wp_mime_type_icon( $mime = 0 ) { if ( !is_numeric($mime) ) $icon = wp_cache_get("mime_type_icon_$mime"); $post_id = 0; if ( empty($icon) ) { $post_mimes = array(); if ( is_numeric($mime) ) { $mime = (int) $mime; if ( $post = get_post( $mime ) ) { $post_id = (int) $post->ID; $ext = preg_replace('/^.+?\.([^.]+)$/', '$1', $post->guid); if ( !empty($ext) ) { $post_mimes[] = $ext; if ( $ext_type = wp_ext2type( $ext ) ) $post_mimes[] = $ext_type; } $mime = $post->post_mime_type; } else { $mime = 0; } } else { $post_mimes[] = $mime; } $icon_files = wp_cache_get('icon_files'); if ( !is_array($icon_files) ) { /** * Filter the icon directory path. * * @since 2.0.0 * * @param string $path Icon directory absolute path. */ $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); /** * Filter the icon directory URI. * * @since 2.0.0 * * @param string $uri Icon directory URI. */ $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); /** * Filter the list of icon directory URIs. * * @since 2.5.0 * * @param array $uris List of icon directory URIs. */ $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); $icon_files = array(); while ( $dirs ) { $keys = array_keys( $dirs ); $dir = array_shift( $keys ); $uri = array_shift($dirs); if ( $dh = opendir($dir) ) { while ( false !== $file = readdir($dh) ) { $file = basename($file); if ( substr($file, 0, 1) == '.' ) continue; if ( !in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg') ) ) { if ( is_dir("$dir/$file") ) $dirs["$dir/$file"] = "$uri/$file"; continue; } $icon_files["$dir/$file"] = "$uri/$file"; } closedir($dh); } } wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); } // Icon basename - extension = MIME wildcard. foreach ( $icon_files as $file => $uri ) $types[ preg_replace('/^([^.]*).*$/', '$1', basename($file)) ] =& $icon_files[$file]; if ( ! empty($mime) ) { $post_mimes[] = substr($mime, 0, strpos($mime, '/')); $post_mimes[] = substr($mime, strpos($mime, '/') + 1); $post_mimes[] = str_replace('/', '_', $mime); } $matches = wp_match_mime_types(array_keys($types), $post_mimes); $matches['default'] = array('default'); foreach ( $matches as $match => $wilds ) { if ( isset($types[$wilds[0]])) { $icon = $types[$wilds[0]]; if ( !is_numeric($mime) ) wp_cache_add("mime_type_icon_$mime", $icon); break; } } } /** * Filter the mime type icon. * * @since 2.1.0 * * @param string $icon Path to the mime type icon. * @param string $mime Mime type. * @param int $post_id Attachment ID. Will equal 0 if the function passed * the mime type. */ return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); } |