class-wp-dependency.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Dependencies API: _WP_Dependency class
  4. *
  5. * @since 4.7.0
  6. *
  7. * @package WordPress
  8. * @subpackage Dependencies
  9. */
  10. /**
  11. * Class _WP_Dependency
  12. *
  13. * Helper class to register a handle and associated data.
  14. *
  15. * @access private
  16. * @since 2.6.0
  17. */
  18. #[AllowDynamicProperties]
  19. class _WP_Dependency {
  20. /**
  21. * The handle name.
  22. *
  23. * @since 2.6.0
  24. * @var string
  25. */
  26. public $handle;
  27. /**
  28. * The handle source.
  29. *
  30. * @since 2.6.0
  31. * @var string
  32. */
  33. public $src;
  34. /**
  35. * An array of handle dependencies.
  36. *
  37. * @since 2.6.0
  38. * @var string[]
  39. */
  40. public $deps = array();
  41. /**
  42. * The handle version.
  43. *
  44. * Used for cache-busting.
  45. *
  46. * @since 2.6.0
  47. * @var bool|string
  48. */
  49. public $ver = false;
  50. /**
  51. * Additional arguments for the handle.
  52. *
  53. * @since 2.6.0
  54. * @var array
  55. */
  56. public $args = null; // Custom property, such as $in_footer or $media.
  57. /**
  58. * Extra data to supply to the handle.
  59. *
  60. * @since 2.6.0
  61. * @var array
  62. */
  63. public $extra = array();
  64. /**
  65. * Translation textdomain set for this dependency.
  66. *
  67. * @since 5.0.0
  68. * @var string
  69. */
  70. public $textdomain;
  71. /**
  72. * Translation path set for this dependency.
  73. *
  74. * @since 5.0.0
  75. * @var string
  76. */
  77. public $translations_path;
  78. /**
  79. * Setup dependencies.
  80. *
  81. * @since 2.6.0
  82. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  83. * to the function signature.
  84. *
  85. * @param mixed ...$args Dependency information.
  86. */
  87. public function __construct( ...$args ) {
  88. list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = $args;
  89. if ( ! is_array( $this->deps ) ) {
  90. $this->deps = array();
  91. }
  92. }
  93. /**
  94. * Add handle data.
  95. *
  96. * @since 2.6.0
  97. *
  98. * @param string $name The data key to add.
  99. * @param mixed $data The data value to add.
  100. * @return bool False if not scalar, true otherwise.
  101. */
  102. public function add_data( $name, $data ) {
  103. if ( ! is_scalar( $name ) ) {
  104. return false;
  105. }
  106. $this->extra[ $name ] = $data;
  107. return true;
  108. }
  109. /**
  110. * Sets the translation domain for this dependency.
  111. *
  112. * @since 5.0.0
  113. *
  114. * @param string $domain The translation textdomain.
  115. * @param string $path Optional. The full file path to the directory containing translation files.
  116. * @return bool False if $domain is not a string, true otherwise.
  117. */
  118. public function set_translations( $domain, $path = '' ) {
  119. if ( ! is_string( $domain ) ) {
  120. return false;
  121. }
  122. $this->textdomain = $domain;
  123. $this->translations_path = $path;
  124. return true;
  125. }
  126. }