📄 functions.php
字号:
}
if ( !$meta_id )
return false;
if ( empty($value) ) {
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
AND meta_key = '$key'");
unset($post_meta_cache['$post_id'][$key]);
} else {
$wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id = '$post_id'
AND meta_key = '$key' AND meta_value = '$value'");
$cache_key = $post_meta_cache['$post_id'][$key];
if ($cache_key) foreach ( $cache_key as $index => $data )
if ( $data == $value )
unset($post_meta_cache['$post_id'][$key][$index]);
}
unset($post_meta_cache['$post_id'][$key]);
return true;
}
function get_post_meta($post_id, $key, $single = false) {
global $wpdb, $post_meta_cache;
if ( isset($post_meta_cache[$post_id][$key]) ) {
if ( $single ) {
return maybe_unserialize( $post_meta_cache[$post_id][$key][0] );
} else {
return maybe_unserialize( $post_meta_cache[$post_id][$key] );
}
}
$metalist = $wpdb->get_results("SELECT meta_value FROM $wpdb->postmeta WHERE post_id = '$post_id' AND meta_key = '$key'", ARRAY_N);
$values = array();
if ( $metalist ) {
foreach ($metalist as $metarow) {
$values[] = $metarow[0];
}
}
if ( $single ) {
if ( count($values) ) {
$return = maybe_unserialize( $values[0] );
} else {
return '';
}
} else {
$return = $values;
}
return maybe_unserialize($return);
}
function update_post_meta($post_id, $key, $value, $prev_value = '') {
global $wpdb, $post_meta_cache;
$original_value = $value;
if ( is_array($value) || is_object($value) )
$value = $wpdb->escape(serialize($value));
$original_prev = $prev_value;
if ( is_array($prev_value) || is_object($prev_value) )
$prev_value = $wpdb->escape(serialize($prev_value));
if (! $wpdb->get_var("SELECT meta_key FROM $wpdb->postmeta WHERE meta_key
= '$key' AND post_id = '$post_id'") ) {
return false;
}
if ( empty($prev_value) ) {
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
meta_key = '$key' AND post_id = '$post_id'");
$cache_key = $post_meta_cache['$post_id'][$key];
if ( !empty($cache_key) )
foreach ($cache_key as $index => $data)
$post_meta_cache['$post_id'][$key][$index] = $original_value;
} else {
$wpdb->query("UPDATE $wpdb->postmeta SET meta_value = '$value' WHERE
meta_key = '$key' AND post_id = '$post_id' AND meta_value = '$prev_value'");
$cache_key = $post_meta_cache['$post_id'][$key];
if ( !empty($cache_key) )
foreach ($cache_key as $index => $data)
if ( $data == $original_prev )
$post_meta_cache['$post_id'][$key][$index] = $original_value;
}
return true;
}
// Deprecated. Use get_post().
function get_postdata($postid) {
$post = &get_post($postid);
$postdata = array (
'ID' => $post->ID,
'Author_ID' => $post->post_author,
'Date' => $post->post_date,
'Content' => $post->post_content,
'Excerpt' => $post->post_excerpt,
'Title' => $post->post_title,
'Category' => $post->post_category,
'post_status' => $post->post_status,
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_password' => $post->post_password,
'to_ping' => $post->to_ping,
'pinged' => $post->pinged,
'post_name' => $post->post_name
);
return $postdata;
}
// Retrieves post data given a post ID or post object.
// Handles post caching.
function &get_post(&$post, $output = OBJECT) {
global $post_cache, $wpdb;
if ( empty($post) ) {
if ( isset($GLOBALS['post']) )
$_post = & $GLOBALS['post'];
else
$_post = null;
} elseif ( is_object($post) ) {
if ( 'static' == $post->post_status )
return get_page($post, $output);
if ( !isset($post_cache[$post->ID]) )
$post_cache[$post->ID] = &$post;
$_post = & $post_cache[$post->ID];
} else {
if ( $_post = wp_cache_get($post, 'pages') )
return get_page($_post, $output);
elseif ( isset($post_cache[$post]) )
$_post = & $post_cache[$post];
else {
$query = "SELECT * FROM $wpdb->posts WHERE ID = '$post' LIMIT 1";
$_post = & $wpdb->get_row($query);
if ( 'static' == $_post->post_status )
return get_page($_post, $output);
$post_cache[$post] = & $_post;
}
}
if ( defined(WP_IMPORTING) )
unset($post_cache);
if ( $output == OBJECT ) {
return $_post;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($_post);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($_post));
} else {
return $_post;
}
}
function &get_children($post = 0, $output = OBJECT) {
global $post_cache, $wpdb;
if ( empty($post) ) {
if ( isset($GLOBALS['post']) )
$post_parent = & $GLOBALS['post']->post_parent;
else
return false;
} elseif ( is_object($post) ) {
$post_parent = $post->post_parent;
} else {
$post_parent = $post;
}
$post_parent = (int) $post_parent;
$query = "SELECT * FROM $wpdb->posts WHERE post_parent = $post_parent";
$children = $wpdb->get_results($query);
if ( $children ) {
foreach ( $children as $key => $child ) {
$post_cache[$child->ID] =& $children[$key];
$kids[$child->ID] =& $children[$key];
}
} else {
return false;
}
if ( $output == OBJECT ) {
return $kids;
} elseif ( $output == ARRAY_A ) {
foreach ( $kids as $kid )
$weeuns[$kid->ID] = get_object_vars($kids[$kid->ID]);
return $weeuns;
} elseif ( $output == ARRAY_N ) {
foreach ( $kids as $kid )
$babes[$kid->ID] = array_values(get_object_vars($kids[$kid->ID]));
return $babes;
} else {
return $kids;
}
}
function set_page_path($page) {
$page->fullpath = '/' . $page->post_name;
$path = $page->fullpath;
$curpage = $page;
while ($curpage->post_parent != 0) {
$curpage = get_page($curpage->post_parent);
$path = '/' . $curpage->post_name . $path;
}
$page->fullpath = $path;
return $page;
}
// Retrieves page data given a page ID or page object.
// Handles page caching.
function &get_page(&$page, $output = OBJECT) {
global $wpdb;
if ( empty($page) ) {
if ( isset($GLOBALS['page']) ) {
$_page = & $GLOBALS['page'];
wp_cache_add($_page->ID, $_page, 'pages');
} else {
$_page = null;
}
} elseif ( is_object($page) ) {
if ( 'static' != $page->post_status )
return get_post($page, $output);
wp_cache_add($page->ID, $page, 'pages');
$_page = $page;
} else {
if ( isset($GLOBALS['page']) && ($page == $GLOBALS['page']->ID) ) {
$_page = & $GLOBALS['page'];
wp_cache_add($_page->ID, $_page, 'pages');
} elseif ( $_page = $GLOBALS['post_cache'][$page] ) {
return get_post($page, $output);
} elseif ( $_page = wp_cache_get($page, 'pages') ) {
// Got it.
} else {
$query = "SELECT * FROM $wpdb->posts WHERE ID= '$page' LIMIT 1";
$_page = & $wpdb->get_row($query);
if ( 'static' != $_page->post_status )
return get_post($_page, $output);
wp_cache_add($_page->ID, $_page, 'pages');
}
}
if (!isset($_page->fullpath)) {
$_page = set_page_path($_page);
wp_cache_replace($_page->cat_ID, $_page, 'pages');
}
if ( $output == OBJECT ) {
return $_page;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($_page);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($_page));
} else {
return $_page;
}
}
function set_category_path($cat) {
$cat->fullpath = '/' . $cat->category_nicename;
$path = $cat->fullpath;
$curcat = $cat;
while ($curcat->category_parent != 0) {
$curcat = get_category($curcat->category_parent);
$path = '/' . $curcat->category_nicename . $path;
}
$cat->fullpath = $path;
return $cat;
}
// Retrieves category data given a category ID or category object.
// Handles category caching.
function &get_category(&$category, $output = OBJECT) {
global $wpdb;
if ( empty($category) )
return null;
if ( is_object($category) ) {
wp_cache_add($category->cat_ID, $category, 'category');
$_category = $category;
} else {
if ( ! $_category = wp_cache_get($category, 'category') ) {
$_category = $wpdb->get_row("SELECT * FROM $wpdb->categories WHERE cat_ID = '$category' LIMIT 1");
wp_cache_add($category, $_category, 'category');
}
}
$_category = apply_filters('get_category', $_category);
if ( !isset($_category->fullpath) ) {
$_category = set_category_path($_category);
wp_cache_replace($_category->cat_ID, $_category, 'category');
}
if ( $output == OBJECT ) {
return $_category;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($_category);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($_category));
} else {
return $_category;
}
}
// Retrieves comment data given a comment ID or comment object.
// Handles comment caching.
function &get_comment(&$comment, $output = OBJECT) {
global $comment_cache, $wpdb;
if ( empty($comment) )
return null;
if ( is_object($comment) ) {
if ( !isset($comment_cache[$comment->comment_ID]) )
$comment_cache[$comment->comment_ID] = &$comment;
$_comment = & $comment_cache[$comment->comment_ID];
} else {
if ( !isset($comment_cache[$comment]) ) {
$_comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment' LIMIT 1");
$comment_cache[$comment->comment_ID] = & $_comment;
} else {
$_comment = & $comment_cache[$comment];
}
}
if ( $output == OBJECT ) {
return $_comment;
} elseif ( $output == ARRAY_A ) {
return get_object_vars($_comment);
} elseif ( $output == ARRAY_N ) {
return array_values(get_object_vars($_comment));
} else {
return $_comment;
}
}
function get_catname($cat_ID) {
$category = &get_category($cat_ID);
return $category->cat_name;
}
function get_all_category_ids() {
global $wpdb;
if ( ! $cat_ids = wp_cache_get('all_category_ids', 'category') ) {
$cat_ids = $wpdb->get_col("SELECT cat_ID FROM $wpdb->categories");
wp_cache_add('all_category_ids', $cat_ids, 'category');
}
return $cat_ids;
}
function get_all_page_ids() {
global $wpdb;
if ( ! $page_ids = wp_cache_get('all_page_ids', 'pages') ) {
$page_ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status='static'");
wp_cache_add('all_page_ids', $page_ids, 'pages');
}
return $page_ids;
}
function gzip_compression() {
if ( !get_settings('gzipcompression') ) return false;
if ( extension_loaded('zlib') ) {
ob_start('ob_gzhandler');
}
}
// functions to count the page generation time (from phpBB2)
// ( or just any time between timer_start() and timer_stop() )
function timer_stop($display = 0, $precision = 3) { //if called like timer_stop(1), will echo $timetotal
global $timestart, $timeend;
$mtime = microtime();
$mtime = explode(' ',$mtime);
$mtime = $mtime[1] + $mtime[0];
$timeend = $mtime;
$timetotal = $timeend-$timestart;
if ( $display )
echo number_format($timetotal,$precision);
return $timetotal;
}
function weblog_ping($server = '', $path = '') {
global $wp_version;
include_once (ABSPATH . WPINC . '/class-IXR.php');
// using a timeout of 3 seconds should be enough to cover slow servers
$client = new IXR_Client($server, ((!strlen(trim($path)) || ('/' == $path)) ? false : $path));
$client->timeout = 3;
$client->useragent .= ' -- WordPress/'.$wp_version;
// when set to true, this outputs debug messages by itself
$client->debug = false;
$home = trailingslashit( get_option('home') );
$blogname = encoding_wp2rss(get_settings('blogname'));
if ( !$client->query('weblogUpdates.extendedPing', $blogname, $home, get_bloginfo('rss2_url') ) ) // then try a normal ping
$client->query('weblogUpdates.ping', $blogname, $home);
}
function generic_ping($post_id = 0) {
$services = get_settings('ping_sites');
$services = preg_replace("|(\s)+|", '$1', $services); // Kill dupe lines
$services = trim($services);
if ( '' != $services ) {
$services = explode("\n", $services);
foreach ($services as $service) {
weblog_ping($service);
}
}
return $post_id;
}
// Send a Trackback
function trackback($trackback_url, $title, $excerpt, $ID) {
global $wpdb, $wp_version;
if ( empty($trackback_url) )
return;
$GLOBALS["doing_trackback"] = 1;
$title = encoding_wp2rss($title);
$excerpt = encoding_wp2rss($excerpt);
$title = urlencode($title);
$excerpt = urlencode($excerpt);
$blog_name = urlencode(get_settings('blogname'));
$tb_url = $trackback_url;
$url = urlencode(get_permalink($ID));
$query_string = "title=$title&url=$url&blog_name=$blog_name&excerpt=$excerpt";
$trackback_url = parse_url($trackback_url);
$http_request = 'POST ' . $trackback_url['path'] . ($trackback_url['query'] ? '?'.$trackback_url['query'] : '') . " HTTP/1.0\r\n";
$http_request .= 'Host: '.$trackback_url['host']."\r\n";
$http_request .= 'Content-Type: application/x-www-form-urlencoded; charset='.get_settings('blog_charset')."\r\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -