wp-duckbehaviorjournal/functions.php
Kumi b3246b8549
feat(meta): consolidate save logic for article meta boxes
Merged saving logic for article authors and DOI into a single function
to streamline and reduce redundant code. This ensures that both
meta fields are consistently handled during post save operations.
2024-06-20 14:47:10 +02:00

391 lines
16 KiB
PHP

<?php
function duck_behavior_journal_setup()
{
add_theme_support('post-thumbnails');
add_theme_support('title-tag');
register_nav_menus(array(
'primary' => __('Primary Menu', 'duck-behavior-journal'),
'footer' => __('Footer Menu', 'duck-behavior-journal'),
));
add_theme_support('custom-logo');
add_theme_support('custom-header', array(
'default-image' => get_template_directory_uri() . '/assets/img/hero.png',
'width' => 1920,
'height' => 1080,
'flex-height' => true,
'flex-width' => true,
));
add_theme_support('pagination');
}
add_action('after_setup_theme', 'duck_behavior_journal_setup');
function duck_behavior_journal_scripts()
{
wp_enqueue_style('bootstrap-css', get_template_directory_uri() . '/assets/dist/css/bootstrap.min.css');
wp_enqueue_style('theme-style', get_stylesheet_uri());
wp_enqueue_script('jquery', get_template_directory_uri() . '/assets/dist/js/jquery-3.5.1.min.js', array(), null, true);
wp_enqueue_script('bootstrap-js', get_template_directory_uri() . '/assets/dist/js/bootstrap.min.js', array(), null, true);
}
add_action('wp_enqueue_scripts', 'duck_behavior_journal_scripts');
require_once get_template_directory() . '/class-wp-bootstrap-navwalker.php';
function duck_behavior_journal_customizer($wp_customize)
{
// Hero Image Section
$wp_customize->add_section('hero_image_section', array(
'title' => __('Hero Section', 'duck-behavior-journal'),
'priority' => 30,
));
// Hero Image Setting
$wp_customize->add_setting('hero_image', array(
'default' => get_template_directory_uri() . '/assets/img/hero.png',
'transport' => 'refresh',
));
$wp_customize->add_control(new WP_Customize_Image_Control($wp_customize, 'hero_image_control', array(
'label' => __('Hero Image', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_image',
)));
// Hero Title Setting
$wp_customize->add_setting('hero_title', array(
'default' => 'Advancing the Understanding of Duck Behavior',
'transport' => 'refresh',
));
$wp_customize->add_control('hero_title_control', array(
'label' => __('Hero Title', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_title',
'type' => 'text',
));
// Hero Subtitle Setting
$wp_customize->add_setting('hero_subtitle', array(
'default' => 'Through Rigorous Scientific Research',
'transport' => 'refresh',
));
$wp_customize->add_control('hero_subtitle_control', array(
'label' => __('Hero Subtitle', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_subtitle',
'type' => 'text',
));
// Hero Primary Button Text Setting
$wp_customize->add_setting('hero_primary_button_text', array(
'default' => 'Submit Your Research',
'transport' => 'refresh',
));
$wp_customize->add_control('hero_primary_button_text_control', array(
'label' => __('Hero Primary Button Text', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_primary_button_text',
'type' => 'text',
));
// Hero Primary Button URL Setting
$wp_customize->add_setting('hero_primary_button_url', array(
'default' => '#',
'transport' => 'refresh',
));
$wp_customize->add_control('hero_primary_button_url_control', array(
'label' => __('Hero Primary Button URL', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_primary_button_url',
'type' => 'url',
));
// Hero Secondary Button Text Setting
$wp_customize->add_setting('hero_secondary_button_text', array(
'default' => 'Subscribe to the Journal',
'transport' => 'refresh',
));
$wp_customize->add_control('hero_secondary_button_text_control', array(
'label' => __('Hero Secondary Button Text', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_secondary_button_text',
'type' => 'text',
));
// Hero Secondary Button URL Setting
$wp_customize->add_setting('hero_secondary_button_url', array(
'default' => '#',
'transport' => 'refresh',
));
$wp_customize->add_control('hero_secondary_button_url_control', array(
'label' => __('Hero Secondary Button URL', 'duck-behavior-journal'),
'section' => 'hero_image_section',
'settings' => 'hero_secondary_button_url',
'type' => 'url',
));
// Footer Section
$wp_customize->add_section('footer_section', array(
'title' => __('Footer', 'duck-behavior-journal'),
'priority' => 40,
));
// Footer Text Setting
$wp_customize->add_setting('footer_text', array(
'default' => '&copy; ' . date('Y') . ' Duck Behavior Journal. All rights reserved.',
'transport' => 'refresh',
));
$wp_customize->add_control('footer_text_control', array(
'label' => __('Footer Text', 'duck-behavior-journal'),
'section' => 'footer_section',
'settings' => 'footer_text',
'type' => 'textarea',
));
// Footer Social Media Links
$social_media = array('facebook', 'twitter', 'linkedin');
foreach ($social_media as $platform) {
$wp_customize->add_setting("footer_{$platform}_link", array(
'default' => '#',
'transport' => 'refresh',
));
$wp_customize->add_control("footer_{$platform}_link_control", array(
'label' => __(ucfirst($platform) . ' URL', 'duck-behavior-journal'),
'section' => 'footer_section',
'settings' => "footer_{$platform}_link",
'type' => 'url',
));
}
}
add_action('customize_register', 'duck_behavior_journal_customizer');
function create_article_post_type()
{
$labels = array(
'name' => _x('Articles', 'Post type general name', 'duck-behavior-journal'),
'singular_name' => _x('Article', 'Post type singular name', 'duck-behavior-journal'),
'menu_name' => _x('Articles', 'Admin Menu text', 'duck-behavior-journal'),
'name_admin_bar' => _x('Article', 'Add New on Toolbar', 'duck-behavior-journal'),
'add_new' => __('Add New', 'duck-behavior-journal'),
'add_new_item' => __('Add New Article', 'duck-behavior-journal'),
'new_item' => __('New Article', 'duck-behavior-journal'),
'edit_item' => __('Edit Article', 'duck-behavior-journal'),
'view_item' => __('View Article', 'duck-behavior-journal'),
'all_items' => __('All Articles', 'duck-behavior-journal'),
'search_items' => __('Search Articles', 'duck-behavior-journal'),
'parent_item_colon' => __('Parent Articles:', 'duck-behavior-journal'),
'not_found' => __('No articles found.', 'duck-behavior-journal'),
'not_found_in_trash' => __('No articles found in Trash.', 'duck-behavior-journal'),
'featured_image' => _x('Featured Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'set_featured_image' => _x('Set featured image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'remove_featured_image' => _x('Remove featured image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'use_featured_image' => _x('Use as featured image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'duck-behavior-journal'),
'archives' => _x('Article archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'duck-behavior-journal'),
'insert_into_item' => _x('Insert into article', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'duck-behavior-journal'),
'uploaded_to_this_item' => _x('Uploaded to this article', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'duck-behavior-journal'),
'filter_items_list' => _x('Filter articles list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'duck-behavior-journal'),
'items_list_navigation' => _x('Articles list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'duck-behavior-journal'),
'items_list' => _x('Articles list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'duck-behavior-journal'),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array('slug' => 'article'),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),
);
register_post_type('article', $args);
}
add_action('init', 'create_article_post_type');
function create_publication_status_taxonomy()
{
$labels = array(
'name' => _x('Publication Statuses', 'taxonomy general name', 'duck-behavior-journal'),
'singular_name' => _x('Publication Status', 'taxonomy singular name', 'duck-behavior-journal'),
'search_items' => __('Search Publication Statuses', 'duck-behavior-journal'),
'all_items' => __('All Publication Statuses', 'duck-behavior-journal'),
'parent_item' => __('Parent Publication Status', 'duck-behavior-journal'),
'parent_item_colon' => __('Parent Publication Status:', 'duck-behavior-journal'),
'edit_item' => __('Edit Publication Status', 'duck-behavior-journal'),
'update_item' => __('Update Publication Status', 'duck-behavior-journal'),
'add_new_item' => __('Add New Publication Status', 'duck-behavior-journal'),
'new_item_name' => __('New Publication Status Name', 'duck-behavior-journal'),
'menu_name' => __('Publication Status', 'duck-behavior-journal'),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array('slug' => 'publication-status'),
);
register_taxonomy('publication_status', array('article'), $args);
}
add_action('init', 'create_publication_status_taxonomy', 0);
function add_article_meta_boxes()
{
add_meta_box(
'article_authors',
__('Article Authors', 'duck-behavior-journal'),
'render_article_authors_meta_box',
'article',
'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');
function render_article_authors_meta_box($post)
{
$authors = get_post_meta($post->ID, 'article_authors', true);
?>
<div id="article-authors">
<div id="article-authors-list">
<?php
if (!empty($authors)) {
foreach ($authors as $index => $author) {
?>
<p>
<input type="text" name="article_authors[]" value="<?php echo esc_attr($author); ?>" />
<button type="button" class="button remove-author-button" data-index="<?php echo $index; ?>"><?php _e('Remove', 'duck-behavior-journal'); ?></button>
</p>
<?php
}
} else {
?>
<p><input type="text" name="article_authors[]" value="" /></p>
<?php
}
?>
</div>
<button type="button" id="add-author-button" class="button"><?php _e('Add Author', 'duck-behavior-journal'); ?></button>
</div>
<script>
jQuery(document).ready(function($) {
$('#add-author-button').on('click', function() {
$('#article-authors-list').append('<p><input type="text" name="article_authors[]" value="" /><button type="button" class="button remove-author-button"><?php _e('Remove', 'duck-behavior-journal'); ?></button></p>');
});
$(document).on('click', '.remove-author-button', function() {
$(this).parent().remove();
});
});
</script>
<?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_meta_boxes($post_id)
{
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;
if (!isset($_POST['article_authors'])) return;
$authors = array_map('sanitize_text_field', $_POST['article_authors']);
update_post_meta($post_id, 'article_authors', $authors);
if (isset($_POST['article_doi'])) {
$doi = sanitize_text_field($_POST['article_doi']);
update_post_meta($post_id, 'article_doi', $doi);
}
}
add_action('save_post', 'save_article_meta_boxes');
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');