From 4613588810c70d16a400797041d41e19122b9d32 Mon Sep 17 00:00:00 2001 From: Mohammad Jangda <batmoo@gmail.com> Date: Fri, 31 Jan 2014 03:55:50 +0000 Subject: [PATCH] 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. --- class-wp-codebird.php | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/class-wp-codebird.php b/class-wp-codebird.php index 39001c1..02aea42 100644 --- a/class-wp-codebird.php +++ b/class-wp-codebird.php @@ -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 ); + } + } -- GitLab