| 1 |
<?php |
| 2 |
/** |
| 3 |
* Parse.ly Suggestions API Endpoint: Suggest Headline |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.17.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\Services\Suggestions_API\Endpoints; |
| 12 |
|
| 13 |
use WP_Error; |
| 14 |
|
| 15 |
/** |
| 16 |
* The endpoint for the /suggest-headline API request. |
| 17 |
* |
| 18 |
* @since 3.17.0 |
| 19 |
* |
| 20 |
* @phpstan-type Traffic_Source = array{ |
| 21 |
* source: string, |
| 22 |
* weight: float |
| 23 |
* } |
| 24 |
* |
| 25 |
* @phpstan-type Endpoint_Suggest_Headline_Options = array{ |
| 26 |
* persona?: string, |
| 27 |
* style?: string, |
| 28 |
* max_items?: int, |
| 29 |
* traffic_sources?: array<int, Traffic_Source> |
| 30 |
* } |
| 31 |
*/ |
| 32 |
class Endpoint_Suggest_Headline extends Suggestions_API_Base_Endpoint { |
| 33 |
/** |
| 34 |
* Returns the endpoint for the API request. |
| 35 |
* |
| 36 |
* @since 3.17.0 |
| 37 |
* |
| 38 |
* @return string The endpoint for the API request. |
| 39 |
*/ |
| 40 |
public function get_endpoint(): string { |
| 41 |
return '/suggest-headline'; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Gets titles (headlines) for a given content using the Parse.ly Content |
| 46 |
* Suggestion API. |
| 47 |
* |
| 48 |
* @since 3.13.0 |
| 49 |
* @since 3.17.0 Updated to use the new API service. |
| 50 |
* |
| 51 |
* @param string $content The query arguments to send to the remote API. |
| 52 |
* @param Endpoint_Suggest_Headline_Options $options The options to pass to the API request. |
| 53 |
* @return array<string>|WP_Error The response from the remote API, or a WP_Error |
| 54 |
* object if the response is an error. |
| 55 |
*/ |
| 56 |
public function get_headlines( |
| 57 |
string $content, |
| 58 |
$options = array() |
| 59 |
) { |
| 60 |
$request_body = array( |
| 61 |
'output_config' => array( |
| 62 |
'persona' => $options['persona'] ?? 'journalist', |
| 63 |
'style' => $options['style'] ?? 'neutral', |
| 64 |
'max_items' => $options['max_items'] ?? 1, |
| 65 |
), |
| 66 |
'text' => wp_strip_all_tags( $content ), |
| 67 |
); |
| 68 |
|
| 69 |
/** @var array<string>|WP_Error $response */ |
| 70 |
$response = $this->request( 'POST', array(), $request_body ); |
| 71 |
|
| 72 |
return $response; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Executes the API request. |
| 77 |
* |
| 78 |
* @since 3.17.0 |
| 79 |
* |
| 80 |
* @param array<mixed> $args The arguments to pass to the API request. |
| 81 |
* @return WP_Error|array<mixed> The response from the API. |
| 82 |
*/ |
| 83 |
public function call( array $args = array() ) { |
| 84 |
/** @var string $title */ |
| 85 |
$title = $args['title'] ?? ''; |
| 86 |
/** @var string $content */ |
| 87 |
$content = $args['content'] ?? ''; |
| 88 |
/** @var Endpoint_Suggest_Headline_Options $options */ |
| 89 |
$options = $args['options'] ?? array(); |
| 90 |
|
| 91 |
return $this->get_headlines( $content, $options ); |
| 92 |
} |
| 93 |
} |
| 94 |
|