Skip to content

Commit 0c4851e

Browse files
committed
ai suggestion
1 parent afb735b commit 0c4851e

16 files changed

+437
-17
lines changed

assets/css/rtafar-admin-global-style.min.css

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/css/rtafar-admin-style.min.css

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/img/ai-technology.png

785 Bytes
Loading

assets/js/rtafar.admin.global.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/rtafar.admin.replace.in.db.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/rtafar.ai.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/rtafar.app.admin.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/rtafar.app.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/js/rtafar.media.replacer.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/actions/RTAFAR_RegisterMenu.php

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use RealTimeAutoFindReplace\admin\functions\Masking;
1717
use RealTimeAutoFindReplace\admin\options\Scripts_Settings;
1818
use RealTimeAutoFindReplace\admin\builders\AdminPageBuilder;
19+
use RealTimeAutoFindReplace\admin\functions\aiHandler;
1920

2021

2122
class RTAFAR_RegisterMenu {
@@ -83,13 +84,24 @@ public function rtafar_register_menu() {
8384
57
8485
);
8586

87+
$this->rtafr_menus['ai_settings'] = add_submenu_page(
88+
CS_RTAFAR_PLUGIN_IDENTIFIER,
89+
__( 'AI Configuration', 'real-time-auto-find-and-replace' ),
90+
__( 'AI Settings', 'real-time-auto-find-and-replace' ),
91+
'read',
92+
'cs-bfar-ai-settings',
93+
array( $this, 'rtafar_page_ai_settings' ),
94+
1
95+
);
96+
8697
$this->rtafr_menus['add_masking_rule'] = add_submenu_page(
8798
CS_RTAFAR_PLUGIN_IDENTIFIER,
8899
__( 'Add Replacement Rule', 'real-time-auto-find-and-replace' ),
89100
__( 'Add New Rule', 'real-time-auto-find-and-replace' ),
90101
'read',
91102
'cs-add-replacement-rule',
92-
array( $this, 'rtafr_page_add_rule' )
103+
array( $this, 'rtafr_page_add_rule' ),
104+
2
93105
);
94106

95107
$this->rtafr_menus['all_masking_rules'] = add_submenu_page(
@@ -98,7 +110,8 @@ public function rtafar_register_menu() {
98110
__( 'All Replacement Rules', 'real-time-auto-find-and-replace' ),
99111
'read',
100112
'cs-all-masking-rules',
101-
array( $this, 'rtafr_page_all_masking_rules' )
113+
array( $this, 'rtafr_page_all_masking_rules' ),
114+
3
102115
);
103116

104117
$this->rtafr_menus['replace_in_db'] = add_submenu_page(
@@ -107,7 +120,8 @@ public function rtafar_register_menu() {
107120
__( 'Replace in Database', 'real-time-auto-find-and-replace' ),
108121
'read',
109122
'cs-replace-in-database',
110-
array( $this, 'rtafr_page_replace_in_db' )
123+
array( $this, 'rtafr_page_replace_in_db' ),
124+
4
111125
);
112126

113127
$this->rtafr_menus['media_replacer'] = add_submenu_page(
@@ -116,7 +130,8 @@ public function rtafar_register_menu() {
116130
__( 'Media Replacer', 'real-time-auto-find-and-replace' ),
117131
'read',
118132
'cs-bfar-media-replacer',
119-
array( $this, 'rtafar_page_media_replacer' )
133+
array( $this, 'rtafar_page_media_replacer' ),
134+
5
120135
);
121136

122137
$this->rtafr_menus['restore_in_db_pro'] = add_submenu_page(
@@ -125,16 +140,20 @@ public function rtafar_register_menu() {
125140
__( 'Restore', 'real-time-auto-find-and-replace' ),
126141
'read',
127142
'cs-bfar-restore-database-pro',
128-
array( $this, 'rtafar_page_restore_db' )
143+
array( $this, 'rtafar_page_restore_db' ),
144+
6
129145
);
130146

147+
148+
131149
$this->rtafr_menus['go_pro'] = add_submenu_page(
132150
CS_RTAFAR_PLUGIN_IDENTIFIER,
133151
__( 'Go Pro', 'real-time-auto-find-and-replace' ),
134152
'<span class="dashicons dashicons-star-filled" style="font-size: 17px"></span> ' . __( 'Go Pro', 'real-time-auto-find-and-replace' ),
135153
'read',
136154
'cs-bfar-go-pro',
137-
array( $this, 'rtafar_handle_external_redirects' )
155+
array( $this, 'rtafar_handle_external_redirects' ),
156+
7
138157
);
139158

140159
// load script
@@ -143,6 +162,7 @@ public function rtafar_register_menu() {
143162
add_action( "load-{$this->rtafr_menus['replace_in_db']}", array( $this, 'rtafr_register_admin_settings_scripts' ) );
144163
add_action( "load-{$this->rtafr_menus['restore_in_db_pro']}", array( $this, 'rtafr_register_admin_settings_scripts' ) );
145164
add_action( "load-{$this->rtafr_menus['media_replacer']}", array( $this, 'rtafr_register_admin_settings_scripts' ) );
165+
add_action( "load-{$this->rtafr_menus['ai_settings']}", array( $this, 'rtafr_register_admin_settings_scripts' ) );
146166

147167
\remove_submenu_page( CS_RTAFAR_PLUGIN_IDENTIFIER, CS_RTAFAR_PLUGIN_IDENTIFIER );
148168

@@ -277,7 +297,7 @@ public function rtafar_page_media_replacer() {
277297
'sub_title' => __( 'Search for specific media files by name and easily replace them with new uploads', 'real-time-auto-find-and-replace' ),
278298
);
279299

280-
if ( current_user_can( 'manage_options' ) || current_user_can( 'administrator' ) || current_user_can( Util::bfar_nav_cap('restore_in_db') ) ) {
300+
if ( current_user_can( 'manage_options' ) || current_user_can( 'administrator' ) || current_user_can( Util::bfar_nav_cap('media_replacer') ) ) {
281301

282302
$MediaReplacer = $this->pages->MediaReplacer();
283303
if ( \is_object( $MediaReplacer ) ) {
@@ -296,6 +316,36 @@ public function rtafar_page_media_replacer() {
296316
}
297317
}
298318

319+
public function rtafar_page_ai_settings() {
320+
$page_info = array(
321+
'title' => __( 'Optional: Enable AI-Powered Suggestions', 'real-time-auto-find-and-replace' ),
322+
'sub_title' => __( 'To use AI-generated text suggestions in your replacement, complete the following settings. Leave this blank if you don\'t wish to use AI features.', 'real-time-auto-find-and-replace' ),
323+
);
324+
325+
$get_settings = aiHandler::getSettings();
326+
if ( empty( $get_settings ) ) {
327+
$get_settings = array();
328+
}
329+
330+
if ( current_user_can( 'manage_options' ) || current_user_can( 'administrator' ) || current_user_can( Util::bfar_nav_cap('ai_settings') ) ) {
331+
332+
$AISettings = $this->pages->AISettings();
333+
if ( \is_object( $AISettings ) ) {
334+
echo $AISettings->generate_page( array_merge_recursive( $page_info, array( 'default_settings' => array() ), $get_settings ) );
335+
} else {
336+
echo $AISettings, Util::cs_allowed_html();
337+
}
338+
339+
} else {
340+
$AccessDenied = $this->pages->AccessDenied();
341+
if ( \is_object( $AccessDenied ) ) {
342+
echo $AccessDenied->generate_access_denided( array_merge_recursive( $page_info, array( 'default_settings' => array() ), $get_settings ) );
343+
} else {
344+
echo $AccessDenied, Util::cs_allowed_html();
345+
}
346+
}
347+
}
348+
299349
/**
300350
* generate instance
301351
*

core/admin/builders/FormBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public function generate_hidden_fields( $fields ) {
102102
* @param type $field
103103
*/
104104
private function generate_field( $field_name, $field, $field_id ) {
105-
$input = '<div class="label"><label>';
105+
106+
$input = '<div class="label"><label class="label-'.$field_id.'">';
106107
$input .= $this->generate_title( $field );
107108
$input .= '</label></div>';
108109
$input .= '<div class="input-group">';

core/admin/functions/aiHandler.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php namespace RealTimeAutoFindReplace\admin\functions;
2+
3+
4+
5+
/**
6+
* Class: DB replacer
7+
*
8+
* @package Admin
9+
* @since 1.3.1
10+
* @author M.Tuhin <info@codesolz.net>
11+
*/
12+
13+
if ( ! defined( 'CS_RTAFAR_VERSION' ) ) {
14+
die();
15+
}
16+
17+
use RealTimeAutoFindReplace\lib\Util;
18+
19+
class aiHandler {
20+
21+
/**
22+
* Get AI settings
23+
*
24+
* @return array
25+
*/
26+
private static $optionKey = 'cs_ai_config';
27+
private static $openAiEndpoint = 'https://api.openai.com/v1/chat/completions';
28+
29+
/**
30+
* Get AI settings
31+
*
32+
* @return array
33+
*/
34+
public static function getSettings() {
35+
$settings = get_option( self::$optionKey, array() );
36+
37+
if ( ! empty( $settings ) ) {
38+
return $settings;
39+
}
40+
41+
return array();
42+
}
43+
44+
/**
45+
* Save AI settings
46+
*
47+
* @param array $data
48+
*/
49+
public static function saveSettings( $data ) {
50+
if ( ! empty( $data['cs_ai_config'] ) ) {
51+
$settings = array(
52+
'api_key' => sanitize_text_field( $data['cs_ai_config']['api_key'] ),
53+
'language_model' => sanitize_text_field( $data['cs_ai_config']['language_model'] ),
54+
);
55+
56+
// Check if the user has permission to save settings
57+
if ( !current_user_can( 'manage_options' ) && !current_user_can( Util::bfar_nav_cap('replace_in_db') ) ) {
58+
return wp_send_json(
59+
array(
60+
'status' => false,
61+
'title' => __( 'Access Denied', 'real-time-auto-find-and-replace' ),
62+
'text' => __( 'You do not have permission to perform this action.', 'real-time-auto-find-and-replace' ),
63+
)
64+
);
65+
}
66+
67+
update_option( self::$optionKey, $settings );
68+
return wp_send_json(
69+
array(
70+
'status' => true,
71+
'title' => __( 'Success', 'real-time-auto-find-and-replace' ),
72+
'text' => __( 'Settings saved successfully.', 'real-time-auto-find-and-replace' ),
73+
)
74+
);
75+
76+
}
77+
78+
return wp_send_json(
79+
array(
80+
'status' => false,
81+
'title' => __( 'Error', 'real-time-auto-find-and-replace' ),
82+
'text' => __( 'Failed to save settings.', 'real-time-auto-find-and-replace' ),
83+
)
84+
);
85+
}
86+
87+
/**
88+
* Get AI suggestion
89+
*
90+
* @param array $userInput
91+
* @return array
92+
*/
93+
public function getAiSuggestion( $userInput ){
94+
$AISettings = self::getSettings();
95+
if ( empty( $AISettings ) || empty( $AISettings['api_key'] ) ) {
96+
return wp_send_json( array(
97+
'status' => false,
98+
'title' => __( 'Error', 'real-time-auto-find-and-replace' ),
99+
'text' => __( 'API key is not set.', 'real-time-auto-find-and-replace' ),
100+
));
101+
}
102+
103+
$text = Util::check_evil_script( $userInput['find'] );
104+
if ( empty( $text ) ) {
105+
return wp_send_json( array(
106+
'status' => false,
107+
'title' => __( 'Error', 'real-time-auto-find-and-replace' ),
108+
'text' => __( 'Please enter find text to get suggestion.', 'real-time-auto-find-and-replace' ),
109+
));
110+
}
111+
112+
$body = [
113+
'model' => $AISettings['language_model'],
114+
'messages' => [
115+
['role' => 'system', 'content' => 'You are a helpful assistant that rewrites text to be more persuasive and SEO-friendly.'],
116+
['role' => 'user', 'content' => "Rewrite this phrase for a website: \"$text\""]
117+
],
118+
'temperature' => 0.7,
119+
'max_tokens' => 60
120+
];
121+
122+
$response = wp_remote_post( self::$openAiEndpoint , [
123+
'headers' => [
124+
'Authorization' => 'Bearer ' . $AISettings['api_key'],
125+
'Content-Type' => 'application/json',
126+
],
127+
'body' => json_encode($body),
128+
'timeout' => 20,
129+
]);
130+
131+
$response_code = wp_remote_retrieve_response_code( $response );
132+
133+
if ( $response_code !== 200 ) {
134+
$body = wp_remote_retrieve_body( $response );
135+
$data = json_decode( $body, true );
136+
137+
// pre_print( $data );
138+
139+
if ( isset( $data['error']['message'] ) ) {
140+
141+
return wp_send_json( array(
142+
'status' => false,
143+
'title' => __( 'API Error', 'real-time-auto-find-and-replace' ),
144+
'text' => esc_html( $data['error']['message'] ),
145+
));
146+
} else {
147+
return wp_send_json(array(
148+
'status' => false,
149+
'title' => __( 'API Error', 'real-time-auto-find-and-replace' ),
150+
'text' => esc_html( 'API returned HTTP ' . $response_code ),
151+
));
152+
}
153+
}
154+
155+
$body = json_decode(wp_remote_retrieve_body($response), true);
156+
157+
if (!isset($body['choices'][0]['message']['content'])) {
158+
return wp_send_json( array(
159+
'status' => false,
160+
'title' => __( 'AI Error', 'real-time-auto-find-and-replace' ),
161+
'text' => __( 'Invalid AI response.', 'real-time-auto-find-and-replace' ),
162+
));
163+
}
164+
165+
return wp_send_json( array(
166+
'status' => true,
167+
'title' => __( 'Applied', 'real-time-auto-find-and-replace' ),
168+
'text' => __( 'The replacement text has been updated.', 'real-time-auto-find-and-replace' ),
169+
'suggestion' => trim($body['choices'][0]['message']['content'])
170+
));
171+
}
172+
173+
}
174+
175+
176+
?>

core/admin/options/Scripts_Settings.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,24 @@ public static function load_admin_settings_scripts( $page_id, $rtafr_menu ) {
4141
true
4242
);
4343

44+
45+
wp_enqueue_script(
46+
'ratfar.ai.features',
47+
CS_RTAFAR_PLUGIN_ASSET_URI . 'js/rtafar.ai.min.js',
48+
array(),
49+
CS_RTAFAR_VERSION,
50+
true
51+
);
52+
53+
// load vars
54+
wp_localize_script(
55+
'ratfar.ai.features',
56+
'ratfar',
57+
array(
58+
'ai_icon' => CS_RTAFAR_PLUGIN_ASSET_URI . 'img/ai-technology.png',
59+
)
60+
);
61+
4462
}
4563

4664
if ( ( isset( $rtafr_menu['replace_in_db'] ) && $page_id == $rtafr_menu['replace_in_db'] )
@@ -69,7 +87,8 @@ public static function load_admin_settings_scripts( $page_id, $rtafr_menu ) {
6987

7088
if ( ( isset( $rtafr_menu['add_masking_rule'] ) && $page_id == $rtafr_menu['add_masking_rule'] ) ||
7189
( isset( $rtafr_menu['replace_in_db'] ) && $page_id == $rtafr_menu['replace_in_db'] ) ||
72-
( isset( $rtafr_menu['brafp_license'] ) && $page_id == $rtafr_menu['brafp_license'] )
90+
( isset( $rtafr_menu['brafp_license'] ) && $page_id == $rtafr_menu['brafp_license'] ) ||
91+
( isset( $rtafr_menu['ai_settings'] ) && $page_id == $rtafr_menu['ai_settings'] )
7392
) {
7493
wp_enqueue_script(
7594
'rtafar.app.admin.min',

0 commit comments

Comments
 (0)