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