| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: Document Gallery |
| 4 |
Description: Display non-images in gallery format on page. |
| 5 |
Version: 1.1 |
| 6 |
Author: Dan Rossiter |
| 7 |
Author URI: http://danrossiter.org/ |
| 8 |
License: GPL2 |
| 9 |
*/ |
| 10 |
|
| 11 |
define( 'DG_URL', plugin_dir_url( __FILE__ ) ); |
| 12 |
|
| 13 |
// CREATE GALLERY STRING // |
| 14 |
function dg_get_attachment_icons($atts) { |
| 15 |
extract( shortcode_atts( array( |
| 16 |
'descriptions' => FALSE, |
| 17 |
'orderby' => 'menu_order', |
| 18 |
'order' => 'ASC', |
| 19 |
'attachment_pg' => FALSE, // default: link directly to file (true to link to attachment pg) |
| 20 |
'ids' => FALSE // not yet supported |
| 21 |
), $atts) ); |
| 22 |
|
| 23 |
|
| 24 |
// Some validation of user values |
| 25 |
$errs = array(); |
| 26 |
|
| 27 |
if($descriptions != FALSE){ $descriptions = TRUE; } |
| 28 |
|
| 29 |
$order = strtoupper( $order ); |
| 30 |
if($order != 'ASC' && $order != 'DEC') |
| 31 |
$errs[] = "The order attribute must be either ASC or DEC. You entered $order."; |
| 32 |
|
| 33 |
if($attachment_pg != FALSE){ $attachment_pg = TRUE; } |
| 34 |
if($ids != FALSE){ $ids = FALSE; } // not yet supported |
| 35 |
|
| 36 |
// http://www.youtube.com/watch?v=ClnSMCdw6E8 |
| 37 |
if( $errs ) return implode(' ', $errs); |
| 38 |
// All's well. Carry on, my wayward son. |
| 39 |
|
| 40 |
|
| 41 |
$args = array( |
| 42 |
'numberposts' => -1, |
| 43 |
'orderby' => $orderby, |
| 44 |
'order' => $order, |
| 45 |
'post_type' => 'attachment', |
| 46 |
'post_mime_type' => 'application,video,text,audio', |
| 47 |
'post_parent' => get_the_ID() ); |
| 48 |
|
| 49 |
if ( $attachments = get_posts($args) ) { |
| 50 |
$attachment_str = array( PHP_EOL.'<!-- Generated using Document Gallery. Get yours here: '. |
| 51 |
'http://wordpress.org/extend/plugins/document-gallery -->'.PHP_EOL ); |
| 52 |
|
| 53 |
$count = 0; |
| 54 |
foreach( $attachments as $attachment ) { //setup array for more than one file attachment |
| 55 |
$url = $attachment->guid; |
| 56 |
$filename = basename( $url ); |
| 57 |
|
| 58 |
if( $attachment_pg ) // link to attachment page |
| 59 |
$url = get_attachment_link( $attachment->ID ); |
| 60 |
|
| 61 |
$title = get_the_title( $attachment->ID ); |
| 62 |
$icon = dg_get_attachment_image( $attachment->ID, $title, $filename ); |
| 63 |
|
| 64 |
if($descriptions) { // start wrapper |
| 65 |
$attachment_str[] = '<div class="document-icon-wrapper descriptions">'.PHP_EOL. |
| 66 |
' <div class="document-icon">'.PHP_EOL; |
| 67 |
} else { // no description |
| 68 |
if( $count % 4 == 0 ) { // start wrapper |
| 69 |
$attachment_str[] = '<div id="document-icon-wrapper">'.PHP_EOL; |
| 70 |
} |
| 71 |
$attachment_str[] = ' <div class="document-icon">'.PHP_EOL; |
| 72 |
} |
| 73 |
|
| 74 |
$attachment_str[] = " <a href=\"$url\">$icon<br>$title</a>".PHP_EOL; |
| 75 |
|
| 76 |
if($descriptions) { // end icon & add description |
| 77 |
$attachment_str[] = ' </div>'.PHP_EOL. |
| 78 |
" <p>$attachment->post_content</p>". |
| 79 |
PHP_EOL.'</div>'.PHP_EOL; |
| 80 |
} else { // end icon |
| 81 |
$attachment_str[] = ' </div>'.PHP_EOL; |
| 82 |
if( ++$count % 4 == 0 ) { // end wrapper |
| 83 |
$attachment_str[] = '</div>'.PHP_EOL; |
| 84 |
} |
| 85 |
} |
| 86 |
} // end looping attachments |
| 87 |
|
| 88 |
// for galleries w/ number of docs != mult of 4 |
| 89 |
if( $count % 4 != 0 && !$descriptions ){ // end wrapper |
| 90 |
$attachment_str[] = '</div>'.PHP_EOL; |
| 91 |
} |
| 92 |
|
| 93 |
// join array & return |
| 94 |
return implode( '', $attachment_str ); |
| 95 |
} // end if attachments |
| 96 |
|
| 97 |
return PHP_EOL.'<!-- Document Gallery: No attachments to display. -->'.PHP_EOL; |
| 98 |
} |
| 99 |
add_shortcode('dg', 'dg_get_attachment_icons'); |
| 100 |
// Depreciated as of v1.0. left for backward compatibility |
| 101 |
add_shortcode('document gallery', 'dg_get_attachment_icons'); |
| 102 |
|
| 103 |
|
| 104 |
// ADD SOME STYLING // |
| 105 |
function dg_add_header_css() { |
| 106 |
wp_enqueue_style( 'document-gallery-css', plugins_url('style.css', __FILE__) ); |
| 107 |
} |
| 108 |
add_action( 'wp_print_styles', 'dg_add_header_css'); |
| 109 |
|
| 110 |
|
| 111 |
// HELPERS // |
| 112 |
|
| 113 |
// pass in $title & $url to avoid mult function calls |
| 114 |
function dg_get_attachment_image( $id, $title, $filename ) { |
| 115 |
$filetype = wp_check_filetype( $filename ); |
| 116 |
|
| 117 |
// identify extension |
| 118 |
switch( $filetype['ext'] ) { |
| 119 |
// Most Common First |
| 120 |
case 'pdf': |
| 121 |
$icon = 'pdf.png'; |
| 122 |
break; |
| 123 |
// MS Office |
| 124 |
case 'doc': |
| 125 |
case 'docx': |
| 126 |
case 'docm': |
| 127 |
case 'dotx': |
| 128 |
case 'dotm': |
| 129 |
$icon = 'msdoc.png'; |
| 130 |
break; |
| 131 |
case 'ppt': |
| 132 |
case 'pot': |
| 133 |
case 'pps': |
| 134 |
case 'pptx': |
| 135 |
case 'pptm': |
| 136 |
case 'ppsx': |
| 137 |
case 'ppsm': |
| 138 |
case 'potx': |
| 139 |
case 'potm': |
| 140 |
case 'ppam': |
| 141 |
case 'sldx': |
| 142 |
case 'sldm': |
| 143 |
$icon = 'msppt.png'; |
| 144 |
break; |
| 145 |
case 'xla': |
| 146 |
case 'xls': |
| 147 |
case 'xlt': |
| 148 |
case 'xlw': |
| 149 |
case 'xlsx': |
| 150 |
case 'xlsm': |
| 151 |
case 'xlsb': |
| 152 |
case 'xltx': |
| 153 |
case 'xltm': |
| 154 |
case 'xlam': |
| 155 |
$icon = 'msxls.png'; |
| 156 |
break; |
| 157 |
case 'mdb': |
| 158 |
$icon = 'msaccess.png'; |
| 159 |
break; |
| 160 |
// Video formats |
| 161 |
case 'avi': |
| 162 |
$icon = 'avi.png'; |
| 163 |
break; |
| 164 |
case 'divx': |
| 165 |
$icon = 'divx.png'; |
| 166 |
break; |
| 167 |
case 'flv': |
| 168 |
$icon = 'flv.png'; |
| 169 |
break; |
| 170 |
case 'qt': |
| 171 |
case 'mov': |
| 172 |
$icon = 'mov.png'; |
| 173 |
break; |
| 174 |
case 'asf': |
| 175 |
case 'asx': |
| 176 |
case 'wax': |
| 177 |
case 'wmv': |
| 178 |
case 'wmx': |
| 179 |
$icon = 'wmv.png'; |
| 180 |
break; |
| 181 |
case 'mkv': |
| 182 |
$icon = 'mkv.png'; |
| 183 |
break; |
| 184 |
// Audio formats |
| 185 |
case 'mp3': |
| 186 |
$icon = 'mp3.png'; |
| 187 |
break; |
| 188 |
case 'wav': |
| 189 |
$icon = 'wav.png'; |
| 190 |
break; |
| 191 |
case 'ogg': |
| 192 |
case 'oga': |
| 193 |
$icon = 'ogg.png'; |
| 194 |
break; |
| 195 |
case 'midi': |
| 196 |
case 'mid': |
| 197 |
$icon = 'midi.png'; |
| 198 |
break; |
| 199 |
case 'wma': |
| 200 |
$icon = 'wma.png'; |
| 201 |
break; |
| 202 |
// Text formats |
| 203 |
case 'rtx': |
| 204 |
$icon = 'rtx.png'; |
| 205 |
break; |
| 206 |
case 'ics': |
| 207 |
$icon = 'ics.png'; |
| 208 |
break; |
| 209 |
case 'csv': |
| 210 |
$icon = 'csv.png'; |
| 211 |
break; |
| 212 |
// Msc application formats |
| 213 |
case 'html': |
| 214 |
case 'htm': // death to all who use this! |
| 215 |
$icon = 'html.png'; |
| 216 |
break; |
| 217 |
case 'css': |
| 218 |
$icon = 'css.png'; |
| 219 |
break; |
| 220 |
case 'js': |
| 221 |
$icon = 'javascript.png'; |
| 222 |
break; |
| 223 |
case 'class': |
| 224 |
$icon = 'java.png'; |
| 225 |
break; |
| 226 |
case 'zip': |
| 227 |
$icon = 'zip.png'; |
| 228 |
break; |
| 229 |
case 'tar': |
| 230 |
case 'gzip': |
| 231 |
case 'gz': |
| 232 |
case 'bz2': // not yet WP-supported |
| 233 |
case 'tgz': // not yet WP-supported |
| 234 |
$icon = 'compressed.png'; |
| 235 |
break; |
| 236 |
case 'rar': // RAWR!!! |
| 237 |
$icon = 'rar.png'; |
| 238 |
break; |
| 239 |
case '7z': |
| 240 |
$icon = '7zip.png'; |
| 241 |
break; |
| 242 |
case 'exec': |
| 243 |
$icon = 'exec.png'; |
| 244 |
break; |
| 245 |
case 'rtf': |
| 246 |
$icon = 'rtf.png'; |
| 247 |
break; |
| 248 |
case 'swf': |
| 249 |
$icon = 'shockwave.png'; |
| 250 |
break; |
| 251 |
// OpenOffice formats |
| 252 |
case 'odt': |
| 253 |
$icon = 'opendocument-text.png'; |
| 254 |
break; |
| 255 |
case 'odp': |
| 256 |
$icon = 'opendocument-presentation.png'; |
| 257 |
break; |
| 258 |
case 'ods': |
| 259 |
$icon = 'opendocument-spreadsheet.png'; |
| 260 |
break; |
| 261 |
case 'odg': |
| 262 |
$icon = 'opendocument-graphics.png'; |
| 263 |
break; |
| 264 |
case 'odb': |
| 265 |
$icon = 'opendocument-database.png'; |
| 266 |
break; |
| 267 |
case 'odf': |
| 268 |
$icon = 'opendocument-formula.png'; |
| 269 |
break; |
| 270 |
// fallback to default icons if not recognized |
| 271 |
default: |
| 272 |
// get_attachment_icon is DEPRECIATED! (replaced in v1.1) |
| 273 |
return wp_get_attachment_image( $id, null, true ); |
| 274 |
} |
| 275 |
|
| 276 |
return '<img src="'.DG_URL.'icons/'.$icon."\" title=\"$title\" alt=\"$title\"/>"; |
| 277 |
} |
| 278 |
// Filtering attachment_icon was considered, then dismissed in v1.0.3 because it would mean almost |
| 279 |
// doubling the amount of processing for each icon. The native WP function would create the icon, |
| 280 |
// then 99% of the time this function would replace it. Better to just call the native WP function |
| 281 |
// at the end when needed. Filter would look like this: |
| 282 |
// add_filter( 'attachment_icon', 'dg_get_attachment_icon', 10, 2 );v |
| 283 |
?> |
| 284 |
|