WordPressを読む 38-6 /blog/wp-includes/post.php 6
2014/12/25
目次
- 1 /blog/wp-includes/post.php 6
- 2 関数 stick_post()
- 3 関数 unstick_post()
- 4 関数 _count_posts_cache_key()
- 5 関数 wp_count_posts()
- 6 関数 wp_count_attachments()
- 7 関数 get_post_mime_types()
- 8 関数 wp_match_mime_types()
- 9 関数 wp_post_mime_type_where()
- 10 関数 wp_delete_post()
- 11 関数 _reset_front_page_settings_for_post()
- 12 関数間の処理
- 13 関数 wp_trash_post()
/blog/wp-includes/post.php 6
関数 stick_post()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Make a post sticky. * * Sticky posts should be displayed at the top of the front page. * * @since 2.7.0 * * @param int $post_id Post ID. */ function stick_post( $post_id ) { $stickies = get_option('sticky_posts'); if ( !is_array($stickies) ) $stickies = array($post_id); if ( ! in_array($post_id, $stickies) ) $stickies[] = $post_id; update_option('sticky_posts', $stickies); } |
関数 unstick_post()
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 | /** * Un-stick a post. * * Sticky posts should be displayed at the top of the front page. * * @since 2.7.0 * * @param int $post_id Post ID. */ function unstick_post( $post_id ) { $stickies = get_option('sticky_posts'); if ( !is_array($stickies) ) return; if ( ! in_array($post_id, $stickies) ) return; $offset = array_search($post_id, $stickies); if ( false === $offset ) return; array_splice($stickies, $offset, 1); update_option('sticky_posts', $stickies); } |
関数 _count_posts_cache_key()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Return the cache key for wp_count_posts() based on the passed arguments. * * @since 3.9.0 * * @param string $type Optional. Post type to retrieve count Default 'post'. * @param string $perm Optional. 'readable' or empty. Default empty. * @return string The cache key. */ function _count_posts_cache_key( $type = 'post', $perm = '' ) { $cache_key = 'posts-' . $type; if ( 'readable' == $perm && is_user_logged_in() ) { $post_type_object = get_post_type_object( $type ); if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { $cache_key .= '_' . $perm . '_' . get_current_user_id(); } } return $cache_key; } |
関数 wp_count_posts()
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 | /** * Count number of posts of a post type and if user has permissions to view. * * This function provides an efficient method of finding the amount of post's * type a blog has. Another method is to count the amount of items in * get_posts(), but that method has a lot of overhead with doing so. Therefore, * when developing for 2.5+, use this function instead. * * The $perm parameter checks for 'readable' value and if the user can read * private posts, it will display that for the user that is signed in. * * @since 2.5.0 * * @param string $type Optional. Post type to retrieve count. Default 'post'. * @param string $perm Optional. 'readable' or empty. Default empty. * @return object Number of posts for each status. */ function wp_count_posts( $type = 'post', $perm = '' ) { global $wpdb; if ( ! post_type_exists( $type ) ) return new stdClass; $cache_key = _count_posts_cache_key( $type, $perm ); $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; if ( 'readable' == $perm && is_user_logged_in() ) { $post_type_object = get_post_type_object($type); if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { $query .= $wpdb->prepare( " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", get_current_user_id() ); } } $query .= ' GROUP BY post_status'; $counts = wp_cache_get( $cache_key, 'counts' ); if ( false === $counts ) { $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); $counts = array_fill_keys( get_post_stati(), 0 ); foreach ( $results as $row ) $counts[ $row['post_status'] ] = $row['num_posts']; $counts = (object) $counts; wp_cache_set( $cache_key, $counts, 'counts' ); } /** * Modify returned post counts by status for the current post type. * * @since 3.7.0 * * @param object $counts An object containing the current post_type's post * counts by status. * @param string $type Post type. * @param string $perm The permission to determine if the posts are 'readable' * by the current user. */ return apply_filters( 'wp_count_posts', $counts, $type, $perm ); } |
関数 wp_count_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 29 30 31 32 33 34 35 36 37 38 | /** * Count number of attachments for the mime type(s). * * If you set the optional mime_type parameter, then an array will still be * returned, but will only have the item you are looking for. It does not give * you the number of attachments that are children of a post. You can get that * by counting the number of children that post has. * * @since 2.5.0 * * @param string|array $mime_type Optional. Array or comma-separated list of * MIME patterns. Default empty. * @return object An object containing the attachment counts by mime type. */ function wp_count_attachments( $mime_type = '' ) { global $wpdb; $and = wp_post_mime_type_where( $mime_type ); $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); $counts = array(); foreach( (array) $count as $row ) { $counts[ $row['post_mime_type'] ] = $row['num_posts']; } $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and"); /** * Modify returned attachment counts by mime type. * * @since 3.7.0 * * @param object $counts An object containing the attachment counts by * mime type. * @param string $mime_type The mime type pattern used to filter the attachments * counted. */ return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); } |
関数 get_post_mime_types()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Get default post mime types. * * @since 2.9.0 * * @return array List of post mime types. */ function get_post_mime_types() { $post_mime_types = array( // array( adj, noun ) 'image' => array(__('Images'), __('Manage Images'), _n_noop('Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>')), 'audio' => array(__('Audio'), __('Manage Audio'), _n_noop('Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>')), 'video' => array(__('Video'), __('Manage Video'), _n_noop('Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>')), ); /** * Filter the default list of post mime types. * * @since 2.5.0 * * @param array $post_mime_types Default list of post mime types. */ return apply_filters( 'post_mime_types', $post_mime_types ); } |
関数 wp_match_mime_types()
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 | /** * Check a MIME-Type against a list. * * If the wildcard_mime_types parameter is a string, it must be comma separated * list. If the real_mime_types is a string, it is also comma separated to * create the list. * * @since 2.5.0 * * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*) * or flash (same as *flash*). * @param string|array $real_mime_types Real post mime type values. * @return array array(wildcard=>array(real types)). */ function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { $matches = array(); if ( is_string( $wildcard_mime_types ) ) { $wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) ); } if ( is_string( $real_mime_types ) ) { $real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) ); } $patternses = array(); $wild = '[-._a-z0-9]*'; foreach ( (array) $wildcard_mime_types as $type ) { $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $type ) ) ); $patternses[1][$type] = "^$regex$"; if ( false === strpos($type, '/') ) { $patternses[2][$type] = "^$regex/"; $patternses[3][$type] = $regex; } } asort( $patternses ); foreach ( $patternses as $patterns ) { foreach ( $patterns as $type => $pattern ) { foreach ( (array) $real_mime_types as $real ) { if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[$type] ) || false === array_search( $real, $matches[$type] ) ) ) { $matches[$type][] = $real; } } } } return $matches; } |
関数 wp_post_mime_type_where()
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 | /** * Convert MIME types into SQL. * * @since 2.5.0 * * @param string|array $post_mime_types List of mime types or comma separated string * of mime types. * @param string $table_alias Optional. Specify a table alias, if needed. * Default empty. * @return string The SQL AND clause for mime searching. */ function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { $where = ''; $wildcards = array('', '%', '%/%'); if ( is_string($post_mime_types) ) $post_mime_types = array_map('trim', explode(',', $post_mime_types)); foreach ( (array) $post_mime_types as $mime_type ) { $mime_type = preg_replace('/\s/', '', $mime_type); $slashpos = strpos($mime_type, '/'); if ( false !== $slashpos ) { $mime_group = preg_replace('/[^-*.a-zA-Z0-9]/', '', substr($mime_type, 0, $slashpos)); $mime_subgroup = preg_replace('/[^-*.+a-zA-Z0-9]/', '', substr($mime_type, $slashpos + 1)); if ( empty($mime_subgroup) ) $mime_subgroup = '*'; else $mime_subgroup = str_replace('/', '', $mime_subgroup); $mime_pattern = "$mime_group/$mime_subgroup"; } else { $mime_pattern = preg_replace('/[^-*.a-zA-Z0-9]/', '', $mime_type); if ( false === strpos($mime_pattern, '*') ) $mime_pattern .= '/*'; } $mime_pattern = preg_replace('/\*+/', '%', $mime_pattern); if ( in_array( $mime_type, $wildcards ) ) return ''; if ( false !== strpos($mime_pattern, '%') ) $wheres[] = empty($table_alias) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; else $wheres[] = empty($table_alias) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; } if ( !empty($wheres) ) $where = ' AND (' . join(' OR ', $wheres) . ') '; return $where; } |
関数 wp_delete_post()
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 | /** * Trash or delete a post or page. * * When the post and page is permanently deleted, everything that is tied to * it is deleted also. This includes comments, post meta fields, and terms * associated with the post. * * The post or page is moved to trash instead of permanently deleted unless * trash is disabled, item is already in the trash, or $force_delete is true. * * @since 1.0.0 * * @global wpdb $wpdb WordPress database access abstraction object. * @see wp_delete_attachment() * @see wp_trash_post() * * @param int $postid Optional. Post ID. Default 0. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. * Default false. * @return array|bool|WP_Post False on failure. */ function wp_delete_post( $postid = 0, $force_delete = false ) { global $wpdb; if ( !$post = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->posts WHERE ID = %d", $postid)) ) return $post; if ( !$force_delete && ( $post->post_type == 'post' || $post->post_type == 'page') && get_post_status( $postid ) != 'trash' && EMPTY_TRASH_DAYS ) return wp_trash_post($postid); if ( $post->post_type == 'attachment' ) return wp_delete_attachment( $postid, $force_delete ); /** * Fires before a post is deleted, at the start of wp_delete_post(). * * @since 3.2.0 * * @see wp_delete_post() * * @param int $postid Post ID. */ do_action( 'before_delete_post', $postid ); delete_post_meta($postid,'_wp_trash_meta_status'); delete_post_meta($postid,'_wp_trash_meta_time'); wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type)); $parent_data = array( 'post_parent' => $post->post_parent ); $parent_where = array( 'post_parent' => $postid ); if ( is_post_type_hierarchical( $post->post_type ) ) { // Point children of this page to its parent, also clean the cache of affected children. $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); $children = $wpdb->get_results( $children_query ); $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); } // Do raw query. wp_get_post_revisions() is filtered. $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. foreach ( $revision_ids as $revision_id ) wp_delete_post_revision( $revision_id ); // Point all attachments to this post up one level. $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid )); 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 ", $postid )); foreach ( $post_meta_ids as $mid ) delete_metadata_by_mid( 'post', $mid ); /** * Fires immediately before a post is deleted from the database. * * @since 1.2.0 * * @param int $postid Post ID. */ do_action( 'delete_post', $postid ); $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); if ( ! $result ) { return false; } /** * Fires immediately after a post is deleted from the database. * * @since 2.2.0 * * @param int $postid Post ID. */ do_action( 'deleted_post', $postid ); clean_post_cache( $post ); if ( is_post_type_hierarchical( $post->post_type ) && $children ) { foreach ( $children as $child ) clean_post_cache( $child ); } wp_clear_scheduled_hook('publish_future_post', array( $postid ) ); /** * Fires after a post is deleted, at the conclusion of wp_delete_post(). * * @since 3.2.0 * * @see wp_delete_post() * * @param int $postid Post ID. */ do_action( 'after_delete_post', $postid ); return $post; } |
関数 _reset_front_page_settings_for_post()
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 | /** * Reset the page_on_front, show_on_front, and page_for_post settings when * a linked page is deleted or trashed. * * Also ensures the post is no longer sticky. * * @since 3.7.0 * @access private * * @param int $post_id Post ID. */ function _reset_front_page_settings_for_post( $post_id ) { $post = get_post( $post_id ); if ( 'page' == $post->post_type ) { /* * If the page is defined in option page_on_front or post_for_posts, * adjust the corresponding options. */ if ( get_option( 'page_on_front' ) == $post->ID ) { update_option( 'show_on_front', 'posts' ); update_option( 'page_on_front', 0 ); } if ( get_option( 'page_for_posts' ) == $post->ID ) { delete_option( 'page_for_posts', 0 ); } } unstick_post( $post->ID ); } |
関数間の処理
1 2 | add_action( 'before_delete_post', '_reset_front_page_settings_for_post' ); add_action( 'wp_trash_post', '_reset_front_page_settings_for_post' ); |
関数 wp_trash_post()
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 | /** * Move a post or page to the Trash * * If trash is disabled, the post or page is permanently deleted. * * @since 2.9.0 * * @see wp_delete_post() * * @param int $post_id Optional. Post ID. Default is ID of the global $post * if EMPTY_TRASH_DAYS equals true. * @return bool|array Post data array, otherwise false. */ function wp_trash_post( $post_id = 0 ) { if ( !EMPTY_TRASH_DAYS ) return wp_delete_post($post_id, true); if ( !$post = get_post($post_id, ARRAY_A) ) return $post; if ( $post['post_status'] == 'trash' ) return false; /** * Fires before a post is sent to the trash. * * @since 3.3.0 * * @param int $post_id Post ID. */ do_action( 'wp_trash_post', $post_id ); add_post_meta($post_id,'_wp_trash_meta_status', $post['post_status']); add_post_meta($post_id,'_wp_trash_meta_time', time()); $post['post_status'] = 'trash'; wp_insert_post($post); wp_trash_post_comments($post_id); /** * Fires after a post is sent to the trash. * * @since 2.9.0 * * @param int $post_id Post ID. */ do_action( 'trashed_post', $post_id ); return $post; } |