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