Example #1
0
 /**
  * Handle an Image upload for the background image.
  *
  * @since 0.0.1
  */
 public function handle_upload()
 {
     if (empty($_FILES)) {
         return;
     }
     check_admin_referer('custom-background-upload', '_hqnonce-custom-background-upload');
     $overrides = array('test_form' => false);
     $uploaded_file = $_FILES['import'];
     $hq_filetype = hq_check_filetype_and_ext($uploaded_file['tmp_name'], $uploaded_file['name']);
     if (!hq_match_mime_types('image', $hq_filetype['type'])) {
         hq_die(__('The uploaded file is not a valid image. Please try again.'));
     }
     $file = hq_handle_upload($uploaded_file, $overrides);
     if (isset($file['error'])) {
         hq_die($file['error']);
     }
     $url = $file['url'];
     $type = $file['type'];
     $file = $file['file'];
     $filename = basename($file);
     // Construct the object array
     $object = array('post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url, 'context' => 'custom-background');
     // Save the data
     $id = hq_insert_attachment($object, $file);
     // Add the meta-data
     hq_update_attachment_metadata($id, hq_generate_attachment_metadata($id, $file));
     update_post_meta($id, '_hq_attachment_is_custom_background', get_option('stylesheet'));
     set_theme_mod('background_image', esc_url_raw($url));
     $thumbnail = hq_get_attachment_image_src($id, 'thumbnail');
     set_theme_mod('background_image_thumb', esc_url_raw($thumbnail[0]));
     /** This action is documented in hq-admin/custom-header.php */
     do_action('hq_create_file_in_uploads', $file, $id);
     // For replication
     $this->updated = true;
 }
Example #2
0
/**
 * Handle importer uploading and add attachment.
 *
 * @since 0.0.1
 *
 * @return array Uploaded file's details on success, error message on failure
 */
function hq_import_handle_upload()
{
    if (!isset($_FILES['import'])) {
        return array('error' => __('File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.'));
    }
    $overrides = array('test_form' => false, 'test_type' => false);
    $_FILES['import']['name'] .= '.txt';
    $upload = hq_handle_upload($_FILES['import'], $overrides);
    if (isset($upload['error'])) {
        return $upload;
    }
    // Construct the object array
    $object = array('post_title' => basename($upload['file']), 'post_content' => $upload['url'], 'post_mime_type' => $upload['type'], 'guid' => $upload['url'], 'context' => 'import', 'post_status' => 'private');
    // Save the data
    $id = hq_insert_attachment($object, $upload['file']);
    /*
     * Schedule a cleanup for one day from now in case of failed
     * import or missing hq_import_cleanup() call.
     */
    hq_schedule_single_event(time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array($id));
    return array('file' => $upload['file'], 'id' => $id);
}
Example #3
0
 /**
  * Upload the file to be cropped in the second step.
  *
  * @since 0.0.1
  */
 public function step_2_manage_upload()
 {
     $overrides = array('test_form' => false);
     $uploaded_file = $_FILES['import'];
     $hq_filetype = hq_check_filetype_and_ext($uploaded_file['tmp_name'], $uploaded_file['name']);
     if (!hq_match_mime_types('image', $hq_filetype['type'])) {
         hq_die(__('The uploaded file is not a valid image. Please try again.'));
     }
     $file = hq_handle_upload($uploaded_file, $overrides);
     if (isset($file['error'])) {
         hq_die($file['error'], __('Image Upload Error'));
     }
     $url = $file['url'];
     $type = $file['type'];
     $file = $file['file'];
     $filename = basename($file);
     // Construct the object array
     $object = array('post_title' => $filename, 'post_content' => $url, 'post_mime_type' => $type, 'guid' => $url, 'context' => 'custom-header');
     // Save the data
     $attachment_id = hq_insert_attachment($object, $file);
     return compact('attachment_id', 'file', 'filename', 'url', 'type');
 }