Код: Выделить всё
if ( ! function_exists( 'wp_create_nonce' ) ) {
require_once( ABSPATH . WPINC . '/pluggable.php' );
}
function generate_download_shortcode( $atts ) {
$file_path = $atts['file'];
$user_id = get_current_user_id();
// File name encryption
$encrypted_filename = base64_encode( $file_path );
// Generate CSRF Token
$nonce = wp_create_nonce( 'download_file_' . $file_path . '_' . $user_id );
// Create code to check the time
$current_time = time();
$last_update_time = get_option( 'last_update_time', 0 );
if ( $current_time - $last_update_time >= 60 ) { // 1 minutes (For Test)
// Update time of the last update
update_option( 'last_update_time', $current_time );
// Create a new link
$download_link = generate_download_link( $file_path, $user_id );
} else {
// Create download link
$download_link = site_url() . '/download/?file=' . $encrypted_filename . '&nonce=' . $nonce;
}
// Create (tag a ) to be placed in the html page
return 'Download'; // The name of the display link in the html page
}
function generate_download_link( $file_path, $user_id ) {
// Create new token
$token = md5( $user_id . time() . rand() );
// Creating a download link with speed limit
$download_link = site_url() . '/download/?file=' . $file_path . '&token=' . $token . '&rate=500kbps';
return $download_link;
}
add_shortcode( 'dow', 'generate_download_shortcode' );
Код: Выделить всё
[dow file="http://localhost/site/wp-content/uploads/2024/03/download_for_test.zip"]
Я хочу сделать это без каких-либо плагинов.
Можно ли в кодах создать режим, чтобы вместо создания такой ссылки:
Код: Выделить всё
http://localhost/site/download/?file=http://localhost/site/wp-content/uploads/2024/03/download_for_test.zip&token=042c59c35ce0b267d8b9b90fec5fba91&rate=500kbps
Код: Выделить всё
http://localhost/site/download/?file=http://localhost/site/wp-content/uploads/2024/03/&token=042c59c35ce0b267d8b9b90fec5fba91&rate=500kbps&download_for_test.zip
Это моя идея, и я не знаю, возможно ли такое или нет. Я просто ищу способ загрузить файл, используя приведенный выше код.
Пожалуйста, помогите мне решить эту проблему.
Подробнее здесь: https://stackoverflow.com/questions/782 ... -wordpress
Мобильная версия