|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Define a class that handle and upload all images from external sources. |
|
5
|
|
|
* |
|
6
|
|
|
* @since 3.18.0 |
|
7
|
|
|
* @package Wordlift |
|
8
|
|
|
*/ |
|
9
|
|
|
class Wordlift_Remote_Image_Service { |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Save the image with the specified URL locally. |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $url The image remote URL. |
|
15
|
|
|
* |
|
16
|
|
|
* @since 3.18.0 |
|
17
|
|
|
* |
|
18
|
|
|
* @return array|false An array with information about the saved image (*path*: the local path to the image, *url*: the local |
|
19
|
|
|
* url, *content_type*: the image content type) or false on error. |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function save_from_url( $url ) { |
|
22
|
|
|
|
|
23
|
|
|
// Load `WP_Filesystem`. |
|
24
|
|
|
WP_Filesystem(); |
|
25
|
|
|
global $wp_filesystem; |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
// Parse the url. |
|
28
|
|
|
$parts = wp_parse_url( $url ); |
|
29
|
|
|
|
|
30
|
|
|
// Get the bare filename (filename w/o the extension). |
|
31
|
|
|
$basename = pathinfo( $parts['path'], PATHINFO_FILENAME ); |
|
32
|
|
|
|
|
33
|
|
|
// Get the base dir. |
|
34
|
|
|
$wp_upload_dir = wp_upload_dir(); |
|
35
|
|
|
|
|
36
|
|
|
// Set the upload directory and URL. |
|
37
|
|
|
$upload_dir = $wp_upload_dir['basedir'] . '/wl' . $wp_upload_dir['subdir']; |
|
38
|
|
|
$upload_url = $wp_upload_dir['baseurl'] . '/wl' . $wp_upload_dir['subdir']; |
|
39
|
|
|
|
|
40
|
|
|
// Get the full path to the local filename. |
|
41
|
|
|
$image_full_path = $upload_dir . '/' . $basename; |
|
42
|
|
|
$image_full_url = $upload_url . '/' . $basename; |
|
43
|
|
|
|
|
44
|
|
|
// Create custom directory and bail on failure. |
|
45
|
|
|
if ( ! wp_mkdir_p( $upload_dir ) ) { |
|
46
|
|
|
wl_write_log( "save_image_from_url : failed creating upload dir $upload_dir \n" ); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
|
|
$response = self::get_response( $url ); |
|
52
|
|
|
|
|
53
|
|
|
// Bail if the response is not set. |
|
54
|
|
|
if ( false === $response ) { |
|
55
|
|
|
wl_write_log( "save_image_from_url : failed to fetch the response from: $url \n" ); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
return false; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Get the content type of response. |
|
61
|
|
|
$content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
|
62
|
|
|
|
|
63
|
|
|
// Get the file extension. |
|
64
|
|
|
$extension = self::get_extension_from_content_type( $content_type ); |
|
65
|
|
|
|
|
66
|
|
|
// Bail if the content type is not supported. |
|
67
|
|
|
if ( empty( $extension ) ) { |
|
68
|
|
|
return false; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Complete the local filename. |
|
72
|
|
|
$image_full_path .= $extension; |
|
73
|
|
|
$image_full_url .= $extension; |
|
74
|
|
|
|
|
75
|
|
|
// Store the data locally. |
|
76
|
|
|
$wp_filesystem->put_contents( $image_full_path, wp_remote_retrieve_body( $response ) ); |
|
77
|
|
|
|
|
78
|
|
|
// Return the path. |
|
79
|
|
|
return array( |
|
80
|
|
|
'path' => $image_full_path, |
|
81
|
|
|
'url' => $image_full_url, |
|
82
|
|
|
'content_type' => $content_type, |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the file extension using the content type. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $content_type File content type. |
|
90
|
|
|
* |
|
91
|
|
|
* @since 3.18.0 |
|
92
|
|
|
* |
|
93
|
|
|
* @return string|bool The file extension on success and |
|
94
|
|
|
* false on fail or if the content type is not supported. |
|
95
|
|
|
*/ |
|
96
|
|
|
private static function get_extension_from_content_type( $content_type ) { |
|
97
|
|
|
|
|
98
|
|
|
// Return the extension if match. |
|
99
|
|
|
switch ( $content_type ) { |
|
100
|
|
|
case 'image/jpeg': |
|
101
|
|
|
case 'image/jpg': |
|
102
|
|
|
return '.jpg'; |
|
103
|
|
|
case 'image/gif': |
|
104
|
|
|
return '.gif'; |
|
105
|
|
|
case 'image/png': |
|
106
|
|
|
return '.png'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
// Otherwise return false. |
|
110
|
|
|
return false; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Retrieve the response from url and sets the response. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $url The url to retrieve. |
|
117
|
|
|
* |
|
118
|
|
|
* @since 3.18.0 |
|
119
|
|
|
* |
|
120
|
|
|
* @return false|array True on success and false on failure. |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function get_response( $url ) { |
|
123
|
|
|
// Request the remote file. |
|
124
|
|
|
$response = wp_remote_get( $url ); |
|
125
|
|
|
|
|
126
|
|
|
// Bail out if the response is not ok. |
|
127
|
|
|
if ( |
|
128
|
|
|
is_wp_error( $response ) |
|
129
|
|
|
|| 200 !== (int) $response['response']['code'] |
|
130
|
|
|
|| ! isset( $response['body'] ) |
|
131
|
|
|
) { |
|
132
|
|
|
wl_write_log( "save_image_from_url : error fetching image $url \n" ); |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
// Set the response. |
|
138
|
|
|
return $response; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state