From cfb35a9d85662dd27c8d4829167305140bf17e22 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Tue, 4 Sep 2012 16:29:28 +0000 Subject: [PATCH] Use get_post() instead of global $post. Make the $post argument to get_post() optional, defaulting to the current post in The Loop. Props nacin see #21309 git-svn-id: http://core.svn.wordpress.org/trunk@21735 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../includes/class-wp-comments-list-table.php | 8 +- .../includes/class-wp-media-list-table.php | 3 +- .../includes/class-wp-posts-list-table.php | 14 +-- wp-admin/includes/export.php | 2 +- wp-admin/includes/meta-boxes.php | 11 +-- wp-admin/includes/post.php | 18 ++-- wp-admin/includes/template.php | 36 +++---- wp-includes/admin-bar.php | 5 +- wp-includes/author-template.php | 6 +- wp-includes/category-template.php | 24 ++--- wp-includes/class-wp-atom-server.php | 8 +- wp-includes/class-wp-editor.php | 9 +- wp-includes/comment-template.php | 7 +- wp-includes/deprecated.php | 4 +- wp-includes/general-template.php | 13 ++- wp-includes/link-template.php | 93 +++++++++---------- wp-includes/media.php | 23 +++-- wp-includes/post-template.php | 46 ++++----- wp-includes/post.php | 19 ++-- 19 files changed, 160 insertions(+), 189 deletions(-) diff --git a/wp-admin/includes/class-wp-comments-list-table.php b/wp-admin/includes/class-wp-comments-list-table.php index 1b194113be..65fb5ba889 100644 --- a/wp-admin/includes/class-wp-comments-list-table.php +++ b/wp-admin/includes/class-wp-comments-list-table.php @@ -302,7 +302,8 @@ class WP_Comments_List_Table extends WP_List_Table { } function single_row( $a_comment ) { - global $post, $comment; + global $comment; + $post = get_post(); $comment = $a_comment; $the_comment_class = join( ' ', get_comment_class( wp_get_comment_status( $comment->comment_ID ) ) ); @@ -325,7 +326,8 @@ class WP_Comments_List_Table extends WP_List_Table { } function column_comment( $comment ) { - global $post, $comment_status; + global $comment_status; + $post = get_post(); $user_can = $this->user_can; @@ -479,7 +481,7 @@ class WP_Comments_List_Table extends WP_List_Table { } function column_response( $comment ) { - global $post; + $post = get_post(); if ( isset( $this->pending_count[$post->ID] ) ) { $pending_comments = $this->pending_count[$post->ID]; diff --git a/wp-admin/includes/class-wp-media-list-table.php b/wp-admin/includes/class-wp-media-list-table.php index 2f959b8dd4..96fe45d6b7 100644 --- a/wp-admin/includes/class-wp-media-list-table.php +++ b/wp-admin/includes/class-wp-media-list-table.php @@ -156,7 +156,8 @@ class WP_Media_List_Table extends WP_List_Table { } function display_rows() { - global $post, $id; + $post = get_post(); + $id = $post->ID; add_filter( 'the_title','esc_html' ); $alt = ''; diff --git a/wp-admin/includes/class-wp-posts-list-table.php b/wp-admin/includes/class-wp-posts-list-table.php index f3f10b0a41..b58a5715c1 100644 --- a/wp-admin/includes/class-wp-posts-list-table.php +++ b/wp-admin/includes/class-wp-posts-list-table.php @@ -316,7 +316,7 @@ class WP_Posts_List_Table extends WP_List_Table { } function _display_rows( $posts, $level = 0 ) { - global $post, $mode; + global $mode; // Create array of post IDs. $post_ids = array(); @@ -458,12 +458,12 @@ class WP_Posts_List_Table extends WP_List_Table { unset( $children_pages[$parent] ); //required in order to keep track of orphans } - function single_row( $a_post, $level = 0 ) { - global $post, $mode; + function single_row( $post, $level = 0 ) { + global $mode; static $alternate; - $global_post = $post; - $post = $a_post; + $global_post = get_post(); + $GLOBALS['post'] = $post; setup_postdata( $post ); $edit_link = get_edit_post_link( $post->ID ); @@ -529,7 +529,7 @@ class WP_Posts_List_Table extends WP_List_Table { } else { $attributes = 'class="post-title page-title column-title"' . $style; - + $pad = str_repeat( '— ', $level ); ?> >post_status != 'trash' ) { ?> @@ -684,7 +684,7 @@ class WP_Posts_List_Table extends WP_List_Table { ?> post_type ); if ( empty( $taxonomies ) ) diff --git a/wp-admin/includes/meta-boxes.php b/wp-admin/includes/meta-boxes.php index b09e201f4b..c4a095e24d 100644 --- a/wp-admin/includes/meta-boxes.php +++ b/wp-admin/includes/meta-boxes.php @@ -466,15 +466,15 @@ function post_comment_meta_box_thead($result) { * * @param object $post */ -function post_comment_meta_box($post) { - global $wpdb, $post_ID; +function post_comment_meta_box( $post ) { + global $wpdb; wp_nonce_field( 'get-comments', 'add_comment_nonce', false ); ?> -

+

$post_ID, 'number' => 1, 'count' => true ) ); + $total = get_comments( array( 'post_id' => $post->ID, 'number' => 1, 'count' => true ) ); $wp_list_table = _get_list_table('WP_Post_Comments_List_Table'); $wp_list_table->display( true ); @@ -910,8 +910,7 @@ function link_advanced_meta_box($link) { * * @since 2.9.0 */ -function post_thumbnail_meta_box() { - global $post; +function post_thumbnail_meta_box( $post ) { $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); echo _wp_post_thumbnail_html( $thumbnail_id ); } diff --git a/wp-admin/includes/post.php b/wp-admin/includes/post.php index c49d195da7..0a7a1a4e4e 100644 --- a/wp-admin/includes/post.php +++ b/wp-admin/includes/post.php @@ -1121,16 +1121,15 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { * @since 2.9.0 * * @param int $thumbnail_id ID of the attachment used for thumbnail - * @param int $post_id ID of the post associated with the thumbnail, defaults to global $post_ID + * @param mixed $post The post ID or object associated with the thumbnail, defaults to global $post. * @return string html */ -function _wp_post_thumbnail_html( $thumbnail_id = null, $post_id = null ) { - global $content_width, $_wp_additional_image_sizes, $post_ID; +function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { + global $content_width, $_wp_additional_image_sizes; - if ( empty( $post_id ) ) - $post_id = $post_ID; + $post = get_post( $post ); - $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post_id) ); + $upload_iframe_src = esc_url( get_upload_iframe_src('image', $post->ID ) ); $set_thumbnail_link = '

%s

'; $content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) ); @@ -1142,14 +1141,14 @@ function _wp_post_thumbnail_html( $thumbnail_id = null, $post_id = null ) { else $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' ); if ( !empty( $thumbnail_html ) ) { - $ajax_nonce = wp_create_nonce( "set_post_thumbnail-$post_id" ); + $ajax_nonce = wp_create_nonce( 'set_post_thumbnail-' . $post->ID ); $content = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html ); $content .= '

' . esc_html__( 'Remove featured image' ) . '

'; } $content_width = $old_content_width; } - return apply_filters( 'admin_post_thumbnail_html', $content, $post_id ); + return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID ); } /** @@ -1207,8 +1206,7 @@ function wp_set_post_lock( $post_id ) { * @return none */ function _admin_notice_post_locked() { - global $post; - + $post = get_post(); $lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) ); $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); $last_user = get_userdata( $user ); diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index bdc1212386..2825e4ebb1 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -166,10 +166,9 @@ function wp_terms_checklist($post_id = 0, $args = array()) { * @return array List of popular term IDs. */ function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) { - global $post_ID; - - if ( $post_ID ) - $checked_terms = wp_get_object_terms($post_ID, $taxonomy, array('fields'=>'ids')); + $post = get_post(); + if ( $post->ID ) + $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids')); else $checked_terms = array(); @@ -575,7 +574,8 @@ function meta_form() { * @param unknown_type $multi */ function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) { - global $wp_locale, $post, $comment; + global $wp_locale, $comment; + $post = get_post(); if ( $for_post ) $edit = ! ( in_array($post->post_status, array('draft', 'pending') ) && (!$post->post_date_gmt || '0000-00-00 00:00:00' == $post->post_date_gmt ) ); @@ -670,17 +670,16 @@ function page_template_dropdown( $default = '' ) { * @return unknown */ function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) { - global $wpdb, $post_ID; + global $wpdb; + $post = get_post(); $items = $wpdb->get_results( $wpdb->prepare("SELECT ID, post_parent, post_title FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'page' ORDER BY menu_order", $parent) ); if ( $items ) { foreach ( $items as $item ) { // A page cannot be its own parent. - if (!empty ( $post_ID ) ) { - if ( $item->ID == $post_ID ) { - continue; - } - } + if ( $post->ID && $item->ID == $post->ID ) + continue; + $pad = str_repeat( ' ', $level * 3 ); if ( $item->ID == $default) $current = ' selected="selected"'; @@ -1345,8 +1344,9 @@ function find_posts_div($found_action = '') { * @since 2.7.0 */ function the_post_password() { - global $post; - if ( isset( $post->post_password ) ) echo esc_attr( $post->post_password ); + $post = get_post(); + if ( isset( $post->post_password ) ) + echo esc_attr( $post->post_password ); } /** @@ -1356,13 +1356,13 @@ function the_post_password() { * returned. * * @since 2.7.0 - * @param int $post_id The post id. If not supplied the global $post is used. + * @param mixed $post Post id or object. If not supplied the global $post is used. * @return string The post title if set */ -function _draft_or_post_title( $post_id = 0 ) { - $title = get_the_title($post_id); - if ( empty($title) ) - $title = __('(no title)'); +function _draft_or_post_title( $post = 0 ) { + $title = get_the_title( $post ); + if ( empty( $title ) ) + $title = __( '(no title)' ); return $title; } diff --git a/wp-includes/admin-bar.php b/wp-includes/admin-bar.php index 4e3b9cfe1a..e733a1844c 100644 --- a/wp-includes/admin-bar.php +++ b/wp-includes/admin-bar.php @@ -419,10 +419,11 @@ function wp_admin_bar_shortlink_menu( $wp_admin_bar ) { * @since 3.1.0 */ function wp_admin_bar_edit_menu( $wp_admin_bar ) { - global $post, $tag, $wp_the_query; + global $tag, $wp_the_query; if ( is_admin() ) { $current_screen = get_current_screen(); + $post = get_post(); if ( 'post' == $current_screen->base && 'add' != $current_screen->action @@ -619,7 +620,7 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) { return; $title = '' . number_format_i18n( $update_data['counts']['total'] ) . ''; - $title .= '' . $update_data['title'] . ''; + $title .= '' . $update_data['title'] . ''; $wp_admin_bar->add_menu( array( 'id' => 'updates', diff --git a/wp-includes/author-template.php b/wp-includes/author-template.php index ab2737c563..1e65baa1e6 100644 --- a/wp-includes/author-template.php +++ b/wp-includes/author-template.php @@ -69,8 +69,7 @@ function the_author( $deprecated = '', $deprecated_echo = true ) { * @return string The author's display name. */ function get_the_modified_author() { - global $post; - if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) { + if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) { $last_user = get_userdata($last_id); return apply_filters('the_modified_author', $last_user->display_name); } @@ -164,8 +163,7 @@ function the_author_link() { * @return int The number of posts by the author. */ function get_the_author_posts() { - global $post; - return count_user_posts($post->post_author); + return count_user_posts( get_post()->post_author ); } /** diff --git a/wp-includes/category-template.php b/wp-includes/category-template.php index 2e23f8a3fe..c4ba37d0d9 100644 --- a/wp-includes/category-template.php +++ b/wp-includes/category-template.php @@ -1054,29 +1054,21 @@ function term_description( $term = 0, $taxonomy = 'post_tag' ) { * * @since 2.5.0 * - * @param int $id Post ID. + * @param mixed $post Post ID or object. * @param string $taxonomy Taxonomy name. * @return array|bool False on failure. Array of term objects on success. */ -function get_the_terms( $id, $taxonomy ) { - global $post; +function get_the_terms( $post, $taxonomy ) { + if ( ! $post = get_post( $post ) ) + return false; - $id = (int) $id; - - if ( !$id ) { - if ( empty( $post->ID ) ) - return false; - else - $id = (int) $post->ID; - } - - $terms = get_object_term_cache( $id, $taxonomy ); + $terms = get_object_term_cache( $post->ID, $taxonomy ); if ( false === $terms ) { - $terms = wp_get_object_terms( $id, $taxonomy ); - wp_cache_add($id, $terms, $taxonomy . '_relationships'); + $terms = wp_get_object_terms( $post->ID, $taxonomy ); + wp_cache_add($post->ID, $terms, $taxonomy . '_relationships'); } - $terms = apply_filters( 'get_the_terms', $terms, $id, $taxonomy ); + $terms = apply_filters( 'get_the_terms', $terms, $post->ID, $taxonomy ); if ( empty( $terms ) ) return false; diff --git a/wp-includes/class-wp-atom-server.php b/wp-includes/class-wp-atom-server.php index ca61ae73e3..16c3d9a2da 100644 --- a/wp-includes/class-wp-atom-server.php +++ b/wp-includes/class-wp-atom-server.php @@ -846,10 +846,9 @@ EOD; * @param int $postID Post ID. * @return string */ - function get_entry_url($postID = null) { + function get_entry_url( $postID = null ) { if (!isset($postID)) { - global $post; - $postID = (int) $post->ID; + $postID = (int) get_post()->ID; } $url = $this->app_base . $this->ENTRY_PATH . "/$postID"; @@ -878,8 +877,7 @@ EOD; */ function get_media_url($postID = null) { if (!isset($postID)) { - global $post; - $postID = (int) $post->ID; + $postID = (int) get_post()->ID; } $url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID"; diff --git a/wp-includes/class-wp-editor.php b/wp-includes/class-wp-editor.php index f2f5689f86..e2dda8e3f4 100644 --- a/wp-includes/class-wp-editor.php +++ b/wp-includes/class-wp-editor.php @@ -126,7 +126,7 @@ final class _WP_Editors { } public static function editor_settings($editor_id, $set) { - global $editor_styles, $post; + global $editor_styles; $first_run = false; if ( empty(self::$first_init) ) { @@ -370,8 +370,8 @@ final class _WP_Editors { $body_class = $editor_id; - if ( isset($post) ) - $body_class .= " post-type-$post->post_type"; + if ( $post = get_post() ) + $body_class .= ' post-type-' . $post->post_type; if ( !empty($set['tinymce']['body_class']) ) { $body_class .= ' ' . $set['tinymce']['body_class']; @@ -612,7 +612,8 @@ final class _WP_Editors { } public static function wp_fullscreen_html() { - global $content_width, $post; + global $content_width; + $post = get_post(); $width = isset($content_width) && 800 > $content_width ? $content_width : 800; $width = $width + 22; // compensate for the padding and border diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index c6f8c455a7..00ce175367 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -810,11 +810,8 @@ function pings_open( $post_id = null ) { * @uses $post Gets the ID of the current post for the token */ function wp_comment_form_unfiltered_html_nonce() { - global $post; - - $post_id = 0; - if ( !empty($post) ) - $post_id = $post->ID; + $post = get_post(); + $post_id = $post ? $post->ID : 0; if ( current_user_can( 'unfiltered_html' ) ) { wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false ); diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 8718586b9b..da8bb1df26 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -57,14 +57,14 @@ function get_postdata($postid) { * @deprecated Use The Loop - {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop} */ function start_wp() { - global $wp_query, $post; + global $wp_query; _deprecated_function( __FUNCTION__, '1.5', __('new WordPress Loop') ); // Since the old style loop is being used, advance the query iterator here. $wp_query->next_post(); - setup_postdata($post); + setup_postdata( get_post() ); } /** diff --git a/wp-includes/general-template.php b/wp-includes/general-template.php index 1992a7a58e..20b401cbd0 100644 --- a/wp-includes/general-template.php +++ b/wp-includes/general-template.php @@ -1311,8 +1311,7 @@ function allowed_tags() { * @since 1.0.0 */ function the_date_xml() { - global $post; - echo mysql2date('Y-m-d', $post->post_date, false); + echo mysql2date( 'Y-m-d', get_post()->post_date, false ); } /** @@ -1367,7 +1366,7 @@ function the_date( $d = '', $before = '', $after = '', $echo = true ) { * @return string|null Null if displaying, string if retrieving. */ function get_the_date( $d = '' ) { - global $post; + $post = get_post(); $the_date = ''; if ( '' == $d ) @@ -1528,8 +1527,8 @@ function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translat * @uses $post */ function the_weekday() { - global $wp_locale, $post; - $the_weekday = $wp_locale->get_weekday(mysql2date('w', $post->post_date, false)); + global $wp_locale; + $the_weekday = $wp_locale->get_weekday( mysql2date( 'w', get_post()->post_date, false ) ); $the_weekday = apply_filters('the_weekday', $the_weekday); echo $the_weekday; } @@ -1546,11 +1545,11 @@ function the_weekday() { * @param string $after Optional Output after the date. */ function the_weekday_date($before='',$after='') { - global $wp_locale, $post, $day, $previousweekday; + global $wp_locale, $day, $previousweekday; $the_weekday_date = ''; if ( $currentday != $previousweekday ) { $the_weekday_date .= $before; - $the_weekday_date .= $wp_locale->get_weekday(mysql2date('w', $post->post_date, false)); + $the_weekday_date .= $wp_locale->get_weekday( mysql2date( 'w', get_post()->post_date, false ) ); $the_weekday_date .= $after; $previousweekday = $currentday; } diff --git a/wp-includes/link-template.php b/wp-includes/link-template.php index 0fa27fde8c..5761036a8a 100644 --- a/wp-includes/link-template.php +++ b/wp-includes/link-template.php @@ -54,11 +54,11 @@ function user_trailingslashit($string, $type_of_url = '') { * * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'. */ -function permalink_anchor($mode = 'id') { - global $post; - switch ( strtolower($mode) ) { +function permalink_anchor( $mode = 'id' ) { + $post = get_post(); + switch ( strtolower( $mode ) ) { case 'title': - $title = sanitize_title($post->post_title) . '-' . $post->ID; + $title = sanitize_title( $post->post_title ) . '-' . $post->ID; echo ''; break; case 'id': @@ -77,7 +77,7 @@ function permalink_anchor($mode = 'id') { * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name. * @return string */ -function get_permalink($id = 0, $leavename = false) { +function get_permalink( $id = 0, $leavename = false ) { $rewritecode = array( '%year%', '%monthnum%', @@ -232,24 +232,20 @@ function post_permalink( $post_id = 0, $deprecated = '' ) { * * @since 1.5.0 * - * @param int $id Optional. Post ID. + * @param mixed $post Optional. Post ID or object. * @param bool $leavename Optional, defaults to false. Whether to keep page name. * @param bool $sample Optional, defaults to false. Is it a sample permalink. * @return string */ -function get_page_link( $id = false, $leavename = false, $sample = false ) { - global $post; +function get_page_link( $post = false, $leavename = false, $sample = false ) { + $post = get_post( $post ); - $id = (int) $id; - if ( !$id ) - $id = (int) $post->ID; - - if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) + if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) $link = home_url('/'); else - $link = _get_page_link( $id , $leavename, $sample ); + $link = _get_page_link( $post, $leavename, $sample ); - return apply_filters('page_link', $link, $id, $sample); + return apply_filters( 'page_link', $link, $post->ID, $sample ); } /** @@ -260,15 +256,15 @@ function get_page_link( $id = false, $leavename = false, $sample = false ) { * @since 2.1.0 * @access private * - * @param int $id Optional. Post ID. + * @param mixed $post Optional. Post ID or object. * @param bool $leavename Optional. Leave name. * @param bool $sample Optional. Sample permalink. * @return string */ -function _get_page_link( $id = false, $leavename = false, $sample = false ) { +function _get_page_link( $post = false, $leavename = false, $sample = false ) { global $wp_rewrite; - $post = get_post( $id ); + $post = get_post( $post ); $draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); @@ -276,16 +272,16 @@ function _get_page_link( $id = false, $leavename = false, $sample = false ) { if ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) { if ( ! $leavename ) { - $link = str_replace('%pagename%', get_page_uri($id), $link); + $link = str_replace('%pagename%', get_page_uri( $post ), $link); } $link = home_url($link); $link = user_trailingslashit($link, 'page'); } else { - $link = home_url("?page_id=$id"); + $link = home_url( '?page_id=' . $post->ID ); } - return apply_filters( '_get_page_link', $link, $id ); + return apply_filters( '_get_page_link', $link, $post->ID ); } /** @@ -295,38 +291,36 @@ function _get_page_link( $id = false, $leavename = false, $sample = false ) { * * @since 2.0.0 * - * @param int $id Optional. Post ID. + * @param mixed $post Optional. Post ID or object. * @return string */ -function get_attachment_link($id = false) { - global $post, $wp_rewrite; +function get_attachment_link( $post = null ) { + global $wp_rewrite; $link = false; - if ( ! $id) - $id = (int) $post->ID; + $post = get_post( $post ); - $object = get_post($id); - if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) { - $parent = get_post($object->post_parent); + if ( $wp_rewrite->using_permalinks() && ( $post->post_parent > 0 ) && ( $post->post_parent != $post->ID ) ) { + $parent = get_post($post->post_parent); if ( 'page' == $parent->post_type ) - $parentlink = _get_page_link( $object->post_parent ); // Ignores page_on_front + $parentlink = _get_page_link( $post->post_parent ); // Ignores page_on_front else - $parentlink = get_permalink( $object->post_parent ); + $parentlink = get_permalink( $post->post_parent ); - if ( is_numeric($object->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) - $name = 'attachment/' . $object->post_name; // // is paged so we use the explicit attachment marker + if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') ) + $name = 'attachment/' . $post->post_name; // // is paged so we use the explicit attachment marker else - $name = $object->post_name; + $name = $post->post_name; if ( strpos($parentlink, '?') === false ) $link = user_trailingslashit( trailingslashit($parentlink) . $name ); } if ( ! $link ) - $link = home_url( "/?attachment_id=$id" ); + $link = home_url( '/?attachment_id=' . $post->ID ); - return apply_filters('attachment_link', $link, $id); + return apply_filters( 'attachment_link', $link, $post->ID ); } /** @@ -895,7 +889,7 @@ function get_post_type_archive_feed_link( $post_type, $feed = '' ) { * @return string */ function get_edit_post_link( $id = 0, $context = 'display' ) { - if ( !$post = get_post( $id ) ) + if ( ! $post = get_post( $id ) ) return; if ( 'display' == $context ) @@ -1122,9 +1116,9 @@ function get_next_post($in_same_cat = false, $excluded_categories = '') { * @return mixed Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists. */ function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true ) { - global $post, $wpdb; + global $wpdb; - if ( empty( $post ) ) + if ( ! $post = get_post() ) return null; $current_post_date = $post->post_date; @@ -1207,10 +1201,10 @@ function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $pr * @return string */ function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { - if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) ) - $post = get_post($GLOBALS['post']->post_parent); + if ( $previous && is_attachment() && $post = get_post() ) + $post = get_post( $post->post_parent ); else - $post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); + $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous ); if ( empty($post) ) return; @@ -1299,9 +1293,8 @@ function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_c * @return object */ function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) { - global $post; - - if ( empty($post) || ! is_single() || is_attachment() ) + $post = get_post(); + if ( ! $post || ! is_single() || is_attachment() ) return null; $cat_array = array(); @@ -1373,7 +1366,7 @@ function next_post_link($format='%link »', $link='%title', $in_same_cat = */ function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { if ( $previous && is_attachment() ) - $post = get_post($GLOBALS['post']->post_parent); + $post = get_post( get_post()->post_parent ); else $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous); @@ -1680,11 +1673,11 @@ function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) { * @return string */ function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { - global $post, $wp_rewrite; + global $wp_rewrite; $pagenum = (int) $pagenum; - $result = get_permalink( $post->ID ); + $result = get_permalink(); if ( 'newest' == get_option('default_comments_page') ) { if ( $pagenum != $max_page ) { @@ -2412,15 +2405,13 @@ function wp_shortlink_header() { * @param string $after Optional HTML to display after the link. */ function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) { - global $post; - if ( empty( $text ) ) $text = __('This is the short link.'); if ( empty( $title ) ) $title = the_title_attribute( array( 'echo' => false ) ); - $shortlink = wp_get_shortlink( $post->ID ); + $shortlink = wp_get_shortlink(); if ( !empty( $shortlink ) ) { $link = '' . $text . ''; diff --git a/wp-includes/media.php b/wp-includes/media.php index 342edd46d9..8839fd9151 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -775,7 +775,7 @@ add_shortcode('gallery', 'gallery_shortcode'); * @return string HTML content to display gallery. */ function gallery_shortcode($attr) { - global $post; + $post = get_post(); static $instance = 0; $instance++; @@ -928,9 +928,8 @@ function next_image_link($size = 'thumbnail', $text = false) { * @param bool $prev Optional. Default is true to display previous link, false for next. */ function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) { - global $post; - $post = get_post($post); - $attachments = array_values(get_children( array('post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID') )); + $post = get_post(); + $attachments = array_values( get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) ) ); foreach ( $attachments as $k => $attachment ) if ( $attachment->ID == $post->ID ) @@ -1102,16 +1101,16 @@ class WP_Embed { * an AJAX request that will call WP_Embed::cache_oembed(). */ function maybe_run_ajax_cache() { - global $post_ID; + $post = get_post(); - if ( empty($post_ID) || empty($_GET['message']) || 1 != $_GET['message'] ) + if ( ! $post || empty($_GET['message']) || 1 != $_GET['message'] ) return; ?> @@ -1167,9 +1166,9 @@ class WP_Embed { * @return string The embed HTML on success, otherwise the original URL. */ function shortcode( $attr, $url = '' ) { - global $post; + $post = get_post(); - if ( empty($url) ) + if ( empty( $url ) ) return ''; $rawattr = $attr; @@ -1190,8 +1189,8 @@ class WP_Embed { } } - $post_ID = ( !empty($post->ID) ) ? $post->ID : null; - if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed() + $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null; + if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed() $post_ID = $this->post_ID; // Unknown URL format. Let oEmbed have a go. @@ -1206,7 +1205,7 @@ class WP_Embed { if ( '{{unknown}}' === $cache ) return $this->maybe_make_link( $url ); - if ( !empty($cache) ) + if ( ! empty( $cache ) ) return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); } diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index 28eb164b81..af55326005 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -26,8 +26,7 @@ function the_ID() { * @return int */ function get_the_ID() { - global $post; - return $post->ID; + return get_post()->ID; } /** @@ -97,24 +96,25 @@ function the_title_attribute( $args = '' ) { * * @since 0.71 * - * @param int $id Optional. Post ID. + * @param mixed $post Optional. Post ID or object. * @return string */ -function get_the_title( $id = 0 ) { - $post = get_post($id); +function get_the_title( $post = 0 ) { + $post = get_post( $post ); - $title = isset($post->post_title) ? $post->post_title : ''; - $id = isset($post->ID) ? $post->ID : (int) $id; + $title = isset( $post->post_title ) ? $post->post_title : ''; + $id = isset( $post->ID ) ? $post->ID : 0; - if ( !is_admin() ) { - if ( !empty($post->post_password) ) { - $protected_title_format = apply_filters('protected_title_format', __('Protected: %s')); - $title = sprintf($protected_title_format, $title); - } else if ( isset($post->post_status) && 'private' == $post->post_status ) { - $private_title_format = apply_filters('private_title_format', __('Private: %s')); - $title = sprintf($private_title_format, $title); + if ( ! is_admin() ) { + if ( ! empty( $post->post_password ) ) { + $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) ); + $title = sprintf( $protected_title_format, $title ); + } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) { + $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) ); + $title = sprintf( $private_title_format, $title ); } } + return apply_filters( 'the_title', $title, $id ); } @@ -177,8 +177,10 @@ function the_content($more_link_text = null, $stripteaser = false) { * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false. * @return string */ -function get_the_content($more_link_text = null, $stripteaser = false) { - global $post, $more, $page, $pages, $multipage, $preview; +function get_the_content( $more_link_text = null, $stripteaser = false ) { + global $more, $page, $pages, $multipage, $preview; + + $post = get_post(); if ( null === $more_link_text ) $more_link_text = __( '(more...)' ); @@ -187,7 +189,7 @@ function get_the_content($more_link_text = null, $stripteaser = false) { $hasTeaser = false; // If post password required and it doesn't match the cookie. - if ( post_password_required($post) ) + if ( post_password_required() ) return get_the_password_form(); if ( $page > count($pages) ) // if the requested page doesn't exist @@ -259,8 +261,7 @@ function get_the_excerpt( $deprecated = '' ) { if ( !empty( $deprecated ) ) _deprecated_argument( __FUNCTION__, '2.3' ); - global $post; - if ( post_password_required($post) ) { + if ( post_password_required() ) { return __( 'There is no excerpt because this is a protected post.' ); } @@ -676,7 +677,8 @@ function wp_link_pages($args = '') { * @return string Link. */ function _wp_link_page( $i ) { - global $post, $wp_rewrite; + global $wp_rewrite; + $post = get_post(); if ( 1 == $i ) { $url = get_permalink(); @@ -1171,7 +1173,7 @@ function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = fals * @return string */ function prepend_attachment($content) { - global $post; + $post = get_post(); if ( empty($post->post_type) || $post->post_type != 'attachment' ) return $content; @@ -1198,7 +1200,7 @@ function prepend_attachment($content) { * @return string HTML content for password form for password protected post. */ function get_the_password_form() { - global $post; + $post = get_post(); $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID ); $output = '

' . __("This post is password protected. To view it please enter your password below:") . '

diff --git a/wp-includes/post.php b/wp-includes/post.php index 2246005a6e..4bc1e7e684 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -372,12 +372,12 @@ function get_extended($post) { * @uses $wpdb * @link http://codex.wordpress.org/Function_Reference/get_post * - * @param int|object $post Post ID or post object. + * @param int|object $post Post ID or post object. Optional, default is the current post from the loop. * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N. * @param string $filter Optional, default is raw. * @return WP_Post|null WP_Post on success or null on failure */ -function get_post( $post, $output = OBJECT, $filter = 'raw' ) { +function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { if ( empty( $post ) && isset( $GLOBALS['post'] ) ) $post = $GLOBALS['post']; @@ -945,19 +945,12 @@ function post_type_exists( $post_type ) { * * @uses $post The Loop current post global * - * @param mixed $the_post Optional. Post object or post ID. + * @param mixed $post Optional. Post object or post ID. * @return bool|string post type or false on failure. */ -function get_post_type( $the_post = false ) { - global $post; - - if ( false === $the_post ) - $the_post = $post; - elseif ( is_numeric($the_post) ) - $the_post = get_post($the_post); - - if ( is_object($the_post) ) - return $the_post->post_type; +function get_post_type( $post = null ) { + if ( $post = get_post( $post ) ) + return $post->post_type; return false; }