diff --git a/CHANGELOG.md b/CHANGELOG.md index 11a1335..594c3e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d6b1d33..e619690 100644 --- a/README.md +++ b/README.md @@ -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.**
(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 diff --git a/src/Controllers/ShortcodableAdminController.php b/src/Controllers/ShortcodableAdminController.php index 162d61e..028d697 100644 --- a/src/Controllers/ShortcodableAdminController.php +++ b/src/Controllers/ShortcodableAdminController.php @@ -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); } diff --git a/src/Shortcodable.php b/src/Shortcodable.php index 5db9502..bbfa6ce 100644 --- a/src/Shortcodable.php +++ b/src/Shortcodable.php @@ -2,6 +2,7 @@ namespace Shortcodable; +use LogicException; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Config; use SilverStripe\Core\Config\Configurable; @@ -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); diff --git a/tests/ShortcodableAdminControllerTest.php b/tests/ShortcodableAdminControllerTest.php index f939676..539f3de 100644 --- a/tests/ShortcodableAdminControllerTest.php +++ b/tests/ShortcodableAdminControllerTest.php @@ -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(); diff --git a/tests/ShortcodableTest.php b/tests/ShortcodableTest.php index 6d92dff..9ca2f3e 100644 --- a/tests/ShortcodableTest.php +++ b/tests/ShortcodableTest.php @@ -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; @@ -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']; diff --git a/tests/Stub/MissingParserShortcode.php b/tests/Stub/MissingParserShortcode.php new file mode 100644 index 0000000..eb06950 --- /dev/null +++ b/tests/Stub/MissingParserShortcode.php @@ -0,0 +1,22 @@ +