class-qdr-tss-public.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. add_rewrite_rule(
  96. '^shared-file-download/([a-zA-Z0-9]+)/?$',
  97. 'index.php?pagename=shared-file-download&qdr_file_id=$matches[1]',
  98. 'top'
  99. );
  100. }
  101. /**
  102. * Add query vars
  103. *
  104. * @since 1.0.0
  105. * @param array $vars The array of query vars.
  106. * @return array The modified array of query vars.
  107. */
  108. public function add_query_vars($vars) {
  109. $vars[] = 'qdr_file_id';
  110. return $vars;
  111. }
  112. }