WPSEO_Admin_Asset

Represents a WPSEO asset.

Defined (1)

The class is defined in the following location(s).

/admin/class-asset.php  
  1. class WPSEO_Admin_Asset { 
  2.  
  3. const TYPE_JS = 'js'; 
  4. const TYPE_CSS = 'css'; 
  5.  
  6. const NAME = 'name'; 
  7. const SRC = 'src'; 
  8. const DEPS = 'deps'; 
  9. const VERSION = 'version'; 
  10.  
  11. // Style specific. 
  12. const MEDIA = 'media'; 
  13. const RTL = 'rtl'; 
  14.  
  15. // Script specific. 
  16. const IN_FOOTER = 'in_footer'; 
  17.  
  18. /** 
  19. * @var string 
  20. */ 
  21. protected $name; 
  22.  
  23. /** 
  24. * @var string 
  25. */ 
  26. protected $src; 
  27.  
  28. /** 
  29. * @var string|array 
  30. */ 
  31. protected $deps; 
  32.  
  33. /** 
  34. * @var string 
  35. */ 
  36. protected $version; 
  37.  
  38. /** 
  39. * @var string 
  40. */ 
  41. protected $media; 
  42.  
  43. /** 
  44. * @var boolean 
  45. */ 
  46. protected $in_footer; 
  47.  
  48. /** 
  49. * @var boolean 
  50. */ 
  51. protected $rtl; 
  52.  
  53. /** 
  54. * @var string 
  55. */ 
  56. protected $suffix; 
  57.  
  58. /** 
  59. * @param array $args The arguments for this asset. 
  60. * @throws InvalidArgumentException Throws when no name or src has been provided. 
  61. */ 
  62. public function __construct( array $args ) { 
  63. if ( ! isset( $args['name'] ) ) { 
  64. throw new InvalidArgumentException( 'name is a required argument' ); 
  65.  
  66. if ( ! isset( $args['src'] ) ) { 
  67. throw new InvalidArgumentException( 'src is a required argument' ); 
  68.  
  69. $args = array_merge( array( 
  70. 'deps' => array(),  
  71. 'version' => WPSEO_VERSION,  
  72. 'in_footer' => true,  
  73. 'rtl' => true,  
  74. 'media' => 'all',  
  75. 'suffix' => WPSEO_CSSJS_SUFFIX,  
  76. ), $args ); 
  77.  
  78. $this->name = $args['name']; 
  79. $this->src = $args['src']; 
  80. $this->deps = $args['deps']; 
  81. $this->version = $args['version']; 
  82. $this->media = $args['media']; 
  83. $this->in_footer = $args['in_footer']; 
  84. $this->rtl = $args['rtl']; 
  85. $this->suffix = $args['suffix']; 
  86.  
  87. /** 
  88. * @return string 
  89. */ 
  90. public function get_name() { 
  91. return $this->name; 
  92.  
  93. /** 
  94. * @return string 
  95. */ 
  96. public function get_src() { 
  97. return $this->src; 
  98.  
  99. /** 
  100. * @return array|string 
  101. */ 
  102. public function get_deps() { 
  103. return $this->deps; 
  104.  
  105. /** 
  106. * @return string 
  107. */ 
  108. public function get_version() { 
  109. return $this->version; 
  110.  
  111. /** 
  112. * @return string 
  113. */ 
  114. public function get_media() { 
  115. return $this->media; 
  116.  
  117. /** 
  118. * @return boolean 
  119. */ 
  120. public function is_in_footer() { 
  121. return $this->in_footer; 
  122.  
  123. /** 
  124. * @return boolean 
  125. */ 
  126. public function has_rtl() { 
  127. return $this->rtl; 
  128.  
  129. /** 
  130. * @return string 
  131. */ 
  132. public function get_suffix() { 
  133. return $this->suffix; 
  134.  
  135. /** 
  136. * Returns the full URL for this asset based on the path to the plugin file. 
  137. * @param string $type Type of asset. 
  138. * @param string $plugin_file Absolute path to the plugin file. 
  139. * @return string The full URL to the asset. 
  140. */ 
  141. public function get_url( $type, $plugin_file ) { 
  142.  
  143. $relative_path = $this->get_relative_path( $type ); 
  144. if ( empty( $relative_path ) ) { 
  145. return ''; 
  146.  
  147. if ( 'development' !== YOAST_ENVIRONMENT && ! $this->get_suffix() ) { 
  148. $plugin_path = plugin_dir_path( $plugin_file ); 
  149. if ( ! file_exists( $plugin_path . $relative_path ) ) { 
  150.  
  151. // Give a notice to the user in the console (only once). 
  152. WPSEO_Utils::javascript_console_notification( 
  153. 'Development Files',  
  154. sprintf( 
  155. /** translators: %1$s resolves to https://github.com/Yoast/wordpress-seo */ 
  156. __( 'You are trying to load non-minified files, these are only available in our development package. Check out %1$s to see all the source files.', 'wordpress-seo' ),  
  157. 'https://github.com/Yoast/wordpress-seo' 
  158. ),  
  159. true 
  160. ); 
  161.  
  162. // Just load the .min file. 
  163. $relative_path = $this->get_relative_path( $type, '.min' ); 
  164.  
  165. return plugins_url( $relative_path, $plugin_file ); 
  166.  
  167. /** 
  168. * Get the relative file for this asset 
  169. * @param string $type Type of this asset. 
  170. * @param null $force_suffix Force use suffix. 
  171. * @return string 
  172. */ 
  173. protected function get_relative_path( $type, $force_suffix = null ) { 
  174. $relative_path = $rtl_path = $rtl_suffix = ''; 
  175.  
  176. $suffix = ( is_null( $force_suffix ) ) ? $this->get_suffix() : $force_suffix; 
  177.  
  178. switch ( $type ) { 
  179. case self::TYPE_JS: 
  180. $relative_path = 'js/dist/' . $this->get_src() . $suffix . '.js'; 
  181. break; 
  182.  
  183. case self::TYPE_CSS: 
  184. // Path and suffix for RTL stylesheets. 
  185. if ( function_exists( 'is_rtl' ) && is_rtl() && $this->has_rtl() ) { 
  186. $rtl_path = 'dist/'; 
  187. $rtl_suffix = '-rtl'; 
  188. $relative_path = 'css/' . $rtl_path . $this->get_src() . $rtl_suffix . $suffix . '.css'; 
  189. break; 
  190.  
  191. return $relative_path;