Skip to content

Commit

Permalink
Add ability to have multiple authors. DRY out template tags. Minor im…
Browse files Browse the repository at this point in the history
…provement to back-end styling.
  • Loading branch information
rjbaniel committed Oct 31, 2017
1 parent 4cd220a commit d330a4a
Show file tree
Hide file tree
Showing 5 changed files with 379 additions and 112 deletions.
127 changes: 127 additions & 0 deletions field-tags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php
function scholarpress_coins__author_fields( $data, $locked ) {
$authors = array();
$legacy_author = array();
if ( ! is_array( $data['_coins-author-first'] ) ) {
$legacy_author['first'] = $data['_coins-author-first'];
if ( $data['_coins-author-last'] )
$legacy_author['last'] = $data['_coins-author-last'];
array_push($authors, $legacy_author);
} else {
foreach( $data['_coins-author-first'] as $index => $author_first ) {
array_push($authors,
array(
'first' => $author_first,
'last' => $data['_coins-author-last'][$index],
)
);
}
}
?>
<div class="scholarpress-field__input">
<fieldset <?php if ( $locked ) echo 'disabled'; ?> id="coins-author">
<legend>Authors</legend>
<?php
foreach ( $authors as $index => $author ) {
scholarpress_coins__single_author_fieldset( $author, $index );
}
?>
</fieldset>

<button id="coins-add-author" class="add_author_button" <?php if ( $locked ) echo 'disabled'; ?>>Add author</button>
</div>

<div class="scholarpress-field__lock">
<input
id="coins-author-lock"
name="_coins-author-lock"
type="checkbox"
<?php if ( $locked ) echo ' checked'; ?>
>
<label for="coins-author-lock">
<?php _e( 'Lock fields to post author?', 'scholarpress-coins' ); ?>
</label>
</div>
<?php
global $post;
$default_author_data = get_userdata( $post->post_author );
$default_author_first = $default_author_data->first_name;
$default_author_last = $default_author_data->last_name;

if ( ! ($default_author_last && $default_author_first ) ) {
$default_author_first = $default_author_data->display_name;
$default_author_last = '';
}
?>
<input type="hidden" class="coins-author-first-hidden" value="<?php echo esc_attr( $default_author_first ); ?>">
<input type="hidden" class="coins-author-last-hidden" value="<?php echo esc_attr( $default_author_last ); ?>">

<?php
}

function scholarpress_coins__single_author_fieldset( $author, $field_id_index ) {
?>
<fieldset class="coins_single_author">
<label>
<?php _e( 'First name:', 'scholarpress-coins' ); ?>
<input
class="widefat"
<?php if ( $field_id_index === 0 ) : ?>
id="coins-author-first-primary"
<?php endif; ?>
name="_coins-author-first[]"
type="text"
value="<?php echo esc_attr( $author['first'] ); ?>"
>
</label>
<label>
<?php _e( 'Last name:', 'scholarpress-coins' ); ?>
<input
class="widefat"
<?php if ( $field_id_index === 0 ) : ?>
id="coins-author-last-primary"
<?php endif; ?>
name="_coins-author-last[]"
type="text"
<?php if ( isset( $author['last'] ) ) : ?>
value="<?php echo esc_attr( $author['last'] ); ?>"
<?php endif; ?>
>
</label>
</fieldset>
<?php
}

function scholarpress_coins__input_field( $field, $title, $value, $locked ) {
?>
<div class="scholarpress-field__input">
<label for="<?php echo esc_attr( 'coins-' . $field ); ?>">
<?php echo esc_attr( $title ); ?></label>
<input
class="widefat"
id="<?php echo esc_attr( 'coins-' . $field ); ?>"
name="<?php echo esc_attr( '_coins-' . $field ); ?>"
type="text"
value="<?php echo esc_attr( $value ); ?>"
<?php if ( $locked ) echo ' disabled'; ?>
>
</input>
</div>
<?php
}

function scholarpress_coins__field_lock( $field, $locked, $label_text ) {
?>
<div class="scholarpress-field__lock">
<input
id="coins-<?php echo esc_attr( $field ); ?>-lock"
name="_coins-<?php echo esc_attr( $field ); ?>-lock"
type="checkbox"'
<?php if ( $locked ) echo ' checked'; ?>
>
<label for="coins-<?php echo esc_attr( $field ); ?>-lock"><?php echo esc_html( $label_text ); ?></label>
</div>
<?php
}

?>
7 changes: 6 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

function scholarpress_coins__enqueue_assets() {
wp_enqueue_script( 'scholarpress-coins', plugins_url( '/js/scholarpress-coins.js', __FILE__ ), array(), '2.1', true );
wp_enqueue_style( 'scholarpress-coins', plugins_url( 'scholarpress-coins.css', __FILE__ ) );
}

function scholarpress_coins_keys() {
return array(
'_coins-title',
Expand All @@ -14,7 +19,6 @@ function scholarpress_coins_keys() {
function scholarpress_coins_prepare_data_for_display( $post_id ) {
if( empty( $post_id ) )
return false;

$legacy_data = scholarpress_coins_get_legacy_post_data();
$return_data = array();

Expand All @@ -35,6 +39,7 @@ function scholarpress_coins_prepare_data_for_display( $post_id ) {
}
}
return $return_data;

}
function scholarpress_coins_get_legacy_post_data( $post_id = null ) {
global $post, $authordata;
Expand Down
121 changes: 102 additions & 19 deletions js/scholarpress-coins.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,112 @@
jQuery( document ).ready( function() {
var coins_metabox = document.getElementById( 'scholarpress-coins-meta-box' );
if ( coins_metabox === null ) {
jQuery(document).ready(function() {
var coins_metabox = document.getElementById('scholarpress-coins-meta-box');
if (coins_metabox === null) {
return false;
}
var fields_to_lock = ['title', 'author-first', 'subjects', 'identifier'];
fields_to_lock.forEach( function( field ) {
var field_lock_id = 'coins-' + field + '-lock';
document.getElementById( field_lock_id ).addEventListener( 'click', function( e ) {
var field_id = e.target.id.replace('-lock', '');
var field = document.getElementById( field_id );
if ( this.checked ) {
field.disabled = true;
field.value = document.getElementsByClassName(field_id + '-hidden')[0].value;
if ( field_id == 'coins-author-first' ) {
var last_name_field = document.getElementById( 'coins-author-last' );
last_name_field.disabled = true;
var add_author_button = document.getElementById('coins-add-author');
add_author_button.addEventListener('click', coins_add_author);

var author_fieldsets = document.getElementsByClassName( "coins_single_author" );
for ( var i = 1; i < author_fieldsets.length; i++ ) {
add_remove_button( author_fieldsets[i] );
}

var locking_field_names = ['title', 'author', 'subjects', 'identifier'];
locking_field_names.forEach( function(field) {
var locking_field_checkbox = document.getElementById('coins-' + field + '-lock');
if(!locking_field_checkbox) {
return;
}
locking_field_checkbox.addEventListener('click', function(e) {
var locking_field_id = e.target.id.replace('-lock', '');
var locking_field = document.getElementById( locking_field_id );

if (this.checked) {
locking_field.disabled = true;
if ( locking_field_id == 'coins-author' ) {
for (var i = 1; i < author_fieldsets.length; i++) {
author_fieldsets[i].remove();
}
document.getElementById('coins-add-author').disabled = true;
var first_name_field = document.getElementById('coins-author-first-primary');
var last_name_field = document.getElementById('coins-author-last-primary');
first_name_field.value = document.getElementsByClassName( 'coins-author-first-hidden' )[0].value;
last_name_field.value = document.getElementsByClassName( 'coins-author-last-hidden' )[0].value;
} else {
locking_field.value = document.getElementsByClassName(locking_field_id + '-hidden')[0].value;
}
} else {
field.disabled = false;
if ( field_id == 'coins-author-first' ) {
document.getElementById( 'coins-author-last' ).disabled = false;
locking_field.disabled = false;
if (locking_field_id == 'coins-author') {
document.getElementById('coins-add-author').disabled = false;
}
}

} );
} );
});


});

function coins_add_author( event ) {
event.preventDefault();
add_author_fieldset();
}

function add_author_fieldset() {
var new_fieldset = document.createElement("fieldset");

// First Name label and field
var first_name_label = document.createElement("label");
var first_name_label_text = document.createTextNode("First name:");
first_name_label.appendChild(first_name_label_text);

var first_name_field = document.createElement("input");
first_name_field.setAttribute("name", "_coins-author-first[]");
first_name_field.setAttribute("type", "text");
first_name_field.classList.add("widefat");
first_name_label.appendChild(first_name_field);

new_fieldset.appendChild(first_name_label);

// Last Name label and field
var last_name_label = document.createElement("label");
var last_name_label_text = document.createTextNode("Last name:");
last_name_label.appendChild(last_name_label_text);

var last_name_field = document.createElement("input");
last_name_field.setAttribute("name", "_coins-author-last[]");
last_name_field.setAttribute("type", "text");
last_name_field.classList.add("widefat");
last_name_label.appendChild(last_name_field);

new_fieldset.appendChild(last_name_label);

// Remove button
var remove_button = document.createElement("button");
remove_button.classList.add("remove_author_button");
var remove_button_text = document.createTextNode('Remove author');
remove_button.appendChild(remove_button_text);
remove_button.addEventListener('click', remove_button_parent);
new_fieldset.appendChild(remove_button);

var author_fieldset = document.getElementById('coins-author');
new_fieldset.classList.add("coins_single_author");
author_fieldset.appendChild(new_fieldset);
}

function add_remove_button( author_fieldset ) {
console.log( author_fieldset );

var remove_button = document.createElement("button");
remove_button.classList.add("remove_author_button");
var remove_button_text = document.createTextNode('Remove author');
remove_button.appendChild(remove_button_text);
remove_button.addEventListener('click', remove_button_parent);
author_fieldset.appendChild(remove_button);
}

function remove_button_parent( event ) {
event.preventDefault();
this.parentNode.remove();
}
20 changes: 20 additions & 0 deletions scholarpress-coins.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.scholarpress-field {
margin-bottom: 1rem;
}
.scholarpress-field__input + .scholarpress-field__lock {
margin-top: .5rem;
}

.coins_single_author {
padding: .5rem 0;
}
.coins_single_author:first-of-type {
padding-top: 0;
}
.coins_single_author:not(:last-child) {
border-bottom: .1rem solid lightgray;
}
.remove_author_button,
.add_author_button {
margin-top: .25rem;
}
Loading

0 comments on commit d330a4a

Please sign in to comment.