-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsimple-gallery.php
More file actions
363 lines (287 loc) · 13.1 KB
/
simple-gallery.php
File metadata and controls
363 lines (287 loc) · 13.1 KB
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
<?php
/*
Plugin Name: Simple Image Gallery
Plugin URI: https://tectalic.com/software/
Description: Creates powerful and attractive image galleries that don't require Adobe Flash.
Version: 1.8.5
Author: Tectalic
Author URI: https://tectalic.com/software/
Text Domain: om4-simplegallery
Git URI: https://github.com/tectalic/simple-gallery
Git Branch: release
License: GPLv2
*/
/* Copyright 2009-2024 OM4, now Tectalic (email: plugins@tectalic.com web: https://tectalic.com/software/)
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
*/
class OM4_Simple_Gallery {
public $version = '1.8.5';
public $dbVersion = 1;
public $installedVersion;
public $dirname;
public $url;
static $number = 1;
public $script_suffix = '';
public $style_suffix = '';
/**
* Constructor
*
*/
public function __construct() {
// Uncomment to prevent browsers caching the JS file while debugging.
//$this->version .= time();
// Store the name of the directory that this plugin is installed in
$this->dirname = str_replace('/simple-gallery.php', '', plugin_basename(__FILE__));
$this->url = plugins_url($this->dirname . '/');
register_activation_hook(__FILE__, array($this, 'Activate'));
add_action('init', array($this, 'LoadDomain'));
add_action('init', array($this, 'CheckVersion'));
add_action('init', array($this, 'RegisterShortcode'));
add_action('wp_enqueue_scripts', array($this, 'RegisterScripts'));
add_filter('option_srel_options', array($this, 'ForceShutterReloadedInHead'));
$this->installedVersion = intval(get_option('om4_simple_gallery_db_version'));
}
/**
* Intialise I18n
*
*/
function LoadDomain() {
load_plugin_textdomain( 'om4-simplegallery', false, dirname( plugin_basename( __FILE__) ) );
}
/**
* Plugin Activation Tasks
*
*/
function Activate() {
// There aren't really any installation tasks at the moment
if (!$this->installedVersion) {
$this->installedVersion = $this->dbVersion;
$this->SaveInstalledVersion();
}
}
/**
* Performs any upgrade tasks if required
*
*/
function CheckVersion() {
if ($this->installedVersion != $this->dbVersion) {
// Upgrade tasks
if ($this->installedVersion == 0) {
$this->installedVersion++;
}
$this->SaveInstalledVersion();
}
}
function RegisterShortcode() {
add_shortcode('simplegallery', array($this, 'ShortcodeHandler'));
}
/**
* Register the required JS/CSS so it is included in the page's <head> section
*/
function RegisterScripts() {
// Load the minified js and CSS files, unless these constants are set
$this->script_suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
$this->style_suffix = defined('STYLE_DEBUG') && STYLE_DEBUG ? '.dev' : '';
wp_enqueue_script('simple_gallery_js', "{$this->url}simple-gallery{$this->script_suffix}.js", array('jquery'), $this->version);
wp_enqueue_style('simple_gallery', "{$this->url}simple-gallery{$this->style_suffix}.css", array(), $this->version, 'screen');
}
/**
* Handler for the [simplegallery] shortcode
*/
function ShortcodeHandler($atts, $content = null) {
// List of supported shortcode attributes and their default values
$defaults = array(
'columns' => '1', // The number of columns for the thumbnails. If columns is set to 0, no row breaks will be included. Default: 1
'cssid' => '', // Unique HTML/CSS ID/selector for this image gallery. Default: auto-generated in the format simplegallery_x where x is a unique number
'exclude' => '', // Comma separated list of attachment IDs to exclude from this gallery. exclude="21,32,43" Please note that include and exclude cannot be used together (in that case include="" will take preference)
'fade' => 'out-in', // out-in|none|over out-in: when changing the large image, fade the current image out, and fade the next image in (displays the "loading" message).
// none: just change the large image with no fading
// over: fades the new image over the top of the existing image (doesn't display a "loading" message). For this effect to work properly, all images should be the same size, and the largeimagewidth and largeimageheight parameters should be set to this size.
'fadespeed' => 600, // Number of milliseconds it takes to fade in the image. Has no effect if fade="none"
'id' => get_the_ID(), // The page/post ID to display the images from. The gallery will display images which are attached to that post. The default behaviour if no ID is specified is to display images attached to the current post.
'include' => '', // Comma separated list of attachment IDs to include in this gallery. include="23,39,45 will show only these attachment IDs.
'largeimagewidth' => '600', // Maximum width of the large image (in pixels). Default: 600
'largeimageheight' => '400', // Maximum height of the large image (in pixels). Default: 400
'navigation' => 1, // Whether or not to display the next/previous navigation. 1=Yes, 0=No. Default: 1
'orderby' => 'menu_order', // The field used to sort the thumbnails. The default is "menu_order". Valid values: menu_order|ID|rand
// menu_order: Sort by the how the images are sorted on the media gallery tab.
// ID: Sort by the attachment ID. ie. the order that they were uploaded into the media library.
// rand: Randomly order the thumbnails on each page load. The 'order' parameter is ignored if orderby=rand
'order' => 'ASC', // Sort order (ascending or descending) for the thumbnails. Valid values: ASC | DESC. Default: ASC.
'thumbnailalign' => 'left', // Thumbnail section alignment. Valid values: left | right | top | bottom Default: left
'thumbnailscroll' => 0, // Whether or not to display the next/previous scrolling navigation. Only supported when thumbnailalign="bottom" 1=Yes, 0=No. Default: 0
'size' => 'thumbnail' // Thumbnail image size. Valid values: thumbnail | medium | large default: thumbnail The size of the images for "thumbnail", "medium" and "large" can be configured in WordPress admin panel under Settings > Media
);
$atts = shortcode_atts( $defaults, $atts);
// Valid values for each parameter
$validValues['thumbnailalign'] = array('left', 'right', 'top', 'bottom');
$validValues['fade'] = array('out-in', 'none', 'over');
$atts['largeimagewidth'] = intval($atts['largeimagewidth']);
if (! $atts['largeimagewidth'] ) $atts['largeimagewidth'] = $defaults['largeimagewidth'];
$atts['largeimageheight'] = intval($atts['largeimageheight']);
if (! $atts['largeimageheight'] ) $atts['largeimageheight'] = $defaults['largeimageheight'];
$atts['fadespeed'] = intval($atts['fadespeed']);
if (! $atts['fadespeed'] ) $atts['fadespeed'] = $defaults['fadespeed'];
$atts['navigation'] = intval($atts['navigation']);
// Validate each of the parameters
foreach ($atts as $key => $value) {
if (isset($validValues[$key])) {
if (!in_array($value, $validValues[$key])) {
// Invalid value
if (isset($defaults[$key])) {
//use default instead
$atts[$key] = $defaults[$key];
} else {
unset($atts[$key]);
}
}
} else {
$atts[$key] = esc_attr($value);
}
}
$atts['thumbnailscroll'] = intval($atts['thumbnailscroll']);
if ( 'bottom' != $atts['thumbnailalign'] ) // Only supported when thumbnailalign=bottom
$atts['thumbnailscroll'] = 0;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
if ($attr['orderby'] == 'rand') {
$attr['order'] = '';
} else {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
}
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract( $atts, EXTR_SKIP );
if (empty($cssid)) {
$cssid = 'simplegallery_' . self::$number;
}
// CSS rules for this image gallery
$selector = "#$cssid";
$css = <<<EOD
$selector .loading { background:transparent url('{$this->url}loading.gif') no-repeat scroll 50% 20%; }
$selector .largeimage img { max-width: {$largeimagewidth}px; max-height: {$largeimageheight}px; }
$selector .largeimage { width: {$largeimagewidth}px; height: {$largeimageheight}px; }
EOD;
// CSS rules that are dependent on the gallery thumbnail alignnment
switch ($thumbnailalign) {
case 'left':
case 'right':
// Float the thumbnails to the left/right
$css .= <<<EOD
$selector .thumbnails { float: $thumbnailalign; height: {$largeimageheight}px; }
EOD;
break;
case 'top':
case 'bottom':
// thumbnails position is controlled below (by the HTML output order)
// Override the columns parameter so the thumbs appear next to each other
$columns = 0;
// Remove all floats and dimensions
$css .= <<<EOD
$selector, $selector .thumbnails { height: auto; }
$selector .largeimage, $selector .thumbnails { width: 100%; }
$selector .thumbnails { float: none; }
$selector .largeimage { float: none; }
$selector .thumbnails .gallery-item { width: auto !important; }
EOD;
break;
}
$html = <<<EOD
<style type="text/css">
$css
</style>
<script type="text/javascript">
//<![CDATA[
if (typeof gallerySettings === 'undefined') {
var gallerySettings = []; // Define the empty settings array
}
EOD;
$html .= <<<EOD
gallerySettings['$cssid'] = {
fade : '$fade',
fadespeed : $fadespeed,
thumbnailscroll : $thumbnailscroll
}
//]]>
</script>
EOD;
$html .= '<div class="simplegallery" id="' . $cssid . '">';
// Thumbnail panel HTML code
// See http://codex.wordpress.org/Gallery_Shortcode for documentation on the WordPress [gallery] shortcode
// gallery_shortcode() in wp-includes/media.php is the WordPress [gallery] shortcode handler function
$class = 'thumbnails';
if ( $thumbnailscroll ) {
$class .= ' hasthumbnailscroll';
}
$thumbs = '<div class="' . $class. '">';
if ( $thumbnailscroll ) {
$thumbs .= '<div class="thumbnailscroll thumbnailscroll-prev"> </div>';
$thumbs .= '<div class="thumbnailscroll thumbnailscroll-next"> </div>';
}
$thumbs .= '<div class="gallerywrapper">';
$thumbs .= do_shortcode("[gallery columns=$columns link=file id=$id orderby=\"$orderby\" order=\"$order\" size=\"$size\" include=\"$include\" exclude=\"$exclude\"]");
$thumbs .= '</div>'; // end div.gallerywrapper
$thumbs .= '</div>'; // end div.thumbnails
// Large image panel HTML code
$large = '<div class="largeimage loading">';
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'numberposts' => 1) );
// If there are no images for the gallery, then don't output anything
if (!is_array($attachments)) return '';
$link = '';
$caption = '';
foreach ($attachments as $att) {
$link = wp_get_attachment_image_src($att->ID, 'full');
$caption = $att->post_excerpt;
}
$large .= '<a href="' . $link[0] . '"><img src="' . $this->url . '/img/trans.gif" title="" alt="" class="simplegallerylargeimage" /></a>';
// Display image caption
$large .= '<h3 class="imagecaption">' . $caption . '</h3>';
if ($navigation) {
// Display image navigation
$large .= '<div class="simplegallerynavbar"></div>';
}
$large .= "</div>";
// Now output the thumbnail/largeimage divs in the appropriate order
if ($thumbnailalign == 'bottom') {
// Bottom thumbnail alignment -> print large image div then thumbnail div
$html .= $large . $thumbs;
} else {
// left, right or top alignment -> print thumbnail div first
$html .= $thumbs . $large;
}
$html .= "<div class=\"clearboth\"></div></div>";
self::$number++;
return $html;
}
/**
* If using the shutter reloaded plugin, force the shutter scripts to be loaded in the header.
* If they are loaded in the footer, then the full screen shutter view doesn't work properly in IE8.
* See Bug #1406
*
* Called during get_option('srel_options')
*/
function ForceShutterReloadedInHead($value) {
$value['headload'] = true;
return $value;
}
function SaveInstalledVersion() {
update_option('om4_simple_gallery_db_version', $this->installedVersion);
}
}
if(defined('ABSPATH') && defined('WPINC')) {
if (!isset($GLOBALS["om4_Simple_Gallery"])) {
$GLOBALS["om4_Simple_Gallery"] = new OM4_Simple_Gallery();
}
}
?>