From 20e757483c6b68fbfe2ba2f2bea576747c47c78b Mon Sep 17 00:00:00 2001 From: EarthlingDavey <15802017+EarthlingDavey@users.noreply.github.com> Date: Tue, 22 Oct 2024 09:39:22 +0100 Subject: [PATCH] Update `getFormattedFilesize` to use post_meta even for documents --- public/app/themes/justice/inc/content.php | 29 ++++++++++--------- .../justice/inc/documents/documents.php | 15 +++++----- public/app/themes/justice/inc/templates.php | 6 +--- 3 files changed, 23 insertions(+), 27 deletions(-) diff --git a/public/app/themes/justice/inc/content.php b/public/app/themes/justice/inc/content.php index 871cc6ef..9fa2086a 100644 --- a/public/app/themes/justice/inc/content.php +++ b/public/app/themes/justice/inc/content.php @@ -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); } diff --git a/public/app/themes/justice/inc/documents/documents.php b/public/app/themes/justice/inc/documents/documents.php index 104af753..1d097088 100644 --- a/public/app/themes/justice/inc/documents/documents.php +++ b/public/app/themes/justice/inc/documents/documents.php @@ -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. @@ -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; } /** diff --git a/public/app/themes/justice/inc/templates.php b/public/app/themes/justice/inc/templates.php index 6227d078..842d2b10 100644 --- a/public/app/themes/justice/inc/templates.php +++ b/public/app/themes/justice/inc/templates.php @@ -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);