| 1 |
<?php |
| 2 |
defined( 'WPINC' ) OR exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Holds data specific to a given document. |
| 6 |
* |
| 7 |
* @author drossiter |
| 8 |
*/ |
| 9 |
class DG_Document { |
| 10 |
|
| 11 |
/*========================================================================== |
| 12 |
* PRIVATE FIELDS |
| 13 |
*=========================================================================*/ |
| 14 |
|
| 15 |
// general document data |
| 16 |
private $description, $gallery, $ID, $link, $title, $title_attribute, $path, $extension, $size; |
| 17 |
|
| 18 |
/*========================================================================== |
| 19 |
* INIT GALLERY |
| 20 |
*=========================================================================*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* Constructs instance of Document. |
| 24 |
* |
| 25 |
* @param WP_Post $attachment Attachment object used to initalize fields. |
| 26 |
* @param DG_Gallery $gallery Instance of Gallery class. |
| 27 |
*/ |
| 28 |
public function __construct( $attachment, $gallery ) { |
| 29 |
// init general document data |
| 30 |
$this->gallery = $gallery; |
| 31 |
$this->description = wptexturize( $attachment->post_content ); |
| 32 |
$this->ID = $attachment->ID; |
| 33 |
$this->link = $gallery->linkToAttachmentPg() |
| 34 |
? get_attachment_link( $attachment->ID ) |
| 35 |
: wp_get_attachment_url( $attachment->ID ); |
| 36 |
$this->title = wptexturize( $attachment->post_title ); |
| 37 |
$this->title_attribute = esc_attr( strip_tags( $this->title ) ); |
| 38 |
|
| 39 |
$this->path = get_attached_file( $attachment->ID ); |
| 40 |
$wp_filetype = wp_check_filetype_and_ext( $this->path, basename( $this->path ) ); |
| 41 |
$this->extension = $wp_filetype['ext']; |
| 42 |
$this->size = size_format( filesize( $this->path ) ); |
| 43 |
} |
| 44 |
|
| 45 |
/*========================================================================== |
| 46 |
* OUTPUT HTML STRING |
| 47 |
*=========================================================================*/ |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns HTML representing this Document. |
| 51 |
* @filter dg_icon_template Filters the DG icon HTML. Passes a single |
| 52 |
* bool value indicating whether the gallery is using descriptions or not. |
| 53 |
* @return string The gallery HTML. |
| 54 |
*/ |
| 55 |
public function __toString() { |
| 56 |
include_once DG_PATH . 'inc/class-thumber.php'; |
| 57 |
$options = DG_Thumber::getOptions(); |
| 58 |
|
| 59 |
$thumb = null; |
| 60 |
$data = ''; |
| 61 |
$description = ''; |
| 62 |
$target = $this->gallery->openLinkInNewWindow() ? '_blank' : '_self'; |
| 63 |
|
| 64 |
if ( $this->gallery->useFancyThumbs() ) { |
| 65 |
if ( array_key_exists( $this->ID, $options['thumbs'] ) ) { |
| 66 |
// icon has already been generated so include it in generated gallery |
| 67 |
$thumb = DG_Thumber::getThumbnail( $this->ID, 1, false ); |
| 68 |
} else { |
| 69 |
// include a data-* attribute for client side to asynchronously request icon after gallery load |
| 70 |
$data = ' data-dg-id="' . $this->ID . '"'; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
if ( is_null($thumb) ) { |
| 75 |
$thumb = DG_Thumber::getDefaultThumbnail( $this->ID ); |
| 76 |
} |
| 77 |
|
| 78 |
$repl = array( $this->link, $thumb, $this->title_attribute, $this->title, $target, $this->extension, $this->size, $this->path ); |
| 79 |
$find = array( '%link%', '%img%', '%title_attribute%', '%title%', '%target%', '%extension%', '%size%', '%path%' ); |
| 80 |
|
| 81 |
// if descriptions then add filterable tag and value to replaced tag |
| 82 |
if ( $this->gallery->useDescriptions() ) { |
| 83 |
$repl[] = $this->description; |
| 84 |
$find[] = '%description%'; |
| 85 |
$description = ' <p>%description%</p>'; |
| 86 |
} |
| 87 |
|
| 88 |
$doc_icon = |
| 89 |
' <div class="document-icon"'. $data . '>' . PHP_EOL . |
| 90 |
' <a href="%link%" target="%target%"><img src="%img%" title="%title_attribute%" alt="%title_attribute%" /><br>%title%</a>' . PHP_EOL . |
| 91 |
' </div>' . PHP_EOL . |
| 92 |
$description; |
| 93 |
|
| 94 |
// allow developers to filter icon output |
| 95 |
$doc_icon = apply_filters( |
| 96 |
'dg_icon_template', |
| 97 |
$doc_icon, |
| 98 |
$this->gallery->useDescriptions(), |
| 99 |
$this->ID ); |
| 100 |
|
| 101 |
return str_replace( $find, $repl, $doc_icon ); |
| 102 |
} |
| 103 |
} |