Skip to content

Instantly share code, notes, and snippets.

@alexandreelise
Created October 2, 2022 19:21
Show Gist options
  • Save alexandreelise/880aef00d50cbfc8cb7f45dc73102d2e to your computer and use it in GitHub Desktop.
Save alexandreelise/880aef00d50cbfc8cb7f45dc73102d2e to your computer and use it in GitHub Desktop.
Quick fix on line 31 to prevent error when using it with Joomla! Webservices Api
<?php
/**
* @package ttc-freebies.plugin-responsive-images
*
* @copyright Copyright (C) 2017 Dimitrios Grammatikogiannis. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') || die;
use Joomla\CMS\Plugin\CMSPlugin;
/**
* Content responsive images plugin
*/
class PlgContentResponsive extends CMSPlugin
{
/**
* System std Event Prepare
*
* @param string $context The context of the content being passed to the plugin.
* @param object &$row The article object. Note $article->text is also available
* @param mixed &$params The article params
* @param integer $page The 'page' number
*
* @return mixed Always returns void or true
*
* @throws Exception
*
* @since 4.0.0
*/
public function onContentPrepare($context, &$row, &$params, $page = 0)
{
$this->mainLogic($context, $row, true);
}
/**
* System std Event After Save
*
* @param string $context The context of the content being passed to the plugin.
* @param object &$article The article object. Note $article->text is also available
* @param mixed &$params The article params
* @param integer $page The 'page' number
*
* @return mixed Always returns void or true
*
* @throws Exception
*
* @since 4.0.0
*/
public function onContentAfterSave($context, &$article, &$params)
{
$this->mainLogic($context, $article, false);
}
/**
* Creates all the image sizes on the fly on the save and optionally changes markup
*
* @param string $context The context of the content being passed to the plugin.
* @param object &$article The article object. Note $article->text is also available
* @param mixed &$params The article params
* @param integer $page The 'page' number
*
* @return mixed Always returns void or true
*
* @throws Exception
*
* @since 4.0.0
*/
private function mainLogic($context, &$row, $replaceTags = false)
{
// Bail out if the helper isn't loaded
if (!class_exists('\Ttc\Freebies\Responsive\Helper')) {
return;
}
$pluginComponents = $this->params->get('components');
if (!is_object($pluginComponents)) {
try {
$pluginComponents = \json_decode($this->params->get('components'));
} catch (\Exception $e) {
return;
}
if ($pluginComponents === false) {
return;
}
}
if ((array) $pluginComponents) {
foreach ($pluginComponents as $key => $component) {
$views = preg_split('/[\s,]+/', $component->component_view);
if ($views === '') {
continue;
}
foreach ($views as $view) {
if ($context !== $component->component_name . '.' . $view && substr($component->component_name, 0, 4) !== "mod_") {
continue;
}
$columns = preg_split('/[\s,]+/', $component->component_db_column);
if ($columns === '') {
continue;
}
foreach ($columns as $currentNeedle) {
// $currentNeedle = $component->component_db_columnn;
$matches = [];
if (!empty($row->{$currentNeedle})) {
if (!preg_match_all('#<img\s[^>]+>#', $row->{$currentNeedle}, $matches)) {
continue;
}
if (count($matches)) {
foreach ($matches[0] as $img) {
// Make sure we have a src
if (strpos($img, ' src=') !== false && strpos($img, '//') === false) {
$processed = (new \Ttc\Freebies\Responsive\Helper)->transformImage($img, [200, 320, 480, 768, 992, 1200, 1600, 1920]);
if ($replaceTags && $processed !== $img) {
$row->{$currentNeedle} = str_replace($img, $processed, $row->{$currentNeedle});
}
}
}
}
}
};
};
if (substr($component->component_name, 0, 4) === "mod_") {
$columns = preg_split('/[\s,]+/', $component->component_db_column);
if ($columns === '') {
continue;
}
foreach ($columns as $currentNeedle) {
// $currentNeedle = $component->component_db_columnn;
$matches = [];
if (!empty($row->{$currentNeedle})) {
if (!preg_match_all('#<img\s[^>]+>#', $row->{$currentNeedle}, $matches)) {
continue;
}
if (count($matches)) {
foreach ($matches[0] as $img) {
// Make sure we have a src
if (strpos($img, ' src=') !== false && strpos($img, '//') === false) {
$processed = (new \Ttc\Freebies\Responsive\Helper)->transformImage($img, [200, 320, 480, 768, 992, 1200, 1600, 1920]);
if ($replaceTags && $processed !== $img) {
$row->{$currentNeedle} = str_replace($img, $processed, $row->{$currentNeedle});
}
}
}
}
}
};
}
}
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment