-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPipitTemplateFilter_cloudinary.class.php
129 lines (90 loc) · 3.27 KB
/
PipitTemplateFilter_cloudinary.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* v1.2
* https://github.com/Pipits/pipit-cloudinary
*/
class PipitTemplateFilter_cloudinary extends PerchTemplateFilter {
public function filterBeforeProcessing($value, $valueIsMarkup = false) {
if (!defined('CLOUDINARY_CLOUDNAME')) {
PerchUtil::debug('Cloudinary Name is not set in config', 'notice');
return $value;
}
$allow_dev = false;
if(defined('PIPIT_CLOUDINARY_DEV')) $allow_dev = PIPIT_CLOUDINARY_DEV;
if(!$allow_dev) {
if (PERCH_PRODUCTION_MODE == PERCH_DEVELOPMENT || PERCH_PRODUCTION_MODE == PERCH_STAGING) {
return $value;
}
}
$pre = 'https://res.cloudinary.com/'. CLOUDINARY_CLOUDNAME .'/image/fetch/';
$pre_upload = 'https://res.cloudinary.com/'. CLOUDINARY_CLOUDNAME .'/image/upload/';
$opts = $domain = '';
// Cloudinary image manipulation options
// fetch URLs e.g. /image/fetch/{opts}/{file_url}
// upload URLs e.g. /image/upload/{opts}/...
if ($this->Tag->opts) {
$opts = $this->_replace_vars($this->Tag->opts, $this->content);
$opts .= '/';
}
// is it an upload URL?
// check if $value starts with $pre_upload
if(substr($value, 0, strlen($pre_upload)) == $pre_upload) {
return str_replace($pre_upload, $pre_upload.$opts, $value);
}
// if URL has no domain, include site's domain
if (!$this->_is_full_link($value)) {
$domain = $this->_get_domain();
}
$value = $pre . $opts . urlencode($domain . $value);
return $value;
}
/**
* Get domain from config or settings
* @return string
*/
private function _get_domain() {
if(defined('SITE_URL')) {
$domain = SITE_URL;
} else {
$API = new PerchAPI(1.0, 'pipit');
$Settings = $API->get('Settings');
$domain = $Settings->get('siteURL')->val();
}
// remove trailing slash
if(substr($domain, -1) == '/') $domain = rtrim($domain, '/');
return $domain;
}
/**
*
* @return boolean
*/
private function _is_full_link($link) {
$file_url_parts = parse_url($link);
// value includes a domain host
if(isset($file_url_parts['host'])) {
return true;
}
return false;
}
/**
* @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);
// remove rouge commas
$out = trim($out, ',');
$out = preg_replace_callback('/[,]{2,}/', function($matches) { return ',';}, $out);
return $out;
}
}
PerchSystem::register_template_filter('cloudinary', 'PipitTemplateFilter_cloudinary');