Repository navigation
Expand file tree
/
Copy pathwp-react-admin-panel.php
More file actions
202 lines (189 loc) · 5.3 KB
/
Copy pathwp-react-admin-panel.php
File metadata and controls
202 lines (189 loc) · 5.3 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
<?php
/**
* Plugin Name: WP React Admin Panel
* Plugin URI: https://github.com/your-username/wp-react-admin-panel
* Description: A React-based admin panel for WordPress customization settings
* Version: 1.0.0
* Requires at least: 5.8
* Requires PHP: 7.2
* Author: Kamal Hosen
* Author URI: https://example.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-react-admin-panel
* Domain Path: /languages
*
* @package WP_React_Admin_Panel
*/
if (!defined('ABSPATH')) {
exit;
}
/**
* Register the admin menu page for the plugin.
*
* Creates a top-level menu item in the WordPress admin sidebar
* with the title "React Admin" and appropriate capabilities.
*
* @since 1.0.0
* @return void
*/
function wrp_admin_menu() {
add_menu_page(
'WP React Admin Panel',
'React Admin',
'manage_options',
'wp-react-admin-panel',
'wrp_admin_page',
'dashicons-admin-generic',
30
);
}
add_action('admin_menu', 'wrp_admin_menu');
/**
* Render the admin page content.
*
* Outputs the container div where the React application will be mounted.
*
* @since 1.0.0
* @return void
*/
function wrp_admin_page() {
echo '<div id="wp-react-admin-panel"></div>';
}
/**
* Enqueue scripts and styles for the admin page.
*
* Loads the compiled React application and its dependencies only on the plugin's admin page.
*
* @since 1.0.0
* @param string $hook The current admin page hook.
* @return void
*/
function wrp_admin_enqueue_scripts($hook) {
if ('toplevel_page_wp-react-admin-panel' !== $hook) {
return;
}
$asset_file = include(plugin_dir_path(__FILE__) . 'build/index.asset.php');
wp_enqueue_script(
'wp-react-admin-panel',
plugin_dir_url(__FILE__) . 'build/index.js',
$asset_file['dependencies'],
$asset_file['version'],
true
);
$css_file = plugin_dir_path(__FILE__) . 'build/index.css';
if (file_exists($css_file)) {
wp_enqueue_style(
'wp-react-admin-panel',
plugin_dir_url(__FILE__) . 'build/index.css',
['wp-components'],
filemtime($css_file)
);
}
}
add_action('admin_enqueue_scripts', 'wrp_admin_enqueue_scripts');
/**
* Register REST API endpoints for the plugin settings.
*
* Creates GET and POST endpoints to retrieve and update plugin settings.
*
* @since 1.0.0
* @return void
*/
function wrp_register_rest_route() {
register_rest_route('wp/v2/settings', '/wrp_settings', array(
array(
'methods' => 'POST',
'callback' => 'wrp_update_settings',
'permission_callback' => function() {
return current_user_can('manage_options');
}
),
array(
'methods' => 'GET',
'callback' => 'wrp_get_settings',
'permission_callback' => function() {
return current_user_can('manage_options');
}
)
));
}
/**
* Callback for the settings GET endpoint.
*
* Retrieves the current plugin settings from the database or returns default values.
*
* @since 1.0.0
* @return WP_REST_Response The settings response object.
*/
function wrp_get_settings() {
$settings = get_option('wrp_settings');
if (!$settings) {
$settings = array(
'general' => array(
'site_title' => '',
'admin_email' => ''
),
'appearance' => array(
'admin_color' => '#ffffff',
'menu_position' => 'left'
),
'advanced' => array(
'custom_css' => '',
'custom_js' => ''
)
);
}
return new WP_REST_Response($settings, 200);
}
add_action('rest_api_init', 'wrp_register_rest_route');
/**
* Callback for the settings POST endpoint.
*
* Updates the plugin settings in the database based on the request data.
*
* @since 1.0.0
* @param WP_REST_Request $request The request object containing the settings data.
* @return WP_REST_Response The response object with updated settings or error message.
*/
function wrp_update_settings($request) {
$settings = $request->get_json_params();
$updated = update_option('wrp_settings', $settings);
if ($updated) {
return new WP_REST_Response($settings, 200);
}
return new WP_REST_Response(array('message' => 'Failed to update settings'), 500);
}
/**
* Register the plugin settings in WordPress.
*
* Defines the settings schema and makes the settings available via the REST API.
*
* @since 1.0.0
* @return void
*/
function wrp_register_settings() {
register_setting(
'wp_react_admin_panel',
'wrp_settings',
array(
'type' => 'object',
'default' => array(
'general' => array(
'site_title' => '',
'admin_email' => ''
),
'appearance' => array(
'admin_color' => '#ffffff',
'menu_position' => 'left'
),
'advanced' => array(
'custom_css' => '',
'custom_js' => ''
)
),
'show_in_rest' => true,
)
);
}
add_action('init', 'wrp_register_settings');