Skip to content

Commit 79675fd

Browse files
authored
[5.3] Ajax component support of Stringable results (joomla#43530)
Thanks
1 parent bee183a commit 79675fd

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

components/com_ajax/ajax.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Joomla\CMS\Log\Log;
1717
use Joomla\CMS\Plugin\PluginHelper;
1818
use Joomla\CMS\Response\JsonResponse;
19+
use Joomla\CMS\String\StringableInterface;
1920
use Joomla\CMS\Table\Table;
2021

2122
/*
@@ -205,15 +206,26 @@
205206
// Return the results in the desired format
206207
switch ($format) {
207208
case 'json':
208-
// JSONinzed
209-
echo new JsonResponse($results, null, false, $input->get('ignoreMessages', true, 'bool'));
209+
if (!($results instanceof Throwable) && $results instanceof StringableInterface) {
210+
echo $results;
211+
} else {
212+
if (\is_object($results) && !($results instanceof Throwable) && $results instanceof \Stringable) {
213+
@trigger_error(
214+
'Ajax result object (except Throwable) which implements Stringable interface (implicitly or explicitly), will be rendered directly. Starting from 7.0',
215+
\E_USER_DEPRECATED
216+
);
217+
}
218+
219+
// JSONized
220+
echo new JsonResponse($results, null, false, $input->get('ignoreMessages', true, 'bool'));
221+
}
210222

211223
break;
212224

213225
default:
214226
// Handle as raw format
215227
// Output exception
216-
if ($results instanceof Exception) {
228+
if ($results instanceof Throwable) {
217229
// Log an error
218230
Log::add($results->getMessage(), Log::ERROR);
219231

@@ -222,7 +234,7 @@
222234

223235
// Echo exception type and message
224236
$out = \get_class($results) . ': ' . $results->getMessage();
225-
} elseif (\is_scalar($results)) {
237+
} elseif (\is_scalar($results) || $results instanceof StringableInterface) {
226238
// Output string/ null
227239
$out = (string) $results;
228240
} else {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* Joomla! Content Management System
5+
*
6+
* @copyright (C) 2024 Open Source Matters, Inc. <https://www.joomla.org>
7+
* @license GNU General Public License version 2 or later; see LICENSE.txt
8+
*/
9+
10+
namespace Joomla\CMS\String;
11+
12+
// phpcs:disable PSR1.Files.SideEffects
13+
\defined('_JEXEC') or die;
14+
// phpcs:enable PSR1.Files.SideEffects
15+
16+
/**
17+
* A transitioning interface to PHP implicit \Stringable interface
18+
*
19+
* @since __DEPLOY_VERSION__
20+
*/
21+
interface StringableInterface
22+
{
23+
/**
24+
* To String magick.
25+
*
26+
* @return string
27+
*
28+
* @since __DEPLOY_VERSION__
29+
*/
30+
public function __toString(): string;
31+
}

plugins/editors/tinymce/src/Extension/TinyMCE.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
namespace Joomla\Plugin\Editors\TinyMCE\Extension;
1212

1313
use Joomla\CMS\Event\Editor\EditorSetupEvent;
14+
use Joomla\CMS\Event\Plugin\AjaxEvent;
1415
use Joomla\CMS\Language\Text;
1516
use Joomla\CMS\Plugin\CMSPlugin;
1617
use Joomla\CMS\Session\Session;
18+
use Joomla\CMS\String\StringableInterface;
1719
use Joomla\Database\DatabaseAwareTrait;
1820
use Joomla\Event\SubscriberInterface;
1921
use Joomla\Filesystem\Folder;
@@ -77,11 +79,21 @@ public function onEditorSetup(EditorSetupEvent $event)
7779
*
7880
* @since 5.0.0
7981
*/
80-
public function onAjaxTinymce()
82+
public function onAjaxTinymce(AjaxEvent $event)
8183
{
84+
// Create response object, with list of the templates
85+
$response = new class () implements StringableInterface {
86+
public $data = [];
87+
88+
public function __toString(): string
89+
{
90+
return json_encode($this->data);
91+
}
92+
};
93+
$event->updateEventResult($response);
94+
8295
if (!Session::checkToken('request')) {
83-
echo json_encode([]);
84-
exit();
96+
return;
8597
}
8698

8799
$this->loadLanguage();
@@ -91,8 +103,7 @@ public function onAjaxTinymce()
91103
$template = $this->getApplication()->getInput()->getPath('template', '');
92104

93105
if ('' === $template) {
94-
echo json_encode([]);
95-
exit();
106+
return;
96107
}
97108

98109
$filepaths = is_dir(JPATH_ROOT . '/templates/' . $template)
@@ -115,7 +126,7 @@ public function onAjaxTinymce()
115126
];
116127
}
117128

118-
echo json_encode($templates);
119-
exit();
129+
// Add the list of templates to the response
130+
$response->data = $templates;
120131
}
121132
}

0 commit comments

Comments
 (0)