检查域名后创建原始下载链接

问题描述 投票:0回答:1

我已将这样的代码放在

functions.php
文件中作为我网站的下载链接:

// Define the function to generate the shortcode
 function generate_download_shortcode( $atts ) {

  // Get the file path from the shortcode attributes
  $file_path = $atts['file'];

  // Get the upload directory information
  $upload_dir = wp_upload_dir();

  // Get the base URL of the upload directory
  $base_url = $upload_dir['baseurl'];

  // Get the filename without extension
  $filename = basename($file_path, '.' . pathinfo($file_path)['extension']);

  // URL encode the filename
  $encoded_filename = urlencode($filename);

  // Combine base URL, index.php, and encoded filename
  $download_link = $base_url . '/index.php/' . $encoded_filename . '.' . pathinfo($file_path)['extension'] . '?token=' . md5( time() . rand() ) . '&rate=500kbps';

  return '<a href="' . $download_link . '" class="download-link">Download</a>';
}

// Add the shortcode to WordPress
add_shortcode( 'dow', 'generate_download_shortcode' );

// Add the JavaScript to handle the download link click
add_action( 'wp_footer', 'download_link_script' );

function download_link_script() {
  ?>
  <script>
  // Add event listener to the window load event
  window.addEventListener('load', function() {

  // Get all elements with the "download-link" class
  var downloadLinks = document.getElementsByClassName('download-link');

  // Add click event listener to each download link
  for (var i = 0; i < downloadLinks.length; i++) {
  downloadLinks[i].addEventListener('click', downloadLinkClickHandler);
  }

  // Function to handle download link click
  function downloadLinkClickHandler(event) {

  // Prevent default link action
  event.preventDefault();

  // Get the download link element
  var linkElement = event.target;

  // Start countdown
  var countdown = 3;
  var interval = setInterval(function() {
  linkElement.textContent = 'Download (' + countdown + ')';
  countdown--;
  if (countdown === 0) {
  clearInterval(interval);

  // Update the link element with the original download link without parameters
  linkElement.href = linkElement.href.split('?')[0];
  linkElement.textContent = 'Download';
  }
  }, 1000);

  }

  });
  </script>
  <?php
}

// Add rewrite rule to handle download requests
add_rewrite_rule('^download/([^/]+)/?', 'index.php?page_id=123&file=$matches[1]', 'top');

  
  // Function to handle download requests
  function handle_download_request() {
  
  if ( !isset($_GET['download_file']) ) {
  return; // No download request
  }
  
  $file_path = urldecode($_GET['download_file']);
  
  // Check if user has permission to access the file
  if ( !user_can( 'edit_files' ) && !file_exists($file_path) ) {
  wp_die('You do not have permission to download this file.');
  }
  
  // Generate the download link with parameters
  $download_link = generate_download_link( $file_path );
  
  // Start the download process
  startDownload($file_path);
  
  }
  
  // Add the action to handle download requests
  add_action( 'init', 'handle_download_request' );

此代码将正确应用新链接并将下载速度应用到我想要的下载链接。

使用脚本,我创建了一个条件,如果用户单击站点主地址中的链接,三秒钟后,令牌和速度限制将从链接中删除,并向用户显示主链接。

但是我的代码有两个大问题: 1- 当秒数结束并创建新链接时,必须立即下载文件,但这种情况不会发生。

2- 当再次点击链接时,秒计数器再次开始计数,这是完全错误的。如果用户单击第二次或多次,则应下载文件而不是倒计时。

请帮我解决这个问题。

javascript php wordpress download
1个回答
0
投票

这里有一个自动开始下载文件的解决方案:

如何在单击 HTML 按钮或 JavaScript 时触发文件下载

添加此功能并更新“downloadLinkClickHandler”功能:

// Function to handle download link click
function downloadLinkClickHandler(event) {

  // Get the download link element
  var linkElement = event.target;

  // Check countdown finished
  if (linkElement.classList.contains("countdown_finished")) {
    return; // default event: manual start dowload
  }

  // Prevent default link action
  event.preventDefault();

  // Check countdown working
  if (linkElement.classList.contains("countdown_started")) {
    return; // Wait for countdown finish
  }

  // Start countdown
  var countdown = 3;
  linkElement.classList.add('countdown_started')
  linkElement.textContent = 'Download (' + countdown + ')';
  var interval = setInterval(function() {
    countdown--;
    linkElement.textContent = 'Download (' + countdown + ')';
    if (countdown === 0) {
      clearInterval(interval);

      // Mark as countdown finished
      linkElement.classList.add('countdown_finished')

      // Update the link element with the original download link without parameters
      linkElement.href = linkElement.href.split('?')[0];
      linkElement.textContent = 'Download';

      // Programmatically start downloading file
      download(linkElement.href);
    }
  }, 1000);

}

function download(url) {
  const a = document.createElement('a')
  a.href = url
  a.download = url.split('/').pop()
  document.body.appendChild(a)
  a.click()
  document.body.removeChild(a)
}
© www.soinside.com 2019 - 2024. All rights reserved.