Skip to content

Commit

Permalink
Template Improvements
Browse files Browse the repository at this point in the history
Fixed call to incorrect option key
Moved item de-listing check to wp query to greatly improve efficiency
Fixed call by front-end to incorrect item meta key
Renamed get_item_meta() to display_item_meta()
Removed prep_item_meta(), merging functionality into display_item_meta()
Changed display_item_meta() to use <dl> elements and refactored code
heavily
  • Loading branch information
kittsville committed Dec 12, 2014
1 parent 0eccac2 commit 1d7923e
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 96 deletions.
46 changes: 39 additions & 7 deletions css/front-end-core.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,49 @@
* Version: 0.1
*/

.wp-lib-item-left {
.wp-lib-item-description {
float: left;
width: 700px;

text-align: left;
display: flex;
}

.wp-lib-item-right {
.wp-lib-item-title, .item-meta-wrap, .wp-lib-item-description {
min-width: 250px;
}

.wp-lib-item-cover {
float: right;
display:inline;
vertical-align: top;
padding: 0px 0px 5px 5px;
display: block;
}

article.wp-lib-items {
display: block;
}

div.item-meta-wrap * {
padding: 0px;
margin: 0px;
border: none;
background: transparent;
}

div.item-meta-wrap {
display: inline-block;
}

.meta-row {
overflow: auto;
}

table.item-metabox th {
white-space: nowrap;
font-weight: bold;
vertical-align: text-top;
text-align: left;
}

.wp_lib_items:last-of-type {
border-bottom: none;
table.item-metabox td {
padding-left: 5px;
}
36 changes: 12 additions & 24 deletions templates/archive-wp_lib_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,23 @@
<div id="primary">
<div id="content" role="main">
<?php
// Loops posts
// If there are items, loop through them
if (have_posts()) : while (have_posts()) : the_post();

// If item is set not to be publicly displayed, skips displaying item
if ( get_post_meta( get_the_ID(), 'wp_lib_item_delist', true ) )
continue;
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="wp-lib-item">
<div class="wp-lib-item-left">
<!-- Item title and meta -->
<strong>Title: </strong><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
<?php echo wp_lib_fetch_meta( get_the_ID() ); ?>
<!-- Item description -->
<div class="entry-content">
<?php the_content(); ?>
</div>
</div>
<div class="wp-lib-item-right">
<!-- Item cover image -->
<div class="wp-lib-item-cover">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( array( 200, 500 ) ); ?>
</a>
</div>
</div>
<!-- Item cover image -->
<div class="wp-lib-item-cover">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 200, 500 ) ); ?></a>
</div>
<!-- Item title, taxonomy terms and meta -->
<div class="item-meta-wrap">
<?php wp_lib_display_item_meta( get_the_ID() ); ?>
</div>
<!-- Item description -->
<div class="wp-lib-item-description entry-content">
<?php the_content(); ?>
</div>
</article>

<?php if ( $wp_query->current_post + 1 !== $wp_query->post_count ): ?>
<hr />
<?php endif; endwhile; else: endif; ?>
Expand Down
31 changes: 14 additions & 17 deletions templates/single-wp_lib_items.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@
<div id="content" role="main">
<?php
if (have_posts()) : while (have_posts()) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="wp-lib-item-left">
<header class="entry-header">
<!-- Item title and meta -->
<strong>Title:</strong> <?php the_title(); ?><br />
<?php echo wp_lib_fetch_meta( get_the_ID() ); ?>
</header>
<!-- Item description -->
<div class="entry-content"><?php the_content(); ?></div>
</div>
<div class="wp-lib-item-right">
<!-- Item cover image -->
<div class="wp-lib-item-cover">
<?php the_post_thumbnail( array( 200, 500 ) ); ?>
</div>
</div>
</article>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Item cover image -->
<div class="wp-lib-item-cover">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( array( 200, 500 ) ); ?></a>
</div>
<!-- Item title, taxonomy terms and meta -->
<div class="item-meta-wrap">
<?php wp_lib_display_item_meta( get_the_ID() ); ?>
</div>
<!-- Item description -->
<div class="wp-lib-item-description entry-content">
<?php the_content(); ?>
</div>
</article>
<?php endwhile; else: endif; ?>
</div>
</div>
Expand Down
130 changes: 82 additions & 48 deletions wp-librarian-helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1046,62 +1046,96 @@ function wp_lib_prep_item_status( $item_id, $no_url = false, $short = false ) {
return $status;
}

// Prepares taxonomy and metabox information for theme use
function wp_lib_fetch_meta( $item_id ) {
// Metabox data is fetched and relevant functions are called to format the data
$meta_array = array(
'media type' => wp_lib_prep_meta( get_the_terms( $item_id, 'wp_lib_media_type' ), 'Media Type' ),
'authors' => wp_lib_prep_meta( get_the_terms( $item_id, 'wp_lib_author' ), 'Author' ),
'donor' => wp_lib_prep_meta( get_post_meta( $item_id, 'wp_lib_donor', true ), 'Donor' ),
'isbn' => wp_lib_prep_meta( get_post_meta( $item_id, 'wp_lib_item_isbn', true ), 'ISBN' ),
'available' => wp_lib_prep_meta( wp_lib_prep_item_status( $item_id ), 'Status' ),
// Renders item's tax terms and public meta to the page
function wp_lib_display_item_meta( $item_id, $item_permalink = true ) {
// Fetches default taxonomy spacer
$spacer = get_option( 'wp_lib_taxonomy_spacer', ', ' );

// If user is librarian, or higher, fetches item donor
// If user isn't a librarian, or there is no listed donor, returns false
$donor_id = ( wp_lib_is_librarian() ? get_post_meta( $item_id, 'wp_lib_item_donor', true ) : false );

// If donor ID belongs to a valid donor, fetch donor's name
$donor = ( is_numeric( $donor_id ) ? get_the_title( $donor_id ) : false );

// Creates array of raw item meta
$raw_meta = array(
array( 'Title', get_the_title( $item_id )),
array( 'Media Type',get_the_terms( $item_id, 'wp_lib_media_type' )),
array( 'Author', get_the_terms( $item_id, 'wp_lib_author' )),
array( 'Donor', $donor ),
array( 'ISBN', get_post_meta( $item_id, 'wp_lib_item_isbn', true )),
array( 'Status', wp_lib_prep_item_status( $item_id ))
);
$all_meta = '';
// Runs through each meta value and, if the meta exists, adds it to the end of the $all_meta string
foreach ( $meta_array as $value ) {
if ( $value != false )
$all_meta .= $value . '<br />';
}

return $all_meta;
}

// Formats author/media type/donor arrays and formats them as a comma separated list with hyperlinks
function wp_lib_prep_meta( $tax_array, $bold_name ) {
// If tax array doesn't exist, return empty string
if ( $tax_array == false )
return '';

// If there is one than one of a taxonomy item it makes the term plural (Author -> Authors)
if ( count( $tax_array ) > 1 )
$bold_name .= 's';

// Formats meta name
$item_string = '<strong>' . $bold_name . ': </strong>';
// If item title should be a link to manage the item
if ( $item_permalink )
$raw_meta[0][2] = get_permalink( $item_id );

// If $tax_array is not an array, return formatted string before foreach loop
if ( !is_array( $tax_array ) )
return $item_string . $tax_array;
// Initialises formatted meta output
$meta_output = array();

// Iterates through tax items
foreach ( $tax_array as $tax_item ) {
// Gets tax term's URL
$tax_url = get_term_link( $tax_item );
// Iterates over raw taxonomy
foreach ( $raw_meta as $key => $meta ) {
// If meta value is a tax term
if ( is_array( $meta[1] ) ) {
// Initilises output for tax terms
$tax_terms_output = array();

// Iterates through tax terms
foreach ( $meta[1] as $tax_key => $tax_term ) {
// Gets tax term's URL
$tax_url = get_term_link( $tax_term );

// Deletes term if error occurred
if ( is_wp_error( $tax_url ) )
continue;

// Formats tax item as link
$tax_terms_output[] = '<a href="' . esc_url( $tax_url ) . '">' . $tax_term->name . '</a>';
}

// Overwrites tax term objects with formatted tax terms
$meta[1] = $tax_terms_output;

// Counts number of valid tax terms
$count = count( $meta[1] );

// If all tax terms were invalid, remove meta value
if ( $count === 0 ) {
unset( $tax_array[$key] );
continue;
// If there is one than one of a taxonomy item it makes the term plural (Author -> Authors)
} elseif ( $count > 1 ) {
$meta[0] .= 's';
}

// Implodes array into string separated by users preferred spacer
$meta[1] = implode( $spacer, $meta[1] );
}

// Skips term if error occurred
if ( is_wp_error( $tax_url ) )
continue;
// If output is a string with a URL, create hyperlink
if ( isset( $meta[2] ) )
$meta[1] = '<a href="' . $meta[2] . '">' . $meta[1] . '</a>';

// Formats tax item as link
$formatted_values[] = '<a href="' . esc_url( $tax_url ) . '">' . $tax_item->name . '</a>';
// If meta output is valid, add to output
if ( $meta[1] !== false && $meta[1] !== '' )
$meta_output[] = $meta;
}

// If there are no formatted values, return empty string
if ( !isset( $formatted_values ) )
return '';

// Implodes array into string separated by users preferred spacer
return $item_string . implode( get_option( 'wp_lib_taxonomy_spacer', ', ' ), $formatted_values );
// If there are any remaining valid meta fields
if ( count( $meta_output ) > 0 ) {
// Renders description list
echo '<table class="item-metabox"><tbody>';

// Iterates over meta fields, rendering them to details list
foreach ( $meta_output as $meta_field ) {
echo '<tr class="meta-row"><th>' . $meta_field[0] . ':</th><td>' . $meta_field[1] . '</td></tr>';
}

// Ends Description list
echo '</tbody></table>';
}
}

// Renders notification to page, to send notification to cl
Expand Down
20 changes: 20 additions & 0 deletions wp-librarian.php
Original file line number Diff line number Diff line change
Expand Up @@ -1259,4 +1259,24 @@ function wp_lib_add_profile_page_fields( $user ) {
// If post is not a Library item
return $template;
}, 10, 1 );

// Adds query parameters to exclude de-listed items from the Library archive
// Note that items are still public and can be accessed by their direct URL, that's what setting an item as private exists for!
add_action( 'pre_get_posts', function( $query ) {
if ( $query->is_post_type_archive('wp_lib_items') && $query->is_main_query() ) {
$query->set( 'meta_query', array(
'relation' => 'OR',
array(
'key' => 'wp_lib_item_delist',
'value' => '1',
'compare' => '!=',
),
array(
'key' => 'wp_lib_item_delist',
'value' => 'bug #23268', // Allows WP-Librarian to run on pre-3.9 WP installs (bug was fixed for 3.9, text is arbitrary)
'compare' => 'NOT EXISTS',
)
));
}
}, 10, 1 );
?>

0 comments on commit 1d7923e

Please sign in to comment.