| 1 |
<?php |
| 2 |
/** |
| 3 |
* Remote API: Content Suggestions Suggest Brief API |
| 4 |
* |
| 5 |
* @package Parsely |
| 6 |
* @since 3.13.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
declare(strict_types=1); |
| 10 |
|
| 11 |
namespace Parsely\RemoteAPI\ContentSuggestions; |
| 12 |
|
| 13 |
use Parsely\Parsely; |
| 14 |
use WP_Error; |
| 15 |
|
| 16 |
/** |
| 17 |
* Class for Content Suggestions Suggest Brief API. |
| 18 |
* This class is used to get the meta description for a given content. |
| 19 |
* |
| 20 |
* @since 3.13.0 |
| 21 |
* @since 3.14.0 Renamed from Suggest_Meta_Description_API to Suggest_Brief_API. |
| 22 |
* |
| 23 |
* @phpstan-import-type WP_HTTP_Request_Args from Parsely |
| 24 |
*/ |
| 25 |
class Suggest_Brief_API extends Content_Suggestions_Base_API { |
| 26 |
protected const ENDPOINT = '/suggest-brief'; |
| 27 |
protected const QUERY_FILTER = 'wp_parsely_suggest_brief_endpoint_args'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Gets the first brief (meta description) for a given content using the |
| 31 |
* Parse.ly Content Suggestion API. |
| 32 |
* |
| 33 |
* @since 3.13.0 |
| 34 |
* |
| 35 |
* @param string $title The title of the content. |
| 36 |
* @param string $content The query arguments to send to the remote API. |
| 37 |
* @param string $persona The persona to use for the suggestion. |
| 38 |
* @param string $style The style to use for the suggestion. |
| 39 |
* @return string|WP_Error The response from the remote API, or a WP_Error |
| 40 |
* object if the response is an error. |
| 41 |
*/ |
| 42 |
public function get_suggestion( |
| 43 |
string $title, |
| 44 |
string $content, |
| 45 |
string $persona = 'journalist', |
| 46 |
string $style = 'neutral' |
| 47 |
) { |
| 48 |
$body = array( |
| 49 |
'output_config' => array( |
| 50 |
'persona' => $persona, |
| 51 |
'style' => $style, |
| 52 |
), |
| 53 |
'title' => $title, |
| 54 |
'text' => $content, |
| 55 |
); |
| 56 |
|
| 57 |
$decoded = $this->post_request( array(), $body ); |
| 58 |
|
| 59 |
if ( is_wp_error( $decoded ) ) { |
| 60 |
return $decoded; |
| 61 |
} |
| 62 |
|
| 63 |
if ( ! property_exists( $decoded, 'result' ) || |
| 64 |
! is_string( $decoded->result[0] ) ) { |
| 65 |
return new WP_Error( |
| 66 |
400, |
| 67 |
__( 'Unable to parse meta description from upstream API', 'wp-parsely' ) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
return $decoded->result[0]; |
| 72 |
} |
| 73 |
} |
| 74 |
|