-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-bootstrap-gallery.php
executable file
·186 lines (156 loc) · 4.71 KB
/
wp-bootstrap-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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
/**
* WP Bootstrap Navwalker
*
* @package WP-Bootstrap-Navwalker
*/
/**
* Class Name: wp_bootstrap_gallery
* Plugin Name: WP Bootstrap Navwalker
* Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-gallery
* Description: A custom Wordpress gallery for dynamic thumbnail layout using Twitter Bootstrap 2 thumbnail layouts.
* Version: 1.0.1
* Author: Edward McIntyre - @twittem, Brandon Hubbard
* GitHub URI: https://github.com/wp-bootstrap/wp-bootstrap-gallery
* GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-gallery
* GitHub Branch: master
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
*/
/**
* WP Bootstrap Gallery.
*
* @access public
* @param mixed $content Content.
* @param mixed $attr Attributes.
* @return void
*/
function wp_bootstrap_gallery( $content, $attr ) {
global $instance, $post;
$instance++;
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' => 'figure',
'icontag' => 'div',
'captiontag' => 'figcaption',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
), $attr ) );
$id = intval( $id );
if ( 'RAND' === $order ) {
$orderby = 'none';
}
if ( $include ) {
$include = preg_replace( '/[^0-9,]+/', '', $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 ( $exclude ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $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 );
$columns = intval( min( array( 8, $columns ) ) );
$float = (is_rtl()) ? 'right' : 'left';
$selector = "gallery-{$instance}";
$size_class = sanitize_html_class( $size );
$output = "<div class='row' id='$selector'>";
/**
* Count number of items in $attachments array, and assign a colum layout to $span_array
* variable based on the mumber of images in the $attachments array
*/
$span_array = null;
switch ( count( $attachments ) ) {
case 1:
/* One full width image. */
$span_array = array( 12 );
break;
case 2:
/* Two half width images. */
$span_array = array( 6,6 );
break;
case 3:
/* One 3/4 width image with two 1/4 width images to the right. */
$span_array = array( 8,4,4 );
break;
case 4:
/* One full width image with three 1/3 width images underneath. */
$span_array = array( 12,4,4,4 );
break;
case 5:
/* Two half width images with fout 1/4 width images underneath. */
$span_array = array( 6,6,4,4,4 );
break;
case 6:
/* One 2/3 width image with two 1/3 width images to the right, and three 1/3 width images underneath. */
$span_array = array( 8,4,4,4,4,4 );
break;
default:
/* One full width image with two 1/2 width images underneath. All remaining images 1/3 width underneath. */
$span_array = array( 12,6,6,4 );
break;
}
$attachment_count = 0;
foreach ( $attachments as $id => $attachment ) {
$attachment_image = wp_get_attachment_image( $id, 'full' );
$attachment_link = wp_get_attachment_link( $id, 'full', ! ( isset( $attr['link'] ) and 'file' === $attr['link'] ) );
$output .= "<div class='span" . $span_array[ $attachment_count ] . "'>";
$output .= $attachment_link . "\n";
$output .= "</div>\n";
if ( count( $attachments ) >= 7 && 3 === $attachment_count ) {
$attachment_count = 3;
} else {
$attachment_count++;
}
}
$output .= "</div>\n";
return $output;
}
add_filter( 'post_gallery', 'wp_bootstrap_gallery', 10, 2 );