| 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 |
$size = @filesize( $this->path ); |
| 43 |
$this->size = ($size !== false) ? size_format( $size ) : 0; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @return int The attachment ID. |
| 48 |
*/ |
| 49 |
public function getId() { |
| 50 |
return $this->ID; |
| 51 |
} |
| 52 |
|
| 53 |
/*========================================================================== |
| 54 |
* OUTPUT HTML STRING |
| 55 |
*=========================================================================*/ |
| 56 |
|
| 57 |
/** |
| 58 |
* Returns HTML representing this Document. |
| 59 |
* @filter dg_icon_template Filters the DG icon HTML. Passes a single |
| 60 |
* bool value indicating whether the gallery is using descriptions or not. |
| 61 |
* @return string The gallery HTML. |
| 62 |
*/ |
| 63 |
public function __toString() { |
| 64 |
include_once DG_PATH . 'inc/class-thumber.php'; |
| 65 |
|
| 66 |
$data = ''; |
| 67 |
$description = ''; |
| 68 |
$target = $this->gallery->openLinkInNewWindow() ? '_blank' : '_self'; |
| 69 |
|
| 70 |
if ( $this->gallery->useFancyThumbs() ) { |
| 71 |
$thumb_obj = DG_Thumb::getThumb( $this->ID ); |
| 72 |
if ( ! is_null( $thumb_obj ) ) { |
| 73 |
if ( $thumb_obj->isSuccess() ) { |
| 74 |
// icon has already been generated so include it in generated gallery |
| 75 |
$thumb = $thumb_obj->getUrl(); |
| 76 |
} |
| 77 |
} else { |
| 78 |
// include a data-* attribute for client side to asynchronously request icon after gallery load |
| 79 |
$data = 'data-id="' . $this->ID . '"'; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
if ( ! isset( $thumb ) ) { |
| 84 |
$thumb = DG_DefaultThumber::init()->getThumbnail( $this->ID ); |
| 85 |
} |
| 86 |
|
| 87 |
$repl = array( $this->link, $thumb, $this->title_attribute, $this->title, $target, $this->extension, $this->size, $this->path, $data ); |
| 88 |
$find = array( '%link%', '%img%', '%title_attribute%', '%title%', '%target%', '%extension%', '%size%', '%path%', '%data%' ); |
| 89 |
|
| 90 |
// if descriptions then add filterable tag and value to replaced tag |
| 91 |
if ( $this->gallery->useDescriptions() ) { |
| 92 |
$repl[] = $this->description; |
| 93 |
$find[] = '%description%'; |
| 94 |
$description = ' <p>%description%</p>'; |
| 95 |
} |
| 96 |
|
| 97 |
$doc_icon = |
| 98 |
' <div class="document-icon">' . PHP_EOL . |
| 99 |
' <a href="%link%" target="%target%">' . PHP_EOL . |
| 100 |
' <img src="%img%" title="%title_attribute%" alt="%title_attribute%" %data%/>' . PHP_EOL . |
| 101 |
' <span class="title">%title%</span>' . PHP_EOL . |
| 102 |
' </a>' . PHP_EOL . |
| 103 |
' </div>' . PHP_EOL . |
| 104 |
$description; |
| 105 |
|
| 106 |
// allow developers to filter icon output |
| 107 |
$doc_icon = apply_filters( 'dg_icon_template', $doc_icon, $this->gallery->useDescriptions(), $this->ID ); |
| 108 |
|
| 109 |
return str_replace( $find, $repl, $doc_icon ); |
| 110 |
} |
| 111 |
} |