Skip to content

Commit

Permalink
If $sizes have an imgix parameter defined, use them. Add a source() m…
Browse files Browse the repository at this point in the history
…ethod to attachment to return a <source> element.
  • Loading branch information
jawngee committed Oct 30, 2019
1 parent a590f7a commit 4ef0a0a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
8 changes: 8 additions & 0 deletions Classes/Core/UI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
64 changes: 47 additions & 17 deletions Classes/Models/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <source> 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 "<source srcset='{$src}' media='({$query})'>";
}

return null;
}

//endregion

Expand Down
4 changes: 2 additions & 2 deletions stem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit 4ef0a0a

Please sign in to comment.