| 1 |
<?php |
| 2 |
|
| 3 |
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure. |
| 4 |
namespace Yoast\WP\SEO\AI\Content_Planner\User_Interface; |
| 5 |
|
| 6 |
use RuntimeException; |
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use Yoast\WP\SEO\AI\Content_Planner\Application\Content_Suggestion_Command; |
| 10 |
use Yoast\WP\SEO\AI\Content_Planner\Application\Content_Suggestion_Command_Handler; |
| 11 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Payment_Required_Exception; |
| 12 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Remote_Request_Exception; |
| 13 |
use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception; |
| 14 |
use Yoast\WP\SEO\Conditionals\AI_Conditional; |
| 15 |
use Yoast\WP\SEO\Main; |
| 16 |
use Yoast\WP\SEO\Routes\Route_Interface; |
| 17 |
|
| 18 |
/** |
| 19 |
* Registers a route to get content suggestions from the AI API. |
| 20 |
* |
| 21 |
* @internal This route powers the Yoast SEO admin UI's Content Planner feature. It is not part of the plugin's public REST API surface, requires the `edit_posts` capability (see {@see self::check_permissions()}), and may change at any time without notice. |
| 22 |
* |
| 23 |
* @makePublic |
| 24 |
* |
| 25 |
* @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded |
| 26 |
*/ |
| 27 |
class Get_Suggestions_Route implements Route_Interface { |
| 28 |
|
| 29 |
/** |
| 30 |
* The namespace for this route. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE; |
| 35 |
|
| 36 |
/** |
| 37 |
* The prefix for this route. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
public const ROUTE_PREFIX = '/ai_content_planner/get_suggestions'; |
| 42 |
|
| 43 |
/** |
| 44 |
* The command handler instance. |
| 45 |
* |
| 46 |
* @var Content_Suggestion_Command_Handler |
| 47 |
*/ |
| 48 |
private $command_handler; |
| 49 |
|
| 50 |
/** |
| 51 |
* Returns the conditionals based in which this loadable should be active. |
| 52 |
* |
| 53 |
* @return array<string> The conditionals. |
| 54 |
*/ |
| 55 |
public static function get_conditionals() { |
| 56 |
return [ AI_Conditional::class ]; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Class constructor. |
| 61 |
* |
| 62 |
* @param Content_Suggestion_Command_Handler $command_handler The command handler instance. |
| 63 |
*/ |
| 64 |
public function __construct( Content_Suggestion_Command_Handler $command_handler ) { |
| 65 |
$this->command_handler = $command_handler; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Registers routes with WordPress. |
| 70 |
* |
| 71 |
* @return void |
| 72 |
*/ |
| 73 |
public function register_routes() { |
| 74 |
\register_rest_route( |
| 75 |
self::ROUTE_NAMESPACE, |
| 76 |
self::ROUTE_PREFIX, |
| 77 |
[ |
| 78 |
'methods' => 'GET', |
| 79 |
'args' => [ |
| 80 |
'post_type' => [ |
| 81 |
'required' => true, |
| 82 |
'type' => 'string', |
| 83 |
'description' => 'The post type to get content suggestions for.', |
| 84 |
], |
| 85 |
'language' => [ |
| 86 |
'required' => true, |
| 87 |
'type' => 'string', |
| 88 |
'description' => 'The language the content is written in.', |
| 89 |
], |
| 90 |
'editor' => [ |
| 91 |
'required' => true, |
| 92 |
'type' => 'string', |
| 93 |
'enum' => [ |
| 94 |
'classic', |
| 95 |
'elementor', |
| 96 |
'gutenberg', |
| 97 |
], |
| 98 |
'description' => 'The current editor.', |
| 99 |
], |
| 100 |
], |
| 101 |
'callback' => [ $this, 'get_suggestions' ], |
| 102 |
'permission_callback' => [ $this, 'check_permissions' ], |
| 103 |
], |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Runs the callback to get AI-generated content suggestions. |
| 109 |
* |
| 110 |
* @param WP_REST_Request $request The request object. |
| 111 |
* |
| 112 |
* @return WP_REST_Response The response of the get_suggestions action. |
| 113 |
*/ |
| 114 |
public function get_suggestions( WP_REST_Request $request ): WP_REST_Response { |
| 115 |
try { |
| 116 |
$user = \wp_get_current_user(); |
| 117 |
|
| 118 |
$command = new Content_Suggestion_Command( |
| 119 |
$user, |
| 120 |
$request->get_param( 'post_type' ), |
| 121 |
$request->get_param( 'language' ), |
| 122 |
$request->get_param( 'editor' ), |
| 123 |
); |
| 124 |
$data = $this->command_handler->handle( $command ); |
| 125 |
} catch ( Remote_Request_Exception $e ) { |
| 126 |
$message = [ |
| 127 |
'message' => $e->getMessage(), |
| 128 |
'errorIdentifier' => $e->get_error_identifier(), |
| 129 |
]; |
| 130 |
if ( $e instanceof Payment_Required_Exception || $e instanceof Too_Many_Requests_Exception ) { |
| 131 |
$message['missingLicenses'] = $e->get_missing_licenses(); |
| 132 |
} |
| 133 |
return new WP_REST_Response( |
| 134 |
$message, |
| 135 |
$e->getCode(), |
| 136 |
); |
| 137 |
} catch ( RuntimeException $e ) { |
| 138 |
return new WP_REST_Response( 'Failed to get content suggestions.', 500 ); |
| 139 |
} |
| 140 |
|
| 141 |
return new WP_REST_Response( $data->to_array() ); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Checks if the user is logged in and can edit posts. |
| 146 |
* |
| 147 |
* @return bool Whether the user is logged in and can edit posts. |
| 148 |
*/ |
| 149 |
public function check_permissions(): bool { |
| 150 |
$user = \wp_get_current_user(); |
| 151 |
if ( $user === null || $user->ID < 1 ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return \user_can( $user, 'edit_posts' ); |
| 156 |
} |
| 157 |
} |
| 158 |
|