Skip to content

Commit

Permalink
Update getFormattedFilesize to use post_meta even for documents
Browse files Browse the repository at this point in the history
  • Loading branch information
EarthlingDavey committed Oct 22, 2024
1 parent 5e61620 commit 20e7574
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
29 changes: 15 additions & 14 deletions public/app/themes/justice/inc/content.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,23 +77,24 @@ public function getFormattedFilesize(int $postId): string|null
// Init a WP_Document_Revisions class so that we can use document specific functions
$document = new WP_Document_Revisions;

// If this is a document (from wp-document-revisions) get the filesize with filesize()
// If this is a document (from wp-document-revisions) get the attachment ID and set the $postId to that
if ($document->verify_post_type($postId)) {
$post = $document->get_document($postId);
$file = get_attached_file($post->ID);
$filesize = filesize($file);
$attachment = $document->get_document($postId);
if ($attachment?->ID) {
// Update the $postId variable to the document's attachment ID
$postId = $attachment->ID;
}
}

// Otherwise check the db for the saved filesize
if (!$filesize) {
$postMeta = get_post_meta($postId, '_wp_attachment_metadata', true);
// Prefer the original filesize
if (!empty($postMeta['filesize']) && is_int($postMeta['filesize'])) {
$filesize = $postMeta['filesize'];
// But if it's offloaded get the size saved by AS3CF
} else {
$offloadedFilesize = get_post_meta($postId, 'as3cf_filesize_total', true);
$filesize = !empty($offloadedFilesize) && is_int($offloadedFilesize) ? $offloadedFilesize : null;
}
$postMeta = get_post_meta($postId, '_wp_attachment_metadata', true);
// Prefer the original filesize
if (!empty($postMeta['filesize']) && is_int($postMeta['filesize'])) {
$filesize = $postMeta['filesize'];
// But if it's offloaded get the size saved by AS3CF
} else {
$offloadedFilesize = get_post_meta($postId, 'as3cf_filesize_total', true);
$filesize = !empty($offloadedFilesize) && is_int($offloadedFilesize) ? $offloadedFilesize : null;
}
return size_format($filesize);
}
Expand Down
15 changes: 7 additions & 8 deletions public/app/themes/justice/inc/documents/documents.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,21 @@ public function getDocumentBySourcePath(string $source_path): ?WP_Post
* It will first try to get the document by the source path - for legacy documents.
* If that fails, it will try to get the document by the slug.
*
* @param string $url
* @param ?string $url
* @return int|null - Post ID, or 0 on failure.
*/

public function getDocumentIdByUrl(string $url): Int
public function getDocumentIdByUrl(?string $url): Int
{
if (!$url) {
return null;
if (!$url || !is_string($url)) {
return 0;
}

// Get the path from the URL.
$path = parse_url($url, PHP_URL_PATH);

if (!$path) {
return null;
return 0;
}

// Get the document by the source path - for legacy documents.
Expand All @@ -337,10 +337,9 @@ public function getDocumentIdByUrl(string $url): Int
return $document->ID;
}

// Get the slug from the link
$slug = basename(untrailingslashit($url));
$post_id = url_to_postid($path);

return url_to_postid('/documents/' . $slug);
return $this->isDocument($post_id) ? $post_id : 0;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions public/app/themes/justice/inc/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,7 @@ public function getFileDownloadParams(DOMNode $node, $format): array
$href = $node->getAttribute('href');
$label = $node->nodeValue;

// // Get the slug from the link
// $slug = basename(untrailingslashit($href));

// $postId = url_to_postid('/documents/' . $slug);

// Get the document ID from the link
$postId = $this->documents->getDocumentIdByUrl($href);

$label = $label ?? get_the_title($postId);
Expand Down

0 comments on commit 20e7574

Please sign in to comment.