class-asset.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /**
  12. * Constant used to identify file type as a JS file.
  13. *
  14. * @var string
  15. */
  16. const TYPE_JS = 'js';
  17. /**
  18. * Constant used to identify file type as a CSS file.
  19. *
  20. * @var string
  21. */
  22. const TYPE_CSS = 'css';
  23. /**
  24. * The name option identifier.
  25. *
  26. * @var string
  27. */
  28. const NAME = 'name';
  29. /**
  30. * The source option identifier.
  31. *
  32. * @var string
  33. */
  34. const SRC = 'src';
  35. /**
  36. * The dependencies option identifier.
  37. *
  38. * @var string
  39. */
  40. const DEPS = 'deps';
  41. /**
  42. * The version option identifier.
  43. *
  44. * @var string
  45. */
  46. const VERSION = 'version';
  47. /* Style specific. */
  48. /**
  49. * The media option identifier.
  50. *
  51. * @var string
  52. */
  53. const MEDIA = 'media';
  54. /**
  55. * The rtl option identifier.
  56. *
  57. * @var string
  58. */
  59. const RTL = 'rtl';
  60. /* Script specific. */
  61. /**
  62. * The "in footer" option identifier.
  63. *
  64. * @var string
  65. */
  66. const IN_FOOTER = 'in_footer';
  67. /**
  68. * Asset identifier.
  69. *
  70. * @var string
  71. */
  72. protected $name;
  73. /**
  74. * Path to the asset.
  75. *
  76. * @var string
  77. */
  78. protected $src;
  79. /**
  80. * Asset dependencies.
  81. *
  82. * @var string|array
  83. */
  84. protected $deps;
  85. /**
  86. * Asset version.
  87. *
  88. * @var string
  89. */
  90. protected $version;
  91. /**
  92. * For CSS Assets. The type of media for which this stylesheet has been defined.
  93. *
  94. * See https://www.w3.org/TR/CSS2/media.html#media-types.
  95. *
  96. * @var string
  97. */
  98. protected $media;
  99. /**
  100. * For JS Assets. Whether or not the script should be loaded in the footer.
  101. *
  102. * @var boolean
  103. */
  104. protected $in_footer;
  105. /**
  106. * For CSS Assets. Whether this stylesheet is a right-to-left stylesheet.
  107. *
  108. * @var boolean
  109. */
  110. protected $rtl;
  111. /**
  112. * File suffix.
  113. *
  114. * @var string
  115. */
  116. protected $suffix;
  117. /**
  118. * Default asset arguments.
  119. *
  120. * @var array
  121. */
  122. private $defaults = [
  123. 'deps' => [],
  124. 'version' => WPSEO_VERSION,
  125. 'in_footer' => true,
  126. 'rtl' => true,
  127. 'media' => 'all',
  128. 'suffix' => WPSEO_CSSJS_SUFFIX,
  129. ];
  130. /**
  131. * Constructs an instance of the WPSEO_Admin_Asset class.
  132. *
  133. * @param array $args The arguments for this asset.
  134. *
  135. * @throws InvalidArgumentException Throws when no name or src has been provided.
  136. */
  137. public function __construct( array $args ) {
  138. if ( ! isset( $args['name'] ) ) {
  139. throw new InvalidArgumentException( 'name is a required argument' );
  140. }
  141. if ( ! isset( $args['src'] ) ) {
  142. throw new InvalidArgumentException( 'src is a required argument' );
  143. }
  144. $args = array_merge( $this->defaults, $args );
  145. $this->name = $args['name'];
  146. $this->src = $args['src'];
  147. $this->deps = $args['deps'];
  148. $this->version = $args['version'];
  149. $this->media = $args['media'];
  150. $this->in_footer = $args['in_footer'];
  151. $this->rtl = $args['rtl'];
  152. $this->suffix = $args['suffix'];
  153. }
  154. /**
  155. * Returns the asset identifier.
  156. *
  157. * @return string
  158. */
  159. public function get_name() {
  160. return $this->name;
  161. }
  162. /**
  163. * Returns the path to the asset.
  164. *
  165. * @return string
  166. */
  167. public function get_src() {
  168. return $this->src;
  169. }
  170. /**
  171. * Returns the asset dependencies.
  172. *
  173. * @return array|string
  174. */
  175. public function get_deps() {
  176. return $this->deps;
  177. }
  178. /**
  179. * Returns the asset version.
  180. *
  181. * @return string
  182. */
  183. public function get_version() {
  184. return $this->version;
  185. }
  186. /**
  187. * Returns the media type for CSS assets.
  188. *
  189. * @return string
  190. */
  191. public function get_media() {
  192. return $this->media;
  193. }
  194. /**
  195. * Returns whether a script asset should be loaded in the footer of the page.
  196. *
  197. * @return boolean
  198. */
  199. public function is_in_footer() {
  200. return $this->in_footer;
  201. }
  202. /**
  203. * Returns whether this CSS has a RTL counterpart.
  204. *
  205. * @return boolean
  206. */
  207. public function has_rtl() {
  208. return $this->rtl;
  209. }
  210. /**
  211. * Returns the file suffix.
  212. *
  213. * @return string
  214. */
  215. public function get_suffix() {
  216. return $this->suffix;
  217. }
  218. /**
  219. * Returns the full URL for this asset based on the path to the plugin file.
  220. *
  221. * @deprecated 6.2
  222. * @codeCoverageIgnore
  223. *
  224. * @param string $type Type of asset.
  225. * @param string $plugin_file Absolute path to the plugin file.
  226. *
  227. * @return string The full URL to the asset.
  228. */
  229. public function get_url( $type, $plugin_file ) {
  230. _deprecated_function( __CLASS__ . '::get_url', '6.2', 'WPSEO_Admin_Asset_SEO_Location::get_url' );
  231. $asset_location = new WPSEO_Admin_Asset_SEO_Location( $plugin_file );
  232. return $asset_location->get_url( $this, $type );
  233. }
  234. }