-
Notifications
You must be signed in to change notification settings - Fork 3
/
Asset.php
executable file
·67 lines (45 loc) · 1.35 KB
/
Asset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Creates an asset to attach to a style object. Checks
* if that file differs from when it was originally saved
* in the database.
*/
class Asset extends PluginObject {
public $url;
public $input_path;
public $path;
public $hash;
public static $hash_type = "md5";
function __construct( $url, $path, $hash = null ) {
self::plugin_info();
$this->url = $url;
$this->input_path = $path;
$this->_set_path();
$this->hash = ( empty($hash) ) ? $this->generate_hash() : $hash;
}
public function file_has_changed() {
return ( $this->hash !== $this->generate_hash() && ! is_null($this->hash) ) ? true : false;
}
public function generate_hash() {
if ( !empty($this->path) ) {
return hash_file( self::$hash_type, $this->path );
}
}
public function relative_path() {
$sanitized = str_replace("\\\\", "\\", $this->input_path);
$sanitized = $this->_fix_slashes( $sanitized );
$template = $this->_get_template_dir();
return str_replace( $template, "", $sanitized );
}
private function _set_path() {
$relative = $this->relative_path();
$template = $this->_get_template_dir();
$this->path = $template.$relative;
}
private function _get_template_dir() {
return $this->_fix_slashes( self::$template_path );
}
private function _fix_slashes( $string ) {
return preg_replace('/[\\\\\/]/', DS, $string);
}
}