@@ -51,10 +51,10 @@ |
||
51 | 51 | return $this->getArrayCopy(); |
52 | 52 | } |
53 | 53 | |
54 | - /** |
|
55 | - * @param array $posts |
|
56 | - * @return array |
|
57 | - */ |
|
54 | + /** |
|
55 | + * @param array $posts |
|
56 | + * @return array |
|
57 | + */ |
|
58 | 58 | static function maybe_set_preview( $posts ) { |
59 | 59 | if ( is_array( $posts ) && isset( $_GET['preview'] ) && $_GET['preview'] |
60 | 60 | && isset( $_GET['preview_id'] ) && $_GET['preview_id'] |
@@ -2,333 +2,333 @@ |
||
2 | 2 | |
3 | 3 | class TimberURLHelper { |
4 | 4 | |
5 | - /** |
|
6 | - * |
|
7 | - * |
|
8 | - * @return string |
|
9 | - */ |
|
10 | - public static function get_current_url() { |
|
11 | - $pageURL = "http://"; |
|
12 | - if ( isset( $_SERVER['HTTPS'] ) && $_SERVER["HTTPS"] == "on" ) { |
|
13 | - $pageURL = "https://";; |
|
14 | - } |
|
15 | - if ( isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] && $_SERVER["SERVER_PORT"] != "80" ) { |
|
16 | - $pageURL .= self::get_host() . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; |
|
17 | - } else { |
|
18 | - $pageURL .= self::get_host() . $_SERVER["REQUEST_URI"]; |
|
19 | - } |
|
20 | - return $pageURL; |
|
21 | - } |
|
5 | + /** |
|
6 | + * |
|
7 | + * |
|
8 | + * @return string |
|
9 | + */ |
|
10 | + public static function get_current_url() { |
|
11 | + $pageURL = "http://"; |
|
12 | + if ( isset( $_SERVER['HTTPS'] ) && $_SERVER["HTTPS"] == "on" ) { |
|
13 | + $pageURL = "https://";; |
|
14 | + } |
|
15 | + if ( isset($_SERVER["SERVER_PORT"]) && $_SERVER["SERVER_PORT"] && $_SERVER["SERVER_PORT"] != "80" ) { |
|
16 | + $pageURL .= self::get_host() . ":" . $_SERVER["SERVER_PORT"] . $_SERVER["REQUEST_URI"]; |
|
17 | + } else { |
|
18 | + $pageURL .= self::get_host() . $_SERVER["REQUEST_URI"]; |
|
19 | + } |
|
20 | + return $pageURL; |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * |
|
25 | - * |
|
26 | - * @param string $url |
|
27 | - * @return bool |
|
28 | - */ |
|
29 | - public static function is_url( $url ) { |
|
30 | - if ( !is_string( $url ) ) { |
|
31 | - return false; |
|
32 | - } |
|
33 | - $url = strtolower( $url ); |
|
34 | - if ( strstr( $url, '://' ) ) { |
|
35 | - return true; |
|
36 | - } |
|
37 | - return false; |
|
38 | - } |
|
23 | + /** |
|
24 | + * |
|
25 | + * |
|
26 | + * @param string $url |
|
27 | + * @return bool |
|
28 | + */ |
|
29 | + public static function is_url( $url ) { |
|
30 | + if ( !is_string( $url ) ) { |
|
31 | + return false; |
|
32 | + } |
|
33 | + $url = strtolower( $url ); |
|
34 | + if ( strstr( $url, '://' ) ) { |
|
35 | + return true; |
|
36 | + } |
|
37 | + return false; |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public static function get_path_base() { |
|
46 | - $struc = get_option( 'permalink_structure' ); |
|
47 | - $struc = explode( '/', $struc ); |
|
48 | - $p = '/'; |
|
49 | - foreach ( $struc as $s ) { |
|
50 | - if ( !strstr( $s, '%' ) && strlen( $s ) ) { |
|
51 | - $p .= $s . '/'; |
|
52 | - } |
|
53 | - } |
|
54 | - return $p; |
|
55 | - } |
|
40 | + /** |
|
41 | + * |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public static function get_path_base() { |
|
46 | + $struc = get_option( 'permalink_structure' ); |
|
47 | + $struc = explode( '/', $struc ); |
|
48 | + $p = '/'; |
|
49 | + foreach ( $struc as $s ) { |
|
50 | + if ( !strstr( $s, '%' ) && strlen( $s ) ) { |
|
51 | + $p .= $s . '/'; |
|
52 | + } |
|
53 | + } |
|
54 | + return $p; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * |
|
59 | - * |
|
60 | - * @param string $url |
|
61 | - * @param bool $force |
|
62 | - * @return string |
|
63 | - */ |
|
64 | - public static function get_rel_url( $url, $force = false ) { |
|
65 | - $url_info = parse_url( $url ); |
|
66 | - if ( isset( $url_info['host'] ) && $url_info['host'] != self::get_host() && !$force ) { |
|
67 | - return $url; |
|
68 | - } |
|
69 | - $link = ''; |
|
70 | - if ( isset( $url_info['path'] ) ) { |
|
71 | - $link = $url_info['path']; |
|
72 | - } |
|
73 | - if ( isset( $url_info['query'] ) && strlen( $url_info['query'] ) ) { |
|
74 | - $link .= '?' . $url_info['query']; |
|
75 | - } |
|
76 | - if ( isset( $url_info['fragment'] ) && strlen( $url_info['fragment'] ) ) { |
|
77 | - $link .= '#' . $url_info['fragment']; |
|
78 | - } |
|
79 | - $link = TimberURLHelper::remove_double_slashes( $link ); |
|
80 | - return $link; |
|
81 | - } |
|
57 | + /** |
|
58 | + * |
|
59 | + * |
|
60 | + * @param string $url |
|
61 | + * @param bool $force |
|
62 | + * @return string |
|
63 | + */ |
|
64 | + public static function get_rel_url( $url, $force = false ) { |
|
65 | + $url_info = parse_url( $url ); |
|
66 | + if ( isset( $url_info['host'] ) && $url_info['host'] != self::get_host() && !$force ) { |
|
67 | + return $url; |
|
68 | + } |
|
69 | + $link = ''; |
|
70 | + if ( isset( $url_info['path'] ) ) { |
|
71 | + $link = $url_info['path']; |
|
72 | + } |
|
73 | + if ( isset( $url_info['query'] ) && strlen( $url_info['query'] ) ) { |
|
74 | + $link .= '?' . $url_info['query']; |
|
75 | + } |
|
76 | + if ( isset( $url_info['fragment'] ) && strlen( $url_info['fragment'] ) ) { |
|
77 | + $link .= '#' . $url_info['fragment']; |
|
78 | + } |
|
79 | + $link = TimberURLHelper::remove_double_slashes( $link ); |
|
80 | + return $link; |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated |
|
85 | - * @link http://stackoverflow.com/questions/2297403/http-host-vs-server-name |
|
86 | - * @return string the HTTP_HOST or SERVER_NAME |
|
87 | - */ |
|
88 | - public static function get_host() { |
|
89 | - if (isset($_SERVER['HTTP_HOST'])) { |
|
90 | - return $_SERVER['HTTP_HOST']; |
|
91 | - } |
|
92 | - if (isset($_SERVER['SERVER_NAME'])) { |
|
93 | - return $_SERVER['SERVER_NAME']; |
|
94 | - } |
|
95 | - return ''; |
|
96 | - } |
|
83 | + /** |
|
84 | + * Some setups like HTTP_HOST, some like SERVER_NAME, it's complicated |
|
85 | + * @link http://stackoverflow.com/questions/2297403/http-host-vs-server-name |
|
86 | + * @return string the HTTP_HOST or SERVER_NAME |
|
87 | + */ |
|
88 | + public static function get_host() { |
|
89 | + if (isset($_SERVER['HTTP_HOST'])) { |
|
90 | + return $_SERVER['HTTP_HOST']; |
|
91 | + } |
|
92 | + if (isset($_SERVER['SERVER_NAME'])) { |
|
93 | + return $_SERVER['SERVER_NAME']; |
|
94 | + } |
|
95 | + return ''; |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * |
|
100 | - * |
|
101 | - * @param string $url |
|
102 | - * @return bool |
|
103 | - */ |
|
104 | - public static function is_local( $url ) { |
|
105 | - if ( strstr( $url, self::get_host() ) ) { |
|
106 | - return true; |
|
107 | - } |
|
108 | - return false; |
|
109 | - } |
|
98 | + /** |
|
99 | + * |
|
100 | + * |
|
101 | + * @param string $url |
|
102 | + * @return bool |
|
103 | + */ |
|
104 | + public static function is_local( $url ) { |
|
105 | + if ( strstr( $url, self::get_host() ) ) { |
|
106 | + return true; |
|
107 | + } |
|
108 | + return false; |
|
109 | + } |
|
110 | 110 | |
111 | - /** |
|
112 | - * |
|
113 | - * |
|
114 | - * @param string $src |
|
115 | - * @return string |
|
116 | - */ |
|
117 | - public static function get_full_path( $src ) { |
|
118 | - $root = ABSPATH; |
|
119 | - $old_root_path = $root . $src; |
|
120 | - $old_root_path = str_replace( '//', '/', $old_root_path ); |
|
121 | - return $old_root_path; |
|
122 | - } |
|
111 | + /** |
|
112 | + * |
|
113 | + * |
|
114 | + * @param string $src |
|
115 | + * @return string |
|
116 | + */ |
|
117 | + public static function get_full_path( $src ) { |
|
118 | + $root = ABSPATH; |
|
119 | + $old_root_path = $root . $src; |
|
120 | + $old_root_path = str_replace( '//', '/', $old_root_path ); |
|
121 | + return $old_root_path; |
|
122 | + } |
|
123 | 123 | |
124 | - /** |
|
125 | - * Takes a url and figures out its place based in the file system based on path |
|
126 | - * NOTE: Not fool-proof, makes a lot of assumptions about the file path |
|
127 | - * matching the URL path |
|
128 | - * |
|
129 | - * @param string $url |
|
130 | - * @return string |
|
131 | - */ |
|
132 | - public static function url_to_file_system( $url ) { |
|
133 | - $url_parts = parse_url( $url ); |
|
134 | - $path = ABSPATH . $url_parts['path']; |
|
135 | - $path = str_replace( '//', '/', $path ); |
|
136 | - return $path; |
|
137 | - } |
|
124 | + /** |
|
125 | + * Takes a url and figures out its place based in the file system based on path |
|
126 | + * NOTE: Not fool-proof, makes a lot of assumptions about the file path |
|
127 | + * matching the URL path |
|
128 | + * |
|
129 | + * @param string $url |
|
130 | + * @return string |
|
131 | + */ |
|
132 | + public static function url_to_file_system( $url ) { |
|
133 | + $url_parts = parse_url( $url ); |
|
134 | + $path = ABSPATH . $url_parts['path']; |
|
135 | + $path = str_replace( '//', '/', $path ); |
|
136 | + return $path; |
|
137 | + } |
|
138 | 138 | |
139 | - public static function file_system_to_url( $fs ) { |
|
140 | - $relative_path = self::get_rel_path( $fs ); |
|
141 | - $home = home_url( '/'.$relative_path ); |
|
142 | - return $home; |
|
143 | - } |
|
139 | + public static function file_system_to_url( $fs ) { |
|
140 | + $relative_path = self::get_rel_path( $fs ); |
|
141 | + $home = home_url( '/'.$relative_path ); |
|
142 | + return $home; |
|
143 | + } |
|
144 | 144 | |
145 | - /** |
|
146 | - * |
|
147 | - * |
|
148 | - * @param string $src |
|
149 | - * @return string |
|
150 | - */ |
|
151 | - public static function get_rel_path( $src ) { |
|
152 | - if ( strstr( $src, ABSPATH ) ) { |
|
153 | - return str_replace( ABSPATH, '', $src ); |
|
154 | - } |
|
155 | - //its outside the wordpress directory, alternate setups: |
|
156 | - $src = str_replace( WP_CONTENT_DIR, '', $src ); |
|
157 | - return WP_CONTENT_SUBDIR . $src; |
|
158 | - } |
|
145 | + /** |
|
146 | + * |
|
147 | + * |
|
148 | + * @param string $src |
|
149 | + * @return string |
|
150 | + */ |
|
151 | + public static function get_rel_path( $src ) { |
|
152 | + if ( strstr( $src, ABSPATH ) ) { |
|
153 | + return str_replace( ABSPATH, '', $src ); |
|
154 | + } |
|
155 | + //its outside the wordpress directory, alternate setups: |
|
156 | + $src = str_replace( WP_CONTENT_DIR, '', $src ); |
|
157 | + return WP_CONTENT_SUBDIR . $src; |
|
158 | + } |
|
159 | 159 | |
160 | - /** |
|
161 | - * |
|
162 | - * |
|
163 | - * @param string $url |
|
164 | - * @return string |
|
165 | - */ |
|
166 | - public static function remove_double_slashes( $url ) { |
|
167 | - $url = str_replace( '//', '/', $url ); |
|
168 | - if ( strstr( $url, 'http:' ) && !strstr( $url, 'http://' ) ) { |
|
169 | - $url = str_replace( 'http:/', 'http://', $url ); |
|
170 | - } |
|
171 | - return $url; |
|
172 | - } |
|
160 | + /** |
|
161 | + * |
|
162 | + * |
|
163 | + * @param string $url |
|
164 | + * @return string |
|
165 | + */ |
|
166 | + public static function remove_double_slashes( $url ) { |
|
167 | + $url = str_replace( '//', '/', $url ); |
|
168 | + if ( strstr( $url, 'http:' ) && !strstr( $url, 'http://' ) ) { |
|
169 | + $url = str_replace( 'http:/', 'http://', $url ); |
|
170 | + } |
|
171 | + return $url; |
|
172 | + } |
|
173 | 173 | |
174 | - /** |
|
175 | - * |
|
176 | - * |
|
177 | - * @param string $url |
|
178 | - * @param string $path |
|
179 | - * @return string |
|
180 | - */ |
|
181 | - public static function prepend_to_url( $url, $path ) { |
|
182 | - if ( strstr( strtolower( $url ), 'http' ) ) { |
|
183 | - $url_parts = parse_url( $url ); |
|
184 | - $url = $url_parts['scheme'] . '://' . $url_parts['host'] . $path . $url_parts['path']; |
|
185 | - if ( isset( $url_parts['query'] ) ) { |
|
186 | - $url .= $url_parts['query']; |
|
187 | - } |
|
188 | - if ( isset( $url_parts['fragment'] ) ) { |
|
189 | - $url .= $url_parts['fragment']; |
|
190 | - } |
|
191 | - } else { |
|
192 | - $url = $url . $path; |
|
193 | - } |
|
194 | - return self::remove_double_slashes( $url ); |
|
195 | - } |
|
174 | + /** |
|
175 | + * |
|
176 | + * |
|
177 | + * @param string $url |
|
178 | + * @param string $path |
|
179 | + * @return string |
|
180 | + */ |
|
181 | + public static function prepend_to_url( $url, $path ) { |
|
182 | + if ( strstr( strtolower( $url ), 'http' ) ) { |
|
183 | + $url_parts = parse_url( $url ); |
|
184 | + $url = $url_parts['scheme'] . '://' . $url_parts['host'] . $path . $url_parts['path']; |
|
185 | + if ( isset( $url_parts['query'] ) ) { |
|
186 | + $url .= $url_parts['query']; |
|
187 | + } |
|
188 | + if ( isset( $url_parts['fragment'] ) ) { |
|
189 | + $url .= $url_parts['fragment']; |
|
190 | + } |
|
191 | + } else { |
|
192 | + $url = $url . $path; |
|
193 | + } |
|
194 | + return self::remove_double_slashes( $url ); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * |
|
199 | - * |
|
200 | - * @param string $path |
|
201 | - * @return string |
|
202 | - */ |
|
203 | - public static function preslashit( $path ) { |
|
204 | - if ( strpos( $path, '/' ) != 0 ) { |
|
205 | - $path = '/' . $path; |
|
206 | - } |
|
207 | - return $path; |
|
208 | - } |
|
197 | + /** |
|
198 | + * |
|
199 | + * |
|
200 | + * @param string $path |
|
201 | + * @return string |
|
202 | + */ |
|
203 | + public static function preslashit( $path ) { |
|
204 | + if ( strpos( $path, '/' ) != 0 ) { |
|
205 | + $path = '/' . $path; |
|
206 | + } |
|
207 | + return $path; |
|
208 | + } |
|
209 | 209 | |
210 | - /** |
|
211 | - * This will evaluate wheter a URL is at an aboslute location (like http://example.org/whatever) |
|
212 | - * |
|
213 | - * @return boolean true if $path is an absolute url, false if relative. |
|
214 | - */ |
|
215 | - public static function is_absolute( $path ) { |
|
216 | - return (boolean) ( strstr( $path, 'http' ) ); |
|
217 | - } |
|
210 | + /** |
|
211 | + * This will evaluate wheter a URL is at an aboslute location (like http://example.org/whatever) |
|
212 | + * |
|
213 | + * @return boolean true if $path is an absolute url, false if relative. |
|
214 | + */ |
|
215 | + public static function is_absolute( $path ) { |
|
216 | + return (boolean) ( strstr( $path, 'http' ) ); |
|
217 | + } |
|
218 | 218 | |
219 | - /** |
|
220 | - * This function is slightly different from the one below in the case of: |
|
221 | - * an image hosted on the same domain BUT on a different site than the |
|
222 | - * Wordpress install will be reported as external content. |
|
223 | - * |
|
224 | - * @param string $url a URL to evaluate against |
|
225 | - * @return boolean if $url points to an external location returns true |
|
226 | - */ |
|
227 | - public static function is_external_content( $url ) { |
|
228 | - $is_external = TimberURLHelper::is_absolute( $url ) && ! TimberURLHelper::is_internal_content( $url ); |
|
219 | + /** |
|
220 | + * This function is slightly different from the one below in the case of: |
|
221 | + * an image hosted on the same domain BUT on a different site than the |
|
222 | + * Wordpress install will be reported as external content. |
|
223 | + * |
|
224 | + * @param string $url a URL to evaluate against |
|
225 | + * @return boolean if $url points to an external location returns true |
|
226 | + */ |
|
227 | + public static function is_external_content( $url ) { |
|
228 | + $is_external = TimberURLHelper::is_absolute( $url ) && ! TimberURLHelper::is_internal_content( $url ); |
|
229 | 229 | |
230 | - return $is_external; |
|
231 | - } |
|
230 | + return $is_external; |
|
231 | + } |
|
232 | 232 | |
233 | - private static function is_internal_content($url) { |
|
234 | - // using content_url() instead of site_url or home_url is IMPORTANT |
|
235 | - // otherwise you run into errors with sites that: |
|
236 | - // 1. use WPML plugin |
|
237 | - // 2. or redefine content directory |
|
238 | - $is_content_url = strstr( $url, content_url() ); |
|
233 | + private static function is_internal_content($url) { |
|
234 | + // using content_url() instead of site_url or home_url is IMPORTANT |
|
235 | + // otherwise you run into errors with sites that: |
|
236 | + // 1. use WPML plugin |
|
237 | + // 2. or redefine content directory |
|
238 | + $is_content_url = strstr( $url, content_url() ); |
|
239 | 239 | |
240 | - // this case covers when the upload directory has been redefined |
|
241 | - $upload_dir = wp_upload_dir(); |
|
242 | - $is_upload_url = strstr( $url, $upload_dir['baseurl'] ); |
|
240 | + // this case covers when the upload directory has been redefined |
|
241 | + $upload_dir = wp_upload_dir(); |
|
242 | + $is_upload_url = strstr( $url, $upload_dir['baseurl'] ); |
|
243 | 243 | |
244 | - return $is_content_url || $is_upload_url; |
|
245 | - } |
|
244 | + return $is_content_url || $is_upload_url; |
|
245 | + } |
|
246 | 246 | |
247 | - /** |
|
248 | - * |
|
249 | - * |
|
250 | - * @param string $url |
|
251 | - * @return bool true if $path is an external url, false if relative or local. |
|
252 | - * true if it's a subdomain (http://cdn.example.org = true) |
|
253 | - */ |
|
254 | - public static function is_external( $url ) { |
|
255 | - $has_http = strstr( strtolower( $url ), 'http' ); |
|
256 | - $on_domain = strstr( $url, self::get_host() ); |
|
257 | - if ( $has_http && !$on_domain ) { |
|
258 | - return true; |
|
259 | - } |
|
260 | - return false; |
|
261 | - } |
|
247 | + /** |
|
248 | + * |
|
249 | + * |
|
250 | + * @param string $url |
|
251 | + * @return bool true if $path is an external url, false if relative or local. |
|
252 | + * true if it's a subdomain (http://cdn.example.org = true) |
|
253 | + */ |
|
254 | + public static function is_external( $url ) { |
|
255 | + $has_http = strstr( strtolower( $url ), 'http' ); |
|
256 | + $on_domain = strstr( $url, self::get_host() ); |
|
257 | + if ( $has_http && !$on_domain ) { |
|
258 | + return true; |
|
259 | + } |
|
260 | + return false; |
|
261 | + } |
|
262 | 262 | |
263 | - /** |
|
264 | - * Pass links through untrailingslashit unless they are a single / |
|
265 | - * |
|
266 | - * @param string $link |
|
267 | - * @return string |
|
268 | - */ |
|
269 | - public static function remove_trailing_slash( $link ) { |
|
270 | - if ( $link != "/" ) |
|
271 | - $link = untrailingslashit( $link ); |
|
272 | - return $link; |
|
273 | - } |
|
263 | + /** |
|
264 | + * Pass links through untrailingslashit unless they are a single / |
|
265 | + * |
|
266 | + * @param string $link |
|
267 | + * @return string |
|
268 | + */ |
|
269 | + public static function remove_trailing_slash( $link ) { |
|
270 | + if ( $link != "/" ) |
|
271 | + $link = untrailingslashit( $link ); |
|
272 | + return $link; |
|
273 | + } |
|
274 | 274 | |
275 | - /** |
|
276 | - * Download an external file via a URL |
|
277 | - * |
|
278 | - * @param string $url |
|
279 | - * @param int $timeout |
|
280 | - * @return string|WP_Error the location of the temporay file name or an error |
|
281 | - * @deprecated since 0.20.0 |
|
282 | - */ |
|
283 | - static function download_url( $url, $timeout = 300 ) { |
|
284 | - if ( !$url ) { |
|
285 | - return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) ); |
|
286 | - } |
|
275 | + /** |
|
276 | + * Download an external file via a URL |
|
277 | + * |
|
278 | + * @param string $url |
|
279 | + * @param int $timeout |
|
280 | + * @return string|WP_Error the location of the temporay file name or an error |
|
281 | + * @deprecated since 0.20.0 |
|
282 | + */ |
|
283 | + static function download_url( $url, $timeout = 300 ) { |
|
284 | + if ( !$url ) { |
|
285 | + return new WP_Error( 'http_no_url', __( 'Invalid URL Provided.' ) ); |
|
286 | + } |
|
287 | 287 | |
288 | - $tmpfname = wp_tempnam( $url ); |
|
289 | - if ( !$tmpfname ) { |
|
290 | - return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) ); |
|
291 | - } |
|
288 | + $tmpfname = wp_tempnam( $url ); |
|
289 | + if ( !$tmpfname ) { |
|
290 | + return new WP_Error( 'http_no_file', __( 'Could not create Temporary file.' ) ); |
|
291 | + } |
|
292 | 292 | |
293 | - $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); |
|
293 | + $response = wp_remote_get( $url, array( 'timeout' => $timeout, 'stream' => true, 'filename' => $tmpfname ) ); |
|
294 | 294 | |
295 | - if ( is_wp_error( $response ) ) { |
|
296 | - unlink( $tmpfname ); |
|
297 | - return $response; |
|
298 | - } |
|
299 | - if ( 200 != wp_remote_retrieve_response_code( $response ) ) { |
|
300 | - unlink( $tmpfname ); |
|
301 | - return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) ); |
|
302 | - } |
|
303 | - return $tmpfname; |
|
304 | - } |
|
295 | + if ( is_wp_error( $response ) ) { |
|
296 | + unlink( $tmpfname ); |
|
297 | + return $response; |
|
298 | + } |
|
299 | + if ( 200 != wp_remote_retrieve_response_code( $response ) ) { |
|
300 | + unlink( $tmpfname ); |
|
301 | + return new WP_Error( 'http_404', trim( wp_remote_retrieve_response_message( $response ) ) ); |
|
302 | + } |
|
303 | + return $tmpfname; |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
307 | - * Returns the url parameters, for example for url http://example.org/blog/post/news/2014/whatever |
|
308 | - * this will return array('blog', 'post', 'news', '2014', 'whatever'); |
|
309 | - * OR if sent an integer like: TimberUrlHelper::get_params(2); this will return 'news'; |
|
310 | - * |
|
311 | - * @param int $i the position of the parameter to grab. |
|
312 | - * @return array|string |
|
313 | - */ |
|
314 | - public static function get_params( $i = false ) { |
|
315 | - $args = explode( '/', trim( strtolower( $_SERVER['REQUEST_URI'] ) ) ); |
|
316 | - $newargs = array(); |
|
317 | - foreach ( $args as $arg ) { |
|
318 | - if ( strlen( $arg ) ) { |
|
319 | - $newargs[] = $arg; |
|
320 | - } |
|
321 | - } |
|
322 | - if ( $i === false ) { |
|
323 | - return $newargs; |
|
324 | - } |
|
325 | - if ( $i < 0 ) { |
|
326 | - //count from end |
|
327 | - $i = count( $newargs ) + $i; |
|
328 | - } |
|
329 | - if ( isset( $newargs[$i] ) ) { |
|
330 | - return $newargs[$i]; |
|
331 | - } |
|
332 | - } |
|
306 | + /** |
|
307 | + * Returns the url parameters, for example for url http://example.org/blog/post/news/2014/whatever |
|
308 | + * this will return array('blog', 'post', 'news', '2014', 'whatever'); |
|
309 | + * OR if sent an integer like: TimberUrlHelper::get_params(2); this will return 'news'; |
|
310 | + * |
|
311 | + * @param int $i the position of the parameter to grab. |
|
312 | + * @return array|string |
|
313 | + */ |
|
314 | + public static function get_params( $i = false ) { |
|
315 | + $args = explode( '/', trim( strtolower( $_SERVER['REQUEST_URI'] ) ) ); |
|
316 | + $newargs = array(); |
|
317 | + foreach ( $args as $arg ) { |
|
318 | + if ( strlen( $arg ) ) { |
|
319 | + $newargs[] = $arg; |
|
320 | + } |
|
321 | + } |
|
322 | + if ( $i === false ) { |
|
323 | + return $newargs; |
|
324 | + } |
|
325 | + if ( $i < 0 ) { |
|
326 | + //count from end |
|
327 | + $i = count( $newargs ) + $i; |
|
328 | + } |
|
329 | + if ( isset( $newargs[$i] ) ) { |
|
330 | + return $newargs[$i]; |
|
331 | + } |
|
332 | + } |
|
333 | 333 | |
334 | 334 | } |
@@ -18,242 +18,242 @@ |
||
18 | 18 | */ |
19 | 19 | class TimberSite extends TimberCore implements TimberCoreInterface { |
20 | 20 | |
21 | - /** |
|
22 | - * @api |
|
23 | - * @var string the admin email address set in the WP admin panel |
|
24 | - */ |
|
25 | - public $admin_email; |
|
26 | - public $blogname; |
|
27 | - /** |
|
28 | - * @api |
|
29 | - * @var string |
|
30 | - */ |
|
31 | - public $charset; |
|
21 | + /** |
|
22 | + * @api |
|
23 | + * @var string the admin email address set in the WP admin panel |
|
24 | + */ |
|
25 | + public $admin_email; |
|
26 | + public $blogname; |
|
27 | + /** |
|
28 | + * @api |
|
29 | + * @var string |
|
30 | + */ |
|
31 | + public $charset; |
|
32 | 32 | |
33 | - /** |
|
34 | - * @api |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - public $description; |
|
38 | - /** |
|
39 | - * @api |
|
40 | - * @var int the ID of a site in multisite |
|
41 | - */ |
|
42 | - public $id; |
|
43 | - /** |
|
44 | - * @api |
|
45 | - * @var string the language setting ex: en-US |
|
46 | - */ |
|
47 | - public $language; |
|
48 | - /** |
|
49 | - * @api |
|
50 | - * @var string of language attributes for usage in the <html> tag |
|
51 | - */ |
|
52 | - public $language_attributes; |
|
53 | - /** |
|
54 | - * @api |
|
55 | - * @var bool true if multisite, false if plain ole' WordPress |
|
56 | - */ |
|
57 | - public $multisite; |
|
33 | + /** |
|
34 | + * @api |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + public $description; |
|
38 | + /** |
|
39 | + * @api |
|
40 | + * @var int the ID of a site in multisite |
|
41 | + */ |
|
42 | + public $id; |
|
43 | + /** |
|
44 | + * @api |
|
45 | + * @var string the language setting ex: en-US |
|
46 | + */ |
|
47 | + public $language; |
|
48 | + /** |
|
49 | + * @api |
|
50 | + * @var string of language attributes for usage in the <html> tag |
|
51 | + */ |
|
52 | + public $language_attributes; |
|
53 | + /** |
|
54 | + * @api |
|
55 | + * @var bool true if multisite, false if plain ole' WordPress |
|
56 | + */ |
|
57 | + public $multisite; |
|
58 | 58 | |
59 | - /** |
|
60 | - * @api |
|
61 | - * @var string |
|
62 | - */ |
|
63 | - public $name; |
|
59 | + /** |
|
60 | + * @api |
|
61 | + * @var string |
|
62 | + */ |
|
63 | + public $name; |
|
64 | 64 | |
65 | - /** @api |
|
66 | - * @var string for people who like trackback spam |
|
67 | - */ |
|
68 | - public $pingback_url; |
|
69 | - public $siteurl; |
|
70 | - /** |
|
71 | - * @api |
|
72 | - * @var [TimberTheme](#TimberTheme) |
|
73 | - */ |
|
74 | - public $theme; |
|
75 | - /** |
|
76 | - * @api |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - public $title; |
|
80 | - public $url; |
|
65 | + /** @api |
|
66 | + * @var string for people who like trackback spam |
|
67 | + */ |
|
68 | + public $pingback_url; |
|
69 | + public $siteurl; |
|
70 | + /** |
|
71 | + * @api |
|
72 | + * @var [TimberTheme](#TimberTheme) |
|
73 | + */ |
|
74 | + public $theme; |
|
75 | + /** |
|
76 | + * @api |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + public $title; |
|
80 | + public $url; |
|
81 | 81 | |
82 | - /** |
|
83 | - * @api |
|
84 | - * @var string |
|
85 | - */ |
|
82 | + /** |
|
83 | + * @api |
|
84 | + * @var string |
|
85 | + */ |
|
86 | 86 | |
87 | - public $rdf; |
|
88 | - public $rss; |
|
89 | - public $rss2; |
|
90 | - public $atom; |
|
87 | + public $rdf; |
|
88 | + public $rss; |
|
89 | + public $rss2; |
|
90 | + public $atom; |
|
91 | 91 | |
92 | - /** |
|
93 | - * Constructs a TimberSite object |
|
94 | - * @example |
|
95 | - * ```php |
|
96 | - * //multisite setup |
|
97 | - * $site = new TimberSite(1); |
|
98 | - * $site_two = new TimberSite("My Cool Site"); |
|
99 | - * //non-multisite |
|
100 | - * $site = new TimberSite(); |
|
101 | - * ``` |
|
102 | - * @param string|int $site_name_or_id |
|
103 | - */ |
|
104 | - function __construct( $site_name_or_id = null ) { |
|
105 | - $this->init(); |
|
106 | - if ( is_multisite() ) { |
|
107 | - $this->init_as_multisite( $site_name_or_id ); |
|
108 | - } else { |
|
109 | - $this->init_as_singlesite(); |
|
110 | - } |
|
111 | - } |
|
92 | + /** |
|
93 | + * Constructs a TimberSite object |
|
94 | + * @example |
|
95 | + * ```php |
|
96 | + * //multisite setup |
|
97 | + * $site = new TimberSite(1); |
|
98 | + * $site_two = new TimberSite("My Cool Site"); |
|
99 | + * //non-multisite |
|
100 | + * $site = new TimberSite(); |
|
101 | + * ``` |
|
102 | + * @param string|int $site_name_or_id |
|
103 | + */ |
|
104 | + function __construct( $site_name_or_id = null ) { |
|
105 | + $this->init(); |
|
106 | + if ( is_multisite() ) { |
|
107 | + $this->init_as_multisite( $site_name_or_id ); |
|
108 | + } else { |
|
109 | + $this->init_as_singlesite(); |
|
110 | + } |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * @internal |
|
115 | - * @param string|int $site_name_or_id |
|
116 | - */ |
|
117 | - protected function init_as_multisite( $site_name_or_id ) { |
|
118 | - if ( $site_name_or_id === null ) { |
|
119 | - //this is necessary for some reason, otherwise returns 1 all the time |
|
120 | - if ( is_multisite() ) { |
|
121 | - restore_current_blog(); |
|
122 | - $site_name_or_id = get_current_blog_id(); |
|
123 | - } |
|
124 | - } |
|
125 | - $info = get_blog_details( $site_name_or_id ); |
|
126 | - $this->import( $info ); |
|
127 | - $this->ID = $info->blog_id; |
|
128 | - $this->id = $this->ID; |
|
129 | - $this->name = $this->blogname; |
|
130 | - $this->title = $this->blogname; |
|
131 | - $this->url = $this->siteurl; |
|
132 | - $theme_slug = get_blog_option( $info->blog_id, 'stylesheet' ); |
|
133 | - $this->theme = new TimberTheme( $theme_slug ); |
|
134 | - $this->description = get_blog_option( $info->blog_id, 'blogdescription' ); |
|
135 | - $this->admin_email = get_blog_option( $info->blog_id, 'admin_email' ); |
|
136 | - $this->multisite = true; |
|
137 | - } |
|
113 | + /** |
|
114 | + * @internal |
|
115 | + * @param string|int $site_name_or_id |
|
116 | + */ |
|
117 | + protected function init_as_multisite( $site_name_or_id ) { |
|
118 | + if ( $site_name_or_id === null ) { |
|
119 | + //this is necessary for some reason, otherwise returns 1 all the time |
|
120 | + if ( is_multisite() ) { |
|
121 | + restore_current_blog(); |
|
122 | + $site_name_or_id = get_current_blog_id(); |
|
123 | + } |
|
124 | + } |
|
125 | + $info = get_blog_details( $site_name_or_id ); |
|
126 | + $this->import( $info ); |
|
127 | + $this->ID = $info->blog_id; |
|
128 | + $this->id = $this->ID; |
|
129 | + $this->name = $this->blogname; |
|
130 | + $this->title = $this->blogname; |
|
131 | + $this->url = $this->siteurl; |
|
132 | + $theme_slug = get_blog_option( $info->blog_id, 'stylesheet' ); |
|
133 | + $this->theme = new TimberTheme( $theme_slug ); |
|
134 | + $this->description = get_blog_option( $info->blog_id, 'blogdescription' ); |
|
135 | + $this->admin_email = get_blog_option( $info->blog_id, 'admin_email' ); |
|
136 | + $this->multisite = true; |
|
137 | + } |
|
138 | 138 | |
139 | - /** |
|
140 | - * Executed for single-blog sites |
|
141 | - * @internal |
|
142 | - */ |
|
143 | - protected function init_as_singlesite() { |
|
144 | - $this->admin_email = get_bloginfo( 'admin_email' ); |
|
145 | - $this->name = get_bloginfo( 'name' ); |
|
146 | - $this->title = $this->name; |
|
147 | - $this->description = get_bloginfo( 'description' ); |
|
148 | - $this->url = get_bloginfo( 'url' ); |
|
149 | - $this->theme = new TimberTheme(); |
|
150 | - $this->language_attributes = TimberHelper::function_wrapper( 'language_attributes' ); |
|
151 | - $this->multisite = false; |
|
152 | - } |
|
139 | + /** |
|
140 | + * Executed for single-blog sites |
|
141 | + * @internal |
|
142 | + */ |
|
143 | + protected function init_as_singlesite() { |
|
144 | + $this->admin_email = get_bloginfo( 'admin_email' ); |
|
145 | + $this->name = get_bloginfo( 'name' ); |
|
146 | + $this->title = $this->name; |
|
147 | + $this->description = get_bloginfo( 'description' ); |
|
148 | + $this->url = get_bloginfo( 'url' ); |
|
149 | + $this->theme = new TimberTheme(); |
|
150 | + $this->language_attributes = TimberHelper::function_wrapper( 'language_attributes' ); |
|
151 | + $this->multisite = false; |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * Executed for all types of sites: both multisite and "regular" |
|
156 | - * @internal |
|
157 | - */ |
|
158 | - protected function init() { |
|
159 | - $this->rdf = get_bloginfo( 'rdf_url' ); |
|
160 | - $this->rss = get_bloginfo( 'rss_url' ); |
|
161 | - $this->rss2 = get_bloginfo( 'rss2_url' ); |
|
162 | - $this->atom = get_bloginfo( 'atom_url' ); |
|
163 | - $this->language = get_bloginfo( 'language' ); |
|
164 | - $this->charset = get_bloginfo( 'charset' ); |
|
165 | - $this->pingback = get_bloginfo( 'pingback_url' ); |
|
166 | - $this->language_attributes = TimberHelper::function_wrapper( 'language_attributes' ); |
|
167 | - /* deprecated benath this comment */ |
|
168 | - $this->pingback_url = get_bloginfo( 'pingback_url' ); |
|
169 | - } |
|
154 | + /** |
|
155 | + * Executed for all types of sites: both multisite and "regular" |
|
156 | + * @internal |
|
157 | + */ |
|
158 | + protected function init() { |
|
159 | + $this->rdf = get_bloginfo( 'rdf_url' ); |
|
160 | + $this->rss = get_bloginfo( 'rss_url' ); |
|
161 | + $this->rss2 = get_bloginfo( 'rss2_url' ); |
|
162 | + $this->atom = get_bloginfo( 'atom_url' ); |
|
163 | + $this->language = get_bloginfo( 'language' ); |
|
164 | + $this->charset = get_bloginfo( 'charset' ); |
|
165 | + $this->pingback = get_bloginfo( 'pingback_url' ); |
|
166 | + $this->language_attributes = TimberHelper::function_wrapper( 'language_attributes' ); |
|
167 | + /* deprecated benath this comment */ |
|
168 | + $this->pingback_url = get_bloginfo( 'pingback_url' ); |
|
169 | + } |
|
170 | 170 | |
171 | - /** |
|
172 | - * |
|
173 | - * |
|
174 | - * @param string $field |
|
175 | - * @return mixed |
|
176 | - */ |
|
177 | - function __get( $field ) { |
|
178 | - if ( !isset( $this->$field ) ) { |
|
179 | - if ( is_multisite() ) { |
|
180 | - $this->$field = get_blog_option( $this->ID, $field ); |
|
181 | - } else { |
|
182 | - $this->$field = get_option( $field ); |
|
183 | - } |
|
184 | - } |
|
185 | - return $this->$field; |
|
186 | - } |
|
171 | + /** |
|
172 | + * |
|
173 | + * |
|
174 | + * @param string $field |
|
175 | + * @return mixed |
|
176 | + */ |
|
177 | + function __get( $field ) { |
|
178 | + if ( !isset( $this->$field ) ) { |
|
179 | + if ( is_multisite() ) { |
|
180 | + $this->$field = get_blog_option( $this->ID, $field ); |
|
181 | + } else { |
|
182 | + $this->$field = get_option( $field ); |
|
183 | + } |
|
184 | + } |
|
185 | + return $this->$field; |
|
186 | + } |
|
187 | 187 | |
188 | - /** |
|
189 | - * @deprecated 0.21.9 |
|
190 | - * @internal |
|
191 | - * @return string |
|
192 | - */ |
|
193 | - function get_link() { |
|
194 | - return $this->link(); |
|
195 | - } |
|
188 | + /** |
|
189 | + * @deprecated 0.21.9 |
|
190 | + * @internal |
|
191 | + * @return string |
|
192 | + */ |
|
193 | + function get_link() { |
|
194 | + return $this->link(); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * @deprecated 0.21.9 |
|
199 | - * @internal |
|
200 | - * @return string |
|
201 | - */ |
|
202 | - function get_url() { |
|
203 | - return $this->get_link(); |
|
204 | - } |
|
197 | + /** |
|
198 | + * @deprecated 0.21.9 |
|
199 | + * @internal |
|
200 | + * @return string |
|
201 | + */ |
|
202 | + function get_url() { |
|
203 | + return $this->get_link(); |
|
204 | + } |
|
205 | 205 | |
206 | - /** |
|
207 | - * Returns the link to the site's home. |
|
208 | - * @example |
|
209 | - * ```twig |
|
210 | - * <a href="{{ site.link }}" title="Home"> |
|
211 | - * <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" /> |
|
212 | - * </a> |
|
213 | - * ``` |
|
214 | - * ```html |
|
215 | - * <a href="http://example.org" title="Home"> |
|
216 | - * <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" /> |
|
217 | - * </a> |
|
218 | - * ``` |
|
219 | - * @api |
|
220 | - * @return string |
|
221 | - */ |
|
222 | - public function link() { |
|
223 | - return $this->url; |
|
224 | - } |
|
206 | + /** |
|
207 | + * Returns the link to the site's home. |
|
208 | + * @example |
|
209 | + * ```twig |
|
210 | + * <a href="{{ site.link }}" title="Home"> |
|
211 | + * <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" /> |
|
212 | + * </a> |
|
213 | + * ``` |
|
214 | + * ```html |
|
215 | + * <a href="http://example.org" title="Home"> |
|
216 | + * <img src="/wp-content/uploads/logo.png" alt="Logo for some stupid thing" /> |
|
217 | + * </a> |
|
218 | + * ``` |
|
219 | + * @api |
|
220 | + * @return string |
|
221 | + */ |
|
222 | + public function link() { |
|
223 | + return $this->url; |
|
224 | + } |
|
225 | 225 | |
226 | - /** |
|
227 | - * @ignore |
|
228 | - */ |
|
229 | - public function meta( $field ) { |
|
230 | - return $this->__get( $field ); |
|
231 | - } |
|
226 | + /** |
|
227 | + * @ignore |
|
228 | + */ |
|
229 | + public function meta( $field ) { |
|
230 | + return $this->__get( $field ); |
|
231 | + } |
|
232 | 232 | |
233 | - /** |
|
234 | - * |
|
235 | - * @ignore |
|
236 | - * @param string $key |
|
237 | - * @param mixed $value |
|
238 | - */ |
|
239 | - public function update( $key, $value ) { |
|
240 | - $value = apply_filters( 'timber_site_set_meta', $value, $key, $this->ID, $this ); |
|
241 | - if ( is_multisite() ) { |
|
242 | - update_blog_option( $this->ID, $key, $value ); |
|
243 | - } else { |
|
244 | - update_option( $key, $value ); |
|
245 | - } |
|
246 | - $this->$key = $value; |
|
247 | - } |
|
233 | + /** |
|
234 | + * |
|
235 | + * @ignore |
|
236 | + * @param string $key |
|
237 | + * @param mixed $value |
|
238 | + */ |
|
239 | + public function update( $key, $value ) { |
|
240 | + $value = apply_filters( 'timber_site_set_meta', $value, $key, $this->ID, $this ); |
|
241 | + if ( is_multisite() ) { |
|
242 | + update_blog_option( $this->ID, $key, $value ); |
|
243 | + } else { |
|
244 | + update_option( $key, $value ); |
|
245 | + } |
|
246 | + $this->$key = $value; |
|
247 | + } |
|
248 | 248 | |
249 | - /** |
|
250 | - * |
|
251 | - * @api |
|
252 | - * @see TimberSite::link |
|
253 | - * @return string |
|
254 | - */ |
|
255 | - function url() { |
|
256 | - return $this->get_link(); |
|
257 | - } |
|
249 | + /** |
|
250 | + * |
|
251 | + * @api |
|
252 | + * @see TimberSite::link |
|
253 | + * @return string |
|
254 | + */ |
|
255 | + function url() { |
|
256 | + return $this->get_link(); |
|
257 | + } |
|
258 | 258 | |
259 | 259 | } |
@@ -3,32 +3,32 @@ |
||
3 | 3 | * TimberRequest exposes $_GET and $_POST to the context |
4 | 4 | */ |
5 | 5 | class TimberRequest extends TimberCore implements TimberCoreInterface { |
6 | - public $post = array(); |
|
7 | - public $get = array(); |
|
6 | + public $post = array(); |
|
7 | + public $get = array(); |
|
8 | 8 | |
9 | - /** |
|
10 | - * Constructs a TimberRequest object |
|
11 | - * @example |
|
12 | - */ |
|
13 | - function __construct() { |
|
14 | - $this->init(); |
|
15 | - } |
|
16 | - /** |
|
17 | - * @internal |
|
18 | - */ |
|
19 | - protected function init() { |
|
20 | - $this->post = $_POST; |
|
21 | - $this->get = $_GET; |
|
22 | - } |
|
9 | + /** |
|
10 | + * Constructs a TimberRequest object |
|
11 | + * @example |
|
12 | + */ |
|
13 | + function __construct() { |
|
14 | + $this->init(); |
|
15 | + } |
|
16 | + /** |
|
17 | + * @internal |
|
18 | + */ |
|
19 | + protected function init() { |
|
20 | + $this->post = $_POST; |
|
21 | + $this->get = $_GET; |
|
22 | + } |
|
23 | 23 | |
24 | - public function __call( $field, $args ) {} |
|
24 | + public function __call( $field, $args ) {} |
|
25 | 25 | |
26 | - public function __get( $field ) {} |
|
26 | + public function __get( $field ) {} |
|
27 | 27 | |
28 | - /** |
|
29 | - * @return boolean |
|
30 | - */ |
|
31 | - public function __isset( $field ) {} |
|
28 | + /** |
|
29 | + * @return boolean |
|
30 | + */ |
|
31 | + public function __isset( $field ) {} |
|
32 | 32 | |
33 | - public function meta( $key ) {} |
|
33 | + public function meta( $key ) {} |
|
34 | 34 | } |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Contains the class for running image retina-izing operations |
|
4 | - */ |
|
3 | + * Contains the class for running image retina-izing operations |
|
4 | + */ |
|
5 | 5 | |
6 | 6 | /** |
7 | 7 | * Increases image size by a given factor |
@@ -56,11 +56,11 @@ discard block |
||
56 | 56 | $image->crop( 0, 0, $src_w, $src_h, $w, $h ); |
57 | 57 | $result = $image->save( $save_filename ); |
58 | 58 | if ( is_wp_error( $result ) ) { |
59 | - // @codeCoverageIgnoreStart |
|
60 | - TimberHelper::error_log( 'Error resizing image' ); |
|
61 | - TimberHelper::error_log( $result ); |
|
62 | - return false; |
|
63 | - // @codeCoverageIgnoreEnd |
|
59 | + // @codeCoverageIgnoreStart |
|
60 | + TimberHelper::error_log( 'Error resizing image' ); |
|
61 | + TimberHelper::error_log( $result ); |
|
62 | + return false; |
|
63 | + // @codeCoverageIgnoreEnd |
|
64 | 64 | } else { |
65 | 65 | return true; |
66 | 66 | } |
@@ -1,203 +1,203 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Changes image to new size, by shrinking/enlarging |
|
4 | - * then cropping to respect new ratio. |
|
5 | - * |
|
6 | - * Arguments: |
|
7 | - * - width of new image |
|
8 | - * - height of new image |
|
9 | - * - crop method |
|
10 | - */ |
|
3 | + * Changes image to new size, by shrinking/enlarging |
|
4 | + * then cropping to respect new ratio. |
|
5 | + * |
|
6 | + * Arguments: |
|
7 | + * - width of new image |
|
8 | + * - height of new image |
|
9 | + * - crop method |
|
10 | + */ |
|
11 | 11 | class TimberImageOperationResize extends TimberImageOperation { |
12 | 12 | |
13 | - private $w, $h, $crop; |
|
13 | + private $w, $h, $crop; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param int $w width of new image |
|
17 | - * @param int $h height of new image |
|
18 | - * @param string $crop cropping method, one of: 'default', 'center', 'top', 'bottom', 'left', 'right', 'top-center', 'bottom-center'. |
|
19 | - */ |
|
20 | - function __construct($w, $h, $crop) { |
|
21 | - $this->w = $w; |
|
22 | - $this->h = $h; |
|
23 | - // Sanitize crop position |
|
24 | - $allowed_crop_positions = array( 'default', 'center', 'top', 'bottom', 'left', 'right', 'top-center', 'bottom-center' ); |
|
25 | - if ( $crop !== false && !in_array( $crop, $allowed_crop_positions ) ) { |
|
26 | - $crop = $allowed_crop_positions[0]; |
|
27 | - } |
|
28 | - $this->crop = $crop; |
|
29 | - } |
|
15 | + /** |
|
16 | + * @param int $w width of new image |
|
17 | + * @param int $h height of new image |
|
18 | + * @param string $crop cropping method, one of: 'default', 'center', 'top', 'bottom', 'left', 'right', 'top-center', 'bottom-center'. |
|
19 | + */ |
|
20 | + function __construct($w, $h, $crop) { |
|
21 | + $this->w = $w; |
|
22 | + $this->h = $h; |
|
23 | + // Sanitize crop position |
|
24 | + $allowed_crop_positions = array( 'default', 'center', 'top', 'bottom', 'left', 'right', 'top-center', 'bottom-center' ); |
|
25 | + if ( $crop !== false && !in_array( $crop, $allowed_crop_positions ) ) { |
|
26 | + $crop = $allowed_crop_positions[0]; |
|
27 | + } |
|
28 | + $this->crop = $crop; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
|
33 | - * @param string $src_extension the extension (ex: .jpg) |
|
34 | - * @return string the final filename to be used (ex: my-awesome-pic-300x200-c-default.jpg) |
|
35 | - */ |
|
36 | - public function filename($src_filename, $src_extension) { |
|
37 | - $w = 0; |
|
38 | - $h = 0; |
|
39 | - if ( $this->w ) { |
|
40 | - $w = $this->w; |
|
41 | - } |
|
42 | - if ( $this->h ) { |
|
43 | - $h = $this->h; |
|
44 | - } |
|
45 | - $result = $src_filename . '-' . $w . 'x' . $h . '-c-' . ( $this->crop ? $this->crop : 'f' ); // Crop will be either user named or f (false) |
|
46 | - if($src_extension) { |
|
47 | - $result .= '.'.$src_extension; |
|
48 | - } |
|
49 | - return $result; |
|
50 | - } |
|
31 | + /** |
|
32 | + * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
|
33 | + * @param string $src_extension the extension (ex: .jpg) |
|
34 | + * @return string the final filename to be used (ex: my-awesome-pic-300x200-c-default.jpg) |
|
35 | + */ |
|
36 | + public function filename($src_filename, $src_extension) { |
|
37 | + $w = 0; |
|
38 | + $h = 0; |
|
39 | + if ( $this->w ) { |
|
40 | + $w = $this->w; |
|
41 | + } |
|
42 | + if ( $this->h ) { |
|
43 | + $h = $this->h; |
|
44 | + } |
|
45 | + $result = $src_filename . '-' . $w . 'x' . $h . '-c-' . ( $this->crop ? $this->crop : 'f' ); // Crop will be either user named or f (false) |
|
46 | + if($src_extension) { |
|
47 | + $result .= '.'.$src_extension; |
|
48 | + } |
|
49 | + return $result; |
|
50 | + } |
|
51 | 51 | |
52 | - /** |
|
53 | - * @param string $load_filename |
|
54 | - * @param string $save_filename |
|
55 | - */ |
|
56 | - protected function run_animated_gif( $load_filename, $save_filename ) { |
|
57 | - $image = wp_get_image_editor( $load_filename ); |
|
58 | - $current_size = $image->get_size(); |
|
59 | - $src_w = $current_size['width']; |
|
60 | - $src_h = $current_size['height']; |
|
61 | - $w = $this->w; |
|
62 | - $h = $this->h; |
|
63 | - if ( !class_exists('Imagick') ) { |
|
64 | - return false; |
|
65 | - } |
|
66 | - $image = new Imagick($load_filename); |
|
67 | - $image = $image->coalesceImages(); |
|
68 | - $crop = self::get_target_sizes( $load_filename ); |
|
69 | - foreach ($image as $frame) { |
|
70 | - $frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']); |
|
71 | - $frame->thumbnailImage($w, $h); |
|
72 | - $frame->setImagePage($w, $h, 0, 0); |
|
73 | - } |
|
74 | - $image = $image->deconstructImages(); |
|
75 | - return $image->writeImages($save_filename, true); |
|
76 | - } |
|
52 | + /** |
|
53 | + * @param string $load_filename |
|
54 | + * @param string $save_filename |
|
55 | + */ |
|
56 | + protected function run_animated_gif( $load_filename, $save_filename ) { |
|
57 | + $image = wp_get_image_editor( $load_filename ); |
|
58 | + $current_size = $image->get_size(); |
|
59 | + $src_w = $current_size['width']; |
|
60 | + $src_h = $current_size['height']; |
|
61 | + $w = $this->w; |
|
62 | + $h = $this->h; |
|
63 | + if ( !class_exists('Imagick') ) { |
|
64 | + return false; |
|
65 | + } |
|
66 | + $image = new Imagick($load_filename); |
|
67 | + $image = $image->coalesceImages(); |
|
68 | + $crop = self::get_target_sizes( $load_filename ); |
|
69 | + foreach ($image as $frame) { |
|
70 | + $frame->cropImage($crop['src_w'], $crop['src_h'], $crop['x'], $crop['y']); |
|
71 | + $frame->thumbnailImage($w, $h); |
|
72 | + $frame->setImagePage($w, $h, 0, 0); |
|
73 | + } |
|
74 | + $image = $image->deconstructImages(); |
|
75 | + return $image->writeImages($save_filename, true); |
|
76 | + } |
|
77 | 77 | |
78 | - protected function get_target_sizes( $load_filename ) { |
|
79 | - $image = wp_get_image_editor( $load_filename ); |
|
80 | - $w = $this->w; |
|
81 | - $h = $this->h; |
|
82 | - $crop = $this->crop; |
|
78 | + protected function get_target_sizes( $load_filename ) { |
|
79 | + $image = wp_get_image_editor( $load_filename ); |
|
80 | + $w = $this->w; |
|
81 | + $h = $this->h; |
|
82 | + $crop = $this->crop; |
|
83 | 83 | |
84 | - $current_size = $image->get_size(); |
|
85 | - $src_w = $current_size['width']; |
|
86 | - $src_h = $current_size['height']; |
|
87 | - $src_ratio = $src_w / $src_h; |
|
88 | - if ( !$h ) { |
|
89 | - $h = round( $w / $src_ratio ); |
|
90 | - } |
|
91 | - if ( !$w ) { |
|
92 | - //the user wants to resize based on constant height |
|
93 | - $w = round( $h * $src_ratio ); |
|
94 | - } |
|
95 | - if ( !$crop ) { |
|
96 | - return array( |
|
97 | - 'x' => 0, 'y' => 0, |
|
98 | - 'src_w' => $src_w, 'src_h' => $src_h, |
|
99 | - 'target_w' => $w, 'target_h' => $h |
|
100 | - ); |
|
101 | - } |
|
102 | - // Get ratios |
|
103 | - $dest_ratio = $w / $h; |
|
104 | - $src_wt = $src_h * $dest_ratio; |
|
105 | - $src_ht = $src_w / $dest_ratio; |
|
106 | - $src_x = $src_w / 2 - $src_wt / 2; |
|
107 | - $src_y = ( $src_h - $src_ht ) / 6; |
|
108 | - //now specific overrides based on options: |
|
109 | - switch ( $crop ) { |
|
110 | - case 'center': |
|
111 | - // Get source x and y |
|
112 | - $src_x = round( ( $src_w - $src_wt ) / 2 ); |
|
113 | - $src_y = round( ( $src_h - $src_ht ) / 2 ); |
|
114 | - break; |
|
84 | + $current_size = $image->get_size(); |
|
85 | + $src_w = $current_size['width']; |
|
86 | + $src_h = $current_size['height']; |
|
87 | + $src_ratio = $src_w / $src_h; |
|
88 | + if ( !$h ) { |
|
89 | + $h = round( $w / $src_ratio ); |
|
90 | + } |
|
91 | + if ( !$w ) { |
|
92 | + //the user wants to resize based on constant height |
|
93 | + $w = round( $h * $src_ratio ); |
|
94 | + } |
|
95 | + if ( !$crop ) { |
|
96 | + return array( |
|
97 | + 'x' => 0, 'y' => 0, |
|
98 | + 'src_w' => $src_w, 'src_h' => $src_h, |
|
99 | + 'target_w' => $w, 'target_h' => $h |
|
100 | + ); |
|
101 | + } |
|
102 | + // Get ratios |
|
103 | + $dest_ratio = $w / $h; |
|
104 | + $src_wt = $src_h * $dest_ratio; |
|
105 | + $src_ht = $src_w / $dest_ratio; |
|
106 | + $src_x = $src_w / 2 - $src_wt / 2; |
|
107 | + $src_y = ( $src_h - $src_ht ) / 6; |
|
108 | + //now specific overrides based on options: |
|
109 | + switch ( $crop ) { |
|
110 | + case 'center': |
|
111 | + // Get source x and y |
|
112 | + $src_x = round( ( $src_w - $src_wt ) / 2 ); |
|
113 | + $src_y = round( ( $src_h - $src_ht ) / 2 ); |
|
114 | + break; |
|
115 | 115 | |
116 | - case 'top': |
|
117 | - $src_y = 0; |
|
118 | - break; |
|
116 | + case 'top': |
|
117 | + $src_y = 0; |
|
118 | + break; |
|
119 | 119 | |
120 | - case 'bottom': |
|
121 | - $src_y = $src_h - $src_ht; |
|
122 | - break; |
|
120 | + case 'bottom': |
|
121 | + $src_y = $src_h - $src_ht; |
|
122 | + break; |
|
123 | 123 | |
124 | - case 'top-center': |
|
125 | - $src_y = round( ( $src_h - $src_ht ) / 4 ); |
|
126 | - break; |
|
124 | + case 'top-center': |
|
125 | + $src_y = round( ( $src_h - $src_ht ) / 4 ); |
|
126 | + break; |
|
127 | 127 | |
128 | - case 'bottom-center': |
|
129 | - $src_y = $src_h - $src_ht - round( ( $src_h - $src_ht ) / 4 ); |
|
130 | - break; |
|
128 | + case 'bottom-center': |
|
129 | + $src_y = $src_h - $src_ht - round( ( $src_h - $src_ht ) / 4 ); |
|
130 | + break; |
|
131 | 131 | |
132 | - case 'left': |
|
133 | - $src_x = 0; |
|
134 | - break; |
|
132 | + case 'left': |
|
133 | + $src_x = 0; |
|
134 | + break; |
|
135 | 135 | |
136 | - case 'right': |
|
137 | - $src_x = $src_w - $src_wt; |
|
138 | - break; |
|
139 | - } |
|
140 | - // Crop the image |
|
141 | - return ( $dest_ratio > $src_ratio ) |
|
142 | - ? array( |
|
143 | - 'x' => 0, 'y' => $src_y, |
|
144 | - 'src_w' => $src_w, 'src_h' => $src_ht, |
|
145 | - 'target_w' => $w, 'target_h' => $h |
|
146 | - ) |
|
147 | - : array( |
|
148 | - 'x' => $src_x, 'y' => 0, |
|
149 | - 'src_w' => $src_wt, 'src_h' => $src_h, |
|
150 | - 'target_w' => $w, 'target_h' => $h |
|
151 | - ); |
|
152 | - } |
|
136 | + case 'right': |
|
137 | + $src_x = $src_w - $src_wt; |
|
138 | + break; |
|
139 | + } |
|
140 | + // Crop the image |
|
141 | + return ( $dest_ratio > $src_ratio ) |
|
142 | + ? array( |
|
143 | + 'x' => 0, 'y' => $src_y, |
|
144 | + 'src_w' => $src_w, 'src_h' => $src_ht, |
|
145 | + 'target_w' => $w, 'target_h' => $h |
|
146 | + ) |
|
147 | + : array( |
|
148 | + 'x' => $src_x, 'y' => 0, |
|
149 | + 'src_w' => $src_wt, 'src_h' => $src_h, |
|
150 | + 'target_w' => $w, 'target_h' => $h |
|
151 | + ); |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * Performs the actual image manipulation, |
|
156 | - * including saving the target file. |
|
157 | - * |
|
158 | - * @param string $load_filename filepath (not URL) to source file |
|
159 | - * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
|
160 | - * @param string $save_filename filepath (not URL) where result file should be saved |
|
161 | - * (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg) |
|
162 | - * @return bool true if everything went fine, false otherwise |
|
163 | - */ |
|
164 | - public function run($load_filename, $save_filename) { |
|
165 | - //should be resized by gif resizer |
|
166 | - if ( TimberImageHelper::is_animated_gif($load_filename) ) { |
|
167 | - //attempt to resize |
|
168 | - //return if successful |
|
169 | - //proceed if not |
|
170 | - $gif = self::run_animated_gif($load_filename, $save_filename); |
|
171 | - if ($gif) { |
|
172 | - return true; |
|
173 | - } |
|
174 | - } |
|
175 | - $image = wp_get_image_editor( $load_filename ); |
|
176 | - if ( !is_wp_error( $image ) ) { |
|
177 | - $crop = self::get_target_sizes( $load_filename ); |
|
178 | - $image->crop( $crop['x'], |
|
179 | - $crop['y'], |
|
180 | - $crop['src_w'], |
|
181 | - $crop['src_h'], |
|
182 | - $crop['target_w'], |
|
183 | - $crop['target_h'] |
|
184 | - ); |
|
185 | - $result = $image->save( $save_filename ); |
|
186 | - if ( is_wp_error( $result ) ) { |
|
187 | - // @codeCoverageIgnoreStart |
|
188 | - TimberHelper::error_log( 'Error resizing image' ); |
|
189 | - TimberHelper::error_log( $result ); |
|
190 | - return false; |
|
191 | - // @codeCoverageIgnoreEnd |
|
192 | - } else { |
|
193 | - return true; |
|
194 | - } |
|
195 | - } else if ( isset( $image->error_data['error_loading_image'] ) ) { |
|
196 | - // @codeCoverageIgnoreStart |
|
197 | - TimberHelper::error_log( 'Error loading ' . $image->error_data['error_loading_image'] ); |
|
198 | - } else { |
|
199 | - TimberHelper::error_log( $image ); |
|
200 | - // @codeCoverageIgnoreEnd |
|
201 | - } |
|
202 | - } |
|
154 | + /** |
|
155 | + * Performs the actual image manipulation, |
|
156 | + * including saving the target file. |
|
157 | + * |
|
158 | + * @param string $load_filename filepath (not URL) to source file |
|
159 | + * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
|
160 | + * @param string $save_filename filepath (not URL) where result file should be saved |
|
161 | + * (ex: /src/var/www/wp-content/uploads/my-pic-300x200-c-default.jpg) |
|
162 | + * @return bool true if everything went fine, false otherwise |
|
163 | + */ |
|
164 | + public function run($load_filename, $save_filename) { |
|
165 | + //should be resized by gif resizer |
|
166 | + if ( TimberImageHelper::is_animated_gif($load_filename) ) { |
|
167 | + //attempt to resize |
|
168 | + //return if successful |
|
169 | + //proceed if not |
|
170 | + $gif = self::run_animated_gif($load_filename, $save_filename); |
|
171 | + if ($gif) { |
|
172 | + return true; |
|
173 | + } |
|
174 | + } |
|
175 | + $image = wp_get_image_editor( $load_filename ); |
|
176 | + if ( !is_wp_error( $image ) ) { |
|
177 | + $crop = self::get_target_sizes( $load_filename ); |
|
178 | + $image->crop( $crop['x'], |
|
179 | + $crop['y'], |
|
180 | + $crop['src_w'], |
|
181 | + $crop['src_h'], |
|
182 | + $crop['target_w'], |
|
183 | + $crop['target_h'] |
|
184 | + ); |
|
185 | + $result = $image->save( $save_filename ); |
|
186 | + if ( is_wp_error( $result ) ) { |
|
187 | + // @codeCoverageIgnoreStart |
|
188 | + TimberHelper::error_log( 'Error resizing image' ); |
|
189 | + TimberHelper::error_log( $result ); |
|
190 | + return false; |
|
191 | + // @codeCoverageIgnoreEnd |
|
192 | + } else { |
|
193 | + return true; |
|
194 | + } |
|
195 | + } else if ( isset( $image->error_data['error_loading_image'] ) ) { |
|
196 | + // @codeCoverageIgnoreStart |
|
197 | + TimberHelper::error_log( 'Error loading ' . $image->error_data['error_loading_image'] ); |
|
198 | + } else { |
|
199 | + TimberHelper::error_log( $image ); |
|
200 | + // @codeCoverageIgnoreEnd |
|
201 | + } |
|
202 | + } |
|
203 | 203 | } |
@@ -10,98 +10,98 @@ |
||
10 | 10 | */ |
11 | 11 | class TimberImageOperationLetterbox extends TimberImageOperation { |
12 | 12 | |
13 | - private $w, $h, $color; |
|
13 | + private $w, $h, $color; |
|
14 | 14 | |
15 | - /** |
|
16 | - * @param int $w width of result image |
|
17 | - * @param int $h height |
|
18 | - * @param string $color hex string, for color of padding bands |
|
19 | - */ |
|
20 | - function __construct($w, $h, $color) { |
|
21 | - $this->w = $w; |
|
22 | - $this->h = $h; |
|
23 | - $this->color = $color; |
|
24 | - } |
|
15 | + /** |
|
16 | + * @param int $w width of result image |
|
17 | + * @param int $h height |
|
18 | + * @param string $color hex string, for color of padding bands |
|
19 | + */ |
|
20 | + function __construct($w, $h, $color) { |
|
21 | + $this->w = $w; |
|
22 | + $this->h = $h; |
|
23 | + $this->color = $color; |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
|
28 | - * @param string $src_extension the extension (ex: .jpg) |
|
29 | - * @return string the final filename to be used |
|
30 | - * (ex: my-awesome-pic-lbox-300x200-FF3366.jpg) |
|
31 | - */ |
|
32 | - public function filename($src_filename, $src_extension) { |
|
33 | - $color = str_replace( '#', '', $this->color ); |
|
34 | - $newbase = $src_filename . '-lbox-' . $this->w . 'x' . $this->h . '-' . $color; |
|
35 | - $new_name = $newbase . '.' . $src_extension; |
|
36 | - return $new_name; |
|
37 | - } |
|
26 | + /** |
|
27 | + * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
|
28 | + * @param string $src_extension the extension (ex: .jpg) |
|
29 | + * @return string the final filename to be used |
|
30 | + * (ex: my-awesome-pic-lbox-300x200-FF3366.jpg) |
|
31 | + */ |
|
32 | + public function filename($src_filename, $src_extension) { |
|
33 | + $color = str_replace( '#', '', $this->color ); |
|
34 | + $newbase = $src_filename . '-lbox-' . $this->w . 'x' . $this->h . '-' . $color; |
|
35 | + $new_name = $newbase . '.' . $src_extension; |
|
36 | + return $new_name; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Performs the actual image manipulation, |
|
41 | - * including saving the target file. |
|
42 | - * |
|
43 | - * @param string $load_filename filepath (not URL) to source file |
|
44 | - * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
|
45 | - * @param string $save_filename filepath (not URL) where result file should be saved |
|
46 | - * (ex: /src/var/www/wp-content/uploads/my-pic-lbox-300x200-FF3366.jpg) |
|
47 | - * @return bool true if everything went fine, false otherwise |
|
48 | - */ |
|
49 | - public function run($load_filename, $save_filename) { |
|
50 | - $w = $this->w; |
|
51 | - $h = $this->h; |
|
39 | + /** |
|
40 | + * Performs the actual image manipulation, |
|
41 | + * including saving the target file. |
|
42 | + * |
|
43 | + * @param string $load_filename filepath (not URL) to source file |
|
44 | + * (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
|
45 | + * @param string $save_filename filepath (not URL) where result file should be saved |
|
46 | + * (ex: /src/var/www/wp-content/uploads/my-pic-lbox-300x200-FF3366.jpg) |
|
47 | + * @return bool true if everything went fine, false otherwise |
|
48 | + */ |
|
49 | + public function run($load_filename, $save_filename) { |
|
50 | + $w = $this->w; |
|
51 | + $h = $this->h; |
|
52 | 52 | |
53 | - $bg = imagecreatetruecolor( $w, $h ); |
|
54 | - $c = self::hexrgb( $this->color ); |
|
55 | - $bgColor = imagecolorallocate( $bg, $c['red'], $c['green'], $c['blue'] ); |
|
56 | - imagefill( $bg, 0, 0, $bgColor ); |
|
57 | - $image = wp_get_image_editor( $load_filename ); |
|
58 | - if ( !is_wp_error( $image ) ) { |
|
59 | - $current_size = $image->get_size(); |
|
60 | - $quality = $image->get_quality(); |
|
61 | - $ow = $current_size['width']; |
|
62 | - $oh = $current_size['height']; |
|
63 | - $new_aspect = $w / $h; |
|
64 | - $old_aspect = $ow / $oh; |
|
65 | - if ( $new_aspect > $old_aspect ) { |
|
66 | - //taller than goal |
|
67 | - $h_scale = $h / $oh; |
|
68 | - $owt = $ow * $h_scale; |
|
69 | - $y = 0; |
|
70 | - $x = $w / 2 - $owt / 2; |
|
71 | - $oht = $h; |
|
72 | - $image->crop( 0, 0, $ow, $oh, $owt, $oht ); |
|
73 | - } else { |
|
74 | - $w_scale = $w / $ow; |
|
75 | - $oht = $oh * $w_scale; |
|
76 | - $x = 0; |
|
77 | - $y = $h / 2 - $oht / 2; |
|
78 | - $owt = $w; |
|
79 | - $image->crop( 0, 0, $ow, $oh, $owt, $oht ); |
|
80 | - } |
|
81 | - $result = $image->save( $save_filename ); |
|
82 | - $func = 'imagecreatefromjpeg'; |
|
83 | - $save_func = 'imagejpeg'; |
|
84 | - $ext = pathinfo( $save_filename, PATHINFO_EXTENSION ); |
|
85 | - if ( $ext == 'gif' ) { |
|
86 | - $func = 'imagecreatefromgif'; |
|
87 | - $save_func = 'imagegif'; |
|
88 | - } else if ( $ext == 'png' ) { |
|
89 | - $func = 'imagecreatefrompng'; |
|
90 | - $save_func = 'imagepng'; |
|
91 | - if ($quality > 9) { |
|
92 | - $quality = $quality/10; |
|
93 | - $quality = round(10 - $quality); |
|
94 | - } |
|
95 | - } |
|
96 | - $image = $func( $save_filename ); |
|
97 | - imagecopy( $bg, $image, $x, $y, 0, 0, $owt, $oht ); |
|
98 | - if ($save_func === 'imagegif') { |
|
99 | - return $save_func( $bg, $save_filename ); |
|
100 | - } |
|
101 | - return $save_func( $bg, $save_filename, $quality ); |
|
102 | - } else { |
|
103 | - TimberHelper::error_log( $image ); |
|
104 | - } |
|
105 | - return false; |
|
106 | - } |
|
53 | + $bg = imagecreatetruecolor( $w, $h ); |
|
54 | + $c = self::hexrgb( $this->color ); |
|
55 | + $bgColor = imagecolorallocate( $bg, $c['red'], $c['green'], $c['blue'] ); |
|
56 | + imagefill( $bg, 0, 0, $bgColor ); |
|
57 | + $image = wp_get_image_editor( $load_filename ); |
|
58 | + if ( !is_wp_error( $image ) ) { |
|
59 | + $current_size = $image->get_size(); |
|
60 | + $quality = $image->get_quality(); |
|
61 | + $ow = $current_size['width']; |
|
62 | + $oh = $current_size['height']; |
|
63 | + $new_aspect = $w / $h; |
|
64 | + $old_aspect = $ow / $oh; |
|
65 | + if ( $new_aspect > $old_aspect ) { |
|
66 | + //taller than goal |
|
67 | + $h_scale = $h / $oh; |
|
68 | + $owt = $ow * $h_scale; |
|
69 | + $y = 0; |
|
70 | + $x = $w / 2 - $owt / 2; |
|
71 | + $oht = $h; |
|
72 | + $image->crop( 0, 0, $ow, $oh, $owt, $oht ); |
|
73 | + } else { |
|
74 | + $w_scale = $w / $ow; |
|
75 | + $oht = $oh * $w_scale; |
|
76 | + $x = 0; |
|
77 | + $y = $h / 2 - $oht / 2; |
|
78 | + $owt = $w; |
|
79 | + $image->crop( 0, 0, $ow, $oh, $owt, $oht ); |
|
80 | + } |
|
81 | + $result = $image->save( $save_filename ); |
|
82 | + $func = 'imagecreatefromjpeg'; |
|
83 | + $save_func = 'imagejpeg'; |
|
84 | + $ext = pathinfo( $save_filename, PATHINFO_EXTENSION ); |
|
85 | + if ( $ext == 'gif' ) { |
|
86 | + $func = 'imagecreatefromgif'; |
|
87 | + $save_func = 'imagegif'; |
|
88 | + } else if ( $ext == 'png' ) { |
|
89 | + $func = 'imagecreatefrompng'; |
|
90 | + $save_func = 'imagepng'; |
|
91 | + if ($quality > 9) { |
|
92 | + $quality = $quality/10; |
|
93 | + $quality = round(10 - $quality); |
|
94 | + } |
|
95 | + } |
|
96 | + $image = $func( $save_filename ); |
|
97 | + imagecopy( $bg, $image, $x, $y, 0, 0, $owt, $oht ); |
|
98 | + if ($save_func === 'imagegif') { |
|
99 | + return $save_func( $bg, $save_filename ); |
|
100 | + } |
|
101 | + return $save_func( $bg, $save_filename, $quality ); |
|
102 | + } else { |
|
103 | + TimberHelper::error_log( $image ); |
|
104 | + } |
|
105 | + return false; |
|
106 | + } |
|
107 | 107 | } |
@@ -7,68 +7,68 @@ |
||
7 | 7 | */ |
8 | 8 | class TimberImageOperationToJpg extends TimberImageOperation { |
9 | 9 | |
10 | - private $color; |
|
10 | + private $color; |
|
11 | 11 | |
12 | - /** |
|
13 | - * @param string $color hex string of color to use for transparent zones |
|
14 | - */ |
|
15 | - function __construct($color) { |
|
16 | - $this->color = $color; |
|
17 | - } |
|
12 | + /** |
|
13 | + * @param string $color hex string of color to use for transparent zones |
|
14 | + */ |
|
15 | + function __construct($color) { |
|
16 | + $this->color = $color; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
|
21 | - * @param string $src_extension ignored |
|
22 | - * @return string the final filename to be used (ex: my-awesome-pic.jpg) |
|
23 | - */ |
|
24 | - function filename($src_filename, $src_extension = 'jpg') { |
|
25 | - $new_name = $src_filename . '.jpg'; |
|
26 | - return $new_name; |
|
27 | - } |
|
19 | + /** |
|
20 | + * @param string $src_filename the basename of the file (ex: my-awesome-pic) |
|
21 | + * @param string $src_extension ignored |
|
22 | + * @return string the final filename to be used (ex: my-awesome-pic.jpg) |
|
23 | + */ |
|
24 | + function filename($src_filename, $src_extension = 'jpg') { |
|
25 | + $new_name = $src_filename . '.jpg'; |
|
26 | + return $new_name; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * Performs the actual image manipulation, |
|
31 | - * including saving the target file. |
|
32 | - * |
|
33 | - * @param string $load_filename filepath (not URL) to source file (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
|
34 | - * @param string $save_filename filepath (not URL) where result file should be saved |
|
35 | - * (ex: /src/var/www/wp-content/uploads/my-pic.png) |
|
36 | - * @return bool true if everything went fine, false otherwise |
|
37 | - */ |
|
38 | - function run($load_filename, $save_filename) { |
|
39 | - $input = self::image_create( $load_filename ); |
|
40 | - list( $width, $height ) = getimagesize( $load_filename ); |
|
41 | - $output = imagecreatetruecolor( $width, $height ); |
|
42 | - $c = self::hexrgb( $this->color ); |
|
43 | - $color = imagecolorallocate( $output, $c['red'], $c['green'], $c['blue'] ); |
|
44 | - imagefilledrectangle( $output, 0, 0, $width, $height, $color ); |
|
45 | - imagecopy( $output, $input, 0, 0, 0, 0, $width, $height ); |
|
46 | - imagejpeg( $output, $save_filename ); |
|
47 | - return true; |
|
48 | - } |
|
29 | + /** |
|
30 | + * Performs the actual image manipulation, |
|
31 | + * including saving the target file. |
|
32 | + * |
|
33 | + * @param string $load_filename filepath (not URL) to source file (ex: /src/var/www/wp-content/uploads/my-pic.jpg) |
|
34 | + * @param string $save_filename filepath (not URL) where result file should be saved |
|
35 | + * (ex: /src/var/www/wp-content/uploads/my-pic.png) |
|
36 | + * @return bool true if everything went fine, false otherwise |
|
37 | + */ |
|
38 | + function run($load_filename, $save_filename) { |
|
39 | + $input = self::image_create( $load_filename ); |
|
40 | + list( $width, $height ) = getimagesize( $load_filename ); |
|
41 | + $output = imagecreatetruecolor( $width, $height ); |
|
42 | + $c = self::hexrgb( $this->color ); |
|
43 | + $color = imagecolorallocate( $output, $c['red'], $c['green'], $c['blue'] ); |
|
44 | + imagefilledrectangle( $output, 0, 0, $width, $height, $color ); |
|
45 | + imagecopy( $output, $input, 0, 0, 0, 0, $width, $height ); |
|
46 | + imagejpeg( $output, $save_filename ); |
|
47 | + return true; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * @param string $filename |
|
52 | - * @return resource an image identifier representing the image obtained from the given filename |
|
53 | - * will return the same data type regardless of whether the source is gif or png |
|
54 | - */ |
|
55 | - function image_create( $filename, $ext = 'auto' ) { |
|
56 | - if ( $ext == 'auto' ) { |
|
57 | - $ext = wp_check_filetype($filename); |
|
58 | - if (isset($ext['ext'])) { |
|
59 | - $ext = $ext['ext']; |
|
60 | - } |
|
61 | - } |
|
62 | - $ext = strtolower($ext); |
|
63 | - if ( $ext == 'gif' ) { |
|
64 | - return imagecreatefromgif($filename); |
|
65 | - } |
|
66 | - if ( $ext == 'png' ) { |
|
67 | - return imagecreatefrompng($filename); |
|
68 | - } |
|
69 | - if ( $ext == 'jpg' || $ext == 'jpeg' ) { |
|
70 | - return imagecreatefromjpeg($filename); |
|
71 | - } |
|
72 | - throw new InvalidArgumentException( 'image_create only accepts PNG, GIF and JPGs. File extension was: '.$ext ); |
|
73 | - } |
|
50 | + /** |
|
51 | + * @param string $filename |
|
52 | + * @return resource an image identifier representing the image obtained from the given filename |
|
53 | + * will return the same data type regardless of whether the source is gif or png |
|
54 | + */ |
|
55 | + function image_create( $filename, $ext = 'auto' ) { |
|
56 | + if ( $ext == 'auto' ) { |
|
57 | + $ext = wp_check_filetype($filename); |
|
58 | + if (isset($ext['ext'])) { |
|
59 | + $ext = $ext['ext']; |
|
60 | + } |
|
61 | + } |
|
62 | + $ext = strtolower($ext); |
|
63 | + if ( $ext == 'gif' ) { |
|
64 | + return imagecreatefromgif($filename); |
|
65 | + } |
|
66 | + if ( $ext == 'png' ) { |
|
67 | + return imagecreatefrompng($filename); |
|
68 | + } |
|
69 | + if ( $ext == 'jpg' || $ext == 'jpeg' ) { |
|
70 | + return imagecreatefromjpeg($filename); |
|
71 | + } |
|
72 | + throw new InvalidArgumentException( 'image_create only accepts PNG, GIF and JPGs. File extension was: '.$ext ); |
|
73 | + } |
|
74 | 74 | } |
@@ -1,49 +1,49 @@ |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Each image filter is represented by a subclass of this class,m |
|
4 | - * and each filter call is a new instance, with call arguments as properties. |
|
5 | - * |
|
6 | - * Only 3 methods need to be implemented: |
|
7 | - * - constructor, storing all filter arguments |
|
8 | - * - filename |
|
9 | - * - run |
|
10 | - */ |
|
3 | + * Each image filter is represented by a subclass of this class,m |
|
4 | + * and each filter call is a new instance, with call arguments as properties. |
|
5 | + * |
|
6 | + * Only 3 methods need to be implemented: |
|
7 | + * - constructor, storing all filter arguments |
|
8 | + * - filename |
|
9 | + * - run |
|
10 | + */ |
|
11 | 11 | abstract class TimberImageOperation { |
12 | - /** |
|
13 | - * Builds the result filename, based on source filename and extension |
|
14 | - * |
|
15 | - * @param string $src_filename source filename (excluding extension and path) |
|
16 | - * @param string $src_extension source file extension |
|
17 | - * @return string resulting filename (including extension but excluding path) |
|
18 | - * ex: my-awesome-file.jpg |
|
19 | - */ |
|
20 | - public abstract function filename($src_filename, $src_extension); |
|
12 | + /** |
|
13 | + * Builds the result filename, based on source filename and extension |
|
14 | + * |
|
15 | + * @param string $src_filename source filename (excluding extension and path) |
|
16 | + * @param string $src_extension source file extension |
|
17 | + * @return string resulting filename (including extension but excluding path) |
|
18 | + * ex: my-awesome-file.jpg |
|
19 | + */ |
|
20 | + public abstract function filename($src_filename, $src_extension); |
|
21 | 21 | |
22 | - /** |
|
23 | - * Performs the actual image manipulation, |
|
24 | - * including saving the target file. |
|
25 | - * |
|
26 | - * @param string $load_filename filepath (not URL) to source file |
|
27 | - * @param string $save_filename filepath (not URL) where result file should be saved |
|
28 | - * @return bool true if everything went fine, false otherwise |
|
29 | - */ |
|
30 | - public abstract function run($load_filename, $save_filename); |
|
22 | + /** |
|
23 | + * Performs the actual image manipulation, |
|
24 | + * including saving the target file. |
|
25 | + * |
|
26 | + * @param string $load_filename filepath (not URL) to source file |
|
27 | + * @param string $save_filename filepath (not URL) where result file should be saved |
|
28 | + * @return bool true if everything went fine, false otherwise |
|
29 | + */ |
|
30 | + public abstract function run($load_filename, $save_filename); |
|
31 | 31 | |
32 | - /** |
|
33 | - * Helper method to convert hex string to rgb array |
|
34 | - * |
|
35 | - * @param string $hexstr hex color string (like '#FF1455') |
|
36 | - * @return array array('red', 'green', 'blue') to int |
|
37 | - * ex: array('red' => 255, 'green' => 20, 'blue' => 85); |
|
38 | - */ |
|
39 | - public static function hexrgb( $hexstr ) { |
|
40 | - if ( !strstr( $hexstr, '#' ) ) { |
|
41 | - $hexstr = '#' . $hexstr; |
|
42 | - } |
|
43 | - if ( strlen( $hexstr ) == 4 ) { |
|
44 | - $hexstr = '#' . $hexstr[1] . $hexstr[1] . $hexstr[2] . $hexstr[2] . $hexstr[3] . $hexstr[3]; |
|
45 | - } |
|
46 | - $int = hexdec( $hexstr ); |
|
47 | - return array( "red" => 0xFF & ( $int >> 0x10 ), "green" => 0xFF & ( $int >> 0x8 ), "blue" => 0xFF & $int ); |
|
48 | - } |
|
32 | + /** |
|
33 | + * Helper method to convert hex string to rgb array |
|
34 | + * |
|
35 | + * @param string $hexstr hex color string (like '#FF1455') |
|
36 | + * @return array array('red', 'green', 'blue') to int |
|
37 | + * ex: array('red' => 255, 'green' => 20, 'blue' => 85); |
|
38 | + */ |
|
39 | + public static function hexrgb( $hexstr ) { |
|
40 | + if ( !strstr( $hexstr, '#' ) ) { |
|
41 | + $hexstr = '#' . $hexstr; |
|
42 | + } |
|
43 | + if ( strlen( $hexstr ) == 4 ) { |
|
44 | + $hexstr = '#' . $hexstr[1] . $hexstr[1] . $hexstr[2] . $hexstr[2] . $hexstr[3] . $hexstr[3]; |
|
45 | + } |
|
46 | + $int = hexdec( $hexstr ); |
|
47 | + return array( "red" => 0xFF & ( $int >> 0x10 ), "green" => 0xFF & ( $int >> 0x8 ), "blue" => 0xFF & $int ); |
|
48 | + } |
|
49 | 49 | } |
50 | 50 | \ No newline at end of file |