PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
3.24.3 3.24.2 3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 All 107 releases
wp-parsely / src / services / suggestions-api / endpoints / class-endpoint-suggest-brief.php

class-endpoint-suggest-brief.php in Parse.ly 3.24.2, at src/services/suggestions-api/endpoints/class-endpoint-suggest-brief.php

92 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Parse.ly Suggestions API Endpoint: Suggest Brief
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-brief API request.
17 *
18 * @since 3.17.0
19 *
20 * @phpstan-type Endpoint_Suggest_Brief_Options = array{
21 * persona?: string,
22 * style?: string,
23 * max_characters?: int,
24 * max_items?: int,
25 * }
26 */
27 class Endpoint_Suggest_Brief extends Suggestions_API_Base_Endpoint {
28 /**
29 * Returns the endpoint for the API request.
30 *
31 * @since 3.17.0
32 *
33 * @return string The endpoint for the API request.
34 */
35 public function get_endpoint(): string {
36 return '/suggest-brief';
37 }
38
39 /**
40 * Gets the first brief (meta description) for a given content using the
41 * Parse.ly Content Suggestion API.
42 *
43 * @since 3.13.0
44 *
45 * @param string $title The title of the content.
46 * @param string $content The query arguments to send to the remote API.
47 * @param Endpoint_Suggest_Brief_Options $options The options to pass to the API request.
48 * @return array<string>|WP_Error The response from the remote API, or a WP_Error
49 * object if the response is an error.
50 */
51 public function get_suggestion(
52 string $title,
53 string $content,
54 $options = array()
55 ) {
56 $request_body = array(
57 'output_config' => array(
58 'persona' => $options['persona'] ?? 'journalist',
59 'style' => $options['style'] ?? 'neutral',
60 'max_characters' => $options['max_characters'] ?? 160,
61 'max_items' => $options['max_items'] ?? 1,
62 ),
63 'title' => $title,
64 'text' => wp_strip_all_tags( $content ),
65 );
66
67 /** @var array<string>|WP_Error $response */
68 $response = $this->request( 'POST', array(), $request_body );
69
70 return $response;
71 }
72
73 /**
74 * Executes the API request.
75 *
76 * @since 3.17.0
77 *
78 * @param array<mixed> $args The arguments to pass to the API request.
79 * @return WP_Error|array<mixed> The response from the API.
80 */
81 public function call( array $args = array() ) {
82 /** @var string $title */
83 $title = $args['title'] ?? '';
84 /** @var string $content */
85 $content = $args['content'] ?? '';
86 /** @var Endpoint_Suggest_Brief_Options $options */
87 $options = $args['options'] ?? array();
88
89 return $this->get_suggestion( $title, $content, $options );
90 }
91 }
92