public function getAudioTitle() { $file = filter_input(INPUT_POST, 'file'); if (strpos($file, '..') !== false) { die(''); } $path = ABSPATH . substr($file, strlen(get_site_url())); if (is_file($path)) { $metadata = wp_read_audio_metadata($path); if ($metadata && isset($metadata['title'])) { die($metadata['title']); } else { $fileParts = explode('/', $file); die(array_pop($fileParts)); } } exit; }
/** * Uses wp_read_video_metadata() and wp_read_audio_metadata() to retrieve * an embedded image to use as a thumbnail. * * @param string $ID The attachment ID to retrieve thumbnail from. * @param int $pg Unused. * * @return bool|string False on failure, URL to thumb on success. */ public function getThumbnail($ID, $pg = 1) { include_once DG_WPADMIN_PATH . 'includes/media.php'; $doc_path = get_attached_file($ID); $mime_type = get_post_mime_type($ID); if (DG_Util::startsWith($mime_type, 'video/')) { $metadata = wp_read_video_metadata($doc_path); } elseif (DG_Util::startsWith($mime_type, 'audio/')) { $metadata = wp_read_audio_metadata($doc_path); } // unsupported mime type || no embedded image present if (!isset($metadata) || empty($metadata['image']['data'])) { return false; } $ext = 'jpg'; switch ($metadata['image']['mime']) { case 'image/gif': $ext = 'gif'; break; case 'image/png': $ext = 'png'; break; } $temp_file = DG_Util::getTempFile($ext); if (!($fp = @fopen($temp_file, 'wb'))) { DG_Logger::writeLog(DG_LogLevel::Error, __('Could not open file: ', 'document-gallery') . $temp_file); return false; } if (!@fwrite($fp, $metadata['image']['data'])) { DG_Logger::writeLog(DG_LogLevel::Error, __('Could not write file: ', 'document-gallery') . $temp_file); fclose($fp); return false; } fclose($fp); return $temp_file; }
/** * This handles the file upload POST itself, creating the attachment post. * * @since 2.5.0 * * @param string $file_id Index into the {@link $_FILES} array of the upload * @param int $post_id The post ID the media is associated with * @param array $post_data allows you to overwrite some of the attachment * @param array $overrides allows you to override the {@link wp_handle_upload()} behavior * @return int|WP_Error ID of the attachment or a WP_Error object on failure. */ function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false)) { $time = current_time('mysql'); if ($post = get_post($post_id)) { if (substr($post->post_date, 0, 4) > 0) { $time = $post->post_date; } } $name = $_FILES[$file_id]['name']; $file = wp_handle_upload($_FILES[$file_id], $overrides, $time); if (isset($file['error'])) { return new WP_Error('upload_error', $file['error']); } $name_parts = pathinfo($name); $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension'])))); $url = $file['url']; $type = $file['type']; $file = $file['file']; $title = $name; $content = ''; if (preg_match('#^audio#', $type)) { $meta = wp_read_audio_metadata($file); if (!empty($meta['title'])) { $title = $meta['title']; } $content = ''; if (!empty($title)) { if (!empty($meta['album']) && !empty($meta['artist'])) { /* translators: 1: audio track title, 2: album title, 3: artist name */ $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']); } else { if (!empty($meta['album'])) { /* translators: 1: audio track title, 2: album title */ $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']); } else { if (!empty($meta['artist'])) { /* translators: 1: audio track title, 2: artist name */ $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']); } else { $content .= sprintf(__('"%s".'), $title); } } } } else { if (!empty($meta['album'])) { if (!empty($meta['artist'])) { /* translators: 1: audio album title, 2: artist name */ $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']); } else { $content .= $meta['album'] . '.'; } } else { if (!empty($meta['artist'])) { $content .= $meta['artist'] . '.'; } } } if (!empty($meta['year'])) { $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']); } if (!empty($meta['track_number'])) { $track_number = explode('/', $meta['track_number']); if (isset($track_number[1])) { $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1])); } else { $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0])); } } if (!empty($meta['genre'])) { $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']); } // Use image exif/iptc data for title and caption defaults if possible. } elseif (0 === strpos($type, 'image/') && ($image_meta = @wp_read_image_metadata($file))) { if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) { $title = $image_meta['title']; } if (trim($image_meta['caption'])) { $content = $image_meta['caption']; } } // Construct the attachment array $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content), $post_data); // This should never be set as it would then overwrite an existing attachment. if (isset($attachment['ID'])) { unset($attachment['ID']); } // Save the data $id = wp_insert_attachment($attachment, $file, $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $file)); } return $id; }
/** * Generate post thumbnail attachment meta data. * * @since 2.1.0 * * @param int $attachment_id Attachment Id to process. * @param string $file Filepath of the Attached image. * @return mixed Metadata for attachment. */ function wp_generate_attachment_metadata($attachment_id, $file) { $attachment = get_post($attachment_id); $metadata = array(); $support = false; if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) { $imagesize = getimagesize($file); $metadata['width'] = $imagesize[0]; $metadata['height'] = $imagesize[1]; // Make the file path relative to the upload dir $metadata['file'] = _wp_relative_upload_path($file); // make thumbnails and other intermediate sizes global $_wp_additional_image_sizes; $sizes = array(); foreach (get_intermediate_image_sizes() as $s) { $sizes[$s] = array('width' => '', 'height' => '', 'crop' => false); if (isset($_wp_additional_image_sizes[$s]['width'])) { $sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']); } else { $sizes[$s]['width'] = get_option("{$s}_size_w"); } // For default sizes set in options if (isset($_wp_additional_image_sizes[$s]['height'])) { $sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']); } else { $sizes[$s]['height'] = get_option("{$s}_size_h"); } // For default sizes set in options if (isset($_wp_additional_image_sizes[$s]['crop'])) { $sizes[$s]['crop'] = intval($_wp_additional_image_sizes[$s]['crop']); } else { $sizes[$s]['crop'] = get_option("{$s}_crop"); } // For default sizes set in options } $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes); if ($sizes) { $editor = wp_get_image_editor($file); if (!is_wp_error($editor)) { $metadata['sizes'] = $editor->multi_resize($sizes); } } else { $metadata['sizes'] = array(); } // fetch additional metadata from exif/iptc $image_meta = wp_read_image_metadata($file); if ($image_meta) { $metadata['image_meta'] = $image_meta; } } elseif (preg_match('#^video/#', get_post_mime_type($attachment))) { $metadata = wp_read_video_metadata($file); $support = current_theme_supports('post-thumbnails', 'attachment:video') && post_type_supports('attachment:video', 'thumbnail'); } elseif (preg_match('#^audio/#', get_post_mime_type($attachment))) { $metadata = wp_read_audio_metadata($file); $support = current_theme_supports('post-thumbnails', 'attachment:audio') && post_type_supports('attachment:audio', 'thumbnail'); } if ($support && !empty($metadata['image']['data'])) { $ext = '.jpg'; switch ($metadata['image']['mime']) { case 'image/gif': $ext = '.gif'; break; case 'image/png': $ext = '.png'; break; } $basename = str_replace('.', '-', basename($file)) . '-image' . $ext; $uploaded = wp_upload_bits($basename, '', $metadata['image']['data']); if (false === $uploaded['error']) { $attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => ''); $sub_attachment_id = wp_insert_attachment($attachment, $uploaded['file']); $attach_data = wp_generate_attachment_metadata($sub_attachment_id, $uploaded['file']); wp_update_attachment_metadata($sub_attachment_id, $attach_data); update_post_meta($attachment_id, '_thumbnail_id', $sub_attachment_id); } } // remove the blob of binary data from the array unset($metadata['image']['data']); return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id); }
/** * Generate meta data for the media * * @since 1.0.0 * * @access private * @param int $attachment_id Media ID to process. * @param string $file Filepath of the Attached image. * * @return mixed Metadata for attachment. */ public function generate_metadata($attachment_id, $file) { $attachment = get_post($attachment_id); $mime_type = get_post_mime_type($attachment); $metadata = array(); if (preg_match('!^image/!', $mime_type) && file_is_displayable_image($file)) { $imagesize = getimagesize($file); $metadata['width'] = $imagesize[0]; $metadata['height'] = $imagesize[1]; // Make the file path relative to the upload dir $metadata['file'] = _wp_relative_upload_path($file); //get the registered media sizes $sizes = mpp_get_media_sizes(); $sizes = apply_filters('mpp_intermediate_image_sizes', $sizes, $attachment_id); if ($sizes) { $editor = wp_get_image_editor($file); if (!is_wp_error($editor)) { $metadata['sizes'] = $editor->multi_resize($sizes); } } else { $metadata['sizes'] = array(); } // fetch additional metadata from exif/iptc $image_meta = wp_read_image_metadata($file); if ($image_meta) { $metadata['image_meta'] = $image_meta; } } elseif (preg_match('#^video/#', $mime_type)) { $metadata = wp_read_video_metadata($file); } elseif (preg_match('#^audio/#', $mime_type)) { $metadata = wp_read_audio_metadata($file); } $dir_path = trailingslashit(dirname($file)) . 'covers'; $url = wp_get_attachment_url($attachment_id); $base_url = str_replace(wp_basename($url), '', $url); //processing for audio/video cover if (!empty($metadata['image']['data'])) { $ext = '.jpg'; switch ($metadata['image']['mime']) { case 'image/gif': $ext = '.gif'; break; case 'image/png': $ext = '.png'; break; } $basename = str_replace('.', '-', basename($file)) . '-image' . $ext; $uploaded = $this->upload_bits($basename, $metadata['image']['data'], array('path' => $dir_path, 'url' => $base_url)); if (false === $uploaded['error']) { $attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => ''); $sub_attachment_id = wp_insert_attachment($attachment, $uploaded['file']); $attach_data = $this->generate_metadata($sub_attachment_id, $uploaded['file']); wp_update_attachment_metadata($sub_attachment_id, $attach_data); //if the option is set to set post thumbnail if (mpp_get_option('set_post_thumbnail')) { mpp_update_media_meta($attachment_id, '_thumbnail_id', $sub_attachment_id); } //set the cover id mpp_update_media_cover_id($attachment_id, $sub_attachment_id); } } // remove the blob of binary data from the array if (isset($metadata['image']['data'])) { unset($metadata['image']['data']); } return apply_filters('mpp_generate_metadata', $metadata, $attachment_id); }
/** * Generate post thumbnail attachment meta data. * * @since 2.1.0 * * @global array $_wp_additional_image_sizes * * @param int $attachment_id Attachment Id to process. * @param string $file Filepath of the Attached image. * @return mixed Metadata for attachment. */ function wp_generate_attachment_metadata($attachment_id, $file) { $attachment = get_post($attachment_id); $metadata = array(); $support = false; if (preg_match('!^image/!', get_post_mime_type($attachment)) && file_is_displayable_image($file)) { $imagesize = getimagesize($file); $metadata['width'] = $imagesize[0]; $metadata['height'] = $imagesize[1]; // Make the file path relative to the upload dir. $metadata['file'] = _wp_relative_upload_path($file); // Make thumbnails and other intermediate sizes. global $_wp_additional_image_sizes; $sizes = array(); foreach (get_intermediate_image_sizes() as $s) { $sizes[$s] = array('width' => '', 'height' => '', 'crop' => false); if (isset($_wp_additional_image_sizes[$s]['width'])) { $sizes[$s]['width'] = intval($_wp_additional_image_sizes[$s]['width']); } else { $sizes[$s]['width'] = get_option("{$s}_size_w"); } // For default sizes set in options if (isset($_wp_additional_image_sizes[$s]['height'])) { $sizes[$s]['height'] = intval($_wp_additional_image_sizes[$s]['height']); } else { $sizes[$s]['height'] = get_option("{$s}_size_h"); } // For default sizes set in options if (isset($_wp_additional_image_sizes[$s]['crop'])) { $sizes[$s]['crop'] = $_wp_additional_image_sizes[$s]['crop']; } else { $sizes[$s]['crop'] = get_option("{$s}_crop"); } // For default sizes set in options } /** * Filter the image sizes automatically generated when uploading an image. * * @since 2.9.0 * @since 4.4.0 Added the `$metadata` argument. * * @param array $sizes An associative array of image sizes. * @param array $metadata An associative array of image metadata: width, height, file. */ $sizes = apply_filters('intermediate_image_sizes_advanced', $sizes, $metadata); if ($sizes) { $editor = wp_get_image_editor($file); if (!is_wp_error($editor)) { $metadata['sizes'] = $editor->multi_resize($sizes); } } else { $metadata['sizes'] = array(); } // Fetch additional metadata from EXIF/IPTC. $image_meta = wp_read_image_metadata($file); if ($image_meta) { $metadata['image_meta'] = $image_meta; } } elseif (wp_attachment_is('video', $attachment)) { $metadata = wp_read_video_metadata($file); $support = current_theme_supports('post-thumbnails', 'attachment:video') || post_type_supports('attachment:video', 'thumbnail'); } elseif (wp_attachment_is('audio', $attachment)) { $metadata = wp_read_audio_metadata($file); $support = current_theme_supports('post-thumbnails', 'attachment:audio') || post_type_supports('attachment:audio', 'thumbnail'); } if ($support && !empty($metadata['image']['data'])) { // Check for existing cover. $hash = md5($metadata['image']['data']); $posts = get_posts(array('fields' => 'ids', 'post_type' => 'attachment', 'post_mime_type' => $metadata['image']['mime'], 'post_status' => 'inherit', 'posts_per_page' => 1, 'meta_key' => '_cover_hash', 'meta_value' => $hash)); $exists = reset($posts); if (!empty($exists)) { update_post_meta($attachment_id, '_thumbnail_id', $exists); } else { $ext = '.jpg'; switch ($metadata['image']['mime']) { case 'image/gif': $ext = '.gif'; break; case 'image/png': $ext = '.png'; break; } $basename = str_replace('.', '-', basename($file)) . '-image' . $ext; $uploaded = wp_upload_bits($basename, '', $metadata['image']['data']); if (false === $uploaded['error']) { $image_attachment = array('post_mime_type' => $metadata['image']['mime'], 'post_type' => 'attachment', 'post_content' => ''); /** * Filter the parameters for the attachment thumbnail creation. * * @since 3.9.0 * * @param array $image_attachment An array of parameters to create the thumbnail. * @param array $metadata Current attachment metadata. * @param array $uploaded An array containing the thumbnail path and url. */ $image_attachment = apply_filters('attachment_thumbnail_args', $image_attachment, $metadata, $uploaded); $sub_attachment_id = wp_insert_attachment($image_attachment, $uploaded['file']); add_post_meta($sub_attachment_id, '_cover_hash', $hash); $attach_data = wp_generate_attachment_metadata($sub_attachment_id, $uploaded['file']); wp_update_attachment_metadata($sub_attachment_id, $attach_data); update_post_meta($attachment_id, '_thumbnail_id', $sub_attachment_id); } } } // Remove the blob of binary data from the array. if ($metadata) { unset($metadata['image']['data']); } /** * Filter the generated attachment meta data. * * @since 2.1.0 * * @param array $metadata An array of attachment meta data. * @param int $attachment_id Current attachment ID. */ return apply_filters('wp_generate_attachment_metadata', $metadata, $attachment_id); }
function cultiv8_sermon_save_audio_enclosure($post_id, $post) { // Stop if no post, auto-save (meta not submitted) or user lacks permission if ('ctc_sermon' != $post->post_type) { return; } $post_type = get_post_type_object($post->post_type); if (empty($_POST) || defined('DOING_AUTOSAVE') && DOING_AUTOSAVE || !current_user_can($post_type->cap->edit_post, $post_id)) { return false; } // Stop if PowerPress plugin is active // Solves conflict regarding enclosure field: http://wordpress.org/support/topic/breaks-blubrry-powerpress-plugin?replies=6 if (defined('POWERPRESS_VERSION')) { return false; } // Get audio URL $audio = get_post_meta($post_id, '_ctc_sermon_audio', true); // The built-in do_enclose method goes a roundabout way of getting the file // length, which involves an http fetch to get the right length. On some server // configurations if the fetch fails the enclosure isn't added. While the fetch // is necessary if the file is remote, it's frustrating if the file is on the // same server, where WP can get all the information without the http fetch. // This method now does the handling of the enclosure data using WP methods if the // file is local and then falls back to the normal do_enclose method if it's // a remote file // Populate enclosure field with URL, length and format, if valid URL found $uploads = wp_upload_dir(); // A local file is assume to be one living in the uploads directory $is_local = stripos($audio, $uploads['baseurl']); if (!(false === $is_local)) { // Get the path to the file $audio_src = str_replace($uploads['baseurl'], $uploads['basedir'], $audio); // Get meta data $metadata = wp_read_audio_metadata($audio_src); if ($metadata) { // Make sure we got metadata and read the mime_type // and filesize values which are needed for the enclosure $mime = $metadata['mime_type']; $length = $metadata['filesize']; if ($mime) { // We've got data, add enclosure meta update_post_meta($post_id, 'enclosure', "{$audio}\n{$length}\n{$mime}\n"); } } } else { // Leave do_enclose for remote files do_enclose($audio, $post_id); } }
function remote_upload_json() { if (!current_user_can('activate_plugins')) { wp_die(__('You do not have sufficient permissions to access this page.')); } if (isset($_POST['file_urls']) && $_POST['file_urls'] != null) { $nonce = $_REQUEST['_wpnonce']; if (wp_verify_nonce($nonce, 'remote-upload-nonce')) { $upload_dir = wp_upload_dir(); if (!file_exists($upload_dir['path'])) { mkdir($upload_dir['path'], 0777, true); $file = fopen($upload_dir['path'] . "/remote_upload_progress.json", "w"); $json_data = array('percentage' => '0'); fwrite($file, json_encode($json_data)); fclose($file); chmod($file, 0777); } $file = fopen($upload_dir['path'] . "/remote_upload_progress.json", "w"); $json_data = array('percentage' => '0', 'total_url_count' => '0', 'url_count' => '0'); fwrite($file, json_encode($json_data)); fclose($file); $file_urls = $_POST['file_urls']; //esc_sql is checking induvidually. It is storing to database (jQuery) GET method. $file_json_url = admin_url('options-general.php?page=remote-upload&json_url=true'); $post_file_url = admin_url('/options-general.php?page=remote-upload&post_file=true'); wp_enqueue_script('upload_js_library', plugins_url('js/upload.js', __FILE__), array('jquery'), '1.0.0'); $upload_dir = wp_upload_dir(); $data = array('file_json_url' => $file_json_url, 'post_file_url' => $post_file_url, 'file_urls' => $file_urls, 'json_data' => $upload_dir['url'] . "/remote_upload_progress.json"); wp_localize_script('upload_js_library', 'php_vars', $data); } } if (isset($_GET['post_file']) && $_GET['post_file'] == 'true') { function is_session_started_for_upload() { if (php_sapi_name() !== 'cli') { if (version_compare(phpversion(), '5.4.0', '>=')) { return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE; } else { return session_id() === '' ? FALSE : TRUE; } } return FALSE; } if (is_session_started_for_upload() === FALSE) { session_start(); } /* Get file total size */ $file_urls = trim($_POST['file_urls']); $file_urls = explode("\r\n", $file_urls); $file_urls = array_filter($file_urls, 'trim'); //$file_urls = array_values(array_filter(explode ("\r\n", $file_urls))); $upload_dir = wp_upload_dir(); $file = fopen($upload_dir['path'] . "/remote_upload_progress.json", "w"); $json_data = array('percentage' => '0', 'total_url_count' => '0', 'url_count' => '0'); fputs($file, json_encode($json_data)); fclose($file); for ($i = 0; $i <= count($file_urls); $i++) { // processing here. $file_url = esc_url(trim($file_urls[$i])); $_SESSION['file_url'] = esc_url(trim($file_urls[$i])); preg_match("/[^\\/]+\$/", $file_url, $matches); $file_name = $matches[0]; $_SESSION['complete'] = false; $_SESSION['percentage'] = 0; $_SESSION['total_url_count'] = count($file_urls); $_SESSION['url_count'] = $i; //Save file set_time_limit(0); //This is the file where we save the information $upload_dir = wp_upload_dir(); $fp = fopen($upload_dir['path'] . "/" . utf8_decode(urldecode($file_name)), 'w+'); //Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ", "%20", $file_url)); curl_setopt($ch, CURLOPT_TIMEOUT, 30000); curl_setopt($ch, CURLOPT_NOPROGRESS, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($source, $download_size, $downloaded, $upload_size, $uploaded) { if ($download_size > 0) { $upload_dir = wp_upload_dir(); $perc = round($downloaded / $download_size * 100); //if($perc != 100 ){ $file = fopen($upload_dir['path'] . "/remote_upload_progress.json", "w"); $json_data = array('percentage' => $perc, 'total_url_count' => $_SESSION['total_url_count'], 'url_count' => $_SESSION['url_count']); fputs($file, json_encode($json_data)); fclose($file); //} if ($perc == 100) { if ($_SESSION['complete'] == false) { $_SESSION['complete'] = true; //here database } } } //ob_flush(); //flush(); //sleep(1); // just to see effect }); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // get curl response $data = curl_exec($ch); curl_close($ch); fclose($fp); chmod($upload_dir['path'] . "/" . utf8_decode(urldecode($file_name)), 0777); $file_name = utf8_decode(urldecode($file_name)); $json_out = array('post' => 'true'); //wp_send_json($json_out); if ($_SESSION['complete'] == true) { // Create post object $file_url = $_SESSION['file_url']; preg_match("/[^\\/]+\$/", $file_url, $matches); $file_name = $matches[0]; $file_name = utf8_decode(urldecode($file_name)); $post_title = substr($file_name, 0, strrpos($file_name, ".")); $finfo = finfo_open(FILEINFO_MIME_TYPE); $mime_type = finfo_file($finfo, $upload_dir['path'] . "/" . $file_name); finfo_close($finfo); $post_data = array('post_title' => esc_sql($post_title), 'post_name' => esc_sql($post_title), 'post_status' => 'inherit', 'guid' => esc_sql($upload_dir['url'] . "/" . str_replace(" ", "%20", $file_name)), 'post_mime_type' => esc_sql($mime_type), 'post_type' => 'attachment'); // Insert the post into the database $post_id = wp_insert_post($post_data); $file_path = ltrim($upload_dir['subdir'] . "/" . $file_name, '/'); add_post_meta($post_id, '_wp_attached_file', esc_sql($file_path)); if (strpos($mime_type, 'image') !== FALSE) { //generate post_meta for image $data = wp_generate_attachment_metadata($post_id, $upload_dir['path'] . '/' . $file_name); wp_update_attachment_metadata($post_id, $data); } if (strpos($mime_type, 'audio') !== FALSE) { //generate post_meta for audio $data = wp_read_audio_metadata($upload_dir['path'] . '/' . $file_name); wp_update_attachment_metadata($post_id, $data); } if (strpos($mime_type, 'video') !== FALSE) { //generate post_meta for video $data = wp_read_video_metadata($upload_dir['path'] . '/' . $file_name); wp_update_attachment_metadata($post_id, $data); } // $file = fopen($upload_dir['path']."/remote_upload_progress.json","w"); // $json_data = array('percentage' => 'done' ); // fputs($file,json_encode($json_data)); //fclose($file); } } $file = fopen($upload_dir['path'] . "/remote_upload_progress.json", "w"); $json_data = array('percentage' => 'done..!'); fputs($file, json_encode($json_data)); fclose($file); } if (isset($_GET['json_url']) && $_GET['json_url'] == true) { if (isset($_SESSION['percentage'])) { if ($_SESSION['percentage'] == 100) { //$_SESSION['percentage'] = 'done'; } $json_out = array('percentage' => $_SESSION['percentage']); wp_send_json($json_out); } } }
/** * Get duration of audio file * @param string $file File name & path * @return mixed File duration on success, boolean false on failure */ public function get_file_duration($file) { if ($file) { // Include media functions if necessary if (!function_exists('wp_read_audio_metadata')) { require_once ABSPATH . 'wp-admin/includes/media.php'; } // Identify file by root path and not URL (required for getID3 class) $site_root = trailingslashit(ABSPATH); $file = str_replace($this->home_url, $site_root, $file); // Get file data (will only work for local files) $data = wp_read_audio_metadata($file); $duration = false; if ($data) { if (isset($data['length_formatted']) && strlen($data['length_formatted']) > 0) { $duration = $data['length_formatted']; } else { if (isset($data['length']) && strlen($data['length']) > 0) { $duration = gmdate('H:i:s', $data['length']); } } } if ($data) { return apply_filters('ssp_file_duration', $duration, $file); } } return false; }
/** * Save a file submitted from a POST request and create an attachment post for it. * * @since 2.5.0 * * @param string $file_id Index of the {@link $_FILES} array that the file was sent. Required. * @param int $post_id The post ID of a post to attach the media item to. Required, but can * be set to 0, creating a media item that has no relationship to a post. * @param array $post_data Overwrite some of the attachment. Optional. * @param array $overrides Override the {@link wp_handle_upload()} behavior. Optional. * @return int|WP_Error ID of the attachment or a WP_Error object on failure. */ function media_handle_upload($file_id, $post_id, $post_data = array(), $overrides = array('test_form' => false)) { $time = current_time('mysql'); if ($post = get_post($post_id)) { if (substr($post->post_date, 0, 4) > 0) { $time = $post->post_date; } } $name = $_FILES[$file_id]['name']; // IMGUR star $img = $_FILES[$file_id]; $filename = $img['tmp_name']; $clientId = "5565248cd8a5d9c"; $handle = fopen($filename, "r"); $data = fread($handle, filesize($filename)); $pvars = array('image' => base64_encode($data)); $timeout = 30; $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_URL, 'https://api.imgur.com/3/image.json'); curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $clientId)); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars); $out = curl_exec($curl); curl_close($curl); $pms = json_decode($out, true); $file = $pms['data']; // IMGUR top // $file = wp_handle_upload($_FILES[$file_id], $overrides, $time); if (isset($pms['status']) != 200) { return new WP_Error('upload_error', $file['error']); } $name_parts = pathinfo($name); $name = trim(substr($name, 0, -(1 + strlen($name_parts['extension'])))); $url = $file['link']; $type = $file['type']; $file = $file['link']; $title = $name; $content = ''; $excerpt = ''; if (preg_match('#^audio#', $type)) { $meta = wp_read_audio_metadata($file); if (!empty($meta['title'])) { $title = $meta['title']; } if (!empty($title)) { if (!empty($meta['album']) && !empty($meta['artist'])) { /* translators: 1: audio track title, 2: album title, 3: artist name */ $content .= sprintf(__('"%1$s" from %2$s by %3$s.'), $title, $meta['album'], $meta['artist']); } elseif (!empty($meta['album'])) { /* translators: 1: audio track title, 2: album title */ $content .= sprintf(__('"%1$s" from %2$s.'), $title, $meta['album']); } elseif (!empty($meta['artist'])) { /* translators: 1: audio track title, 2: artist name */ $content .= sprintf(__('"%1$s" by %2$s.'), $title, $meta['artist']); } else { $content .= sprintf(__('"%s".'), $title); } } elseif (!empty($meta['album'])) { if (!empty($meta['artist'])) { /* translators: 1: audio album title, 2: artist name */ $content .= sprintf(__('%1$s by %2$s.'), $meta['album'], $meta['artist']); } else { $content .= $meta['album'] . '.'; } } elseif (!empty($meta['artist'])) { $content .= $meta['artist'] . '.'; } if (!empty($meta['year'])) { $content .= ' ' . sprintf(__('Released: %d.'), $meta['year']); } if (!empty($meta['track_number'])) { $track_number = explode('/', $meta['track_number']); if (isset($track_number[1])) { $content .= ' ' . sprintf(__('Track %1$s of %2$s.'), number_format_i18n($track_number[0]), number_format_i18n($track_number[1])); } else { $content .= ' ' . sprintf(__('Track %1$s.'), number_format_i18n($track_number[0])); } } if (!empty($meta['genre'])) { $content .= ' ' . sprintf(__('Genre: %s.'), $meta['genre']); } // Use image exif/iptc data for title and caption defaults if possible. } elseif (0 === strpos($type, 'image/') && ($image_meta = @wp_read_image_metadata($file))) { if (trim($image_meta['title']) && !is_numeric(sanitize_title($image_meta['title']))) { $title = $image_meta['title']; } if (trim($image_meta['caption'])) { $excerpt = $image_meta['caption']; } } // Construct the attachment array $attachment = array_merge(array('post_mime_type' => $type, 'guid' => $url, 'post_parent' => $post_id, 'post_title' => $title, 'post_content' => $content, 'post_excerpt' => $excerpt), $post_data); // This should never be set as it would then overwrite an existing attachment. unset($attachment['ID']); // Save the data $id = wp_insert_attachment($attachment, $file, $post_id); if (!is_wp_error($id)) { wp_update_attachment_metadata($id, $file); } return $id; }