-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.php
More file actions
133 lines (118 loc) · 4.02 KB
/
Copy pathsettings.php
File metadata and controls
133 lines (118 loc) · 4.02 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
<?php
defined( 'ABSPATH' ) || exit;
/**
* Register settings and fields.
*/
function autorag_chatbot_register_settings() {
register_setting(
'autorag_chatbot_settings',
'autorag_chatbot_settings',
[
'sanitize_callback' => 'autorag_chatbot_sanitize_options',
'default' => [],
]
);
add_settings_section(
'autorag_chatbot_main',
__( 'Cloudflare credentials', 'autorag-chatbot' ),
'__return_false',
'autorag-chatbot'
);
$fields = [
'account_id' => [ 'label' => __( 'Account ID', 'autorag-chatbot' ), 'type' => 'text' ],
'rag_slug' => [ 'label' => __( 'RAG Slug', 'autorag-chatbot' ), 'type' => 'text' ],
'api_token' => [ 'label' => __( 'API Token', 'autorag-chatbot' ), 'type' => 'password' ],
'accent_color' => [ 'label' => __( 'Accent Colour (Hex)', 'autorag-chatbot' ),'type' => 'text', 'class'=>'color-field' ],
];
foreach ( $fields as $key => $field ) {
add_settings_field(
$key,
$field['label'],
'autorag_chatbot_render_input',
'autorag-chatbot',
'autorag_chatbot_main',
[
'label_for' => "autorag_chatbot_{$key}",
'type' => $field['type'],
'option_key'=> $key,
]
);
}
}
add_action( 'admin_init', 'autorag_chatbot_register_settings' );
/**
* Sanitize all options in one pass.
*/
function autorag_chatbot_sanitize_options( $raw ) {
$clean = [];
// plain text fields
foreach ( [ 'account_id', 'rag_slug', 'api_token' ] as $key ) {
if ( isset( $raw[ $key ] ) ) {
$clean[ $key ] = sanitize_text_field( $raw[ $key ] );
}
}
// hex colour (defaults to plugin orange if empty / invalid)
if ( isset( $raw['accent_color'] ) ) {
$color = sanitize_hex_color( $raw['accent_color'] );
$clean['accent_color'] = $color ? $color : '#d96704';
}
return $clean;
}
/**
* Generic input renderer for the settings fields.
*/
function autorag_chatbot_render_input( $args ) {
$options = get_option( 'autorag_chatbot_settings', [] );
$key = $args['option_key'];
$val = $options[ $key ] ?? '';
printf(
'<input id="%1$s" name="autorag_chatbot_settings[%2$s]" type="%3$s" value="%4$s" class="regular-text" />',
esc_attr( $args['label_for'] ),
esc_attr( $key ),
esc_attr( $args['type'] ),
esc_attr( $val )
);
}
/**
* Settings page markup.
*/
function autorag_chatbot_settings_page() {
?>
<div class="wrap">
<div style="display:flex;align-items:center;gap:12px;margin-bottom:24px">
<img src="<?php echo esc_url( AUTORAG_CB_URL . 'assets/neuno_colour_no_background.png' ); ?>" alt="" style="width:56px;height:56px;border-radius:8px;">
<h1 style="margin:0;font-size:26px;">
<?php esc_html_e( 'WP Cloudflare AutoRAG Chatbot', 'autorag-chatbot' ); ?>
<span style="display:block;font-size:14px;font-weight:400;color:#666"><?php esc_html_e( 'by neuno.ai', 'autorag-chatbot' ); ?></span>
</h1>
</div>
<?php
// embed quick start file if exists
$quick = AUTORAG_CB_DIR . 'quick-start-guide.php';
if ( file_exists( $quick ) ) {
include $quick;
}
?>
<form method="post" action="options.php" style="margin-top:30px">
<?php
settings_fields( 'autorag_chatbot_settings' );
do_settings_sections( 'autorag-chatbot' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Add the plugin settings page to the Settings menu.
*/
function autorag_chatbot_add_menu() {
add_options_page(
__( 'AutoRAG Chatbot', 'autorag-chatbot' ),
__( 'AutoRAG Chatbot', 'autorag-chatbot' ),
'manage_options',
'autorag-chatbot',
'autorag_chatbot_settings_page'
);
}
add_action( 'admin_menu', 'autorag_chatbot_add_menu' );