PluginProbe
Parse.ly / 3.24.2
Parse.ly v3.24.2
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 3.20.2 All 106 releases
wp-parsely / src / rest-api / content-helper / class-endpoint-title-suggestions.php

class-endpoint-title-suggestions.php in Parse.ly 3.24.2, at src/rest-api/content-helper/class-endpoint-title-suggestions.php

175 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * API Endpoint: Title Suggestions
4 * Parse.ly Content Intelligence `/title-suggestions` API endpoint class
5 *
6 * @package Parsely
7 * @since 3.17.0
8 */
9
10 declare(strict_types=1);
11
12 namespace Parsely\REST_API\Content_Helper;
13
14 use Parsely\Content_Helper\Suggestion_Defaults;
15 use Parsely\REST_API\Base_Endpoint;
16 use Parsely\Services\Suggestions_API\Suggestions_API_Service;
17 use WP_Error;
18 use WP_REST_Request;
19 use WP_REST_Response;
20
21 /**
22 * The Title Suggestions API.
23 *
24 * Provides an endpoint for generating titles for the given content.
25 *
26 * @since 3.17.0
27 */
28 class Endpoint_Title_Suggestions extends Base_Endpoint {
29 use Content_Helper_Feature;
30
31 /**
32 * The Suggestions API service.
33 *
34 * @since 3.17.0
35 *
36 * @var Suggestions_API_Service $suggestions_api
37 */
38 protected $suggestions_api;
39
40 /**
41 * Initializes the class.
42 *
43 * @since 3.17.0
44 *
45 * @param Content_Helper_Controller $controller The Content Intelligence controller.
46 */
47 public function __construct( Content_Helper_Controller $controller ) {
48 parent::__construct( $controller );
49 $this->suggestions_api = $controller->get_parsely()->get_suggestions_api();
50 }
51
52 /**
53 * Returns the name of the endpoint.
54 *
55 * @since 3.17.0
56 *
57 * @return string The endpoint's name.
58 */
59 public static function get_endpoint_name(): string {
60 return 'title-suggestions';
61 }
62
63 /**
64 * Returns the name of the feature associated with the current endpoint.
65 *
66 * @since 3.17.0
67 *
68 * @return string
69 */
70 public function get_pch_feature_name(): string {
71 return 'title_suggestions';
72 }
73
74 /**
75 * Registers the routes for the endpoint.
76 *
77 * @since 3.17.0
78 */
79 public function register_routes(): void {
80 /**
81 * POST /title-suggestions/generate
82 * Generates titles for the given content.
83 */
84 $this->register_rest_route(
85 'generate',
86 array( 'POST' ),
87 array( $this, 'generate_titles' ),
88 array(
89 'text' => array(
90 'description' => __( 'The content for which to generate titles.', 'wp-parsely' ),
91 'required' => true,
92 'type' => 'string',
93 ),
94 'limit' => array(
95 'description' => __( 'The maximum number of titles to be suggested.', 'wp-parsely' ),
96 'required' => false,
97 'type' => 'integer',
98 'default' => 3,
99 ),
100 'style' => array(
101 'description' => __( 'The style of the titles to be suggested.', 'wp-parsely' ),
102 'required' => false,
103 'type' => 'string',
104 'default' => Suggestion_Defaults::DEFAULT_TONE,
105 ),
106 'persona' => array(
107 'description' => __( 'The persona of the titles to be suggested.', 'wp-parsely' ),
108 'required' => false,
109 'type' => 'string',
110 'default' => Suggestion_Defaults::DEFAULT_PERSONA,
111 ),
112 )
113 );
114 }
115
116 /**
117 * API Endpoint: POST /title-suggestions/generate
118 *
119 * Generates titles for the given content.
120 *
121 * @since 3.17.0
122 *
123 * @param WP_REST_Request $request The request object.
124 * @return WP_REST_Response|WP_Error The response object or a WP_Error object on failure.
125 */
126 public function generate_titles( WP_REST_Request $request ) {
127 /**
128 * The post content to be sent to the API.
129 *
130 * @var string $post_content
131 */
132 $post_content = $request->get_param( 'text' );
133
134 /**
135 * The maximum number of titles to generate.
136 *
137 * @var int $limit
138 */
139 $limit = $request->get_param( 'limit' );
140
141 /**
142 * The style of the titles to generate.
143 *
144 * @var string $style
145 */
146 $style = $request->get_param( 'style' );
147
148 /**
149 * The tone of the titles to generate.
150 *
151 * @var string $persona
152 */
153 $persona = $request->get_param( 'persona' );
154
155 if ( 0 === $limit ) {
156 $limit = 3;
157 }
158
159 $response = $this->suggestions_api->get_title_suggestions(
160 $post_content,
161 array(
162 'persona' => $persona,
163 'style' => $style,
164 'max_items' => $limit,
165 )
166 );
167
168 if ( is_wp_error( $response ) ) {
169 return $response;
170 }
171
172 return new WP_REST_Response( array( 'data' => $response ), 200 );
173 }
174 }
175