public static function add(array $DATA_ = NULL) { $url = $DATA_['uri']; $name = @$DATA_['name']; $post_id = @$DATA_['post_id']; apply_filters('em_object_get_image_type', 'event', false, isset($this) ? $this : self); //Handle the attachment as a WP Post $attachment = ''; $tmpfname = tempnam(ESS_IO::tmp(), "ESS_IMG_" . $post_id . "_"); $h = fopen($tmpfname, "w"); if ($h && strlen($url) > 5) { fwrite($h, file_get_contents($url)); $image_ = getimagesize($tmpfname); switch ($image_['mime']) { case "image/gif": $mime = "gif"; break; case "image/jpeg": $mime = "jpg"; break; case "image/png": $mime = "png"; break; case "image/bmp": $mime = "bmp"; break; } $file = array('tmp_name' => $tmpfname, 'name' => basename($tmpfname), 'type' => $image_[2], 'mime' => $mime, 'width' => $image_[0], 'height' => $image_[1], 'size' => filesize($tmpfname), 'error' => 0); //var_dump( $file ); if (file_exists($file['tmp_name']) && ESS_Images::image_validator($file)) { require_once ABSPATH . "wp-admin" . '/includes/file.php'; require_once ABSPATH . "wp-admin" . '/includes/image.php'; $attachment = ESS_Images::handle_upload($file); if ($attachment) { //echo "DEBUG: <b>". __CLASS__.":".__LINE__."</b>"; //var_dump( $attachment ); $attachment_id = wp_insert_attachment(array('post_mime_type' => $image_['mime'], 'post_title' => $name, 'post_content' => '', 'post_status' => 'inherit'), $attachment['file'], $post_id); $attachment_metadata = wp_generate_attachment_metadata($attachment_id, $attachment['file']); wp_update_attachment_metadata($attachment_id, $attachment_metadata); update_post_meta($post_id, '_thumbnail_id', $attachment_id); update_post_meta($post_id, '_start_ts', date('U')); update_post_meta($post_id, '_end_ts', intval(date('U') + 60 * 60 * 24 * 365)); @fclose($h); if (file_exists($tmpfname)) { @unlink($tmpfname); } return apply_filters('em_object_image_upload', $attachment_id, $this); } } @fclose($h); if (file_exists($tmpfname)) { @unlink($tmpfname); } } return apply_filters('em_object_image_upload', false, $this); }