forked from bassjobsen/twitter-bootstrap-galleries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter-bootstrap-galleries.php
381 lines (310 loc) · 11.4 KB
/
twitter-bootstrap-galleries.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/*
Plugin Name: Twitter Bootstrap Galleries
Plugin URI: https://github.com/bassjobsen/twitterbootstrap-galleries
Description: Wraps the content of a WordPress media gallery in a Twitter's Bootstrap grid
Version: 1.0.1
Author: Bass Jobsen
Author URI: http://bassjobsen.weblogs.fm/
License: GPLv2
*/
/* Copyright 2013 Bass Jobsen (email : [email protected])
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. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if(!class_exists('Twitterbootstrap_Galleries'))
{
function twitterbootstrap_galleries_get_grid_classes($numberofcolumns)
{
/* the grid display */
/*
| columns | mobile | tablet | desktop |per page |
----------------------------------------------------|-----------|
| 1 | 1 | 1 | 1 | 10 |
|---------------------------------------------------|-----------|
| 2 | 1 | 2 | 2 | 10 |
|---------------------------------------------------|-----------|
| 3 | 1 | 1 | 3 | 9 |
|---------------------------------------------------|-----------|
| 4 | 1 | 2 | 4 | 12 |
|---------------------------------------------------|-----------|
| 5 | n/a | n/a | n/a | n/a |
|---------------------------------------------------|-----------|
| 6 | 2 | 4 | 6 | 12 |
|---------------------------------------------------|-----------|
| >=6 | n/a | n/a | n/a | n/a |
|---------------------------------------------------------------|
*
*
*/
switch($numberofcolumns)
{
case 6: $classes = 'col-xs-6 col-sm-3 col-md-2'; break;
case 4: $classes = 'col-xs-12 col-sm-6 col-md-3'; break;
case 3: $classes = 'col-xs-12 col-sm-12 col-md-4'; break;
case 31: $classes = 'col-xs-12 col-sm-6 col-md-4'; break;
case 2: $classes = 'col-xs-12 col-sm-6 col-md-6'; break;
default: $classes = 'col-xs-12 col-sm-12 col-md-12';
}
return $classes;
}
class Twitterbootstrap_Galleries
{
/*
* Construct the plugin object
*/
public function __construct()
{
load_plugin_textdomain( 'tbgal', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
// register actions
add_action('admin_init', array(&$this, 'admin_init'));
add_action('admin_menu', array(&$this, 'add_menu'));
add_filter( 'init', array( $this, 'init' ) );
}
// END public
/**
* Activate the plugin
**/
public static function activate()
{
// Do nothing
}
// END public static function activate
/**
* Deactivate the plugin
*
**/
public static function deactivate()
{ // Do nothing
}
// END public static function deactivate
/**
* hook into WP's admin_init action hook
* */
public function admin_init()
{
// Set up the settings for this plugin
$this->init_settings();
// Possibly do additional admin_init tasks
}
// END public static function activate - See more at: http://www.yaconiello.com/blog/how-to-write-wordpress-plugin/#sthash.mhyfhl3r.JacOJxrL.dpuf
/** * Initialize some custom settings */
public function init_settings()
{
// register the settings for this plugin
register_setting('twitterbootstrap-galleries-group', 'number_of_columns');
} // END public function init_custom_settings()
/** * add a menu */
public function add_menu()
{
add_options_page('Twitter Bootstrap Galleries Settings', 'Twitter Bootstrap Galleries', 'manage_options', 'twitterbootstrap-galleries', array(&$this, 'plugin_settings_page'));
} // END public function add_menu()
/** * Menu Callback */
public function plugin_settings_page()
{
if(!current_user_can('manage_options'))
{
wp_die(__('You do not have sufficient permissions to access this page.'));
}
// Render the settings template
include(sprintf("%s/templates/settings.php", dirname(__FILE__)));
}
// END public function plugin_settings_page()
function init()
{
if( !function_exists( 'bssetstylesheets' ) ):
function bssetstylesheets()
{
wp_register_style ( 'twitterbootstrap-galleries', plugins_url( 'css/twitterbootstrap-galleries.css' , __FILE__ ));
wp_enqueue_style ( 'twitterbootstrap-galleries');
}
endif;
add_action( 'wp_enqueue_scripts', 'bssetstylesheets', 99 );
remove_shortcode('gallery');
add_shortcode('gallery', array('Twitterbootstrap_Galleries','gallery_shortcode_bootstrap'));
}
/**
* The Gallery shortcode.
*
* This implements the functionality of the Gallery Shortcode for displaying
* WordPress images on a post.
*/
public static function gallery_shortcode_bootstrap($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 ? $post->ID : 0,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => '',
'link' => ''
), $attr, 'gallery'));
$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 {
margin-top: 10px;
text-align: center;
}
#{$selector} img {
border: 2px solid #cfcfcf;
}
#{$selector} .gallery-caption {
margin-left: 0;
}
/* see gallery_shortcode() in wp-includes/media.php */
</style>";
$size_class = sanitize_html_class( $size );
$gallery_div = "<div id='$selector' class='row gallery galleryid-{$id} gallery-size-{$size_class}'>";
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div );
$i = 1;
$numberofcolumns = get_option('number_of_columns', 4 );
$classes = twitterbootstrap_galleries_get_grid_classes($numberofcolumns);
foreach ( $attachments as $id => $attachment ) {
if ( ! empty( $link ) && 'file' === $link )
$image_output = wp_get_attachment_link( $id, $size, false, false );
elseif ( ! empty( $link ) && 'none' === $link )
$image_output = wp_get_attachment_image( $id, $size, false );
else
$image_output = wp_get_attachment_link( $id, $size, true, false );
$image_output = preg_replace('/height="[0-9]+"/','',$image_output);
$image_output = preg_replace('/width="[0-9]+"/','',$image_output);
$image_output = str_replace('class="', 'class="img-responsive ', $image_output);
$image_meta = wp_get_attachment_metadata( $id );
$orientation = '';
if ( isset( $image_meta['height'], $image_meta['width'] ) )
$orientation = ( $image_meta['height'] > $image_meta['width'] ) ? 'portrait' : 'landscape';
$output .= "<div class='gallery-item ".$classes."'>";
$output .= "
<div class='gallery-icon {$orientation}'>
$image_output
</div>";
if ( $captiontag && trim($attachment->post_excerpt) ) {
$output .= "
<{$captiontag} class='wp-caption-text gallery-caption'>
" . wptexturize($attachment->post_excerpt) . "
</{$captiontag}>";
}
$output .= "</div>";
if($numberofcolumns == 6)
{
if(0 == ($i % 6)){$output .= '<div class="clearfix visible-md visible-lg"></div>'; }
if(0 == ($i % 4)){$output .= '<div class="clearfix visible-sm"></div>'; }
if(0 == ($i % 2)){$output .= '<div class="clearfix visible-xs"></div>'; }
}
elseif($numberofcolumns == 4)
{
if(0 == ($i % 4)){$output .= '<div class="clearfix visible-md visible-lg"></div>'; }
if(0 == ($i % 2)){$output .= '<div class="clearfix visible-sm"></div>'; }
}
elseif($numberofcolumns == 3)
{
if(0 == ($i % 3)){$output .= '<div class="clearfix visible-md visible-lg"></div>'; }
}
elseif($numberofcolumns == 31)
{
if(0 == ($i % 3)){$output .= '<div class="clearfix visible-md visible-lg"></div>'; }
if(0 == ($i % 2)){$output .= '<div class="clearfix visible-sm"></div>'; }
}
elseif($numberofcolumns == 2)
{
if(0 == ($i % 2)){$output .= '<div class="clearfix invisible-xs"></div>'; }
}
$i++;
}
$output .= "
</div>\n";
return $output;
}
}
}
if(class_exists('Twitterbootstrap_Galleries'))
{ // Installation and uninstallation hooks
register_activation_hook(__FILE__, array('Twitterbootstrap_Galleries', 'activate'));
register_deactivation_hook(__FILE__, array('Twitterbootstrap_Galleries', 'deactivate'));
$twitterbootstrapgalleries = new Twitterbootstrap_Galleries();
// Add a link to the settings page onto the plugin page
if(isset($twitterbootstrapgalleries))
{
function twitterbootstrapgalleries_plugin_settings_link($links)
{
$settings_link = '<a href="options-general.php?page=twitterbootstrap-galleries">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
$plugin = plugin_basename(__FILE__);
add_filter("plugin_action_links_$plugin", 'twitterbootstrapgalleries_plugin_settings_link');
}
}