certainlyakey
3/3/2014 - 12:43 PM

Wordpress - WP post gallery code cleaner

Wordpress - WP post gallery code cleaner

	// Cleaner Gallery - A valid image gallery script for WordPress. (http://themes.svn.wordpress.org/cakifo/1.5.0/library/extensions/cleaner-gallery.php, unofficial version 0.9.5)
	add_filter( 'post_gallery', 'cleaner_gallery', 10, 2 );
	function cleaner_gallery( $output, $attr ) {
		static $cleaner_gallery_instance = 0;
		$cleaner_gallery_instance++;
		if ( is_feed() )
			return $output;
		if ( isset( $attr['orderby'] ) ) {
			$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
			if ( !$attr['orderby'] )
				unset( $attr['orderby'] );
		}
		$defaults = array(
			'order'       => 'ASC',
			'orderby'     => 'menu_order ID',
			'id'          => get_the_ID(),
			'link'        => '',
			'itemtag'     => 'figure',
			'icontag'     => 'dt',
			'captiontag'  => 'p',
			'columns'     => 10,
			'size'        => 'thumbnail',
			'ids'         => '',
			'include'     => '',
			'exclude'     => '',
			'numberposts' => -1,
			'offset'      => ''
		);
		$defaults = apply_filters( 'cleaner_gallery_defaults', $defaults );
		$attr = apply_filters( 'cleaner_gallery_args', $attr );
		$attr = shortcode_atts( $defaults, $attr );
		extract( $attr );
		$id = intval( $id );
		$children = array(
			'post_status'      => 'inherit',
			'post_type'        => 'attachment',
			'post_mime_type'   => 'image',
			'order'            => $order,
			'orderby'          => $orderby,
			'exclude'          => $exclude,
			'include'          => $include,
			'numberposts'      => $numberposts,
			'offset'           => $offset,
			'suppress_filters' => true
		);
		if ( empty( $include ) )
			$attachments = get_children( array_merge( array( 'post_parent' => $id ), $children ) );
		else
			$attachments = get_posts( $children );
		if ( empty( $attachments ) )
			return '<!-- Here be dragons but no images. -->';
		/* Properly escape the gallery tags. */
		$itemtag    = tag_escape( $itemtag );
		$icontag    = tag_escape( $icontag );
		$captiontag = tag_escape( $captiontag );
		$i = 0;
		$attachment_count = count( $attachments );
		//$columns = ( ( $columns <= $attachment_count ) ? intval( $columns ) : intval( $attachment_count ) );
		$columns = apply_filters( 'cleaner_gallery_columns', intval( $columns ), $attachment_count, $attr );
		$size_class = sanitize_html_class( $size );
		$output = "\n\t\t\t<ul id='gallery-{$id}-{$cleaner_gallery_instance}' class='gallery gallery-{$id} gallery-size-{$size_class}'>";
		foreach ( $attachments as $attachment ) {
			// if ( $columns > 0 && $i % $columns == 0 )
				// $output .= "\n\t\t\t\t<div class='gallery-row gallery-clear'>";
			$output .= "\n\t\t\t\t\t<li><{$itemtag} class='gallery-item'>";
			// $output .= "\n\t\t\t\t\t\t<{$icontag} class='gallery-icon'>";
			$image = ( ( isset( $attr['link'] ) && 'file' == $attr['link'] ) ? wp_get_attachment_link( $attachment->ID, $size, false, false ) : wp_get_attachment_link( $attachment->ID, $size, true, false ) );
			$output .= apply_filters( 'cleaner_gallery_image', $image, $attachment->ID, $attr, $cleaner_gallery_instance );
			// $output .= "</{$icontag}>";
			$captiontitle = apply_filters( 'cleaner_gallery_caption', wptexturize( $attachment->post_title ), $attachment->ID, $attr, $cleaner_gallery_instance );
			$caption = apply_filters( 'cleaner_gallery_caption', wptexturize( $attachment->post_excerpt ), $attachment->ID, $attr, $cleaner_gallery_instance );
			if ( !empty( $captiontitle ) )
				$output .= "\n\t\t\t\t\t\t<figcaption><h2>{$captiontitle}</h2><{$captiontag} class='gallery-caption'>{$caption}</{$captiontag}></figcaption>";
			$output .= "\n\t\t\t\t\t</{$itemtag}></li>";
			// if ( $columns > 0 && ++$i % $columns == 0 )
				// $output .= "\n\t\t\t\t</div>";
		}
		// if ( $columns > 0 && $i % $columns !== 0 )
			// $output .= "\n\t\t\t</div>";
		$output .= "\n\t\t\t</ul><!-- .gallery -->\n";
		return $output;
	}