forked from humanmade/tachyon-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tachyon.php
71 lines (54 loc) · 1.96 KB
/
tachyon.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
<?php
/**
* Plugin Name: Tachyon
* Version: 0.9
* Description: A standalone tachyon proof of concept
* Author: Joe Hoyle | Human Made | Automattic Inc
*/
/**
* Copyright: Automattic Inc
* Copyright: Human Made Limited
*/
if ( ! defined( 'TACHYON_URL' ) || ! TACHYON_URL ) {
return;
}
require_once( dirname( __FILE__ ) . '/inc/class-tachyon.php' );
Tachyon::instance();
/**
* Generates a Tachyon URL.
*
* @see http://developer.wordpress.com/docs/tachyon/
*
* @param string $image_url URL to the publicly accessible image you want to manipulate
* @param array|string $args An array of arguments, i.e. array( 'w' => '300', 'resize' => array( 123, 456 ) ), or in string form (w=123&h=456)
* @return string The raw final URL. You should run this through esc_url() before displaying it.
*/
function tachyon_url( $image_url, $args = array(), $scheme = null ) {
$upload_dir = wp_upload_dir();
$upload_baseurl = $upload_dir['baseurl'];
if ( is_multisite() ) {
$upload_baseurl = preg_replace( '#/sites/[\d]+#', '', $upload_baseurl );
}
$image_url = trim( $image_url );
$image_file = basename( $image_url );
$image_url = str_replace( $image_file, urlencode( $image_file ), $image_url );
if ( strpos( $image_url, $upload_dir['baseurl'] ) !== 0 ) {
return $image_url;
}
if ( false !== apply_filters( 'tachyon_skip_for_url', false, $image_url, $args, $scheme ) ) {
return $image_url;
}
$image_url = apply_filters( 'tachyon_pre_image_url', $image_url, $args, $scheme );
$args = apply_filters( 'tachyon_pre_args', $args, $image_url, $scheme );
$tachyon_url = str_replace( $upload_baseurl, TACHYON_URL, $image_url );
if ( $args ) {
if ( is_array( $args ) ) {
$tachyon_url = add_query_arg( $args, $tachyon_url );
} else {
// You can pass a query string for complicated requests but where you still want CDN subdomain help, etc.
$tachyon_url .= '?' . $args;
}
}
return $tachyon_url;
}
add_filter( 'tachyon_url', 'tachyon_url', 10, 3 );