Changeset 23466 for trunk/wp-includes/post.php
- Timestamp:
- 02/21/2013 09:24:34 PM (13 years ago)
- File:
-
- 1 edited
-
trunk/wp-includes/post.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/post.php
r23449 r23466 752 752 753 753 return false; 754 }755 756 /**757 * Retrieve the format slug for a post758 *759 * @since 3.1.0760 *761 * @param int|object $post A post762 *763 * @return mixed The format if successful. False if no format is set. WP_Error if errors.764 */765 function get_post_format( $post = null ) {766 $post = get_post($post);767 768 if ( ! post_type_supports( $post->post_type, 'post-formats' ) )769 return false;770 771 $_format = get_the_terms( $post->ID, 'post_format' );772 773 if ( empty( $_format ) )774 return false;775 776 $format = array_shift( $_format );777 778 return ( str_replace('post-format-', '', $format->slug ) );779 }780 781 /**782 * Check if a post has a particular format783 *784 * @since 3.1.0785 * @uses has_term()786 *787 * @param string $format The format to check for788 * @param object|id $post The post to check. If not supplied, defaults to the current post if used in the loop.789 * @return bool True if the post has the format, false otherwise.790 */791 function has_post_format( $format, $post = null ) {792 return has_term('post-format-' . sanitize_key($format), 'post_format', $post);793 }794 795 /**796 * Assign a format to a post797 *798 * @since 3.1.0799 *800 * @param int|object $post The post for which to assign a format801 * @param string $format A format to assign. Use an empty string or array to remove all formats from the post.802 * @return mixed WP_Error on error. Array of affected term IDs on success.803 */804 function set_post_format( $post, $format ) {805 $post = get_post($post);806 807 if ( empty($post) )808 return new WP_Error('invalid_post', __('Invalid post'));809 810 if ( !empty($format) ) {811 $format = sanitize_key($format);812 if ( 'standard' == $format || !in_array( $format, array_keys( get_post_format_slugs() ) ) )813 $format = '';814 else815 $format = 'post-format-' . $format;816 }817 818 return wp_set_post_terms($post->ID, $format, 'post_format');819 754 } 820 755 … … 1952 1887 1953 1888 /** 1954 * Retrieve post format metadata for a post1955 *1956 * @since 3.6.01957 *1958 * @param int $post_id1959 * @return null1960 */1961 function get_post_format_meta( $post_id = 0 ) {1962 $values = array(1963 'quote' => '',1964 'quote_source' => '',1965 'image' => '',1966 'url' => '',1967 'gallery' => '',1968 'media' => '',1969 );1970 1971 foreach ( $values as $key => $value )1972 $values[$key] = get_post_meta( $post_id, '_wp_format_' . $key, true );1973 1974 return $values;1975 }1976 1977 /**1978 1889 * Check if post is sticky. 1979 1890 * … … 4951 4862 4952 4863 /** 4953 * Determines which fields of posts are to be saved in revisions.4954 *4955 * Does two things. If passed a post *array*, it will return a post array ready4956 * to be inserted into the posts table as a post revision. Otherwise, returns4957 * an array whose keys are the post fields to be saved for post revisions.4958 *4959 * @package WordPress4960 * @subpackage Post_Revisions4961 * @since 2.6.04962 * @access private4963 * @uses apply_filters() Calls '_wp_post_revision_fields' on 'title', 'content' and 'excerpt' fields.4964 *4965 * @param array $post Optional a post array to be processed for insertion as a post revision.4966 * @param bool $autosave optional Is the revision an autosave?4967 * @return array Post array ready to be inserted as a post revision or array of fields that can be versioned.4968 */4969 function _wp_post_revision_fields( $post = null, $autosave = false ) {4970 static $fields = false;4971 4972 if ( !$fields ) {4973 // Allow these to be versioned4974 $fields = array(4975 'post_title' => __( 'Title' ),4976 'post_content' => __( 'Content' ),4977 'post_excerpt' => __( 'Excerpt' ),4978 );4979 4980 // Runs only once4981 $fields = apply_filters( '_wp_post_revision_fields', $fields );4982 4983 // WP uses these internally either in versioning or elsewhere - they cannot be versioned4984 foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect )4985 unset( $fields[$protect] );4986 }4987 4988 if ( !is_array($post) )4989 return $fields;4990 4991 $return = array();4992 foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field )4993 $return[$field] = $post[$field];4994 4995 $return['post_parent'] = $post['ID'];4996 $return['post_status'] = 'inherit';4997 $return['post_type'] = 'revision';4998 $return['post_name'] = $autosave ? "$post[ID]-autosave" : "$post[ID]-revision";4999 $return['post_date'] = isset($post['post_modified']) ? $post['post_modified'] : '';5000 $return['post_date_gmt'] = isset($post['post_modified_gmt']) ? $post['post_modified_gmt'] : '';5001 5002 return $return;5003 }5004 5005 /**5006 * Saves an already existing post as a post revision.5007 *5008 * Typically used immediately prior to post updates.5009 *5010 * @package WordPress5011 * @subpackage Post_Revisions5012 * @since 2.6.05013 *5014 * @uses _wp_put_post_revision()5015 *5016 * @param int $post_id The ID of the post to save as a revision.5017 * @return mixed Null or 0 if error, new revision ID, if success.5018 */5019 function wp_save_post_revision( $post_id, $new_data = null ) {5020 // We do autosaves manually with wp_create_post_autosave()5021 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )5022 return;5023 5024 // WP_POST_REVISIONS = 0, false5025 if ( ! WP_POST_REVISIONS )5026 return;5027 5028 if ( !$post = get_post( $post_id, ARRAY_A ) )5029 return;5030 5031 if ( 'auto-draft' == $post['post_status'] )5032 return;5033 5034 if ( !post_type_supports($post['post_type'], 'revisions') )5035 return;5036 5037 // if new data is supplied, check that it is different from last saved revision, unless a plugin tells us to always save regardless5038 if ( apply_filters( 'wp_save_post_revision_check_for_changes', true, $post, $new_data ) && is_array( $new_data ) ) {5039 $post_has_changed = false;5040 foreach ( array_keys( _wp_post_revision_fields() ) as $field ) {5041 if ( normalize_whitespace( $new_data[ $field ] ) != normalize_whitespace( $post[ $field ] ) ) {5042 $post_has_changed = true;5043 break;5044 }5045 }5046 //don't save revision if post unchanged5047 if( ! $post_has_changed )5048 return;5049 }5050 5051 $return = _wp_put_post_revision( $post );5052 5053 // WP_POST_REVISIONS = true (default), -15054 if ( !is_numeric( WP_POST_REVISIONS ) || WP_POST_REVISIONS < 0 )5055 return $return;5056 5057 // all revisions and (possibly) one autosave5058 $revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) );5059 5060 // WP_POST_REVISIONS = (int) (# of autosaves to save)5061 $delete = count($revisions) - WP_POST_REVISIONS;5062 5063 if ( $delete < 1 )5064 return $return;5065 5066 $revisions = array_slice( $revisions, 0, $delete );5067 5068 for ( $i = 0; isset($revisions[$i]); $i++ ) {5069 if ( false !== strpos( $revisions[$i]->post_name, 'autosave' ) )5070 continue;5071 wp_delete_post_revision( $revisions[$i]->ID );5072 }5073 5074 return $return;5075 }5076 5077 /**5078 * Retrieve the autosaved data of the specified post.5079 *5080 * Returns a post object containing the information that was autosaved for the5081 * specified post.5082 *5083 * @package WordPress5084 * @subpackage Post_Revisions5085 * @since 2.6.05086 *5087 * @param int $post_id The post ID.5088 * @return object|bool The autosaved data or false on failure or when no autosave exists.5089 */5090 function wp_get_post_autosave( $post_id ) {5091 5092 if ( !$post = get_post( $post_id ) )5093 return false;5094 5095 $q = array(5096 'name' => "{$post->ID}-autosave",5097 'post_parent' => $post->ID,5098 'post_type' => 'revision',5099 'post_status' => 'inherit'5100 );5101 5102 // Use WP_Query so that the result gets cached5103 $autosave_query = new WP_Query;5104 5105 add_action( 'parse_query', '_wp_get_post_autosave_hack' );5106 $autosave = $autosave_query->query( $q );5107 remove_action( 'parse_query', '_wp_get_post_autosave_hack' );5108 5109 if ( $autosave && is_array($autosave) && is_object($autosave[0]) )5110 return $autosave[0];5111 5112 return false;5113 }5114 5115 /**5116 * Internally used to hack WP_Query into submission.5117 *5118 * @package WordPress5119 * @subpackage Post_Revisions5120 * @since 2.6.05121 *5122 * @param object $query WP_Query object5123 */5124 function _wp_get_post_autosave_hack( $query ) {5125 $query->is_single = false;5126 }5127 5128 /**5129 * Determines if the specified post is a revision.5130 *5131 * @package WordPress5132 * @subpackage Post_Revisions5133 * @since 2.6.05134 *5135 * @param int|object $post Post ID or post object.5136 * @return bool|int False if not a revision, ID of revision's parent otherwise.5137 */5138 function wp_is_post_revision( $post ) {5139 if ( !$post = wp_get_post_revision( $post ) )5140 return false;5141 return (int) $post->post_parent;5142 }5143 5144 /**5145 * Determines if the specified post is an autosave.5146 *5147 * @package WordPress5148 * @subpackage Post_Revisions5149 * @since 2.6.05150 *5151 * @param int|object $post Post ID or post object.5152 * @return bool|int False if not a revision, ID of autosave's parent otherwise5153 */5154 function wp_is_post_autosave( $post ) {5155 if ( !$post = wp_get_post_revision( $post ) )5156 return false;5157 if ( "{$post->post_parent}-autosave" !== $post->post_name )5158 return false;5159 return (int) $post->post_parent;5160 }5161 5162 /**5163 * Inserts post data into the posts table as a post revision.5164 *5165 * @package WordPress5166 * @subpackage Post_Revisions5167 * @since 2.6.05168 *5169 * @uses wp_insert_post()5170 *5171 * @param int|object|array $post Post ID, post object OR post array.5172 * @param bool $autosave Optional. Is the revision an autosave?5173 * @return mixed Null or 0 if error, new revision ID if success.5174 */5175 function _wp_put_post_revision( $post = null, $autosave = false ) {5176 if ( is_object($post) )5177 $post = get_object_vars( $post );5178 elseif ( !is_array($post) )5179 $post = get_post($post, ARRAY_A);5180 if ( !$post || empty($post['ID']) )5181 return;5182 5183 if ( isset($post['post_type']) && 'revision' == $post['post_type'] )5184 return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );5185 5186 $post = _wp_post_revision_fields( $post, $autosave );5187 5188 $revision_id = wp_insert_post( $post );5189 if ( is_wp_error($revision_id) )5190 return $revision_id;5191 5192 if ( $revision_id )5193 do_action( '_wp_put_post_revision', $revision_id );5194 return $revision_id;5195 }5196 5197 /**5198 * Gets a post revision.5199 *5200 * @package WordPress5201 * @subpackage Post_Revisions5202 * @since 2.6.05203 *5204 * @uses get_post()5205 *5206 * @param int|object $post Post ID or post object5207 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N.5208 * @param string $filter Optional sanitation filter. @see sanitize_post()5209 * @return mixed Null if error or post object if success5210 */5211 function wp_get_post_revision(&$post, $output = OBJECT, $filter = 'raw') {5212 $null = null;5213 if ( !$revision = get_post( $post, OBJECT, $filter ) )5214 return $revision;5215 if ( 'revision' !== $revision->post_type )5216 return $null;5217 5218 if ( $output == OBJECT ) {5219 return $revision;5220 } elseif ( $output == ARRAY_A ) {5221 $_revision = get_object_vars($revision);5222 return $_revision;5223 } elseif ( $output == ARRAY_N ) {5224 $_revision = array_values(get_object_vars($revision));5225 return $_revision;5226 }5227 5228 return $revision;5229 }5230 5231 /**5232 * Restores a post to the specified revision.5233 *5234 * Can restore a past revision using all fields of the post revision, or only selected fields.5235 *5236 * @package WordPress5237 * @subpackage Post_Revisions5238 * @since 2.6.05239 *5240 * @uses wp_get_post_revision()5241 * @uses wp_update_post()5242 * @uses do_action() Calls 'wp_restore_post_revision' on post ID and revision ID if wp_update_post()5243 * is successful.5244 *5245 * @param int|object $revision_id Revision ID or revision object.5246 * @param array $fields Optional. What fields to restore from. Defaults to all.5247 * @return mixed Null if error, false if no fields to restore, (int) post ID if success.5248 */5249 function wp_restore_post_revision( $revision_id, $fields = null ) {5250 if ( !$revision = wp_get_post_revision( $revision_id, ARRAY_A ) )5251 return $revision;5252 5253 if ( !is_array( $fields ) )5254 $fields = array_keys( _wp_post_revision_fields() );5255 5256 $update = array();5257 foreach( array_intersect( array_keys( $revision ), $fields ) as $field )5258 $update[$field] = $revision[$field];5259 5260 if ( !$update )5261 return false;5262 5263 $update['ID'] = $revision['post_parent'];5264 5265 $post_id = wp_update_post( $update );5266 if ( is_wp_error( $post_id ) )5267 return $post_id;5268 5269 if ( $post_id )5270 do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] );5271 5272 return $post_id;5273 }5274 5275 /**5276 * Deletes a revision.5277 *5278 * Deletes the row from the posts table corresponding to the specified revision.5279 *5280 * @package WordPress5281 * @subpackage Post_Revisions5282 * @since 2.6.05283 *5284 * @uses wp_get_post_revision()5285 * @uses wp_delete_post()5286 *5287 * @param int|object $revision_id Revision ID or revision object.5288 * @return mixed Null or WP_Error if error, deleted post if success.5289 */5290 function wp_delete_post_revision( $revision_id ) {5291 if ( !$revision = wp_get_post_revision( $revision_id ) )5292 return $revision;5293 5294 $delete = wp_delete_post( $revision->ID );5295 if ( is_wp_error( $delete ) )5296 return $delete;5297 5298 if ( $delete )5299 do_action( 'wp_delete_post_revision', $revision->ID, $revision );5300 5301 return $delete;5302 }5303 5304 /**5305 * Returns all revisions of specified post.5306 *5307 * @package WordPress5308 * @subpackage Post_Revisions5309 * @since 2.6.05310 *5311 * @uses get_children()5312 *5313 * @param int|object $post_id Post ID or post object5314 * @return array empty if no revisions5315 */5316 function wp_get_post_revisions( $post_id = 0, $args = null ) {5317 if ( ! WP_POST_REVISIONS )5318 return array();5319 if ( ( !$post = get_post( $post_id ) ) || empty( $post->ID ) )5320 return array();5321 5322 $defaults = array( 'order' => 'DESC', 'orderby' => 'date' );5323 $args = wp_parse_args( $args, $defaults );5324 $args = array_merge( $args, array( 'post_parent' => $post->ID, 'post_type' => 'revision', 'post_status' => 'inherit' ) );5325 5326 if ( !$revisions = get_children( $args ) )5327 return array();5328 return $revisions;5329 }5330 5331 function _set_preview($post) {5332 5333 if ( ! is_object($post) )5334 return $post;5335 5336 $preview = wp_get_post_autosave($post->ID);5337 5338 if ( ! is_object($preview) )5339 return $post;5340 5341 $preview = sanitize_post($preview);5342 5343 $post->post_content = $preview->post_content;5344 $post->post_title = $preview->post_title;5345 $post->post_excerpt = $preview->post_excerpt;5346 5347 return $post;5348 }5349 5350 function _show_post_preview() {5351 5352 if ( isset($_GET['preview_id']) && isset($_GET['preview_nonce']) ) {5353 $id = (int) $_GET['preview_id'];5354 5355 if ( false == wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) )5356 wp_die( __('You do not have permission to preview drafts.') );5357 5358 add_filter('the_preview', '_set_preview');5359 }5360 }5361 5362 /**5363 4864 * Returns the post's parent's post_ID 5364 4865 * … … 5417 4918 5418 4919 return $post_parent; 5419 }5420 5421 /**5422 * Returns an array of post format slugs to their translated and pretty display versions5423 *5424 * @since 3.1.05425 *5426 * @return array The array of translations5427 */5428 function get_post_format_strings() {5429 $strings = array(5430 'standard' => _x( 'Standard', 'Post format' ), // Special case. any value that evals to false will be considered standard5431 'aside' => _x( 'Aside', 'Post format' ),5432 'chat' => _x( 'Chat', 'Post format' ),5433 'gallery' => _x( 'Gallery', 'Post format' ),5434 'link' => _x( 'Link', 'Post format' ),5435 'image' => _x( 'Image', 'Post format' ),5436 'quote' => _x( 'Quote', 'Post format' ),5437 'status' => _x( 'Status', 'Post format' ),5438 'video' => _x( 'Video', 'Post format' ),5439 'audio' => _x( 'Audio', 'Post format' ),5440 );5441 return $strings;5442 }5443 5444 /**5445 * Retrieves an array of post format slugs.5446 *5447 * @since 3.1.05448 *5449 * @return array The array of post format slugs.5450 */5451 function get_post_format_slugs() {5452 $slugs = array_keys( get_post_format_strings() );5453 return array_combine( $slugs, $slugs );5454 }5455 5456 /**5457 * Returns a pretty, translated version of a post format slug5458 *5459 * @since 3.1.05460 *5461 * @param string $slug A post format slug5462 * @return string The translated post format name5463 */5464 function get_post_format_string( $slug ) {5465 $strings = get_post_format_strings();5466 if ( !$slug )5467 return $strings['standard'];5468 else5469 return ( isset( $strings[$slug] ) ) ? $strings[$slug] : '';5470 4920 } 5471 4921 … … 5507 4957 5508 4958 /** 5509 * Returns a link to a post format index.5510 *5511 * @since 3.1.05512 *5513 * @param string $format Post format5514 * @return string Link5515 */5516 function get_post_format_link( $format ) {5517 $term = get_term_by('slug', 'post-format-' . $format, 'post_format' );5518 if ( ! $term || is_wp_error( $term ) )5519 return false;5520 return get_term_link( $term );5521 }5522 5523 /**5524 4959 * Deletes auto-drafts for new posts that are > 7 days old 5525 4960 * … … 5534 4969 wp_delete_post( $delete, true ); // Force delete 5535 4970 } 5536 5537 /**5538 * Filters the request to allow for the format prefix.5539 *5540 * @access private5541 * @since 3.1.05542 */5543 function _post_format_request( $qvs ) {5544 if ( ! isset( $qvs['post_format'] ) )5545 return $qvs;5546 $slugs = get_post_format_slugs();5547 if ( isset( $slugs[ $qvs['post_format'] ] ) )5548 $qvs['post_format'] = 'post-format-' . $slugs[ $qvs['post_format'] ];5549 $tax = get_taxonomy( 'post_format' );5550 if ( ! is_admin() )5551 $qvs['post_type'] = $tax->object_type;5552 return $qvs;5553 }5554 add_filter( 'request', '_post_format_request' );5555 5556 /**5557 * Filters the post format term link to remove the format prefix.5558 *5559 * @access private5560 * @since 3.1.05561 */5562 function _post_format_link( $link, $term, $taxonomy ) {5563 global $wp_rewrite;5564 if ( 'post_format' != $taxonomy )5565 return $link;5566 if ( $wp_rewrite->get_extra_permastruct( $taxonomy ) ) {5567 return str_replace( "/{$term->slug}", '/' . str_replace( 'post-format-', '', $term->slug ), $link );5568 } else {5569 $link = remove_query_arg( 'post_format', $link );5570 return add_query_arg( 'post_format', str_replace( 'post-format-', '', $term->slug ), $link );5571 }5572 }5573 add_filter( 'term_link', '_post_format_link', 10, 3 );5574 5575 /**5576 * Remove the post format prefix from the name property of the term object created by get_term().5577 *5578 * @access private5579 * @since 3.1.05580 */5581 function _post_format_get_term( $term ) {5582 if ( isset( $term->slug ) ) {5583 $term->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );5584 }5585 return $term;5586 }5587 add_filter( 'get_post_format', '_post_format_get_term' );5588 5589 /**5590 * Remove the post format prefix from the name property of the term objects created by get_terms().5591 *5592 * @access private5593 * @since 3.1.05594 */5595 function _post_format_get_terms( $terms, $taxonomies, $args ) {5596 if ( in_array( 'post_format', (array) $taxonomies ) ) {5597 if ( isset( $args['fields'] ) && 'names' == $args['fields'] ) {5598 foreach( $terms as $order => $name ) {5599 $terms[$order] = get_post_format_string( str_replace( 'post-format-', '', $name ) );5600 }5601 } else {5602 foreach ( (array) $terms as $order => $term ) {5603 if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {5604 $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );5605 }5606 }5607 }5608 }5609 return $terms;5610 }5611 add_filter( 'get_terms', '_post_format_get_terms', 10, 3 );5612 5613 /**5614 * Remove the post format prefix from the name property of the term objects created by wp_get_object_terms().5615 *5616 * @access private5617 * @since 3.1.05618 */5619 function _post_format_wp_get_object_terms( $terms ) {5620 foreach ( (array) $terms as $order => $term ) {5621 if ( isset( $term->taxonomy ) && 'post_format' == $term->taxonomy ) {5622 $terms[$order]->name = get_post_format_string( str_replace( 'post-format-', '', $term->slug ) );5623 }5624 }5625 return $terms;5626 }5627 add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );5628 4971 5629 4972 /**
Note: See TracChangeset
for help on using the changeset viewer.