# wp-parsely/3.14.1/src/RemoteAPI/content-suggestions/class-suggest-headline-api.php

Parse.ly, version 3.14.1. 70 lines.

- Page: https://pluginprobe.com/plugins/wp-parsely/3.14.1/code/src/RemoteAPI/content-suggestions/class-suggest-headline-api.php
- Raw: https://pluginprobe.com/plugins/wp-parsely/3.14.1/raw/src/RemoteAPI/content-suggestions/class-suggest-headline-api.php
- Modified: 2024-03-12T09:31:56+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/wp-parsely/3.14.1/code/src/RemoteAPI/content-suggestions/class-suggest-headline-api.php#L10-L20`.

```php
<?php
/**
 * Remote API: Content Suggestions Suggest Headline API
 *
 * @package Parsely
 * @since 3.12.0
 */

namespace Parsely\RemoteAPI\ContentSuggestions;

use Parsely\Parsely;
use WP_Error;

/**
 * Class for Content Suggestions Suggest Headline API.
 *
 * @since 3.12.0
 * @since 3.14.0 Renamed from Write_Title_API to Suggest_Headline_API.
 *
 * @phpstan-import-type WP_HTTP_Request_Args from Parsely
 */
class Suggest_Headline_API extends Content_Suggestions_Base_API {
	protected const ENDPOINT     = '/suggest-headline';
	protected const QUERY_FILTER = 'wp_parsely_suggest_headline_endpoint_args';

	/**
	 * Generates titles for a given content using the Parse.ly
	 * Content Suggestion API.
	 *
	 * @since 3.12.0
	 *
	 * @param  string $content The content to generate titles for.
	 * @param  int    $limit The number of titles to generate.
	 * @param  string $persona The persona to use when generating titles.
	 * @param  string $tone The tone to use when generating titles.
	 * @return array<string>|WP_Error The response from the remote API, or a WP_Error
	 *                                object if the response is an error.
	 */
	public function get_titles(
		string $content,
		int $limit,
		string $persona = 'journalist',
		string $tone = 'neutral'
	) {
		$body = array(
			'output_config' => array(
				'persona'   => $persona,
				'style'     => $tone,
				'max_items' => $limit,
			),
			'text'          => $content,
		);

		$decoded = $this->post_request( array(), $body );

		if ( is_wp_error( $decoded ) ) {
			return $decoded;
		}

		if ( ! property_exists( $decoded, 'result' ) || ! is_array( $decoded->result ) ) {
			return new WP_Error(
				400,
				__( 'Unable to parse titles from upstream API', 'wp-parsely' )
			);
		}

		return $decoded->result;
	}
}

```
