-
Notifications
You must be signed in to change notification settings - Fork 901
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Supply frontend with endpoints and nonce
* the UI needs to fetch the scores for SEO and readability * those routes are protected by a nonce we need to provide * introduce endpoint interface -- apply that to the seo scores and readability scores * refactor the routes slightly to add the namespace * lazier approach with the nonce # Conflicts: # src/dashboard/user-interface/scores/abstract-scores-route.php # src/dashboard/user-interface/scores/readability-scores-route.php # src/dashboard/user-interface/scores/seo-scores-route.php
- Loading branch information
1 parent
d5b1a13
commit c5f6c46
Showing
8 changed files
with
264 additions
and
2 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
42 changes: 42 additions & 0 deletions
42
src/dashboard/application/endpoints/endpoints-repository.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,42 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Application\Endpoints; | ||
|
||
use Yoast\WP\SEO\Dashboard\Domain\Endpoint\Endpoint_Interface; | ||
use Yoast\WP\SEO\Dashboard\Domain\Endpoint\Endpoint_List; | ||
|
||
/** | ||
* Repository for endpoints. | ||
*/ | ||
class Endpoints_Repository { | ||
|
||
/** | ||
* Holds the endpoints. | ||
* | ||
* @var array<Endpoint_Interface> | ||
*/ | ||
private $endpoints; | ||
|
||
/** | ||
* Constructs the repository. | ||
* | ||
* @param Endpoint_Interface ...$endpoints The endpoints to add to the repository. | ||
*/ | ||
public function __construct( Endpoint_Interface ...$endpoints ) { | ||
$this->endpoints = $endpoints; | ||
} | ||
|
||
/** | ||
* Creates a list with all endpoints. | ||
* | ||
* @return Endpoint_List The list with all endpoints. | ||
*/ | ||
public function get_all_endpoints(): Endpoint_List { | ||
$list = new Endpoint_List(); | ||
foreach ( $this->endpoints as $endpoint ) { | ||
$list->add_endpoint( $endpoint ); | ||
} | ||
|
||
return $list; | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Domain\Endpoint; | ||
|
||
interface Endpoint_Interface { | ||
|
||
/** | ||
* Gets the name. | ||
* | ||
* @return string | ||
*/ | ||
public function get_name(): string; | ||
|
||
/** | ||
* Gets the namespace. | ||
* | ||
* @return string | ||
*/ | ||
public function get_namespace(): string; | ||
|
||
/** | ||
* Gets the route. | ||
* | ||
* @return string | ||
*/ | ||
public function get_route(): string; | ||
|
||
/** | ||
* Gets the URL. | ||
* | ||
* @return string | ||
*/ | ||
public function get_url(): string; | ||
} |
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,41 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Domain\Endpoint; | ||
|
||
/** | ||
* List of endpoints. | ||
*/ | ||
class Endpoint_List { | ||
|
||
/** | ||
* Holds the endpoints. | ||
* | ||
* @var array<Endpoint_Interface> $endpoints | ||
*/ | ||
private $endpoints = []; | ||
|
||
/** | ||
* Adds an endpoint to the list. | ||
* | ||
* @param Endpoint_Interface $endpoint An endpoint. | ||
* | ||
* @return void | ||
*/ | ||
public function add_endpoint( Endpoint_Interface $endpoint ): void { | ||
$this->endpoints[] = $endpoint; | ||
} | ||
|
||
/** | ||
* Converts the list to an array. | ||
* | ||
* @return array<string,string> The array of endpoints. | ||
*/ | ||
public function to_array(): array { | ||
$result = []; | ||
foreach ( $this->endpoints as $endpoint ) { | ||
$result[ $endpoint->get_name() ] = $endpoint->get_url(); | ||
} | ||
|
||
return $result; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/dashboard/infrastructure/endpoints/readability-scores-endpoint.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,48 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Endpoints; | ||
|
||
use Yoast\WP\SEO\Dashboard\Domain\Endpoint\Endpoint_Interface; | ||
use Yoast\WP\SEO\Dashboard\User_Interface\Scores\Readability_Scores_Route; | ||
|
||
/** | ||
* Represents the readability scores endpoint. | ||
*/ | ||
class Readability_Scores_Endpoint implements Endpoint_Interface { | ||
|
||
/** | ||
* Gets the name. | ||
* | ||
* @return string | ||
*/ | ||
public function get_name(): string { | ||
return 'readabilityScores'; | ||
} | ||
|
||
/** | ||
* Gets the namespace. | ||
* | ||
* @return string | ||
*/ | ||
public function get_namespace(): string { | ||
return Readability_Scores_Route::ROUTE_NAMESPACE; | ||
} | ||
|
||
/** | ||
* Gets the route. | ||
* | ||
* @return string | ||
*/ | ||
public function get_route(): string { | ||
return Readability_Scores_Route::ROUTE_PREFIX; | ||
} | ||
|
||
/** | ||
* Gets the URL. | ||
* | ||
* @return string | ||
*/ | ||
public function get_url(): string { | ||
return \rest_url( $this->get_namespace() . $this->get_route() ); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/dashboard/infrastructure/endpoints/seo-scores-endpoint.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,48 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Endpoints; | ||
|
||
use Yoast\WP\SEO\Dashboard\Domain\Endpoint\Endpoint_Interface; | ||
use Yoast\WP\SEO\Dashboard\User_Interface\Scores\SEO_Scores_Route; | ||
|
||
/** | ||
* Represents the SEO scores endpoint. | ||
*/ | ||
class Seo_Scores_Endpoint implements Endpoint_Interface { | ||
|
||
/** | ||
* Gets the name. | ||
* | ||
* @return string | ||
*/ | ||
public function get_name(): string { | ||
return 'seoScores'; | ||
} | ||
|
||
/** | ||
* Gets the namespace. | ||
* | ||
* @return string | ||
*/ | ||
public function get_namespace(): string { | ||
return Seo_Scores_Route::ROUTE_NAMESPACE; | ||
} | ||
|
||
/** | ||
* Gets the route. | ||
* | ||
* @return string | ||
*/ | ||
public function get_route(): string { | ||
return Seo_Scores_Route::ROUTE_PREFIX; | ||
} | ||
|
||
/** | ||
* Gets the URL. | ||
* | ||
* @return string | ||
*/ | ||
public function get_url(): string { | ||
return \rest_url( $this->get_namespace() . $this->get_route() ); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. | ||
namespace Yoast\WP\SEO\Dashboard\Infrastructure\Nonces; | ||
|
||
/** | ||
* Repository for WP nonces. | ||
*/ | ||
class Nonce_Repository { | ||
|
||
/** | ||
* Creates the nonce for a WP REST request. | ||
* | ||
* @return string | ||
*/ | ||
public function get_rest_nonce(): string { | ||
return \wp_create_nonce( 'wp_rest' ); | ||
} | ||
} |
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