| 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, $date, $time, $author; |
| 17 |
|
| 18 |
/*========================================================================== |
| 19 |
* GETTERS |
| 20 |
*=========================================================================*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* @return int The attachment ID. |
| 24 |
*/ |
| 25 |
public function getId() { |
| 26 |
return $this->ID; |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* @return string This document's description. |
| 31 |
*/ |
| 32 |
public function getDescription() { |
| 33 |
return $this->description; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* @return DG_Gallery The gallery which this document is a member. |
| 38 |
*/ |
| 39 |
public function getGallery() { |
| 40 |
return $this->gallery; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @return string The link to be followed when clicking this document. |
| 45 |
*/ |
| 46 |
public function getLink() { |
| 47 |
return $this->link; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* @return string The title of this document. |
| 52 |
*/ |
| 53 |
public function getTitle() { |
| 54 |
return $this->title; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @return string The title attribute string. |
| 59 |
*/ |
| 60 |
public function getTitleAttribute() { |
| 61 |
return $this->title_attribute; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @return string The path to this document. |
| 66 |
*/ |
| 67 |
public function getPath() { |
| 68 |
return $this->path; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* @return string The extension of this document's file. |
| 73 |
*/ |
| 74 |
public function getExtension() { |
| 75 |
return $this->extension; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @return string The size of this document human-readable formatted. |
| 80 |
*/ |
| 81 |
public function getSize() { |
| 82 |
return $this->size; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/*========================================================================== |
| 87 |
* INIT GALLERY |
| 88 |
*=========================================================================*/ |
| 89 |
|
| 90 |
/** |
| 91 |
* Constructs instance of Document. |
| 92 |
* |
| 93 |
* @param WP_Post $attachment Attachment object used to initalize fields. |
| 94 |
* @param DG_Gallery $gallery Instance of Gallery class. |
| 95 |
*/ |
| 96 |
public function __construct( $attachment, $gallery ) { |
| 97 |
// init general document data |
| 98 |
$this->gallery = $gallery; |
| 99 |
$this->description = wptexturize( $attachment->post_content ); |
| 100 |
$this->ID = $attachment->ID; |
| 101 |
$this->link = $gallery->linkToAttachmentPg() |
| 102 |
? get_attachment_link( $attachment->ID ) |
| 103 |
: wp_get_attachment_url( $attachment->ID ); |
| 104 |
$this->title = wptexturize( $attachment->post_title ); |
| 105 |
$this->title_attribute = esc_attr( strip_tags( $this->title ) ); |
| 106 |
|
| 107 |
$this->path = get_attached_file( $attachment->ID ); |
| 108 |
$wp_filetype = wp_check_filetype_and_ext( $this->path, basename( $this->path ) ); |
| 109 |
$this->extension = $wp_filetype['ext']; |
| 110 |
$size = @filesize( $this->path ); |
| 111 |
$this->size = ($size !== false) ? size_format( $size ) : '0'; |
| 112 |
$this->date = get_post_time( get_option( 'date_format' ), false, $this->ID ); |
| 113 |
$this->time = get_post_time( get_option( 'time_format' ), false, $this->ID ); |
| 114 |
$this->author = $attachment->post_author; |
| 115 |
} |
| 116 |
|
| 117 |
/*========================================================================== |
| 118 |
* OUTPUT HTML STRING |
| 119 |
*=========================================================================*/ |
| 120 |
|
| 121 |
/** |
| 122 |
* Returns HTML representing this Document. |
| 123 |
* @filter dg_icon_template Filters the DG icon HTML. Passes a single |
| 124 |
* bool value indicating whether the gallery is using descriptions or not. |
| 125 |
* @return string The gallery HTML. |
| 126 |
*/ |
| 127 |
public function __toString() { |
| 128 |
include_once DG_PATH . 'inc/class-thumber.php'; |
| 129 |
|
| 130 |
$data = 'data-ext="' . $this->extension . '"'; |
| 131 |
$description = ''; |
| 132 |
$target = $this->gallery->openLinkInNewWindow() ? '_blank' : '_self'; |
| 133 |
|
| 134 |
if ( $this->gallery->useFancyThumbs() ) { |
| 135 |
$thumb_obj = DG_Thumb::getThumb( $this->ID ); |
| 136 |
if ( ! is_null( $thumb_obj ) ) { |
| 137 |
if ( $thumb_obj->isSuccess() ) { |
| 138 |
// icon has already been generated so include it in generated gallery |
| 139 |
$thumb = $thumb_obj->getUrl(); |
| 140 |
} |
| 141 |
} else { |
| 142 |
// include a data-* attribute for client side to asynchronously request icon after gallery load |
| 143 |
$data .= ' data-id="' . $this->ID . '"'; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
if ( ! isset( $thumb ) ) { |
| 148 |
$thumb = DG_DefaultThumber::init()->getThumbnail( $this->ID ); |
| 149 |
} |
| 150 |
|
| 151 |
$repl = array( $this->link, $thumb, $this->title_attribute, $this->title, $target, $this->extension, $this->size, $this->path, $data, $this->date, $this->time, $this->author ); |
| 152 |
$find = array( '%link%', '%img%', '%title_attribute%', '%title%', '%target%', '%extension%', '%size%', '%path%', '%data%', '%date%', '%time%', '%author%' ); |
| 153 |
|
| 154 |
// if descriptions then add filterable tag and value to replaced tag |
| 155 |
if ( $this->gallery->useDescriptions() ) { |
| 156 |
$repl[] = $this->description; |
| 157 |
$find[] = '%description%'; |
| 158 |
$description = ' <p>%description%</p>'; |
| 159 |
} |
| 160 |
|
| 161 |
$doc_icon = |
| 162 |
' <div class="document-icon">' . PHP_EOL . |
| 163 |
' <a href="%link%" target="%target%">' . PHP_EOL . |
| 164 |
' <img src="%img%" title="%title_attribute%" alt="%title_attribute%" %data%/>' . PHP_EOL . |
| 165 |
' <span class="title">%title%</span>' . PHP_EOL . |
| 166 |
' </a>' . PHP_EOL . |
| 167 |
' </div>' . PHP_EOL . |
| 168 |
$description; |
| 169 |
|
| 170 |
// allow developers to filter icon output |
| 171 |
$doc_icon = apply_filters( 'dg_icon_template', $doc_icon, $this->gallery->useDescriptions(), $this->ID ); |
| 172 |
|
| 173 |
return str_replace( $find, $repl, $doc_icon ); |
| 174 |
} |
| 175 |
} |