From 4ef0a0a769cd5273696f2b9309d1076a107f4bc3 Mon Sep 17 00:00:00 2001 From: Jon Gilkison Date: Wed, 30 Oct 2019 22:55:57 +0700 Subject: [PATCH] If $sizes have an imgix parameter defined, use them. Add a source() method to attachment to return a element. --- Classes/Core/UI.php | 8 +++++ Classes/Models/Attachment.php | 64 +++++++++++++++++++++++++---------- stem.php | 4 +-- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/Classes/Core/UI.php b/Classes/Core/UI.php index e7be964..3f1abc6 100644 --- a/Classes/Core/UI.php +++ b/Classes/Core/UI.php @@ -496,6 +496,14 @@ private function loadImageSizes() return $sizes; }); + + add_filter('media-cloud/dynamic-images/filter-parameters', function($parameters, $imageSize, $attachmentId, $attachmentMeta) use ($sizesConfig) { + if (isset($sizesConfig['sizes'][$imageSize]) && isset($sizesConfig['sizes'][$imageSize]['imgix'])) { + return array_merge($sizesConfig['sizes'][$imageSize]['imgix'], $parameters); + } + + return $parameters; + }, 1000, 4); } } diff --git a/Classes/Models/Attachment.php b/Classes/Models/Attachment.php index 5703055..7f2ccb9 100644 --- a/Classes/Models/Attachment.php +++ b/Classes/Models/Attachment.php @@ -399,25 +399,55 @@ public function ampImg($size = 'thumbnail', $responsive = true, $attr = null) { return $img; } - /** - * Returns the url for an image using the requested size template. - * - * @param string $size The size template to use. - * - * @return string|null - */ - public function src($size = 'original') { - if (empty($this->id)) { - return null; - } + /** + * Returns the url for an image using the requested size template. + * + * @param string $size The size template to use. + * + * @return string|null + */ + public function src($size = 'original') { + if (empty($this->id)) { + return null; + } - $result = wp_get_attachment_image_src($this->id, $size); - if ($result && is_array($result) && (count($result) > 0)) { - return $result[0]; - } + $result = wp_get_attachment_image_src($this->id, $size); + if ($result && is_array($result) && (count($result) > 0)) { + return $result[0]; + } - return null; - } + return null; + } + + /** + * Returns a tag + * + * @param string $size The size template to use. + * @param array $mediaQuery The media query to match this source + * + * @return string|null + */ + public function source($size = 'original', $mediaQuery = []) { + if (empty($this->id)) { + return null; + } + + $result = wp_get_attachment_image_src($this->id, $size); + if ($result && is_array($result) && (count($result) > 0)) { + $src = $result[0]; + + $queryEle = []; + foreach($mediaQuery as $query => $querySize) { + $queryEle[] = "$query: $querySize"; + } + + $query = implode(' and ', $queryEle); + + return ""; + } + + return null; + } //endregion diff --git a/stem.php b/stem.php index f4a4a41..f5f80b4 100644 --- a/stem.php +++ b/stem.php @@ -4,14 +4,14 @@ Plugin URI: https://github.com/jawngee/stem Description: Framework for building applications using Wordpress and Symfony Author: Jon Gilkison -Version: 0.7.8 +Version: 0.7.9 Author URI: http://interfacelab.com */ define('ILAB_STEM', __FILE__); define('ILAB_STEM_DIR', dirname(__FILE__)); define('ILAB_STEM_VIEW_DIR', ILAB_STEM_DIR.'/views'); -define('ILAB_STEM_VERSION', '0.7.7'); +define('ILAB_STEM_VERSION', '0.7.9'); if (file_exists(ILAB_STEM_DIR.'/vendor/autoload.php')) { require_once ILAB_STEM_DIR.'/vendor/autoload.php';