| 1 |
<?php |
| 2 |
/** |
| 3 |
* Endpoints: Parse.ly Content Suggestions `/suggest-linked-reference` API proxy endpoint |
| 4 |
* class |
| 5 |
* |
| 6 |
* @package Parsely |
| 7 |
* @since 3.14.0 |
| 8 |
*/ |
| 9 |
|
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace Parsely\Endpoints\ContentSuggestions; |
| 13 |
|
| 14 |
use Parsely\Endpoints\Base_API_Proxy; |
| 15 |
use Parsely\Parsely; |
| 16 |
use Parsely\RemoteAPI\ContentSuggestions\Suggest_Linked_Reference_API; |
| 17 |
use stdClass; |
| 18 |
use WP_REST_Request; |
| 19 |
use WP_Error; |
| 20 |
|
| 21 |
/** |
| 22 |
* Configures the `/content-suggestions/suggest-linked-reference` REST API endpoint. |
| 23 |
* |
| 24 |
* @since 3.14.0 |
| 25 |
*/ |
| 26 |
final class Suggest_Linked_Reference_API_Proxy extends Base_API_Proxy { |
| 27 |
/** |
| 28 |
* The Suggest Linked Reference API instance. |
| 29 |
* |
| 30 |
* @var Suggest_Linked_Reference_API $suggest_linked_reference_api |
| 31 |
*/ |
| 32 |
private $suggest_linked_reference_api; |
| 33 |
|
| 34 |
/** |
| 35 |
* Initializes the class. |
| 36 |
* |
| 37 |
* @since 3.14.0 |
| 38 |
* |
| 39 |
* @param Parsely $parsely The Parsely plugin instance. |
| 40 |
*/ |
| 41 |
public function __construct( Parsely $parsely ) { |
| 42 |
$this->suggest_linked_reference_api = new Suggest_Linked_Reference_API( $parsely ); |
| 43 |
parent::__construct( $parsely, $this->suggest_linked_reference_api ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Registers the endpoint's WP REST route. |
| 48 |
* |
| 49 |
* @since 3.14.0 |
| 50 |
*/ |
| 51 |
public function run(): void { |
| 52 |
$this->register_endpoint( '/content-suggestions/suggest-linked-reference', array( 'POST' ) ); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Generates the final data from the passed response. |
| 57 |
* |
| 58 |
* @since 3.14.0 |
| 59 |
* |
| 60 |
* @param array<stdClass> $response The response received by the proxy. |
| 61 |
* @return array<stdClass> The generated data. |
| 62 |
*/ |
| 63 |
protected function generate_data( $response ): array { |
| 64 |
// Unused function. |
| 65 |
return $response; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Cached "proxy" to the Parse.ly API endpoint. |
| 70 |
* |
| 71 |
* @since 3.14.0 |
| 72 |
* |
| 73 |
* @param WP_REST_Request $request The request object. |
| 74 |
* @return stdClass|WP_Error stdClass containing the data or a WP_Error |
| 75 |
* object on failure. |
| 76 |
*/ |
| 77 |
public function get_items( WP_REST_Request $request ) { |
| 78 |
$validation = $this->validate_apikey_and_secret(); |
| 79 |
if ( is_wp_error( $validation ) ) { |
| 80 |
return $validation; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* The post content to be sent to the API. |
| 85 |
* |
| 86 |
* @var string|null $post_content |
| 87 |
*/ |
| 88 |
$post_content = $request->get_param( 'content' ); |
| 89 |
|
| 90 |
/** |
| 91 |
* The maximum amount of words of the link text. |
| 92 |
* |
| 93 |
* @var string|null $max_link_words |
| 94 |
*/ |
| 95 |
$max_link_words = $request->get_param( 'max_link_words' ); |
| 96 |
|
| 97 |
/** |
| 98 |
* The maximum number of links to return. |
| 99 |
* |
| 100 |
* @var string|null $max_links |
| 101 |
*/ |
| 102 |
$max_links = $request->get_param( 'max_links' ); |
| 103 |
|
| 104 |
if ( null === $post_content ) { |
| 105 |
return new WP_Error( |
| 106 |
'parsely_content_not_set', |
| 107 |
__( 'A post content must be set to use this endpoint', 'wp-parsely' ), |
| 108 |
array( 'status' => 403 ) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
if ( is_numeric( $max_link_words ) ) { |
| 113 |
$max_link_words = (int) $max_link_words; |
| 114 |
} else { |
| 115 |
$max_link_words = 4; |
| 116 |
} |
| 117 |
|
| 118 |
if ( is_numeric( $max_links ) ) { |
| 119 |
$max_links = (int) $max_links; |
| 120 |
} else { |
| 121 |
$max_links = 10; |
| 122 |
} |
| 123 |
|
| 124 |
$response = $this->suggest_linked_reference_api->get_links( |
| 125 |
$post_content, |
| 126 |
$max_link_words, |
| 127 |
$max_links |
| 128 |
); |
| 129 |
|
| 130 |
if ( is_wp_error( $response ) ) { |
| 131 |
return $response; |
| 132 |
} |
| 133 |
|
| 134 |
return (object) array( |
| 135 |
'data' => $response, |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
|