| @@ -1,26 +1,58 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /* |
| 3 | 3 | Plugin Name: Document Gallery |
| 4 | -Description: Display non-images in gallery format on page. | |
| 5 | -Version: 1.0 | |
| 4 | +Description: Display non-images (and images) in gallery format on a page or post with the [dg] shortcode. | |
| 5 | +Version: 1.2 | |
| 6 | 6 | Author: Dan Rossiter |
| 7 | 7 | Author URI: http://danrossiter.org/ |
| 8 | 8 | License: GPL2 |
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | -define( 'DG_URL', plugin_dir_url().'document-gallery/' ); | |
| 11 | +define( 'DG_URL', plugin_dir_url( __FILE__ ) ); | |
| 12 | 12 | |
| 13 | 13 | // CREATE GALLERY STRING // |
| 14 | 14 | function dg_get_attachment_icons($atts) { |
| 15 | 15 | extract( shortcode_atts( array( |
| 16 | 16 | 'descriptions' => FALSE, |
| 17 | - 'echo' => FALSE, | |
| 18 | 17 | 'orderby' => 'menu_order', |
| 19 | - 'order' => 'ASC' | |
| 18 | + 'order' => 'ASC', | |
| 19 | + 'attachment_pg' => FALSE, // default: link directly to file (true to link to attachment pg) | |
| 20 | + 'images' => FALSE, // if enabled, all images attached to current page will be included also | |
| 21 | + 'ids' => FALSE // comma-separated list of attachment ids | |
| 20 | 22 | ), $atts) ); |
| 21 | - | |
| 22 | - $args = array( | |
| 23 | + | |
| 24 | + // INIT | |
| 25 | + $attachments = array(); | |
| 26 | + $errs = array(); | |
| 27 | + | |
| 28 | + | |
| 29 | + // ATTRIBUTE VALIDATION | |
| 30 | + if( strtolower($descriptions) == "false" ){ $descriptions = FALSE; } | |
| 31 | + | |
| 32 | + $order = strtoupper( $order ); | |
| 33 | + if($order != 'ASC' && $order != 'DEC') | |
| 34 | + $errs[] = "The order attribute must be either ASC or DEC. You entered $order."; | |
| 35 | + | |
| 36 | + if( strtolower($attachment_pg) == "false" ){ $attachment_pg = FALSE; } | |
| 37 | + | |
| 38 | + if( strtolower($images) == "false" ){ $images = FALSE; } | |
| 39 | + | |
| 40 | + if( strtolower($ids) ){ $ids = FALSE; } | |
| 41 | + | |
| 42 | + // http://www.youtube.com/watch?v=ClnSMCdw6E8 | |
| 43 | + if( $errs ) return implode(' ', $errs); | |
| 44 | + // END VALIDATION (WE MADE IT!) | |
| 45 | + | |
| 46 | + | |
| 47 | + // LET'S GET SOME DOCUMENTS! | |
| 48 | + if( $ids && ( $ids = explode( ',', $ids ) ) ){ | |
| 49 | + $attachments = dg_get_attachments_by_ids( $ids ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + // if 'ids' was used, skip | |
| 53 | + if( !$attachments ){ | |
| 54 | + $args = array( | |
| 23 | 55 | 'numberposts' => -1, |
| 24 | 56 | 'orderby' => $orderby, |
| 25 | 57 | 'order' => $order, |
| 26 | 58 | 'post_type' => 'attachment', |
| @@ -25,73 +57,83 @@ | ||
| 25 | 57 | 'order' => $order, |
| 26 | 58 | 'post_type' => 'attachment', |
| 27 | 59 | 'post_mime_type' => 'application,video,text,audio', |
| 28 | 60 | 'post_parent' => get_the_ID() ); |
| 61 | + if( $images ) $args['post_mime_type'] .= ',image'; | |
| 62 | + | |
| 63 | + $attachments = get_posts($args); | |
| 64 | + } | |
| 29 | 65 | |
| 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 ); | |
| 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 ); | |
| 33 | 69 | |
| 34 | - if($descriptions) { | |
| 35 | - $attachment_str[] = '<table id="document-icon-wrapper">'; | |
| 36 | - } else { | |
| 37 | - $attachment_str[] = '<div id="document-icon-wrapper">'; | |
| 38 | - } | |
| 39 | - | |
| 40 | 70 | $count = 0; |
| 41 | 71 | foreach( $attachments as $attachment ) { //setup array for more than one file attachment |
| 42 | - $url = wp_get_attachment_url( $attachment->ID ); | |
| 43 | - $title = wp_get_attachment_link( $attachment->ID ); | |
| 44 | - $icon = dg_get_attachment_icon( $attachment->ID, $tile, $url ); | |
| 72 | + $url = $attachment->guid; | |
| 73 | + $filename = basename( $url ); | |
| 74 | + | |
| 75 | + if( $attachment_pg ) // link to attachment page | |
| 76 | + $url = get_attachment_link( $attachment->ID ); | |
| 77 | + | |
| 78 | + $title = get_the_title( $attachment->ID ); | |
| 79 | + $icon = dg_get_attachment_image( $attachment->ID, $title, $filename ); | |
| 45 | 80 | |
| 46 | - if($descriptions) { | |
| 47 | - $attachment_str[] = '<tr><td class="document-icon">'; | |
| 48 | - } else { | |
| 49 | - $attachment_str[] = '<div class="document-icon">'; | |
| 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; | |
| 50 | 89 | } |
| 51 | 90 | |
| 52 | - $attachment_str[] = "<a href=\"$url\">$icon</a><br><a href=\"$url\">$title</a>"; | |
| 91 | + $attachment_str[] = " <a href=\"$url\">$icon<br>$title</a>".PHP_EOL; | |
| 53 | 92 | |
| 54 | - if($descriptions) { | |
| 55 | - $attachment_str[] = "</td><td valign=\"top\"><p>$attachment->post_content</p></td></tr>"; | |
| 56 | - } else { | |
| 57 | - $attachment_str[] = '</div>'; | |
| 58 | - if( ++$count % 4 == 0 ) { | |
| 59 | - $attachment_str[] = '<hr>'; | |
| 93 | + if($descriptions) { // end icon & add description | |
| 94 | + $attachment_str[] = ' </div>'.PHP_EOL. | |
| 95 | + " <p>$attachment->post_content</p>". | |
| 96 | + PHP_EOL.'</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; | |
| 60 | 101 | } |
| 61 | 102 | } |
| 62 | 103 | } // end looping attachments |
| 63 | 104 | |
| 64 | - // close #document-icon-wrapper | |
| 65 | - if($descriptions) { | |
| 66 | - $attachment_str[] = '</table>'; | |
| 67 | - } else { | |
| 68 | - $attachment_str[] = '</div>'; | |
| 105 | + // for galleries w/ number of docs != mult of 4 | |
| 106 | + if( $count % 4 != 0 && !$descriptions ){ // end wrapper | |
| 107 | + $attachment_str[] = '</div>'.PHP_EOL; | |
| 69 | 108 | } |
| 70 | 109 | |
| 71 | 110 | // join array & return |
| 72 | - $attachment_str = implode( '', $attachment_str ); | |
| 73 | - return $attachment_str; | |
| 74 | - } // end if attachments | |
| 111 | + return implode( '', $attachment_str ); | |
| 112 | + } // END DOCUMENT LOOP | |
| 75 | 113 | |
| 76 | - return '<!-- Document Gallery: No attachments to display. -->'.PHP_EOL; | |
| 114 | + return PHP_EOL.'<!-- Document Gallery: No attachments to display. -->'.PHP_EOL; | |
| 77 | 115 | } |
| 78 | -add_shortcode('document gallery', 'dg_get_attachment_icons'); | |
| 79 | 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'); | |
| 80 | 119 | |
| 81 | 120 | |
| 82 | -// ADD SOME STYLING // | |
| 83 | -function dg_add_header_css() { | |
| 84 | - wp_enqueue_style( 'document-gallery-css', plugins_url('style.css', __FILE__) ); | |
| 85 | -} | |
| 86 | -add_action( 'wp_print_styles', 'dg_add_header_css'); | |
| 87 | 121 | |
| 88 | - | |
| 89 | 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 | +} | |
| 90 | 133 | |
| 91 | 134 | // pass in $title & $url to avoid mult function calls |
| 92 | -function dg_get_attachment_icon( $id, $title, $url ) { | |
| 93 | - $filename = basename( $url ); | |
| 135 | +function dg_get_attachment_image( $id, $title, $filename ) { | |
| 94 | 136 | $filetype = wp_check_filetype( $filename ); |
| 95 | 137 | |
| 96 | 138 | // identify extension |
| 97 | 139 | switch( $filetype['ext'] ) { |
| @@ -247,11 +289,32 @@ | ||
| 247 | 289 | $icon = 'opendocument-formula.png'; |
| 248 | 290 | break; |
| 249 | 291 | // fallback to default icons if not recognized |
| 250 | 292 | default: |
| 251 | - return get_attachment_icon( $id ); | |
| 293 | + // handle images | |
| 294 | + if( preg_match( '/^image/', $filetype['type'] ) && | |
| 295 | + ( $icon = wp_get_attachment_image( $id, 'thumbnail', false ) ) ) | |
| 296 | + return $icon; | |
| 297 | + | |
| 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; | |
| 301 | + | |
| 302 | + return "<!-- Failed to retrive icon for attachment #$id -->"; // everything failed. This is bad... | |
| 252 | 303 | } |
| 253 | 304 | |
| 254 | - $icon = '<img src="'.DG_URL.'icons/'.$icon."\" title=\"$title\" alt=\"$title\"/>"; | |
| 255 | - return $icon; | |
| 305 | + return '<img src="'.DG_URL."icons/$icon\" title=\"$title\" alt=\"$title\" />"; | |
| 256 | 306 | } |
| 307 | + | |
| 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 | +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 | + | |
| 257 | 320 | ?> |