plugin_name = $plugin_name; $this->version = $version; } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { // Only load on download page if (!is_page('shared-file-download')) { return; } wp_enqueue_style( $this->plugin_name . '-public', plugin_dir_url(dirname(__FILE__)) . 'public/css/public.css', array(), $this->version, 'all' ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { // Only load on download page if (!is_page('shared-file-download')) { return; } wp_enqueue_script( $this->plugin_name . '-public', plugin_dir_url(dirname(__FILE__)) . 'public/js/public.js', array('jquery'), $this->version, true ); } /** * Create download page if it doesn't exist. * * @since 1.0.0 */ public function create_download_page() { $download_page = get_page_by_path('shared-file-download'); if (!$download_page) { wp_insert_post(array( 'post_title' => __('Shared File Download', 'qdr-temporary-shared-storage'), 'post_content' => '[qdr_tss_download]', 'post_status' => 'publish', 'post_type' => 'page', 'post_name' => 'shared-file-download' )); } } /** * Add rewrite rules for pretty download URLs * * @since 1.0.0 */ public function add_rewrite_rules() { // 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(); } } /** * Add query vars * * @since 1.0.0 * @param array $vars The array of query vars. * @return array The modified array of query vars. */ public function add_query_vars($vars) { $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'); } } }