@@ -14,167 +14,167 @@ |
||
14 | 14 | class EEH_Sideloader extends EEH_Base |
15 | 15 | { |
16 | 16 | |
17 | - private $_upload_to; |
|
18 | - private $_upload_from; |
|
19 | - private $_permissions; |
|
20 | - private $_new_file_name; |
|
21 | - |
|
22 | - |
|
23 | - /** |
|
24 | - * constructor allows the user to set the properties on the sideloader on construct. However, there are also setters for doing so. |
|
25 | - * |
|
26 | - * @access public |
|
27 | - * @param array $init array fo initializing the sideloader if keys match the properties. |
|
28 | - */ |
|
29 | - public function __construct($init = array()) |
|
30 | - { |
|
31 | - $this->_init($init); |
|
32 | - } |
|
33 | - |
|
34 | - |
|
35 | - /** |
|
36 | - * sets the properties for class either to defaults or using incoming initialization array |
|
37 | - * |
|
38 | - * @access private |
|
39 | - * @param array $init array on init (keys match properties others ignored) |
|
40 | - * @return void |
|
41 | - */ |
|
42 | - private function _init($init) |
|
43 | - { |
|
44 | - $defaults = array( |
|
45 | - '_upload_to' => $this->_get_wp_uploads_dir(), |
|
46 | - '_upload_from' => '', |
|
47 | - '_permissions' => 0644, |
|
48 | - '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
|
49 | - ); |
|
50 | - |
|
51 | - $props = array_merge($defaults, $init); |
|
52 | - |
|
53 | - foreach ($props as $key => $val) { |
|
54 | - if (EEH_Class_Tools::has_property($this, $key)) { |
|
55 | - $this->{$key} = $val; |
|
56 | - } |
|
57 | - } |
|
58 | - |
|
59 | - // make sure we include the required wp file for needed functions |
|
60 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
61 | - } |
|
62 | - |
|
63 | - |
|
64 | - // utilities |
|
65 | - private function _get_wp_uploads_dir() |
|
66 | - { |
|
67 | - } |
|
68 | - |
|
69 | - // setters |
|
70 | - public function set_upload_to($upload_to_folder) |
|
71 | - { |
|
72 | - $this->_upload_to = $upload_to_folder; |
|
73 | - } |
|
74 | - public function set_upload_from($upload_from_folder) |
|
75 | - { |
|
76 | - $this->_upload_from_folder = $upload_from_folder; |
|
77 | - } |
|
78 | - public function set_permissions($permissions) |
|
79 | - { |
|
80 | - $this->_permissions = $permissions; |
|
81 | - } |
|
82 | - public function set_new_file_name($new_file_name) |
|
83 | - { |
|
84 | - $this->_new_file_name = $new_file_name; |
|
85 | - } |
|
86 | - |
|
87 | - // getters |
|
88 | - public function get_upload_to() |
|
89 | - { |
|
90 | - return $this->_upload_to; |
|
91 | - } |
|
92 | - public function get_upload_from() |
|
93 | - { |
|
94 | - return $this->_upload_from; |
|
95 | - } |
|
96 | - public function get_permissions() |
|
97 | - { |
|
98 | - return $this->_permissions; |
|
99 | - } |
|
100 | - public function get_new_file_name() |
|
101 | - { |
|
102 | - return $this->_new_file_name; |
|
103 | - } |
|
104 | - |
|
105 | - |
|
106 | - // upload methods |
|
107 | - public function sideload() |
|
108 | - { |
|
109 | - // setup temp dir |
|
110 | - $temp_file = wp_tempnam($this->_upload_from); |
|
111 | - |
|
112 | - if (!$temp_file) { |
|
113 | - EE_Error::add_error( |
|
114 | - esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
|
115 | - __FILE__, __FUNCTION__, __LINE__ |
|
116 | - ); |
|
117 | - return false; |
|
118 | - } |
|
119 | - |
|
120 | - do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
|
121 | - |
|
122 | - $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
|
123 | - |
|
124 | - $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); |
|
125 | - |
|
126 | - if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) { |
|
127 | - unlink($temp_file); |
|
128 | - if (defined('WP_DEBUG') && WP_DEBUG) { |
|
129 | - EE_Error::add_error( |
|
130 | - sprintf( |
|
131 | - esc_html__('Unable to upload the file. Either the path given to upload from is incorrect, or something else happened. Here is the path given: %s', 'event_espresso'), |
|
132 | - $this->_upload_from |
|
133 | - ), |
|
134 | - __FILE__, __FUNCTION__, __LINE__ |
|
135 | - ); |
|
136 | - } |
|
137 | - return false; |
|
138 | - } |
|
139 | - |
|
140 | - // possible md5 check |
|
141 | - $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); |
|
142 | - if ($content_md5) { |
|
143 | - $md5_check = verify_file_md5($temp_file, $content_md5); |
|
144 | - if (is_wp_error($md5_check)) { |
|
145 | - unlink($temp_file); |
|
146 | - EE_Error::add_error($md5_check->get_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
147 | - return false; |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - $file = $temp_file; |
|
152 | - |
|
153 | - // now we have the file, let's get it in the right directory with the right name. |
|
154 | - $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
|
155 | - |
|
156 | - // move file in |
|
157 | - if (false === @ rename($file, $path)) { |
|
158 | - unlink($temp_file); |
|
159 | - EE_Error::add_error( |
|
160 | - sprintf( |
|
161 | - esc_html__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'), |
|
162 | - $path |
|
163 | - ), |
|
164 | - __FILE__, __FUNCTION__, __LINE__ |
|
165 | - ); |
|
166 | - return false; |
|
167 | - } |
|
168 | - |
|
169 | - // set permissions |
|
170 | - $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this); |
|
171 | - chmod($path, $permissions); |
|
172 | - |
|
173 | - // that's it. let's allow for actions after file uploaded. |
|
174 | - do_action('AHEE__EE_Sideloader__sideload_after', $this, $path); |
|
175 | - |
|
176 | - // unlink tempfile |
|
177 | - @unlink($temp_file); |
|
178 | - return true; |
|
179 | - } |
|
17 | + private $_upload_to; |
|
18 | + private $_upload_from; |
|
19 | + private $_permissions; |
|
20 | + private $_new_file_name; |
|
21 | + |
|
22 | + |
|
23 | + /** |
|
24 | + * constructor allows the user to set the properties on the sideloader on construct. However, there are also setters for doing so. |
|
25 | + * |
|
26 | + * @access public |
|
27 | + * @param array $init array fo initializing the sideloader if keys match the properties. |
|
28 | + */ |
|
29 | + public function __construct($init = array()) |
|
30 | + { |
|
31 | + $this->_init($init); |
|
32 | + } |
|
33 | + |
|
34 | + |
|
35 | + /** |
|
36 | + * sets the properties for class either to defaults or using incoming initialization array |
|
37 | + * |
|
38 | + * @access private |
|
39 | + * @param array $init array on init (keys match properties others ignored) |
|
40 | + * @return void |
|
41 | + */ |
|
42 | + private function _init($init) |
|
43 | + { |
|
44 | + $defaults = array( |
|
45 | + '_upload_to' => $this->_get_wp_uploads_dir(), |
|
46 | + '_upload_from' => '', |
|
47 | + '_permissions' => 0644, |
|
48 | + '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
|
49 | + ); |
|
50 | + |
|
51 | + $props = array_merge($defaults, $init); |
|
52 | + |
|
53 | + foreach ($props as $key => $val) { |
|
54 | + if (EEH_Class_Tools::has_property($this, $key)) { |
|
55 | + $this->{$key} = $val; |
|
56 | + } |
|
57 | + } |
|
58 | + |
|
59 | + // make sure we include the required wp file for needed functions |
|
60 | + require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
61 | + } |
|
62 | + |
|
63 | + |
|
64 | + // utilities |
|
65 | + private function _get_wp_uploads_dir() |
|
66 | + { |
|
67 | + } |
|
68 | + |
|
69 | + // setters |
|
70 | + public function set_upload_to($upload_to_folder) |
|
71 | + { |
|
72 | + $this->_upload_to = $upload_to_folder; |
|
73 | + } |
|
74 | + public function set_upload_from($upload_from_folder) |
|
75 | + { |
|
76 | + $this->_upload_from_folder = $upload_from_folder; |
|
77 | + } |
|
78 | + public function set_permissions($permissions) |
|
79 | + { |
|
80 | + $this->_permissions = $permissions; |
|
81 | + } |
|
82 | + public function set_new_file_name($new_file_name) |
|
83 | + { |
|
84 | + $this->_new_file_name = $new_file_name; |
|
85 | + } |
|
86 | + |
|
87 | + // getters |
|
88 | + public function get_upload_to() |
|
89 | + { |
|
90 | + return $this->_upload_to; |
|
91 | + } |
|
92 | + public function get_upload_from() |
|
93 | + { |
|
94 | + return $this->_upload_from; |
|
95 | + } |
|
96 | + public function get_permissions() |
|
97 | + { |
|
98 | + return $this->_permissions; |
|
99 | + } |
|
100 | + public function get_new_file_name() |
|
101 | + { |
|
102 | + return $this->_new_file_name; |
|
103 | + } |
|
104 | + |
|
105 | + |
|
106 | + // upload methods |
|
107 | + public function sideload() |
|
108 | + { |
|
109 | + // setup temp dir |
|
110 | + $temp_file = wp_tempnam($this->_upload_from); |
|
111 | + |
|
112 | + if (!$temp_file) { |
|
113 | + EE_Error::add_error( |
|
114 | + esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
|
115 | + __FILE__, __FUNCTION__, __LINE__ |
|
116 | + ); |
|
117 | + return false; |
|
118 | + } |
|
119 | + |
|
120 | + do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
|
121 | + |
|
122 | + $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
|
123 | + |
|
124 | + $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); |
|
125 | + |
|
126 | + if (is_wp_error($response) || 200 != wp_remote_retrieve_response_code($response)) { |
|
127 | + unlink($temp_file); |
|
128 | + if (defined('WP_DEBUG') && WP_DEBUG) { |
|
129 | + EE_Error::add_error( |
|
130 | + sprintf( |
|
131 | + esc_html__('Unable to upload the file. Either the path given to upload from is incorrect, or something else happened. Here is the path given: %s', 'event_espresso'), |
|
132 | + $this->_upload_from |
|
133 | + ), |
|
134 | + __FILE__, __FUNCTION__, __LINE__ |
|
135 | + ); |
|
136 | + } |
|
137 | + return false; |
|
138 | + } |
|
139 | + |
|
140 | + // possible md5 check |
|
141 | + $content_md5 = wp_remote_retrieve_header($response, 'content-md5'); |
|
142 | + if ($content_md5) { |
|
143 | + $md5_check = verify_file_md5($temp_file, $content_md5); |
|
144 | + if (is_wp_error($md5_check)) { |
|
145 | + unlink($temp_file); |
|
146 | + EE_Error::add_error($md5_check->get_error_message(), __FILE__, __FUNCTION__, __LINE__); |
|
147 | + return false; |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + $file = $temp_file; |
|
152 | + |
|
153 | + // now we have the file, let's get it in the right directory with the right name. |
|
154 | + $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
|
155 | + |
|
156 | + // move file in |
|
157 | + if (false === @ rename($file, $path)) { |
|
158 | + unlink($temp_file); |
|
159 | + EE_Error::add_error( |
|
160 | + sprintf( |
|
161 | + esc_html__('Unable to move the file to new location (possible permissions errors). This is the path the class attempted to move the file to: %s', 'event_espresso'), |
|
162 | + $path |
|
163 | + ), |
|
164 | + __FILE__, __FUNCTION__, __LINE__ |
|
165 | + ); |
|
166 | + return false; |
|
167 | + } |
|
168 | + |
|
169 | + // set permissions |
|
170 | + $permissions = apply_filters('FHEE__EEH_Sideloader__sideload__permissions_applied', $this->_permissions, $this); |
|
171 | + chmod($path, $permissions); |
|
172 | + |
|
173 | + // that's it. let's allow for actions after file uploaded. |
|
174 | + do_action('AHEE__EE_Sideloader__sideload_after', $this, $path); |
|
175 | + |
|
176 | + // unlink tempfile |
|
177 | + @unlink($temp_file); |
|
178 | + return true; |
|
179 | + } |
|
180 | 180 | } //end EEH_Template class |
@@ -45,7 +45,7 @@ discard block |
||
45 | 45 | '_upload_to' => $this->_get_wp_uploads_dir(), |
46 | 46 | '_upload_from' => '', |
47 | 47 | '_permissions' => 0644, |
48 | - '_new_file_name' => 'EE_Sideloader_' . uniqid() . '.default' |
|
48 | + '_new_file_name' => 'EE_Sideloader_'.uniqid().'.default' |
|
49 | 49 | ); |
50 | 50 | |
51 | 51 | $props = array_merge($defaults, $init); |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | } |
58 | 58 | |
59 | 59 | // make sure we include the required wp file for needed functions |
60 | - require_once(ABSPATH . 'wp-admin/includes/file.php'); |
|
60 | + require_once(ABSPATH.'wp-admin/includes/file.php'); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | |
@@ -109,7 +109,7 @@ discard block |
||
109 | 109 | // setup temp dir |
110 | 110 | $temp_file = wp_tempnam($this->_upload_from); |
111 | 111 | |
112 | - if (!$temp_file) { |
|
112 | + if ( ! $temp_file) { |
|
113 | 113 | EE_Error::add_error( |
114 | 114 | esc_html__('Something went wrong with the upload. Unable to create a tmp file for the uploaded file on the server', 'event_espresso'), |
115 | 115 | __FILE__, __FUNCTION__, __LINE__ |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | |
120 | 120 | do_action('AHEE__EEH_Sideloader__sideload__before', $this, $temp_file); |
121 | 121 | |
122 | - $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array( 'timeout' => 500, 'stream' => true, 'filename' => $temp_file ), $this, $temp_file); |
|
122 | + $wp_remote_args = apply_filters('FHEE__EEH_Sideloader__sideload__wp_remote_args', array('timeout' => 500, 'stream' => true, 'filename' => $temp_file), $this, $temp_file); |
|
123 | 123 | |
124 | 124 | $response = wp_safe_remote_get($this->_upload_from, $wp_remote_args); |
125 | 125 | |
@@ -151,7 +151,7 @@ discard block |
||
151 | 151 | $file = $temp_file; |
152 | 152 | |
153 | 153 | // now we have the file, let's get it in the right directory with the right name. |
154 | - $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to . $this->_new_file_name, $this); |
|
154 | + $path = apply_filters('FHEE__EEH_Sideloader__sideload__new_path', $this->_upload_to.$this->_new_file_name, $this); |
|
155 | 155 | |
156 | 156 | // move file in |
157 | 157 | if (false === @ rename($file, $path)) { |