adjusts in media class

This commit is contained in:
Eduardo Humberto 2019-03-28 22:52:22 -03:00 committed by leogermani
parent 4d7082f5cc
commit 9f2ca49088
1 changed files with 11 additions and 17 deletions

View File

@ -7,6 +7,7 @@ namespace Tainacan;
class Media { class Media {
private static $instance = null; private static $instance = null;
private static $file_handle = null;
public static function get_instance() { public static function get_instance() {
if(!isset(self::$instance)) { if(!isset(self::$instance)) {
@ -29,18 +30,13 @@ class Media {
* @return Int|false Attachment ID. False on failure * @return Int|false Attachment ID. False on failure
*/ */
public function insert_attachment_from_url($url, $post_id = null) { public function insert_attachment_from_url($url, $post_id = null) {
if( !class_exists( '\WP_Http' ) ) $filename = $this->save_remote_file($url);
include_once( ABSPATH . WPINC . '/class-http.php' );
// $http = new \WP_Http(); if( !file_exists($filename) ) {
return false;
// $response = $http->request( $url, ['sslverify' => false] ); }
// if( !is_array($response) || !isset($response['response']) || $response['response']['code'] != 200 ) {
// return false;
// }
return $this->insert_attachment_from_blob(file_get_contents($this->save_remote_file($url)), basename($url), $post_id); return $this->insert_attachment_from_blob(fopen($filename,'r'), basename($url), $post_id);
} }
@ -57,7 +53,7 @@ class Media {
return false; return false;
} }
return $this->insert_attachment_from_blob(file_get_contents($filename), basename($filename), $post_id); return $this->insert_attachment_from_blob(fopen($filename,'r'), basename($filename), $post_id);
} }
@ -68,7 +64,6 @@ class Media {
* @return string the file path * @return string the file path
*/ */
public function save_remote_file($url) { public function save_remote_file($url) {
global $GlobalFileHandle;
set_time_limit(0); set_time_limit(0);
@ -76,17 +71,16 @@ class Media {
$filename = $wp_upload_dir['path'] . '/' . basename($url); $filename = $wp_upload_dir['path'] . '/' . basename($url);
# Open the file for writing... # Open the file for writing...
$GlobalFileHandle = fopen($filename, 'w+'); self::$file_handle = fopen($filename, 'w+');
$callback = function ($ch, $str) { $callback = function ($ch, $str) {
global $GlobalFileHandle; $len = fwrite(self::$file_handle, $str);
$len = fwrite($GlobalFileHandle, $str);
return $len; return $len;
}; };
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $GlobalFileHandle); curl_setopt($ch, CURLOPT_FILE, self::$file_handle);
curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
@ -108,7 +102,7 @@ class Media {
curl_close($ch); curl_close($ch);
# Close the file pointer # Close the file pointer
fclose($GlobalFileHandle); fclose(self::$file_handle);
return $filename; return $filename;
} }