PluginProbe
Document Gallery / 1.3
Document Gallery v1.3
trunk 0.8 0.8.5 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.1 1.2 1.2.1 1.3 1.3.1 1.4 1.4.1 1.4.2 1.4.3 2.0 2.0.1 2.0.10 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 94 releases
document-gallery / document-gallery.php

document-gallery.php in Document Gallery 1.3, at document-gallery.php

321 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Document Gallery
4 Description: Display non-images (and images) in gallery format on a page or post with the [dg] shortcode.
5 Version: 1.3
6 Author: Dan Rossiter
7 Author URI: http://danrossiter.org/
8 License: GPL2
9 */
10
11 define( 'DG_URL', plugin_dir_url( __FILE__ ) );
12 define( 'DG_IMG_STRING', '<img src="%s" title="%s" alt="%s" />' );
13 define( 'DG_DOC_ICON',
14 ' <div class="document-icon">'.PHP_EOL.
15 ' <a href="%s">%s<br>%s</a>'.PHP_EOL.
16 ' </div>'.PHP_EOL );
17
18 // CREATE GALLERY STRING //
19 function dg_get_attachment_icons($atts) {
20 extract( shortcode_atts( array(
21 'descriptions' => FALSE,
22 'orderby' => 'menu_order',
23 'order' => 'ASC',
24 'attachment_pg' => FALSE, // default: link directly to file (true to link to attachment pg)
25 'images' => FALSE, // if enabled, all images attached to current page will be included also
26 'ids' => FALSE // comma-separated list of attachment ids
27 ), $atts) );
28
29 // INIT
30 $attachments = array();
31 $count = 0;
32 $errs = array();
33
34
35 // ATTRIBUTE VALIDATION
36 $descriptions = strtolower($descriptions) == "false" ? FALSE : TRUE;
37
38 $order = strtoupper( $order );
39 if($order != 'ASC' && $order != 'DEC')
40 $errs[] = "The order attribute must be either ASC or DEC. You entered $order.";
41
42 $attachment_pg = strtolower($attachment_pg) == "false" ? FALSE : TRUE;
43
44 $images = strtolower($images) == "false" ? FALSE : TRUE;
45
46 if( strtolower($ids) == "false" ){ $ids = FALSE; }
47
48 // http://www.youtube.com/watch?v=ClnSMCdw6E8
49 if( $errs ) return implode(' ', $errs);
50 // END VALIDATION (WE MADE IT!)
51
52
53 // LET'S GET SOME DOCUMENTS!
54 if( $ids && ( $ids = explode( ',', $ids ) ) )
55 $attachments = dg_get_attachments_by_ids( $ids );
56
57 // if 'ids' was used, skip this
58 if( !$attachments ){
59 $args = array(
60 'numberposts' => -1,
61 'orderby' => $orderby,
62 'order' => $order,
63 'post_type' => 'attachment',
64 'post_mime_type' => 'application,video,text,audio',
65 'post_parent' => get_the_ID() );
66 if( $images ) $args['post_mime_type'] .= ',image';
67
68 $attachments = get_posts($args);
69 }
70
71 if ( $attachments ) {
72 $attachment_str = PHP_EOL.'<!-- Generated using Document Gallery. Get yours here: '.
73 'http://wordpress.org/extend/plugins/document-gallery -->'.PHP_EOL;
74
75 // DOCUMENT LOOP
76 foreach( $attachments as $attachment ) {
77 // INIT ATTACHMENT-SPECIFIC VARS
78 $url = $attachment->guid;
79 $filename = basename( $url );
80
81 if( $attachment_pg ) // link to attachment page
82 $url = get_attachment_link( $attachment->ID );
83
84 $title = get_the_title( $attachment->ID );
85 $icon = dg_get_attachment_image( $attachment->ID, $title, $filename );
86
87 // GENERATE OUTPUT
88 if($descriptions) { // start description wrapper
89 $attachment_str .= '<div class="document-icon-wrapper descriptions">'.PHP_EOL;
90
91 } elseif( $count % 4 == 0 ) { // no description
92 $attachment_str .= '<div id="document-icon-wrapper">'.PHP_EOL;
93 }
94
95 // insert filtered document-icon
96 $attachment_str .= apply_filters( 'dg_doc_icon',
97 sprintf( DG_DOC_ICON, $url, $icon, $title ), $attachment->ID );
98
99 if($descriptions) { // add description
100 $attachment_str .= " <p>$attachment->post_content</p>".
101 PHP_EOL.'</div>'.PHP_EOL;
102
103 } elseif( ++$count % 4 == 0 ) { // end wrapper
104 $attachment_str .= '</div>'.PHP_EOL;
105 }
106 } // END DOCUMENT LOOP
107
108 // for galleries w/ number of docs != mult of 4
109 if( $count % 4 != 0 && !$descriptions ) // end wrapper
110 $attachment_str .= '</div>'.PHP_EOL;
111
112 return $attachment_str;
113 } // END IF
114
115 // NO DOCUMENTS
116 return PHP_EOL.'<!-- Document Gallery: No attachments to display. How boring... -->'.PHP_EOL;
117 }
118 add_shortcode('dg', 'dg_get_attachment_icons');
119
120 // 'document gallery' shortcode depreciated as of v1.0. left for backward compatibility
121 add_shortcode('document gallery', 'dg_get_attachment_icons');
122
123
124 // HELPERS //
125 function dg_get_attachments_by_ids( $ids ){
126 $attachments = array();
127 foreach( $ids as $id ){
128 $attachment = get_post( $id );
129 if( $attachment->post_type == 'attachment' )
130 $attachments[] = $attachment;
131 // else: not an attachment so skip
132 }
133 return $attachments;
134 }
135
136 // pass in $title & $filename to avoid mult function calls
137 function dg_get_attachment_image( $id, $title, $filename ) {
138 $filetype = wp_check_filetype( $filename );
139
140 // identify extension
141 switch( $filetype['ext'] ) {
142 // Most Common First
143 case 'pdf':
144 $icon = DG_URL.'icons/pdf.png';
145 break;
146 // MS Office
147 case 'doc':
148 case 'docx':
149 case 'docm':
150 case 'dotx':
151 case 'dotm':
152 $icon = DG_URL.'icons/msdoc.png';
153 break;
154 case 'ppt':
155 case 'pot':
156 case 'pps':
157 case 'pptx':
158 case 'pptm':
159 case 'ppsx':
160 case 'ppsm':
161 case 'potx':
162 case 'potm':
163 case 'ppam':
164 case 'sldx':
165 case 'sldm':
166 $icon = DG_URL.'icons/msppt.png';
167 break;
168 case 'xla':
169 case 'xls':
170 case 'xlt':
171 case 'xlw':
172 case 'xlsx':
173 case 'xlsm':
174 case 'xlsb':
175 case 'xltx':
176 case 'xltm':
177 case 'xlam':
178 $icon = DG_URL.'icons/msxls.png';
179 break;
180 case 'mdb':
181 $icon = DG_URL.'icons/msaccess.png';
182 break;
183 // Video formats
184 case 'avi':
185 $icon = DG_URL.'icons/avi.png';
186 break;
187 case 'divx':
188 $icon = DG_URL.'icons/divx.png';
189 break;
190 case 'flv':
191 $icon = DG_URL.'icons/flv.png';
192 break;
193 case 'qt':
194 case 'mov':
195 $icon = DG_URL.'icons/mov.png';
196 break;
197 case 'asf':
198 case 'asx':
199 case 'wax':
200 case 'wmv':
201 case 'wmx':
202 $icon = DG_URL.'icons/wmv.png';
203 break;
204 case 'mkv':
205 $icon = DG_URL.'icons/mkv.png';
206 break;
207 // Audio formats
208 case 'mp3':
209 $icon = DG_URL.'icons/mp3.png';
210 break;
211 case 'wav':
212 $icon = DG_URL.'icons/wav.png';
213 break;
214 case 'ogg':
215 case 'oga':
216 $icon = DG_URL.'icons/ogg.png';
217 break;
218 case 'midi':
219 case 'mid':
220 $icon = DG_URL.'icons/midi.png';
221 break;
222 case 'wma':
223 $icon = DG_URL.'icons/wma.png';
224 break;
225 // Text formats
226 case 'rtx':
227 $icon = DG_URL.'icons/rtx.png';
228 break;
229 case 'ics':
230 $icon = DG_URL.'icons/ics.png';
231 break;
232 case 'csv':
233 $icon = DG_URL.'icons/csv.png';
234 break;
235 // Msc application formats
236 case 'html':
237 case 'htm': // death to all who use this!
238 $icon = DG_URL.'icons/html.png';
239 break;
240 case 'css':
241 $icon = DG_URL.'icons/css.png';
242 break;
243 case 'js':
244 $icon = DG_URL.'icons/javascript.png';
245 break;
246 case 'class':
247 $icon = DG_URL.'icons/java.png';
248 break;
249 case 'zip':
250 $icon = DG_URL.'icons/zip.png';
251 break;
252 case 'tar':
253 case 'gzip':
254 case 'gz':
255 case 'bz2': // not yet WP-supported
256 case 'tgz': // not yet WP-supported
257 $icon = DG_URL.'icons/compressed.png';
258 break;
259 case 'rar': // RAWR!!!
260 $icon = DG_URL.'icons/rar.png';
261 break;
262 case '7z':
263 $icon = DG_URL.'icons/7zip.png';
264 break;
265 case 'exec':
266 $icon = DG_URL.'icons/exec.png';
267 break;
268 case 'rtf':
269 $icon = DG_URL.'icons/rtf.png';
270 break;
271 case 'swf':
272 $icon = DG_URL.'icons/shockwave.png';
273 break;
274 // OpenOffice formats
275 case 'odt':
276 $icon = DG_URL.'icons/opendocument-text.png';
277 break;
278 case 'odp':
279 $icon = DG_URL.'icons/opendocument-presentation.png';
280 break;
281 case 'ods':
282 $icon = DG_URL.'icons/opendocument-spreadsheet.png';
283 break;
284 case 'odg':
285 $icon = DG_URL.'icons/opendocument-graphics.png';
286 break;
287 case 'odb':
288 $icon = DG_URL.'icons/opendocument-database.png';
289 break;
290 case 'odf':
291 $icon = DG_URL.'icons/opendocument-formula.png';
292 break;
293 default:
294 // handle images
295 if( strpos( $filetype['type'], 'image' ) === 0 &&
296 ( $icon = wp_get_attachment_image_src( $id, 'thumbnail', false ) ) ){
297 $icon = $icon[0];
298 break;
299 }
300
301 // fallback to default icons if not recognized
302 if( $icon = wp_get_attachment_image_src( $id, null, true ) ){
303 $icon = $icon[0];
304 break;
305 }
306
307 // everything failed. This is bad...
308 return "<!-- Failed to retrive icon for attachment #$id -->";
309 }
310
311 return sprintf( DG_IMG_STRING, $icon, $title, $title );
312 }
313
314 // ADD SOME STYLING
315 function dg_add_header_css() {
316 wp_enqueue_style( 'document-gallery-css', plugins_url('style.css', __FILE__) );
317 }
318 add_action( 'wp_print_styles', 'dg_add_header_css');
319
320 ?>
321