/
cleaner-gallery.php
167 lines (137 loc) · 5.92 KB
/
cleaner-gallery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/**
* Cleaner Gallery - A valid image gallery script for WordPress.
*
* Cleaner Gallery was created to clean up the invalid HTML and remove the inline styles of the default
* implementation of the WordPress [gallery] shortcode. This has the obvious benefits of creating
* sites with clean, valid code. But, it also allows developers to more easily create custom styles for
* galleries within their themes.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @package CleanerGallery
* @version 0.9.5 - Alpha
* @author Justin Tadlock <justin@justintadlock.com>
* @copyright Copyright (c) 2008 - 2012, Justin Tadlock
* @link http://justintadlock.com/archives/2008/04/13/cleaner-wordpress-gallery-plugin
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*/
/* Filter the post gallery shortcode output. */
add_filter( 'post_gallery', 'cleaner_gallery', 10, 2 );
/**
* Overwrites the default WordPress [gallery] shortcode's output. This function removes the invalid
* HTML and inline styles. It adds the number of columns used as a class attribute, which allows
* developers to style the gallery more easily.
*
* @since 0.9.0
* @access private
* @param string $output The output of the gallery shortcode.
* @param array $attr The arguments for displaying the gallery.
* @return string $output
*/
function cleaner_gallery( $output, $attr ) {
static $cleaner_gallery_instance = 0;
$cleaner_gallery_instance++;
/* We're not worried abut galleries in feeds, so just return the output here. */
if ( is_feed() )
return $output;
/* Orderby. */
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
/* Default gallery settings. */
$defaults = array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => get_the_ID(),
'link' => '',
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'ids' => '',
'include' => '',
'exclude' => '',
'numberposts' => -1,
'offset' => ''
);
/* Apply filters to the default arguments. */
$defaults = apply_filters( 'cleaner_gallery_defaults', $defaults );
/* Apply filters to the arguments. */
$attr = apply_filters( 'cleaner_gallery_args', $attr );
/* Merge the defaults with user input. */
$attr = shortcode_atts( $defaults, $attr );
extract( $attr );
$id = intval( $id );
/* Arguments for get_children(). */
$children = array(
'post_parent' => $id,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => $order,
'orderby' => !empty( $ids ) ? 'post__in' : $orderby,
'exclude' => $exclude,
'include' => !empty( $ids ) ? $ids : $include,
'numberposts' => $numberposts,
'offset' => $offset,
'suppress_filters' => true
);
/* Get image attachments. If none, return. */
$attachments = get_children( $children );
if ( empty( $attachments ) )
return '';
/* Properly escape the gallery tags. */
$itemtag = tag_escape( $itemtag );
$icontag = tag_escape( $icontag );
$captiontag = tag_escape( $captiontag );
$i = 0;
/* Count the number of attachments returned. */
$attachment_count = count( $attachments );
/* Allow developers to overwrite the number of columns. This can be useful for reducing columns with with fewer images than number of columns. */
//$columns = ( ( $columns <= $attachment_count ) ? intval( $columns ) : intval( $attachment_count ) );
$columns = apply_filters( 'cleaner_gallery_columns', intval( $columns ), $attachment_count, $attr );
/* Open the gallery <div>. */
$output = "\n\t\t\t<div id='gallery-{$id}-{$cleaner_gallery_instance}' class='gallery gallery-{$id}'>";
/* Loop through each attachment. */
foreach ( $attachments as $id => $attachment ) {
/* Open each gallery row. */
if ( $columns > 0 && $i % $columns == 0 )
$output .= "\n\t\t\t\t<div class='gallery-row gallery-clear'>";
/* Open each gallery item. */
$output .= "\n\t\t\t\t\t<{$itemtag} class='gallery-item col-{$columns}'>";
/* Open the element to wrap the image. */
$output .= "\n\t\t\t\t\t\t<{$icontag} class='gallery-icon'>";
/* Add the image. */
$image = ( ( isset( $attr['link'] ) && 'file' == $attr['link'] ) ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false ) );
$output .= apply_filters( 'cleaner_gallery_image', $image, $id, $attr, $cleaner_gallery_instance );
/* Close the image wrapper. */
$output .= "</{$icontag}>";
/* Get the caption. */
$caption = apply_filters( 'cleaner_gallery_caption', wptexturize( $attachment->post_excerpt ), $id, $attr, $cleaner_gallery_instance );
/* If image caption is set. */
if ( !empty( $caption ) )
$output .= "\n\t\t\t\t\t\t<{$captiontag} class='gallery-caption'>{$caption}</{$captiontag}>";
/* Close individual gallery item. */
$output .= "\n\t\t\t\t\t</{$itemtag}>";
/* Close gallery row. */
if ( $columns > 0 && ++$i % $columns == 0 )
$output .= "\n\t\t\t\t</div>";
}
/* Close gallery row. */
if ( $columns > 0 && $i % $columns !== 0 )
$output .= "\n\t\t\t</div>";
/* Close the gallery <div>. */
$output .= "\n\t\t\t</div><!-- .gallery -->\n";
/* Return out very nice, valid HTML gallery. */
return $output;
}
?>