From 43f14d4d0ea09507b430fc13321dc064b340b8e1 Mon Sep 17 00:00:00 2001 From: Diede Exterkate Date: Wed, 26 Jan 2022 15:42:19 +0100 Subject: [PATCH] Don't pass null to explode --- src/builders/indexable-post-builder.php | 67 +++++++++++-------- .../builders/indexable-post-builder-test.php | 4 +- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/src/builders/indexable-post-builder.php b/src/builders/indexable-post-builder.php index 71417b8ed06..20fd15df674 100644 --- a/src/builders/indexable-post-builder.php +++ b/src/builders/indexable-post-builder.php @@ -4,8 +4,8 @@ use WP_Error; use WP_Post; -use WPSEO_Meta; use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Found_Exception; +use Yoast\WP\SEO\Helpers\Meta_Helper; use Yoast\WP\SEO\Helpers\Post_Helper; use Yoast\WP\SEO\Helpers\Post_Type_Helper; use Yoast\WP\SEO\Models\Indexable; @@ -49,21 +49,31 @@ class Indexable_Post_Builder { */ protected $version; + /** + * The meta helper. + * + * @var Meta_Helper + */ + protected $meta; + /** * Indexable_Post_Builder constructor. * * @param Post_Helper $post_helper The post helper. * @param Post_Type_Helper $post_type_helper The post type helper. * @param Indexable_Builder_Versions $versions The indexable builder versions. + * @param Meta_Helper $meta The meta helper. */ public function __construct( Post_Helper $post_helper, Post_Type_Helper $post_type_helper, - Indexable_Builder_Versions $versions + Indexable_Builder_Versions $versions, + Meta_Helper $meta ) { $this->post_helper = $post_helper; $this->post_type_helper = $post_type_helper; $this->version = $versions->get_latest_version_for_type( 'post' ); + $this->meta = $meta; } /** @@ -108,21 +118,22 @@ public function build( $post_id, $indexable ) { $indexable->permalink = $this->get_permalink( $post->post_type, $post_id ); $indexable->primary_focus_keyword_score = $this->get_keyword_score( - $this->get_meta_value( $post_id, 'focuskw' ), - (int) $this->get_meta_value( $post_id, 'linkdex' ) + $this->meta->get_value( 'focuskw', $post_id ), + (int) $this->meta->get_value( 'linkdex', $post_id ) ); - $indexable->readability_score = (int) $this->get_meta_value( $post_id, 'content_score' ); + $indexable->readability_score = (int) $this->meta->get_value( 'content_score', $post_id ); - $indexable->is_cornerstone = ( $this->get_meta_value( $post_id, 'is_cornerstone' ) === '1' ); + $indexable->is_cornerstone = ( $this->meta->get_value( 'is_cornerstone', $post_id ) === '1' ); $indexable->is_robots_noindex = $this->get_robots_noindex( - $this->get_meta_value( $post_id, 'meta-robots-noindex' ) + (int) $this->meta->get_value( 'meta-robots-noindex', $post_id ) ); // Set additional meta-robots values. - $indexable->is_robots_nofollow = ( $this->get_meta_value( $post_id, 'meta-robots-nofollow' ) === '1' ); - $noindex_advanced = $this->get_meta_value( $post_id, 'meta-robots-adv' ); + $indexable->is_robots_nofollow = ( $this->meta->get_value( 'meta-robots-nofollow', $post_id ) === '1' ); + $noindex_advanced = $this->meta->get_value( 'meta-robots-adv', $post_id ); $meta_robots = \explode( ',', $noindex_advanced ); + foreach ( $this->get_robots_options() as $meta_robots_option ) { $indexable->{'is_robots_' . $meta_robots_option} = \in_array( $meta_robots_option, $meta_robots, true ) ? 1 : null; } @@ -130,7 +141,7 @@ public function build( $post_id, $indexable ) { $this->reset_social_images( $indexable ); foreach ( $this->get_indexable_lookup() as $meta_key => $indexable_key ) { - $indexable->{$indexable_key} = $this->get_meta_value( $post_id, $meta_key ); + $indexable->{$indexable_key} = $this->empty_string_to_null( $this->meta->get_value( $meta_key, $post_id ) ); } if ( empty( $indexable->breadcrumb_title ) ) { @@ -149,8 +160,8 @@ public function build( $post_id, $indexable ) { $indexable->has_public_posts = $this->has_public_posts( $indexable ); $indexable->blog_id = \get_current_blog_id(); - $indexable->schema_page_type = $this->get_meta_value( $post_id, 'schema_page_type' ); - $indexable->schema_article_type = $this->get_meta_value( $post_id, 'schema_article_type' ); + $indexable->schema_page_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_page_type', $post_id ) ); + $indexable->schema_article_type = $this->empty_string_to_null( $this->meta->get_value( 'schema_article_type', $post_id ) ); $indexable->object_last_modified = $post->post_modified_gmt; $indexable->object_published_at = $post->post_date_gmt; @@ -326,23 +337,6 @@ protected function get_indexable_lookup() { ]; } - /** - * Retrieves the current value for the meta field. - * - * @param int $post_id The post ID to use. - * @param string $meta_key Meta key to fetch. - * - * @return mixed The value of the indexable entry to use. - */ - protected function get_meta_value( $post_id, $meta_key ) { - $value = WPSEO_Meta::get_value( $meta_key, $post_id ); - if ( \is_string( $value ) && $value === '' ) { - return null; - } - - return $value; - } - /** * Finds an alternative image for the social image. * @@ -415,4 +409,19 @@ protected function get_number_of_pages_for_post( $post ) { protected function should_exclude_post( $post ) { return $this->post_type_helper->is_excluded( $post->post_type ); } + + /** + * Transforms an empty string into null. Leaves non-empty strings intact. + * + * @param string $string The string. + * + * @return string|null The input string or null. + */ + protected function empty_string_to_null( $string ) { + if ( ! is_string( $string ) || $string === '' ) { + return null; + } + + return $string; + } } diff --git a/tests/unit/builders/indexable-post-builder-test.php b/tests/unit/builders/indexable-post-builder-test.php index 9bc7411b54c..0346b87a8c7 100644 --- a/tests/unit/builders/indexable-post-builder-test.php +++ b/tests/unit/builders/indexable-post-builder-test.php @@ -8,6 +8,7 @@ use Yoast\WP\SEO\Builders\Indexable_Post_Builder; use Yoast\WP\SEO\Exceptions\Indexable\Post_Not_Found_Exception; use Yoast\WP\SEO\Helpers\Image_Helper; +use Yoast\WP\SEO\Helpers\Meta_Helper; use Yoast\WP\SEO\Helpers\Open_Graph\Image_Helper as Open_Graph_Image_Helper; use Yoast\WP\SEO\Helpers\Post_Helper; use Yoast\WP\SEO\Helpers\Post_Type_Helper; @@ -104,7 +105,8 @@ protected function set_up() { $this->instance = new Indexable_Post_Builder_Double( $this->post, $this->post_type_helper, - new Indexable_Builder_Versions() + new Indexable_Builder_Versions(), + new Meta_Helper() ); $this->instance->set_indexable_repository( $this->indexable_repository );