-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsintacs-mwai-frontend-chatbot-settings-admin.php
More file actions
317 lines (280 loc) · 13 KB
/
sintacs-mwai-frontend-chatbot-settings-admin.php
File metadata and controls
317 lines (280 loc) · 13 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
<?php
class SintacsMwaiFrontendChatbotSettingsAdmin {
var $default_footer_info_text = '<ul>
<li>The blue dot icon <img draggable="false" role="img" class="emoji" alt="🔵" src="https://s.w.org/images/core/emoji/15.0.3/svg/1f535.svg"> indicates that the value of the field differs from the original value.</li>
<li>The "Save" button saves the settings to the user meta fields and overwrites the original values while chatting with this bot.</li>
<li>The "Save to Original" button saves the settings to the original values.</li>
<li>The "Reset to Original" button resets the field values to the original values.</li>
</ul>';
public function __construct() {
add_action( 'admin_menu',array( $this,'add_admin_menu' ),11 );
add_action( 'admin_init',array( $this,'settings_init' ) );
add_action( 'admin_enqueue_scripts',array( $this,'enqueue_admin_scripts' ) );
}
public function add_admin_menu() {
// Add submenu page to Meow Apps main menu
add_submenu_page(
'meowapps-main-menu',
'AI Engine Frontend Chatbot Settings',
'AI Engine Frontend Chatbot Settings',
'manage_options',
'sintacs-ai-engine-settings',
[ $this,'settings_page_html' ] // function
);
}
public function settings_page_html() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$chatbots = $this->get_all_chatbots(); // Fetch chatbots
$defaultChatbotId = ! empty( $chatbots ) ? esc_attr( $chatbots[0]['botId'] ) : ''; // Get the first chatbot ID
$users = get_users(); // Fetch all registered users
?>
<div class="wrap">
<h1><?= esc_html( get_admin_page_title() ); ?></h1>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<!-- main content -->
<div id="post-body-content">
<div class="meta-box-sortables ui-sortable">
<div class="postbox">
<h2 class="hndle"><span><?php _e( 'Settings','textdomain' ); ?></span></h2>
<div class="inside">
<form action="options.php" method="post">
<?php
settings_fields( 'ai_engine_frontend' );
do_settings_sections( 'ai_engine_frontend' );
submit_button( 'Save Settings' );
?>
<input type="hidden" id="parameters-order"
name="sintacs_mwai_chatbot_parameters_order"
value="<?php echo esc_attr( implode( ',',get_option( 'sintacs_mwai_chatbot_parameters_to_show',[] ) ) ); ?>">
</form>
<h2><?php _e( 'Shortcode','textdomain' ); ?></h2>
<p><?php _e( 'Use the following shortcode to insert the chatbot form into a post or page:','textdomain' ); ?></p>
<div class="shortcode-builder">
<label for="chatbot-select"><?php _e( 'Select Chatbot','textdomain' ); ?></label>
<select id="chatbot-select">
<?php foreach ( $chatbots as $chatbot ): ?>
<option value="<?= esc_attr( $chatbot['botId'] ); ?>"><?= esc_html( $chatbot['name'] ); ?></option>
<?php endforeach; ?>
</select>
<label for="user-select"><?php _e( 'Select Users','textdomain' ); ?></label>
<select id="user-select" multiple>
<?php foreach ( $users as $user ): ?>
<option value="<?= esc_attr( $user->user_email ); ?>"><?= esc_html( $user->user_email ); ?></option>
<?php endforeach; ?>
</select>
<label for="chatbot-shortcode"><?php _e( 'Generated Shortcode','textdomain' ); ?></label>
<input type="text" id="chatbot-shortcode"
value="[ai_engine_extension_form chatbot_id="<?= $defaultChatbotId; ?>"]"
readonly style="width: 100%; max-width: 600px;">
<div class="shortcode-button-group">
<button id="copy-shortcode-button" class="button"><?php _e( 'Copy Shortcode','textdomain' ); ?></button>
<button id="reset-shortcode-button" class="button"><?php _e( 'Reset','textdomain' ); ?></button>
</div>
</div>
<p><?php _e( 'If you do not specify a chatbot ID, it will default to the first chatbot on the current post / page.','textdomain' ); ?></p>
</div>
</div>
</div>
</div>
<!-- sidebar -->
<div id="postbox-container-1" class="postbox-container">
<div class="meta-box-sortables">
<div class="postbox">
<h2 class="hndle"><span><?php _e( 'FAQ','textdomain' ); ?></span></h2>
<div class="inside">
<div class="faq-item">
<h3><?php _e( 'How do I add a chatbot to a page?','textdomain' ); ?></h3>
<p><?php _e( 'Use the shortcode provided above to insert the chatbot form into any post or page.','textdomain' ); ?></p>
</div>
<div class="faq-item">
<h3><?php _e( 'How do I manage chatbot settings?','textdomain' ); ?></h3>
<p><?php _e( 'You can manage chatbot settings from this admin page. Make sure to save your changes.','textdomain' ); ?></p>
</div>
<div class="faq-item">
<h3><?php _e( 'What happens if I uninstall the plugin?','textdomain' ); ?></h3>
<p><?php _e( 'If you select the option to delete settings on uninstall, all settings will be removed when the plugin is deleted.','textdomain' ); ?></p>
</div>
<div class="faq-item">
<h3><?php _e( 'How do I sort the parameters?','textdomain' ); ?></h3>
<p><?php _e( 'You can sort the parameters by dragging and dropping them in the desired order in the "Parameters to Show" section. Remember to save your changes after sorting.','textdomain' ); ?></p>
</div>
</div>
</div>
</div>
</div>
</div>
<br class="clear">
</div>
</div>
<?php
}
private function get_all_chatbots() {
return get_option( 'mwai_chatbots',[] );
}
public function settings_init() {
register_setting( 'ai_engine_frontend','sintacs_mwai_chatbot_frontend_allowed_roles',array(
'sanitize_callback' => array( $this,'sanitize_allowed_roles' ),
'default' => array()
) );
register_setting( 'ai_engine_frontend','sintacs_mwai_chatbot_delete_settings_on_uninstall',array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '0'
) );
register_setting( 'ai_engine_frontend','sintacs_mwai_chatbot_show_save_to_original',array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '1'
) );
register_setting( 'ai_engine_frontend','sintacs_mwai_chatbot_parameters_to_show',array(
'sanitize_callback' => array( $this,'sanitize_parameters_to_show' ),
'default' => array()
) );
register_setting( 'ai_engine_frontend','sintacs_mwai_chatbot_show_footer_info',array(
'sanitize_callback' => 'sanitize_text_field',
'default' => '1'
) );
register_setting( 'ai_engine_frontend','sintacs_mwai_chatbot_footer_info_text',array(
'sanitize_callback' => 'wp_kses_post',
'default' => $this->default_footer_info_text
) );
add_settings_section(
'ai_engine_frontend_section',
__( 'AI Engine Frontend Chatbot Settings','textdomain' ),
null,
'ai_engine_frontend'
);
add_settings_field(
'sintacs_mwai_chatbot_parameters_to_show',
__( 'Parameters to Show','textdomain' ),
array( $this,'parameters_to_show_field_render' ),
'ai_engine_frontend',
'ai_engine_frontend_section'
);
add_settings_field(
'sintacs_mwai_chatbot_frontend_allowed_roles',
__( 'Allowed Roles','textdomain' ),
array( $this,'allowed_roles_field_render' ),
'ai_engine_frontend',
'ai_engine_frontend_section'
);
add_settings_field(
'sintacs_mwai_chatbot_delete_settings_on_uninstall',
__( 'Delete Settings on Uninstall','textdomain' ),
array( $this,'delete_settings_on_uninstall_field_render' ),
'ai_engine_frontend',
'ai_engine_frontend_section'
);
add_settings_field(
'sintacs_mwai_chatbot_show_save_to_original',
__( 'Show "Save to Original" Button','textdomain' ),
array( $this,'show_save_to_original_field_render' ),
'ai_engine_frontend',
'ai_engine_frontend_section'
);
add_settings_field(
'sintacs_mwai_chatbot_show_footer_info',
__( 'Show Footer Info','textdomain' ),
array( $this,'show_footer_info_field_render' ),
'ai_engine_frontend',
'ai_engine_frontend_section'
);
add_settings_field(
'sintacs_mwai_chatbot_footer_info_text',
__( 'Footer Info Text','textdomain' ),
array( $this,'footer_info_text_field_render' ),
'ai_engine_frontend',
'ai_engine_frontend_section'
);
}
public function allowed_roles_field_render() {
$options = get_option( 'sintacs_mwai_chatbot_frontend_allowed_roles' );
$roles = wp_roles()->roles;
$allowed_roles = is_array( $options ) ? $options : array();
echo '<select name="sintacs_mwai_chatbot_frontend_allowed_roles[]" multiple class="allowed-roles-select">';
foreach ( $roles as $role_key => $role_info ) {
$selected = in_array( $role_key,$allowed_roles ) ? 'selected' : '';
echo '<option value="' . esc_attr( $role_key ) . '" ' . $selected . '>' . esc_html( $role_info['name'] ) . '</option>';
}
echo '</select>';
}
public function delete_settings_on_uninstall_field_render() {
$option = get_option( 'sintacs_mwai_chatbot_delete_settings_on_uninstall','0' );
?>
<input type="checkbox" name="sintacs_mwai_chatbot_delete_settings_on_uninstall"
value="1" <?php checked( '1',$option ); ?> />
<label for="sintacs_mwai_chatbot_delete_settings_on_uninstall"><?php _e( 'Delete all settings when the plugin is deleted','textdomain' ); ?></label>
<?php
}
public function show_save_to_original_field_render() {
$option = get_option( 'sintacs_mwai_chatbot_show_save_to_original','1' );
?>
<input type="checkbox" name="sintacs_mwai_chatbot_show_save_to_original"
value="1" <?php checked( '1',$option ); ?> />
<label for="sintacs_mwai_chatbot_show_save_to_original"><?php _e( 'Show the "Save to Original" button on the frontend','textdomain' ); ?></label>
<?php
}
public function parameters_to_show_field_render() {
$saved_options = get_option( 'sintacs_mwai_chatbot_parameters_to_show',[] );
$all_parameters = SintacsMwaiFrontendChatbotSettings::get_all_parameter_names();
// Start with the parameters that are saved and checked
$ordered_parameters = array_intersect( $saved_options,$all_parameters );
// Add the remaining parameters that are not saved at the end
$remaining_parameters = array_diff( $all_parameters,$saved_options );
$ordered_parameters = array_merge( $ordered_parameters,$remaining_parameters );
echo '<p>';
echo '<button type="button" id="select-all" class="button button-secondary">Select All</button> ';
echo '<button type="button" id="deselect-all" class="button button-secondary">Deselect All</button>';
echo '</p>';
echo '<div class="params-to-show-wrap">';
echo '<ul id="sortable-parameters" class="connectedSortable">';
foreach ( $ordered_parameters as $parameter ) {
$checked = in_array( $parameter,$saved_options ) ? 'checked' : '';
$label = esc_html( ucfirst( SintacsMwaiFrontendChatbotSettings::split_camel_case( $parameter ) ) );
echo '<li class="ui-state-default"><input type="checkbox" name="sintacs_mwai_chatbot_parameters_to_show[]" value="' . esc_attr( $parameter ) . '" ' . $checked . '> ' . esc_html( $label ) . '</li>';
}
echo '</ul>';
echo '</div>';
}
public function sanitize_parameters_to_show() {
$ordered_input = isset( $_POST['sintacs_mwai_chatbot_parameters_order'] ) ? explode( ',',$_POST['sintacs_mwai_chatbot_parameters_order'] ) : [];
// The $ordered_input already maintains the order, so no further action is needed
// It's an indexed array with the correct order of keys
return $ordered_input;
}
public function settings_section_callback() {
echo __( 'Set the roles allowed to change settings.','textdomain' );
}
public function sanitize_allowed_roles( $input ): array {
$valid_roles = array_keys( wp_roles()->roles );
return array_intersect( $valid_roles,$input );
}
public function enqueue_admin_scripts( $hook ) {
if ( $hook !== 'meow-apps_page_sintacs-ai-engine-settings' ) {
return;
}
error_log('hook: ' . $hook);
wp_enqueue_script( 'sintacs-admin-js',plugin_dir_url( __FILE__ ) . 'assets/js/admin.js',array( 'jquery' ),null,true );
wp_enqueue_style( 'sintacs-admin-css',plugin_dir_url( __FILE__ ) . 'assets/css/admin.css' );
}
public function show_footer_info_field_render() {
$option = get_option( 'sintacs_mwai_chatbot_show_footer_info','1' );
?>
<input type="checkbox" name="sintacs_mwai_chatbot_show_footer_info"
value="1" <?php checked( '1',$option ); ?> />
<label for="sintacs_mwai_chatbot_show_footer_info"><?php _e( 'Show the footer info on the frontend','textdomain' ); ?></label>
<?php
}
public function footer_info_text_field_render() {
$option = get_option( 'sintacs_mwai_chatbot_footer_info_text',$this->default_footer_info_text );
wp_editor( $option,'sintacs_mwai_chatbot_footer_info_text',array(
'textarea_name' => 'sintacs_mwai_chatbot_footer_info_text',
'textarea_rows' => 10,
'media_buttons' => false,
'teeny' => true,
'quicktags' => false,
) );
}
}
new SintacsMwaiFrontendChatbotSettingsAdmin();