Skip to content

Commit

Permalink
Supply frontend with endpoints and nonce
Browse files Browse the repository at this point in the history
* 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
igorschoester committed Nov 25, 2024
1 parent d5b1a13 commit c5f6c46
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Yoast\WP\SEO\Dashboard\Application\Configuration;

use Yoast\WP\SEO\Dashboard\Application\Content_Types\Content_Types_Repository;
use Yoast\WP\SEO\Dashboard\Application\Endpoints\Endpoints_Repository;
use Yoast\WP\SEO\Dashboard\Infrastructure\Nonces\Nonce_Repository;
use Yoast\WP\SEO\Editors\Application\Analysis_Features\Enabled_Analysis_Features_Repository;
use Yoast\WP\SEO\Editors\Framework\Keyphrase_Analysis;
use Yoast\WP\SEO\Editors\Framework\Readability_Analysis;
Expand Down Expand Up @@ -44,6 +46,20 @@ class Dashboard_Configuration {
*/
private $enabled_analysis_features_repository;

/**
* The endpoints repository.
*
* @var Endpoints_Repository
*/
private $endpoints_repository;

/**
* The nonce repository.
*
* @var Nonce_Repository
*/
private $nonce_repository;

/**
* The constructor.
*
Expand All @@ -53,17 +69,23 @@ class Dashboard_Configuration {
* @param User_Helper $user_helper The user helper.
* @param Enabled_Analysis_Features_Repository $enabled_analysis_features_repository The analysis feature
* repository.
* @param Endpoints_Repository $endpoints_repository The endpoints repository.
* @param Nonce_Repository $nonce_repository The nonce repository.
*/
public function __construct(
Content_Types_Repository $content_types_repository,
Indexable_Helper $indexable_helper,
User_Helper $user_helper,
Enabled_Analysis_Features_Repository $enabled_analysis_features_repository
Enabled_Analysis_Features_Repository $enabled_analysis_features_repository,
Endpoints_Repository $endpoints_repository,
Nonce_Repository $nonce_repository
) {
$this->content_types_repository = $content_types_repository;
$this->indexable_helper = $indexable_helper;
$this->user_helper = $user_helper;
$this->enabled_analysis_features_repository = $enabled_analysis_features_repository;
$this->endpoints_repository = $endpoints_repository;
$this->nonce_repository = $nonce_repository;
}

/**
Expand All @@ -82,6 +104,8 @@ public function get_configuration(): array {
Keyphrase_Analysis::NAME,
]
)->to_array(),
'endpoints' => $this->endpoints_repository->get_all_endpoints()->to_array(),
'nonce' => $this->nonce_repository->get_rest_nonce(),
];
}
}
42 changes: 42 additions & 0 deletions src/dashboard/application/endpoints/endpoints-repository.php
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;
}
}
34 changes: 34 additions & 0 deletions src/dashboard/domain/endpoint/endpoint-interface.php
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;
}
41 changes: 41 additions & 0 deletions src/dashboard/domain/endpoint/endpoint-list.php
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;
}
}
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 src/dashboard/infrastructure/endpoints/seo-scores-endpoint.php
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() );
}
}
18 changes: 18 additions & 0 deletions src/dashboard/infrastructure/nonces/nonce-repository.php
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' );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ abstract class Abstract_Scores_Route implements Route_Interface {

use No_Conditionals;

/**
* The namespace of the rout.
*
* @var string
*/
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;

/**
* The prefix of the rout.
*
Expand Down Expand Up @@ -116,7 +123,7 @@ public function get_route_prefix() {
*/
public function register_routes() {
\register_rest_route(
Main::API_V1_NAMESPACE,
self::ROUTE_NAMESPACE,
$this->get_route_prefix(),
[
[
Expand Down

0 comments on commit c5f6c46

Please sign in to comment.