forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ForceDownload.class.php
67 lines (60 loc) · 1.74 KB
/
ForceDownload.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Contains ForceDownload class definition
*/
/**
* Force an attachment to be downloaded
* @author etessore
* @version 1.0.0
* @package classes
*/
class ForceDownload{
/**
* Enables the force to download feature
* @param bool $priv
* @param bool $nopriv
*/
public function __construct($priv=true, $nopriv=true){
if($priv) add_action('wp_ajax_download', array(&$this, 'force_download'));
if($nopriv) add_action('wp_ajax_nopriv_download', array(&$this, 'force_download'));
}
/**
* AJAX callback for forcing the download of a file
*/
public static function force_download(){
if(!self::check_request()){
wp_die('Unauthorized Resource');
}
$file = get_attached_file($_REQUEST['id']);
if(file_exists($file)){
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename= " . basename($file));
header("Content-Transfer-Encoding: binary");
die(file_get_contents($file));
} else {
header('HTTP/1.0 404 Not Found');
}
}
/**
* Tests if the $_REQUEST[] field is valid
* Overload this to insert more checks
*/
public static function check_request(){
if(!is_numeric($_REQUEST['id'])){
return false;
}
return true;
}
/**
* Calculates the url for the attachment with id $id
* @param int $id the attachment id
* @param string $label the text inside the <a> tag
* @param array $parms additional parameters for the <a> tag
* @see HtmlHelper::anchor()
* @return string an <a> tag
*/
public function force_download_anchor($id, $label, $parms){
return HtmlHelper::anchor(admin_url('admin-ajax.php').'?action=download&id='.$id, $label, $parms);
}
}