問題描述
我正在更新我的一個外掛,我有點卡住了不贊成的功能。
最初,我的外掛有一個全域性變數,外掛的主類被例項化並儲存在全域性變數中。這樣,使用者可以使用全域性訪問外掛類中的函式。
$GLOBALS['my_custom_plugin'] = new my_custom_plugin();
那麼,例如,在我的 FAQ 中,我有一些程式碼,顯示瞭如何從一個特定的鉤子中刪除我的一個類的函式,並新增到另一個鉤子中:
function move_input(){
global $my_custom_plugin;
remove_action( 'before_main_content', array( $my_custom_plugin, 'display_input') );
add_action( 'after_main_content', array( $my_custom_plugin, 'display_input' ) );
}
add_action( 'wp_head' , 'move_input' );
現在,在我的更新中,display_input()功能已被移動到另一個類,我想讓人們知道如何訪問它。我嘗試用以下棄用通知替換原始函式 (在主要外掛類中):
public function display_input() {
_deprecated_function( 'display_price', '2.0', 'my_custom_plugin()->display->display_input' );
return $this->display->display_input();
}
但是,add_action 和 remove_action 函式似乎沒有觸發廢止通知。奇怪的是,即使 array( $my_custom_plugin, 'display_input')不存在,完全刪除該功能也不會導致錯誤。
如果有人試圖直接訪問該功能:
$my_custom_plugin->display_input();
然後我看到除錯通知。這是_deprecated_function()的預期結果嗎?還是我錯過了什麼?有人嘗試使用不推薦使用的功能刪除或新增動作時,是否可以顯示除錯通知?
更新
我意識到,我剛剛看到 add_action 的除錯資訊,因為我在頁面上新增相當低。 #facepalm!但是,我仍然沒有看到 remove_action 的任何除錯通知。
最佳解決方案
非現有的回撥
其中一個好處是,如果不存在回撥,do_action()和 apply_filters()都不會觸發錯誤。這意味著它是將外掛資料插入到模板中最安全的方法:如果外掛被關閉,並且 do_action() /apply_filters()在全域性 $wp_filters 陣列中找不到回撥,則不會發生任何事情。
錯誤輸出
現在當您在最初掛接回撥的函式/方法中呼叫 remove_filter()時,回撥將簡單地從全域性陣列中刪除,這意味著回撥將永遠不會被執行,因為它不再註冊了。
解決方案很簡單:透過從回撥本身中刪除回撥,在觸發後刪除回撥。
刪除回撥
我們都知道,WPA 外掛”API” 是一個痛苦的,當涉及到刪除。問題主要是將”unique” 名稱新增到 global $wp_filter; 陣列中的鍵的奇怪構造。一個非常簡單的解決方案是使用__METHOD__並呼叫要在靜態上下文中刪除的過濾器:
class FOoooo
{
public function __construct()
{
add_filter( 'hook', array( __CLASS__, 'bar' ) );
}
public static function bar( $baz )
{
remove_filter( current_filter(), __METHOD__ );
return $baz;
}
}
雖然這不是很好,它是… 一些解決方案的一些用例。但是甚至不要考慮去掉一個關閉。
以上只是刪除回撥,仍然執行它。仍然可以進一步使用 remove_all_actions()(或 remove_all_filters()) 。
// Check if a callback is attached and tell about the deprecated stuff
if ( has_action( 'before_main_content' ) )
_deprecated_function( 'display_price', '2.0', 'my_custom_plugin()->display->display_input' );
// Remove the callback
remove_all_actions( 'before_main_content' );
您甚至可以進一步,將全域性過濾器陣列和 re-attach 中的回撥從新的鉤子/過濾器 (如果它們相容) 提取出來。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。