first commit
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Routes;
|
||||
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Main;
|
||||
use Yoast\WP\SEO\Premium\Actions\Link_Suggestions_Action;
|
||||
|
||||
/**
|
||||
* Registers the route for the link suggestions retrieval.
|
||||
*
|
||||
* @package Yoast\WP\SEO\Routes
|
||||
*/
|
||||
class Link_Suggestions_Route implements Route_Interface {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* Represents the endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ENDPOINT_QUERY = 'link_suggestions';
|
||||
|
||||
/**
|
||||
* Instance of the Link_Suggestions_Action.
|
||||
*
|
||||
* @var Link_Suggestions_Action
|
||||
*/
|
||||
protected $link_suggestions_action;
|
||||
|
||||
/**
|
||||
* Link_Suggestions_Route constructor.
|
||||
*
|
||||
* @param Link_Suggestions_Action $link_suggestions_action The action to handle the requests to the endpoint.
|
||||
*/
|
||||
public function __construct( Link_Suggestions_Action $link_suggestions_action ) {
|
||||
$this->link_suggestions_action = $link_suggestions_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers routes with WordPress.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_routes() {
|
||||
$route_args = [
|
||||
'methods' => 'GET',
|
||||
'args' => [
|
||||
'prominent_words' => [
|
||||
'required' => true,
|
||||
'type' => 'object',
|
||||
'description' => 'Stems of prominent words and their term frequencies we want link suggestions based on',
|
||||
],
|
||||
'object_id' => [
|
||||
'required' => true,
|
||||
'type' => 'integer',
|
||||
'description' => 'The object id of the current indexable.',
|
||||
],
|
||||
'object_type' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => 'The object type of the current indexable.',
|
||||
],
|
||||
'limit' => [
|
||||
'required' => false,
|
||||
'default' => 5,
|
||||
'type' => 'integer',
|
||||
'description' => 'The maximum number of link suggestions to retrieve',
|
||||
],
|
||||
],
|
||||
'callback' => [ $this, 'run_get_suggestions_action' ],
|
||||
'permission_callback' => [ $this, 'can_retrieve_data' ],
|
||||
];
|
||||
\register_rest_route( Main::API_V1_NAMESPACE, self::ENDPOINT_QUERY, $route_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the get suggestions action..
|
||||
*
|
||||
* @param WP_REST_Request $request The request object.
|
||||
*
|
||||
* @return WP_REST_Response The response for the query of link suggestions.
|
||||
*/
|
||||
public function run_get_suggestions_action( WP_REST_Request $request ) {
|
||||
$prominent_words = $request->get_param( 'prominent_words' );
|
||||
$limit = $request->get_param( 'limit' );
|
||||
$object_id = $request->get_param( 'object_id' );
|
||||
$object_type = $request->get_param( 'object_type' );
|
||||
|
||||
return new WP_REST_Response(
|
||||
$this->link_suggestions_action->get_suggestions(
|
||||
$prominent_words,
|
||||
$limit,
|
||||
$object_id,
|
||||
$object_type
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current user is allowed to use this endpoint.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function can_retrieve_data() {
|
||||
return \current_user_can( 'edit_posts' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Routes;
|
||||
|
||||
use Exception;
|
||||
use WP_Error;
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use Yoast\WP\SEO\Actions\Indexing\Indexation_Action_Interface;
|
||||
use Yoast\WP\SEO\Conditionals\No_Conditionals;
|
||||
use Yoast\WP\SEO\Config\Indexing_Reasons;
|
||||
use Yoast\WP\SEO\Helpers\Indexing_Helper;
|
||||
use Yoast\WP\SEO\Main;
|
||||
use Yoast\WP\SEO\Premium\Actions\Prominent_Words\Complete_Action;
|
||||
use Yoast\WP\SEO\Premium\Actions\Prominent_Words\Content_Action;
|
||||
use Yoast\WP\SEO\Premium\Actions\Prominent_Words\Save_Action;
|
||||
|
||||
/**
|
||||
* Class Prominent_Words_Route
|
||||
*
|
||||
* @package Yoast\WP\SEO\Routes
|
||||
*/
|
||||
class Prominent_Words_Route extends Abstract_Indexation_Route {
|
||||
|
||||
use No_Conditionals;
|
||||
|
||||
/**
|
||||
* Feature namespace for the REST endpoint.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const FEATURE_NAMESPACE = 'prominent_words';
|
||||
|
||||
/**
|
||||
* The get content route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const GET_CONTENT_ROUTE = self::FEATURE_NAMESPACE . '/get_content';
|
||||
|
||||
/**
|
||||
* The full content route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const FULL_GET_CONTENT_ROUTE = Main::API_V1_NAMESPACE . '/' . self::GET_CONTENT_ROUTE;
|
||||
|
||||
/**
|
||||
* The route for saving the prominent words.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SAVE_ROUTE = self::FEATURE_NAMESPACE . '/save';
|
||||
|
||||
/**
|
||||
* The full namespaced route for saving the prominent words.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const FULL_SAVE_ROUTE = Main::API_V1_NAMESPACE . '/' . self::SAVE_ROUTE;
|
||||
|
||||
/**
|
||||
* The posts data route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const COMPLETE_ROUTE = self::FEATURE_NAMESPACE . '/complete';
|
||||
|
||||
/**
|
||||
* The full post data route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const FULL_COMPLETE_ROUTE = Main::API_V1_NAMESPACE . '/' . self::COMPLETE_ROUTE;
|
||||
|
||||
/**
|
||||
* Represents that action that retrieves the content to index.
|
||||
*
|
||||
* @var Content_Action
|
||||
*/
|
||||
protected $content_action;
|
||||
|
||||
/**
|
||||
* The action to complete the prominent words indexing.
|
||||
*
|
||||
* @var Complete_Action
|
||||
*/
|
||||
protected $complete_action;
|
||||
|
||||
/**
|
||||
* The action for saving prominent words to an indexable.
|
||||
*
|
||||
* @var Save_Action
|
||||
*/
|
||||
protected $save_action;
|
||||
|
||||
/**
|
||||
* The indexing helper.
|
||||
*
|
||||
* @var Indexing_Helper
|
||||
*/
|
||||
protected $indexing_helper;
|
||||
|
||||
/**
|
||||
* Prominent_Words_Route constructor.
|
||||
*
|
||||
* @param Content_Action $content_action The content action.
|
||||
* @param Save_Action $save_action The save action.
|
||||
* @param Complete_Action $complete_action The complete action.
|
||||
* @param Indexing_Helper $indexing_helper The indexing helper.
|
||||
*/
|
||||
public function __construct(
|
||||
Content_Action $content_action,
|
||||
Save_Action $save_action,
|
||||
Complete_Action $complete_action,
|
||||
Indexing_Helper $indexing_helper
|
||||
) {
|
||||
$this->content_action = $content_action;
|
||||
$this->save_action = $save_action;
|
||||
$this->complete_action = $complete_action;
|
||||
$this->indexing_helper = $indexing_helper;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers routes with WordPress.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_routes() {
|
||||
\register_rest_route(
|
||||
Main::API_V1_NAMESPACE,
|
||||
self::GET_CONTENT_ROUTE,
|
||||
[
|
||||
'methods' => 'POST',
|
||||
'callback' => [ $this, 'run_content_action' ],
|
||||
'permission_callback' => [ $this, 'can_retrieve_data' ],
|
||||
]
|
||||
);
|
||||
|
||||
\register_rest_route(
|
||||
Main::API_V1_NAMESPACE,
|
||||
self::COMPLETE_ROUTE,
|
||||
[
|
||||
'methods' => 'POST',
|
||||
'callback' => [ $this, 'run_complete_action' ],
|
||||
'permission_callback' => [ $this, 'can_retrieve_data' ],
|
||||
]
|
||||
);
|
||||
|
||||
$route_args = [
|
||||
'methods' => 'POST',
|
||||
'args' => [
|
||||
'data' => [
|
||||
'type' => 'array',
|
||||
'required' => false,
|
||||
'items' => [
|
||||
'type' => 'object',
|
||||
'properties' => [
|
||||
'object_id' => [
|
||||
'type' => 'number',
|
||||
'required' => true,
|
||||
],
|
||||
'prominent_words' => [
|
||||
'type' => 'object',
|
||||
'required' => false,
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'callback' => [ $this, 'run_save_action' ],
|
||||
'permission_callback' => [ $this, 'can_retrieve_data' ],
|
||||
];
|
||||
|
||||
\register_rest_route( Main::API_V1_NAMESPACE, self::SAVE_ROUTE, $route_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the content that needs to be analyzed for prominent words.
|
||||
*
|
||||
* @return WP_REST_Response Response with the content that needs to be analyzed for prominent words.
|
||||
*/
|
||||
public function run_content_action() {
|
||||
return $this->run_indexation_action( $this->content_action, self::FULL_GET_CONTENT_ROUTE );
|
||||
}
|
||||
|
||||
/**
|
||||
* Marks the indexing of prominent words as completed.
|
||||
*
|
||||
* @return WP_REST_Response Response with empty data.
|
||||
*/
|
||||
public function run_complete_action() {
|
||||
$this->complete_action->complete();
|
||||
|
||||
return $this->respond_with( [], false );
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the prominent words for the indexables.
|
||||
*
|
||||
* The request should have the parameters:
|
||||
* - **data**: The data array containing:
|
||||
* - **object_id**: The ID of the object (post-id, term-id, etc.).
|
||||
* - **prominent_words**: The map of `'stem' => weight` key-value pairs,
|
||||
* e.g. the stems of the prominent words and their weights.
|
||||
* Leave this out when the indexable has no prominent words.
|
||||
*
|
||||
* @param WP_REST_Request $request The request to handle.
|
||||
*
|
||||
* @return WP_REST_Response The response to give.
|
||||
*/
|
||||
public function run_save_action( WP_REST_Request $request ) {
|
||||
$this->save_action->save( $request->get_param( 'data' ) );
|
||||
|
||||
return new WP_REST_Response(
|
||||
[ 'message' => 'The words have been successfully saved for the given indexables.' ]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the current user is allowed to use this endpoint.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function can_retrieve_data() {
|
||||
return \current_user_can( 'edit_posts' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs an indexing action and returns the response.
|
||||
*
|
||||
* @param Indexation_Action_Interface $indexation_action The indexing action.
|
||||
* @param string $url The url of the indexing route.
|
||||
*
|
||||
* @return WP_REST_Response|WP_Error The response, or an error when running the indexing action failed.
|
||||
*/
|
||||
protected function run_indexation_action( Indexation_Action_Interface $indexation_action, $url ) {
|
||||
try {
|
||||
return parent::run_indexation_action( $indexation_action, $url );
|
||||
} catch ( Exception $exception ) {
|
||||
$this->indexing_helper->set_reason( Indexing_Reasons::REASON_INDEXING_FAILED );
|
||||
|
||||
return new WP_Error( 'wpseo_error_indexing', $exception->getMessage() );
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
namespace Yoast\WP\SEO\Routes;
|
||||
|
||||
use WP_REST_Request;
|
||||
use WP_REST_Response;
|
||||
use Yoast\WP\SEO\Conditionals\Zapier_Enabled_Conditional;
|
||||
use Yoast\WP\SEO\Main;
|
||||
use Yoast\WP\SEO\Premium\Actions\Zapier_Action;
|
||||
|
||||
/**
|
||||
* Registers the route for the Zapier integration.
|
||||
*
|
||||
* @package Yoast\WP\SEO\Routes
|
||||
*/
|
||||
class Zapier_Route implements Route_Interface {
|
||||
|
||||
/**
|
||||
* The Zapier route prefix.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ROUTE_PREFIX = 'zapier';
|
||||
|
||||
/**
|
||||
* The subscribe route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SUBSCRIBE_ROUTE = self::ROUTE_PREFIX . '/subscribe';
|
||||
|
||||
/**
|
||||
* The unsubscribe route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const UNSUBSCRIBE_ROUTE = self::ROUTE_PREFIX . '/unsubscribe';
|
||||
|
||||
/**
|
||||
* The check route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const CHECK_API_KEY_ROUTE = self::ROUTE_PREFIX . '/check';
|
||||
|
||||
/**
|
||||
* The perform list route constant.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const PERFORM_LIST = self::ROUTE_PREFIX . '/list';
|
||||
|
||||
/**
|
||||
* Instance of the Zapier_Action.
|
||||
*
|
||||
* @var Zapier_Action
|
||||
*/
|
||||
protected $zapier_action;
|
||||
|
||||
/**
|
||||
* Zapier_Route constructor.
|
||||
*
|
||||
* @param Zapier_Action $zapier_action The action to handle the requests to the endpoint.
|
||||
*/
|
||||
public function __construct( Zapier_Action $zapier_action ) {
|
||||
$this->zapier_action = $zapier_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers routes with WordPress.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register_routes() {
|
||||
$subscribe_route_args = [
|
||||
'methods' => 'POST',
|
||||
'args' => [
|
||||
'url' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => 'The callback URL to use.',
|
||||
],
|
||||
'api_key' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => 'The API key to validate.',
|
||||
],
|
||||
],
|
||||
'callback' => [ $this, 'subscribe' ],
|
||||
'permission_callback' => '__return_true',
|
||||
];
|
||||
\register_rest_route( Main::API_V1_NAMESPACE, self::SUBSCRIBE_ROUTE, $subscribe_route_args );
|
||||
|
||||
$unsubscribe_route_args = [
|
||||
'methods' => 'DELETE',
|
||||
'args' => [
|
||||
'id' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => 'The ID of the subscription to unsubscribe.',
|
||||
],
|
||||
],
|
||||
'callback' => [ $this, 'unsubscribe' ],
|
||||
'permission_callback' => '__return_true',
|
||||
];
|
||||
\register_rest_route( Main::API_V1_NAMESPACE, self::UNSUBSCRIBE_ROUTE, $unsubscribe_route_args );
|
||||
|
||||
$check_api_key_route_args = [
|
||||
'methods' => 'POST',
|
||||
'args' => [
|
||||
'api_key' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => 'The API key to validate.',
|
||||
],
|
||||
],
|
||||
'callback' => [ $this, 'check_api_key' ],
|
||||
'permission_callback' => '__return_true',
|
||||
];
|
||||
\register_rest_route( Main::API_V1_NAMESPACE, self::CHECK_API_KEY_ROUTE, $check_api_key_route_args );
|
||||
|
||||
$perform_list_route_args = [
|
||||
'methods' => 'GET',
|
||||
'args' => [
|
||||
'api_key' => [
|
||||
'required' => true,
|
||||
'type' => 'string',
|
||||
'description' => 'The API key to validate.',
|
||||
],
|
||||
],
|
||||
'callback' => [ $this, 'perform_list' ],
|
||||
'permission_callback' => '__return_true',
|
||||
];
|
||||
\register_rest_route( Main::API_V1_NAMESPACE, self::PERFORM_LIST, $perform_list_route_args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the subscribe action.
|
||||
*
|
||||
* @param WP_REST_Request $request The request object.
|
||||
*
|
||||
* @return WP_REST_Response The response of the subscribe action.
|
||||
*/
|
||||
public function subscribe( WP_REST_Request $request ) {
|
||||
$subscription = $this->zapier_action->subscribe( $request['url'], $request['api_key'] );
|
||||
$response = $subscription->data;
|
||||
|
||||
if ( empty( $response ) && \property_exists( $subscription, 'message' ) ) {
|
||||
$response = $subscription->message;
|
||||
}
|
||||
|
||||
return new WP_REST_Response( $response, $subscription->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the unsubscribe action.
|
||||
*
|
||||
* @param WP_REST_Request $request The request object.
|
||||
*
|
||||
* @return WP_REST_Response The response of the unsubscribe action.
|
||||
*/
|
||||
public function unsubscribe( WP_REST_Request $request ) {
|
||||
$subscription = $this->zapier_action->unsubscribe( $request['id'] );
|
||||
|
||||
return new WP_REST_Response( $subscription->message, $subscription->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the check_api_key action.
|
||||
*
|
||||
* @param WP_REST_Request $request The request object.
|
||||
*
|
||||
* @return WP_REST_Response The response of the check_api_key action.
|
||||
*/
|
||||
public function check_api_key( WP_REST_Request $request ) {
|
||||
$check = $this->zapier_action->check_api_key( $request['api_key'] );
|
||||
|
||||
return new WP_REST_Response( $check->message, $check->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the check_api_key action.
|
||||
*
|
||||
* @param WP_REST_Request $request The request object.
|
||||
*
|
||||
* @return WP_REST_Response The response of the check_api_key action.
|
||||
*/
|
||||
public function perform_list( WP_REST_Request $request ) {
|
||||
$response = $this->zapier_action->perform_list( $request['api_key'] );
|
||||
|
||||
return new WP_REST_Response( $response->data, $response->status );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the conditionals based in which these routes should be active.
|
||||
*
|
||||
* @return array The list of conditionals.
|
||||
*/
|
||||
public static function get_conditionals() {
|
||||
return [ Zapier_Enabled_Conditional::class ];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user