_wp_put_post_revision
() 是一個(gè)WordPress內(nèi)部函數(shù),用于將文章數(shù)據(jù)插入到 posts
表中作為文章修訂版本。這個(gè)函數(shù)不是為了插件或主題開發(fā)者設(shè)計(jì)的,而是主要用于核心功能之間的交互。它的存在是為了確保文章的修訂版本能夠正確地保存到數(shù)據(jù)庫中。
null
false
WP_Error
對象或 0
;如果成功,返回新修訂版本的ID。wp-includes/revision.php
function _wp_put_post_revision( $post = null, $autosave = false ) {
// 如果 $post 是對象,轉(zhuǎn)換為數(shù)組
if ( is_object($post) )
$post = get_object_vars( $post );
// 如果 $post 不是數(shù)組,嘗試獲取文章數(shù)據(jù)
elseif ( !is_array($post) )
$post = get_post($post, ARRAY_A);
// 檢查文章數(shù)據(jù)是否有效
if ( ! $post || empty($post['ID']) )
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) );
// 檢查是否嘗試創(chuàng)建修訂版本的修訂版本
if ( isset($post['post_type']) && 'revision' == $post['post_type'] )
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) );
// 準(zhǔn)備修訂版本數(shù)據(jù)
$post = _wp_post_revision_data( $post, $autosave );
$post = wp_slash($post); // 由于數(shù)據(jù)來自數(shù)據(jù)庫,需要進(jìn)行轉(zhuǎn)義
// 插入修訂版本
$revision_id = wp_insert_post( $post );
if ( is_wp_error($revision_id) )
return $revision_id;
// 如果修訂版本插入成功,觸發(fā)動(dòng)作
if ( $revision_id ) {
do_action( '_wp_put_post_revision', $revision_id );
}
return $revision_id;
}
wp_save_post_revision
函數(shù),并遵循其官方文檔中的指導(dǎo)。_wp_register_meta_args_whitelist
()是一個(gè)wordpress內(nèi)部函數(shù),用于過濾 register_meta()
函數(shù)的參數(shù),只保留白名單中的參數(shù)。這個(gè)函數(shù)不是為了插件或主題開發(fā)者設(shè)計(jì)的,而是主要用于核心功能之間的交互。它的存在是為了確保 register_meta()
函數(shù)的參數(shù)在未來發(fā)生變化時(shí),不會(huì)因?yàn)橐胄碌膮?shù)而導(dǎo)致舊代碼出現(xiàn)問題。
register_meta()
傳遞過來的參數(shù)數(shù)組。register_meta()
函數(shù)的默認(rèn)參數(shù)數(shù)組。wp-includes/meta.php
function _wp_register_meta_args_whitelist( $args, $default_args ) {
$whitelist = array_keys( $default_args ); // 獲取默認(rèn)參數(shù)的鍵名作為白名單
// 遍歷傳遞進(jìn)來的參數(shù)
foreach ( $args as $key => $value ) {
if ( ! in_array( $key, $whitelist ) ) { // 如果參數(shù)不在白名單中
unset( $args[ $key ] ); // 移除該參數(shù)
}
}
return $args; // 返回過濾后的參數(shù)數(shù)組
}
register_meta()
函數(shù),并遵循其官方文檔中的指導(dǎo)。add_post_meta
、update_post_meta
等函數(shù)進(jìn)行添加和更新。DELETE FROM wp_posts WHERE post_type = "revision";
,刪除未批準(zhǔn)的評論的語句是DELETE from wp_comments WHERE comment_approved = '0';
。通過上述方法,你可以有效地清理WordPress數(shù)據(jù)庫中的冗余數(shù)據(jù),提高網(wǎng)站性能。記得在進(jìn)行任何清理操作前備份數(shù)據(jù)庫,以確保數(shù)據(jù)安全。
]]>DELETE FROM wp_posts WHERE post_type = 'revision';
wp 媒體管理器上傳圖片的時(shí)候同事會(huì)成才attachment 文章格式,如果你不需要根據(jù)這個(gè)檢索圖片的話也可以刪掉。
DELETE FROM wp_posts WHERE post_type = 'attachment';
wp_postmeta是重災(zāi)區(qū),可以執(zhí)行以下命令
DELETE FROM wp_postmeta WHERE meta_key = '_edit_lock';
DELETE FROM wp_postmeta WHERE meta_key = '_edit_last';
DELETE FROM wp_postmeta WHERE meta_key = '_revision-control';
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT post_id FROM wp_posts);
DELETE FROM wp_postmeta WHERE meta_key = '_wp_old_slug';
DELETE FROM wp_postmeta WHERE meta_key = '_revision-control';
DELETE FROM wp_postmeta WHERE meta_value = '{{unknown}}';
如果你不需要管理你的多媒體文件可以使用下面這兩句
DELETE FROM wp_postmeta WHERE meta_key = '_wp_attached_file';
DELETE FROM wp_postmeta WHERE meta_key = '_wp_attachment_metadata';
綜合到一起可直接執(zhí)行下面兩句
DELETE FROM wp_postmeta WHERE meta_key in ('_edit_lock', '_edit_last', '_wp_old_slug', '_revision-control', '{{unknown}}', '_wp_attached_file', '_wp_attachment_metadata');
DELETE FROM wp_postmeta WHERE post_id NOT IN (SELECT id FROM wp_posts);
刪除所有垃圾評論
DELETE FROM wp_comments WHERE comment_type = 'trash';
刪除所有待審評論
DELETE FROM wp_comments WHERE comment_approved = '0';
一般來說直接刪掉wp_commentmeta就可以了,我是完全不使用這貨的,如果你使用了commentmeta存儲(chǔ)信息請謹(jǐn)慎執(zhí)行此句
TRUNCATE TABLE wp_commentmeta
wp_options
尤其是使用我的網(wǎng)易云音樂和牧風(fēng)的hermit,都會(huì)在wp_options表中產(chǎn)生大量垃圾數(shù)據(jù)。
DELETE FROM wp_options WHERE option_name LIKE 'transient%';
DELETE FROM wp_options WHERE option_name LIKE 'transient_timeout%'
這樣會(huì)刪除所有臨時(shí)數(shù)據(jù),包括未過期的,謹(jǐn)慎使用。
File: wp-includes/post.php
function _wp_relative_upload_path( $path ) {
$new_path = $path;
$uploads = wp_get_upload_dir();
if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) {
$new_path = str_replace( $uploads['basedir'], '', $new_path );
$new_path = ltrim( $new_path, '/' );
}
/**
* Filters the relative path to an uploaded file.
*
* @since 2.9.0
*
* @param string $new_path Relative path to the file.
* @param string $path Full path to the file.
*/
return apply_filters( '_wp_relative_upload_path', $new_path, $path );
}
Version | 描述 |
---|---|
2.9.0 | Introduced. |
在WordPress中,_wp_relative_upload_path()
是一個(gè)內(nèi)部函數(shù),用于獲取上傳文件相對于WordPress上傳目錄的相對路徑。這個(gè)函數(shù)通常不推薦在主題或插件代碼中直接使用,因?yàn)樗赡軙?huì)在沒有通知的情況下在WordPress的未來版本中更改。
但是,如果你需要這個(gè)函數(shù)的用法,以下是其基本的使用方法:
$wp_upload_dir = wp_upload_dir();
$relative_path = _wp_relative_upload_path( $file_path );
這里是一個(gè)完整的例子:
// 獲取WordPress上傳目錄信息
$wp_upload_dir = wp_upload_dir();
// 定義一個(gè)文件路徑,假設(shè)這個(gè)文件位于上傳目錄內(nèi)
$file_path = $wp_upload_dir['basedir'] . '/my-custom-folder/my-image.jpg';
// 使用 _wp_relative_upload_path() 獲取相對路徑
$relative_path = _wp_relative_upload_path( $file_path );
// 輸出相對路徑
echo $relative_path; // 輸出可能是:my-custom-folder/my-image.jpg
在這個(gè)例子中,wp_upload_dir()
函數(shù)用于獲取WordPress的上傳目錄信息,包括基礎(chǔ)的URL和文件路徑。然后,我們使用 _wp_relative_upload_path()
函數(shù)傳入一個(gè)完整的文件路徑,該函數(shù)會(huì)返回該路徑相對于上傳目錄的相對路徑。
以下是 _wp_relative_upload_path()
函數(shù)的參數(shù):
$file_path
:需要獲取相對路徑的文件的完整路徑。false
。_wp_relative_upload_path()
是一個(gè)內(nèi)部函數(shù),因此最好避免在公共代碼中使用它。如果你需要獲取文件相對于上傳目錄的路徑,可以考慮以下替代方法:$wp_upload_dir = wp_upload_dir();
$basedir = trailingslashit( $wp_upload_dir['basedir'] );
// 確保文件位于上傳目錄內(nèi)
if ( 0 === strpos( $file_path, $basedir ) ) {
$relative_path = substr( $file_path, strlen( $basedir ) );
} else {
// 文件不在上傳目錄內(nèi)
$relative_path = false;
}
// 輸出相對路徑或錯(cuò)誤處理
if ( false !== $relative_path ) {
echo $relative_path;
} else {
// 錯(cuò)誤處理
echo 'File is not in the upload directory.';
}
在上面的代碼中,我們使用了 wp_upload_dir()
來獲取上傳目錄的路徑,并使用 strpos()
和 substr()
來計(jì)算相對路徑。這種方法不依賴于內(nèi)部函數(shù),因此更加穩(wěn)定和兼容。
File: wp-includes/widgets.php
function _wp_sidebars_changed() {
global $sidebars_widgets;
if ( ! is_array( $sidebars_widgets ) )
$sidebars_widgets = wp_get_sidebars_widgets();
retrieve_widgets(true);
}
Version | 描述 |
---|---|
3.3.0 | Introduced. |
在WordPress中,_wp_sidebars_changed()
是一個(gè)內(nèi)部函數(shù),用于在側(cè)邊欄(widget areas)被添加、更新或刪除時(shí)更新側(cè)邊欄的元數(shù)據(jù)。這個(gè)函數(shù)通常不應(yīng)該在你的主題或插件代碼中直接調(diào)用,因?yàn)樗饕菫閃ordPress內(nèi)部使用而設(shè)計(jì)的。
不過,了解這個(gè)函數(shù)的工作方式可能有助于你在開發(fā)主題或插件時(shí)理解WordPress側(cè)邊欄和widget系統(tǒng)的工作原理。_wp_sidebars_changed()
函數(shù)不接受任何參數(shù),并且沒有返回值。當(dāng)你在管理后臺(tái)添加、編輯或刪除側(cè)邊欄時(shí),WordPress會(huì)在內(nèi)部調(diào)用這個(gè)函數(shù)來更新側(cè)邊欄的元數(shù)據(jù)。
下面是該函數(shù)的基本用法:
_wp_sidebars_changed();
由于這個(gè)函數(shù)是內(nèi)部的,并且不面向開發(fā)者公開,因此你通常不需要直接使用它。WordPress核心會(huì)在適當(dāng)?shù)臅r(shí)機(jī)自動(dòng)調(diào)用這個(gè)函數(shù)。
如果你正在開發(fā)一個(gè)需要操作側(cè)邊欄(widget areas)的插件或主題,你應(yīng)該使用WordPress提供的公共API函數(shù)來添加、刪除或修改側(cè)邊欄。以下是一些相關(guān)的函數(shù):
register_sidebar()
:用于注冊一個(gè)新的側(cè)邊欄。unregister_sidebar()
:用于注銷一個(gè)已注冊的側(cè)邊欄。wp_get_sidebars_widgets()
:獲取當(dāng)前激活的側(cè)邊欄和小部件。register_sidebar()
來添加一個(gè)新的側(cè)邊欄:function my_custom_sidebar() {
// 注冊一個(gè)新的側(cè)邊欄
register_sidebar( array(
'name' => 'My Custom Sidebar',
'id' => 'my-custom-sidebar',
'description' => 'A custom sidebar for my theme',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
) );
}
// 在 'widgets_init' 動(dòng)作中添加側(cè)邊欄
add_action( 'widgets_init', 'my_custom_sidebar' );
在上面的代碼中,我們定義了一個(gè)函數(shù) my_custom_sidebar()
,該函數(shù)使用 register_sidebar()
注冊一個(gè)新的側(cè)邊欄。然后,我們使用 add_action()
將該函數(shù)掛鉤到 widgets_init
動(dòng)作上,這樣在WordPress初始化側(cè)邊欄時(shí)就會(huì)調(diào)用我們的函數(shù)。
請記住,直接使用WordPress的內(nèi)部函數(shù)可能會(huì)導(dǎo)致代碼不穩(wěn)定,因?yàn)樗鼈兛赡茉谖磥淼腤ordPress版本中更改或被移除。始終推薦使用公開的API函數(shù)和鉤子來開發(fā)插件和主題。
File: wp-includes/formatting.php
function _wp_specialchars( $string, $quote_style = ENT_NOQUOTES, $charset = false, $double_encode = false ) {
$string = (string) $string;
if ( 0 === strlen( $string ) )
return '';
// Don't bother if there are no specialchars - saves some processing
if ( ! preg_match( '/[&<>"\']/', $string ) )
return $string;
// Account for the previous behaviour of the function when the $quote_style is not an accepted value
if ( empty( $quote_style ) )
$quote_style = ENT_NOQUOTES;
elseif ( ! in_array( $quote_style, array( 0, 2, 3, 'single', 'double' ), true ) )
$quote_style = ENT_QUOTES;
// Store the site charset as a static to avoid multiple calls to wp_load_alloptions()
if ( ! $charset ) {
static $_charset = null;
if ( ! isset( $_charset ) ) {
$alloptions = wp_load_alloptions();
$_charset = isset( $alloptions['blog_charset'] ) ? $alloptions['blog_charset'] : '';
}
$charset = $_charset;
}
if ( in_array( $charset, array( 'utf8', 'utf-8', 'UTF8' ) ) )
$charset = 'UTF-8';
$_quote_style = $quote_style;
if ( $quote_style === 'double' ) {
$quote_style = ENT_COMPAT;
$_quote_style = ENT_COMPAT;
} elseif ( $quote_style === 'single' ) {
$quote_style = ENT_NOQUOTES;
}
if ( ! $double_encode ) {
// Guarantee every &entity; is valid, convert &garbage; into &garbage;
// This is required for PHP < 5.4.0 because ENT_HTML401 flag is unavailable.
$string = wp_kses_normalize_entities( $string );
}
$string = @htmlspecialchars( $string, $quote_style, $charset, $double_encode );
// Back-compat.
if ( 'single' === $_quote_style )
$string = str_replace( "'", ''', $string );
return $string;
}
Version | 描述 |
---|---|
1.2.2 | Introduced. |
在WordPress中,_wp_specialchars()
是一個(gè)內(nèi)部函數(shù),用于轉(zhuǎn)義HTML實(shí)體,以避免XSS攻擊和其他安全問題。這個(gè)函數(shù)通常在WordPress內(nèi)部使用,不是公開文檔化的,因此不應(yīng)該在公開的主題或插件代碼中使用。_wp_specialchars()
函數(shù)類似于PHP的 htmlspecialchars()
函數(shù),但是它有一些WordPress特定的默認(rèn)設(shè)置。
以下是 _wp_specialchars()
函數(shù)的基本用法:
string _wp_specialchars( string $text, string $quote_style = ENT_NOQUOTES, string $charset = false, bool $double_encode = false )
參數(shù)解釋如下:
$text
:要轉(zhuǎn)義的字符串。$quote_style
:如何處理引號(hào)。默認(rèn)為 ENT_NOQUOTES
,表示不轉(zhuǎn)義引號(hào)。其他可能的值包括 ENT_COMPAT
(只轉(zhuǎn)義雙引號(hào))、ENT_QUOTES
(轉(zhuǎn)義雙引號(hào)和單引號(hào))。$charset
:字符集。如果為 false
,將使用WordPress的默認(rèn)字符集。$double_encode
:是否對已經(jīng)轉(zhuǎn)義的實(shí)體進(jìn)行再次轉(zhuǎn)義。默認(rèn)為 false
。_wp_specialchars()
函數(shù)的示例:<?php
// 要轉(zhuǎn)義的字符串
$text = 'The "quick" brown fox jumps over the lazy dog.';
// 使用 _wp_specialchars() 轉(zhuǎn)義字符串
$escaped_text = _wp_specialchars( $text, ENT_QUOTES );
// 輸出轉(zhuǎn)義后的字符串
echo $escaped_text;
?>
在這個(gè)例子中,我們轉(zhuǎn)義了一個(gè)包含引號(hào)的字符串,并且指定了 ENT_QUOTES
作為 $quote_style
參數(shù),這樣單引號(hào)和雙引號(hào)都會(huì)被轉(zhuǎn)義。
需要注意的是,由于 _wp_specialchars()
是一個(gè)內(nèi)部函數(shù),其使用并不推薦在公開的主題或插件代碼中。如果你需要在WordPress中轉(zhuǎn)義HTML實(shí)體,應(yīng)該使用WordPress提供的公開函數(shù),如 esc_html()
、esc_attr()
或 esc_textarea()
等。這些函數(shù)都是專門設(shè)計(jì)來在WordPress中安全地轉(zhuǎn)義輸出內(nèi)容的。
例如,如果你想要轉(zhuǎn)義HTML內(nèi)容,你可以使用:
echo esc_html( $text );
如果你需要轉(zhuǎn)義HTML屬性,你可以使用:
echo esc_attr( $text );
使用這些公開函數(shù)可以確保你的代碼與WordPress的未來版本兼容,并且更加安全。
]]>File: wp-includes/functions.php
function _wp_timezone_choice_usort_callback( $a, $b ) {
// Don't use translated versions of Etc
if ( 'Etc' === $a['continent'] && 'Etc' === $b['continent'] ) {
// Make the order of these more like the old dropdown
if ( 'GMT+' === substr( $a['city'], 0, 4 ) && 'GMT+' === substr( $b['city'], 0, 4 ) ) {
return -1 * ( strnatcasecmp( $a['city'], $b['city'] ) );
}
if ( 'UTC' === $a['city'] ) {
if ( 'GMT+' === substr( $b['city'], 0, 4 ) ) {
return 1;
}
return -1;
}
if ( 'UTC' === $b['city'] ) {
if ( 'GMT+' === substr( $a['city'], 0, 4 ) ) {
return -1;
}
return 1;
}
return strnatcasecmp( $a['city'], $b['city'] );
}
if ( $a['t_continent'] == $b['t_continent'] ) {
if ( $a['t_city'] == $b['t_city'] ) {
return strnatcasecmp( $a['t_subcity'], $b['t_subcity'] );
}
return strnatcasecmp( $a['t_city'], $b['t_city'] );
} else {
// Force Etc to the bottom of the list
if ( 'Etc' === $a['continent'] ) {
return 1;
}
if ( 'Etc' === $b['continent'] ) {
return -1;
}
return strnatcasecmp( $a['t_continent'], $b['t_continent'] );
}
}
Version | 描述 |
---|---|
2.9.0 | Introduced. |
在WordPress中,_wp_timezone_choice_usort_callback()
是一個(gè)內(nèi)部函數(shù),用于對時(shí)區(qū)選擇進(jìn)行排序。這個(gè)函數(shù)不是公開文檔化的,通常用于WordPress后臺(tái)設(shè)置中,特別是當(dāng)用戶需要選擇他們的時(shí)區(qū)時(shí)。
這個(gè)函數(shù)是作為回調(diào)函數(shù)傳遞給 usort()
函數(shù),用于比較兩個(gè)時(shí)區(qū)名稱,并按字母順序?qū)λ鼈冞M(jìn)行排序。
以下是 _wp_timezone_choice_usort_callback()
函數(shù)的基本用法:
usort( $timezones, '_wp_timezone_choice_usort_callback' );
參數(shù)解釋如下:
$timezones
:一個(gè)包含時(shí)區(qū)名稱的數(shù)組。_wp_timezone_choice_usort_callback()
的步驟:usort()
和回調(diào)函數(shù)進(jìn)行排序:然后,使用 usort()
函數(shù)和 _wp_timezone_choice_usort_callback()
作為回調(diào)來對時(shí)區(qū)數(shù)組進(jìn)行排序。_wp_timezone_choice_usort_callback()
函數(shù)的例子:<?php
// 獲取所有時(shí)區(qū)
$all_timezones = timezone_identifiers_list();
// 過濾掉一些不需要的時(shí)區(qū)(可選)
$timezones = array_filter( $all_timezones, function( $tz ) {
return strpos( $tz, '/' ) !== false;
});
// 使用 _wp_timezone_choice_usort_callback() 對時(shí)區(qū)進(jìn)行排序
usort( $timezones, '_wp_timezone_choice_usort_callback' );
// 輸出排序后的時(shí)區(qū)列表
foreach ( $timezones as $timezone ) {
echo $timezone . '<br>';
}
?>
在這個(gè)例子中,我們首先獲取了所有時(shí)區(qū)標(biāo)識(shí)符,然后過濾掉了一些不需要的時(shí)區(qū)(這是一個(gè)可選步驟)。接著,我們使用 usort()
函數(shù)和 _wp_timezone_choice_usort_callback()
回調(diào)函數(shù)對時(shí)區(qū)數(shù)組進(jìn)行了排序,并輸出了排序后的時(shí)區(qū)列表。
需要注意的是,由于 _wp_timezone_choice_usort_callback()
是一個(gè)內(nèi)部函數(shù),它的使用并不推薦在公開的主題或插件代碼中。這個(gè)函數(shù)可能在WordPress的未來版本中發(fā)生變化或被移除。如果你需要對時(shí)區(qū)進(jìn)行排序,建議直接使用PHP內(nèi)置的排序函數(shù),如 sort()
或 asort()
,并結(jié)合適當(dāng)?shù)谋容^函數(shù)來達(dá)到相同的效果。
File: wp-includes/http.php
function _wp_translate_php_url_constant_to_key( $constant ) {
$translation = array(
PHP_URL_SCHEME => 'scheme',
PHP_URL_HOST => 'host',
PHP_URL_PORT => 'port',
PHP_URL_USER => 'user',
PHP_URL_PASS => 'pass',
PHP_URL_PATH => 'path',
PHP_URL_QUERY => 'query',
PHP_URL_FRAGMENT => 'fragment',
);
if ( isset( $translation[ $constant ] ) ) {
return $translation[ $constant ];
} else {
return false;
}
}
Version | 描述 |
---|---|
4.7.0 | Introduced. |
在WordPress中,_wp_translate_php_url_constant_to_key()
是一個(gè)內(nèi)部函數(shù),它用于將PHP的URL常量轉(zhuǎn)換為WordPress內(nèi)部用于查詢字符串的鍵。這個(gè)函數(shù)不是公開文檔化的,它通常用于WordPress內(nèi)部,特別是在處理HTTP請求和響應(yīng)時(shí)。
由于這個(gè)函數(shù)是內(nèi)部的,它的使用并不推薦在公開的主題或插件代碼中。然而,如果你好奇或者出于某種特定原因需要了解它的用法,以下是其基本結(jié)構(gòu):
_wp_translate_php_url_constant_to_key( $constant_name );
參數(shù)解釋如下:
$constant_name
:一個(gè)PHP URL常量的名稱,如 PHP_URL_HOST
、PHP_URL_PATH
等。_wp_translate_php_url_constant_to_key()
:<?php
// 假設(shè)我們有一個(gè)URL
$url = 'http://www.example.com/path/to/resource?query=string';
// 解析URL
$parsed_url = parse_url( $url );
// 使用 _wp_translate_php_url_constant_to_key() 來獲取WordPress內(nèi)部鍵
$host_key = _wp_translate_php_url_constant_to_key( 'PHP_URL_HOST' );
$path_key = _wp_translate_php_url_constant_to_key( 'PHP_URL_PATH' );
// 使用WordPress內(nèi)部鍵來訪問解析后的URL部分
$host = isset( $parsed_url[$host_key] ) ? $parsed_url[$host_key] : '';
$path = isset( $parsed_url[$path_key] ) ? $parsed_url[$path_key] : '';
// 輸出結(jié)果
echo 'Host: ' . $host . '<br>';
echo 'Path: ' . $path;
?>
在這個(gè)例子中,我們首先解析了一個(gè)URL,然后使用 _wp_translate_php_url_constant_to_key()
函數(shù)來獲取WordPress內(nèi)部用來表示主機(jī)和路徑的鍵。接著,我們使用這些鍵來訪問解析后的URL數(shù)組中的相應(yīng)部分。
需要注意的是,由于 _wp_translate_php_url_constant_to_key()
是一個(gè)內(nèi)部函數(shù),它可能在WordPress的未來版本中發(fā)生變化或被移除。因此,你應(yīng)該避免在公開的主題或插件代碼中使用它。如果你需要處理URL,直接使用PHP的 parse_url()
函數(shù)并結(jié)合WordPress公開的函數(shù)和方法通常是更安全和更推薦的做法。
File: wp-includes/revision.php
function _wp_upgrade_revisions_of_post( $post, $revisions ) {
global $wpdb;
// Add post option exclusively
$lock = "revision-upgrade-{$post->ID}";
$now = time();
$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'no') /* LOCK */", $lock, $now ) );
if ( ! $result ) {
// If we couldn't get a lock, see how old the previous lock is
$locked = get_option( $lock );
if ( ! $locked ) {
// Can't write to the lock, and can't read the lock.
// Something broken has happened
return false;
}
if ( $locked > $now - 3600 ) {
// Lock is not too old: some other process may be upgrading this post. Bail.
return false;
}
// Lock is too old - update it (below) and continue
}
// If we could get a lock, re-"add" the option to fire all the correct filters.
update_option( $lock, $now );
reset( $revisions );
$add_last = true;
do {
$this_revision = current( $revisions );
$prev_revision = next( $revisions );
$this_revision_version = _wp_get_post_revision_version( $this_revision );
// Something terrible happened
if ( false === $this_revision_version )
continue;
// 1 is the latest revision version, so we're already up to date.
// No need to add a copy of the post as latest revision.
if ( 0 < $this_revision_version ) {
$add_last = false;
continue;
}
// Always update the revision version
$update = array(
'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ),
);
// If this revision is the oldest revision of the post, i.e. no $prev_revision,
// the correct post_author is probably $post->post_author, but that's only a good guess.
// Update the revision version only and Leave the author as-is.
if ( $prev_revision ) {
$prev_revision_version = _wp_get_post_revision_version( $prev_revision );
// If the previous revision is already up to date, it no longer has the information we need :(
if ( $prev_revision_version < 1 )
$update['post_author'] = $prev_revision->post_author;
}
// Upgrade this revision
$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) );
if ( $result )
wp_cache_delete( $this_revision->ID, 'posts' );
} while ( $prev_revision );
delete_option( $lock );
// Add a copy of the post as latest revision.
if ( $add_last )
wp_save_post_revision( $post->ID );
return true;
}
Version | 描述 |
---|---|
3.6.0 | Introduced. |
在WordPress中,wp_upgrade_revisions_of_post()
函數(shù)用于將舊版本的帖子修訂版本升級到新版本的帖子修訂系統(tǒng)。這個(gè)函數(shù)通常在插件或主題的代碼中使用,或者在WordPress核心更新過程中用于處理修訂版本的數(shù)據(jù)遷移。
以下是 wp_upgrade_revisions_of_post()
函數(shù)的基本用法:
wp_upgrade_revisions_of_post( $post_id );
參數(shù)解釋如下:
$post_id
:要升級修訂版本的帖子ID。wp_upgrade_revisions_of_post()
的步驟:wp_upgrade_revisions_of_post()
函數(shù),并傳入相應(yīng)的帖子ID。wp_upgrade_revisions_of_post()
函數(shù)的例子:<?php
// 假設(shè)我們有一個(gè)特定的帖子ID
$post_id = 123;
// 升級該帖子的修訂版本
wp_upgrade_revisions_of_post( $post_id );
?>
在這個(gè)例子中,我們假設(shè)有一個(gè)帖子ID為123,然后我們調(diào)用 wp_upgrade_revisions_of_post()
函數(shù)來升級這個(gè)帖子的修訂版本。
需要注意的是,這個(gè)函數(shù)主要用于WordPress內(nèi)部的數(shù)據(jù)遷移和升級過程。通常,開發(fā)者或網(wǎng)站管理員不需要直接使用這個(gè)函數(shù),因?yàn)閃ordPress會(huì)自動(dòng)處理帖子修訂版本的升級。
此外,wp_upgrade_revisions_of_post()
函數(shù)可能在未來的WordPress版本中被棄用或更改,因?yàn)樗婕暗絎ordPress內(nèi)部的數(shù)據(jù)處理。因此,除非你明確知道自己在做什么,并且有充分的理由需要手動(dòng)處理帖子修訂版本的升級,否則建議不要在常規(guī)的插件或主題開發(fā)中使用這個(gè)函數(shù)。如果你確實(shí)需要使用它,請確保對WordPress核心有深入的了解,并且已經(jīng)對數(shù)據(jù)進(jìn)行備份,以防萬一。