|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* |
|
4
|
|
|
* Class JobHandlerFile |
|
5
|
|
|
* |
|
6
|
|
|
* Base class for common implementations of JobHandlerInterface, but ones |
|
7
|
|
|
* which write to a temporary file |
|
8
|
|
|
* |
|
9
|
|
|
* @package Event Espresso |
|
10
|
|
|
* @subpackage batch |
|
11
|
|
|
* @author Mike Nelson |
|
12
|
|
|
* @since 4.8.26 |
|
13
|
|
|
* |
|
14
|
|
|
*/ |
|
15
|
|
|
namespace EventEspressoBatchRequest\JobHandlerBaseClasses; |
|
16
|
|
|
|
|
17
|
|
|
use EventEspressoBatchRequest\Helpers\BatchRequestException; |
|
18
|
|
|
|
|
19
|
|
|
if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
|
20
|
|
|
exit( 'No direct script access allowed' ); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
abstract class JobHandlerFile extends JobHandler { |
|
26
|
|
|
|
|
27
|
|
|
const temp_folder_name = 'batch_temp_folder'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* |
|
31
|
|
|
* @var \EEHI_File |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $_file_helper = null; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* JobHandlerFile constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \EEHI_File|null $file_helper |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( \EEHI_File $file_helper = null ) { |
|
43
|
|
|
if( ! $file_helper ) { |
|
44
|
|
|
$this->_file_helper = new \EEH_File(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Creates a file |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $job_id |
|
54
|
|
|
* @param string $filename |
|
55
|
|
|
* @param string $filetype |
|
56
|
|
|
* @return string |
|
57
|
|
|
* @throws \EventEspressoBatchRequest\Helpers\BatchRequestException |
|
58
|
|
|
*/ |
|
59
|
|
|
public function create_file_from_job_with_name( $job_id, $filename, $filetype = 'application/ms-excel' ) { |
|
60
|
|
|
$filepath = ''; |
|
61
|
|
|
try{ |
|
62
|
|
|
$base_folder = $this->get_base_folder(); |
|
63
|
|
|
$success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
|
64
|
|
|
$base_folder . JobHandlerFile::temp_folder_name |
|
65
|
|
|
); |
|
66
|
|
|
if ( $success ) { |
|
67
|
|
|
$success = $this->_file_helper->ensure_folder_exists_and_is_writable( |
|
68
|
|
|
$base_folder . JobHandlerFile::temp_folder_name . DS . $job_id |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
if( $success ) { |
|
72
|
|
|
$filepath = $base_folder . JobHandlerFile::temp_folder_name . DS . $job_id . DS. $filename; |
|
73
|
|
|
$success = $this->_file_helper->ensure_file_exists_and_is_writable( $filepath ); |
|
74
|
|
|
} |
|
75
|
|
|
//let's add the .htaccess file so safari will open the file properly |
|
76
|
|
|
if( $success ) { |
|
77
|
|
|
$extension = \EEH_File::get_file_extension( $filepath ); |
|
78
|
|
|
\EEH_File::write_to_file( |
|
79
|
|
|
$base_folder . JobHandlerFile::temp_folder_name . DS . $job_id . DS . '.htaccess', |
|
80
|
|
|
'AddType ' . $filetype . ' ' . $extension, |
|
81
|
|
|
'.htaccess' |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
//those methods normally fail with an exception, but if not, let's do it |
|
85
|
|
|
if( ! $success ) { |
|
86
|
|
|
throw new \EE_Error( __( 'Could not create temporary file, an unknown error occurred', 'event_espresso' ) ); |
|
87
|
|
|
} |
|
88
|
|
|
} catch( \EE_Error $e ) { |
|
89
|
|
|
throw new BatchRequestException( |
|
90
|
|
|
sprintf( |
|
91
|
|
|
__( 'Could not create temporary file for job %1$s, because: %2$s ', 'event_espresso' ), |
|
92
|
|
|
$job_id, |
|
93
|
|
|
$e->getMessage() |
|
94
|
|
|
), |
|
95
|
|
|
500, |
|
96
|
|
|
$e |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
return $filepath; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Gets the URL to download the file |
|
104
|
|
|
* @param string $filepath |
|
105
|
|
|
* @return string url to file |
|
106
|
|
|
*/ |
|
107
|
|
|
public function get_url_to_file( $filepath ) { |
|
108
|
|
|
return str_replace( $this->get_base_folder(), $this->get_base_url(), $filepath ); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Gets the folder which will contain the "batch_temp_folder" |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
|
|
public function get_base_folder() { |
|
116
|
|
|
return apply_filters( |
|
117
|
|
|
'FHEE__EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile__get_base_folder', |
|
118
|
|
|
EVENT_ESPRESSO_UPLOAD_DIR |
|
119
|
|
|
); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function get_base_url() { |
|
123
|
|
|
return apply_filters( |
|
124
|
|
|
'FHEE__EventEspressoBatchRequest\JobHandlerBaseClasses\JobHandlerFile__get_base_url', |
|
125
|
|
|
EVENT_ESPRESSO_UPLOAD_URL |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|