-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-reference-manager.php
207 lines (179 loc) · 6 KB
/
wp-reference-manager.php
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
<?php
/**
* Plugin Name: WP Reference Manager
* Plugin URI: https://your-website.com/wp-reference-manager
* Description: A modern block editor plugin for managing references, citations, and footnotes with support for PMID, DOI, URL, and ISBN.
* Version: 1.1.0
* Author: Your Name
* Author URI: https://your-website.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: wp-reference-manager
* Domain Path: /languages
*
* @package WP_Reference_Manager
*/
// Exit if accessed directly.
if (!defined('ABSPATH')) {
exit;
}
// Define plugin constants
define('WP_REFERENCE_MANAGER_VERSION', '1.1.0');
define('WP_REFERENCE_MANAGER_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WP_REFERENCE_MANAGER_PLUGIN_URL', plugin_dir_url(__FILE__));
/**
* Main plugin class
*/
class WP_Reference_Manager {
/**
* Instance of this class
*
* @var WP_Reference_Manager
*/
private static $instance = null;
/**
* Get instance of this class
*
* @return WP_Reference_Manager
*/
public static function get_instance() {
if (null === self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Constructor
*/
private function __construct() {
$this->init();
}
/**
* Initialize plugin
*/
private function init() {
// Load translations
add_action('init', array($this, 'load_textdomain'));
// Register post meta
add_action('init', array($this, 'register_post_meta'));
// Register blocks
add_action('init', array($this, 'register_blocks'));
// Fetch and display reference list in the editor
add_action('rest_api_init', function() {
register_rest_field('post', '_wp_reference_list', array(
'get_callback' => function($post_arr) {
return get_post_meta($post_arr['id'], '_wp_reference_list', true);
},
'schema' => array(
'type' => 'array',
'context' => array('view', 'edit')
),
));
});
// Enqueue editor assets
add_action('enqueue_block_editor_assets', array($this, 'enqueue_editor_assets'));
// Save reference list to post meta on post save
add_action('save_post', array($this, 'save_reference_list'));
}
/**
* Load plugin textdomain
*/
public function load_textdomain() {
load_plugin_textdomain(
'wp-reference-manager',
false,
dirname(plugin_basename(__FILE__)) . '/languages'
);
}
/**
* Register post meta
*/
public function register_post_meta() {
register_post_meta('', '_wp_reference_list', array(
'show_in_rest' => array(
'schema' => array(
'type' => 'array',
'items' => array(
'type' => 'string', // Assuming each reference is a string
),
),
),
'single' => true,
'type' => 'array',
'default' => array(),
'auth_callback' => function() {
return current_user_can('edit_posts');
},
));
}
/**
* Register blocks
*/
public function register_blocks() {
// Register blocks here
if (!function_exists('register_block_type')) {
return;
}
// Will be implemented in blocks/index.js
register_block_type('wp-reference-manager/citation-block');
register_block_type('wp-reference-manager/footnote-block');
register_block_type('wp-reference-manager/reference-list-block');
}
/**
* Enqueue editor assets
*/
public function enqueue_editor_assets() {
$asset_file_path = WP_REFERENCE_MANAGER_PLUGIN_DIR . 'build/index.asset.php';
if (!file_exists($asset_file_path)) {
error_log('WP Reference Manager: Asset file not found: ' . $asset_file_path);
return;
}
$asset_file = include($asset_file_path);
if (!is_array($asset_file) || !isset($asset_file['dependencies']) || !isset($asset_file['version'])) {
error_log('WP Reference Manager: Invalid asset file structure');
return;
}
wp_enqueue_script(
'wp-reference-manager-editor',
WP_REFERENCE_MANAGER_PLUGIN_URL . 'build/index.js',
array_merge($asset_file['dependencies'], array('wp-plugins', 'wp-edit-post')),
$asset_file['version']
);
wp_enqueue_style(
'wp-reference-manager-editor',
WP_REFERENCE_MANAGER_PLUGIN_URL . 'build/style-main.css',
array('wp-edit-blocks'),
$asset_file['version']
);
}
/**
* Save reference list to post meta on post save
*
* @param int $post_id The ID of the post being saved.
*/
public function save_reference_list($post_id) {
// Check if this is an autosave or a revision, and if so, return early
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
if (wp_is_post_revision($post_id)) {
return;
}
// Check if the current user has permission to edit the post
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Get the reference list from the request
$reference_list = isset($_POST['_wp_reference_list']) ? $_POST['_wp_reference_list'] : [];
// Log the reference list for debugging
error_log('Saving reference list for post ID ' . $post_id . ': ' . print_r($reference_list, true));
// Update the post meta
update_post_meta($post_id, '_wp_reference_list', $reference_list);
}
}
// Initialize the plugin
function wp_reference_manager() {
return WP_Reference_Manager::get_instance();
}
// Start the plugin
wp_reference_manager();