From ac24737f7ee06e53bc73f68cd883950389915ad3 Mon Sep 17 00:00:00 2001 From: Alec Geatches Date: Fri, 25 Apr 2025 11:55:37 -0600 Subject: [PATCH] Fix "invalid_page_template" error when updating pages with unregistered page templates --- src/Models/class-inbound-smart-link.php | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Models/class-inbound-smart-link.php b/src/Models/class-inbound-smart-link.php index 8a91476798..844cfae3b8 100644 --- a/src/Models/class-inbound-smart-link.php +++ b/src/Models/class-inbound-smart-link.php @@ -15,6 +15,7 @@ use Parsely\Utils\Utils; use ReflectionClass; use WP_Post; +use WP_Error; /** * Model for Inbound Smart Link. @@ -812,7 +813,7 @@ public function apply() { true ); - if ( is_wp_error( $updated_post ) ) { + if ( is_wp_error( $updated_post ) && ! $this->is_ignorable_update_error( $updated_post ) ) { return $updated_post; } @@ -942,7 +943,7 @@ public function remove( $restore_original_link = false, $delete_smart_link = tru true ); - if ( is_wp_error( $updated_post ) ) { + if ( is_wp_error( $updated_post ) && ! $this->is_ignorable_update_error( $updated_post ) ) { return $updated_post; } @@ -1280,4 +1281,27 @@ private function is_the_same_line( string $line1, string $line2 ): bool { return $text1 === $text2; } + + /** + * Checks if a WP_Error from wp_update_post() is ignorable. + * + * @since 3.19.0 + * + * @param WP_Error $error The error to check. + * @return bool True if the error is ignorable, false otherwise. + */ + private function is_ignorable_update_error( WP_Error $error ): bool { + // The 'invalid_page_template' error can be returned from wp_update_post() + // if the saved page template is a custom type that no longer exists. + // This error is returned *after* the post has been updated with a new smart + // link, so we can safely ignore it. + // + // The post will use the 'default' page template if a custom type + // doesn't exist anyway. + if ( 'invalid_page_template' === $error->get_error_code() ) { + return true; + } + + return false; + } }