diff --git a/public/app/themes/clarity/inc/admin/plugins/wp-document-revisions.php b/public/app/themes/clarity/inc/admin/plugins/wp-document-revisions.php index bfb6be294..6d6752324 100644 --- a/public/app/themes/clarity/inc/admin/plugins/wp-document-revisions.php +++ b/public/app/themes/clarity/inc/admin/plugins/wp-document-revisions.php @@ -20,6 +20,7 @@ class WPDocumentRevisions private $home_url = ''; private $site_url = ''; + private $document_url_regex = ''; private $wp_document_revisions = null; public function __construct() @@ -28,6 +29,9 @@ public function __construct() $this->site_url = get_site_url(); $this->home_url = get_home_url(); + // Set the document URL regex - optional home_url, followed by /documents/. + $this->document_url_regex = '/^(' . preg_quote($this->home_url, '/') . ')?\/documents/'; + // load hooks here, inside WP ecosys... $this->hooks(); } @@ -35,6 +39,8 @@ public function __construct() public function hooks(): void { add_filter('document_permalink', [$this, 'filterPermalink'], 10, 2); + // Filter to remove trailing slash from the document's URL. + add_filter('user_trailingslashit', [$this, 'filterTrailingSlash'], 10, 2); // Filter using gzip, always return false, let nginx handle gzipping where necessary. add_filter('document_serve_use_gzip', '__return_false', null, 2); // Filter to retry missing files. @@ -60,6 +66,29 @@ public function filterPermalink(string $link, null|object|array $document) return str_replace($this->site_url, $this->home_url, $link); } + /** + * Remove trailing slash from the document's URL. + * + * This is required because the site-wide permalink structure is set to include a trailing slash. + * Here, that default is overridden to remove the trailing slash for the document post type. + * + * This affects the URL in various places, e.g. + * - the document's URL on the document edit screen. + * - the URL for document downloads. + * - document URLs accessed with a trailing slash will be redirected to remove it. + * + * @param string $string The URL. + * @return string The filtered URL. + */ + public function filterTrailingSlash($string) + { + if (preg_match($this->document_url_regex, $string)) { + return untrailingslashit($string); + } + + return $string; + } + /** * Extract the date from the file path. * @@ -134,7 +163,7 @@ public function retryFilesNotFound($file, $attachment_id) // Let's replace the date with the one from the attachment's guid. $new_file = str_replace($dates['file'], $dates['guid'], $file); - + // If the new file exists, return it. if (is_file($new_file)) { return $new_file;