-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexternal-dashboard-form.php
More file actions
283 lines (246 loc) · 8.64 KB
/
external-dashboard-form.php
File metadata and controls
283 lines (246 loc) · 8.64 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
<?php
/**
* Plugin Name: External Dashboard Forms
* Plugin URI:
* Description: Allows administrators to embed custom external forms directly on the WordPress dashboard screen
* Version: 1.0
* Author: Monroe Digital
* Author URI: https://www.monroedigitalconsulting.com/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: external-dashboard-form
* Domain Path: /languages
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('EDF_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('EDF_PLUGIN_URL', plugin_dir_url(__FILE__));
define('EDF_PLUGIN_VERSION', '1.0');
// Include admin functions
require_once EDF_PLUGIN_DIR . 'admin/form-manager.php';
/**
* Initialize the plugin
*/
function edf_init() {
// Register dashboard widgets for each saved form
add_action('wp_dashboard_setup', 'edf_setup_dashboard_widgets');
// Admin menu
add_action('admin_menu', 'edf_admin_menu');
// Add admin scripts and styles
add_action('admin_enqueue_scripts', 'edf_admin_scripts');
}
add_action('plugins_loaded', 'edf_init');
/**
* Register admin menu
*/
function edf_admin_menu() {
// Add top level menu
add_menu_page(
__('External Forms', 'external-dashboard-form'),
__('External Forms', 'external-dashboard-form'),
'manage_options',
'external-forms',
'edf_forms_page',
'dashicons-feedback',
30
);
// Get saved forms
$forms = get_option('external_dashboard_forms', array());
// Add submenu items for each form
if (!empty($forms)) {
foreach ($forms as $form) {
add_submenu_page(
'external-forms',
$form['name'],
$form['name'],
'manage_options',
'index.php#' . sanitize_title($form['name']),
''
);
}
}
}
/**
* Admin page for managing forms
*/
function edf_forms_page() {
// Check user capabilities
if (!current_user_can('manage_options')) {
return;
}
// Process form submission
if (isset($_POST['edf_add_form_nonce']) && wp_verify_nonce($_POST['edf_add_form_nonce'], 'edf_add_form')) {
// Process form submission
edf_process_form_submission();
}
// Get existing forms
$forms = get_option('external_dashboard_forms', array());
// Display the form
?>
<div class="wrap">
<h1><?php echo esc_html(get_admin_page_title()); ?></h1>
<h2><?php _e('Add New Form', 'external-dashboard-form'); ?></h2>
<form method="post" action="">
<?php wp_nonce_field('edf_add_form', 'edf_add_form_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="form_name"><?php _e('Form Name', 'external-dashboard-form'); ?></label></th>
<td>
<input name="form_name" type="text" id="form_name" value="" class="regular-text" required>
</td>
</tr>
<tr>
<th scope="row"><label for="form_url"><?php _e('Form URL', 'external-dashboard-form'); ?></label></th>
<td>
<input name="form_url" type="url" id="form_url" value="" class="regular-text" required>
<p class="description"><?php _e('URL must begin with https://', 'external-dashboard-form'); ?></p>
</td>
</tr>
</table>
<?php submit_button(__('Add Form', 'external-dashboard-form')); ?>
</form>
<?php if (!empty($forms)) : ?>
<h2><?php _e('Existing Forms', 'external-dashboard-form'); ?></h2>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th><?php _e('Form Name', 'external-dashboard-form'); ?></th>
<th><?php _e('Form URL', 'external-dashboard-form'); ?></th>
<th><?php _e('Actions', 'external-dashboard-form'); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($forms as $form_id => $form) : ?>
<tr>
<td><?php echo esc_html($form['name']); ?></td>
<td><?php echo esc_url($form['url']); ?></td>
<td>
<a href="<?php echo esc_url(admin_url('index.php#' . sanitize_title($form['name']))); ?>"><?php _e('View on Dashboard', 'external-dashboard-form'); ?></a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
</div>
<?php
}
/**
* Process form submission
*/
function edf_process_form_submission() {
// Check for required fields
if (!isset($_POST['form_name']) || !isset($_POST['form_url'])) {
add_settings_error('external_dashboard_form', 'missing_fields', __('Form name and URL are required.', 'external-dashboard-form'), 'error');
return;
}
$form_name = sanitize_text_field($_POST['form_name']);
$form_url = esc_url_raw($_POST['form_url']);
// Validate URL
if (empty($form_name)) {
add_settings_error('external_dashboard_form', 'invalid_name', __('Form name cannot be empty.', 'external-dashboard-form'), 'error');
return;
}
// Validate URL starts with https://
if (strpos($form_url, 'https://') !== 0) {
add_settings_error('external_dashboard_form', 'invalid_url', __('Form URL must begin with https://.', 'external-dashboard-form'), 'error');
return;
}
// Get existing forms
$forms = get_option('external_dashboard_forms', array());
// Generate unique ID
$form_id = 'form_' . time();
// Add new form
$forms[$form_id] = array(
'id' => $form_id,
'name' => $form_name,
'url' => $form_url
);
// Save to database
update_option('external_dashboard_forms', $forms);
add_settings_error('external_dashboard_form', 'form_added', __('Form added successfully.', 'external-dashboard-form'), 'success');
}
/**
* Set up dashboard widgets
*/
function edf_setup_dashboard_widgets() {
// Check user capabilities
if (!current_user_can('manage_options')) {
return;
}
// Get saved forms
$forms = get_option('external_dashboard_forms', array());
if (!empty($forms)) {
foreach ($forms as $form_id => $form) {
wp_add_dashboard_widget(
sanitize_title($form['name']),
$form['name'],
'edf_render_dashboard_widget',
'edf_dashboard_widget_control',
array('form_id' => $form_id)
);
}
}
}
/**
* Render the dashboard widget
*/
function edf_render_dashboard_widget($post, $callback_args) {
$form_id = $callback_args['args']['form_id'];
$forms = get_option('external_dashboard_forms', array());
if (isset($forms[$form_id])) {
$form = $forms[$form_id];
echo '<div class="edf-iframe-container">';
echo '<iframe src="' . esc_url($form['url']) . '" width="100%" height="400" frameborder="0"></iframe>';
echo '</div>';
}
}
/**
* Dashboard widget control callback
*/
function edf_dashboard_widget_control($widget_id, $callback_args) {
$form_id = $callback_args['args']['form_id'];
// Process removal if requested
if (isset($_POST['edf_remove_form']) && $_POST['edf_remove_form'] == $form_id) {
edf_remove_form($form_id);
?>
<script>
window.location.reload();
</script>
<?php
return;
}
// Show remove form button
?>
<form method="post">
<input type="hidden" name="edf_remove_form" value="<?php echo esc_attr($form_id); ?>">
<?php submit_button(__('Remove Form', 'external-dashboard-form'), 'delete', 'edf_remove_form_submit', false); ?>
</form>
<?php
}
/**
* Remove a form from the database
*/
function edf_remove_form($form_id) {
$forms = get_option('external_dashboard_forms', array());
if (isset($forms[$form_id])) {
unset($forms[$form_id]);
update_option('external_dashboard_forms', $forms);
return true;
}
return false;
}
/**
* Enqueue admin scripts and styles
*/
function edf_admin_scripts($hook) {
// Only on dashboard
if ($hook != 'index.php' && $hook != 'toplevel_page_external-forms') {
return;
}
// Add custom CSS
wp_enqueue_style('edf-admin-style', EDF_PLUGIN_URL . 'assets/style.css', array(), EDF_PLUGIN_VERSION);
}