Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert media action plugins to service provider #40169

Merged
merged 11 commits into from
Apr 3, 2023
4 changes: 3 additions & 1 deletion plugins/media-action/crop/crop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_MEDIA-ACTION_CROP_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\MediaAction\Crop</namespace>
<files>
<filename plugin="crop">crop.php</filename>
<folder>form</folder>
<folder plugin="crop">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_media-action_crop.ini</language>
Expand Down
47 changes: 47 additions & 0 deletions plugins/media-action/crop/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Media-Action.crop
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\MediaAction\Crop\Extension\Crop;

return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container): void
{
$container->set(
PluginInterface::class,
function (Container $container) {
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new Crop(
$dispatcher,
(array) PluginHelper::getPlugin('media-action', 'crop')
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt

* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

use Joomla\CMS\Factory;
namespace Joomla\Plugin\MediaAction\Crop\Extension;

use Joomla\CMS\Application\CMSWebApplicationInterface;
use Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -21,7 +22,7 @@
*
* @since 4.0.0
*/
class PlgMediaActionCrop extends \Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin
final class Crop extends MediaActionPlugin
{
/**
* Load the javascript files of the plugin.
Expand All @@ -34,7 +35,11 @@ protected function loadJs()
{
parent::loadJs();

Factory::getApplication()->getDocument()->getWebAssetManager()->useScript('cropperjs');
if (!$this->getApplication() instanceof CMSWebApplicationInterface) {
return;
}

$this->getApplication()->getDocument()->getWebAssetManager()->useScript('cropperjs');
}

/**
Expand All @@ -48,6 +53,10 @@ protected function loadCss()
{
parent::loadCss();

Factory::getApplication()->getDocument()->getWebAssetManager()->useStyle('cropperjs');
if (!$this->getApplication() instanceof CMSWebApplicationInterface) {
return;
}

$this->getApplication()->getDocument()->getWebAssetManager()->useStyle('cropperjs');
}
}
4 changes: 3 additions & 1 deletion plugins/media-action/resize/resize.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_MEDIA-ACTION_RESIZE_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\MediaAction\Resize</namespace>
<files>
<filename plugin="resize">resize.php</filename>
<folder>form</folder>
<folder plugin="resize">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_media-action_resize.ini</language>
Expand Down
47 changes: 47 additions & 0 deletions plugins/media-action/resize/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Media-Action.resize
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\MediaAction\Resize\Extension\Resize;

return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container): void
{
$container->set(
PluginInterface::class,
function (Container $container) {
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new Resize(
$dispatcher,
(array) PluginHelper::getPlugin('media-action', 'resize')
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt

* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

namespace Joomla\Plugin\MediaAction\Resize\Extension;

use Joomla\CMS\Image\Image;
use Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
Expand All @@ -21,7 +22,7 @@
*
* @since 4.0.0
*/
class PlgMediaActionResize extends \Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin
final class Resize extends MediaActionPlugin
{
/**
* The save event.
Expand Down Expand Up @@ -51,10 +52,7 @@ public function onContentBeforeSave($context, $item, $isNew, $data = [])

$imgObject = new Image(imagecreatefromstring($item->data));

if (
$imgObject->getWidth() < $this->params->get('batch_width', 0)
&& $imgObject->getHeight() < $this->params->get('batch_height', 0)
) {
if ($imgObject->getWidth() < $this->params->get('batch_width', 0) && $imgObject->getHeight() < $this->params->get('batch_height', 0)) {
laoneo marked this conversation as resolved.
Show resolved Hide resolved
return;
}

Expand Down
4 changes: 3 additions & 1 deletion plugins/media-action/rotate/rotate.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<authorUrl>www.joomla.org</authorUrl>
<version>4.0.0</version>
<description>PLG_MEDIA-ACTION_ROTATE_XML_DESCRIPTION</description>
<namespace path="src">Joomla\Plugin\MediaAction\Rotate</namespace>
<files>
<filename plugin="rotate">rotate.php</filename>
<folder>form</folder>
<folder plugin="rotate">services</folder>
<folder>src</folder>
</files>
<languages>
<language tag="en-GB">language/en-GB/plg_media-action_rotate.ini</language>
Expand Down
47 changes: 47 additions & 0 deletions plugins/media-action/rotate/services/provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/**
* @package Joomla.Plugin
* @subpackage Media-Action.rotate
*
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Extension\PluginInterface;
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
use Joomla\Event\DispatcherInterface;
use Joomla\Plugin\MediaAction\Rotate\Extension\Rotate;

return new class () implements ServiceProviderInterface {
/**
* Registers the service provider with a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since __DEPLOY_VERSION__
*/
public function register(Container $container): void
{
$container->set(
PluginInterface::class,
function (Container $container) {
$dispatcher = $container->get(DispatcherInterface::class);
$plugin = new Rotate(
$dispatcher,
(array) PluginHelper::getPlugin('media-action', 'rotate')
);
$plugin->setApplication(Factory::getApplication());

return $plugin;
}
);
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt

* @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
*/

namespace Joomla\Plugin\MediaAction\Rotate\Extension;

use Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin;

// phpcs:disable PSR1.Files.SideEffects
\defined('_JEXEC') or die;
// phpcs:enable PSR1.Files.SideEffects
Expand All @@ -19,6 +21,6 @@
*
* @since 4.0.0
*/
class PlgMediaActionRotate extends \Joomla\Component\Media\Administrator\Plugin\MediaActionPlugin
final class Rotate extends MediaActionPlugin
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('Test that the crop media action plugin', () => {
it('is shown when editing an image', () => {
cy.doAdministratorLogin();
cy.visit('/administrator/index.php?option=com_media&view=file&mediatypes=0,1,2,3&path=local-images:/joomla_black.png');
cy.get('button[role="tab"]:contains(Crop)').click();

cy.contains('legend', 'Crop');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('Test that the resize media action plugin', () => {
it('is shown when editing an image', () => {
cy.doAdministratorLogin();
cy.visit('/administrator/index.php?option=com_media&view=file&mediatypes=0,1,2,3&path=local-images:/joomla_black.png');
cy.get('button[role="tab"]:contains(Resize)').click();

cy.contains('legend', 'Resize');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
describe('Test that the rotate media action plugin', () => {
it('is shown when editing an image', () => {
cy.doAdministratorLogin();
cy.visit('/administrator/index.php?option=com_media&view=file&mediatypes=0,1,2,3&path=local-images:/joomla_black.png');
cy.get('button[role="tab"]:contains(Rotate)').click();

cy.contains('legend', 'Rotate');
});
});