| 1 |
<?php |
| 2 |
/** |
| 3 |
* Remote API: Content Suggestions Suggest Headline API |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.12.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace Parsely\RemoteAPI\ContentSuggestions; |
| 10 |
|
| 11 |
use Parsely\Parsely; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class for Content Suggestions Suggest Headline API. |
| 16 |
* |
| 17 |
* @since 3.12.0 |
| 18 |
* @since 3.14.0 Renamed from Write_Title_API to Suggest_Headline_API. |
| 19 |
* |
| 20 |
* @phpstan-import-type WP_HTTP_Request_Args from Parsely |
| 21 |
*/ |
| 22 |
class Suggest_Headline_API extends Content_Suggestions_Base_API { |
| 23 |
protected const ENDPOINT = '/suggest-headline'; |
| 24 |
protected const QUERY_FILTER = 'wp_parsely_suggest_headline_endpoint_args'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Generates titles for a given content using the Parse.ly |
| 28 |
* Content Suggestion API. |
| 29 |
* |
| 30 |
* @since 3.12.0 |
| 31 |
* |
| 32 |
* @param string $content The content to generate titles for. |
| 33 |
* @param int $limit The number of titles to generate. |
| 34 |
* @param string $persona The persona to use when generating titles. |
| 35 |
* @param string $tone The tone to use when generating titles. |
| 36 |
* @return array<string>|WP_Error The response from the remote API, or a WP_Error |
| 37 |
* object if the response is an error. |
| 38 |
*/ |
| 39 |
public function get_titles( |
| 40 |
string $content, |
| 41 |
int $limit, |
| 42 |
string $persona = 'journalist', |
| 43 |
string $tone = 'neutral' |
| 44 |
) { |
| 45 |
$body = array( |
| 46 |
'output_config' => array( |
| 47 |
'persona' => $persona, |
| 48 |
'style' => $tone, |
| 49 |
'max_items' => $limit, |
| 50 |
), |
| 51 |
'text' => $content, |
| 52 |
); |
| 53 |
|
| 54 |
$decoded = $this->post_request( array(), $body ); |
| 55 |
|
| 56 |
if ( is_wp_error( $decoded ) ) { |
| 57 |
return $decoded; |
| 58 |
} |
| 59 |
|
| 60 |
if ( ! property_exists( $decoded, 'result' ) || ! is_array( $decoded->result ) ) { |
| 61 |
return new WP_Error( |
| 62 |
400, |
| 63 |
__( 'Unable to parse titles from upstream API', 'wp-parsely' ) |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
return $decoded->result; |
| 68 |
} |
| 69 |
} |
| 70 |
|