-
-
Notifications
You must be signed in to change notification settings - Fork 163
Description
Background
I am in the process of upgrading a quite old WonderCMS installation (3.0.4) and hit this problem.
Fixed here
main...hightowe:wondercms-subpage-bugfix:main
Description
A PHP Fatal error occurs when attempting to create or save a subpage in WonderCMS (v3.6.0) if the parent page was created in an older version of WonderCMS that did not explicitly initialize the subpages property in database.js.
When the parent page lacks the subpages key, the code attempts to descend into it, resulting in a null value. Subsequent attempts to assign properties to this null object trigger a Fatal Error.
Error Trace
PHP Fatal error: Uncaught Error: Attempt to assign property "testsubpage" on null in index.php:798
Stack trace:
#0 index.php(2656): Wcms->createPage()
#1 index.php(164): Wcms->saveAction()
#2 index.php(15): Wcms->init()
Additionally, several PHP warnings are triggered in findAndUpdateMenuKey due to null values being passed to explode() and foreach().
Root Cause
The core issue is that many existing WonderCMS installations have a database.js where pages do not have a subpages key. The current code assumes this key always exists after a depth of 1, which fails on legacy data or after certain upgrade paths.
Proposed Fix
The fix involves adding surgical property_exists checks and ensuring that the subpages property is initialized as a stdClass before assignment. This ensures compatibility with older database structures without requiring a full database migration.
Key Changes:
- createPage(): Add
property_existschecks before descending into child pages. - findAndUpdateMenuKey(): Ensure the menu key is cast to a string and return an empty array if null to prevent
foreachwarnings. - Initialization: Use the internal
self::DB_PAGES_SUBPAGE_KEYconstant consistently.
Impact
This affects users who have upgraded their WonderCMS installation over time and are now trying to utilize subpage functionality on older parent pages.