类文档

_WP_Dependency

💡 云策文档标注

概述

_WP_Dependency 是 WordPress 中用于注册脚本和样式句柄及其相关数据的辅助类。它管理依赖项、版本控制、额外参数和翻译设置,是 wp_register_script 和 wp_register_style 等函数的核心底层组件。

关键要点

  • _WP_Dependency 类用于表示一个资源句柄(如脚本或样式),包含句柄名称、源文件、依赖项、版本、参数等属性。
  • 主要方法包括 __construct 用于初始化依赖项,add_data 用于添加额外数据,set_translations 用于设置翻译域和路径。
  • 属性如 $handle、$src、$deps、$ver、$args、$extra、$textdomain 和 $translations_path 定义了资源的具体配置和行为。
  • 该类自 WordPress 2.6.0 引入,后续版本如 5.0.0 和 5.3.0 增加了翻译支持和参数规范化。

代码示例

// 示例:创建一个 _WP_Dependency 实例
$dependency = new _WP_Dependency( 'my-script', 'path/to/script.js', array( 'jquery' ), '1.0.0', null );
$dependency->add_data( 'in_footer', true );
$dependency->set_translations( 'my-textdomain', 'path/to/languages' );

注意事项

  • _WP_Dependency 通常由 WordPress 核心内部使用,开发者应优先使用高级函数如 wp_register_script 或 wp_register_style 来注册资源。
  • 在直接操作此类时,需确保属性类型正确,例如 $deps 应为数组,$domain 应为字符串,以避免错误。

📄 原文内容

Class _WP_Dependency

Description

Helper class to register a handle and associated data.

Methods

Name Description
_WP_Dependency::__construct Setup dependencies.
_WP_Dependency::add_data Add handle data.
_WP_Dependency::set_translations Sets the translation domain for this dependency.

Source

class _WP_Dependency {
	/**
	 * The handle name.
	 *
	 * @since 2.6.0
	 * @var string
	 */
	public $handle;

	/**
	 * The handle source.
	 *
	 * If source is set to false, the item is an alias of other items it depends on.
	 *
	 * @since 2.6.0
	 * @var string|false
	 */
	public $src;

	/**
	 * An array of handle dependencies.
	 *
	 * @since 2.6.0
	 * @var string[]
	 */
	public $deps = array();

	/**
	 * The handle version.
	 *
	 * Used for cache-busting.
	 *
	 * @since 2.6.0
	 * @var string|false|null
	 */
	public $ver = false;

	/**
	 * Additional arguments for the handle.
	 *
	 * @since 2.6.0
	 * @var mixed
	 */
	public $args = null;  // Custom property, such as $in_footer or $media.

	/**
	 * Extra data to supply to the handle.
	 *
	 * @since 2.6.0
	 * @var array
	 */
	public $extra = array();

	/**
	 * Translation textdomain set for this dependency.
	 *
	 * @since 5.0.0
	 * @var string
	 */
	public $textdomain;

	/**
	 * Translation path set for this dependency.
	 *
	 * @since 5.0.0
	 * @var string
	 */
	public $translations_path;

	/**
	 * Setup dependencies.
	 *
	 * @since 2.6.0
	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
	 *              to the function signature.
	 *
	 * @param mixed ...$args Dependency information.
	 */
	public function __construct( ...$args ) {
		list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = $args;
		if ( ! is_array( $this->deps ) ) {
			$this->deps = array();
		}
	}

	/**
	 * Add handle data.
	 *
	 * @since 2.6.0
	 *
	 * @param string $name The data key to add.
	 * @param mixed  $data The data value to add.
	 * @return bool False if not scalar, true otherwise.
	 */
	public function add_data( $name, $data ) {
		if ( ! is_scalar( $name ) ) {
			return false;
		}
		$this->extra[ $name ] = $data;
		return true;
	}

	/**
	 * Sets the translation domain for this dependency.
	 *
	 * @since 5.0.0
	 *
	 * @param string $domain The translation textdomain.
	 * @param string $path   Optional. The full file path to the directory containing translation files.
	 * @return bool False if $domain is not a string, true otherwise.
	 */
	public function set_translations( $domain, $path = '' ) {
		if ( ! is_string( $domain ) ) {
			return false;
		}
		$this->textdomain        = $domain;
		$this->translations_path = $path;
		return true;
	}
}

Changelog

Version Description
2.6.0 Introduced.