WordPressを読む 38-9 /blog/wp-includes/post.php 9
2014/12/25
目次
- 1 /blog/wp-includes/post.php 9
- 2 関数 check_and_publish_future_post()
- 3 関数 wp_unique_post_slug()
- 4 関数 _truncate_post_slug()
- 5 関数 wp_add_post_tags()
- 6 関数 wp_set_post_tags()
- 7 関数 wp_set_post_terms()
- 8 関数 wp_set_post_categories()
- 9 関数 wp_transition_post_status()
- 10 関数 add_ping()
- 11 関数 get_enclosed()
- 12 関数 get_pung()
- 13 関数 get_to_ping()
/blog/wp-includes/post.php 9
関数 check_and_publish_future_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 | /** * Publish future post and make sure post ID has future post status. * * Invoked by cron 'publish_future_post' event. This safeguard prevents cron * from publishing drafts, etc. * * @since 2.5.0 * * @param int|WP_Post $post_id Post ID or post object. * @return null Nothing is returned. Which can mean that no action is required * or post was published. */ function check_and_publish_future_post( $post_id ) { $post = get_post($post_id); if ( empty($post) ) return; if ( 'future' != $post->post_status ) return; $time = strtotime( $post->post_date_gmt . ' GMT' ); // Uh oh, someone jumped the gun! if ( $time > time() ) { wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); return; } return wp_publish_post($post_id); } |
関数 wp_unique_post_slug()
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 | /** * Computes a unique slug for the post, when given the desired slug and some post details. * * @since 2.8.0 * * @global wpdb $wpdb WordPress database abstraction object. * @global WP_Rewrite $wp_rewrite * * @param string $slug The desired slug (post_name). * @param int $post_ID Post ID. * @param string $post_status No uniqueness checks are made if the post is still draft or pending. * @param string $post_type Post type. * @param int $post_parent Post parent ID. * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) */ function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) ) return $slug; global $wpdb, $wp_rewrite; $original_slug = $slug; $feeds = $wp_rewrite->feeds; if ( ! is_array( $feeds ) ) $feeds = array(); $hierarchical_post_types = get_post_types( array('hierarchical' => true) ); if ( 'attachment' == $post_type ) { // Attachment slugs must be unique across all types. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); /** * Filter whether the post slug would make a bad attachment slug. * * @since 3.1.0 * * @param bool $bad_slug Whether the slug would be bad as an attachment slug. * @param string $slug The post slug. */ if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { $suffix = 2; do { $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); $suffix++; } while ( $post_name_check ); $slug = $alt_post_name; } } elseif ( in_array( $post_type, $hierarchical_post_types ) ) { if ( 'nav_menu_item' == $post_type ) return $slug; /* * Page slugs must be unique within their own trees. Pages are in a separate * namespace than posts so page slugs are allowed to overlap post slugs. */ $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( '" . implode( "', '", esc_sql( $hierarchical_post_types ) ) . "' ) AND ID != %d AND post_parent = %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID, $post_parent ) ); /** * Filter whether the post slug would make a bad hierarchical post slug. * * @since 3.1.0 * * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. * @param string $slug The post slug. * @param string $post_type Post type. * @param int $post_parent Post parent ID. */ if ( $post_name_check || in_array( $slug, $feeds ) || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { $suffix = 2; do { $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID, $post_parent ) ); $suffix++; } while ( $post_name_check ); $slug = $alt_post_name; } } else { // Post slugs must be unique across all posts. $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); /** * Filter whether the post slug would be bad as a flat slug. * * @since 3.1.0 * * @param bool $bad_slug Whether the post slug would be bad as a flat slug. * @param string $slug The post slug. * @param string $post_type Post type. */ if ( $post_name_check || in_array( $slug, $feeds ) || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { $suffix = 2; do { $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); $suffix++; } while ( $post_name_check ); $slug = $alt_post_name; } } /** * Filter the unique post slug. * * @since 3.3.0 * * @param string $slug The post slug. * @param int $post_ID Post ID. * @param string $post_status The post status. * @param string $post_type Post type. * @param int $post_parent Post parent ID * @param string $original_slug The original post slug. */ return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); } |
関数 _truncate_post_slug()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * Truncate a post slug. * * @since 3.6.0 * @access private * * @see utf8_uri_encode() * * @param string $slug The slug to truncate. * @param int $length Optional. Max length of the slug. Default 200 (characters). * @return string The truncated slug. */ function _truncate_post_slug( $slug, $length = 200 ) { if ( strlen( $slug ) > $length ) { $decoded_slug = urldecode( $slug ); if ( $decoded_slug === $slug ) $slug = substr( $slug, 0, $length ); else $slug = utf8_uri_encode( $decoded_slug, $length ); } return rtrim( $slug, '-' ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Add tags to a post. * * @see wp_set_post_tags() * * @since 2.3.0 * * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. * Default 0. * @param string $tags Optional. The tags to set for the post, separated by commas. Default empty. * @return bool|null Will return false if $post_id is not an integer or is 0. Will return null otherwise. */ function wp_add_post_tags( $post_id = 0, $tags = '' ) { return wp_set_post_tags($post_id, $tags, true); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Set the tags for a post. * * @since 2.3.0 * * @see wp_set_object_terms() * * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. * @param string $tags Optional. The tags to set for the post, separated by commas. * Default empty. * @param bool $append Optional. If true, don't delete existing tags, just add on. If false, * replace the tags with the new tags. Default false. * @return mixed Array of affected term IDs. WP_Error or false on failure. */ function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { return wp_set_post_terms( $post_id, $tags, 'post_tag', $append); } |
関数 wp_set_post_terms()
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 | /** * Set the terms for a post. * * @since 2.8.0 * * @see wp_set_object_terms() * * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. * @param string $tags Optional. The tags to set for the post, separated by commas. Default empty. * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. * @param bool $append Optional. If true, don't delete existing tags, just add on. If false, * replace the tags with the new tags. Default false. * @return mixed Array of affected term IDs. WP_Error or false on failure. */ function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { $post_id = (int) $post_id; if ( !$post_id ) return false; if ( empty($tags) ) $tags = array(); if ( ! is_array( $tags ) ) { $comma = _x( ',', 'tag delimiter' ); if ( ',' !== $comma ) $tags = str_replace( $comma, ',', $tags ); $tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) ); } /* * Hierarchical taxonomies must always pass IDs rather than names so that * children with the same names but different parents aren't confused. */ if ( is_taxonomy_hierarchical( $taxonomy ) ) { $tags = array_unique( array_map( 'intval', $tags ) ); } return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); } |
関数 wp_set_post_categories()
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 | /** * Set categories for a post. * * If the post categories parameter is not set, then the default category is * going used. * * @since 2.1.0 * * @param int $post_ID Optional. The Post ID. Does not default to the ID * of the global $post. Default 0. * @param array|int $post_categories Optional. List of categories or ID of category. * Default empty array. * @param bool $append If true, don't delete existing categories, just add on. * If false, replace the categories with the new categories. * @return bool|mixed */ function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { $post_ID = (int) $post_ID; $post_type = get_post_type( $post_ID ); $post_status = get_post_status( $post_ID ); // If $post_categories isn't already an array, make it one: $post_categories = (array) $post_categories; if ( empty( $post_categories ) ) { if ( 'post' == $post_type && 'auto-draft' != $post_status ) { $post_categories = array( get_option('default_category') ); $append = false; } else { $post_categories = array(); } } else if ( 1 == count($post_categories) && '' == reset($post_categories) ) { return true; } return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); } |
関数 wp_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 53 54 55 56 57 | /** * Transition the post status of a post. * * Calls hooks to transition post status. * * The first is 'transition_post_status' with new status, old status, and post data. * * The next action called is 'OLDSTATUS_to_NEWSTATUS' the 'NEWSTATUS' is the * $new_status parameter and the 'OLDSTATUS' is $old_status parameter; it has the * post data. * * The final action is named 'NEWSTATUS_POSTTYPE', 'NEWSTATUS' is from the $new_status * parameter and POSTTYPE is post_type post data. * * @since 2.3.0 * * @param string $new_status Transition to this post status. * @param string $old_status Previous post status. * @param object $post Post data. */ function wp_transition_post_status( $new_status, $old_status, $post ) { /** * Fires when a post is transitioned from one status to another. * * @since 2.3.0 * * @param string $new_status New post status. * @param string $old_status Old post status. * @param WP_Post $post Post object. */ do_action( 'transition_post_status', $new_status, $old_status, $post ); /** * Fires when a post is transitioned from one status to another. * * The dynamic portions of the hook name, $new_status and $old status, * refer to the old and new post statuses, respectively. * * @since 2.3.0 * * @param WP_Post $post Post object. */ do_action( "{$old_status}_to_{$new_status}", $post ); /** * Fires when a post is transitioned from one status to another. * * The dynamic portions of the hook name, $new_status and $post->post_type, * refer to the new post status and post type, respectively. * * @since 2.3.0 * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); } |
関数 add_ping()
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 | // // Trackback and ping functions // /** * Add a URL to those already pinged. * * @since 1.5.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $post_id Post ID. * @param string $uri Ping URI. * @return int How many rows were updated. */ function add_ping( $post_id, $uri ) { global $wpdb; $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); $pung = trim($pung); $pung = preg_split('/\s/', $pung); $pung[] = $uri; $new = implode("\n", $pung); /** * Filter the new ping URL to add for the given post. * * @since 2.0.0 * * @param string $new New ping URL to add. */ $new = apply_filters( 'add_ping', $new ); // expected_slashed ($new). $new = wp_unslash($new); return $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post_id ) ); } |
関数 get_enclosed()
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 enclosures already enclosed for a post. * * @since 1.5.0 * * @param int $post_id Post ID. * @return array List of enclosures. */ function get_enclosed( $post_id ) { $custom_fields = get_post_custom( $post_id ); $pung = array(); if ( !is_array( $custom_fields ) ) return $pung; foreach ( $custom_fields as $key => $val ) { if ( 'enclosure' != $key || !is_array( $val ) ) continue; foreach( $val as $enc ) { $enclosure = explode( "\n", $enc ); $pung[] = trim( $enclosure[ 0 ] ); } } /** * Filter the list of enclosures already enclosed for the given post. * * @since 2.0.0 * * @param array $pung Array of enclosures for the given post. * @param int $post_id Post ID. */ $pung = apply_filters( 'get_enclosed', $pung, $post_id ); return $pung; } |
関数 get_pung()
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 | /** * Retrieve URLs already pinged for a post. * * @since 1.5.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $post_id Post ID. * @return array */ function get_pung( $post_id ) { global $wpdb; $pung = $wpdb->get_var( $wpdb->prepare( "SELECT pinged FROM $wpdb->posts WHERE ID = %d", $post_id )); $pung = trim($pung); $pung = preg_split('/\s/', $pung); /** * Filter the list of already-pinged URLs for the given post. * * @since 2.0.0 * * @param array $pung Array of URLs already pinged for the given post. */ $pung = apply_filters( 'get_pung', $pung ); return $pung; } |
関数 get_to_ping()
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 | /** * Retrieve URLs that need to be pinged. * * @since 1.5.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $post_id Post ID * @return array */ function get_to_ping( $post_id ) { global $wpdb; $to_ping = $wpdb->get_var( $wpdb->prepare( "SELECT to_ping FROM $wpdb->posts WHERE ID = %d", $post_id )); $to_ping = sanitize_trackback_urls( $to_ping ); $to_ping = preg_split('/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY); /** * Filter the list of URLs yet to ping for the given post. * * @since 2.0.0 * * @param array $to_ping List of URLs yet to ping. */ $to_ping = apply_filters( 'get_to_ping', $to_ping ); return $to_ping; } |