Skip to content

Instantly share code, notes, and snippets.

@trepmal
Last active December 14, 2015 02:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trepmal/5011670 to your computer and use it in GitHub Desktop.
Save trepmal/5011670 to your computer and use it in GitHub Desktop.
Put a group of galleries into an album, then put the album in a page.
<?php
// Better version over here:
// https://github.com/trepmal/Easy-Albums
//Plugin Name: Easy Albums
//Description: Put a group of galleries into an album, then put the album in a page.
/*
Quick release!
It works, but it's not a finished product. I have some fine-tuning to do, but it may just serve as a good starting point for you.
*/
remove_shortcode( 'gallery', 'gallery_shortcode' );
add_shortcode( 'gallery', 'gallery_shortcode_plus' );
/**
* The Gallery Plus shortcode.
*
* Plus simply adds 'imgw' and 'imgh' attributes that allow generating of real thumbnails on-the-fly
*
* This implements the functionality of the Gallery Shortcode for displaying
* WordPress images on a post.
*
* @since 2.5.0
*
* @param array $attr Attributes of the shortcode.
* @return string HTML content to display gallery.
*/
function gallery_shortcode_plus($attr) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) )
$attr['orderby'] = 'post__in';
$attr['include'] = $attr['ids'];
}
// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'imgw' => false,
'imgh' => false,
), $attr));
if ( $imgw && $imgh ) {
$size = "_{$imgw}x{$imgh}";
}
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$icontag = tag_escape($icontag);
$valid_tags = wp_kses_allowed_html( 'post' );
if ( ! isset( $valid_tags[ $itemtag ] ) )
$itemtag = 'dl';
if ( ! isset( $valid_tags[ $captiontag ] ) )
$captiontag = 'dd';
if ( ! isset( $valid_tags[ $icontag ] ) )
$icontag = 'dt';
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>
<!-- see gallery_shortcode() in wp-includes/media.php -->";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
add_image_size( $size, $imgw, $imgh, true );
maybe_create_missing_intermediate_images( $id, $size );
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
}
/************************************************************************************************************
*************************************************************************************************************
************************************************************************************************************/
$galleries_and_albums = new Galleries_and_Albums();
class Galleries_and_Albums {
function __construct() {
add_action( 'init', array( &$this, 'init_register_cpt' ) );
add_action( 'edit_form_after_title', array( &$this, 'edit_form_after_title' ) );
add_filter( 'is_protected_meta', array( &$this, 'is_protected_meta'), 10, 3 );
add_action( 'save_post', array( &$this, 'save_box' ), 10, 2 );
add_shortcode( 'album', array( &$this, 'sc_album' ) );
add_shortcode( 'albumgallery', array( &$this, 'albumgallery_shortcode') );
}
function init_register_cpt() {
$args = array(
'label' => 'Gallery',
'public' => false,
'show_ui' => true,
'rewrite' => false,
'query_var' => false,
'supports' => array( 'title', 'editor', 'thumbnail' )
);
register_post_type( 'gallery', $args );
$args = array(
'label' => 'Album',
'public' => false,
'show_ui' => true,
'supports' => array( 'title' ),
'rewrite' => false,
'query_var' => false,
'register_meta_box_cb' => array( &$this, 'register_album_meta_box' )
);
register_post_type( 'album', $args );
}
function register_album_meta_box( $post ) {
add_meta_box( 'gallerypicker', 'Galleries', array( &$this, 'the_album_box' ), $post->post_type, 'normal' );
}
function the_album_box( $post ) {
$allgalleries = get_posts( 'post_type=gallery&numberposts=-1' );
if ( count( $allgalleries ) < 1 ) {
echo '<p><em>No galleries created yet</em></p>';
return;
}
$savedgalleries = get_post_meta( $post->ID, 'galleries' );
wp_nonce_field( 'na_galleries', 'nn_galleries');
echo '<input type="hidden" name="galleries[]" value="" /><ul>';
foreach( $allgalleries as $gallery ) {
$s = in_array( $gallery->ID, $savedgalleries ) ? " checked='checked'" : '';
echo "<li><label><input type='checkbox' name='galleries[]' value='{$gallery->ID}'$s /> {$gallery->post_title}</label></li>";
}
echo '</ul>';
}
function edit_form_after_title() {
if ( 'album' != get_post_type() ) return;
global $post;
if ( empty( $post->post_name ) ) return;
echo "<p>Embed this album in a post or page by using: <code>[album name='{$post->post_name}']</code></p>";
}
function is_protected_meta( $protected, $meta_key, $meta_type ) {
if ( 'album' != get_post_type() ) return $protected;
if ( 'galleries' == $meta_key ) return true;
return $protected;
}
function save_box( $post_id, $post ) {
if ( ! isset( $_POST['galleries'] ) ) //make sure our custom value is being sent
return;
if ( ! wp_verify_nonce( $_POST['nn_galleries'], 'na_galleries' ) ) //verify intent
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) //no auto saving
return;
if ( ! current_user_can( 'edit_post', $post_id ) ) //verify permissions
return;
delete_post_meta( $post_id, 'galleries' );
$galleries = array_map( 'intval', $_POST['galleries'] ); //sanitize
foreach( $galleries as $g )
add_post_meta( $post_id, 'galleries', $g ); //save
}
function sc_album( $atts, $content ) {
extract( shortcode_atts( array(
'name' => false,
'id' => false,
), $atts ) );
unset( $atts['name'] );
unset( $atts['id'] );
$att_string = '';
foreach ( $atts as $k => $v ) {
//pass params on to sub-galleries for consistency
$att_string .= " $k='$v'";
}
if ( !$id && !$name ) return;
if ( ! $id ) {
$name = strtolower( $name );
$album_post = array_shift( get_posts( "name=$name&post_type=album") );
$id = $album_post->ID;
} else {
$album_post = get_post( $id );
}
$galleries = get_post_meta( $id, 'galleries' );
//if requesting a sub-gallery
if ( isset( $_GET['showgallery'] ) ) {
//and sub-gallery is in this set (in case of multiple albums per page)
if ( in_array($_GET['showgallery'], $galleries ) ) {
$back = remove_query_arg( 'showgallery' );
$back = "<p><a href='$back'>&larr; Back</a></p>";
$gal = get_post($_GET['showgallery']);
return $back . '<h2 id="albumgal-'.$gal->ID.'">'. $gal->post_title .'</h2>'. do_shortcode( str_replace(']', $att_string.']', $gal->post_content ) );
}
}
$gallery_thumbs = array();
foreach ( $galleries as $gal ) {
if ( has_post_thumbnail( $gal ) ) {
$gallery_thumbs[] = get_post_thumbnail_id( $gal );
} else {
$galpost = get_post( $gal )->post_content;
preg_match( '/\[gallery ids="(\d*)/', $galpost, $matches );
$gallery_thumbs[] = isset( $matches[1] ) ? $matches[1] : 0;
}
}
$sc = '[albumgallery ids="'. implode( ',', $gallery_thumbs ).'" gals="'. implode( ',', $galleries ).'"'. $att_string .']';
$html = do_shortcode( $sc );
return $html;
} // end sc_album()
/**
* The Modified gallery shortcode. A gallery of galleries.
*
* This implements the functionality of the Gallery Shortcode for displaying
* WordPress images on a post.
*
* @since 2.5.0
*
* @param array $attr Attributes of the shortcode.
* @return string HTML content to display gallery.
*/
function albumgallery_shortcode($attr) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
// 'ids' is explicitly ordered, unless you specify otherwise.
if ( empty( $attr['orderby'] ) )
$attr['orderby'] = 'post__in';
$attr['include'] = $attr['ids'];
}
//we need to associate the thumbs (ids) with their parent galleries (gals)
$pairs = array_combine( explode( ',', $attr['ids'] ), explode( ',', $attr['gals'] ) );
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
// if ( isset( $attr['orderby'] ) ) {
// $attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
// if ( !$attr['orderby'] )
// unset( $attr['orderby'] );
// }
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'imgw' => false,
'imgh' => false,
), $attr));
if ( $imgw && $imgh ) {
$size = "_{$imgw}x{$imgh}";
}
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
$itemtag = tag_escape($itemtag);
$captiontag = tag_escape($captiontag);
$icontag = tag_escape($icontag);
$valid_tags = wp_kses_allowed_html( 'post' );
if ( ! isset( $valid_tags[ $itemtag ] ) )
$itemtag = 'dl';
if ( ! isset( $valid_tags[ $captiontag ] ) )
$captiontag = 'dd';
if ( ! isset( $valid_tags[ $icontag ] ) )
$icontag = 'dt';
$columns = intval($columns);
$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
$float = is_rtl() ? 'right' : 'left';
$selector = "gallery-{$instance}";
$gallery_style = $gallery_div = '';
if ( apply_filters( 'use_default_gallery_style', true ) )
$gallery_style = "
<style type='text/css'>
#{$selector} {
margin: auto;
}
#{$selector} .gallery-item {
float: {$float};
margin-top: 10px;
text-align: center;
width: {$itemwidth}%;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
</style>";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='gallery album galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 0;
foreach ( $attachments as $id => $attachment ) {
add_image_size( $size, $imgw, $imgh, true );
maybe_create_missing_intermediate_images( $id, $size );
$img = wp_get_attachment_image($id, $size);
$url = add_query_arg( array(
'showgallery' => $pairs[$id],
) );
$link = "<a href='$url#albumgal-{$pairs[$id]}'>$img</a>";
$output .= "<{$itemtag} class='gallery-item'>";
$output .= "
<{$icontag} class='gallery-icon'>
$link
</{$icontag}>";
if ( $captiontag ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . get_the_title( $pairs[$id] ) . "
</{$captiontag}>";
}
$output .= "</{$itemtag}>";
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= '<br style="clear: both" />';
}
$output .= "
<br style='clear: both;' />
</div>\n";
return $output;
} //end albumgallery_shortcode()
}
if ( ! function_exists( 'maybe_create_missing_intermediate_images') ) {
/*
* @param int $id Image attachment ID
* @param string $size_name Name of custom image size as added with add_image_size()
* return bool True if intermediate image exists or was created. False if failed to create.
*/
function maybe_create_missing_intermediate_images( $id, $size_name ) {
if ( ! image_get_intermediate_size( $id, $size_name ) ) { //if size doesn't exist for given image
if ( ! function_exists('wp_generate_attachment_metadata' ) )
include( ABSPATH . 'wp-admin/includes/image.php' );
$upload_dir = wp_upload_dir();
$image_path = str_replace( $upload_dir['baseurl'], $upload_dir['basedir'], wp_get_attachment_url( $id ) );
$new = wp_generate_attachment_metadata( $id, $image_path );
wp_update_attachment_metadata( $id, $new );
if ( image_get_intermediate_size( $id, $size_name ) )
return true; //new image size created
else
return false; //failed to create new image size
}
return true; //already exists
}
}// end if func not exists
//eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment