99久久精品国产观看,中文在线中文资源,91久久电影 http://www.lalz.net 你的IT之家,站長(zhǎng)網(wǎng)絡(luò)技術(shù)學(xué)習(xí)、網(wǎng)站賺錢(qián)方法交流之家 Wed, 20 Nov 2024 04:20:49 +0000 zh-Hans hourly 1 https://wordpress.org/?v=6.8.1 WordPress函數(shù)_wp_put_post_revision()用法 http://www.lalz.net/7266.html http://www.lalz.net/7266.html#respond Sun, 17 Nov 2024 00:33:00 +0000 http://www.lalz.net/?p=7266 描述

_wp_put_post_revision() 是一個(gè)WordPress內(nèi)部函數(shù),用于將文章數(shù)據(jù)插入到 posts 表中作為文章修訂版本。這個(gè)函數(shù)不是為了插件或主題開(kāi)發(fā)者設(shè)計(jì)的,而是主要用于核心功能之間的交互。它的存在是為了確保文章的修訂版本能夠正確地保存到數(shù)據(jù)庫(kù)中。

參數(shù)

  • $post (int|WP_Post|array|null) (可選) – 文章ID、文章對(duì)象或文章數(shù)組。
  • 默認(rèn)值:null
  • $autosave (bool) (可選) – 修訂版本是否為自動(dòng)保存。
  • 默認(rèn)值:false

返回值

  • (int|WP_Error) 如果發(fā)生錯(cuò)誤,返回 WP_Error 對(duì)象或 0;如果成功,返回新修訂版本的ID。

源代碼位置

  • 文件:wp-includes/revision.php

函數(shù)講解

function _wp_put_post_revision( $post = null, $autosave = false ) {
    // 如果 $post 是對(duì)象,轉(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ù)來(lái)自數(shù)據(jù)庫(kù),需要進(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;
}

注意事項(xiàng)

  • 由于這是一個(gè)私有函數(shù),通常情況下你不應(yīng)該在自己的插件或主題中直接調(diào)用它。如果你需要管理文章修訂版本,請(qǐng)使用 wp_save_post_revision 函數(shù),并遵循其官方文檔中的指導(dǎo)。
  • 如果你在開(kāi)發(fā)過(guò)程中遇到了需要保存文章修訂版本的類(lèi)似需求,可以參考此函數(shù)的實(shí)現(xiàn)方式來(lái)編寫(xiě)自己的邏輯。

術(shù)語(yǔ)解釋

  • 修訂版本:文章的歷史版本,用于記錄文章的修改歷史。
  • 自動(dòng)保存:WordPress 自動(dòng)保存的文章草稿,通常每分鐘自動(dòng)保存一次。
  • 動(dòng)作:WordPress 中的動(dòng)作(action)是一種鉤子(hook),允許在特定事件發(fā)生時(shí)執(zhí)行某些操作。

]]>
http://www.lalz.net/7266.html/feed 0
WordPress函數(shù)_wp_register_meta_args_whitelist()用法 http://www.lalz.net/7261.html http://www.lalz.net/7261.html#respond Thu, 07 Nov 2024 00:33:00 +0000 http://www.lalz.net/?p=7261

描述

_wp_register_meta_args_whitelist ()是一個(gè)wordpress內(nèi)部函數(shù),用于過(guò)濾 register_meta() 函數(shù)的參數(shù),只保留白名單中的參數(shù)。這個(gè)函數(shù)不是為了插件或主題開(kāi)發(fā)者設(shè)計(jì)的,而是主要用于核心功能之間的交互。它的存在是為了確保 register_meta() 函數(shù)的參數(shù)在未來(lái)發(fā)生變化時(shí),不會(huì)因?yàn)橐胄碌膮?shù)而導(dǎo)致舊代碼出現(xiàn)問(wèn)題。

參數(shù)

  • $args (array) (必需) – 從 register_meta() 傳遞過(guò)來(lái)的參數(shù)數(shù)組。
  • $default_args (array) (必需) – register_meta() 函數(shù)的默認(rèn)參數(shù)數(shù)組。

返回值

  • (array) 過(guò)濾后的參數(shù)數(shù)組,只包含白名單中的參數(shù)。

函數(shù)源代碼位置

  • 文件:wp-includes/meta.php

函數(shù)講解

function _wp_register_meta_args_whitelist( $args, $default_args ) {
    $whitelist = array_keys( $default_args ); // 獲取默認(rèn)參數(shù)的鍵名作為白名單

    // 遍歷傳遞進(jìn)來(lái)的參數(shù)
    foreach ( $args as $key => $value ) {
        if ( ! in_array( $key, $whitelist ) ) { // 如果參數(shù)不在白名單中
            unset( $args[ $key ] ); // 移除該參數(shù)
        }
    }

    return $args; // 返回過(guò)濾后的參數(shù)數(shù)組
}

注意事項(xiàng)

  • 由于這是一個(gè)私有函數(shù),通常情況下你不應(yīng)該在自己的插件或主題中直接調(diào)用它。如果你需要注冊(cè)自定義元數(shù)據(jù),請(qǐng)使用 register_meta() 函數(shù),并遵循其官方文檔中的指導(dǎo)。
  • 如果你在開(kāi)發(fā)過(guò)程中遇到了需要過(guò)濾參數(shù)的類(lèi)似需求,可以參考此函數(shù)的實(shí)現(xiàn)方式來(lái)編寫(xiě)自己的過(guò)濾邏輯。

術(shù)語(yǔ)解釋

  • 白名單:允許使用的參數(shù)列表。在這個(gè)函數(shù)中,只有白名單中的參數(shù)才會(huì)被保留。
  • 元數(shù)據(jù):與WordPress中的帖子、用戶(hù)、評(píng)論或術(shù)語(yǔ)等實(shí)體相關(guān)聯(lián)的額外信息。元數(shù)據(jù)可以通過(guò) add_post_meta、update_post_meta 等函數(shù)進(jìn)行添加和更新。

]]>
http://www.lalz.net/7261.html/feed 0
WordPress 清理數(shù)據(jù)庫(kù)冗余數(shù)據(jù) http://www.lalz.net/7260.html http://www.lalz.net/7260.html#respond Mon, 04 Nov 2024 00:32:00 +0000 http://www.lalz.net/?p=7260 清理WordPress數(shù)據(jù)庫(kù)中的冗余數(shù)據(jù)可以通過(guò)以下幾種方法:

  1. 使用WP-Optimize插件:WP-Optimize是一個(gè)數(shù)據(jù)清理、壓縮和緩存插件,可以幫助你清理WordPress數(shù)據(jù)庫(kù)中的不必要數(shù)據(jù),如垃圾評(píng)論、過(guò)期的修訂版本、未使用的標(biāo)簽和元數(shù)據(jù)等。它還提供了數(shù)據(jù)庫(kù)優(yōu)化統(tǒng)計(jì)信息,并支持自動(dòng)優(yōu)化數(shù)據(jù)庫(kù)。WP-Optimize提供免費(fèi)版本,并有不同級(jí)別的付費(fèi)版本以滿(mǎn)足不同用戶(hù)的需求。
  2. 使用WP-Sweep插件:WP-Sweep插件可以幫助你清理WordPress數(shù)據(jù)庫(kù)中的垃圾數(shù)據(jù)。它通過(guò)分析數(shù)據(jù)庫(kù)并提供報(bào)告,讓你知道可以清理哪些內(nèi)容。你可以選擇單獨(dú)清理項(xiàng)目或者全部清理。WP-Sweep使用WordPress的刪除功能,而不是直接運(yùn)行MySQL查詢(xún),這樣可以避免留下孤立數(shù)據(jù)。
  3. 手動(dòng)清理:如果你熟悉SQL,可以通過(guò)執(zhí)行SQL語(yǔ)句來(lái)手動(dòng)刪除數(shù)據(jù)庫(kù)中的冗余數(shù)據(jù)。例如,刪除所有修訂版本的語(yǔ)句是DELETE FROM wp_posts WHERE post_type = "revision";,刪除未批準(zhǔn)的評(píng)論的語(yǔ)句是DELETE from wp_comments WHERE comment_approved = '0';
  4. 使用phpMyAdmin:phpMyAdmin是一個(gè)強(qiáng)大的數(shù)據(jù)庫(kù)管理工具,通過(guò)它可以直接執(zhí)行SQL查詢(xún)和操作,優(yōu)化數(shù)據(jù)庫(kù)表,修復(fù)損壞的表,并執(zhí)行其他管理任務(wù)。
  5. 使用WP Cleaner Pro插件:WP Cleaner Pro是一個(gè)性?xún)r(jià)比極高的WordPress數(shù)據(jù)庫(kù)清理方案,提供清理計(jì)劃任務(wù)工具和整個(gè)數(shù)據(jù)庫(kù)的備份功能。它可以清除超過(guò)25種類(lèi)型的數(shù)據(jù)項(xiàng),全面優(yōu)化數(shù)據(jù)庫(kù)以確保它不會(huì)拖慢網(wǎng)站速度。

通過(guò)上述方法,你可以有效地清理WordPress數(shù)據(jù)庫(kù)中的冗余數(shù)據(jù),提高網(wǎng)站性能。記得在進(jìn)行任何清理操作前備份數(shù)據(jù)庫(kù),以確保數(shù)據(jù)安全。

]]>
http://www.lalz.net/7260.html/feed 0
WordPress 清理數(shù)據(jù)庫(kù)冗余數(shù)據(jù)命令 http://www.lalz.net/7259.html http://www.lalz.net/7259.html#respond Sun, 03 Nov 2024 00:37:00 +0000 http://www.lalz.net/?p=7259
刪除文章修訂版本

DELETE FROM wp_posts WHERE post_type = 'revision';
wp 媒體管理器上傳圖片的時(shí)候同事會(huì)成才attachment 文章格式,如果你不需要根據(jù)這個(gè)檢索圖片的話(huà)也可以刪掉。

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

刪除所有垃圾評(píng)論

DELETE FROM wp_comments WHERE comment_type = 'trash';
刪除所有待審評(píng)論

DELETE FROM wp_comments WHERE comment_approved = '0';

一般來(lái)說(shuō)直接刪掉wp_commentmeta就可以了,我是完全不使用這貨的,如果你使用了commentmeta存儲(chǔ)信息請(qǐng)謹(jǐn)慎執(zhí)行此句

TRUNCATE TABLE wp_commentmeta
wp_options

尤其是使用我的網(wǎng)易云音樂(lè)和牧風(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ù),包括未過(guò)期的,謹(jǐn)慎使用。

]]>
http://www.lalz.net/7259.html/feed 0
WordPress函數(shù)_wp_relative_upload_path()用法 http://www.lalz.net/7238.html http://www.lalz.net/7238.html#respond Sun, 15 Sep 2024 00:33:00 +0000 http://www.lalz.net/?p=7238 源代碼

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.0Introduced.

在WordPress中,_wp_relative_upload_path() 是一個(gè)內(nèi)部函數(shù),用于獲取上傳文件相對(duì)于WordPress上傳目錄的相對(duì)路徑。這個(gè)函數(shù)通常不推薦在主題或插件代碼中直接使用,因?yàn)樗赡軙?huì)在沒(méi)有通知的情況下在WordPress的未來(lái)版本中更改。
但是,如果你需要這個(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() 獲取相對(duì)路徑
$relative_path = _wp_relative_upload_path( $file_path );
// 輸出相對(duì)路徑
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ì)返回該路徑相對(duì)于上傳目錄的相對(duì)路徑。
以下是 _wp_relative_upload_path() 函數(shù)的參數(shù):

  • $file_path:需要獲取相對(duì)路徑的文件的完整路徑。
    返回值:
  • 相對(duì)于WordPress上傳目錄的文件路徑。如果文件不在上傳目錄內(nèi),函數(shù)將返回 false。
    再次強(qiáng)調(diào),由于 _wp_relative_upload_path() 是一個(gè)內(nèi)部函數(shù),因此最好避免在公共代碼中使用它。如果你需要獲取文件相對(duì)于上傳目錄的路徑,可以考慮以下替代方法:
$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;
}
// 輸出相對(duì)路徑或錯(cuò)誤處理
if ( false !== $relative_path ) {
    echo $relative_path;
} else {
    // 錯(cuò)誤處理
    echo 'File is not in the upload directory.';
}

在上面的代碼中,我們使用了 wp_upload_dir() 來(lái)獲取上傳目錄的路徑,并使用 strpos()substr() 來(lái)計(jì)算相對(duì)路徑。這種方法不依賴(lài)于內(nèi)部函數(shù),因此更加穩(wěn)定和兼容。

]]>
http://www.lalz.net/7238.html/feed 0
WordPress函數(shù)_wp_sidebars_changed()用法 http://www.lalz.net/7237.html http://www.lalz.net/7237.html#respond Sat, 14 Sep 2024 00:33:00 +0000 http://www.lalz.net/?p=7237 源代碼

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.0Introduced.

在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ì)的。
不過(guò),了解這個(gè)函數(shù)的工作方式可能有助于你在開(kāi)發(fā)主題或插件時(shí)理解WordPress側(cè)邊欄和widget系統(tǒng)的工作原理。
_wp_sidebars_changed() 函數(shù)不接受任何參數(shù),并且沒(méi)有返回值。當(dāng)你在管理后臺(tái)添加、編輯或刪除側(cè)邊欄時(shí),WordPress會(huì)在內(nèi)部調(diào)用這個(gè)函數(shù)來(lái)更新側(cè)邊欄的元數(shù)據(jù)。
下面是該函數(shù)的基本用法:

_wp_sidebars_changed();

由于這個(gè)函數(shù)是內(nèi)部的,并且不面向開(kāi)發(fā)者公開(kāi),因此你通常不需要直接使用它。WordPress核心會(huì)在適當(dāng)?shù)臅r(shí)機(jī)自動(dòng)調(diào)用這個(gè)函數(shù)。
如果你正在開(kāi)發(fā)一個(gè)需要操作側(cè)邊欄(widget areas)的插件或主題,你應(yīng)該使用WordPress提供的公共API函數(shù)來(lái)添加、刪除或修改側(cè)邊欄。以下是一些相關(guān)的函數(shù):

  • register_sidebar():用于注冊(cè)一個(gè)新的側(cè)邊欄。
  • unregister_sidebar():用于注銷(xiāo)一個(gè)已注冊(cè)的側(cè)邊欄。
  • wp_get_sidebars_widgets():獲取當(dāng)前激活的側(cè)邊欄和小部件。
    以下是一個(gè)示例,展示如何使用 register_sidebar() 來(lái)添加一個(gè)新的側(cè)邊欄:
function my_custom_sidebar() {
    // 注冊(cè)一個(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() 注冊(cè)一個(gè)新的側(cè)邊欄。然后,我們使用 add_action() 將該函數(shù)掛鉤到 widgets_init 動(dòng)作上,這樣在WordPress初始化側(cè)邊欄時(shí)就會(huì)調(diào)用我們的函數(shù)。
請(qǐng)記住,直接使用WordPress的內(nèi)部函數(shù)可能會(huì)導(dǎo)致代碼不穩(wěn)定,因?yàn)樗鼈兛赡茉谖磥?lái)的WordPress版本中更改或被移除。始終推薦使用公開(kāi)的API函數(shù)和鉤子來(lái)開(kāi)發(fā)插件和主題。

]]>
http://www.lalz.net/7237.html/feed 0
WordPress函數(shù)_wp_specialchars()用法 http://www.lalz.net/7236.html http://www.lalz.net/7236.html#respond Fri, 13 Sep 2024 00:33:00 +0000 http://www.lalz.net/?p=7236 源代碼

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 &amp;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.2Introduced.

在WordPress中,_wp_specialchars() 是一個(gè)內(nèi)部函數(shù),用于轉(zhuǎn)義HTML實(shí)體,以避免XSS攻擊和其他安全問(wèn)題。這個(gè)函數(shù)通常在WordPress內(nèi)部使用,不是公開(kāi)文檔化的,因此不應(yīng)該在公開(kāi)的主題或插件代碼中使用。
_wp_specialchars() 函數(shù)類(lèi)似于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:是否對(duì)已經(jīng)轉(zhuǎn)義的實(shí)體進(jìn)行再次轉(zhuǎn)義。默認(rèn)為 false
    以下是一個(gè)使用 _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ù),其使用并不推薦在公開(kāi)的主題或插件代碼中。如果你需要在WordPress中轉(zhuǎn)義HTML實(shí)體,應(yīng)該使用WordPress提供的公開(kāi)函數(shù),如 esc_html()esc_attr()esc_textarea() 等。這些函數(shù)都是專(zhuān)門(mén)設(shè)計(jì)來(lái)在WordPress中安全地轉(zhuǎn)義輸出內(nèi)容的。
例如,如果你想要轉(zhuǎn)義HTML內(nèi)容,你可以使用:

echo esc_html( $text );

如果你需要轉(zhuǎn)義HTML屬性,你可以使用:

echo esc_attr( $text );

使用這些公開(kāi)函數(shù)可以確保你的代碼與WordPress的未來(lái)版本兼容,并且更加安全。

]]>
http://www.lalz.net/7236.html/feed 0
WordPress函數(shù)_wp_timezone_choice_usort_callback()用法 http://www.lalz.net/7235.html http://www.lalz.net/7235.html#respond Thu, 12 Sep 2024 00:33:00 +0000 http://www.lalz.net/?p=7235 源代碼

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.0Introduced.

在WordPress中,_wp_timezone_choice_usort_callback() 是一個(gè)內(nèi)部函數(shù),用于對(duì)時(shí)區(qū)選擇進(jìn)行排序。這個(gè)函數(shù)不是公開(kāi)文檔化的,通常用于WordPress后臺(tái)設(shè)置中,特別是當(dāng)用戶(hù)需要選擇他們的時(shí)區(qū)時(shí)。
這個(gè)函數(shù)是作為回調(diào)函數(shù)傳遞給 usort() 函數(shù),用于比較兩個(gè)時(shí)區(qū)名稱(chēng),并按字母順序?qū)λ鼈冞M(jìn)行排序。
以下是 _wp_timezone_choice_usort_callback() 函數(shù)的基本用法:

usort( $timezones, '_wp_timezone_choice_usort_callback' );

參數(shù)解釋如下:

  • $timezones:一個(gè)包含時(shí)區(qū)名稱(chēng)的數(shù)組。
    使用 _wp_timezone_choice_usort_callback() 的步驟:
  1. 獲取時(shí)區(qū)數(shù)組:首先,你需要獲取一個(gè)包含時(shí)區(qū)名稱(chēng)的數(shù)組。
  2. 使用 usort() 和回調(diào)函數(shù)進(jìn)行排序:然后,使用 usort() 函數(shù)和 _wp_timezone_choice_usort_callback() 作為回調(diào)來(lái)對(duì)時(shí)區(qū)數(shù)組進(jìn)行排序。
    下面是一個(gè)使用 _wp_timezone_choice_usort_callback() 函數(shù)的例子:
<?php
// 獲取所有時(shí)區(qū)
$all_timezones = timezone_identifiers_list();
// 過(guò)濾掉一些不需要的時(shí)區(qū)(可選)
$timezones = array_filter( $all_timezones, function( $tz ) {
    return strpos( $tz, '/' ) !== false;
});
// 使用 _wp_timezone_choice_usort_callback() 對(duì)時(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í)符,然后過(guò)濾掉了一些不需要的時(shí)區(qū)(這是一個(gè)可選步驟)。接著,我們使用 usort() 函數(shù)和 _wp_timezone_choice_usort_callback() 回調(diào)函數(shù)對(duì)時(shí)區(qū)數(shù)組進(jìn)行了排序,并輸出了排序后的時(shí)區(qū)列表。
需要注意的是,由于 _wp_timezone_choice_usort_callback() 是一個(gè)內(nèi)部函數(shù),它的使用并不推薦在公開(kāi)的主題或插件代碼中。這個(gè)函數(shù)可能在WordPress的未來(lái)版本中發(fā)生變化或被移除。如果你需要對(duì)時(shí)區(qū)進(jìn)行排序,建議直接使用PHP內(nèi)置的排序函數(shù),如 sort()asort(),并結(jié)合適當(dāng)?shù)谋容^函數(shù)來(lái)達(dá)到相同的效果。

]]>
http://www.lalz.net/7235.html/feed 0
WordPress函數(shù)_wp_translate_php_url_constant_to_key()用法 http://www.lalz.net/7234.html http://www.lalz.net/7234.html#respond Wed, 11 Sep 2024 00:33:00 +0000 http://www.lalz.net/?p=7234 源代碼

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.0Introduced.

在WordPress中,_wp_translate_php_url_constant_to_key() 是一個(gè)內(nèi)部函數(shù),它用于將PHP的URL常量轉(zhuǎn)換為WordPress內(nèi)部用于查詢(xún)字符串的鍵。這個(gè)函數(shù)不是公開(kāi)文檔化的,它通常用于WordPress內(nèi)部,特別是在處理HTTP請(qǐng)求和響應(yīng)時(shí)。
由于這個(gè)函數(shù)是內(nèi)部的,它的使用并不推薦在公開(kāi)的主題或插件代碼中。然而,如果你好奇或者出于某種特定原因需要了解它的用法,以下是其基本結(jié)構(gòu):

_wp_translate_php_url_constant_to_key( $constant_name );

參數(shù)解釋如下:

  • $constant_name:一個(gè)PHP URL常量的名稱(chēng),如 PHP_URL_HOST、PHP_URL_PATH 等。
    這個(gè)函數(shù)會(huì)返回一個(gè)字符串,這個(gè)字符串是WordPress內(nèi)部用來(lái)表示對(duì)應(yīng)URL部分的鍵。
    以下是一個(gè)示例,展示了如何使用 _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() 來(lái)獲取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)部鍵來(lái)訪(fǎng)問(wèn)解析后的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ù)來(lái)獲取WordPress內(nèi)部用來(lái)表示主機(jī)和路徑的鍵。接著,我們使用這些鍵來(lái)訪(fǎng)問(wèn)解析后的URL數(shù)組中的相應(yīng)部分。
需要注意的是,由于 _wp_translate_php_url_constant_to_key() 是一個(gè)內(nèi)部函數(shù),它可能在WordPress的未來(lái)版本中發(fā)生變化或被移除。因此,你應(yīng)該避免在公開(kāi)的主題或插件代碼中使用它。如果你需要處理URL,直接使用PHP的 parse_url() 函數(shù)并結(jié)合WordPress公開(kāi)的函數(shù)和方法通常是更安全和更推薦的做法。

]]>
http://www.lalz.net/7234.html/feed 0
WordPress函數(shù)_wp_upgrade_revisions_of_post()用法 http://www.lalz.net/7231.html http://www.lalz.net/7231.html#respond Mon, 09 Sep 2024 00:33:00 +0000 http://www.lalz.net/?p=7231 源代碼

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.0Introduced.

在WordPress中,wp_upgrade_revisions_of_post() 函數(shù)用于將舊版本的帖子修訂版本升級(jí)到新版本的帖子修訂系統(tǒng)。這個(gè)函數(shù)通常在插件或主題的代碼中使用,或者在WordPress核心更新過(guò)程中用于處理修訂版本的數(shù)據(jù)遷移。
以下是 wp_upgrade_revisions_of_post() 函數(shù)的基本用法:

wp_upgrade_revisions_of_post( $post_id );

參數(shù)解釋如下:

  • $post_id:要升級(jí)修訂版本的帖子ID。
    使用 wp_upgrade_revisions_of_post() 的步驟:
  1. 確定帖子ID:首先,你需要確定你想要升級(jí)修訂版本的帖子的ID。
  2. 調(diào)用函數(shù):使用 wp_upgrade_revisions_of_post() 函數(shù),并傳入相應(yīng)的帖子ID。
    下面是一個(gè)使用 wp_upgrade_revisions_of_post() 函數(shù)的例子:
<?php
// 假設(shè)我們有一個(gè)特定的帖子ID
$post_id = 123;
// 升級(jí)該帖子的修訂版本
wp_upgrade_revisions_of_post( $post_id );
?>

在這個(gè)例子中,我們假設(shè)有一個(gè)帖子ID為123,然后我們調(diào)用 wp_upgrade_revisions_of_post() 函數(shù)來(lái)升級(jí)這個(gè)帖子的修訂版本。
需要注意的是,這個(gè)函數(shù)主要用于WordPress內(nèi)部的數(shù)據(jù)遷移和升級(jí)過(guò)程。通常,開(kāi)發(fā)者或網(wǎng)站管理員不需要直接使用這個(gè)函數(shù),因?yàn)閃ordPress會(huì)自動(dòng)處理帖子修訂版本的升級(jí)。
此外,wp_upgrade_revisions_of_post() 函數(shù)可能在未來(lái)的WordPress版本中被棄用或更改,因?yàn)樗婕暗絎ordPress內(nèi)部的數(shù)據(jù)處理。因此,除非你明確知道自己在做什么,并且有充分的理由需要手動(dòng)處理帖子修訂版本的升級(jí),否則建議不要在常規(guī)的插件或主題開(kāi)發(fā)中使用這個(gè)函數(shù)。如果你確實(shí)需要使用它,請(qǐng)確保對(duì)WordPress核心有深入的了解,并且已經(jīng)對(duì)數(shù)據(jù)進(jìn)行備份,以防萬(wàn)一。

]]>
http://www.lalz.net/7231.html/feed 0