/** * Retrieve the icon for a MIME type. * * @since 0.0.1 * * @param string|int $mime MIME type or attachment ID. * @return string|false Icon, false otherwise. */ function hq_mime_type_icon($mime = 0) { if (!is_numeric($mime)) { $icon = hq_cache_get("mime_type_icon_{$mime}"); } $post_id = 0; if (empty($icon)) { $post_mimes = array(); if (is_numeric($mime)) { $mime = (int) $mime; if ($post = get_post($mime)) { $post_id = (int) $post->ID; $ext = preg_replace('/^.+?\\.([^.]+)$/', '$1', $post->guid); if (!empty($ext)) { $post_mimes[] = $ext; if ($ext_type = hq_ext2type($ext)) { $post_mimes[] = $ext_type; } } $mime = $post->post_mime_type; } else { $mime = 0; } } else { $post_mimes[] = $mime; } $icon_files = hq_cache_get('icon_files'); if (!is_array($icon_files)) { /** * Filter the icon directory path. * * @since 0.0.1 * * @param string $path Icon directory absolute path. */ $icon_dir = apply_filters('icon_dir', ABSPATH . HQINC . '/images/media'); /** * Filter the icon directory URI. * * @since 0.0.1 * * @param string $uri Icon directory URI. */ $icon_dir_uri = apply_filters('icon_dir_uri', includes_url('images/media')); /** * Filter the list of icon directory URIs. * * @since 0.0.1 * * @param array $uris List of icon directory URIs. */ $dirs = apply_filters('icon_dirs', array($icon_dir => $icon_dir_uri)); $icon_files = array(); while ($dirs) { $keys = array_keys($dirs); $dir = array_shift($keys); $uri = array_shift($dirs); if ($dh = opendir($dir)) { while (false !== ($file = readdir($dh))) { $file = basename($file); if (substr($file, 0, 1) == '.') { continue; } if (!in_array(strtolower(substr($file, -4)), array('.png', '.gif', '.jpg'))) { if (is_dir("{$dir}/{$file}")) { $dirs["{$dir}/{$file}"] = "{$uri}/{$file}"; } continue; } $icon_files["{$dir}/{$file}"] = "{$uri}/{$file}"; } closedir($dh); } } hq_cache_add('icon_files', $icon_files, 'default', 600); } $types = array(); // Icon basename - extension = MIME wildcard. foreach ($icon_files as $file => $uri) { $types[preg_replace('/^([^.]*).*$/', '$1', basename($file))] =& $icon_files[$file]; } if (!empty($mime)) { $post_mimes[] = substr($mime, 0, strpos($mime, '/')); $post_mimes[] = substr($mime, strpos($mime, '/') + 1); $post_mimes[] = str_replace('/', '_', $mime); } $matches = hq_match_mime_types(array_keys($types), $post_mimes); $matches['default'] = array('default'); foreach ($matches as $match => $wilds) { if (isset($types[$wilds[0]])) { $icon = $types[$wilds[0]]; if (!is_numeric($mime)) { hq_cache_add("mime_type_icon_{$mime}", $icon); } break; } } } /** * Filter the mime type icon. * * @since 0.0.1 * * @param string $icon Path to the mime type icon. * @param string $mime Mime type. * @param int $post_id Attachment ID. Will equal 0 if the function passed * the mime type. */ return apply_filters('hq_mime_type_icon', $icon, $mime, $post_id); }
/** * 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; }
/** * 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'); }