class-qdr-tss-public.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * The public-facing functionality of the plugin.
  4. *
  5. * @package QDR_Temporary_Shared_Storage
  6. * @since 1.0.0
  7. */
  8. class QDR_TSS_Public {
  9. /**
  10. * The ID of this plugin.
  11. *
  12. * @since 1.0.0
  13. * @access private
  14. * @var string $plugin_name The ID of this plugin.
  15. */
  16. private $plugin_name;
  17. /**
  18. * The version of this plugin.
  19. *
  20. * @since 1.0.0
  21. * @access private
  22. * @var string $version The current version of this plugin.
  23. */
  24. private $version;
  25. /**
  26. * Initialize the class and set its properties.
  27. *
  28. * @since 1.0.0
  29. * @param string $plugin_name The name of this plugin.
  30. * @param string $version The version of this plugin.
  31. */
  32. public function __construct($plugin_name, $version) {
  33. $this->plugin_name = $plugin_name;
  34. $this->version = $version;
  35. }
  36. /**
  37. * Register the stylesheets for the public-facing side of the site.
  38. *
  39. * @since 1.0.0
  40. */
  41. public function enqueue_styles() {
  42. // Only load on download page
  43. if (!is_page('shared-file-download')) {
  44. return;
  45. }
  46. wp_enqueue_style(
  47. $this->plugin_name . '-public',
  48. plugin_dir_url(dirname(__FILE__)) . 'public/css/public.css',
  49. array(),
  50. $this->version,
  51. 'all'
  52. );
  53. }
  54. /**
  55. * Register the JavaScript for the public-facing side of the site.
  56. *
  57. * @since 1.0.0
  58. */
  59. public function enqueue_scripts() {
  60. // Only load on download page
  61. if (!is_page('shared-file-download')) {
  62. return;
  63. }
  64. wp_enqueue_script(
  65. $this->plugin_name . '-public',
  66. plugin_dir_url(dirname(__FILE__)) . 'public/js/public.js',
  67. array('jquery'),
  68. $this->version,
  69. true
  70. );
  71. }
  72. /**
  73. * Create download page if it doesn't exist.
  74. *
  75. * @since 1.0.0
  76. */
  77. public function create_download_page() {
  78. $download_page = get_page_by_path('shared-file-download');
  79. if (!$download_page) {
  80. wp_insert_post(array(
  81. 'post_title' => __('Shared File Download', 'qdr-temporary-shared-storage'),
  82. 'post_content' => '[qdr_tss_download]',
  83. 'post_status' => 'publish',
  84. 'post_type' => 'page',
  85. 'post_name' => 'shared-file-download'
  86. ));
  87. }
  88. }
  89. /**
  90. * Add rewrite rules for pretty download URLs
  91. *
  92. * @since 1.0.0
  93. */
  94. public function add_rewrite_rules() {
  95. // Get the page ID of the download page
  96. $download_page = get_page_by_path('shared-file-download');
  97. if ($download_page) {
  98. // Add rewrite rule using page ID
  99. add_rewrite_rule(
  100. '^shared-file-download/([a-zA-Z0-9]+)/?$',
  101. 'index.php?page_id=' . $download_page->ID . '&qdr_file_id=$matches[1]',
  102. 'top'
  103. );
  104. } else {
  105. // Fallback to pagename
  106. add_rewrite_rule(
  107. '^shared-file-download/([a-zA-Z0-9]+)/?$',
  108. 'index.php?pagename=shared-file-download&qdr_file_id=$matches[1]',
  109. 'top'
  110. );
  111. }
  112. // Force flush if our rule isn't in the rewrite rules
  113. global $wp_rewrite;
  114. $rules = $wp_rewrite->wp_rewrite_rules();
  115. if (!isset($rules['^shared-file-download/([a-zA-Z0-9]+)/?$'])) {
  116. flush_rewrite_rules();
  117. }
  118. }
  119. /**
  120. * Add query vars
  121. *
  122. * @since 1.0.0
  123. * @param array $vars The array of query vars.
  124. * @return array The modified array of query vars.
  125. */
  126. public function add_query_vars($vars) {
  127. $vars[] = 'qdr_file_id';
  128. return $vars;
  129. }
  130. /**
  131. * Handle template redirect for download URLs
  132. *
  133. * @since 1.0.0
  134. */
  135. public function template_redirect() {
  136. global $wp;
  137. // Check if we're on a URL that matches our pattern
  138. if (preg_match('/^shared-file-download\/([a-zA-Z0-9]+)\/?$/', $wp->request, $matches)) {
  139. // Get the file ID from the URL
  140. $file_id = $matches[1];
  141. // Find the download page
  142. $download_page = get_page_by_path('shared-file-download');
  143. if ($download_page) {
  144. // Set the query var
  145. set_query_var('qdr_file_id', $file_id);
  146. // Redirect to the download page with the file ID
  147. $url = add_query_arg('id', $file_id, get_permalink($download_page->ID));
  148. wp_redirect($url);
  149. exit;
  150. }
  151. }
  152. }
  153. /**
  154. * Parse request to handle custom URLs
  155. *
  156. * @since 1.0.0
  157. * @param WP $wp The WordPress environment instance.
  158. */
  159. public function parse_request($wp) {
  160. // Check if the request matches our pattern
  161. if (preg_match('/^shared-file-download\/([a-zA-Z0-9]+)\/?$/', $wp->request, $matches)) {
  162. $wp->set_query_var('qdr_file_id', $matches[1]);
  163. $wp->set_query_var('pagename', 'shared-file-download');
  164. }
  165. }
  166. }