-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathRewriteTone.php
More file actions
337 lines (304 loc) · 9.96 KB
/
Copy pathRewriteTone.php
File metadata and controls
337 lines (304 loc) · 9.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
namespace Classifai\Features;
use Classifai\Providers\OpenAI\ChatGPT;
use Classifai\Services\LanguageProcessing;
use WP_REST_Server;
use WP_REST_Request;
use WP_Error;
use function Classifai\get_asset_info;
use function Classifai\sanitize_prompts;
/**
* Class RewriteTone
*/
class RewriteTone extends Feature {
/**
* ID of the current feature.
*
* @var string
*/
const ID = 'feature_rewrite_tone';
/**
* Constructor.
*/
public function __construct() {
$this->label = __( 'Rewrite Tone', 'classifai' );
// Contains all providers that are registered to the service.
$this->provider_instances = $this->get_provider_instances( LanguageProcessing::get_service_providers() );
// Contains just the providers this feature supports.
$this->supported_providers = [
ChatGPT::ID => __( 'OpenAI ChatGPT', 'classifai' ),
];
}
/**
* Set up necessary hooks.
*
* We utilize this so we can register the REST route.
*/
public function setup() {
parent::setup();
add_action( 'rest_api_init', [ $this, 'register_endpoints' ] );
}
/**
* Set up necessary hooks.
*/
public function feature_setup() {
add_action( 'enqueue_block_assets', [ $this, 'enqueue_editor_assets' ] );
}
/**
* Register any needed endpoints.
*/
public function register_endpoints() {
register_rest_route(
'classifai/v1',
'rewrite-tone',
[
'methods' => WP_REST_Server::CREATABLE,
'callback' => [ $this, 'rest_endpoint_callback' ],
'permission_callback' => '__return_true',
'args' => [
'id' => [
'required' => true,
'type' => 'integer',
'sanitize_callback' => 'absint',
'description' => esc_html__( 'Post ID to resize the content for.', 'classifai' ),
],
'content' => [
'type' => 'array',
'sanitize_callback' => function ( $content_array ) {
if ( is_array( $content_array ) ) {
return array_map(
function ( $item ) {
$item['clientId'] = sanitize_text_field( $item['clientId'] );
$item['content'] = wp_kses_post( $item['content'] );
return $item;
},
$content_array
);
}
return [];
},
'validate_callback' => function ( $content_array ) {
if ( is_array( $content_array ) ) {
foreach ( $content_array as $item ) {
if ( ! isset( $item['clientId'] ) || ! is_string( $item['clientId'] ) ) {
return new WP_Error( 'rewrite_tone_invalid_client_id', __( 'Each item must have a valid clientId string.', 'classifai' ), [ 'status' => 400 ] );
}
if ( ! isset( $item['content'] ) || ! is_string( $item['content'] ) ) {
return new WP_Error( 'rewrite_tone_invalid_content', __( 'Each item must have valid content as a string.', 'classifai' ), [ 'status' => 400 ] );
}
}
return true;
}
return new WP_Error( 'rewrite_tone_invalid_data_format', __( 'Content must be an array of objects.', 'classifai' ), [ 'status' => 400 ] );
},
'description' => esc_html__( 'The content to resize.', 'classifai' ),
],
],
]
);
}
/**
* Check if a given request has access to resize content.
*
* @param WP_REST_Request $request Full data about the request.
* @return WP_Error|bool
*/
public function resize_content_permissions_check( WP_REST_Request $request ) {
$post_id = $request->get_param( 'id' );
// Ensure we have a logged in user that can edit the item.
if ( empty( $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) {
return false;
}
$post_type = get_post_type( $post_id );
$post_type_obj = get_post_type_object( $post_type );
// Ensure the post type is allowed in REST endpoints.
if ( ! $post_type || empty( $post_type_obj ) || empty( $post_type_obj->show_in_rest ) ) {
return false;
}
// Ensure the feature is enabled. Also runs a user check.
if ( ! $this->is_feature_enabled() ) {
return new WP_Error( 'not_enabled', esc_html__( 'Rewrite Tone is not currently enabled.', 'classifai' ) );
}
return true;
}
/**
* Generic request handler for all our custom routes.
*
* @param WP_REST_Request $request The full request object.
* @return \WP_REST_Response
*/
public function rest_endpoint_callback( WP_REST_Request $request ) {
$route = $request->get_route();
if ( strpos( $route, '/classifai/v1/rewrite-tone' ) === 0 ) {
return rest_ensure_response(
$this->run(
$request->get_param( 'id' ),
'rewrite_tone',
[
'content' => $request->get_param( 'content' ),
'tone' => $request->get_param( 'tone' ),
'tones' => $this->get_tones(),
]
)
);
}
return parent::rest_endpoint_callback( $request );
}
/**
* Enqueue the editor scripts.
*/
public function enqueue_editor_assets() {
global $post;
if ( empty( $post ) || ! is_admin() ) {
return;
}
wp_enqueue_script(
'classifai-plugin-rewrite-tone-js',
CLASSIFAI_PLUGIN_URL . 'dist/classifai-plugin-rewrite-tone.js',
array_merge(
get_asset_info( 'classifai-plugin-rewrite-tone', 'dependencies' ),
array( Feature::PLUGIN_AREA_SCRIPT )
),
get_asset_info( 'classifai-plugin-rewrite-tone', 'version' ),
true
);
wp_enqueue_script( 'classifai-chat-ui-js' );
wp_add_inline_script(
'classifai-plugin-rewrite-tone-js',
sprintf(
'let classifaiRewriteToneTones = %s',
wp_json_encode(
array_map(
fn( $x ) => array(
'label' => $x['label'],
'value' => $x['value'],
'info' => $x['info'],
),
$this->get_tones()
)
)
)
);
}
/**
* Returns an array of tones available.
*
* @since x.x.x
*
* @return array
*/
private function get_tones() {
/**
* Filter the tones available for the Rewrite Tone feature.
*
* @since x.x.x
* @hook classifai_feature_rewrite_tone_get_tones
*
* @param {array} $tones Array of tones.
*
* @return {array} Filtered array of tones.
*/
return apply_filters(
'classifai_' . self::ID . 'get_tones',
array(
array(
'label' => __( '🙂 Friendly', 'classifai' ),
'value' => 'friendly',
'info' => __( 'Conversational and warm, like talking to a friend.', 'classifai' ),
'sys_prompt' => __(
'You are a friendly and approachable writer. Rewrite the given content using a conversational and welcoming tone. Use inclusive language and keep the reader engaged with warmth and clarity, as if you\'re talking to a friend.',
'classifai'
),
),
array(
'label' => __( '💼 Professional', 'classifai' ),
'value' => 'professional',
'info' => __( 'Formal and polished, suitable for business settings.', 'classifai' ),
'sys_prompt' => __(
'You are a professional copywriter. Rewrite the given content with a formal and polished tone suitable for a business or corporate audience. Maintain clarity and authority, avoid contractions, and use industry-appropriate language.',
'classifai'
),
),
array(
'label' => __( '🗣️ Persuasive', 'classifai' ),
'value' => 'persuasive',
'info' => __( 'Compelling and action-driven to influence readers.', 'classifai' ),
'sys_prompt' => __(
'You are a persuasive marketing expert. Rewrite the content to convince the reader to take action. Use compelling language, clear value propositions, and emotional appeal. Emphasize benefits over features, and guide the reader confidently toward the intended goal.',
'classifai'
),
),
array(
'label' => __( '📘 Educational', 'classifai' ),
'value' => 'educational',
'info' => __( 'Clear and instructive, ideal for teaching.', 'classifai' ),
'sys_prompt' => __(
'You are an educational content specialist. Rewrite the content in a tone that is instructive, clear, and easy to follow. Use simple, precise language to teach or explain, ensuring the reader understands the topic step by step.',
'classifai'
),
),
array(
'label' => __( '📖 Storytelling', 'classifai' ),
'value' => 'storytelling',
'info' => __( 'Narrative and engaging, turning facts into stories.', 'classifai' ),
'sys_prompt' => __(
'You are a skilled storyteller. Rewrite the content as a narrative that draws the reader in. Use vivid descriptions, emotional hooks, and a clear arc to transform information into an engaging story that resonates with the audience.',
'classifai'
),
),
)
);
}
/**
* Get the description for the enable field.
*
* @return string
*/
public function get_enable_description(): string {
return esc_html__( '"Condense this text" and "Expand this text" menu items will be added to the paragraph block\'s toolbar menu.', 'classifai' );
}
/**
* Add any needed custom fields.
*/
public function add_custom_settings_fields() {
$settings = $this->get_settings();
add_settings_field(
'rewrite_tone_prompt',
esc_html__( 'Prompt', 'classifai' ),
[ $this, 'render_prompt_repeater_field' ],
$this->get_option_name(),
$this->get_option_name() . '_section',
[
'label_for' => 'rewrite_tone_prompt',
'default_value' => $settings['rewrite_tone_prompt'],
'description' => esc_html__( 'Add a custom prompt, if desired.', 'classifai' ),
]
);
}
/**
* Returns the default settings for the feature.
*
* @return array
*/
public function get_feature_default_settings(): array {
return [
'rewrite_tone_prompt' => [
[
'title' => esc_html__( 'ClassifAI default', 'classifai' ),
'original' => 1,
],
],
'provider' => ChatGPT::ID,
];
}
/**
* Sanitizes the default feature settings.
*
* @param array $new_settings Settings being saved.
* @return array
*/
public function sanitize_default_feature_settings( array $new_settings ): array {
$new_settings['rewrite_tone_prompt'] = sanitize_prompts( 'rewrite_tone_prompt', $new_settings );
return $new_settings;
}
}