From d2aec405c1934837442c7e72e5b0ef27584ace40 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 12:07:15 +0200 Subject: [PATCH 01/15] Set all base definitions to false --- config/dependency-injection/services.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/dependency-injection/services.php b/config/dependency-injection/services.php index 5cdf386d2be..12f41b04ce9 100644 --- a/config/dependency-injection/services.php +++ b/config/dependency-injection/services.php @@ -73,7 +73,7 @@ $yoast_seo_base_definition ->setAutowired( true ) ->setAutoconfigured( true ) - ->setPublic( true ); + ->setPublic( false ); /** * Holds the dependency injection loader. From 466472c9dd6f574facd130b9c884265494cbb220 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 12:07:42 +0200 Subject: [PATCH 02/15] Make sure all the definitions needed remain public --- config/dependency-injection/loader-pass.php | 34 ++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/config/dependency-injection/loader-pass.php b/config/dependency-injection/loader-pass.php index 8eef82a0358..f22bec2fa6e 100644 --- a/config/dependency-injection/loader-pass.php +++ b/config/dependency-injection/loader-pass.php @@ -12,7 +12,7 @@ use Yoast\WP\SEO\Integrations\Integration_Interface; use Yoast\WP\SEO\Loader; use Yoast\WP\SEO\Routes\Route_Interface; - +use Yoast\WP\SEO\Conditionals\Conditional; /** * A pass is a step in the compilation process of the container. * @@ -53,20 +53,51 @@ public function process( ContainerBuilder $container ) { private function process_definition( Definition $definition, Definition $loader_definition ) { $class = $definition->getClass(); + $reflect = new ReflectionClass( $class ); + $path = $reflect->getFileName(); + if( strpos($path, 'wordpress-seo/src/helpers') || + strpos($path, 'wordpress-seo/src/surfaces') || + strpos($path, 'wordpress-seo/src/actions') || + strpos($path, 'wordpress-seo/src/builders') || + strpos($path, 'wordpress-seo/src/config') || + strpos($path, 'wordpress-seo/src/generators') || + strpos($path, 'wordpress-seo/src/integrations') || + strpos($path, 'wordpress-seo/src/logger') || + strpos($path, 'wordpress-seo/src/loader') || + strpos($path, 'wordpress-seo/src/memoizers') || + strpos($path, 'wordpress-seo/src/presentations') || + strpos($path, 'wordpress-seo/src/repositories') || + strpos($path, 'wordpress-seo/src/services') || + strpos($path, 'wordpress-seo/src/schema-templates') || + strpos($path, 'wordpress-seo/src/wrappers') || + strpos($path, 'wordpress-seo/src/context') || + strpos($path, 'wordpress-seo/src/values') + ) { + $definition->setPublic( true ); + } + + if ( is_subclass_of( $class, Conditional::class ) ) { + $definition->setPublic( true ); + } + if ( \is_subclass_of( $class, Initializer_Interface::class ) ) { $loader_definition->addMethodCall( 'register_initializer', [ $class ] ); + $definition->setPublic( true ); } if ( \is_subclass_of( $class, Integration_Interface::class ) ) { $loader_definition->addMethodCall( 'register_integration', [ $class ] ); + $definition->setPublic( true ); } if ( \is_subclass_of( $class, Route_Interface::class ) ) { $loader_definition->addMethodCall( 'register_route', [ $class ] ); + $definition->setPublic( true ); } if ( \is_subclass_of( $class, Command_Interface::class ) ) { $loader_definition->addMethodCall( 'register_command', [ $class ] ); + $definition->setPublic( true ); } if ( \is_subclass_of( $class, Migration::class ) ) { @@ -76,6 +107,7 @@ private function process_definition( Definition $definition, Definition $loader_ $version = \explode( '_', $file )[0]; $plugin = $class::$plugin; $loader_definition->addMethodCall( 'register_migration', [ $plugin, $version, $class ] ); + $definition->setPublic( true ); } } } From 7e45409ab98dd2167f2f15f90ec86d7ec3d4175f Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 14:30:29 +0200 Subject: [PATCH 03/15] Add exception handling --- config/dependency-injection/loader-pass.php | 46 +++++++++++---------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/config/dependency-injection/loader-pass.php b/config/dependency-injection/loader-pass.php index f22bec2fa6e..cfe09d7a2e3 100644 --- a/config/dependency-injection/loader-pass.php +++ b/config/dependency-injection/loader-pass.php @@ -53,27 +53,31 @@ public function process( ContainerBuilder $container ) { private function process_definition( Definition $definition, Definition $loader_definition ) { $class = $definition->getClass(); - $reflect = new ReflectionClass( $class ); - $path = $reflect->getFileName(); - if( strpos($path, 'wordpress-seo/src/helpers') || - strpos($path, 'wordpress-seo/src/surfaces') || - strpos($path, 'wordpress-seo/src/actions') || - strpos($path, 'wordpress-seo/src/builders') || - strpos($path, 'wordpress-seo/src/config') || - strpos($path, 'wordpress-seo/src/generators') || - strpos($path, 'wordpress-seo/src/integrations') || - strpos($path, 'wordpress-seo/src/logger') || - strpos($path, 'wordpress-seo/src/loader') || - strpos($path, 'wordpress-seo/src/memoizers') || - strpos($path, 'wordpress-seo/src/presentations') || - strpos($path, 'wordpress-seo/src/repositories') || - strpos($path, 'wordpress-seo/src/services') || - strpos($path, 'wordpress-seo/src/schema-templates') || - strpos($path, 'wordpress-seo/src/wrappers') || - strpos($path, 'wordpress-seo/src/context') || - strpos($path, 'wordpress-seo/src/values') - ) { - $definition->setPublic( true ); + try { + $reflect = new ReflectionClass( $class ); + $path = $reflect->getFileName(); + if( strpos($path, 'wordpress-seo/src/helpers') || + strpos($path, 'wordpress-seo/src/surfaces') || + strpos($path, 'wordpress-seo/src/actions') || + strpos($path, 'wordpress-seo/src/builders') || + strpos($path, 'wordpress-seo/src/config') || + strpos($path, 'wordpress-seo/src/generators') || + strpos($path, 'wordpress-seo/src/integrations') || + strpos($path, 'wordpress-seo/src/logger') || + strpos($path, 'wordpress-seo/src/loader') || + strpos($path, 'wordpress-seo/src/memoizers') || + strpos($path, 'wordpress-seo/src/presentations') || + strpos($path, 'wordpress-seo/src/repositories') || + strpos($path, 'wordpress-seo/src/services') || + strpos($path, 'wordpress-seo/src/schema-templates') || + strpos($path, 'wordpress-seo/src/wrappers') || + strpos($path, 'wordpress-seo/src/context') || + strpos($path, 'wordpress-seo/src/values') + ) { + $definition->setPublic( true ); + } + } catch ( \Exception $e){ + } if ( is_subclass_of( $class, Conditional::class ) ) { From 35ac0ed7c6f38260616264a06de9df80c0232eea Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 14:31:14 +0200 Subject: [PATCH 04/15] Integration to add new searchAction schema piece --- .../framework/search-result-integration.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 src/schema/framework/search-result-integration.php diff --git a/src/schema/framework/search-result-integration.php b/src/schema/framework/search-result-integration.php new file mode 100644 index 00000000000..d6ce79678ad --- /dev/null +++ b/src/schema/framework/search-result-integration.php @@ -0,0 +1,52 @@ +current_page_helper = $current_page_helper; + $this->handler = $handler; + } + + /** + * {@inheritDoc} + */ + public function register_hooks() { + \add_filter( 'wpseo_schema_graph_pieces', [ $this, 'add_search_result_schema_piece' ], 10, 2 ); + } + + public function add_search_result_schema_piece( $graph, $context ) { + if ( $this->current_page_helper->is_search_result() ) { + $graph[] = ( $this->handler )( new Generate_Search_Result_Schema_Piece( \get_search_query() ) ); + } + + return $graph; + } +} + From b777b91c9c6ee06280371947bdf10a64b5a83a48 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 14:32:29 +0200 Subject: [PATCH 05/15] Search term domain object --- src/schema/domain/search-term.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/schema/domain/search-term.php diff --git a/src/schema/domain/search-term.php b/src/schema/domain/search-term.php new file mode 100644 index 00000000000..26473adda26 --- /dev/null +++ b/src/schema/domain/search-term.php @@ -0,0 +1,17 @@ +query = $query; + } + + public function get_query() { + return $this->query; + } +} \ No newline at end of file From 277d93d07504867f3b13f0c7919ffb686a7c126b Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 14:32:44 +0200 Subject: [PATCH 06/15] Search result schema piece domain object --- .../domain/search-result-schema-piece.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/schema/domain/search-result-schema-piece.php diff --git a/src/schema/domain/search-result-schema-piece.php b/src/schema/domain/search-result-schema-piece.php new file mode 100644 index 00000000000..20c1407e76c --- /dev/null +++ b/src/schema/domain/search-result-schema-piece.php @@ -0,0 +1,29 @@ +search_term = $search_term; + } + public function generate() { + $data = [ + '@type' => "SearchAction", + 'actionStatus' => "https://schema.org/CompletedActionStatus", + 'query' => $this->search_term->get_query(), + 'result' => [ '@id' => $this->context->main_schema_id ] + ]; + + return $data; + } + + public function is_needed() + { + return true; + } +} From 16c9eb8fc8bd0ad2780eca90e194405094c48843 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 14:33:35 +0200 Subject: [PATCH 07/15] Generate search result schema piece command --- .../generate-search-result-schema-piece.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/schema/application/generate-search-result-schema-piece.php diff --git a/src/schema/application/generate-search-result-schema-piece.php b/src/schema/application/generate-search-result-schema-piece.php new file mode 100644 index 00000000000..a8d8b367c4f --- /dev/null +++ b/src/schema/application/generate-search-result-schema-piece.php @@ -0,0 +1,20 @@ +search_term = new Search_Term( $search_term ); + } + + public function get_search_term() { + return $this->search_term; + } + +} From ba9e982c336ad0154b80f14d6118bdc4af1aadf7 Mon Sep 17 00:00:00 2001 From: Paolo Luigi Scala Date: Thu, 27 Oct 2022 14:33:44 +0200 Subject: [PATCH 08/15] Generate search result schema piece command handler --- .../generate-search-result-schema-piece-handler.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 src/schema/application/generate-search-result-schema-piece-handler.php diff --git a/src/schema/application/generate-search-result-schema-piece-handler.php b/src/schema/application/generate-search-result-schema-piece-handler.php new file mode 100644 index 00000000000..085eab45e63 --- /dev/null +++ b/src/schema/application/generate-search-result-schema-piece-handler.php @@ -0,0 +1,13 @@ +get_search_term() ); + } + +} From cdd5cade5c8d2b966b8647fa0163d556b377ba6a Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Thu, 27 Oct 2022 15:19:01 +0200 Subject: [PATCH 09/15] CS --- config/dependency-injection/loader-pass.php | 41 +++++++++---------- ...ate-search-result-schema-piece-handler.php | 17 ++++++-- .../generate-search-result-schema-piece.php | 19 ++++++++- .../domain/search-result-schema-piece.php | 35 +++++++++++++--- src/schema/domain/search-term.php | 18 +++++++- .../framework/search-result-integration.php | 27 ++++++++++-- 6 files changed, 121 insertions(+), 36 deletions(-) diff --git a/config/dependency-injection/loader-pass.php b/config/dependency-injection/loader-pass.php index cfe09d7a2e3..c0aa1354633 100644 --- a/config/dependency-injection/loader-pass.php +++ b/config/dependency-injection/loader-pass.php @@ -22,8 +22,7 @@ class Loader_Pass implements CompilerPassInterface { /** - * Checks all definitions to ensure all classes implementing the Integration interface - * are registered with the Loader class. + * Checks all definitions to ensure all classes implementing the Integration interface are registered with the Loader class. * * @param ContainerBuilder $container The container. */ @@ -56,28 +55,28 @@ private function process_definition( Definition $definition, Definition $loader_ try { $reflect = new ReflectionClass( $class ); $path = $reflect->getFileName(); - if( strpos($path, 'wordpress-seo/src/helpers') || - strpos($path, 'wordpress-seo/src/surfaces') || - strpos($path, 'wordpress-seo/src/actions') || - strpos($path, 'wordpress-seo/src/builders') || - strpos($path, 'wordpress-seo/src/config') || - strpos($path, 'wordpress-seo/src/generators') || - strpos($path, 'wordpress-seo/src/integrations') || - strpos($path, 'wordpress-seo/src/logger') || - strpos($path, 'wordpress-seo/src/loader') || - strpos($path, 'wordpress-seo/src/memoizers') || - strpos($path, 'wordpress-seo/src/presentations') || - strpos($path, 'wordpress-seo/src/repositories') || - strpos($path, 'wordpress-seo/src/services') || - strpos($path, 'wordpress-seo/src/schema-templates') || - strpos($path, 'wordpress-seo/src/wrappers') || - strpos($path, 'wordpress-seo/src/context') || - strpos($path, 'wordpress-seo/src/values') + if ( strpos( $path, 'wordpress-seo/src/helpers' ) + || strpos( $path, 'wordpress-seo/src/surfaces' ) + || strpos( $path, 'wordpress-seo/src/actions' ) + || strpos( $path, 'wordpress-seo/src/builders' ) + || strpos( $path, 'wordpress-seo/src/config' ) + || strpos( $path, 'wordpress-seo/src/generators' ) + || strpos( $path, 'wordpress-seo/src/integrations' ) + || strpos( $path, 'wordpress-seo/src/logger' ) + || strpos( $path, 'wordpress-seo/src/loader' ) + || strpos( $path, 'wordpress-seo/src/memoizers' ) + || strpos( $path, 'wordpress-seo/src/presentations' ) + || strpos( $path, 'wordpress-seo/src/repositories' ) + || strpos( $path, 'wordpress-seo/src/services' ) + || strpos( $path, 'wordpress-seo/src/schema-templates' ) + || strpos( $path, 'wordpress-seo/src/wrappers' ) + || strpos( $path, 'wordpress-seo/src/context' ) + || strpos( $path, 'wordpress-seo/src/values' ) ) { $definition->setPublic( true ); } - } catch ( \Exception $e){ - + } catch ( \Exception $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch + // Catch all for non-existing classes. } if ( is_subclass_of( $class, Conditional::class ) ) { diff --git a/src/schema/application/generate-search-result-schema-piece-handler.php b/src/schema/application/generate-search-result-schema-piece-handler.php index 085eab45e63..65b1da0ba70 100644 --- a/src/schema/application/generate-search-result-schema-piece-handler.php +++ b/src/schema/application/generate-search-result-schema-piece-handler.php @@ -1,13 +1,24 @@ get_search_term() ); + return new Search_Result_Schema_Piece( $command->get_search_term() ); } - } diff --git a/src/schema/application/generate-search-result-schema-piece.php b/src/schema/application/generate-search-result-schema-piece.php index a8d8b367c4f..31a9f30de3c 100644 --- a/src/schema/application/generate-search-result-schema-piece.php +++ b/src/schema/application/generate-search-result-schema-piece.php @@ -3,18 +3,33 @@ use Yoast\WP\SEO\Schema\Domain\Search_Term; +/** + * The action for search result schema + */ class Generate_Search_Result_Schema_Piece { + /** + * The search term domain object. + * + * @var \Yoast\WP\SEO\Schema\Domain\Search_Term $search_term The search term domain object. + */ private $search_term; + /** - * @param string $query The query. + * The search term. + * + * @param string $search_term The search term. */ public function __construct( $search_term ) { $this->search_term = new Search_Term( $search_term ); } + /** + * Gets the search term domain object. + * + * @return \Yoast\WP\SEO\Schema\Domain\Search_Term + */ public function get_search_term() { return $this->search_term; } - } diff --git a/src/schema/domain/search-result-schema-piece.php b/src/schema/domain/search-result-schema-piece.php index 20c1407e76c..ef4839f888e 100644 --- a/src/schema/domain/search-result-schema-piece.php +++ b/src/schema/domain/search-result-schema-piece.php @@ -4,26 +4,49 @@ use Yoast\WP\SEO\Generators\Schema\Abstract_Schema_Piece; use Yoast\WP\SEO\Schema\Domain\Search_Term; +/** + * Generates the SearchAction schema Piece + */ class Search_Result_Schema_Piece extends Abstract_Schema_Piece { + /** + * The search term domain object. + * + * @var \Yoast\WP\SEO\Schema\Domain\Search_Term $search_term + */ private $search_term; + /** + * Search_Result_Schema_Piece constructor. + * + * @param \Yoast\WP\SEO\Schema\Domain\Search_Term $search_term The search term domain object. + */ public function __construct( Search_Term $search_term ) { $this->search_term = $search_term; } + + /** + * Generates the data to be added to the schema graph. + * + * @return array The data to be added to the schema graph + */ public function generate() { - $data = [ - '@type' => "SearchAction", - 'actionStatus' => "https://schema.org/CompletedActionStatus", + $data = [ + '@type' => 'SearchAction', + 'actionStatus' => 'https://schema.org/CompletedActionStatus', 'query' => $this->search_term->get_query(), - 'result' => [ '@id' => $this->context->main_schema_id ] + 'result' => [ '@id' => $this->context->main_schema_id ], ]; return $data; } - public function is_needed() - { + /** + * Returns if the schema graph is needed. + * + * @return bool + */ + public function is_needed() { return true; } } diff --git a/src/schema/domain/search-term.php b/src/schema/domain/search-term.php index 26473adda26..12487f87a09 100644 --- a/src/schema/domain/search-term.php +++ b/src/schema/domain/search-term.php @@ -1,17 +1,33 @@ query = $query; } + /** + * Return the search query. + * + * @return string The search query. + */ public function get_query() { return $this->query; } -} \ No newline at end of file +} diff --git a/src/schema/framework/search-result-integration.php b/src/schema/framework/search-result-integration.php index d6ce79678ad..247c016dd76 100644 --- a/src/schema/framework/search-result-integration.php +++ b/src/schema/framework/search-result-integration.php @@ -9,9 +9,11 @@ use Yoast\WP\SEO\Helpers\Current_Page_Helper; use Yoast\WP\SEO\Conditionals\Front_End_Conditional; +/** + * Integrates the search action graph piece into the schema graph. + */ class Search_Result_Integration implements Integration_Interface { - /** * The current page helper. * @@ -19,6 +21,11 @@ class Search_Result_Integration implements Integration_Interface { */ private $current_page_helper; + /** + * The Generate_Search_Result_Schema handler. + * + * @var \Yoast\WP\SEO\Schema\Application\Generate_Search_Result_Schema_Piece_Handler + */ private $handler; /** @@ -28,10 +35,16 @@ public static function get_conditionals() { return [ Front_End_Conditional::class ]; } - public function __construct ( Current_Page_Helper $current_page_helper, + /** + * Search_Result_Integration constructor. + * + * @param \Yoast\WP\SEO\Helpers\Current_Page_Helper $current_page_helper The current page helper class. + * @param \Yoast\WP\SEO\Schema\Application\Generate_Search_Result_Schema_Piece_Handler $handler The generate command handler. + */ + public function __construct( Current_Page_Helper $current_page_helper, Generate_Search_Result_Schema_Piece_Handler $handler ) { $this->current_page_helper = $current_page_helper; - $this->handler = $handler; + $this->handler = $handler; } /** @@ -41,6 +54,14 @@ public function register_hooks() { \add_filter( 'wpseo_schema_graph_pieces', [ $this, 'add_search_result_schema_piece' ], 10, 2 ); } + /** + * Integrates a new Schema piece into the graph if we are on a search page. + * + * @param array $graph The current schema graph. + * @param array $context The schema context. + * + * @return mixed Returns the schema graph + */ public function add_search_result_schema_piece( $graph, $context ) { if ( $this->current_page_helper->is_search_result() ) { $graph[] = ( $this->handler )( new Generate_Search_Result_Schema_Piece( \get_search_query() ) ); From a93ac1d31263d2244bad4bf78d22e4894abcfb81 Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Thu, 27 Oct 2022 15:58:47 +0200 Subject: [PATCH 10/15] Remove invoke to make PHP 5.6 work. --- .../application/generate-search-result-schema-piece-handler.php | 2 +- src/schema/framework/search-result-integration.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/schema/application/generate-search-result-schema-piece-handler.php b/src/schema/application/generate-search-result-schema-piece-handler.php index 65b1da0ba70..599763ccf9d 100644 --- a/src/schema/application/generate-search-result-schema-piece-handler.php +++ b/src/schema/application/generate-search-result-schema-piece-handler.php @@ -18,7 +18,7 @@ class Generate_Search_Result_Schema_Piece_Handler { * * @return \Yoast\WP\SEO\Schema\Domain\Search_Result_Schema_Piece */ - public function __invoke( Generate_Search_Result_Schema_Piece $command ) { + public function handle( Generate_Search_Result_Schema_Piece $command ) { return new Search_Result_Schema_Piece( $command->get_search_term() ); } } diff --git a/src/schema/framework/search-result-integration.php b/src/schema/framework/search-result-integration.php index 247c016dd76..af805641eea 100644 --- a/src/schema/framework/search-result-integration.php +++ b/src/schema/framework/search-result-integration.php @@ -64,7 +64,7 @@ public function register_hooks() { */ public function add_search_result_schema_piece( $graph, $context ) { if ( $this->current_page_helper->is_search_result() ) { - $graph[] = ( $this->handler )( new Generate_Search_Result_Schema_Piece( \get_search_query() ) ); + $graph[] = $this->handler->handle( new Generate_Search_Result_Schema_Piece( \get_search_query() ) ); } return $graph; From fb540d6a12d5c5ddbd3ad53bcc34e868cf3dd93c Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Mon, 24 Apr 2023 11:44:30 +0200 Subject: [PATCH 11/15] Cleaning up the code. And added a way to mark a class as public. --- config/dependency-injection/loader-pass.php | 62 ++++++++++++++++--- ...ate-search-result-schema-piece-handler.php | 4 +- .../generate-search-result-schema-piece.php | 4 +- .../domain/search-result-schema-piece.php | 4 +- .../search-result-integration.php | 24 ++++--- 5 files changed, 72 insertions(+), 26 deletions(-) rename src/schema/{framework => user-interface}/search-result-integration.php (71%) diff --git a/config/dependency-injection/loader-pass.php b/config/dependency-injection/loader-pass.php index c0aa1354633..e5dd9b68943 100644 --- a/config/dependency-injection/loader-pass.php +++ b/config/dependency-injection/loader-pass.php @@ -56,22 +56,23 @@ private function process_definition( Definition $definition, Definition $loader_ $reflect = new ReflectionClass( $class ); $path = $reflect->getFileName(); if ( strpos( $path, 'wordpress-seo/src/helpers' ) - || strpos( $path, 'wordpress-seo/src/surfaces' ) || strpos( $path, 'wordpress-seo/src/actions' ) || strpos( $path, 'wordpress-seo/src/builders' ) || strpos( $path, 'wordpress-seo/src/config' ) + || strpos( $path, 'wordpress-seo/src/context' ) || strpos( $path, 'wordpress-seo/src/generators' ) + || strpos( $path, 'wordpress-seo/src/surfaces' ) || strpos( $path, 'wordpress-seo/src/integrations' ) - || strpos( $path, 'wordpress-seo/src/logger' ) - || strpos( $path, 'wordpress-seo/src/loader' ) + || strpos( $path, 'wordpress-seo/src/loggers' ) || strpos( $path, 'wordpress-seo/src/memoizers' ) + || strpos( $path, 'wordpress-seo/src/models' ) || strpos( $path, 'wordpress-seo/src/presentations' ) || strpos( $path, 'wordpress-seo/src/repositories' ) || strpos( $path, 'wordpress-seo/src/services' ) - || strpos( $path, 'wordpress-seo/src/schema-templates' ) - || strpos( $path, 'wordpress-seo/src/wrappers' ) - || strpos( $path, 'wordpress-seo/src/context' ) || strpos( $path, 'wordpress-seo/src/values' ) + || strpos( $path, 'wordpress-seo/src/wrappers' ) + || strpos( $path, 'wordpress-seo/src/wordpress' ) + || strpos( $path, 'wordpress-seo/src/loader' ) ) { $definition->setPublic( true ); } @@ -79,6 +80,10 @@ private function process_definition( Definition $definition, Definition $loader_ // Catch all for non-existing classes. } + if ( $this->should_make_public( $definition ) ) { + $definition->setPublic( true ); + } + if ( is_subclass_of( $class, Conditional::class ) ) { $definition->setPublic( true ); } @@ -104,7 +109,7 @@ private function process_definition( Definition $definition, Definition $loader_ } if ( \is_subclass_of( $class, Migration::class ) ) { - $reflect = new ReflectionClass( $class ); + $reflect = new \ReflectionClass( $class ); $path = $reflect->getFileName(); $file = \basename( $path, '.php' ); $version = \explode( '_', $file )[0]; @@ -113,4 +118,47 @@ private function process_definition( Definition $definition, Definition $loader_ $definition->setPublic( true ); } } + + /** + * Checks if a class should be public. A class can be made public in the dependency injection by adding the + * `@makePublic` annotation in the doc block. + * + * @param Definition $definition The definition to make public. + * @return bool + */ + private function should_make_public( Definition $definition ): bool { + $doc_comment = $this->get_method_doc_block( $definition ); + + if ( empty( $doc_comment ) ) { + // If there is no doc comment, assume we should autowire. + return false; + } + + return strpos( $doc_comment, '* @makePublic' ) !== false; + } + + /** + * Retrieves the doc block comment for the given definition. + * + * @param Definition $definition The definition to parse. + * + * @return string The doc block. + */ + private function get_method_doc_block( Definition $definition ) { + $classname = $definition->getClass(); + try { + $reflection_class = new \ReflectionClass( $classname ); + } catch ( \ReflectionException $exception ) { + return ''; + } + + /** + * The DocComment for the class we're reflecting. + * + * @var string|false $doc_comment + */ + $doc_comment = $reflection_class->getDocComment(); + + return ( $doc_comment === false ) ? '' : $doc_comment; + } } diff --git a/src/schema/application/generate-search-result-schema-piece-handler.php b/src/schema/application/generate-search-result-schema-piece-handler.php index 599763ccf9d..0ac000266b4 100644 --- a/src/schema/application/generate-search-result-schema-piece-handler.php +++ b/src/schema/application/generate-search-result-schema-piece-handler.php @@ -14,9 +14,9 @@ class Generate_Search_Result_Schema_Piece_Handler { /** * Invokes the generation of the Schema Piece * - * @param \Yoast\WP\SEO\Schema\Application\Generate_Search_Result_Schema_Piece $command The command. + * @param Generate_Search_Result_Schema_Piece $command The command. * - * @return \Yoast\WP\SEO\Schema\Domain\Search_Result_Schema_Piece + * @return Search_Result_Schema_Piece */ public function handle( Generate_Search_Result_Schema_Piece $command ) { return new Search_Result_Schema_Piece( $command->get_search_term() ); diff --git a/src/schema/application/generate-search-result-schema-piece.php b/src/schema/application/generate-search-result-schema-piece.php index 31a9f30de3c..853843a45c3 100644 --- a/src/schema/application/generate-search-result-schema-piece.php +++ b/src/schema/application/generate-search-result-schema-piece.php @@ -11,7 +11,7 @@ class Generate_Search_Result_Schema_Piece { /** * The search term domain object. * - * @var \Yoast\WP\SEO\Schema\Domain\Search_Term $search_term The search term domain object. + * @var Search_Term $search_term The search term domain object. */ private $search_term; @@ -27,7 +27,7 @@ public function __construct( $search_term ) { /** * Gets the search term domain object. * - * @return \Yoast\WP\SEO\Schema\Domain\Search_Term + * @return Search_Term */ public function get_search_term() { return $this->search_term; diff --git a/src/schema/domain/search-result-schema-piece.php b/src/schema/domain/search-result-schema-piece.php index ef4839f888e..d6808676486 100644 --- a/src/schema/domain/search-result-schema-piece.php +++ b/src/schema/domain/search-result-schema-piece.php @@ -12,14 +12,14 @@ class Search_Result_Schema_Piece extends Abstract_Schema_Piece { /** * The search term domain object. * - * @var \Yoast\WP\SEO\Schema\Domain\Search_Term $search_term + * @var Search_Term $search_term */ private $search_term; /** * Search_Result_Schema_Piece constructor. * - * @param \Yoast\WP\SEO\Schema\Domain\Search_Term $search_term The search term domain object. + * @param Search_Term $search_term The search term domain object. */ public function __construct( Search_Term $search_term ) { $this->search_term = $search_term; diff --git a/src/schema/framework/search-result-integration.php b/src/schema/user-interface/search-result-integration.php similarity index 71% rename from src/schema/framework/search-result-integration.php rename to src/schema/user-interface/search-result-integration.php index af805641eea..0367223d48d 100644 --- a/src/schema/framework/search-result-integration.php +++ b/src/schema/user-interface/search-result-integration.php @@ -1,13 +1,12 @@ current_page_helper = $current_page_helper; $this->handler = $handler; } @@ -57,8 +55,8 @@ public function register_hooks() { /** * Integrates a new Schema piece into the graph if we are on a search page. * - * @param array $graph The current schema graph. - * @param array $context The schema context. + * @param array $graph The current schema graph. + * @param array $context The schema context. * * @return mixed Returns the schema graph */ From f76d857d312162e325b6bf97181adfc54ecd5e9c Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Mon, 24 Apr 2023 15:46:14 +0200 Subject: [PATCH 12/15] Add tests --- config/dependency-injection/loader-pass.php | 6 +- ...earch-result-schema-piece-handler-test.php | 50 +++++++ ...nerate-search-result-schema-piece-test.php | 47 +++++++ .../search-result-schema-piece-test.php | 72 ++++++++++ tests/unit/schema/domain/search-term-test.php | 46 +++++++ .../search-result-integration-test.php | 125 ++++++++++++++++++ 6 files changed, 343 insertions(+), 3 deletions(-) create mode 100644 tests/unit/schema/application/generate-search-result-schema-piece-handler-test.php create mode 100644 tests/unit/schema/application/generate-search-result-schema-piece-test.php create mode 100644 tests/unit/schema/domain/search-result-schema-piece-test.php create mode 100644 tests/unit/schema/domain/search-term-test.php create mode 100644 tests/unit/schema/user-interface/search-result-integration-test.php diff --git a/config/dependency-injection/loader-pass.php b/config/dependency-injection/loader-pass.php index e5dd9b68943..0738aa2ff28 100644 --- a/config/dependency-injection/loader-pass.php +++ b/config/dependency-injection/loader-pass.php @@ -70,9 +70,9 @@ private function process_definition( Definition $definition, Definition $loader_ || strpos( $path, 'wordpress-seo/src/repositories' ) || strpos( $path, 'wordpress-seo/src/services' ) || strpos( $path, 'wordpress-seo/src/values' ) - || strpos( $path, 'wordpress-seo/src/wrappers' ) - || strpos( $path, 'wordpress-seo/src/wordpress' ) - || strpos( $path, 'wordpress-seo/src/loader' ) + || strpos( $path, 'wordpress-seo/src/wrappers' ) + || strpos( $path, 'wordpress-seo/src/wordpress' ) + || strpos( $path, 'wordpress-seo/src/loader' ) ) { $definition->setPublic( true ); } diff --git a/tests/unit/schema/application/generate-search-result-schema-piece-handler-test.php b/tests/unit/schema/application/generate-search-result-schema-piece-handler-test.php new file mode 100644 index 00000000000..3246717c631 --- /dev/null +++ b/tests/unit/schema/application/generate-search-result-schema-piece-handler-test.php @@ -0,0 +1,50 @@ +instance = new Generate_Search_Result_Schema_Piece_Handler(); + } + + /** + * Tests the handle method. + * + * @covers ::handle + * @return void + */ + public function test_handle(): void { + $this->assertInstanceOf( Search_Result_Schema_Piece::class, $this->instance->handle( new Generate_Search_Result_Schema_Piece( 'the term.' ) ) ); + } +} diff --git a/tests/unit/schema/application/generate-search-result-schema-piece-test.php b/tests/unit/schema/application/generate-search-result-schema-piece-test.php new file mode 100644 index 00000000000..be1bf8a5558 --- /dev/null +++ b/tests/unit/schema/application/generate-search-result-schema-piece-test.php @@ -0,0 +1,47 @@ +instance = new Generate_Search_Result_Schema_Piece( 'My search term.' ); + } + + /** + * Tests if the passed variable ends up as the search term. + * + * @covers ::get_search_term + * @return void + */ + public function test_get_search_term(): void { + $this->assertSame( 'My search term.', $this->instance->get_search_term()->get_query() ); + } +} diff --git a/tests/unit/schema/domain/search-result-schema-piece-test.php b/tests/unit/schema/domain/search-result-schema-piece-test.php new file mode 100644 index 00000000000..f0078632943 --- /dev/null +++ b/tests/unit/schema/domain/search-result-schema-piece-test.php @@ -0,0 +1,72 @@ +instance = new Search_Result_Schema_Piece( $search_term ); + $this->instance->context = $context_mock; + } + + /** + * Tests the is needed function, this should always return true for this schema piece. + * + * @covers ::is_needed + * @return void + */ + public function test_is_needed(): void { + $this->assertTrue( $this->instance->is_needed() ); + } + + /** + * Tests the generate function. + * + * @covers ::generate + * @return void + */ + public function test_generate() { + $this->assertSame( + [ + '@type' => 'SearchAction', + 'actionStatus' => 'https://schema.org/CompletedActionStatus', + 'query' => 'the_query', + 'result' => [ + '@id' => null, + ], + ], + $this->instance->generate() + ); + } +} diff --git a/tests/unit/schema/domain/search-term-test.php b/tests/unit/schema/domain/search-term-test.php new file mode 100644 index 00000000000..4a45d03ed38 --- /dev/null +++ b/tests/unit/schema/domain/search-term-test.php @@ -0,0 +1,46 @@ +instance = new Search_Term( 'the_query' ); + } + + /** + * Tests the get query method. + * + * @covers ::get_query + * @return void + */ + public function test_get_query(): void { + $this->assertSame( 'the_query', $this->instance->get_query() ); + } +} diff --git a/tests/unit/schema/user-interface/search-result-integration-test.php b/tests/unit/schema/user-interface/search-result-integration-test.php new file mode 100644 index 00000000000..1c1e2fd212d --- /dev/null +++ b/tests/unit/schema/user-interface/search-result-integration-test.php @@ -0,0 +1,125 @@ +current_page_helper = Mockery::mock( Current_Page_Helper::class ); + $this->search_piece_handler = Mockery::mock( Generate_Search_Result_Schema_Piece_Handler::class ); + + $this->instance = new Search_Result_Integration( $this->current_page_helper, $this->search_piece_handler ); + } + + /** + * Tests the register hooks function. + * + * @covers ::register_hooks + * @return void + */ + public function test_register_hooks(): void { + $this->instance->register_hooks(); + + $this->assertNotFalse( + \has_filter( + 'wpseo_schema_graph_pieces', + [ + $this->instance, + 'add_search_result_schema_piece', + ] + ) + ); + } + + /** + * Tests the get conditionals function. + * + * @covers ::get_conditionals + * @return void + */ + public function test_get_conditionals(): void { + $this->assertSame( [ Front_End_Conditional::class ], Search_Result_Integration::get_conditionals() ); + } + + /** + * Tests the add_search_result_schema_piece function. + * + * @covers ::add_search_result_schema_piece + * + * @return void + */ + public function test_add_search_result_schema_piece(): void { + $this->current_page_helper->expects( 'is_search_result' )->andReturnTrue(); + $this->search_piece_handler->expects( 'handle' )->andReturn( new Search_Result_Schema_Piece( new Search_Term( 'the_query' ) ) ); + + Monkey\Functions\expect( 'get_search_query' ) + ->once() + ->andReturn( 'the_query' ); + $result = $this->instance->add_search_result_schema_piece( [], [] ); + + + $this->assertInstanceOf( Search_Result_Schema_Piece::class, $result[0] ); + } + + /** + * Tests the add_search_result_schema_piece function. + * + * @covers ::add_search_result_schema_piece + * + * @return void + */ + public function test_add_search_result_schema_piece_not_search_page(): void { + $this->current_page_helper->expects( 'is_search_result' )->andReturnFalse(); + $this->search_piece_handler->expects( 'handle' )->never(); + + Monkey\Functions\expect( 'get_search_query' )->never(); + $this->instance->add_search_result_schema_piece( [], [] ); + } +} From 28329cabda2c436bf1e5aafb26279bd753f517cb Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Tue, 25 Apr 2023 10:12:57 +0200 Subject: [PATCH 13/15] Unit test update. --- ...nerate-search-result-schema-piece-test.php | 23 +++---------------- .../search-result-schema-piece-test.php | 3 ++- tests/unit/schema/domain/search-term-test.php | 22 +++--------------- .../search-result-integration-test.php | 3 ++- 4 files changed, 10 insertions(+), 41 deletions(-) diff --git a/tests/unit/schema/application/generate-search-result-schema-piece-test.php b/tests/unit/schema/application/generate-search-result-schema-piece-test.php index be1bf8a5558..256109d4c4f 100644 --- a/tests/unit/schema/application/generate-search-result-schema-piece-test.php +++ b/tests/unit/schema/application/generate-search-result-schema-piece-test.php @@ -3,7 +3,6 @@ namespace Yoast\WP\SEO\Tests\Unit\Schema\Application; use Yoast\WP\SEO\Schema\Application\Generate_Search_Result_Schema_Piece; -use Yoast\WP\SEO\Schema\Domain\Search_Result_Schema_Piece; use Yoast\WP\SEO\Tests\Unit\TestCase; @@ -17,31 +16,15 @@ */ class Generate_Search_Result_Schema_Piece_Test extends TestCase { - /** - * The subject of testing. - * - * @var Generate_Search_Result_Schema_Piece $instance - */ - private $instance; - - /** - * The setup function. - * - * @return void - */ - protected function setUp(): void { - parent::setUp(); - - $this->instance = new Generate_Search_Result_Schema_Piece( 'My search term.' ); - } - /** * Tests if the passed variable ends up as the search term. * * @covers ::get_search_term + * @covers ::__construct * @return void */ public function test_get_search_term(): void { - $this->assertSame( 'My search term.', $this->instance->get_search_term()->get_query() ); + $instance = new Generate_Search_Result_Schema_Piece( 'My search term.' ); + $this->assertSame( 'My search term.', $instance->get_search_term()->get_query() ); } } diff --git a/tests/unit/schema/domain/search-result-schema-piece-test.php b/tests/unit/schema/domain/search-result-schema-piece-test.php index f0078632943..c778402698e 100644 --- a/tests/unit/schema/domain/search-result-schema-piece-test.php +++ b/tests/unit/schema/domain/search-result-schema-piece-test.php @@ -27,7 +27,7 @@ class Search_Result_Schema_Piece_Test extends TestCase { /** * The setup function. - * + * @return void */ protected function setUp(): void { @@ -43,6 +43,7 @@ protected function setUp(): void { /** * Tests the is needed function, this should always return true for this schema piece. * + * @covers ::__construct * @covers ::is_needed * @return void */ diff --git a/tests/unit/schema/domain/search-term-test.php b/tests/unit/schema/domain/search-term-test.php index 4a45d03ed38..c7501bca8cc 100644 --- a/tests/unit/schema/domain/search-term-test.php +++ b/tests/unit/schema/domain/search-term-test.php @@ -16,31 +16,15 @@ */ class Search_Term_Test extends TestCase { - /** - * The subject of testing. - * - * @var Search_Term $instance - */ - private $instance; - - /** - * The setup function. - * - * @return void - */ - protected function setUp(): void { - parent::setUp(); - - $this->instance = new Search_Term( 'the_query' ); - } - /** * Tests the get query method. * * @covers ::get_query + * @covers ::__construct * @return void */ public function test_get_query(): void { - $this->assertSame( 'the_query', $this->instance->get_query() ); + $instance = new Search_Term( 'the_query' ); + $this->assertSame( 'the_query', $instance->get_query() ); } } diff --git a/tests/unit/schema/user-interface/search-result-integration-test.php b/tests/unit/schema/user-interface/search-result-integration-test.php index 1c1e2fd212d..02532015d88 100644 --- a/tests/unit/schema/user-interface/search-result-integration-test.php +++ b/tests/unit/schema/user-interface/search-result-integration-test.php @@ -46,7 +46,7 @@ class Search_Result_Integration_Test extends TestCase { /** * The setup function. - * + * @return void */ protected function setUp(): void { @@ -62,6 +62,7 @@ protected function setUp(): void { * Tests the register hooks function. * * @covers ::register_hooks + * @covers ::__construct * @return void */ public function test_register_hooks(): void { From a819d27799d48de2542a24c5ae5a45f5cc409f28 Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Tue, 25 Apr 2023 12:35:37 +0200 Subject: [PATCH 14/15] Update collectors to be public. --- src/analytics/application/missing-indexables-collector.php | 1 + src/analytics/application/to-be-cleaned-indexables-collector.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/analytics/application/missing-indexables-collector.php b/src/analytics/application/missing-indexables-collector.php index fc0b6d43d3e..88704e38037 100644 --- a/src/analytics/application/missing-indexables-collector.php +++ b/src/analytics/application/missing-indexables-collector.php @@ -9,6 +9,7 @@ /** * Manages the collection of the missing indexable data. + * @makePublic */ class Missing_Indexables_Collector implements WPSEO_Collection { diff --git a/src/analytics/application/to-be-cleaned-indexables-collector.php b/src/analytics/application/to-be-cleaned-indexables-collector.php index 561d41dafc7..19d1f4a7f4f 100644 --- a/src/analytics/application/to-be-cleaned-indexables-collector.php +++ b/src/analytics/application/to-be-cleaned-indexables-collector.php @@ -9,6 +9,7 @@ /** * Collects data about to-be-cleaned indexables. + * @makePublic */ class To_Be_Cleaned_Indexables_Collector implements WPSEO_Collection { From f806491b326c1c1703ae8f0f4967467dfbc519b9 Mon Sep 17 00:00:00 2001 From: Thijs van der Heijden Date: Thu, 25 May 2023 14:09:40 +0200 Subject: [PATCH 15/15] cs --- src/analytics/application/missing-indexables-collector.php | 1 + src/analytics/application/to-be-cleaned-indexables-collector.php | 1 + 2 files changed, 2 insertions(+) diff --git a/src/analytics/application/missing-indexables-collector.php b/src/analytics/application/missing-indexables-collector.php index 33465e17ba6..b0ef9ab81d7 100644 --- a/src/analytics/application/missing-indexables-collector.php +++ b/src/analytics/application/missing-indexables-collector.php @@ -9,6 +9,7 @@ /** * Manages the collection of the missing indexable data. + * * @makePublic */ class Missing_Indexables_Collector implements WPSEO_Collection { diff --git a/src/analytics/application/to-be-cleaned-indexables-collector.php b/src/analytics/application/to-be-cleaned-indexables-collector.php index 21299f81f3a..4692d57baad 100644 --- a/src/analytics/application/to-be-cleaned-indexables-collector.php +++ b/src/analytics/application/to-be-cleaned-indexables-collector.php @@ -9,6 +9,7 @@ /** * Collects data about to-be-cleaned indexables. + * * @makePublic */ class To_Be_Cleaned_Indexables_Collector implements WPSEO_Collection {