-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplace-uploads-url.php
executable file
·186 lines (143 loc) · 4.68 KB
/
replace-uploads-url.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
<?php
/*
* Plugin Name: Replace Uploads URL
* Plugin URI: https://github.com/victorfreitas/replace-uploads-url
* Description: Replace development environment to production uploads url
* Author: Victor Freitas
* Version: 2.1
*/
defined( 'ABSPATH' ) || exit;
if ( ! defined( 'RUU_PRODUCTION_URL' ) ) {
define( 'RUU_PRODUCTION_URL', '' );
}
class Replace_Uploads_Url {
private static $instance = null;
public $production_url = RUU_PRODUCTION_URL;
public $option_name = 'ruu_production_url';
public $field_section = 'ruu_field_section';
public $title = 'Replace Uploads URL';
public $extensions = [ 'jpe?g', 'gif', 'png', 'svg', 'webp', 'mp4', 'pdf', 'docx?' ];
private function __construct() {
if ( $this->production_url_option() ) {
$this->init_actions();
}
}
public function production_url_option() {
if ( ! empty( $this->production_url ) ) {
return true;
}
$this->set_production_url();
add_action( 'admin_init', array( $this, 'register_option' ) );
return ! empty( $this->production_url );
}
public function init_actions() {
if ( is_admin() ) {
$this->admin_actions();
return;
}
add_action( 'wp_loaded', array( $this, 'begin' ) );
}
public function admin_actions() {
add_action( 'wp_get_attachment_url', array( $this, 'parse_thumb_url' ) );
add_action( 'wp_calculate_image_srcset', array( $this, 'image_srcset' ), 10, 5 );
}
public function begin() {
ob_start( array( $this, 'replace' ) );
}
public function get_domain_host() {
return parse_url(get_site_url(), PHP_URL_HOST);
}
public function get_extensions() {
return implode( '|', $this->extensions );
}
public function get_pattern() {
return sprintf(
'#(https?:\/\/)%s[/|.|\w|-]*\.(%s)#',
$this->get_domain_host(),
$this->get_extensions()
);
}
public function replace( $content ) {
preg_match_all( $this->get_pattern(), $content, $images_match );
if ( empty( $images_match[0] ) ) {
return $content;
}
$images = array_unique( $images_match[0] );
foreach ( $images as $image_url ) {
$new_image_url = $this->parse_thumb_url( $image_url );
if ( $new_image_url ) {
$content = str_replace( $image_url, $new_image_url, $content );
}
}
return $content;
}
public function set_production_url() {
$this->production_url = untrailingslashit( $this->get_option() );
}
public function indexof( $value, $search ) {
return ( false !== strpos( $value, $search ) );
}
public function get_relative_uri( $url ) {
return preg_replace( '/(https?:\/\/(localhost\/|).+?\/)/', '', $url );
}
public function get_uri( $url ) {
$uri = $this->get_relative_uri( $url );
return file_exists( ABSPATH . $uri ) ? false : $uri;
}
public function parse_thumb_url( $url ) {
if ( ! $uri = $this->get_uri( $url ) ) {
return $url;
}
return sprintf( '%s/%s', $this->production_url, $uri );
}
public function image_srcset( $sources ) {
foreach ( $sources as $key => $source ) :
$source['url'] = $this->parse_thumb_url( $source['url'] );
$sources[ $key ] = $source;
endforeach;
return $sources;
}
public function register_option() {
register_setting(
'general',
$this->option_name,
'esc_url'
);
add_settings_section(
$this->field_section,
$this->title,
'__return_false',
'general'
);
add_settings_field(
$this->option_name,
'Production URL',
array( $this, 'render_field' ),
'general',
$this->field_section
);
}
public function render_field() {
printf(
'<input
type="url"
name="%s"
class="regular-text"
value="%s"
placeholder="Enter your production URL"
>',
$this->option_name,
$this->get_option()
);
}
public function get_option() {
return esc_url( get_option( $this->option_name ) );
}
public static function instance() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
}
add_action( 'plugins_loaded', array( 'Replace_Uploads_Url', 'instance' ), -1 );