public function ajax_file_field_upload()
 {
     echo '<form action="" method="POST" enctype="multipart/form-data">';
     echo '<input type="file" name="file" class="file-upload" onchange="return window.parent.WPBDP.fileUpload.handleUpload(this);"/>';
     echo '</form>';
     if (isset($_FILES['file']) && $_FILES['file']['error'] == 0) {
         // TODO: we support only images for now but we could use this for anything later
         if ($media_id = wpbdp_media_upload($_FILES['file'], true, true, array(), $errors)) {
             echo '<div class="preview" style="display: none;">';
             echo wp_get_attachment_image($media_id, 'thumb', false);
             echo '</div>';
             echo '<script type="text/javascript">';
             echo sprintf('window.parent.WPBDP.fileUpload.finishUpload(%d, %d);', $_REQUEST['field_id'], $media_id);
             echo '</script>';
         } else {
             print $errors;
         }
     }
     echo sprintf('<script type="text/javascript">window.parent.WPBDP.fileUpload.resizeIFrame(%d);</script>', $_REQUEST['field_id']);
     exit;
 }
 private function upload_image($filename)
 {
     $filepath = $this->images_dir . DIRECTORY_SEPARATOR . $filename;
     if (!$this->images_dir || !file_exists($filepath)) {
         return false;
     }
     // Make a copy of the file because wpbdp_media_upload() moves the original file.
     copy($filepath, $filepath . '.backup');
     $media_id = wpbdp_media_upload($filepath, true, true);
     rename($filepath . '.backup', $filepath);
     return $media_id;
 }