-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathplugin.php
More file actions
235 lines (207 loc) · 6.6 KB
/
Copy pathplugin.php
File metadata and controls
235 lines (207 loc) · 6.6 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
<?php
/**
* Plugin Name: Graph View
* Description: Visualize your WordPress content in a Graph View, similar to Obsidian. Support for Admin, Gutenberg, and Frontend.
* Version: 2.0.0
* Author: James LePage
* Author URI: https://www.j.cv
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if (!defined('ABSPATH')) {
exit;
}
define('GRAPHVIEW_VERSION', '2.0.0');
define('GRAPHVIEW_PLUGIN_URL', plugin_dir_url(__FILE__));
define('GRAPHVIEW_PLUGIN_DIR', plugin_dir_path(__FILE__));
// Include core classes
require_once GRAPHVIEW_PLUGIN_DIR . 'includes/class-graphview-databuilder.php';
require_once GRAPHVIEW_PLUGIN_DIR . 'includes/class-graphview-rest.php';
require_once GRAPHVIEW_PLUGIN_DIR . 'includes/class-graphview-adminpage.php';
// Set default options on activation
register_activation_hook(__FILE__, 'graphview_set_default_options');
function graphview_set_default_options()
{
if (!get_option('graphview_auto_insert')) {
update_option('graphview_auto_insert', 'yes');
}
if (!get_option('graphview_position')) {
update_option('graphview_position', 'below');
}
}
/**
* Initialize the plugin
*/
function graphview_init_plugin()
{
// Register REST routes
add_action('rest_api_init', array('graphview_REST', 'register_routes'));
// Enqueue our built React scripts in admin
add_action('admin_enqueue_scripts', 'graphview_enqueue_admin_scripts');
// Enqueue our built React scripts on the front end
add_action('wp_enqueue_scripts', 'graphview_enqueue_frontend_scripts');
// Enqueue Gutenberg sidebar script
add_action('enqueue_block_editor_assets', 'graphview_enqueue_gutenberg_scripts');
// Add admin menu
add_action('admin_menu', 'graphview_register_admin_page');
// Handle content insertion based on position setting
$auto_insert = get_option('graphview_auto_insert', 'yes');
if ($auto_insert === 'yes') {
$position = get_option('graphview_position', 'below');
if ($position === 'above') {
add_filter('the_content', 'graphview_prepend_to_content', 5);
} else {
add_filter('the_content', 'graphview_append_to_content', 20);
}
}
}
add_action('plugins_loaded', 'graphview_init_plugin');
/**
* Add graph above content
*/
function graphview_prepend_to_content($content)
{
if (!is_single() && !is_page()) {
return $content;
}
$graph_container = '<div id="graphview-frontend-root"></div>';
return $graph_container . $content;
}
/**
* Add graph below content
*/
function graphview_append_to_content($content)
{
if (!is_single() && !is_page()) {
return $content;
}
$graph_container = '<div id="graphview-frontend-root"></div>';
return $content . $graph_container;
}
/**
* Enqueue the admin bundle
*/
function graphview_enqueue_admin_scripts()
{
$screen = get_current_screen();
if (!isset($screen->id)) {
return;
}
// Only load on our plugin's admin page
if ($screen->id === 'toplevel_page_graphview') {
// Enqueue the compiled React admin script
wp_enqueue_script(
'graphview-admin-bundle',
GRAPHVIEW_PLUGIN_URL . 'build/admin.js',
array('wp-element'), // or empty array if not using WP's React
GRAPHVIEW_VERSION,
true
);
// Provide REST URL, nonce, settings, etc. to the script
wp_localize_script('graphview-admin-bundle', 'graphviewAdminData', array(
'restUrl' => esc_url_raw(rest_url('graphview/v1')),
'nonce' => wp_create_nonce('wp_rest'),
'themeColors' => graphview_get_theme_colors(),
));
}
}
/**
* Enqueue the frontend bundle
*/
function graphview_enqueue_frontend_scripts()
{
$auto_insert = get_option('graphview_auto_insert', 'yes');
if (($auto_insert === 'yes') && (is_single() || is_page())) {
// Enqueue the compiled React front-end script
wp_enqueue_script(
'graphview-frontend-bundle',
GRAPHVIEW_PLUGIN_URL . 'build/frontend.js',
array(
'react',
'react-dom',
'wp-components',
'wp-element',
'wp-primitives',
'wp-icons'
),
GRAPHVIEW_VERSION,
true
);
// Get current post URL for comparison
$current_post_id = get_the_ID();
$current_post_url = get_permalink($current_post_id);
// Pass data
wp_localize_script('graphview-frontend-bundle', 'graphviewData', array(
'restUrl' => esc_url_raw(rest_url('graphview/v1')),
'currentPostId' => $current_post_id ? $current_post_id : 0,
'currentPostUrl' => $current_post_url,
'themeColors' => graphview_get_theme_colors(),
));
}
}
/**
* Basic function to pull theme colors
*/
function graphview_get_theme_colors()
{
// Fallback color set; adjust as desired
$colors = array(
'primary' => '#9370DB',
'secondary' => '#444444',
'tertiary' => '#cccccc',
);
// If you want to read from theme.json or global settings, do so here...
// For brevity, we just return the fallback array
return $colors;
}
/**
* Register the Admin Menu
*/
function graphview_register_admin_page()
{
add_menu_page(
__('Graph View', 'my-graph-view'),
__('Graph View', 'my-graph-view'),
'manage_options',
'graphview',
array('graphview_AdminPage', 'render_graph'),
'dashicons-networking',
90
);
add_submenu_page(
'graphview',
__('Graph Settings', 'my-graph-view'),
__('Settings', 'my-graph-view'),
'manage_options',
'graphview-settings',
array('graphview_AdminPage', 'render_settings')
);
}
/**
* Enqueue the Gutenberg sidebar script
*/
function graphview_enqueue_gutenberg_scripts()
{
wp_enqueue_script(
'graphview-gutenberg',
GRAPHVIEW_PLUGIN_URL . 'build/gutenberg.js',
array(
'wp-plugins',
'wp-edit-post',
'wp-components',
'wp-i18n',
'wp-data',
'wp-element',
'react',
'react-dom'
),
GRAPHVIEW_VERSION,
true
);
// Pass data to the script
wp_localize_script('graphview-gutenberg', 'graphviewData', array(
'restUrl' => esc_url_raw(rest_url('graphview/v1')),
'nonce' => wp_create_nonce('wp_rest'),
'themeColors' => graphview_get_theme_colors(),
));
}