feat: add DOI functionality to articles

Introduced DOI meta box with a generator button in the article editor to assign and display DOIs for articles. Added functionality to save DOI values and correspondingly set up rewrite rules and redirections based on DOI queries. Display DOI on the article single view page for enhanced article identification and accessibility.
This commit is contained in:
Kumi 2024-06-20 14:44:20 +02:00
parent 04cab36072
commit 33f4d65bed
Signed by: kumi
GPG key ID: ECBCC9082395383F
2 changed files with 79 additions and 0 deletions

View file

@ -257,6 +257,15 @@ function add_article_meta_boxes()
'side',
'default'
);
add_meta_box(
'article_doi',
__('Article DOI', 'duck-behavior-journal'),
'render_article_doi_meta_box',
'article',
'side',
'default'
);
}
add_action('add_meta_boxes', 'add_article_meta_boxes');
@ -299,6 +308,32 @@ function render_article_authors_meta_box($post)
<?php
}
function render_article_doi_meta_box($post)
{
$doi = get_post_meta($post->ID, 'article_doi', true);
?>
<div id="article-doi">
<p>
<input type="text" name="article_doi" value="<?php echo esc_attr($doi); ?>" readonly />
<button type="button" id="generate-doi-button" class="button"><?php _e('Generate DOI', 'duck-behavior-journal'); ?></button>
</p>
</div>
<script>
function generateRandomDOI(prefix) {
const randomString = Math.random().toString(36).substring(2, 10);
return prefix + randomString;
}
jQuery(document).ready(function($) {
$('#generate-doi-button').on('click', function() {
const doi = generateRandomDOI('22.2222/DBJ/');
$('input[name="article_doi"]').val(doi);
});
});
</script>
<?php
}
function save_article_authors_meta_box($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
@ -307,4 +342,45 @@ function save_article_authors_meta_box($post_id)
$authors = array_map('sanitize_text_field', $_POST['article_authors']);
update_post_meta($post_id, 'article_authors', $authors);
}
add_action('save_post', 'save_article_authors_meta_box');
function add_doi_rewrite_rule()
{
add_rewrite_rule('^([0-9]{2})\.([0-9]{4})/([a-zA-Z0-9_\-\/]+)?$', 'index.php?doi=$matches[1].$matches[2]/$matches[3]', 'top');
}
add_action('init', 'add_doi_rewrite_rule');
function add_doi_query_var($vars)
{
$vars[] = 'doi';
return $vars;
}
add_filter('query_vars', 'add_doi_query_var');
function redirect_doi_to_article($query)
{
if (isset($query->query_vars['doi'])) {
$doi = $query->query_vars['doi'];
$args = array(
'post_type' => 'article',
'meta_query' => array(
array(
'key' => 'article_doi',
'value' => $doi,
'compare' => '='
)
)
);
$articles = new WP_Query($args);
if ($articles->have_posts()) {
$articles->the_post();
wp_redirect(get_permalink(), 301);
exit;
}
}
}
add_action('parse_query', 'redirect_doi_to_article');

View file

@ -15,6 +15,9 @@
the_author();
}
?>
<p class="text-muted">
<strong>DOI:</strong> <?php echo esc_html(get_post_meta(get_the_ID(), 'article_doi', true)); ?>
</p>
<p class="text-muted">
<?php
$terms = get_the_terms(get_the_ID(), 'publication_status');