Elementor Core Cơ bản Chúng ta có thể chuyển các thành phần khác nhau vào một lớp duy nhất – lớp chính của chúng ta. Lớp này sẽ kiểm tra các yêu cầu cơ bản và tải các tệp cần thiết để kích hoạt chức năng của tiện ích bổ sung, sau đó tải các thành phần khác nhau.
Cấu trúc Lớp Một ví dụ điển hình về lớp tiện ích bổ sung là một lớp chứa các chức năng sau:
final class Plugin {
private static $_instance = null;
public static function instance() {}
public function __construct() {}
public function is_compatible(): bool {}
public function init(): void {}
}
Một Phiên bản Duy nhất Lớp chính nên sử dụng mẫu thiết kế singleton để đảm bảo nó chỉ được tải một lần:
final class Plugin {
/**
* Instance
*
* @since 1.0.0
* @access private
* @static
* @var \Elementor_Test_Addon\Plugin The single instance of the class.
*/
private static $_instance = null;
/**
* Instance
*
* Ensures only one instance of the class is loaded or can be loaded.
*
* @since 1.0.0
* @access public
* @static
* @return \Elementor_Test_Addon\Plugin An instance of the class.
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
}
\Plugin::instance();
Hàm Khởi tạo Hàm khởi tạo nên thực hiện các kiểm tra tương thích để đảm bảo các yêu cầu cơ bản được đáp ứng. Nếu tất cả các kiểm tra đều đạt, chức năng chính sẽ được khởi tạo.
final class Plugin {
/**
* Constructor
*
* Perform some compatibility checks to make sure basic requirements are meet.
* If all compatibility checks pass, initialize the functionality.
*
* @since 1.0.0
* @access public
*/
public function __construct() {
if ( $this->is_compatible() ) {
add_action( ‘elementor/init’, [ $this, ‘init’ ] );
}
}
/**
* Compatibility Checks
*
* Checks whether the site meets the addon requirement.
*
* @since 1.0.0
* @access public
*/
public function is_compatible(): bool {
// Compatibility checks here…
}
/**
* Initialize
*
* Load the addons functionality only after Elementor is initialized.
*
* Fired by `elementor/init` action hook.
*
* @since 1.0.0
* @access public
*/
public function init(): void {
// Addon functionality here…
}
}
Bây giờ, hãy xem xét một vài ví dụ về [kiểm tra khả năng tương thích của tiện ích bổ sung](https://developers.elementor.com/docs/addons/compatibility/)
Nguồn:https://developers.elementor.com