Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 5.2.2 (2026-11-03)

### Fixed

- **The placeholder of a shortcode whose record was deleted no longer ends in a 500** (#6). The
placeholder endpoint looked the `ObjectID` up with `byID()` and called a method on the result, which
is `null` once the record is gone. The class singleton now answers instead, as it already did for a
shortcode without an ID, so the editor shows a placeholder.

### Changed

- **A shortcodable class without its parser method throws a `LogicException`** (#5), with the same
message, instead of `user_error(..., E_USER_ERROR)`, which PHP 8.4 deprecates.
**Behaviour change:** on a misconfigured site this is now an uncaught (but catchable) exception
instead of a fatal error; every request still fails until the class is fixed.

## 5.2.1 (2026-11-02)

### Security
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ Implement these methods on your shortcodable classes (they may also be added via
**NOTE: the parser method gets called on a singleton object instance.**<br />
(So there's no `$this->ID` or `$this->owner->ID` etc.)

A registered class without its parser method is a fatal error (`E_USER_ERROR`) when the
shortcodes are registered, on every request.
A registered class without its parser method throws a `LogicException` when the shortcodes are
registered, so every request fails until it is fixed (up to 5.2.1 this was a fatal `E_USER_ERROR`).

A plain class (not a DataObject) needs the `Extensible` trait: the module checks methods with
`hasMethod()`, which comes from that trait (`DataObject` and `ViewableData`/`ModelData` subclasses
Expand Down
6 changes: 5 additions & 1 deletion src/Controllers/ShortcodableAdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,13 @@ public function shortcodePlaceHolder($request)
return;
}

$object = null;
if ($object_id && is_subclass_of($sc_class, DataObject::class)) {
$object = $sc_class::get()->byID($object_id);
} else {
}
# No ObjectID, not a DataObject, or a record that no longer exists (deleted after the shortcode was
# inserted): the class singleton answers, so the editor gets a placeholder instead of a 500 (#6)
if (!$object) {
$object = singleton($sc_class);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Shortcodable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Shortcodable;

use LogicException;
use SilverStripe\Core\ClassInfo;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
Expand Down Expand Up @@ -39,7 +40,10 @@ public static function register_class($class)
$parseMethodName = Config::inst()->get($class, 'shortcode_callback') ?: 'parse_shortcode';

if (!singleton($class)->hasMethod($parseMethodName)) {
user_error("Failed to register \"$class\" with Shortcodable: missing/unconfigured shortcode parser method (looked for: $parseMethodName)", E_USER_ERROR);
# An exception rather than user_error(..., E_USER_ERROR): PHP 8.4 deprecates passing E_USER_ERROR to
# trigger_error() (#5). Same message; it still stops the request, on every request, until fixed.
// user_error("Failed to register \"$class\" with Shortcodable: missing/unconfigured shortcode parser method (looked for: $parseMethodName)", E_USER_ERROR);
throw new LogicException("Failed to register \"$class\" with Shortcodable: missing/unconfigured shortcode parser method (looked for: $parseMethodName)");
}
// if (!$shortcode = Config::inst()->get($class, 'shortcode')) {
// user_error("Registering \"$class\" with short classname as shortcode: missing \$shortcode config value", E_USER_NOTICE);
Expand Down
17 changes: 17 additions & 0 deletions tests/ShortcodableAdminControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,23 @@ public function testDefaultPlaceholderConfigChangesTheSize()
$this->assertEquals(77, $query['h']);
}

public function testPlaceholderForADeletedRecordFallsBackToTheClassSingleton()
{
// An ObjectID whose record no longer exists (deleted after the shortcode was inserted) must not
// end in a 500 from calling a method on null (#6): the class singleton answers instead, here
// with the default SVG placeholder, as for a request without an ObjectID.
$missingId = 999999;
$this->assertNull(ShortcodableRecord::get()->byID($missingId));

$this->logInAsCmsUser();
$this->autoFollowRedirection = false;
$response = $this->get('admin/shortcodable/placeholder/sc_record/' . $missingId . '?sc='
. urlencode('[sc_record id="' . $missingId . '"]'));

$this->assertSame(302, $response->getStatusCode());
$this->assertSame('[sc_record id="' . $missingId . '"]', $this->redirectQuery($response)['txt']);
}

public function testPlaceholderForAnUnknownShortcodeIsEmpty()
{
$this->logInAsCmsUser();
Expand Down
14 changes: 14 additions & 0 deletions tests/ShortcodableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Shortcodable\Shortcodable;
use Shortcodable\Tests\Stub\BlockShortcode;
use Shortcodable\Tests\Stub\CustomCallbackShortcode;
use Shortcodable\Tests\Stub\MissingParserShortcode;
use Shortcodable\Tests\Stub\ShortcodableRecord;
use Shortcodable\Tests\Stub\SimpleShortcode;
use SilverStripe\Admin\LeftAndMain;
Expand Down Expand Up @@ -75,6 +76,19 @@ public function testRegisterClassIgnoresClassesThatDoNotExist()
$this->assertSame($before, ShortcodeParser::get()->getRegisteredShortcodes());
}

public function testRegisterClassWithoutItsParserMethodThrows()
{
// A LogicException, not user_error(..., E_USER_ERROR): PHP 8.4 deprecates passing E_USER_ERROR
// to trigger_error() (#5). The message stays the same, so it still names the class and method.
$this->expectException(\LogicException::class);
$this->expectExceptionMessage(
'Failed to register "' . MissingParserShortcode::class . '" with Shortcodable: '
. 'missing/unconfigured shortcode parser method (looked for: parse_shortcode)'
);

Shortcodable::register_class(MissingParserShortcode::class);
}

public function testRegisterClassesRegistersEachClass()
{
$this->registeredTags = ['sc_simple', 'sc_block'];
Expand Down
22 changes: 22 additions & 0 deletions tests/Stub/MissingParserShortcode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Shortcodable\Tests\Stub;

use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Dev\TestOnly;

/**
* A misconfigured shortcodable class: it has no parse_shortcode() and no $shortcode_callback, so
* registering it must fail loudly. Never put it in a shortcodable_classes list that is registered
* in setUp(), or every test in that class fails.
*/
class MissingParserShortcode implements TestOnly
{
use Configurable;
use Extensible;
use Injectable;

private static $shortcode = 'sc_missing_parser';
}
Loading