問題描述
我正在使用一個簡單的搜尋表單小工具,內建自動完成功能 (您可以下載當前版本的 here) 。該外掛正在工作,但我正在使用 OOP 重寫所有的程式碼。我遇到的一個問題是 Wordpress Widget 已經是 WP_Widget 類的一部分。我可以將程式碼分為 2 類。第一個載入所有指令碼和 css,並初始化視窗小工具。這是程式碼的大綱:
class wdSearchForm {
public function __construct() {
// Action hook to load widget
// Register and enqueue styles
// Register and enqueue scripts
}
// register widget in WordPress so that it is available under the widgets section
public function wd_searchform() {
register_widget( 'wd_searchform' );
}
}
這裡是 widget 類的大綱:
class wd_searchform extends WP_Widget {
// Constructor
function wd_searchform() {
}
// The widget itself
function widget( $args, $instance ) {
// The widget code
}
//Update the widget
function update( $new_instance, $old_instance ) {
// Update code
}
function form( $instance ) {
//Set up widget settings.
}
}
我想結合這兩個,以便使用 wp_localize_script 並使用視窗小工具選項載入指令碼。我應該怎麼做歡迎任何建議,即使你會告訴我我完全錯誤的方向…
最佳解決方案
你可以簡單地把你的 init 程式碼放在類的建構函式中。例如:
class myWidget extends WP_Widget{
function myWidget(){
// Init code here
}
function widget( $args, $instance ) {
// The widget code
wp_enqueue_script(...);
wp_enqueue_style(...);
}
// Over methods...
}
register_widget('myWidget');
我的偏好是將排隊呼叫實際上放在短程式碼處理函式中,以避免與在給定頁面上未使用的 JavaScript 和樣式表相關聯的開銷和潛在衝突。
次佳解決方案
你的程式碼是 PHP4 風格。 PHP4 樣式程式碼不應該被使用了。只是把一些函式放在一個類結構中就不是 OOP 。如果要編寫可重用的程式碼,請分開程式碼。
例如:
class Widget_Setup
{
public $widget_class = '';
public $admin_styles = array();
public $admin_scripts = array();
public $front_styles = array();
public $front_scripts = array();
public $script_defaults = array(
'handle' => '',
'src' => '',
'deps' => array(),
'version' => false,
'in_footer' => false
);
public $style_defaults = array(
'handle' => '',
'src' => '',
'deps' => array(),
'version' => false,
'media' => 'all'
);
public function __construct( $widget_class = '', $admin_styles = array(), $admin_scripts = array(), $front_styles = array(), $front_scripts = array() ) {
$this->widget_class = $widget_class;
$this->admin_styles = $admin_styles;
$this->admin_scripts = $admin_scripts;
$this->front_styles = $front_styles;
$this->front_scripts = $front_scripts;
add_action( 'admin_print_styles', array( $this, 'add_styles' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'add_scripts' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'add_styles' ) );
add_action( 'wp_enqueue_scripts', array( $this, 'add_scripts' ) );
if( ! empty( $this->widget_class ) )
add_action( 'widgets_init', array( $this, 'register_widget' ) );
}
public function register_widget() {
register_widget( $this->widget_class );
return true;
}
public function add_styles() {
$styles = ( is_admin() ) ?
$this->admin_styles : $this->front_styles;
if( empty( $styles ) )
return false;
foreach( $styles as $style ) {
$style = wp_parse_args( $style, $this->style_defaults );
wp_enqueue_style(
$style['handle'],
$style['src'],
$style['deps'],
$style['version'],
$style['media']
);
}
return true;
}
public function add_scripts() {
$scripts = ( is_admin() ) ?
$this->admin_scripts : $this->front_scripts;
if( empty( $scripts ) )
return false;
foreach( $scripts as $script ) {
$script = wp_parse_args( $script, $this->script_defaults );
wp_enqueue_script(
$script['handle'],
$script['src'],
$script['deps'],
$script['version'],
$script['media']
);
}
return true;
}
}
這個類可以重用於每個 widget 。 OOP 背後的想法是透過編寫具有附加功能的資料結構來重用您的程式碼。沒有使用類結構,因為有人說它是好的風格。
該類可以這樣使用:
class MyAwesomeWidget extends WP_Widget
{
const TEXTDOMAIN = 'widget_textdomain';
public function __construct() {
parent::__construct(
'widget-name-id',
__( 'Widget Name', self::TEXTDOMAIN ),
array(
'classname' => __CLASS__,
'description' => __( 'Short description.', self::TEXTDOMAIN )
)
);
$admin_style = array(
array( 'handle' => 'somehandle', 'src' => 'path/to/source' ),
array( 'handle' => 'someotherhandle', 'src' => 'path/to/source' ),
);
$admin_scripts = array(
array( 'handle' => 'scrpthandle', 'src' => 'path/to/source', 'deps' => array( 'jquery') ),
);
$front_styles = array(
array( 'handle' => 'frontstyle', 'src' => 'path/to/src' ),
);
new Widget_Setup( __CLASS__, $admin_style, $admin_scripts, $front_styles );
}
public function widget( $instance, $args ) {}
public function update( $new_instance, $old_instance ) {}
public function form( $instance ) {}
}
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。