wordpress/wp-content/themes/basic/themify/themify-wpajax.php
/**
 * Download an image from external URL and returns the file
 *
 * @param $post_id Attachments may be associated with a parent post or page.
 *
 * @return WP_Error|int ID of created attachment, or WP_Error
 */
Download an image from external URL and returns the file
@param $post_id Attachments may be associated with a parent post or page.

@return WP_Error|int ID of created attachment, or WP_Error
Download an image from external URL and returns the file @param $post_id Attachments may be associated with a parent post or page . @return WP_Error|int ID of created attachment or WP_Error
function tf_fetch_remote_file( $url, $post_id = null ) {
	$file_name = basename( $url );
	$upload = wp_upload_bits( $file_name, 0, '' );
	if ( $upload['error'] )
		return new WP_Error( 'upload_dir_error', $upload['error'] );
	$remote_response = wp_safe_remote_get( $url, array(
		'timeout' => 300,
		'stream' => true,
		'filename' => $upload['file'],
	) );
	$headers = wp_remote_retrieve_headers( $remote_response );
	if ( ! $headers ) {
		@unlink( $upload['file'] );
		return new WP_Error( 'import_file_error', __('Remote server did not respond', 'themify') );
	}
	$remote_response_code = wp_remote_retrieve_response_code( $remote_response );
	if ( $remote_response_code != '200' ) {
		@unlink( $upload['file'] );
		return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'themify'), esc_html( $remote_response_code ), get_status_header_desc( $remote_response_code ) ) );
	}
	$filesize = filesize( $upload['file'] );
	if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
	}
	if ( 0 == $filesize ) {
		@unlink( $upload['file'] );
		return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'themify') );
	}
	$post = array(
		'post_title' => '',
		'post_content' => '',
		'post_status' => 'inherit',
	);
	if ( $info = wp_check_filetype( $upload['file'] ) )
		$post['post_mime_type'] = $info['type'];
	else
		return new WP_Error( 'mime_type_error', __('Invalid file type', 'themify') );
	$attach_id = wp_insert_attachment( $post, $upload['file'], $post_id );
	wp_update_attachment_metadata( $attach_id, wp_generate_attachment_metadata( $attach_id, $upload['file'] ) );
	return $attach_id;
}
function tf_fetch_remote_file ( $ url , $ post_id = null ) { $ file_name = basename ( $ url ) ; $ upload = wp_upload_bits ( $ file_name , 0 , '' ) ; if ( $ upload [ 'error' ] ) return new WP_Error ( 'upload_dir_error' , $ upload [ 'error' ] ) ; $ remote_response = wp_safe_remote_get ( $ url , array ( 'timeout' => 300 , 'stream' => true , 'filename' => $ upload [ 'file' ] , ) ) ; $ headers = wp_remote_retrieve_headers ( $ remote_response ) ; if ( ! $ headers ) { @ unlink ( $ upload [ 'file' ] ) ; return new WP_Error ( 'import_file_error' , __ ( 'Remote server did not respond' , 'themify' ) ) ; } $ remote_response_code = wp_remote_retrieve_response_code ( $ remote_response ) ; if ( $ remote_response_code != '200' ) { @ unlink ( $ upload [ 'file' ] ) ; return new WP_Error ( 'import_file_error' , sprintf ( __ ( 'Remote server returned error response %1$d %2$s' , 'themify' ) , esc_html ( $ remote_response_code ) , get_status_header_desc ( $ remote_response_code ) ) ) ; } $ filesize = filesize ( $ upload [ 'file' ] ) ; if ( isset ( $ headers [ 'content-length' ] ) && $ filesize != $ headers [ 'content-length' ] ) { } if ( 0 == $ filesize ) { @ unlink ( $ upload [ 'file' ] ) ; return new WP_Error ( 'import_file_error' , __ ( 'Zero size file downloaded' , 'themify' ) ) ; } $ post = array ( 'post_title' => '' , 'post_content' => '' , 'post_status' => 'inherit' , ) ; if ( $ info = wp_check_filetype ( $ upload [ 'file' ] ) ) $ post [ 'post_mime_type' ] = $ info [ 'type' ] ; else return new WP_Error ( 'mime_type_error' , __ ( 'Invalid file type' , 'themify' ) ) ; $ attach_id = wp_insert_attachment ( $ post , $ upload [ 'file' ] , $ post_id ) ; wp_update_attachment_metadata ( $ attach_id , wp_generate_attachment_metadata ( $ attach_id , $ upload [ 'file' ] ) ) ; return $ attach_id ; }
Download an image from external URL and returns the file
 @param $post_id Attachments may be associated with a parent post or page.
Download an image from external URL and returns the file @param $post_id Attachments may be associated with a parent post or page .
// extract the file name and extension from the url // get placeholder file in the upload dir with a unique, sanitized filename // fetch the remote url and write it to the placeholder file // request failed // make sure the fetch was successful /* note: this is intentionally disabled, if gZip is enabled, $headers['content-length'] and $filesize do not necessarily match */