-
Notifications
You must be signed in to change notification settings - Fork 893
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20074 from Yoast/feature/di-improvements
- Loading branch information
Showing
14 changed files
with
607 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/schema/application/generate-search-result-schema-piece-handler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Yoast\WP\SEO\Schema\Application; | ||
|
||
use Yoast\WP\SEO\Schema\Domain\Search_Result_Schema_Piece; | ||
|
||
/** | ||
* Handles the Generate_Search_Result_Schema_Piece action | ||
* | ||
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded | ||
*/ | ||
class Generate_Search_Result_Schema_Piece_Handler { | ||
|
||
/** | ||
* Invokes the generation of the Schema Piece | ||
* | ||
* @param Generate_Search_Result_Schema_Piece $command The command. | ||
* | ||
* @return Search_Result_Schema_Piece | ||
*/ | ||
public function handle( Generate_Search_Result_Schema_Piece $command ) { | ||
return new Search_Result_Schema_Piece( $command->get_search_term() ); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/schema/application/generate-search-result-schema-piece.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
namespace Yoast\WP\SEO\Schema\Application; | ||
|
||
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 Search_Term $search_term The search term domain object. | ||
*/ | ||
private $search_term; | ||
|
||
/** | ||
* 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 Search_Term | ||
*/ | ||
public function get_search_term() { | ||
return $this->search_term; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace Yoast\WP\SEO\Schema\Domain; | ||
|
||
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 Search_Term $search_term | ||
*/ | ||
private $search_term; | ||
|
||
/** | ||
* Search_Result_Schema_Piece constructor. | ||
* | ||
* @param 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', | ||
'query' => $this->search_term->get_query(), | ||
'result' => [ '@id' => $this->context->main_schema_id ], | ||
]; | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Returns if the schema graph is needed. | ||
* | ||
* @return bool | ||
*/ | ||
public function is_needed() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
namespace Yoast\WP\SEO\Schema\Domain; | ||
|
||
/** | ||
* The search term domain object. | ||
*/ | ||
class Search_Term { | ||
|
||
/** | ||
* The search query. | ||
* | ||
* @var string $query | ||
*/ | ||
private $query; | ||
|
||
/** | ||
* Search Term constructor. | ||
* | ||
* @param string $query The query. | ||
*/ | ||
public function __construct( $query ) { | ||
$this->query = $query; | ||
} | ||
|
||
/** | ||
* Return the search query. | ||
* | ||
* @return string The search query. | ||
*/ | ||
public function get_query() { | ||
return $this->query; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace Yoast\WP\SEO\Schema\User_Interface; | ||
|
||
use Yoast\WP\SEO\Conditionals\Front_End_Conditional; | ||
use Yoast\WP\SEO\Helpers\Current_Page_Helper; | ||
use Yoast\WP\SEO\Integrations\Integration_Interface; | ||
use Yoast\WP\SEO\Schema\Application\Generate_Search_Result_Schema_Piece; | ||
use Yoast\WP\SEO\Schema\Application\Generate_Search_Result_Schema_Piece_Handler; | ||
|
||
/** | ||
* Integrates the search action graph piece into the schema graph. | ||
*/ | ||
class Search_Result_Integration implements Integration_Interface { | ||
|
||
/** | ||
* The current page helper. | ||
* | ||
* @var Current_Page_Helper | ||
*/ | ||
private $current_page_helper; | ||
|
||
/** | ||
* The Generate_Search_Result_Schema handler. | ||
* | ||
* @var Generate_Search_Result_Schema_Piece_Handler | ||
*/ | ||
private $handler; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public static function get_conditionals() { | ||
return [ Front_End_Conditional::class ]; | ||
} | ||
|
||
/** | ||
* Search_Result_Integration constructor. | ||
* | ||
* @param Current_Page_Helper $current_page_helper The current page helper class. | ||
* @param 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; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
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->handle( new Generate_Search_Result_Schema_Piece( \get_search_query() ) ); | ||
} | ||
|
||
return $graph; | ||
} | ||
} | ||
|
Oops, something went wrong.