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
This commit is contained in:
Ryan Boren 2012-09-04 16:29:28 +00:00
parent c1d2f4770d
commit cfb35a9d85
19 changed files with 160 additions and 189 deletions

View File

@ -302,7 +302,8 @@ class WP_Comments_List_Table extends WP_List_Table {
} }
function single_row( $a_comment ) { function single_row( $a_comment ) {
global $post, $comment; global $comment;
$post = get_post();
$comment = $a_comment; $comment = $a_comment;
$the_comment_class = join( ' ', get_comment_class( wp_get_comment_status( $comment->comment_ID ) ) ); $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 ) { function column_comment( $comment ) {
global $post, $comment_status; global $comment_status;
$post = get_post();
$user_can = $this->user_can; $user_can = $this->user_can;
@ -479,7 +481,7 @@ class WP_Comments_List_Table extends WP_List_Table {
} }
function column_response( $comment ) { function column_response( $comment ) {
global $post; $post = get_post();
if ( isset( $this->pending_count[$post->ID] ) ) { if ( isset( $this->pending_count[$post->ID] ) ) {
$pending_comments = $this->pending_count[$post->ID]; $pending_comments = $this->pending_count[$post->ID];

View File

@ -156,7 +156,8 @@ class WP_Media_List_Table extends WP_List_Table {
} }
function display_rows() { function display_rows() {
global $post, $id; $post = get_post();
$id = $post->ID;
add_filter( 'the_title','esc_html' ); add_filter( 'the_title','esc_html' );
$alt = ''; $alt = '';

View File

@ -316,7 +316,7 @@ class WP_Posts_List_Table extends WP_List_Table {
} }
function _display_rows( $posts, $level = 0 ) { function _display_rows( $posts, $level = 0 ) {
global $post, $mode; global $mode;
// Create array of post IDs. // Create array of post IDs.
$post_ids = array(); $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 unset( $children_pages[$parent] ); //required in order to keep track of orphans
} }
function single_row( $a_post, $level = 0 ) { function single_row( $post, $level = 0 ) {
global $post, $mode; global $mode;
static $alternate; static $alternate;
$global_post = $post; $global_post = get_post();
$post = $a_post; $GLOBALS['post'] = $post;
setup_postdata( $post ); setup_postdata( $post );
$edit_link = get_edit_post_link( $post->ID ); $edit_link = get_edit_post_link( $post->ID );
@ -529,7 +529,7 @@ class WP_Posts_List_Table extends WP_List_Table {
} }
else { else {
$attributes = 'class="post-title page-title column-title"' . $style; $attributes = 'class="post-title page-title column-title"' . $style;
$pad = str_repeat( '— ', $level ); $pad = str_repeat( '— ', $level );
?> ?>
<td <?php echo $attributes ?>><strong><?php if ( $can_edit_post && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ); ?>"><?php echo $pad; echo $title ?></a><?php } else { echo $pad; echo $title; }; _post_states( $post ); ?></strong> <td <?php echo $attributes ?>><strong><?php if ( $can_edit_post && $post->post_status != 'trash' ) { ?><a class="row-title" href="<?php echo $edit_link; ?>" title="<?php echo esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ); ?>"><?php echo $pad; echo $title ?></a><?php } else { echo $pad; echo $title; }; _post_states( $post ); ?></strong>
@ -684,7 +684,7 @@ class WP_Posts_List_Table extends WP_List_Table {
?> ?>
</tr> </tr>
<?php <?php
$post = $global_post; $GLOBALS['post'] = $global_post;
} }
/** /**

View File

@ -279,7 +279,7 @@ function export_wp( $args = array() ) {
* @since 2.3.0 * @since 2.3.0
*/ */
function wxr_post_taxonomy() { function wxr_post_taxonomy() {
global $post; $post = get_post();
$taxonomies = get_object_taxonomies( $post->post_type ); $taxonomies = get_object_taxonomies( $post->post_type );
if ( empty( $taxonomies ) ) if ( empty( $taxonomies ) )

View File

@ -466,15 +466,15 @@ function post_comment_meta_box_thead($result) {
* *
* @param object $post * @param object $post
*/ */
function post_comment_meta_box($post) { function post_comment_meta_box( $post ) {
global $wpdb, $post_ID; global $wpdb;
wp_nonce_field( 'get-comments', 'add_comment_nonce', false ); wp_nonce_field( 'get-comments', 'add_comment_nonce', false );
?> ?>
<p class="hide-if-no-js" id="add-new-comment"><a href="#commentstatusdiv" onclick="commentReply.addcomment(<?php echo $post_ID; ?>);return false;"><?php _e('Add comment'); ?></a></p> <p class="hide-if-no-js" id="add-new-comment"><a href="#commentstatusdiv" onclick="commentReply.addcomment(<?php echo $post->ID; ?>);return false;"><?php _e('Add comment'); ?></a></p>
<?php <?php
$total = get_comments( array( 'post_id' => $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 = _get_list_table('WP_Post_Comments_List_Table');
$wp_list_table->display( true ); $wp_list_table->display( true );
@ -910,8 +910,7 @@ function link_advanced_meta_box($link) {
* *
* @since 2.9.0 * @since 2.9.0
*/ */
function post_thumbnail_meta_box() { function post_thumbnail_meta_box( $post ) {
global $post;
$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true ); $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
echo _wp_post_thumbnail_html( $thumbnail_id ); echo _wp_post_thumbnail_html( $thumbnail_id );
} }

View File

@ -1121,16 +1121,15 @@ function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) {
* @since 2.9.0 * @since 2.9.0
* *
* @param int $thumbnail_id ID of the attachment used for thumbnail * @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 * @return string html
*/ */
function _wp_post_thumbnail_html( $thumbnail_id = null, $post_id = null ) { function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) {
global $content_width, $_wp_additional_image_sizes, $post_ID; global $content_width, $_wp_additional_image_sizes;
if ( empty( $post_id ) ) $post = get_post( $post );
$post_id = $post_ID;
$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 = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>'; $set_thumbnail_link = '<p class="hide-if-no-js"><a title="' . esc_attr__( 'Set featured image' ) . '" href="%s" id="set-post-thumbnail" class="thickbox">%s</a></p>';
$content = sprintf( $set_thumbnail_link, $upload_iframe_src, esc_html__( 'Set featured image' ) ); $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 else
$thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' ); $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'post-thumbnail' );
if ( !empty( $thumbnail_html ) ) { 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 = sprintf( $set_thumbnail_link, $upload_iframe_src, $thumbnail_html );
$content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>'; $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail" onclick="WPRemoveThumbnail(\'' . $ajax_nonce . '\');return false;">' . esc_html__( 'Remove featured image' ) . '</a></p>';
} }
$content_width = $old_content_width; $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 * @return none
*/ */
function _admin_notice_post_locked() { function _admin_notice_post_locked() {
global $post; $post = get_post();
$lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) ); $lock = explode( ':', get_post_meta( $post->ID, '_edit_lock', true ) );
$user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true );
$last_user = get_userdata( $user ); $last_user = get_userdata( $user );

View File

@ -166,10 +166,9 @@ function wp_terms_checklist($post_id = 0, $args = array()) {
* @return array List of popular term IDs. * @return array List of popular term IDs.
*/ */
function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) { function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) {
global $post_ID; $post = get_post();
if ( $post->ID )
if ( $post_ID ) $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids'));
$checked_terms = wp_get_object_terms($post_ID, $taxonomy, array('fields'=>'ids'));
else else
$checked_terms = array(); $checked_terms = array();
@ -575,7 +574,8 @@ function meta_form() {
* @param unknown_type $multi * @param unknown_type $multi
*/ */
function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) { 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 ) 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 ) ); $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 * @return unknown
*/ */
function parent_dropdown( $default = 0, $parent = 0, $level = 0 ) { 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) ); $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 ) { if ( $items ) {
foreach ( $items as $item ) { foreach ( $items as $item ) {
// A page cannot be its own parent. // A page cannot be its own parent.
if (!empty ( $post_ID ) ) { if ( $post->ID && $item->ID == $post->ID )
if ( $item->ID == $post_ID ) { continue;
continue;
}
}
$pad = str_repeat( '&nbsp;', $level * 3 ); $pad = str_repeat( '&nbsp;', $level * 3 );
if ( $item->ID == $default) if ( $item->ID == $default)
$current = ' selected="selected"'; $current = ' selected="selected"';
@ -1345,8 +1344,9 @@ function find_posts_div($found_action = '') {
* @since 2.7.0 * @since 2.7.0
*/ */
function the_post_password() { function the_post_password() {
global $post; $post = get_post();
if ( isset( $post->post_password ) ) echo esc_attr( $post->post_password ); if ( isset( $post->post_password ) )
echo esc_attr( $post->post_password );
} }
/** /**
@ -1356,13 +1356,13 @@ function the_post_password() {
* returned. * returned.
* *
* @since 2.7.0 * @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 * @return string The post title if set
*/ */
function _draft_or_post_title( $post_id = 0 ) { function _draft_or_post_title( $post = 0 ) {
$title = get_the_title($post_id); $title = get_the_title( $post );
if ( empty($title) ) if ( empty( $title ) )
$title = __('(no title)'); $title = __( '(no title)' );
return $title; return $title;
} }

View File

@ -419,10 +419,11 @@ function wp_admin_bar_shortlink_menu( $wp_admin_bar ) {
* @since 3.1.0 * @since 3.1.0
*/ */
function wp_admin_bar_edit_menu( $wp_admin_bar ) { function wp_admin_bar_edit_menu( $wp_admin_bar ) {
global $post, $tag, $wp_the_query; global $tag, $wp_the_query;
if ( is_admin() ) { if ( is_admin() ) {
$current_screen = get_current_screen(); $current_screen = get_current_screen();
$post = get_post();
if ( 'post' == $current_screen->base if ( 'post' == $current_screen->base
&& 'add' != $current_screen->action && 'add' != $current_screen->action
@ -619,7 +620,7 @@ function wp_admin_bar_updates_menu( $wp_admin_bar ) {
return; return;
$title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>'; $title = '<span class="ab-icon"></span><span class="ab-label">' . number_format_i18n( $update_data['counts']['total'] ) . '</span>';
$title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>'; $title .= '<span class="screen-reader-text">' . $update_data['title'] . '</span>';
$wp_admin_bar->add_menu( array( $wp_admin_bar->add_menu( array(
'id' => 'updates', 'id' => 'updates',

View File

@ -69,8 +69,7 @@ function the_author( $deprecated = '', $deprecated_echo = true ) {
* @return string The author's display name. * @return string The author's display name.
*/ */
function get_the_modified_author() { function get_the_modified_author() {
global $post; if ( $last_id = get_post_meta( get_post()->ID, '_edit_last', true) ) {
if ( $last_id = get_post_meta($post->ID, '_edit_last', true) ) {
$last_user = get_userdata($last_id); $last_user = get_userdata($last_id);
return apply_filters('the_modified_author', $last_user->display_name); 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. * @return int The number of posts by the author.
*/ */
function get_the_author_posts() { function get_the_author_posts() {
global $post; return count_user_posts( get_post()->post_author );
return count_user_posts($post->post_author);
} }
/** /**

View File

@ -1054,29 +1054,21 @@ function term_description( $term = 0, $taxonomy = 'post_tag' ) {
* *
* @since 2.5.0 * @since 2.5.0
* *
* @param int $id Post ID. * @param mixed $post Post ID or object.
* @param string $taxonomy Taxonomy name. * @param string $taxonomy Taxonomy name.
* @return array|bool False on failure. Array of term objects on success. * @return array|bool False on failure. Array of term objects on success.
*/ */
function get_the_terms( $id, $taxonomy ) { function get_the_terms( $post, $taxonomy ) {
global $post; if ( ! $post = get_post( $post ) )
return false;
$id = (int) $id; $terms = get_object_term_cache( $post->ID, $taxonomy );
if ( !$id ) {
if ( empty( $post->ID ) )
return false;
else
$id = (int) $post->ID;
}
$terms = get_object_term_cache( $id, $taxonomy );
if ( false === $terms ) { if ( false === $terms ) {
$terms = wp_get_object_terms( $id, $taxonomy ); $terms = wp_get_object_terms( $post->ID, $taxonomy );
wp_cache_add($id, $terms, $taxonomy . '_relationships'); 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 ) ) if ( empty( $terms ) )
return false; return false;

View File

@ -846,10 +846,9 @@ EOD;
* @param int $postID Post ID. * @param int $postID Post ID.
* @return string * @return string
*/ */
function get_entry_url($postID = null) { function get_entry_url( $postID = null ) {
if (!isset($postID)) { if (!isset($postID)) {
global $post; $postID = (int) get_post()->ID;
$postID = (int) $post->ID;
} }
$url = $this->app_base . $this->ENTRY_PATH . "/$postID"; $url = $this->app_base . $this->ENTRY_PATH . "/$postID";
@ -878,8 +877,7 @@ EOD;
*/ */
function get_media_url($postID = null) { function get_media_url($postID = null) {
if (!isset($postID)) { if (!isset($postID)) {
global $post; $postID = (int) get_post()->ID;
$postID = (int) $post->ID;
} }
$url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID"; $url = $this->app_base . $this->MEDIA_SINGLE_PATH ."/file/$postID";

View File

@ -126,7 +126,7 @@ final class _WP_Editors {
} }
public static function editor_settings($editor_id, $set) { public static function editor_settings($editor_id, $set) {
global $editor_styles, $post; global $editor_styles;
$first_run = false; $first_run = false;
if ( empty(self::$first_init) ) { if ( empty(self::$first_init) ) {
@ -370,8 +370,8 @@ final class _WP_Editors {
$body_class = $editor_id; $body_class = $editor_id;
if ( isset($post) ) if ( $post = get_post() )
$body_class .= " post-type-$post->post_type"; $body_class .= ' post-type-' . $post->post_type;
if ( !empty($set['tinymce']['body_class']) ) { if ( !empty($set['tinymce']['body_class']) ) {
$body_class .= ' ' . $set['tinymce']['body_class']; $body_class .= ' ' . $set['tinymce']['body_class'];
@ -612,7 +612,8 @@ final class _WP_Editors {
} }
public static function wp_fullscreen_html() { 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 = isset($content_width) && 800 > $content_width ? $content_width : 800;
$width = $width + 22; // compensate for the padding and border $width = $width + 22; // compensate for the padding and border

View File

@ -810,11 +810,8 @@ function pings_open( $post_id = null ) {
* @uses $post Gets the ID of the current post for the token * @uses $post Gets the ID of the current post for the token
*/ */
function wp_comment_form_unfiltered_html_nonce() { function wp_comment_form_unfiltered_html_nonce() {
global $post; $post = get_post();
$post_id = $post ? $post->ID : 0;
$post_id = 0;
if ( !empty($post) )
$post_id = $post->ID;
if ( current_user_can( 'unfiltered_html' ) ) { if ( current_user_can( 'unfiltered_html' ) ) {
wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false ); wp_nonce_field( 'unfiltered-html-comment_' . $post_id, '_wp_unfiltered_html_comment_disabled', false );

View File

@ -57,14 +57,14 @@ function get_postdata($postid) {
* @deprecated Use The Loop - {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop} * @deprecated Use The Loop - {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop}
*/ */
function start_wp() { function start_wp() {
global $wp_query, $post; global $wp_query;
_deprecated_function( __FUNCTION__, '1.5', __('new WordPress Loop') ); _deprecated_function( __FUNCTION__, '1.5', __('new WordPress Loop') );
// Since the old style loop is being used, advance the query iterator here. // Since the old style loop is being used, advance the query iterator here.
$wp_query->next_post(); $wp_query->next_post();
setup_postdata($post); setup_postdata( get_post() );
} }
/** /**

View File

@ -1311,8 +1311,7 @@ function allowed_tags() {
* @since 1.0.0 * @since 1.0.0
*/ */
function the_date_xml() { function the_date_xml() {
global $post; echo mysql2date( 'Y-m-d', get_post()->post_date, false );
echo mysql2date('Y-m-d', $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. * @return string|null Null if displaying, string if retrieving.
*/ */
function get_the_date( $d = '' ) { function get_the_date( $d = '' ) {
global $post; $post = get_post();
$the_date = ''; $the_date = '';
if ( '' == $d ) if ( '' == $d )
@ -1528,8 +1527,8 @@ function get_post_modified_time( $d = 'U', $gmt = false, $post = null, $translat
* @uses $post * @uses $post
*/ */
function the_weekday() { function the_weekday() {
global $wp_locale, $post; global $wp_locale;
$the_weekday = $wp_locale->get_weekday(mysql2date('w', $post->post_date, false)); $the_weekday = $wp_locale->get_weekday( mysql2date( 'w', get_post()->post_date, false ) );
$the_weekday = apply_filters('the_weekday', $the_weekday); $the_weekday = apply_filters('the_weekday', $the_weekday);
echo $the_weekday; echo $the_weekday;
} }
@ -1546,11 +1545,11 @@ function the_weekday() {
* @param string $after Optional Output after the date. * @param string $after Optional Output after the date.
*/ */
function the_weekday_date($before='',$after='') { function the_weekday_date($before='',$after='') {
global $wp_locale, $post, $day, $previousweekday; global $wp_locale, $day, $previousweekday;
$the_weekday_date = ''; $the_weekday_date = '';
if ( $currentday != $previousweekday ) { if ( $currentday != $previousweekday ) {
$the_weekday_date .= $before; $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; $the_weekday_date .= $after;
$previousweekday = $currentday; $previousweekday = $currentday;
} }

View File

@ -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'. * @param string $mode Permalink mode can be either 'title', 'id', or default, which is 'id'.
*/ */
function permalink_anchor($mode = 'id') { function permalink_anchor( $mode = 'id' ) {
global $post; $post = get_post();
switch ( strtolower($mode) ) { switch ( strtolower( $mode ) ) {
case 'title': case 'title':
$title = sanitize_title($post->post_title) . '-' . $post->ID; $title = sanitize_title( $post->post_title ) . '-' . $post->ID;
echo '<a id="'.$title.'"></a>'; echo '<a id="'.$title.'"></a>';
break; break;
case 'id': 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. * @param bool $leavename Optional, defaults to false. Whether to keep post name or page name.
* @return string * @return string
*/ */
function get_permalink($id = 0, $leavename = false) { function get_permalink( $id = 0, $leavename = false ) {
$rewritecode = array( $rewritecode = array(
'%year%', '%year%',
'%monthnum%', '%monthnum%',
@ -232,24 +232,20 @@ function post_permalink( $post_id = 0, $deprecated = '' ) {
* *
* @since 1.5.0 * @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 $leavename Optional, defaults to false. Whether to keep page name.
* @param bool $sample Optional, defaults to false. Is it a sample permalink. * @param bool $sample Optional, defaults to false. Is it a sample permalink.
* @return string * @return string
*/ */
function get_page_link( $id = false, $leavename = false, $sample = false ) { function get_page_link( $post = false, $leavename = false, $sample = false ) {
global $post; $post = get_post( $post );
$id = (int) $id; if ( 'page' == get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) )
if ( !$id )
$id = (int) $post->ID;
if ( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') )
$link = home_url('/'); $link = home_url('/');
else 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 * @since 2.1.0
* @access private * @access private
* *
* @param int $id Optional. Post ID. * @param mixed $post Optional. Post ID or object.
* @param bool $leavename Optional. Leave name. * @param bool $leavename Optional. Leave name.
* @param bool $sample Optional. Sample permalink. * @param bool $sample Optional. Sample permalink.
* @return string * @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; global $wp_rewrite;
$post = get_post( $id ); $post = get_post( $post );
$draft_or_pending = in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) ); $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 ( !empty($link) && ( ( isset($post->post_status) && !$draft_or_pending ) || $sample ) ) {
if ( ! $leavename ) { 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 = home_url($link);
$link = user_trailingslashit($link, 'page'); $link = user_trailingslashit($link, 'page');
} else { } 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 * @since 2.0.0
* *
* @param int $id Optional. Post ID. * @param mixed $post Optional. Post ID or object.
* @return string * @return string
*/ */
function get_attachment_link($id = false) { function get_attachment_link( $post = null ) {
global $post, $wp_rewrite; global $wp_rewrite;
$link = false; $link = false;
if ( ! $id) $post = get_post( $post );
$id = (int) $post->ID;
$object = get_post($id); if ( $wp_rewrite->using_permalinks() && ( $post->post_parent > 0 ) && ( $post->post_parent != $post->ID ) ) {
if ( $wp_rewrite->using_permalinks() && ($object->post_parent > 0) && ($object->post_parent != $id) ) { $parent = get_post($post->post_parent);
$parent = get_post($object->post_parent);
if ( 'page' == $parent->post_type ) 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 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%') ) if ( is_numeric($post->post_name) || false !== strpos(get_option('permalink_structure'), '%category%') )
$name = 'attachment/' . $object->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker $name = 'attachment/' . $post->post_name; // <permalink>/<int>/ is paged so we use the explicit attachment marker
else else
$name = $object->post_name; $name = $post->post_name;
if ( strpos($parentlink, '?') === false ) if ( strpos($parentlink, '?') === false )
$link = user_trailingslashit( trailingslashit($parentlink) . $name ); $link = user_trailingslashit( trailingslashit($parentlink) . $name );
} }
if ( ! $link ) 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 * @return string
*/ */
function get_edit_post_link( $id = 0, $context = 'display' ) { function get_edit_post_link( $id = 0, $context = 'display' ) {
if ( !$post = get_post( $id ) ) if ( ! $post = get_post( $id ) )
return; return;
if ( 'display' == $context ) 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. * @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 ) { 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; return null;
$current_post_date = $post->post_date; $current_post_date = $post->post_date;
@ -1207,10 +1201,10 @@ function get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $pr
* @return string * @return string
*/ */
function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) { function get_adjacent_post_rel_link($title = '%title', $in_same_cat = false, $excluded_categories = '', $previous = true) {
if ( $previous && is_attachment() && is_object( $GLOBALS['post'] ) ) if ( $previous && is_attachment() && $post = get_post() )
$post = get_post($GLOBALS['post']->post_parent); $post = get_post( $post->post_parent );
else else
$post = get_adjacent_post($in_same_cat,$excluded_categories,$previous); $post = get_adjacent_post( $in_same_cat, $excluded_categories, $previous );
if ( empty($post) ) if ( empty($post) )
return; return;
@ -1299,9 +1293,8 @@ function prev_post_rel_link($title = '%title', $in_same_cat = false, $excluded_c
* @return object * @return object
*/ */
function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) { function get_boundary_post( $in_same_cat = false, $excluded_categories = '', $start = true ) {
global $post; $post = get_post();
if ( ! $post || ! is_single() || is_attachment() )
if ( empty($post) || ! is_single() || is_attachment() )
return null; return null;
$cat_array = array(); $cat_array = array();
@ -1373,7 +1366,7 @@ function next_post_link($format='%link &raquo;', $link='%title', $in_same_cat =
*/ */
function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) { function adjacent_post_link($format, $link, $in_same_cat = false, $excluded_categories = '', $previous = true) {
if ( $previous && is_attachment() ) if ( $previous && is_attachment() )
$post = get_post($GLOBALS['post']->post_parent); $post = get_post( get_post()->post_parent );
else else
$post = get_adjacent_post($in_same_cat, $excluded_categories, $previous); $post = get_adjacent_post($in_same_cat, $excluded_categories, $previous);
@ -1680,11 +1673,11 @@ function posts_nav_link( $sep = '', $prelabel = '', $nxtlabel = '' ) {
* @return string * @return string
*/ */
function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) { function get_comments_pagenum_link( $pagenum = 1, $max_page = 0 ) {
global $post, $wp_rewrite; global $wp_rewrite;
$pagenum = (int) $pagenum; $pagenum = (int) $pagenum;
$result = get_permalink( $post->ID ); $result = get_permalink();
if ( 'newest' == get_option('default_comments_page') ) { if ( 'newest' == get_option('default_comments_page') ) {
if ( $pagenum != $max_page ) { if ( $pagenum != $max_page ) {
@ -2412,15 +2405,13 @@ function wp_shortlink_header() {
* @param string $after Optional HTML to display after the link. * @param string $after Optional HTML to display after the link.
*/ */
function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) { function the_shortlink( $text = '', $title = '', $before = '', $after = '' ) {
global $post;
if ( empty( $text ) ) if ( empty( $text ) )
$text = __('This is the short link.'); $text = __('This is the short link.');
if ( empty( $title ) ) if ( empty( $title ) )
$title = the_title_attribute( array( 'echo' => false ) ); $title = the_title_attribute( array( 'echo' => false ) );
$shortlink = wp_get_shortlink( $post->ID ); $shortlink = wp_get_shortlink();
if ( !empty( $shortlink ) ) { if ( !empty( $shortlink ) ) {
$link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>'; $link = '<a rel="shortlink" href="' . esc_url( $shortlink ) . '" title="' . $title . '">' . $text . '</a>';

View File

@ -775,7 +775,7 @@ add_shortcode('gallery', 'gallery_shortcode');
* @return string HTML content to display gallery. * @return string HTML content to display gallery.
*/ */
function gallery_shortcode($attr) { function gallery_shortcode($attr) {
global $post; $post = get_post();
static $instance = 0; static $instance = 0;
$instance++; $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. * @param bool $prev Optional. Default is true to display previous link, false for next.
*/ */
function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) { function adjacent_image_link($prev = true, $size = 'thumbnail', $text = false) {
global $post; $post = get_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' ) ) );
$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 ) foreach ( $attachments as $k => $attachment )
if ( $attachment->ID == $post->ID ) if ( $attachment->ID == $post->ID )
@ -1102,16 +1101,16 @@ class WP_Embed {
* an AJAX request that will call WP_Embed::cache_oembed(). * an AJAX request that will call WP_Embed::cache_oembed().
*/ */
function maybe_run_ajax_cache() { 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; return;
?> ?>
<script type="text/javascript"> <script type="text/javascript">
/* <![CDATA[ */ /* <![CDATA[ */
jQuery(document).ready(function($){ jQuery(document).ready(function($){
$.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post_ID, 'relative' ); ?>"); $.get("<?php echo admin_url( 'admin-ajax.php?action=oembed-cache&post=' . $post->ID, 'relative' ); ?>");
}); });
/* ]]> */ /* ]]> */
</script> </script>
@ -1167,9 +1166,9 @@ class WP_Embed {
* @return string The embed HTML on success, otherwise the original URL. * @return string The embed HTML on success, otherwise the original URL.
*/ */
function shortcode( $attr, $url = '' ) { function shortcode( $attr, $url = '' ) {
global $post; $post = get_post();
if ( empty($url) ) if ( empty( $url ) )
return ''; return '';
$rawattr = $attr; $rawattr = $attr;
@ -1190,8 +1189,8 @@ class WP_Embed {
} }
} }
$post_ID = ( !empty($post->ID) ) ? $post->ID : null; $post_ID = ( ! empty( $post->ID ) ) ? $post->ID : null;
if ( !empty($this->post_ID) ) // Potentially set by WP_Embed::cache_oembed() if ( ! empty( $this->post_ID ) ) // Potentially set by WP_Embed::cache_oembed()
$post_ID = $this->post_ID; $post_ID = $this->post_ID;
// Unknown URL format. Let oEmbed have a go. // Unknown URL format. Let oEmbed have a go.
@ -1206,7 +1205,7 @@ class WP_Embed {
if ( '{{unknown}}' === $cache ) if ( '{{unknown}}' === $cache )
return $this->maybe_make_link( $url ); return $this->maybe_make_link( $url );
if ( !empty($cache) ) if ( ! empty( $cache ) )
return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID ); return apply_filters( 'embed_oembed_html', $cache, $url, $attr, $post_ID );
} }

View File

@ -26,8 +26,7 @@ function the_ID() {
* @return int * @return int
*/ */
function get_the_ID() { function get_the_ID() {
global $post; return get_post()->ID;
return $post->ID;
} }
/** /**
@ -97,24 +96,25 @@ function the_title_attribute( $args = '' ) {
* *
* @since 0.71 * @since 0.71
* *
* @param int $id Optional. Post ID. * @param mixed $post Optional. Post ID or object.
* @return string * @return string
*/ */
function get_the_title( $id = 0 ) { function get_the_title( $post = 0 ) {
$post = get_post($id); $post = get_post( $post );
$title = isset($post->post_title) ? $post->post_title : ''; $title = isset( $post->post_title ) ? $post->post_title : '';
$id = isset($post->ID) ? $post->ID : (int) $id; $id = isset( $post->ID ) ? $post->ID : 0;
if ( !is_admin() ) { if ( ! is_admin() ) {
if ( !empty($post->post_password) ) { if ( ! empty( $post->post_password ) ) {
$protected_title_format = apply_filters('protected_title_format', __('Protected: %s')); $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) );
$title = sprintf($protected_title_format, $title); $title = sprintf( $protected_title_format, $title );
} else if ( isset($post->post_status) && 'private' == $post->post_status ) { } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) {
$private_title_format = apply_filters('private_title_format', __('Private: %s')); $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) );
$title = sprintf($private_title_format, $title); $title = sprintf( $private_title_format, $title );
} }
} }
return apply_filters( 'the_title', $title, $id ); 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. * @param bool $stripteaser Optional. Strip teaser content before the more text. Default is false.
* @return string * @return string
*/ */
function get_the_content($more_link_text = null, $stripteaser = false) { function get_the_content( $more_link_text = null, $stripteaser = false ) {
global $post, $more, $page, $pages, $multipage, $preview; global $more, $page, $pages, $multipage, $preview;
$post = get_post();
if ( null === $more_link_text ) if ( null === $more_link_text )
$more_link_text = __( '(more...)' ); $more_link_text = __( '(more...)' );
@ -187,7 +189,7 @@ function get_the_content($more_link_text = null, $stripteaser = false) {
$hasTeaser = false; $hasTeaser = false;
// If post password required and it doesn't match the cookie. // 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(); return get_the_password_form();
if ( $page > count($pages) ) // if the requested page doesn't exist if ( $page > count($pages) ) // if the requested page doesn't exist
@ -259,8 +261,7 @@ function get_the_excerpt( $deprecated = '' ) {
if ( !empty( $deprecated ) ) if ( !empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '2.3' ); _deprecated_argument( __FUNCTION__, '2.3' );
global $post; if ( post_password_required() ) {
if ( post_password_required($post) ) {
return __( 'There is no excerpt because this is a protected post.' ); return __( 'There is no excerpt because this is a protected post.' );
} }
@ -676,7 +677,8 @@ function wp_link_pages($args = '') {
* @return string Link. * @return string Link.
*/ */
function _wp_link_page( $i ) { function _wp_link_page( $i ) {
global $post, $wp_rewrite; global $wp_rewrite;
$post = get_post();
if ( 1 == $i ) { if ( 1 == $i ) {
$url = get_permalink(); $url = get_permalink();
@ -1171,7 +1173,7 @@ function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = fals
* @return string * @return string
*/ */
function prepend_attachment($content) { function prepend_attachment($content) {
global $post; $post = get_post();
if ( empty($post->post_type) || $post->post_type != 'attachment' ) if ( empty($post->post_type) || $post->post_type != 'attachment' )
return $content; return $content;
@ -1198,7 +1200,7 @@ function prepend_attachment($content) {
* @return string HTML content for password form for password protected post. * @return string HTML content for password form for password protected post.
*/ */
function get_the_password_form() { function get_the_password_form() {
global $post; $post = get_post();
$label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID ); $label = 'pwbox-' . ( empty($post->ID) ? rand() : $post->ID );
$output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post"> $output = '<form action="' . esc_url( site_url( 'wp-login.php?action=postpass', 'login_post' ) ) . '" method="post">
<p>' . __("This post is password protected. To view it please enter your password below:") . '</p> <p>' . __("This post is password protected. To view it please enter your password below:") . '</p>

View File

@ -372,12 +372,12 @@ function get_extended($post) {
* @uses $wpdb * @uses $wpdb
* @link http://codex.wordpress.org/Function_Reference/get_post * @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 $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional, default is raw. * @param string $filter Optional, default is raw.
* @return WP_Post|null WP_Post on success or null on failure * @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'] ) ) if ( empty( $post ) && isset( $GLOBALS['post'] ) )
$post = $GLOBALS['post']; $post = $GLOBALS['post'];
@ -945,19 +945,12 @@ function post_type_exists( $post_type ) {
* *
* @uses $post The Loop current post global * @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. * @return bool|string post type or false on failure.
*/ */
function get_post_type( $the_post = false ) { function get_post_type( $post = null ) {
global $post; if ( $post = get_post( $post ) )
return $post->post_type;
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;
return false; return false;
} }