-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-wp-local-media-proxy.php
234 lines (193 loc) · 5.24 KB
/
class-wp-local-media-proxy.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Main plugin class.
*
* @package Ndigitals_Wp_Local_Media_Proxy_MuPlugin
*
* @link https://gist.github.com/kingkool68/d5e483528a260e5c7921afb5c88bffd6
*/
namespace Ndigitals\MuPlugin;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Wp_Local_Media_Proxy class.
* Defines plugin functionality.
*
* @package Ndigitals_Wp_Local_Media_Proxy_MuPlugin
*/
class Wp_Local_Media_Proxy {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.1.0
*
* @var string
*/
const VERSION = '1.1.1';
/**
* Instance of this class.
*
* @since 1.1.0
*
* @var Wp_Local_Media_Proxy
*/
protected static $_instance = null;
/**
* The home URL of the site to use for images/media. Default: ''.
*
* @since 1.1.0
*
* @var string
*/
protected $home_url = '';
/**
* The URL of the remote site to use for images/media. Default: ''.
*
* @since 1.1.0
*
* @var string
*/
protected $remote_medial_url = '';
/**
* The WordPress uploads directory path. Default: ''.
*
* @since 1.1.0
*
* @var string
*/
protected $wp_upload_base_dir = '';
/**
* The WordPress uploads directory URL. Default: ''.
*
* @since 1.1.0
*
* @var string
*/
protected $wp_upload_base_url = '';
/**
* Initialize the plugin by setting localization and loading public scripts,
* styles, filters, and hooks.
*
* @since 1.1.0
*/
private function __construct() {
// Make sure the home URL contains a trailing slash for consistent find/replace actions.
$this->home_url = trailingslashit( get_home_url() );
// Check for remote media URL and set the class property.
// Example Configuration "LOCALDEV_USE_REMOTE_MEDIA_URL=https://example.com".
if ( defined( 'LOCALDEV_USE_REMOTE_MEDIA_URL' ) && ! empty( constant( 'LOCALDEV_USE_REMOTE_MEDIA_URL' ) ) ) {
// Make sure the remote URL contains a trailing slash for consistent find/replace actions.
$this->remote_medial_url = trailingslashit( LOCALDEV_USE_REMOTE_MEDIA_URL );
}
// Store the WordPress uploads directory path & URL for image/media replacements.
$wp_upload_dir = wp_upload_dir();
$this->wp_upload_base_dir = $wp_upload_dir['basedir'];
$this->wp_upload_base_url = $wp_upload_dir['baseurl'];
// Register hooks.
$this->register_hooks();
}
/**
* Return an instance of this class.
*
* @since 1.1.0
*
* @return Wp_Local_Media_Proxy A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Register any required hooks for MU Plugin functionality.
*
* @since 1.1.0
*
* @return void
*/
private function register_hooks() {
// Check for remote media URL and add hooks to proxy media library images.
if ( ! empty( $this->remote_medial_url ) ) {
// Add filters only if constant is configured.
add_filter( 'wp_get_attachment_image_src', array( $this, 'filter_wp_get_attachment_image_src' ) );
add_filter( 'wp_calculate_image_srcset', array( $this, 'filter_wp_calculate_image_srcset' ) );
add_filter( 'wp_get_attachment_url', array( $this, 'filter_wp_get_attachment_url' ) );
}
}
/**
* Check for a local media file and if it doesn't exist replace the home URL with the remote URL.
*
* @since 1.1.0
*
* @param string $image_url The URL of the image/media file.
*
* @return string
*/
private function replace_url( $image_url ) {
$absolute_path = str_replace( $this->wp_upload_base_url, $this->wp_upload_base_dir, $image_url );
if ( file_exists( $absolute_path ) ) {
return $image_url;
}
$image_url = str_replace( $this->home_url, $this->remote_medial_url, $image_url );
return $image_url;
}
/**
* Proxy requests for images to a remote URL.
*
* @link https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_src/
*
* @since 1.1.0
*
* @param array|boolean $image Array of image data, or boolean false if no image is available.
*
* @return array|boolean
*/
public function filter_wp_get_attachment_image_src( $image = array() ) {
if ( ! is_array( $image ) || empty( $image ) ) {
return $image;
}
$image[0] = $this->replace_url( $image[0] );
return $image;
}
/**
* Proxy srcset requests to a remote URL.
*
* @link https://developer.wordpress.org/reference/hooks/wp_calculate_image_srcset/
*
* @since 1.1.0
*
* @param array $sources One or more arrays of source data to include in the 'srcset'.
*
* @return array
*/
public function filter_wp_calculate_image_srcset( $sources = array() ) {
if ( is_array( $sources ) && ! is_admin() ) {
foreach ( $sources as $key => $val ) {
$val['url'] = $this->replace_url( $val['url'] );
$sources[ $key ] = $val;
}
}
return $sources;
}
/**
* Proxy the attachement URL to a remote site.
*
* @link https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/
*
* @since 1.1.0
*
* @param string $url URL for the given attachment.
*
* @return string
*/
public function filter_wp_get_attachment_url( $url = '' ) {
if ( empty( $url ) ) {
return $url;
}
return $this->replace_url( $url );
}
}