| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- /**
- * The public-facing functionality of the plugin.
- *
- * @package QDR_Temporary_Shared_Storage
- * @since 1.0.0
- */
- class QDR_TSS_Public {
- /**
- * The ID of this plugin.
- *
- * @since 1.0.0
- * @access private
- * @var string $plugin_name The ID of this plugin.
- */
- private $plugin_name;
- /**
- * The version of this plugin.
- *
- * @since 1.0.0
- * @access private
- * @var string $version The current version of this plugin.
- */
- private $version;
- /**
- * Initialize the class and set its properties.
- *
- * @since 1.0.0
- * @param string $plugin_name The name of this plugin.
- * @param string $version The version of this plugin.
- */
- public function __construct($plugin_name, $version) {
- $this->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');
- }
- }
- }
|