-
Notifications
You must be signed in to change notification settings - Fork 0
/
PipitTemplateFilter_imgix.class.php
88 lines (64 loc) · 2.56 KB
/
PipitTemplateFilter_imgix.class.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
/**
* v1.1
* https://github.com/Pipits/pipit-imgix
*/
class PipitTemplateFilter_imgix extends PerchTemplateFilter {
public function filterBeforeProcessing($value, $valueIsMarkup = false) {
$subdomain = $query = '';
$allow_dev = false;
if(defined('PIPIT_IMGIX_DEV')) $allow_dev = PIPIT_IMGIX_DEV;
if(!$allow_dev) {
if (PERCH_PRODUCTION_MODE == PERCH_DEVELOPMENT || PERCH_PRODUCTION_MODE == PERCH_STAGING) {
return $value;
}
}
if(!defined('PIPIT_IMGIX_SUBDOMAIN') && !$this->Tag->subdomain) {
PerchUtil::debug('Imgix subdomain is not set.', 'notice');
return $value;
}
// Each Imgix source has a unique subdomain
// Default subdomain is PIPIT_IMGIX_SUBDOMAIN. Can specify a different subdomain with a tag attribute
// e.g. images hosted on server has a subdomain & images hosted in S3 has a different subdomain
if ($this->Tag->subdomain) {
$subdomain = $this->Tag->subdomain;
} else {
$subdomain = PIPIT_IMGIX_SUBDOMAIN;
}
$pre = "https://$subdomain.imgix.net";
$file = $value;
if($this->Tag->imgix_opts) {
$query = '?' . $this->_replace_vars($this->Tag->imgix_opts, $this->content);
} elseif($this->Tag->opts) {
$query = '?' . $this->_replace_vars($this->Tag->opts, $this->content);
} else {
$query = array();
$attributes = $this->Tag->search_attributes_for('imgix-');
foreach($attributes as $key => $val) {
$new_key = str_replace('imgix-', '', $key);
$query[$new_key] = $this->_replace_vars($val, $this->content);;
}
$query = '?' .http_build_query($query);
}
$result = $pre . $file . $query;
return $result;
}
/**
* @param string $opts
* @param array $vars
* @return string
*/
private function _replace_vars($opts, $vars) {
$out = preg_replace_callback('/{([A-Za-z0-9_\-]+)}/', function($matches) use($vars) {
if (isset($vars[$matches[1]])) {
if(!is_array( $vars[$matches[1]] )) {
return $vars[$matches[1]];
} elseif(isset( $vars[$matches[1]]['_default'] )) {
return $vars[$matches[1]]['_default'];
}
}
}, $opts);
return $out;
}
}
PerchSystem::register_template_filter('imgix', 'PipitTemplateFilter_imgix');