Created
November 16, 2012 17:14
-
-
Save c3mdigital/4089097 to your computer and use it in GitHub Desktop.
Two file upload handler functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* File upload ajax handler function to save the file and send image or file html back to the browser | |
* These functions are part of a class | |
*/ | |
function ajax_upload_file() { | |
check_ajax_referer( 'ugc_user_upload', 'nonce' ); | |
$file_data = array( | |
'name' => $_FILES['ugc_attachment_file']['name'], | |
'type' => $_FILES['ugc_attachment_file']['type'], | |
'tmp_name' => $_FILES['ugc_attachment_file']['tmp_name'], | |
'error' => $_FILES['ugc_attachment_file']['error'], | |
'size' => $_FILES['ugc_attachment_file']['size'], | |
); | |
$attachment_id = self::ugc_handle_upload( $file_data ); | |
if ( $attachment_id ) { | |
$html = self::get_front_side_html( $attachment_id, $file_data ); | |
$response = array( | |
'success' => true, | |
'html' => $html, | |
); | |
echo json_encode( $response ); | |
exit; | |
} else { | |
$response = array( | |
'success' => false, | |
'html' => $file_data['error'], | |
); | |
echo json_encode( $response ); | |
exit; | |
} | |
} | |
/** | |
* @param string $attachment_id | |
* @param array $file_data | |
* | |
* @return string | |
*/ | |
function get_front_side_html( $attachment_id = '', $file_data = array() ) { | |
$type = explode( '/', $file_data['type'] ); | |
if ( 'image' == $type[0] ) { | |
$image = wp_get_attachment_image_src( $attachment_id, 'entry-image' ); | |
$url = $image[0]; | |
} | |
elseif ( 'video' == $type[0] ) | |
$url = includes_url( '/images/crystal/video.png' ); | |
elseif ( 'audio' == $type[0] ) | |
$url = includes_url( '/images/crystal/audio.png' ); | |
else | |
$url = includes_url( '/images/crystal/default.png' ); | |
$html = sprintf( '<div class="ugc-item_upload" id="attachment-%d">', $attachment_id ); | |
$html .= sprintf( '<img src="%s" alt="%s" class="attachment-entry-image wp-post-image"/>', esc_url( $url ), esc_attr( $file_data['name'] ) ); | |
$html .= sprintf( '<p><span><strong>File name: </strong> %1$s </span><br><span><strong>File type: </strong>%2$s </span><br><span><strong>File size:</strong> %3$s</span></p>', esc_html( $file_data['name']), esc_html( $file_data['type']), esc_html( self::format_bytes( $file_data['size'] ) ) ); | |
$html .= sprintf( '<input type="hidden" name="ugc_featured_img" value="%s" />', $attachment_id ); | |
$html .= '</div>'; | |
return $html; | |
} | |
/** | |
* This just inserts the attachment and forces use of a temp directory | |
* @param array $file_data array extracted from $_FILES on upload | |
* @param int $parent The post id to attach file to | |
* @return mixed false on error or the attachment ID on success | |
*/ | |
function ugc_handle_upload( $file_data, $parent = 0 ) { | |
$filter = new UGC_Attachment(); | |
add_filter( 'upload_dir', array( $filter, 'upload_dir_filter' ) ); | |
$file = wp_handle_upload( $file_data, array( 'test_form' => false ) ); | |
if ( isset($file['file'] ) ) { | |
$location = $file['file']; //File directory location | |
$filename = basename( $file['file']); | |
$filetype = wp_check_filetype( $filename ); | |
$attachment = array( | |
'post_mime_type' => $filetype['type'], | |
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ), | |
'post_content' => '', | |
'post_status' => 'inherit', | |
); | |
$attachment_id = wp_insert_attachment( $attachment, $location, $parent ); | |
require_once( ABSPATH . 'wp-admin/includes/image.php' ); | |
$file_meta = wp_generate_attachment_metadata( $attachment_id, $location ); | |
wp_update_attachment_metadata( $attachment_id, $file_meta ); | |
remove_filter( 'upload_dir', array( $filter, 'upload_dir_filter' ) ); | |
return $attachment_id; | |
} | |
remove_filter( 'upload_dir', array( $filter, 'upload_dir_filter' ) ); | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment