|
|
@@ -82,7 +82,7 @@ class QDR_TSS_Public {
|
|
|
*
|
|
|
* @since 1.0.0
|
|
|
*/
|
|
|
- public function create_download_page() {
|
|
|
+ public function create_download_page() {
|
|
|
$download_page = get_page_by_path('shared-file-download');
|
|
|
if (!$download_page) {
|
|
|
wp_insert_post(array(
|
|
|
@@ -95,17 +95,37 @@ class QDR_TSS_Public {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ /**
|
|
|
* Add rewrite rules for pretty download URLs
|
|
|
*
|
|
|
* @since 1.0.0
|
|
|
*/
|
|
|
public function add_rewrite_rules() {
|
|
|
- add_rewrite_rule(
|
|
|
- '^shared-file-download/([a-zA-Z0-9]+)/?$',
|
|
|
- 'index.php?pagename=shared-file-download&qdr_file_id=$matches[1]',
|
|
|
- 'top'
|
|
|
- );
|
|
|
+ // Get the page ID of the download page
|
|
|
+ $download_page = get_page_by_path('shared-file-download');
|
|
|
+
|
|
|
+ if ($download_page) {
|
|
|
+ // Add rewrite rule using page ID
|
|
|
+ add_rewrite_rule(
|
|
|
+ '^shared-file-download/([a-zA-Z0-9]+)/?$',
|
|
|
+ 'index.php?page_id=' . $download_page->ID . '&qdr_file_id=$matches[1]',
|
|
|
+ 'top'
|
|
|
+ );
|
|
|
+ } else {
|
|
|
+ // Fallback to pagename
|
|
|
+ add_rewrite_rule(
|
|
|
+ '^shared-file-download/([a-zA-Z0-9]+)/?$',
|
|
|
+ 'index.php?pagename=shared-file-download&qdr_file_id=$matches[1]',
|
|
|
+ 'top'
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ // Force flush if our rule isn't in the rewrite rules
|
|
|
+ global $wp_rewrite;
|
|
|
+ $rules = $wp_rewrite->wp_rewrite_rules();
|
|
|
+ if (!isset($rules['^shared-file-download/([a-zA-Z0-9]+)/?$'])) {
|
|
|
+ flush_rewrite_rules();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -119,4 +139,46 @@ class QDR_TSS_Public {
|
|
|
$vars[] = 'qdr_file_id';
|
|
|
return $vars;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Handle template redirect for download URLs
|
|
|
+ *
|
|
|
+ * @since 1.0.0
|
|
|
+ */
|
|
|
+ public function template_redirect() {
|
|
|
+ global $wp;
|
|
|
+
|
|
|
+ // Check if we're on a URL that matches our pattern
|
|
|
+ if (preg_match('/^shared-file-download\/([a-zA-Z0-9]+)\/?$/', $wp->request, $matches)) {
|
|
|
+ // Get the file ID from the URL
|
|
|
+ $file_id = $matches[1];
|
|
|
+
|
|
|
+ // Find the download page
|
|
|
+ $download_page = get_page_by_path('shared-file-download');
|
|
|
+
|
|
|
+ if ($download_page) {
|
|
|
+ // Set the query var
|
|
|
+ set_query_var('qdr_file_id', $file_id);
|
|
|
+
|
|
|
+ // Redirect to the download page with the file ID
|
|
|
+ $url = add_query_arg('id', $file_id, get_permalink($download_page->ID));
|
|
|
+ wp_redirect($url);
|
|
|
+ exit;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Parse request to handle custom URLs
|
|
|
+ *
|
|
|
+ * @since 1.0.0
|
|
|
+ * @param WP $wp The WordPress environment instance.
|
|
|
+ */
|
|
|
+ public function parse_request($wp) {
|
|
|
+ // Check if the request matches our pattern
|
|
|
+ if (preg_match('/^shared-file-download\/([a-zA-Z0-9]+)\/?$/', $wp->request, $matches)) {
|
|
|
+ $wp->set_query_var('qdr_file_id', $matches[1]);
|
|
|
+ $wp->set_query_var('pagename', 'shared-file-download');
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|