CoreFiles by mySites.guru
imagelazyload.php56 lines, 1,404 bytes, from Joomla_4.0.0-beta2-Beta-Full_Package.zip
Raw History Compare with 4.0.0-beta
md5
0e5e0486cc512f166d26070f5436c8ea
sha1
a52bc19345b805db3c0a9e845623e1f5246020fd
sha256
a40bb878dca40bf3367509070d6a19cb265db071bd502599748f924c639ebfb7
<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  content.imagelazyload
 *
 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */


defined('_JEXEC') or die;

use Joomla\CMS\Plugin\CMSPlugin;

/**
 * Plugin adds the lazyloading attribute on the fly
 *
 * @since  1.0
 */
class PlgContentImageLazyload extends CMSPlugin
{
	/**
	 * Plugin that adds on the fly the loading=lazy attribute to image tags
	 *
	 * @param   string   $context  The context of the content being passed to the plugin.
	 * @param   object   &$row     The article object.
	 * @param   mixed    &$params  The article params
	 * @param   integer  $page     The 'page' number
	 *
	 * @return  mixed  Always returns void or true
	 *
	 * @since   4.0.0
	 */
	public function onContentPrepare($context, &$row, &$params, $page = 0)
	{
		if (strpos($row->text, '<img') === false)
		{
			return;
		}

		if (!preg_match_all('/<img\s[^>]+>/', $row->text, $matches))
		{
			return;
		}

		foreach ($matches[0] as $image)
		{
			// Make sure we have a src but no loading attribute
			if (strpos($image, ' src=') !== false && strpos($image, ' loading=') === false)
			{
				$lazyloadImage = str_replace('<img ', '<img loading="lazy" ', $image);
				$row->text = str_replace($image, $lazyloadImage, $row->text);
			}
		}
	}
}

The same content is in 4 releases, Joomla 4.0.0-beta to 4.0.0-beta4. See them all, or every version of this file.