class-asset.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Admin
  6. */
  7. /**
  8. * Represents a WPSEO asset
  9. */
  10. class WPSEO_Admin_Asset {
  11. const TYPE_JS = 'js';
  12. const TYPE_CSS = 'css';
  13. const NAME = 'name';
  14. const SRC = 'src';
  15. const DEPS = 'deps';
  16. const VERSION = 'version';
  17. // Style specific.
  18. const MEDIA = 'media';
  19. const RTL = 'rtl';
  20. // Script specific.
  21. const IN_FOOTER = 'in_footer';
  22. /**
  23. * @var string
  24. */
  25. protected $name;
  26. /**
  27. * @var string
  28. */
  29. protected $src;
  30. /**
  31. * @var string|array
  32. */
  33. protected $deps;
  34. /**
  35. * @var string
  36. */
  37. protected $version;
  38. /**
  39. * @var string
  40. */
  41. protected $media;
  42. /**
  43. * @var boolean
  44. */
  45. protected $in_footer;
  46. /**
  47. * @var boolean
  48. */
  49. protected $rtl;
  50. /**
  51. * @var string
  52. */
  53. protected $suffix;
  54. /**
  55. * @param array $args The arguments for this asset.
  56. *
  57. * @throws InvalidArgumentException Throws when no name or src has been provided.
  58. */
  59. public function __construct( array $args ) {
  60. if ( ! isset( $args['name'] ) ) {
  61. throw new InvalidArgumentException( 'name is a required argument' );
  62. }
  63. if ( ! isset( $args['src'] ) ) {
  64. throw new InvalidArgumentException( 'src is a required argument' );
  65. }
  66. $args = array_merge( array(
  67. 'deps' => array(),
  68. 'version' => WPSEO_VERSION,
  69. 'in_footer' => true,
  70. 'rtl' => true,
  71. 'media' => 'all',
  72. 'suffix' => WPSEO_CSSJS_SUFFIX,
  73. ), $args );
  74. $this->name = $args['name'];
  75. $this->src = $args['src'];
  76. $this->deps = $args['deps'];
  77. $this->version = $args['version'];
  78. $this->media = $args['media'];
  79. $this->in_footer = $args['in_footer'];
  80. $this->rtl = $args['rtl'];
  81. $this->suffix = $args['suffix'];
  82. }
  83. /**
  84. * @return string
  85. */
  86. public function get_name() {
  87. return $this->name;
  88. }
  89. /**
  90. * @return string
  91. */
  92. public function get_src() {
  93. return $this->src;
  94. }
  95. /**
  96. * @return array|string
  97. */
  98. public function get_deps() {
  99. return $this->deps;
  100. }
  101. /**
  102. * @return string
  103. */
  104. public function get_version() {
  105. return $this->version;
  106. }
  107. /**
  108. * @return string
  109. */
  110. public function get_media() {
  111. return $this->media;
  112. }
  113. /**
  114. * @return boolean
  115. */
  116. public function is_in_footer() {
  117. return $this->in_footer;
  118. }
  119. /**
  120. * @return boolean
  121. */
  122. public function has_rtl() {
  123. return $this->rtl;
  124. }
  125. /**
  126. * @return string
  127. */
  128. public function get_suffix() {
  129. return $this->suffix;
  130. }
  131. /**
  132. * Returns the full URL for this asset based on the path to the plugin file.
  133. *
  134. * @param string $type Type of asset.
  135. * @param string $plugin_file Absolute path to the plugin file.
  136. *
  137. * @return string The full URL to the asset.
  138. */
  139. public function get_url( $type, $plugin_file ) {
  140. _deprecated_function( __CLASS__ . '::get_url', '6.2', 'WPSEO_Admin_Asset_SEO_Location::get_url' );
  141. $asset_location = new WPSEO_Admin_Asset_SEO_Location( $plugin_file );
  142. return $asset_location->get_url( $this, $type );
  143. }
  144. }