-
Notifications
You must be signed in to change notification settings - Fork 1
/
sthlm-gallery.php
404 lines (250 loc) · 8.96 KB
/
sthlm-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
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
/*
Plugin Name: Sthlm Gallery
Plugin URI: http://spathon.com
Description: Create galleries with the images uploaded to the media library through an easy drag and drop interface. "Media->Gallery"
Version: 0.3
Author: Patrik Spathon, Jonatan Fried, Linda Eriksson
Author URI: http://spathon.com
*/
/**
* @todo
*
* Edit img alt/title and so on in colorbox (dbclick? for edit or smal edit button on hover?)
* Split css and load right on right page
* add categories/tags to images
* settings page
* pagination
* better insert shortcode
* better structure on code
* and much more
*
* clean the code
*
*
*
* META _thumbnail_id
*
*/
define('STHLM_GALLERY_PLUGIN_URL', WP_PLUGIN_URL.'/'. dirname( plugin_basename(__FILE__) ) );
load_plugin_textdomain('sthlm_gallery', dirname(plugin_basename( __FILE__ )) ."/lang" );
// check if the version of WP is 3.0 or greater
if ( version_compare( $wp_version, '3.0', '>=' ) ):
// templates for common html
include('templates.php');
// the gallery class
include('gallery.class.php');
else:
// if user has to old version of wp print a message
add_action('admin_notices', "sthlm_gallery_need_wp3");
function sthlm_gallery_need_wp3(){
echo '<div class="error fade"><p>'. __('The WordPress version you are using is to old, please update for your on safety', 'sthlm_gallery') .'</p></div>';
}
endif; // wp 3.0 or not
//Create new instance of class
if (class_exists("sthlm_gallery")) {
$sthlm_gallery = new sthlm_gallery();
}
//Actions and Filters
if(isset($sthlm_gallery)){
// Administration Actions
// add menu page
add_action('admin_menu', array($sthlm_gallery,'init'), 1);
// create post type
add_action('init', array($sthlm_gallery, 'sthlm_add_gallery_post_type'));
// AJAX
add_action('wp_ajax_sthlm_load_lightbox_form_ajax', array($sthlm_gallery, 'sthlm_load_lightbox_form_ajax')); // add new gallery
add_action('wp_ajax_sthlm_gallery_ajax_add', array($sthlm_gallery, 'sthlm_gallery_ajax_add')); // add new gallery
add_action('wp_ajax_sthlm_gallery_ajax_delete', array($sthlm_gallery, 'sthlm_gallery_ajax_delete')); // delete a gallery
add_action('wp_ajax_sthlm_gallery_ajax_save_order', array($sthlm_gallery, 'sthlm_gallery_ajax_save_order')); // save the order in the gallery
// insert the correct images when folder is chosen
add_action('wp_ajax_sthlm_get_current_gallery_images_ajax', array($sthlm_gallery, 'sthlm_get_current_gallery_images_ajax'));
add_action('wp_ajax_sthlm_gallery_edit_image_data', array($sthlm_gallery, 'sthlm_gallery_edit_image_data'));
/* FILTERS */
add_action('wp_ajax_sthlm_gallery_query_attachments', array($sthlm_gallery, 'sthlm_gallery_query_attachments'));
// filter by tag
add_action('wp_ajax_sthlm_filter_by_tags_ajax', array($sthlm_gallery, 'sthlm_filter_by_tags_ajax'));
}
/**
*
* DISPLAY
*
* "front end"
*
*/
/**
* Add/remove javascript
*/
add_action('wp_print_scripts', 'sthlm_gallery_include_script');
function sthlm_gallery_include_script(){
if (!is_admin()){
// the scripts
$js_folder = STHLM_GALLERY_PLUGIN_URL . "/js/";
wp_enqueue_script("colorbox", $js_folder ."jquery.colorbox-min.js", array('jquery'), "", true);
wp_enqueue_script("sthlm_gallery", $js_folder ."sthlm-gallery.js", array('jquery', 'colorbox'), "", true);
}
}
/**
* Deregister CSS
*
* wp-pagenavi
*/
add_action( 'wp_print_styles', 'sthlm_gallery_include_styles' );
function sthlm_gallery_include_styles() {
wp_enqueue_style('colorbox', STHLM_GALLERY_PLUGIN_URL.'/css/colorbox.css' );
wp_enqueue_style('sthlm_gallery', STHLM_GALLERY_PLUGIN_URL.'/css/sthlm-gallery.css' );
}
/**
*
* Add a button to the editor
*
*/
add_action('media_buttons_context', 'add_sthlm_gallery_button');
function add_sthlm_gallery_button($context){
$img = STHLM_GALLERY_PLUGIN_URL .'/images/img.png';
$out = '<a href="#TB_inline?width=640&inlineId=add_sthlm_gallery" class="thickbox" title="' . __("Lägg till galleri", 'sthlm_gallery') . '"><img src="'.$img.'" alt="' . __("Lägg till galleri", 'sthlm_gallery') . '" /></a>';
return $context . $out;
}
add_action('admin_footer', 'sthlm_gallery_add_mce_popup');
function sthlm_gallery_add_mce_popup(){
?>
<script>
jQuery(document).ready(function($){
var $selectGallery = $('#sthlm_select_gallery');
// Show thumb on select gallery
$selectGallery.live('change', function(){
var $option = $selectGallery.find('option:selected'),
numImg = $option.attr('data-gallery-count'),
src = $option.attr('data-gallery-thumb');
// Show the number of images
$('#sthlm_gallery_num_img').text(numImg);
// set the thumb src
$('#sthlm_preview_thumb').attr('src', src);
});
$('#sthlm_display_style').find('img').live('click', function(){
var style = $(this).attr('data-style');
$('#sthlm_display_style_input').attr('value', style);
$('#sthlm_display_style').find('img').removeClass('sthlm-selected-style');
$(this).addClass('sthlm-selected-style');
});
var win = window.dialogArguments || opener || parent || top;
$('#add_sthlm_gallery_shortcode').click(function(){
var galleryID = $('#sthlm_select_gallery').val(),
rows = $('#sthlm_rows').val(),
rowsOut = '',
style;
// number of img per row
if(rows > 0){
rowsOut = ' rows='+ rows;
}
// display style
style = ' style="'+ $('#sthlm_display_style_input').attr('value') +'"';
win.send_to_editor('[sthlm_gallery id='+ galleryID + rowsOut + style +' ]');
// close thickbox
tb_remove();
return false;
});
});
</script>
<div id="add_sthlm_gallery" style="display:none;">
<div class="wrap" id="sthlm_add_shortcode">
<div class="sthlm-dropdown-gallery">
<?php
/**
* List all galleries
*/
$args = array(
'post_type' => 'sthlm_gallery',
'numberposts' => -1
);
$gallery = get_posts($args);
if(!empty($gallery)): ?>
<div class="sthlm-dropdown-box">
<h2 class="sthlm-dropdown-title"><?php _e('Select gallery', 'sthlm_gallery') ?></h2>
<select id="sthlm_select_gallery">
<?php
// Echo the galleries in option
$i = 0;
foreach ( (array) $gallery as $g ):
// get the thumbnail
$image_id = get_post_thumbnail_id($g->ID);
$image_url = wp_get_attachment_image_src($image_id,'thumbnail');
$image_url = $image_url[0];
// if the gallery don't have any images
if(empty($image_url)) continue;
$image_ids = get_post_meta($g->ID, '_img_order', true);
$number_images = count($image_ids);
// save the first img src
if($i == 0){
$first_thumb = $image_url;
$first_number = $number_images;
$i = 1;
}
// the option
echo '<option value="'.$g->ID.'" data-gallery-count="'.$number_images.'" data-gallery-thumb="'.$image_url.'">'.
$g->post_title.'</option>';
endforeach;?>
</select>
<div class="sthlm-gallery-img-count">
<?php _e('Number of images:', 'sthlm_gallery'); ?>
<span id="sthlm_gallery_num_img"><?php echo $first_number; ?></span>
</div>
</div>
<?php endif; ?>
<img src="<?php echo $first_thumb; ?>" id="sthlm_preview_thumb" alt="Thumb" />
</div>
<label><?php _e('Rader', 'sthlm_gallery'); ?>
<input type="text" id="sthlm_rows" />
</label>
<div id="sthlm_display_style">
<img class="sthlm-style-thumbs sthlm-selected-style" src="<?php echo STHLM_GALLERY_PLUGIN_URL; ?>/images/style-mini-thumbs.gif" data-style="thumbs" />
<img class="sthlm-style-big-lightbox" src="<?php echo STHLM_GALLERY_PLUGIN_URL; ?>/images/style-big-lightbox.gif" data-style="big_lightbox" />
<img class="sthlm-style-big-with-thumbs" src="<?php echo STHLM_GALLERY_PLUGIN_URL; ?>/images/style-one-big.gif" data-style="big_with_thumbs" />
<input type="hidden" name="sthlm_display_style_input" id="sthlm_display_style_input" value="">
</div>
<a class="button-secondary" href="#" id="add_sthlm_gallery_shortcode"><?php _e('Infoga', 'sthlm_gallery'); ?></a>
</div>
</div>
<?php
}
/**
*
* SHORTCODE
*
*/
function sthlm_gallery_shortcode($atts, $content = null){
extract(shortcode_atts(array(
"id" => false,
"rows" => 4,
"style" => 'thumbs'
), $atts));
$out = "";
if($id){
$meta = get_post_meta($id, '_img_order', true);
$i = 0;
$row_i = 0;
if(!empty($meta)){
switch ($style){
case 'big_lightbox':
include('display/big_lightbox.php');
break;
case 'big_with_thumbs':
include('display/big_with_thumbs.php');
break;
case 'thumbs':
default:
include('display/thumbs.php');
break;
}
}// end if
}
return $out;
}
add_shortcode('sthlm_gallery', 'sthlm_gallery_shortcode');
/* button on edit page
add_action( 'media_buttons' , 'sthlm_gallery_media_button', 100);
function sthlm_gallery_media_button($content){
echo '<a class="thickbox" id="add_sthlm_gallery" href="#">
<img alt="Add Sthlm Gallery" src="images/media-button-image.gif?ver=20100531"></a>';
}
*/