diff --git a/projects/plugins/jetpack/changelog/fix-related-posts-warnings b/projects/plugins/jetpack/changelog/fix-related-posts-warnings new file mode 100644 index 0000000000000..1ff52a765cb28 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-related-posts-warnings @@ -0,0 +1,4 @@ +Significance: patch +Type: other + +Related Posts: Prevent PHP errors when settings are malformed. diff --git a/projects/plugins/jetpack/modules/related-posts/jetpack-related-posts.php b/projects/plugins/jetpack/modules/related-posts/jetpack-related-posts.php index 292c4ec23d12a..c556eadee1915 100644 --- a/projects/plugins/jetpack/modules/related-posts/jetpack-related-posts.php +++ b/projects/plugins/jetpack/modules/related-posts/jetpack-related-posts.php @@ -198,7 +198,7 @@ public function action_frontend_init() { public function get_headline() { $options = $this->get_options(); - if ( $options['show_headline'] ) { + if ( ! empty( $options['show_headline'] ) ) { $headline = sprintf( /** This filter is already documented in modules/sharedaddy/sharing-service.php */ apply_filters( 'jetpack_sharing_headline_html', '', esc_html( $options['headline'] ), 'related-posts' ), @@ -931,7 +931,7 @@ public function get_for_post_id( $post_id, array $args ) { } if ( - ! $options['enabled'] + empty( $options['enabled'] ) || 0 === (int) $post_id || empty( $options['size'] ) ) { @@ -1305,15 +1305,15 @@ protected function action_frontend_init_ajax( array $excludes ) { $response = array( 'version' => self::VERSION, - 'show_thumbnails' => (bool) $options['show_thumbnails'], - 'show_date' => (bool) $options['show_date'], - 'show_context' => (bool) $options['show_context'], - 'layout' => (string) $options['layout'], - 'headline' => (string) $options['headline'], + 'show_thumbnails' => (bool) ( $options['show_thumbnails'] ?? false ), + 'show_date' => (bool) ( $options['show_date'] ?? true ), + 'show_context' => (bool) ( $options['show_context'] ?? true ), + 'layout' => (string) ( $options['layout'] ?? 'grid' ), + 'headline' => (string) ( $options['headline'] ?? '' ), 'items' => array(), ); - if ( count( $related_posts ) === $options['size'] ) { + if ( ! empty( $options['size'] ) && count( $related_posts ) === $options['size'] ) { $response['items'] = $related_posts; }