1 | <?php |
||
3 | class Images_Via_Imgix { |
||
4 | |||
5 | /** |
||
6 | * The instance of the class. |
||
7 | * |
||
8 | * @var Images_Via_Imgix |
||
9 | */ |
||
10 | protected static $instance; |
||
11 | |||
12 | /** |
||
13 | * Plugin options |
||
14 | * |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $options = []; |
||
18 | |||
19 | /** |
||
20 | * Buffer is started by plugin and should be ended on shutdown. |
||
21 | * |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $buffer_started = false; |
||
25 | |||
26 | /** |
||
27 | * ImagesViaImgix constructor. |
||
28 | */ |
||
29 | public function __construct() { |
||
42 | |||
43 | /** |
||
44 | * Plugin loader instance. |
||
45 | * |
||
46 | * @return Images_Via_Imgix |
||
47 | */ |
||
48 | public static function instance() { |
||
55 | |||
56 | /** |
||
57 | * Override options from settings. |
||
58 | * Used in unit tests. |
||
59 | * |
||
60 | * @param array $options |
||
61 | */ |
||
62 | public function set_options( $options ) { |
||
65 | |||
66 | /** |
||
67 | * Find all img tags with sources matching "imgix.net" without the parameter |
||
68 | * "srcset" and add the "srcset" parameter to all those images, appending a new |
||
69 | * source using the "dpr=2" modifier. |
||
70 | * |
||
71 | * @param $content |
||
72 | * |
||
73 | * @return string Content with retina-enriched image tags. |
||
74 | */ |
||
75 | public function add_retina( $content ) { |
||
83 | |||
84 | /** |
||
85 | * Modify image urls for attachments to use imgix host. |
||
86 | * |
||
87 | * @param string $url |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | public function replace_image_url( $url ) { |
||
92 | if ( ! empty ( $this->options['cdn_link'] ) ) { |
||
93 | $pathinfo = pathinfo( $url ); |
||
94 | |||
95 | if ( in_array( $pathinfo['extension'], [ 'jpg', 'gif', 'png', 'jpeg' ] ) ) { |
||
96 | $parsed_url = parse_url( $url ); |
||
97 | if ( isset( $parsed_url['host'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) { |
||
98 | $cdn = parse_url( $this->options['cdn_link'] ); |
||
99 | foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) { |
||
100 | if ( isset( $cdn[ $url_part ] ) ) { |
||
101 | $parsed_url[ $url_part ] = $cdn[ $url_part ]; |
||
102 | } else { |
||
103 | unset( $parsed_url[ $url_part ] ); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | list( $filename, $arguments ) = $this->convert_filename_to_size_args( $pathinfo['basename'] ); |
||
108 | |||
109 | $arguments = array_merge( $arguments, $this->get_global_params() ); |
||
110 | |||
111 | $parsed_url['path'] = trailingslashit( dirname( $parsed_url['path'] ) ) . $filename; |
||
112 | |||
113 | if ( ! empty( $arguments ) ) { |
||
114 | $parsed_url['query'] = empty( $parsed_url['query'] ) ? build_query( $arguments ) : $parsed_url['query'] . '&' . build_query( $arguments ); |
||
115 | } |
||
116 | |||
117 | $url = http_build_url( $parsed_url ); |
||
|
|||
118 | } |
||
119 | } |
||
120 | } |
||
121 | |||
122 | return $url; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Modify image urls in srcset to use imgix host. |
||
127 | * |
||
128 | * @param array $sources |
||
129 | * |
||
130 | * @return array $sources |
||
131 | */ |
||
132 | public function replace_host_in_srcset( $sources ) { |
||
139 | |||
140 | /** |
||
141 | * Modify image urls in content to use imgix host. |
||
142 | * |
||
143 | * @param $content |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | public function replace_images_in_content( $content ) { |
||
157 | |||
158 | /** |
||
159 | * Add tag to dns prefetch cdn host |
||
160 | */ |
||
161 | public function prefetch_cdn() { |
||
171 | |||
172 | /** |
||
173 | * Start output buffer if auto retina is enabled |
||
174 | */ |
||
175 | public function buffer_start_for_retina() { |
||
181 | |||
182 | /** |
||
183 | * Stop output buffer if it was enabled by the plugin |
||
184 | */ |
||
185 | public function buffer_end_for_retina() { |
||
190 | |||
191 | /** |
||
192 | * Returns a array of global parameters to be applied in all images, |
||
193 | * according to plugin's settings. |
||
194 | * |
||
195 | * @return array Global parameters to be appened at the end of each img URL. |
||
196 | */ |
||
197 | protected function get_global_params() { |
||
216 | |||
217 | /** |
||
218 | * Convert sizes in filename to parameters and returns origina filename without sizes. |
||
219 | * If no size is found the original filename is returned. |
||
220 | * |
||
221 | * @param string $filename |
||
222 | * |
||
223 | * @return array with filename and size arguments. |
||
224 | */ |
||
225 | protected function convert_filename_to_size_args( $filename ) { |
||
239 | } |
||
240 | |||
242 |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.