From 52b3f498e62789b99dd3f1446b4b867c1910c9dd Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 28 Aug 2012 19:08:28 +0000 Subject: [PATCH] Add tags_input, page_template, and post_category get magic to WP_Post. Deprecate get_post_to_edit() and wp_get_single_post(). Props scribu see #21309 git-svn-id: http://core.svn.wordpress.org/trunk@21651 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/deprecated.php | 20 +++++- wp-admin/includes/post.php | 20 +----- wp-admin/post.php | 2 +- wp-includes/class-wp-atom-server.php | 4 +- wp-includes/class-wp-xmlrpc-server.php | 26 ++++---- wp-includes/deprecated.php | 22 ++++++- wp-includes/post.php | 91 ++++++++++++-------------- 7 files changed, 99 insertions(+), 86 deletions(-) diff --git a/wp-admin/includes/deprecated.php b/wp-admin/includes/deprecated.php index 6a66e43b16..bc49030af6 100644 --- a/wp-admin/includes/deprecated.php +++ b/wp-admin/includes/deprecated.php @@ -943,7 +943,7 @@ function current_theme_info() { /** * This was once used to display an 'Insert into Post' button. Now it is deprecated and stubbed. - * + * * @deprecated 3.5.0 */ function _insert_into_post_button( $type ) { @@ -952,9 +952,25 @@ function _insert_into_post_button( $type ) { /** * This was once used to display a media button. Now it is deprecated and stubbed. - * + * * @deprecated 3.5.0 */ function _media_button($title, $icon, $type, $id) { _deprecated_function( __FUNCTION__, '3.5' ); } + +/** + * Get an existing post and format it for editing. + * + * @since 2.0.0 + * @deprecated 3.5.0 + * + * @param int $id + * @return object + */ +function get_post_to_edit( $id ) { + _deprecated_function( __FUNCTION__, '3.5', 'get_post()' ); + + return get_post( $id, OBJECT, 'edit' ); +} + diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index b9ec2141d9..c49d195da7 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -463,24 +463,6 @@ function get_default_page_to_edit() { return $page; } -/** - * Get an existing post and format it for editing. - * - * @since 2.0.0 - * - * @param int $id - * @return WP_Post - */ -function get_post_to_edit( $id ) { - - $post = get_post( $id, OBJECT, 'edit' ); - - if ( $post->post_type == 'page' ) - $post->page_template = get_post_meta( $id, '_wp_page_template', true ); - - return $post; -} - /** * Determine if a post exists based on title, content, and date * @@ -928,7 +910,7 @@ function get_available_post_mime_types($type = 'attachment') { /** * Executes a query for attachments. An array of WP_Query arguments * can be passed in, which will override the arguments set by this function. - * + * * @since 2.5.0 * @uses apply_filters() Calls 'upload_per_page' on posts_per_page argument * diff --git a/wp-admin/post.php b/wp-admin/post.php index 216d157bbf..de5080b81d 100644 --- a/wp-admin/post.php +++ b/wp-admin/post.php @@ -165,7 +165,7 @@ case 'edit': } $title = $post_type_object->labels->edit_item; - $post = get_post_to_edit($post_id); + $post = get_post($post_id, OBJECT, 'edit'); if ( post_type_supports($post_type, 'comments') ) { wp_enqueue_script('admin-comments'); diff --git a/wp-includes/class-wp-atom-server.php b/wp-includes/class-wp-atom-server.php index d3816732cc..ca61ae73e3 100644 --- a/wp-includes/class-wp-atom-server.php +++ b/wp-includes/class-wp-atom-server.php @@ -913,7 +913,7 @@ EOD; $this->not_found(); } - $entry = wp_get_single_post($postID,ARRAY_A); + $entry = get_post($postID,ARRAY_A); if (!isset($entry) || !isset($entry['ID'])) $this->not_found(); @@ -1410,7 +1410,7 @@ EOD; switch($this->params[0]) { case $this->ENTRY_PATH: global $post; - $post = wp_get_single_post($this->params[1]); + $post = get_post($this->params[1]); $wp_last_modified = get_post_modified_time('D, d M Y H:i:s', true); $post = null; break; diff --git a/wp-includes/class-wp-xmlrpc-server.php b/wp-includes/class-wp-xmlrpc-server.php index a51cf08fb5..a5273f9f72 100644 --- a/wp-includes/class-wp-xmlrpc-server.php +++ b/wp-includes/class-wp-xmlrpc-server.php @@ -1256,7 +1256,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action( 'xmlrpc_call', 'wp.deletePost' ); - $post = wp_get_single_post( $post_id, ARRAY_A ); + $post = get_post( $post_id, ARRAY_A ); if ( empty( $post['ID'] ) ) return new IXR_Error( 404, __( 'Invalid post ID.' ) ); @@ -1286,7 +1286,7 @@ class wp_xmlrpc_server extends IXR_Server { * groups are 'post' (all basic fields), 'taxonomies', 'custom_fields', * and 'enclosure'. * - * @uses wp_get_single_post() + * @uses get_post() * @param array $args Method parameters. Contains: * - int $post_id * - string $username @@ -1337,7 +1337,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action( 'xmlrpc_call', 'wp.getPost' ); - $post = wp_get_single_post( $post_id, ARRAY_A ); + $post = get_post( $post_id, ARRAY_A ); if ( empty( $post['ID'] ) ) return new IXR_Error( 404, __( 'Invalid post ID.' ) ); @@ -2060,7 +2060,7 @@ class wp_xmlrpc_server extends IXR_Server { // Get the current page based on the page_id and // make sure it is a page and not a post. - $actual_page = wp_get_single_post($page_id, ARRAY_A); + $actual_page = get_post($page_id, ARRAY_A); if ( !$actual_page || ($actual_page['post_type'] != 'page') ) return(new IXR_Error(404, __('Sorry, no such page.'))); @@ -2101,7 +2101,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action('xmlrpc_call', 'wp.editPage'); // Get the page data and make sure it is a page. - $actual_page = wp_get_single_post($page_id, ARRAY_A); + $actual_page = get_post($page_id, ARRAY_A); if ( !$actual_page || ($actual_page['post_type'] != 'page') ) return(new IXR_Error(404, __('Sorry, no such page.'))); @@ -3296,7 +3296,7 @@ class wp_xmlrpc_server extends IXR_Server { if ( !$user = $this->login($username, $password) ) return $this->error; - $post_data = wp_get_single_post($post_ID, ARRAY_A); + $post_data = get_post($post_ID, ARRAY_A); if ( ! $post_data ) return new IXR_Error( 404, __( 'Invalid post ID.' ) ); @@ -3538,7 +3538,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action('xmlrpc_call', 'blogger.editPost'); - $actual_post = wp_get_single_post($post_ID,ARRAY_A); + $actual_post = get_post($post_ID,ARRAY_A); if ( !$actual_post || $actual_post['post_type'] != 'post' ) return new IXR_Error(404, __('Sorry, no such post.')); @@ -3592,7 +3592,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action('xmlrpc_call', 'blogger.deletePost'); - $actual_post = wp_get_single_post($post_ID,ARRAY_A); + $actual_post = get_post($post_ID,ARRAY_A); if ( !$actual_post || $actual_post['post_type'] != 'post' ) return new IXR_Error(404, __('Sorry, no such post.')); @@ -3982,7 +3982,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action('xmlrpc_call', 'metaWeblog.editPost'); - $postdata = wp_get_single_post( $post_ID, ARRAY_A ); + $postdata = get_post( $post_ID, ARRAY_A ); // If there is no post data for the give post id, stop // now and return an error. Other wise a new post will be @@ -4247,7 +4247,7 @@ class wp_xmlrpc_server extends IXR_Server { if ( !$user = $this->login($username, $password) ) return $this->error; - $postdata = wp_get_single_post($post_ID, ARRAY_A); + $postdata = get_post($post_ID, ARRAY_A); if ( ! $postdata ) return new IXR_Error( 404, __( 'Invalid post ID.' ) ); @@ -4827,7 +4827,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action('xmlrpc_call', 'mt.getTrackbackPings'); - $actual_post = wp_get_single_post($post_ID, ARRAY_A); + $actual_post = get_post($post_ID, ARRAY_A); if ( !$actual_post ) return new IXR_Error(404, __('Sorry, no such post.')); @@ -4874,7 +4874,7 @@ class wp_xmlrpc_server extends IXR_Server { do_action('xmlrpc_call', 'mt.publishPost'); - $postdata = wp_get_single_post($post_ID, ARRAY_A); + $postdata = get_post($post_ID, ARRAY_A); if ( ! $postdata ) return new IXR_Error( 404, __( 'Invalid post ID.' ) ); @@ -5088,7 +5088,7 @@ class wp_xmlrpc_server extends IXR_Server { return new IXR_Error(33, __('The specified target URL cannot be used as a target. It either doesn’t exist, or it is not a pingback-enabled resource.')); } - $actual_post = wp_get_single_post($post_ID, ARRAY_A); + $actual_post = get_post($post_ID, ARRAY_A); if ( !$actual_post ) { // No such post = resource not found diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 178afb9fc6..8718586b9b 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -3203,4 +3203,24 @@ function sticky_class( $post_id = null ) { */ function _get_post_ancestors( &$post ) { _deprecated_function( __FUNCTION__, '3.5' ); -} \ No newline at end of file +} + +/** + * Retrieve a single post, based on post ID. + * + * Has categories in 'post_category' property or key. Has tags in 'tags_input' + * property or key. + * + * @since 1.0.0 + * @deprecated 3.5.0 + * @see get_post() + * + * @param int $postid Post ID. + * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A. + * @return object|array Post object or array holding post contents and information + */ +function wp_get_single_post( $postid = 0, $mode = OBJECT ) { + _deprecated_function( __FUNCTION__, '3.5', 'get_post()' ); + return get_post( $postid, $mode, 'edit' ); +} + diff --git a/wp-includes/post.php b/wp-includes/post.php index 94c410589d..01f108b13b 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -475,10 +475,44 @@ final class WP_Post { if ( 'ancestors' == $key ) return true; + if ( 'page_template' == $key ) + return ( 'page' == $this->post_type ); + + if ( 'post_category' == $key ) + return true; + + if ( 'tags_input' == $key ) + return true; + return metadata_exists( 'post', $this->ID, $key ); } public function &__get( $key ) { + if ( 'page_template' == $key && $this->__isset( $key ) ) { + $_ref = get_post_meta( $this->ID, '_wp_page_template', true ); + } + + if ( 'post_category' == $key ) { + if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) { + $_ref = wp_get_post_categories( $this->ID ); + } else { + $_ref = array(); + } + } + + if ( 'tags_input' == $key ) { + if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) { + $_ref = wp_get_post_tags( $this->ID, array( 'fields' => 'names' ) ); + } else { + $_ref = array(); + } + } + + if ( isset( $_ref ) ) + return $_ref; + + // Rest of the values need filtering + if ( 'ancestors' == $key ) { $value = get_post_ancestors( $this ); } else { @@ -504,7 +538,11 @@ final class WP_Post { public function to_array() { $post = get_object_vars( $this ); - $post['ancestors'] = array(); + + foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) { + if ( $this->__isset( $key ) ) + $post[ $key ] = $this->__get( $key ); + } return $post; } @@ -2226,7 +2264,7 @@ function wp_trash_post($post_id = 0) { if ( !EMPTY_TRASH_DAYS ) return wp_delete_post($post_id, true); - if ( !$post = wp_get_single_post($post_id, ARRAY_A) ) + if ( !$post = get_post($post_id, ARRAY_A) ) return $post; if ( $post['post_status'] == 'trash' ) @@ -2258,7 +2296,7 @@ function wp_trash_post($post_id = 0) { * @return mixed False on failure */ function wp_untrash_post($post_id = 0) { - if ( !$post = wp_get_single_post($post_id, ARRAY_A) ) + if ( !$post = get_post($post_id, ARRAY_A) ) return $post; if ( $post['post_status'] != 'trash' ) @@ -2488,49 +2526,6 @@ function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { } -/** - * Retrieve a single post, based on post ID. - * - * Has categories in 'post_category' property or key. Has tags in 'tags_input' - * property or key. - * - * @since 1.0.0 - * - * @param int $postid Post ID. - * @param string $mode How to return result, either OBJECT, ARRAY_N, or ARRAY_A. - * @return WP_Post|array WP_Post object or array holding post contents and information - */ -function wp_get_single_post($postid = 0, $mode = OBJECT) { - $postid = (int) $postid; - - $post = get_post($postid, $mode); - - if ( - ( OBJECT == $mode && empty( $post->ID ) ) || - ( OBJECT != $mode && empty( $post['ID'] ) ) - ) - return ( OBJECT == $mode ? null : array() ); - - // Set categories and tags - if ( $mode == OBJECT ) { - $post->post_category = array(); - if ( is_object_in_taxonomy($post->post_type, 'category') ) - $post->post_category = wp_get_post_categories($postid); - $post->tags_input = array(); - if ( is_object_in_taxonomy($post->post_type, 'post_tag') ) - $post->tags_input = wp_get_post_tags($postid, array('fields' => 'names')); - } else { - $post['post_category'] = array(); - if ( is_object_in_taxonomy($post['post_type'], 'category') ) - $post['post_category'] = wp_get_post_categories($postid); - $post['tags_input'] = array(); - if ( is_object_in_taxonomy($post['post_type'], 'post_tag') ) - $post['tags_input'] = wp_get_post_tags($postid, array('fields' => 'names')); - } - - return $post; -} - /** * Insert a post. * @@ -2832,7 +2827,7 @@ function wp_update_post( $postarr = array(), $wp_error = false ) { } // First, get all of the original fields - $post = wp_get_single_post($postarr['ID'], ARRAY_A); + $post = get_post($postarr['ID'], ARRAY_A); // Escape data pulled from DB. $post = add_magic_quotes($post); @@ -3241,7 +3236,7 @@ function get_to_ping($post_id) { function trackback_url_list($tb_list, $post_id) { if ( ! empty( $tb_list ) ) { // get post data - $postdata = wp_get_single_post($post_id, ARRAY_A); + $postdata = get_post($post_id, ARRAY_A); // import postdata as variables extract($postdata, EXTR_SKIP);