Completed
Push — master ( 0c53d4...debbec )
by David
09:08 queued 10s
created

Wordlift_Remote_Image_Service   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 133
rs 10
c 0
b 0
f 0
wmc 13
lcom 0
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
B save_from_url() 0 64 4
B get_extension_from_content_type() 0 16 5
A get_response() 0 18 4
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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" );
0 ignored issues
show
Deprecated Code introduced by
The function wl_write_log() has been deprecated with message: use Wordlift_Log_Service::get_instance()->info( $log );

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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" );
0 ignored issues
show
Deprecated Code introduced by
The function wl_write_log() has been deprecated with message: use Wordlift_Log_Service::get_instance()->info( $log );

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
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" );
0 ignored issues
show
Deprecated Code introduced by
The function wl_write_log() has been deprecated with message: use Wordlift_Log_Service::get_instance()->info( $log );

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
133
134
			return false;
135
		}
136
137
		// Set the response.
138
		return $response;
139
	}
140
141
}
142