Commit 46135888 authored by Mohammad Jangda's avatar Mohammad Jangda
Browse files

Allow remote image URLs for media assets

We can now pass in either a image path or an image URL when sending
media assets to Twitter. Media filesystem access on WP.com is severely
restricted so this helps us work around that limitation.
parent a8ba0cf9
......@@ -326,22 +326,31 @@ class WP_Codebird extends \Codebird\Codebird {
// check for filenames
if ( in_array( $key, $possible_files ) ) {
if (
in_array( strtolower( end( explode( ".", $value ) ) ), $this->_supported_media_files_extensions )
&& file_exists( $value )
$data = false;
if ( filter_var( $value, FILTER_VALIDATE_URL ) ) {
$filename = basename( parse_url( $value, PHP_URL_PATH ) );
if ( $this->is_supported_media_file_extension( $filename ) ) {
$data = getimagesize( $value );
}
} elseif (
file_exists( $value )
&& is_readable( $value )
&& $data = getimagesize( $value )
&& $this->is_supported_media_file_extension( basename( $value ) )
) {
if ( // is it a supported image format?
in_array( $data[2], $this->_supported_media_files )
) {
// try to read the file
$data = file_get_contents( $value );
if ( strlen( $data ) == 0 ) {
continue;
}
$value = $data;
$data = getimagesize( $value );
}
// is it a supported image format?
if ( $data && in_array( $data[2], $this->_supported_media_files ) ) {
// try to read the file
$data = file_get_contents( $value );
if ( strlen( $data ) == 0 ) {
continue;
}
$value = $data;
} else {
continue;
}
}
......@@ -353,4 +362,9 @@ class WP_Codebird extends \Codebird\Codebird {
return $multipart_request;
}
private function is_supported_media_file_extension( $filename ) {
$extension = pathinfo( $filename, PATHINFO_EXTENSION );
return in_array( strtolower( $extension ), $this->_supported_media_files_extensions );
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment