-
Notifications
You must be signed in to change notification settings - Fork 0
/
publications-manager.php
executable file
·406 lines (322 loc) · 16 KB
/
publications-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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
<?php
/*
Plugin Name: Publications Manager
Plugin URI: https://github.com/idiv-biodiversity/publications-manager
Description: WordPress plugin for managing publications at <a href="https://idiv.de" target="_blank">iDiv</a>
Version: 1.0.5-alpha
Author: Christian Langer
Author URI: https://github.com/christianlanger
Text Domain: publications-manager
GitHub Plugin URI: https://github.com/idiv-biodiversity/publications-manager
*/
/* ################################################################ */
/* BASIC INIT STUFF */
/* ################################################################ */
// Shared function to enqueue Bootstrap, Font Awesome, Selectpicker, and Toggle Switch
function enqueue_publications_manager_shared_assets() {
// Ensure jQuery is enqueued
wp_enqueue_script('jquery');
// Bootstrap 5
if ( ! wp_style_is( 'bootstrap-css', 'enqueued' ) ) {
wp_enqueue_style('bootstrap-css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css');
}
if ( ! wp_script_is( 'bootstrap-js', 'enqueued' ) ) {
wp_enqueue_script('bootstrap-js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js', array('jquery'), '5.3.0', true);
}
// Font Awesome CSS
if ( ! wp_style_is( 'font-awesome', 'enqueued' ) ) {
wp_enqueue_style('font-awesome', 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css', array(), '5.15.4', 'all');
}
// Selectpicker (Bootstrap 5 compatible)
wp_enqueue_style('bootstrap-select-css', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-select.min.css');
wp_enqueue_script('bootstrap-select-js', 'https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap-select.min.js', array('jquery', 'bootstrap-js'), null, true);
// Bootstrap Toggle Switch
wp_enqueue_style('bootstrap-toggle-css', 'https://cdn.jsdelivr.net/npm/[email protected]/css/bootstrap5-toggle.min.css');
wp_enqueue_script('bootstrap-toggle-js', 'https://cdn.jsdelivr.net/npm/[email protected]/js/bootstrap5-toggle.ecmas.min.js', array('jquery'));
}
// Enqueue scripts and styles for Frontend
add_action('wp_enqueue_scripts', 'enqueue_publications_manager_frontend_scripts');
function enqueue_publications_manager_frontend_scripts() {
enqueue_publications_manager_shared_assets(); // Load shared assets
// Custom Frontend CSS and JS
wp_enqueue_style('custom-frontend-css', plugin_dir_url(__FILE__) . 'css/frontend.css');
wp_enqueue_script('custom-frontend-js', plugin_dir_url(__FILE__) . 'js/frontend.js', array('jquery'));
}
// Enqueue scripts and styles for Backend
add_action('admin_enqueue_scripts', 'enqueue_publications_manager_backend_scripts');
function enqueue_publications_manager_backend_scripts() {
enqueue_publications_manager_shared_assets(); // Load shared assets
// Editor Scripts
wp_enqueue_script('editor');
wp_enqueue_script('quicktags');
wp_enqueue_style('editor-buttons');
// Custom Backend CSS and JS
wp_enqueue_style('custom-backend-css', plugin_dir_url(__FILE__) . 'css/backend.css');
wp_enqueue_script('custom-backend-js', plugin_dir_url(__FILE__) . 'js/backend.js', array('jquery'));
wp_localize_script('custom-backend-js', 'ajax_object', array('ajaxurl' => admin_url('admin-ajax.php'))); // Localize script data
}
//$github_token = getenv('GITHUB_TOKEN'); // Load GitHub token from environment variable
$github_token = file_get_contents('/var/www/github_token'); // Read the GitHub token from the file
// Includes
include_once(plugin_dir_path(__FILE__) . 'conf.php');
include_once(plugin_dir_path(__FILE__) . 'functions.php');
// Create the database table on plugin activation
register_activation_hook(__FILE__, 'publications_manager_create_table');
function publications_manager_create_table() {
global $wpdb;
$table_name = $wpdb->prefix . 'idiv_publications';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE IF NOT EXISTS $table_name (
id INT AUTO_INCREMENT PRIMARY KEY,
rec_id INT,
ref_type VARCHAR(50) NOT NULL,
title TEXT NOT NULL,
authors TEXT NOT NULL,
journal VARCHAR(255),
book_title VARCHAR(255),
editors TEXT,
publisher VARCHAR(255),
year_published INT,
doi_link VARCHAR(255),
doi_open_access INT DEFAULT 0,
pdf_link VARCHAR(255),
data_link VARCHAR(255),
code_link VARCHAR(255),
custom_link VARCHAR(255),
custom_link_name VARCHAR(255),
employees TEXT,
departments TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_updated TIMESTAMP,
pub_status INT DEFAULT 0
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
// Create admin menu
add_action('admin_menu', 'publications_manager_menu');
function publications_manager_menu() {
add_menu_page('Publications Manager', 'Publications Manager', 'manage_options', 'publications_manager', 'publications_manager_page','dashicons-book');
// Submenu page
//add_submenu_page('publications_manager', 'Edit Publication', 'Edit Publication', 'manage_options', 'edit_publication', 'edit_publication_page');
// Register a hidden submenu page
add_submenu_page(null, 'Edit Publication', 'Edit Publication', 'manage_options', 'edit_publication', 'edit_publication_page');
}
// Enqueue block scripts
add_action('enqueue_block_editor_assets', 'publications_manager_blocks_enqueue');
function publications_manager_blocks_enqueue() {
wp_enqueue_script(
'publications-manager-blocks',
plugin_dir_url(__FILE__) . 'js/blocks.js',
array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-data', 'wp-i18n'),
filemtime(plugin_dir_path(__FILE__) . 'js/blocks.js')
);
}
/* ################################################################ */
/* UPDATE PROCESS */
/* ################################################################ */
// Force WordPress to check for plugin updates
//delete_site_transient('update_plugins');
//Update Check
add_filter('site_transient_update_plugins', 'plugin_update_publications_check');
function plugin_update_publications_check($transient) {
global $github_token;
//echo $github_token;
if (empty($transient->checked)) {
return $transient;
}
$plugin_slug = plugin_basename(__FILE__);
$response = wp_remote_get('https://api.github.com/repos/idiv-biodiversity/publications-manager/releases', array(
'headers' => array(
'Authorization' => 'token ' . $github_token,
'User-Agent' => 'Publications Manager'
)
));
if (is_wp_error($response)) {
error_log('Error: ' . $response->get_error_message());
return $transient;
}
$github_data = json_decode(wp_remote_retrieve_body($response), true);
// Get the first release, assuming it’s the latest
$latest_release = $github_data[0];
// Parse the version from GitHub (removing 'v' prefix if present)
$new_version = str_replace('v', '', $latest_release['name']);
// Compare the version from GitHub with the plugin version
if (version_compare($transient->checked[$plugin_slug], $new_version, '<')) {
$plugin = array(
'slug' => $plugin_slug,
'new_version' => $new_version,
'url' => $latest_release['html_url'],
'package' => $latest_release['zipball_url'],
);
$transient->response[$plugin_slug] = (object) $plugin;
}
return $transient;
}
// Rename the plugin folder after the update and keep it active
add_filter('upgrader_post_install', 'rename_plugin_publications_folder', 10, 3);
function rename_plugin_publications_folder($response, $hook_extra, $result) {
global $wp_filesystem;
if ($hook_extra['plugin'] !== plugin_basename(__FILE__)) {
return $response;
}
$proper_folder_name = 'publications-manager';
$destination = WP_PLUGIN_DIR . '/' . $proper_folder_name;
// Rename the plugin directory if it's different
if ($result['destination_name'] !== $proper_folder_name) {
$wp_filesystem->move($result['destination'], $destination);
$result['destination'] = $destination;
$result['destination_name'] = $proper_folder_name;
}
// Re-activate the plugin after renaming
activate_plugin($proper_folder_name . '/' . plugin_basename(__FILE__));
return $response;
}
// View details window for new release
add_filter('plugins_api', 'plugin_update_publications_details', 10, 3);
function plugin_update_publications_details($false, $action, $args) {
global $github_token;
// Check if the action is for plugin information
if ($action !== 'plugin_information') {
return $false;
}
$plugin_slug = plugin_basename(__FILE__);
if ($args->slug !== $plugin_slug) {
return $false;
}
// Fetch release details from GitHub
$response = wp_remote_get('https://api.github.com/repos/idiv-biodiversity/publications-manager/releases', array(
'headers' => array(
'Authorization' => 'token ' . $github_token,
'User-Agent' => 'Publications Manager'
)
));
if (is_wp_error($response)) {
error_log('Error: ' . $response->get_error_message());
return $false; // Return false if the request failed
}
$github_data = json_decode(wp_remote_retrieve_body($response), true);
if (empty($github_data) || !is_array($github_data)) {
return $false; // Return false if there is no release data
}
// Get the first release, assuming it’s the latest
$latest_release = $github_data[0];
$plugin_info = new stdClass();
$plugin_info->name = 'Publications Manager';
$plugin_info->slug = $plugin_slug;
$plugin_info->version = str_replace('v', '', $latest_release['name']);
$plugin_info->author = '<a href="https://github.com/ChristianLanger">Christian Langer</a>';
$plugin_info->homepage = 'https://github.com/idiv-biodiversity/publications-manager';
$plugin_info->download_link = $latest_release['zipball_url'];
$plugin_info->sections = array(
'description' => 'WordPress plugin for managing Publications at <a href="https://idiv.de" target="_blank">iDiv</a>',
'changelog' => '<h4>Version ' . str_replace('v', '', $latest_release['name']) . '</h4>'
);
$plugin_info->banners = array(
'low' => 'https://home.uni-leipzig.de/idiv/main-page/banner-low-res.jpg',
'high' => 'https://home.uni-leipzig.de/idiv/main-page/banner-high-res.jpg'
);
return $plugin_info;
}
/* ################################################################ */
/* FUNCTIONS */
/* ################################################################ */
// Callback function for the publication manager page
function publications_manager_page() {
wrapper_start();
publications_upload();
publications_table();
wrapper_end();
exit();
}
// Callback function for the edit publication page
function edit_publication_page() {
wrapper_start();
// Include the PHP file containing the content for the edit publication page
include_once(plugin_dir_path(__FILE__) . 'edit-publication.php');
wrapper_end();
}
/* ################################################################ */
/* Publikationsliste (vollständig) */
/* ################################################################ */
// // shortcode for Frontend
// add_shortcode('publications_public_table', 'publications_manager_public_table_shortcode');
// function publications_manager_public_table_shortcode($atts) {
// ob_start(); // Start output buffering to capture the HTML content
// // Get publications from the database
// global $wpdb;
// $publications = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1");
// // Load the template file and capture its output
// include(plugin_dir_path(__FILE__) . 'templates/publications-list.php');
// return ob_get_clean(); // Return the captured HTML content
// }
add_action('init', 'register_all_publications_block');
function register_all_publications_block() {
register_block_type('publications-manager/all-publications', array(
'render_callback' => 'render_all_publications_block',
));
}
function render_all_publications_block($attributes) {
ob_start(); // Start output buffering to capture the HTML content
// Get publications from the database
global $wpdb;
$publications = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1");
$years = $wpdb->get_col("SELECT DISTINCT year_published FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1 ORDER BY year_published DESC");
// Load the template file and capture its output
include(plugin_dir_path(__FILE__) . 'templates/publications-list.php');
return ob_get_clean(); // Return the captured HTML content
}
/* ################################################################ */
/* Publikationsliste (Arbeitsgruppe) */
/* ################################################################ */
add_action('init', 'register_group_publications_block');
function register_group_publications_block() {
register_block_type('publications-manager/group-publications', array(
'render_callback' => 'render_group_publications_block',
));
}
function render_group_publications_block($attributes) {
$selected_group_id = isset($attributes['id']) ? intval($attributes['id']) : 0;
$current_language= apply_filters( 'wpml_current_language', NULL );
if ($current_language === 'de'){
$selected_group_id = apply_filters( 'wpml_object_id', $selected_group_id, 'post', FALSE, 'en' );
}
if ($selected_group_id === 0) {
return '<p>No group selected.</p>';
}
ob_start(); // Start output buffering to capture the HTML content
// Get publications from the database
global $wpdb;
$publications = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1 AND FIND_IN_SET({$selected_group_id}, departments) > 0;");
$years = $wpdb->get_col("SELECT DISTINCT year_published FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1 AND FIND_IN_SET({$selected_group_id}, departments) > 0 ORDER BY year_published DESC");
// Load the template file and capture its output
include(plugin_dir_path(__FILE__) . 'templates/publications-list.php');
return ob_get_clean(); // Return the captured HTML content
}
/* ################################################################ */
/* Publikationsliste (individuell) */
/* ################################################################ */
add_action('init', 'register_single_publications_block');
function register_single_publications_block() {
register_block_type('publications-manager/single-publications', array(
'render_callback' => 'render_single_publications_block',
));
}
function render_single_publications_block($attributes) {
$selected_staff_id = isset($attributes['id']) ? intval($attributes['id']) : 0;
$current_language= apply_filters( 'wpml_current_language', NULL );
if ($current_language === 'de'){
$selected_staff_id = apply_filters( 'wpml_object_id', $selected_staff_id, 'post', FALSE, 'en' );
}
if ($selected_staff_id === 0) {
return '<p>No employee selected.</p>';
}
ob_start(); // Start output buffering to capture the HTML content
// Get publications from the database
global $wpdb;
$publications = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1 AND FIND_IN_SET({$selected_staff_id}, employees) > 0;");
$years = $wpdb->get_col("SELECT DISTINCT year_published FROM {$wpdb->prefix}idiv_publications WHERE pub_status = 1 AND FIND_IN_SET({$selected_staff_id}, employees) > 0 ORDER BY year_published DESC");
// Load the template file and capture its output
include(plugin_dir_path(__FILE__) . 'templates/publications-list.php');
return ob_get_clean(); // Return the captured HTML content
}