WordPressを読む 38-12 /blog/wp-includes/post.php 12
2015/01/06
目次
- 1 /blog/wp-includes/post.php 12
- 2 関数 wp_check_for_changed_slugs()
- 3 関数 get_private_posts_cap_sql()
- 4 関数 get_posts_by_author_sql()
- 5 関数 get_lastpostdate()
- 6 関数 get_lastpostmodified()
- 7 関数 _get_last_post_time()
- 8 関数 update_post_cache()
- 9 関数 clean_post_cache()
- 10 関数 update_post_caches()
- 11 関数 update_postmeta_cache()
- 12 関数 clean_attachment_cache()
- 13 関数 _transition_post_status()
- 14 関数 _future_post_hook()
- 15 関数 _publish_post_hook()
- 16 関数 wp_get_post_parent_id()
- 17 関数 wp_check_post_hierarchy_for_loops()
- 18 関数 set_post_thumbnail()
- 19 関数 delete_post_thumbnail()
- 20 関数 wp_delete_auto_drafts()
- 21 関数 _update_term_count_on_transition_post_status()
- 22 関数 _prime_post_caches()
/blog/wp-includes/post.php 12
関数 wp_check_for_changed_slugs()
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 | /** * Check for changed slugs for published post objects and save the old slug. * * The function is used when a post object of any type is updated, * by comparing the current and previous post objects. * * If the slug was changed and not already part of the old slugs then it will be * added to the post meta field ('_wp_old_slug') for storing old slugs for that * post. * * The most logically usage of this function is redirecting changed post objects, so * that those that linked to an changed post will be redirected to the new post. * * @since 2.1.0 * * @param int $post_id Post ID. * @param WP_Post $post The Post Object * @param WP_Post $post_before The Previous Post Object * @return int Same as $post_id */ function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { // Don't bother if it hasnt changed. if ( $post->post_name == $post_before->post_name ) return; // We're only concerned with published, non-hierarchical objects. if ( $post->post_status != 'publish' || is_post_type_hierarchical( $post->post_type ) ) return; $old_slugs = (array) get_post_meta($post_id, '_wp_old_slug'); // If we haven't added this old slug before, add it now. if ( !empty( $post_before->post_name ) && !in_array($post_before->post_name, $old_slugs) ) add_post_meta($post_id, '_wp_old_slug', $post_before->post_name); // If the new slug was used previously, delete it from the list. if ( in_array($post->post_name, $old_slugs) ) delete_post_meta($post_id, '_wp_old_slug', $post->post_name); } |
関数 get_private_posts_cap_sql()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Retrieve the private post SQL based on capability. * * This function provides a standardized way to appropriately select on the * post_status of a post type. The function will return a piece of SQL code * that can be added to a WHERE clause; this SQL is constructed to allow all * published posts, and all private posts to which the user has access. * * @since 2.2.0 * * @param string $post_type Post type. Currently only supports 'post' or 'page'. * @return string SQL code that can be added to a where clause. */ function get_private_posts_cap_sql( $post_type ) { return get_posts_by_author_sql( $post_type, false ); } |
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 | /** * Retrieve the post SQL based on capability, author, and type. * * @since 3.0.0 * * @see get_private_posts_cap_sql() * * @param string $post_type Post type. * @param bool $full Optional. Returns a full WHERE statement instead of just * an 'andalso' term. Default true. * @param int $post_author Optional. Query posts having a single author ID. Default null. * @param bool $public_only Optional. Only return public posts. Skips cap checks for * $current_user. Default false. * @return string SQL WHERE code that can be added to a query. */ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { global $wpdb; // Private posts. $post_type_obj = get_post_type_object( $post_type ); if ( ! $post_type_obj ) return $full ? 'WHERE 1 = 0' : ' 1 = 0 '; /** * Filter the capability to read private posts for a custom post type * when generating SQL for getting posts by author. * * @since 2.2.0 * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". * * @param string $cap Capability. */ if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { $cap = $post_type_obj->cap->read_private_posts; } if ( $full ) { if ( null === $post_author ) { $sql = $wpdb->prepare( 'WHERE post_type = %s AND ', $post_type ); } else { $sql = $wpdb->prepare( 'WHERE post_author = %d AND post_type = %s AND ', $post_author, $post_type ); } } else { $sql = ''; } $sql .= "(post_status = 'publish'"; // Only need to check the cap if $public_only is false. if ( false === $public_only ) { if ( current_user_can( $cap ) ) { // Does the user have the capability to view private posts? Guess so. $sql .= " OR post_status = 'private'"; } elseif ( is_user_logged_in() ) { // Users can view their own private posts. $id = get_current_user_id(); if ( null === $post_author || ! $full ) { $sql .= " OR post_status = 'private' AND post_author = $id"; } elseif ( $id == (int) $post_author ) { $sql .= " OR post_status = 'private'"; } // else none } // else none } $sql .= ')'; return $sql; } |
関数 get_lastpostdate()
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 | /** * Retrieve the date that the last post was published. * * The server timezone is the default and is the difference between GMT and * server time. The 'blog' value is the date when the last post was posted. The * 'gmt' is when the last post was posted in GMT formatted date. * * @since 0.71 * * @param string $timezone The location to get the time. Accepts 'gmt', 'blog', * or 'server'. Default 'server'. * @return string The date of the last post. */ function get_lastpostdate( $timezone = 'server' ) { /** * Filter the date the last post was published. * * @since 2.3.0 * * @param string $date Date the last post was published. Likely values are 'gmt', * 'blog', or 'server'. * @param string $timezone Location to use for getting the post published date. */ return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date' ), $timezone ); } |
関数 get_lastpostmodified()
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 last post modified date depending on timezone. * * The server timezone is the default and is the difference between GMT and * server time. The 'blog' value is just when the last post was modified. The * 'gmt' is when the last post was modified in GMT time. * * @since 1.2.0 * * @param string $timezone The location to get the time. Accepts 'gmt', 'blog', or 'server'. * Default 'server'. * @return string The date the post was last modified. */ function get_lastpostmodified( $timezone = 'server' ) { $lastpostmodified = _get_last_post_time( $timezone, 'modified' ); $lastpostdate = get_lastpostdate($timezone); if ( $lastpostdate > $lastpostmodified ) $lastpostmodified = $lastpostdate; /** * Filter the date the last post was modified. * * @since 2.3.0 * * @param string $lastpostmodified Date the last post was modified. * @param string $timezone Location to use for getting the post modified date. */ return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); } |
関数 _get_last_post_time()
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 | /** * Retrieve latest post date data based on timezone. * * @access private * @since 3.1.0 * * @param string $timezone The location to get the time. Accepts 'gmt', 'blog', or 'server'. * @param string $field Field to check. Can be 'date' or 'modified'. * @return string The date. */ function _get_last_post_time( $timezone, $field ) { global $wpdb; if ( !in_array( $field, array( 'date', 'modified' ) ) ) return false; $timezone = strtolower( $timezone ); $key = "lastpost{$field}:$timezone"; $date = wp_cache_get( $key, 'timeinfo' ); if ( !$date ) { $add_seconds_server = date('Z'); $post_types = get_post_types( array( 'public' => true ) ); array_walk( $post_types, array( &$wpdb, 'escape_by_ref' ) ); $post_types = "'" . implode( "', '", $post_types ) . "'"; switch ( $timezone ) { case 'gmt': $date = $wpdb->get_var("SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); break; case 'blog': $date = $wpdb->get_var("SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); break; case 'server': $date = $wpdb->get_var("SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1"); break; } if ( $date ) wp_cache_set( $key, $date, 'timeinfo' ); } return $date; } |
関数 update_post_cache()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Updates posts in cache. * * @since 1.5.1 * * @param array $posts Array of post objects, passed by reference. */ function update_post_cache( &$posts ) { if ( ! $posts ) return; foreach ( $posts as $post ) wp_cache_add( $post->ID, $post, 'posts' ); } |
関数 clean_post_cache()
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 | /** * Will clean the post in the cache. * * Cleaning means delete from the cache of the post. Will call to clean the term * object cache associated with the post ID. * * This function not run if $_wp_suspend_cache_invalidation is not empty. See * wp_suspend_cache_invalidation(). * * @since 2.0.0 * * @global wpdb $wpdb WordPress database access abstraction object. * * @param int|WP_Post $post Post ID or post object to remove from the cache. */ function clean_post_cache( $post ) { global $_wp_suspend_cache_invalidation, $wpdb; if ( ! empty( $_wp_suspend_cache_invalidation ) ) return; $post = get_post( $post ); if ( empty( $post ) ) return; wp_cache_delete( $post->ID, 'posts' ); wp_cache_delete( $post->ID, 'post_meta' ); clean_object_term_cache( $post->ID, $post->post_type ); wp_cache_delete( 'wp_get_archives', 'general' ); /** * Fires immediately after the given post's cache is cleaned. * * @since 2.5.0 * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ do_action( 'clean_post_cache', $post->ID, $post ); if ( 'page' == $post->post_type ) { wp_cache_delete( 'all_page_ids', 'posts' ); /** * Fires immediately after the given page's cache is cleaned. * * @since 2.5.0 * * @param int $post_id Post ID. */ do_action( 'clean_page_cache', $post->ID ); } wp_cache_set( 'last_changed', microtime(), 'posts' ); } |
関数 update_post_caches()
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 | /** * Call major cache updating functions for list of Post objects. * * @since 1.5.0 * * @param array $posts Array of Post objects * @param string $post_type Optional. Post type. Default 'post'. * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. */ function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { // No point in doing all this work if we didn't match any posts. if ( !$posts ) return; update_post_cache($posts); $post_ids = array(); foreach ( $posts as $post ) $post_ids[] = $post->ID; if ( ! $post_type ) $post_type = 'any'; if ( $update_term_cache ) { if ( is_array($post_type) ) { $ptypes = $post_type; } elseif ( 'any' == $post_type ) { // Just use the post_types in the supplied posts. foreach ( $posts as $post ) $ptypes[] = $post->post_type; $ptypes = array_unique($ptypes); } else { $ptypes = array($post_type); } if ( ! empty($ptypes) ) update_object_term_cache($post_ids, $ptypes); } if ( $update_meta_cache ) update_postmeta_cache($post_ids); } |
関数 update_postmeta_cache()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Updates metadata cache for list of post IDs. * * Performs SQL query to retrieve the metadata for the post IDs and updates the * metadata cache for the posts. Therefore, the functions, which call this * function, do not need to perform SQL queries on their own. * * @since 2.1.0 * * @param array $post_ids List of post IDs. * @return bool|array Returns false if there is nothing to update or an array * of metadata. */ function update_postmeta_cache( $post_ids ) { return update_meta_cache('post', $post_ids); } |
関数 clean_attachment_cache()
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 | /** * Will clean the attachment in the cache. * * Cleaning means delete from the cache. Optionally will clean the term * object cache associated with the attachment ID. * * This function will not run if $_wp_suspend_cache_invalidation is not empty. * * @since 3.0.0 * * @see wp_suspend_cache_invalidation() * * @param int $id The attachment ID in the cache to clean. * @param bool $clean_terms Optional. Whether to clean terms cache. Default false. */ function clean_attachment_cache( $id, $clean_terms = false ) { global $_wp_suspend_cache_invalidation; if ( !empty($_wp_suspend_cache_invalidation) ) return; $id = (int) $id; wp_cache_delete($id, 'posts'); wp_cache_delete($id, 'post_meta'); if ( $clean_terms ) clean_object_term_cache($id, 'attachment'); /** * Fires after the given attachment's cache is cleaned. * * @since 3.0.0 * * @param int $id Attachment ID. */ do_action( 'clean_attachment_cache', $id ); } |
関数 _transition_post_status()
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 | // // Hooks // /** * Hook for managing future post transitions to published. * * @since 2.3.0 * @access private * * @see wp_clear_scheduled_hook() * @global wpdb $wpdb WordPress database access abstraction object. * * @param string $new_status New post status. * @param string $old_status Previous post status. * @param WP_Post $post Post object. */ function _transition_post_status( $new_status, $old_status, $post ) { global $wpdb; if ( $old_status != 'publish' && $new_status == 'publish' ) { // Reset GUID if transitioning to publish and it is empty. if ( '' == get_the_guid($post->ID) ) $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); /** * Fires when a post's status is transitioned from private to published. * * @since 1.5.0 * @deprecated 2.3.0 Use 'private_to_publish' instead. * * @param int $post_id Post ID. */ do_action('private_to_published', $post->ID); } // If published posts changed clear the lastpostmodified cache. if ( 'publish' == $new_status || 'publish' == $old_status) { foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); } } if ( $new_status !== $old_status ) { wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); } // Always clears the hook in case the post status bounced from future to draft. wp_clear_scheduled_hook('publish_future_post', array( $post->ID ) ); } |
関数 _future_post_hook()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * Hook used to schedule publication for a post marked for the future. * * The $post properties used and must exist are 'ID' and 'post_date_gmt'. * * @since 2.3.0 * @access private * * @param int $deprecated Not used. Can be set to null. Never implemented. Not marked * as deprecated with _deprecated_argument() as it conflicts with * wp_transition_post_status() and the default filter for * {@see _future_post_hook()}. * @param WP_Post $post Post object. */ function _future_post_hook( $deprecated, $post ) { wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT') , 'publish_future_post', array( $post->ID ) ); } |
関数 _publish_post_hook()
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 | /** * Hook to schedule pings and enclosures when a post is published. * * Uses XMLRPC_REQUEST and WP_IMPORTING constants. * * @since 2.3.0 * @access private * * @param int $post_id The ID in the database table of the post being published. */ function _publish_post_hook( $post_id ) { if ( defined( 'XMLRPC_REQUEST' ) ) { /** * Fires when _publish_post_hook() is called during an XML-RPC request. * * @since 2.1.0 * * @param int $post_id Post ID. */ do_action( 'xmlrpc_publish_post', $post_id ); } if ( defined('WP_IMPORTING') ) return; if ( get_option('default_pingback_flag') ) add_post_meta( $post_id, '_pingme', '1' ); add_post_meta( $post_id, '_encloseme', '1' ); wp_schedule_single_event(time(), 'do_pings'); } |
関数 wp_get_post_parent_id()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Return the post's parent's post_ID * * @since 3.1.0 * * @param int $post_id * * @return int|bool Post parent ID, otherwise false. */ function wp_get_post_parent_id( $post_ID ) { $post = get_post( $post_ID ); if ( !$post || is_wp_error( $post ) ) return false; return (int) $post->post_parent; } |
関数 wp_check_post_hierarchy_for_loops()
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 | /** * Check the given subset of the post hierarchy for hierarchy loops. * * Prevents loops from forming and breaks those that it finds. Attached * to the 'wp_insert_post_parent' filter. * * @since 3.1.0 * * @see wp_find_hierarchy_loop() * * @param int $post_parent ID of the parent for the post we're checking. * @param int $post_ID ID of the post we're checking. * @return int The new post_parent for the post, 0 otherwise. */ function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { // Nothing fancy here - bail. if ( !$post_parent ) return 0; // New post can't cause a loop. if ( empty( $post_ID ) ) return $post_parent; // Can't be its own parent. if ( $post_parent == $post_ID ) return 0; // Now look for larger loops. if ( !$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) ) return $post_parent; // No loop // Setting $post_parent to the given value causes a loop. if ( isset( $loop[$post_ID] ) ) return 0; // There's a loop, but it doesn't contain $post_ID. Break the loop. foreach ( array_keys( $loop ) as $loop_member ) wp_update_post( array( 'ID' => $loop_member, 'post_parent' => 0 ) ); return $post_parent; } |
関数 set_post_thumbnail()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Set a post thumbnail. * * @since 3.1.0 * * @param int|WP_Post $post Post ID or post object where thumbnail should be attached. * @param int $thumbnail_id Thumbnail to attach. * @return bool True on success, false on failure. */ function set_post_thumbnail( $post, $thumbnail_id ) { $post = get_post( $post ); $thumbnail_id = absint( $thumbnail_id ); if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); else return delete_post_meta( $post->ID, '_thumbnail_id' ); } return false; } |
関数 delete_post_thumbnail()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Remove a post thumbnail. * * @since 3.3.0 * * @param int|WP_Post $post Post ID or post object where thumbnail should be removed from. * @return bool True on success, false on failure. */ function delete_post_thumbnail( $post ) { $post = get_post( $post ); if ( $post ) return delete_post_meta( $post->ID, '_thumbnail_id' ); return false; } |
関数 wp_delete_auto_drafts()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Delete auto-drafts for new posts that are > 7 days old. * * @since 3.4.0 * * @global wpdb $wpdb WordPress database access abstraction object. */ function wp_delete_auto_drafts() { global $wpdb; // Cleanup old auto-drafts more than 7 days old. $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" ); foreach ( (array) $old_posts as $delete ) { // Force delete. wp_delete_post( $delete, true ); } } |
関数 _update_term_count_on_transition_post_status()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * Update the custom taxonomies' term counts when a post's status is changed. * * For example, default posts term counts (for custom taxonomies) don't include * private / draft posts. * * @since 3.3.0 * @access private * * @param string $new_status New post status. * @param string $old_status Old post status. * @param WP_Post $post Post object. */ function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { // Update counts for the post's terms. foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); wp_update_term_count( $tt_ids, $taxonomy ); } } |
関数 _prime_post_caches()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Adds any posts from the given ids to the cache that do not already exist in cache * * @since 3.4.0 * @access private * * @see update_post_caches() * * @param array $post_ids ID list * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. */ function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { global $wpdb; $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); if ( !empty( $non_cached_ids ) ) { $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ",", $non_cached_ids ) ) ); update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); } } |