View source
<?php
namespace Drupal\blazy;
use Drupal\blazy\Utility\NestedArray;
abstract class BlazyManagerBase implements BlazyManagerInterface {
private $isCrop;
private $cropStyles;
protected $optionsetOptions;
protected $isIoSettings;
public function config($key = '', $default = NULL, $id = 'blazy.settings', array $defaults = []) {
$config = variable_get($id, $defaults);
if (!$config) {
return $default;
}
if (!$key) {
return $config;
}
if (strpos($key, ".") !== FALSE) {
$parts = explode(".", $key);
$value = isset($config[$parts[0]][$parts[1]]) ? $config[$parts[0]][$parts[1]] : $default;
}
else {
$value = isset($config[$key]) ? $config[$key] : $default;
}
return $value;
}
public function typecast(array &$config, $id = 'blazy.settings') {
}
public function getCacheId(array $settings = []) {
$keys = isset($settings['cache_metadata']['keys']) ? $settings['cache_metadata']['keys'] : [
$settings['id'],
];
$keys[] = count(array_filter($settings));
return implode(':', $keys);
}
public function attach(array $attach) {
$load = [];
$lazy = !empty($attach['blazy']) || isset($attach['lazy']) && $attach['lazy'] == 'blazy';
if (!empty($attach['style'])) {
foreach ([
'column',
'grid',
] as $grid) {
$attach[$grid] = $attach['style'];
}
}
if (!empty($attach['fx']) && $attach['fx'] == 'blur') {
$load['library'][] = [
'blazy',
'fx.blur',
];
}
foreach ([
'column',
'filter',
'grid',
'media',
'ratio',
] as $component) {
if (!empty($attach[$component])) {
$load['library'][] = [
'blazy',
$component,
];
}
}
foreach ([
'lightbox',
'colorbox',
'photobox',
] as $box) {
if (!empty($attach[$box])) {
$load['library'][] = [
'blazy',
$box,
];
}
}
if (!empty($attach['resimage'])) {
$load['library'][] = [
'picture',
'picturefill_head',
];
$load['library'][] = [
'picture',
'picturefill',
];
$load['library'][] = [
'picture',
'picture.ajax',
];
if ($lazy) {
$load['library'][] = [
'picture',
'lazysizes',
];
$load['library'][] = [
'picture',
'lazysizes_aspect_ratio',
];
}
}
$load['js'][] = [
'data' => [
'blazy' => $this
->config('blazy', BlazyDefault::formSettings()['blazy']),
'blazyIo' => $this
->getIoSettings($attach),
],
'type' => 'setting',
];
$load['library'][] = [
'blazy',
'load',
];
if (!empty($attach['use_ajax'])) {
$load['library'][] = [
'blazy',
'bio.ajax',
];
}
drupal_alter('blazy_attach', $load, $attach);
return $load;
}
public function getIoSettings(array $attach = []) {
if (!isset($this->isIoSettings)) {
$thold = trim($this
->config('io.threshold', '0', 'blazy.settings'));
$number = strpos($thold, '.') !== FALSE ? (double) $thold : (int) $thold;
$thold = strpos($thold, ',') !== FALSE ? array_map('trim', explode(',', $thold)) : [
$number,
];
foreach ([
'enabled',
'disconnect',
'rootMargin',
'threshold',
] as $key) {
$default = $key == 'rootMargin' ? '0px' : FALSE;
$value = $key == 'threshold' ? $thold : $this
->config('io.' . $key, $default);
$io[$key] = isset($attach['io.' . $key]) ? $attach['io.' . $key] : $value;
}
$this->isIoSettings = (object) $io;
}
return $this->isIoSettings;
}
public function getCommonSettings() {
return array_intersect_key(variable_get('blazy.settings'), BlazyDefault::uiSettings());
}
public function cropStyles() {
if (!isset($this->cropStyles)) {
$this->cropStyles = [];
foreach (image_styles() as $style) {
foreach ($style['effects'] as $effect) {
if (strpos($effect['name'], 'crop') !== FALSE) {
$this->cropStyles[$style['name']] = $style['name'];
break;
}
}
}
}
return $this->cropStyles;
}
public function isCrop($style) {
if (!isset($this->isCrop[$style])) {
$this->isCrop[$style] = $this
->cropStyles() && isset($this
->cropStyles()[$style]);
}
return $this->isCrop[$style];
}
public function getLightboxes() {
$lightboxes = [];
foreach ([
'colorbox',
'photobox',
] as $lightbox) {
if (function_exists($lightbox . '_theme')) {
$lightboxes[] = $lightbox;
}
}
if (is_file(libraries_get_path('photobox') . '/photobox/jquery.photobox.js')) {
$lightboxes[] = 'photobox';
}
drupal_alter('blazy_lightboxes', $lightboxes);
return array_unique($lightboxes);
}
public function getImageEffects() {
$effects[] = 'blur';
drupal_alter('blazy_image_effects', $effects);
$effects = array_unique($effects);
return array_combine($effects, $effects);
}
public function isBlazy(array &$settings, array $item = []) {
$image = isset($item['item']) ? $item['item'] : NULL;
$settings['first_item'] = $image;
if (isset($item['settings'])) {
$blazy = $item['settings'];
if (empty($settings['breakpoints']) && isset($blazy['breakpoints'])) {
$settings['breakpoints'] = $blazy['breakpoints'];
}
$cherries = BlazyDefault::cherrySettings() + [
'uri' => '',
];
foreach ($cherries as $key => $value) {
$fallback = isset($settings[$key]) ? $settings[$key] : $value;
$settings[$key] = isset($blazy[$key]) && empty($fallback) ? $blazy[$key] : $fallback;
}
$uri = empty($settings['first_uri']) ? $settings['uri'] : $settings['first_uri'];
$settings['_uri'] = $settings['first_uri'] = empty($settings['_uri']) ? $uri : $settings['_uri'];
unset($settings['uri']);
}
$switch = empty($settings['media_switch']) ? FALSE : $settings['media_switch'];
if ($switch) {
$settings[$switch] = empty($settings[$switch]) ? $switch : $settings[$switch];
}
$this
->cleanUpBreakpoints($settings);
if (!empty($settings['breakpoints'])) {
$this
->buildDataBlazy($settings, $image);
}
unset($settings['first_image']);
}
public function getOptionsetOptions(array $entities = []) {
if (!isset($this->optionsetOptions)) {
foreach ($entities as $key => $optionset) {
$this->optionsetOptions[$key] = check_plain($optionset->label);
}
asort($this->optionsetOptions);
}
return $this->optionsetOptions;
}
public function buildSkins($namespace, $skin_class, $methods = []) {
$cid = $namespace . ':skins';
if ($cache = cache_get($cid)) {
return $cache->data;
}
$classes = module_invoke_all($namespace . '_skins_info');
$classes = array_merge([
$skin_class,
], (array) $classes);
$items = $skins = [];
foreach ($classes as $class) {
if (class_exists($class)) {
$reflection = new \ReflectionClass($class);
if ($reflection
->implementsInterface($skin_class . 'Interface')) {
$skin = new $class();
if (empty($methods) && method_exists($skin, 'skins')) {
$items = $skin
->skins();
}
else {
foreach ($methods as $method) {
$items[$method] = method_exists($skin, $method) ? $skin
->{$method}() : [];
}
}
}
}
$skins = NestedArray::mergeDeep($skins, $items);
}
cache_set($cid, $skins, 'cache', CACHE_PERMANENT);
return $skins;
}
public function cleanUpBreakpoints(array &$settings) {
if (!empty($settings['breakpoints'])) {
$breakpoints = array_filter(array_map('array_filter', $settings['breakpoints']));
$settings['breakpoints'] = NestedArray::filter($breakpoints, function ($breakpoint) {
return !(is_array($breakpoint) && (empty($breakpoint['width']) || empty($breakpoint['image_style'])));
});
}
}
public function buildDataBlazy(array &$settings, $item = NULL) {
$settings['blazy'] = TRUE;
if (!empty($settings['blazy_data'])) {
return;
}
Blazy::imageDimensions($settings, $item, TRUE);
$styles = [];
$end = end($settings['breakpoints']);
foreach ($settings['breakpoints'] as $key => $breakpoint) {
if ($this
->isCrop($breakpoint['image_style'])) {
$styles[$key] = TRUE;
}
}
if (count($styles) != count($settings['breakpoints'])) {
return;
}
$json = $sources = [];
foreach ($settings['breakpoints'] as $key => $breakpoint) {
if ($width = Blazy::widthFromDescriptors($breakpoint['width'])) {
if (!empty($settings['ratio'])) {
$dim = Blazy::transformDimensions($breakpoint['image_style'], $item);
$padding = round($dim['height'] / $dim['width'] * 100, 2);
$json['dimensions'][$width] = $padding;
if ($end['width'] == $breakpoint['width']) {
$settings['padding_bottom'] = $padding;
}
}
}
}
if ($sources) {
$json['breakpoints'] = $sources;
}
if ($json) {
$settings['blazy_data'] = $json;
}
}
}