Created
May 30, 2023 20:29
-
-
Save ainsleyclark/5bad3bc4030ed17b7f3da46cf533d021 to your computer and use it in GitHub Desktop.
Alex - WP Imagery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Helpers | |
| * | |
| * A class for WordPress custom utility functions. | |
| * | |
| * @author Ainsley Clark | |
| * @class Helpers | |
| * @category Class | |
| */ | |
| namespace TwoEasy\Media; | |
| if (!defined('ABSPATH')) { | |
| exit; // Exit if accessed directly. | |
| } | |
| /** | |
| * Imagery defines the functions for obtaining imagery and | |
| * media from WordPress. | |
| */ | |
| final class Imagery { | |
| /** | |
| * Image renders a responsive image. | |
| * Takes in a class and a bool to indicate if the image | |
| * should be lazy loaded. <noscript> Tags will be | |
| * appended if lazy is set to true. | |
| * | |
| * @param $image | |
| * @param string $class | |
| * @param bool $lazy | |
| * @param string $maxSize | |
| */ | |
| public static function the_wp_image($image, string $class = '', bool $lazy = false, string $maxSize = ''): void | |
| { | |
| echo self::get_wp_image($image, $class, $lazy, $maxSize); | |
| } | |
| /** | |
| * Returns image instead of echoing. | |
| * | |
| * @param $image | |
| * @param string $class | |
| * @param bool $lazy | |
| * @param string $maxSize | |
| * @return string | |
| */ | |
| public static function get_wp_image($image, string $class = '', bool $lazy = false, string $maxSize = ''): string | |
| { | |
| if (empty($image)) { | |
| return ""; | |
| } | |
| if (!is_array($image)) { | |
| $image = acf_get_attachment($image); | |
| } | |
| if (!$image) { | |
| return ""; | |
| } | |
| $str = ''; | |
| $lazyStr = ''; | |
| $origClass = $class; | |
| if ($lazy) { | |
| $class = trim($class . ' lazy', ' '); | |
| $lazyStr = 'data-'; | |
| } | |
| if (!in_array('sizes', $image) || $image['mime_type'] === "image/svg+xml") { | |
| return '<img ' . $lazyStr . 'src="' . $image['url'] . '" class="' . $class . '" alt="' . $image['alt'] . '">'; | |
| } | |
| $sizes = $image['sizes']; | |
| if ($sizes['mobile']) { | |
| $str .= '<source media="(max-width: 767px)" ' . $lazyStr . 'srcset="' . $sizes['mobile'] . '" data-size="mobile">'; | |
| } | |
| if ($sizes['tablet']) { | |
| $str .= '<source media="(max-width: 1024px)" ' . $lazyStr . 'srcset="' . $sizes['tablet'] . '" data-size="tablet">'; | |
| } | |
| if ($sizes['desktop']) { | |
| $str .= '<source media="(max-width: 1408px)" ' . $lazyStr . 'srcset="' . $sizes['desktop'] . '" data-size="desktop">'; | |
| } | |
| if ($maxSize !== '') { | |
| $str .= '<img class="' . $class . '"' . $lazyStr . 'src="' . $sizes[$maxSize] . '" alt="' . $image['alt'] . '" data-size="' . $maxSize . '">'; | |
| } else if ($sizes['hd']) { | |
| $str .= '<img class="' . $class . '"' . $lazyStr . 'src="' . $sizes['hd'] . '" alt="' . $image['alt'] . '" data-size="hd">'; | |
| } else { | |
| $str .= '<img class="' . $class . '"' . $lazyStr . 'src="' .$image['url'] . '" alt="' . $image['alt'] . '" data-size="hd">'; | |
| } | |
| if ($lazy) { | |
| $str .= '<noscript><img class="' . $origClass . '" src="' . $image['url'] . '" alt="' . $image['alt'] . '"></noscript>'; | |
| } | |
| return $str; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment