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