|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * The Image helper |
| 4 | + */ |
| 5 | + |
| 6 | +namespace DustPress; |
| 7 | + |
| 8 | +/** |
| 9 | + * This helper provides a functionality to print |
| 10 | + * an image element with its srcset. The helper can either get |
| 11 | + * the image URLs from WP with a given ID or print the element |
| 12 | + * with custom source URLs. |
| 13 | + */ |
| 14 | +class Image extends Helper { |
| 15 | + |
| 16 | + /** |
| 17 | + * Outputs the image markup or an error |
| 18 | + * |
| 19 | + * @return string The HTML markup or an error message. |
| 20 | + */ |
| 21 | + public function output() { |
| 22 | + |
| 23 | + // Store the parameters. |
| 24 | + $image_data = $this->get_image_data( $this->params ); |
| 25 | + |
| 26 | + // ID given |
| 27 | + if ( null !== $image_data['id'] ) { |
| 28 | + |
| 29 | + // SRC also given. |
| 30 | + if ( null !== $image_data['src'] ) { |
| 31 | + |
| 32 | + return '<p><strong>Dustpress image helper error:</strong> |
| 33 | + <em>Image id and custom src both given. |
| 34 | + Only one of these parameters can be used.</em></p>'; |
| 35 | + |
| 36 | + } else { // Only the ID given as the original image source. |
| 37 | + |
| 38 | + // No custom responsive parameters given |
| 39 | + if ( null === $image_data['srcset'] && null === $image_data['sizes'] ) { |
| 40 | + |
| 41 | + // Return the WordPress default img-tag |
| 42 | + // from the full-sized image with a source set. |
| 43 | + $the_image_markup = wp_get_attachment_image( |
| 44 | + $image_data['id'], |
| 45 | + 'full', |
| 46 | + false, |
| 47 | + $image_data['attrs'] |
| 48 | + ); |
| 49 | + |
| 50 | + if ( $the_image_markup ) { |
| 51 | + |
| 52 | + return $the_image_markup; |
| 53 | + |
| 54 | + } else { |
| 55 | + |
| 56 | + return '<p><strong>Dustpress image helper error:</strong> |
| 57 | + <em>No image found from the database with the given id.</em></p>'; |
| 58 | + |
| 59 | + } |
| 60 | + } else { // Custom responsive parameters are given. |
| 61 | + |
| 62 | + // SRCSET exists but no SIZES attribute is given. |
| 63 | + if ( null === $image_data['sizes'] ) { |
| 64 | + |
| 65 | + return '<p><strong>Dustpress image helper error:</strong> |
| 66 | + <em>Srcset exists but no sizes attribute is given.</em></p>'; |
| 67 | + |
| 68 | + } else { // Both custom responsive parameters and the id is given. |
| 69 | + |
| 70 | + return $this->get_image_markup( $image_data ); |
| 71 | + |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } else { // No image ID given |
| 76 | + |
| 77 | + // No SRC given either. |
| 78 | + if ( null === $image_data['src'] ) { |
| 79 | + |
| 80 | + return '<p><strong>Dustpress image helper error:</strong> |
| 81 | + <em>Image id or custom src not given. |
| 82 | + The helper needs at least one of these parameters.</em></p>'; |
| 83 | + |
| 84 | + } else { // Only the SRC given as the original image source. |
| 85 | + |
| 86 | + // When using the custom SRC, both SRCSET and SIZES need to be given. |
| 87 | + if ( null === $image_data['srcset'] ) { |
| 88 | + |
| 89 | + return '<p><strong>Dustpress image helper error:</strong> |
| 90 | + <em>Srcset not given. Both the srcset and the sizes are |
| 91 | + needed when using a custom src.</em></p>'; |
| 92 | + } |
| 93 | + |
| 94 | + // When using the custom SRC, both SRCSET and SIZES need to be given. |
| 95 | + if ( null === $image_data['sizes'] ) { |
| 96 | + |
| 97 | + return '<p><strong>Dustpress image helper error:</strong> |
| 98 | + <em>Sizes not given. Both the srcset and the sizes are |
| 99 | + needed when using a custom src.</em></p>'; |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + return $this->get_image_markup( $image_data ); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Gets and formats the data from the parameters given |
| 110 | + * to the image helper tag. |
| 111 | + * |
| 112 | + * @param array $params The array object. |
| 113 | + * |
| 114 | + * @return array $image_data The formatted array. |
| 115 | + */ |
| 116 | + private function get_image_data( $params ) { |
| 117 | + |
| 118 | + // Init the settings array and the img attrs array. |
| 119 | + $image_data = [ |
| 120 | + 'id' => null, |
| 121 | + 'src' => null, |
| 122 | + 'srcset' => null, |
| 123 | + 'sizes' => null, |
| 124 | + 'attrs' => [], |
| 125 | + ]; |
| 126 | + |
| 127 | + // Store the images ID if it is given. |
| 128 | + if ( isset( $params->id ) ) { |
| 129 | + $image_data['id'] = (int) $params->id; |
| 130 | + } |
| 131 | + |
| 132 | + // Add the src attribute to the data array if it is given. |
| 133 | + if ( isset( $params->src ) ) { |
| 134 | + $image_data['src'] = $params->src; |
| 135 | + } |
| 136 | + |
| 137 | + // Add the srcset attribute to the data array if it is given. |
| 138 | + if ( isset( $params->srcset ) ) { |
| 139 | + $image_data['srcset'] = $params->srcset; |
| 140 | + } |
| 141 | + |
| 142 | + // Add the sizes attribute to the data array if it is given. |
| 143 | + if ( isset( $params->sizes ) ) { |
| 144 | + $image_data['sizes'] = $params->sizes; |
| 145 | + } |
| 146 | + |
| 147 | + // If a class string is given, store it to the meta params. |
| 148 | + if ( isset( $params->class ) ) { |
| 149 | + $image_data['attrs']['class'] = $params->class; |
| 150 | + } |
| 151 | + |
| 152 | + // If an alt string is given, store it to the meta params. |
| 153 | + if ( isset( $params->alt ) ) { |
| 154 | + $image_data['attrs']['alt'] = $params->alt; |
| 155 | + } |
| 156 | + |
| 157 | + return $image_data; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Get the custom HTML srcset markup with the given settings |
| 162 | + * |
| 163 | + * @param array $image_data The given srcset and sizes. |
| 164 | + * |
| 165 | + * @return string The image markup. |
| 166 | + */ |
| 167 | + private function get_image_markup( $image_data ) { |
| 168 | + |
| 169 | + // If the src attribute is given, use it as the original src. |
| 170 | + if ( null !== $image_data['src'] ) { |
| 171 | + |
| 172 | + $image_src_string = '<img src="' . $image_data['src'] . '"'; |
| 173 | + |
| 174 | + } else { // Else get the images src from WP. |
| 175 | + |
| 176 | + // Construct the beginning markup of the image string if the image is found. |
| 177 | + if ( $image_src_array = wp_get_attachment_image_src( $image_data['id'], 'full' ) ) { |
| 178 | + |
| 179 | + $image_src = $image_src_array[0]; |
| 180 | + $image_width = $image_src_array[1]; |
| 181 | + $image_height = $image_src_array[2]; |
| 182 | + $image_src_string = '<img width="' . $image_width . |
| 183 | + '" height="' . $image_height . |
| 184 | + '" src="' . $image_src . '"'; |
| 185 | + |
| 186 | + } else { // Else return an error. |
| 187 | + |
| 188 | + return '<p><strong>Dustpress image helper error:</strong> |
| 189 | + <em>No image found from the database with the given id.</em></p>'; |
| 190 | + |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + // Set the class string. |
| 195 | + $image_class_string = ( isset( $image_data['attrs']['class'] ) |
| 196 | + ? 'class="'. $image_data['attrs']['class'] .'"' |
| 197 | + : '' |
| 198 | + ); |
| 199 | + |
| 200 | + // Set the alt string. |
| 201 | + $image_alt_string = ( isset( $image_data['attrs']['alt'] ) |
| 202 | + ? 'alt="'. $image_data['attrs']['alt'] .'"' |
| 203 | + : '' |
| 204 | + ); |
| 205 | + |
| 206 | + // Set the sizes attribute string. |
| 207 | + $sizes = $image_data['sizes']; |
| 208 | + |
| 209 | + // Check that the srcset is given as an array. |
| 210 | + if ( ! is_array( $sizes ) ) { |
| 211 | + |
| 212 | + return '<p><strong>Dustpress image helper error:</strong> |
| 213 | + <em>Given sizes attribute is not an array.</em></p>'; |
| 214 | + |
| 215 | + } |
| 216 | + |
| 217 | + // Concatenate the given sizes to a comma separated list |
| 218 | + // and construct the sizes string. |
| 219 | + $image_sizes_string = 'sizes="' . implode( ', ', $sizes ) .'"'; |
| 220 | + |
| 221 | + // Either use the srcset array that is given |
| 222 | + // or fetch the urls and widths using the WP sizes. |
| 223 | + $srcset_array = ( isset( $image_data['srcset'] ) |
| 224 | + ? $image_data['srcset'] |
| 225 | + : $this->get_wp_image_sizes_array( $image_data['id'] ) |
| 226 | + ); |
| 227 | + |
| 228 | + // Check that the srcset is given as an array. |
| 229 | + if ( ! is_array( $srcset_array ) ) { |
| 230 | + |
| 231 | + return '<p><strong>Dustpress image helper error:</strong> |
| 232 | + <em>Given srcset attribute is not an array.</em></p>'; |
| 233 | + |
| 234 | + } |
| 235 | + |
| 236 | + // Construct the srcset string. |
| 237 | + $image_srcset_string = 'srcset="' . implode( ', ', $srcset_array ) .'"'; |
| 238 | + |
| 239 | + // Close the img tag. |
| 240 | + $image_close_string = '>'; |
| 241 | + |
| 242 | + // Concatenate all of the images strings together. |
| 243 | + $html = $image_src_string . |
| 244 | + $image_alt_string . |
| 245 | + $image_class_string . |
| 246 | + $image_srcset_string . |
| 247 | + $image_sizes_string . |
| 248 | + $image_close_string; |
| 249 | + |
| 250 | + return $html; |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Get all the registered image sizes along with their dimensions |
| 255 | + * |
| 256 | + * @global array $_wp_additional_image_sizes |
| 257 | + * @param int $id The image ID. |
| 258 | + * |
| 259 | + * @return array $image_sizes The image sizes |
| 260 | + */ |
| 261 | + private function get_wp_image_sizes_array( $id ) { |
| 262 | + |
| 263 | + // The registered image sizes. |
| 264 | + global $_wp_additional_image_sizes; |
| 265 | + |
| 266 | + // The default wordpress image sizes. Exclude the thumbnail size. |
| 267 | + $default_image_sizes = array( 'medium', 'medium_large', 'large' ); |
| 268 | + |
| 269 | + // Loop through the sizes and get the corresponding options from the db. |
| 270 | + foreach ( $default_image_sizes as $size ) { |
| 271 | + |
| 272 | + $image_sizes[ $size ]['width'] = intval( get_option( "{$size}_size_w" ) ); |
| 273 | + $image_sizes[ $size ]['height'] = intval( get_option( "{$size}_size_h" ) ); |
| 274 | + $image_sizes[ $size ]['crop'] = ( get_option( "{$size}_crop" ) |
| 275 | + ? get_option( "{$size}_crop" ) |
| 276 | + : false |
| 277 | + ); |
| 278 | + } |
| 279 | + |
| 280 | + // Add custom sizes to the array. |
| 281 | + if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) { |
| 282 | + |
| 283 | + $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes ); |
| 284 | + |
| 285 | + } |
| 286 | + |
| 287 | + // The final array in which we have the properly formatted urls and widths. |
| 288 | + $srcset_array = []; |
| 289 | + |
| 290 | + // Loop through the sizes in the array and get the urls and widths from WP. |
| 291 | + foreach ( $image_sizes as $size => $size_options ) { |
| 292 | + |
| 293 | + $url = wp_get_attachment_image_src( $id, $size )[0]; |
| 294 | + $width = $size_options['width']; |
| 295 | + $entry = $url . ' ' . $width . 'w'; |
| 296 | + $srcset_array[] = $entry; |
| 297 | + |
| 298 | + } |
| 299 | + |
| 300 | + return $srcset_array; |
| 301 | + } |
| 302 | +} |
| 303 | +// Add the helper. |
| 304 | +$this->add_helper( 'image', new Image() ); |
0 commit comments