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 ) {
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];

View File

@ -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 = '';

View File

@ -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 );
@ -684,7 +684,7 @@ class WP_Posts_List_Table extends WP_List_Table {
?>
</tr>
<?php
$post = $global_post;
$GLOBALS['post'] = $global_post;
}
/**

View File

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

View File

@ -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 );
?>
<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
$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->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 );
}

View File

@ -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 = '<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' ) );
@ -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 .= '<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;
}
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 );

View File

@ -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 ) {
if ( $post->ID && $item->ID == $post->ID )
continue;
}
}
$pad = str_repeat( '&nbsp;', $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;
}

View File

@ -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

View File

@ -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 );
}
/**

View File

@ -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;
$id = (int) $id;
if ( !$id ) {
if ( empty( $post->ID ) )
function get_the_terms( $post, $taxonomy ) {
if ( ! $post = get_post( $post ) )
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;

View File

@ -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";

View File

@ -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

View File

@ -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 );

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}
*/
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() );
}
/**

View File

@ -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;
}

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'.
*/
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 '<a id="'.$title.'"></a>';
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; // <permalink>/<int>/ 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; // <permalink>/<int>/ 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 &raquo;', $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 = '<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.
*/
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;
?>
<script type="text/javascript">
/* <![CDATA[ */
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>
@ -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 );
}

View File

@ -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 = '<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>

View File

@ -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;
}