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