Fatal error in PostModel.php when post object is null
-
Hello PublishPress Future team,
A critical error related to manual workflow triggers occurred in the previous version.
I am reporting this because updating the plugin to the latest version did not resolve the issue.Error message:
Warning: Attempt to read property "ID" on null in /wp-content/plugins/post-expirator/src/Modules/Workflows/Models/PostModel.php on line 124 Fatal error: Uncaught TypeError: array_map(): Argument #2 ($array) must be of type array, false given in /wp-content/plugins/post-expirator/src/Modules/Workflows/Models/PostModel.php:125The issue appears to occur inside:
public function getManuallyEnabledWorkflows(): array { $selectedWorkflowIds = get_post_meta( $this->post->ID, self::META_KEY_WORKFLOW_MANUALLY_TRIGGERED, false ); $selectedWorkflowIds = array_map('intval', $selectedWorkflowIds); return $selectedWorkflowIds; }The following is a report of improvements made in ChatGPT.
The issue appears to occur inside:
It seems
$this->postcan becomenullunder some conditions, possibly due to:- orphaned postmeta records
- deleted posts
- deleted workflows
- inconsistent workflow metadata after plugin updates
- REST API preload during block editor initialization
In this case, the plugin attempts to access:
$this->post->IDwithout validating that
$this->postis a validWP_Postobject.Additionally,
get_post_meta()may return a non-array value, which later causes:array_map(): Argument #2 ($array) must be of type array, false givenSuggested fix:
public function getManuallyEnabledWorkflows(): array { if (!$this->post || !isset($this->post->ID)) { return []; } $selectedWorkflowIds = get_post_meta( $this->post->ID, self::META_KEY_WORKFLOW_MANUALLY_TRIGGERED, false ); if (!is_array($selectedWorkflowIds)) { return []; } return array_map('intval', $selectedWorkflowIds); }I believe adding these guards would prevent fatal crashes caused by inconsistent metadata or deleted content.
This concludes the answers obtained through ChatGPT.
Thank you for your work on the plugin.
You must be logged in to reply to this topic.