Example #1
0
/**
 * Retrieve the icon for a MIME type.
 *
 * @since 2.1.0
 *
 * @param string $mime MIME type
 * @return string|bool
 */
function nxt_mime_type_icon($mime = 0)
{
    if (!is_numeric($mime)) {
        $icon = nxt_cache_get("mime_type_icon_{$mime}");
    }
    if (empty($icon)) {
        $post_id = 0;
        $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 = nxt_ext2type($ext)) {
                        $post_mimes[] = $ext_type;
                    }
                }
                $mime = $post->post_mime_type;
            } else {
                $mime = 0;
            }
        } else {
            $post_mimes[] = $mime;
        }
        $icon_files = nxt_cache_get('icon_files');
        if (!is_array($icon_files)) {
            $icon_dir = apply_filters('icon_dir', ABSPATH . nxtINC . '/images/crystal');
            $icon_dir_uri = apply_filters('icon_dir_uri', includes_url('images/crystal'));
            $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);
                }
            }
            nxt_cache_set('icon_files', $icon_files, 600);
        }
        // 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 = nxt_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)) {
                    nxt_cache_set("mime_type_icon_{$mime}", $icon);
                }
                break;
            }
        }
    }
    return apply_filters('nxt_mime_type_icon', $icon, $mime, $post_id);
    // Last arg is 0 if function pass mime type.
}
Example #2
0
/**
 * {@internal Missing Short Description}}
 *
 * @since 2.5.0
 *
 * @return unknown
 */
function nxt_media_upload_handler()
{
    global $is_iphone;
    $errors = array();
    $id = 0;
    if (isset($_POST['html-upload']) && !empty($_FILES)) {
        check_admin_referer('media-form');
        // Upload File button was clicked
        $id = media_handle_upload('async-upload', $_REQUEST['post_id']);
        unset($_FILES);
        if (is_nxt_error($id)) {
            $errors['upload_error'] = $id;
            $id = false;
        }
    }
    if (!empty($_POST['insertonlybutton'])) {
        $src = $_POST['src'];
        if (!empty($src) && !strpos($src, '://')) {
            $src = "http://{$src}";
        }
        if (isset($_POST['media_type']) && 'image' != $_POST['media_type']) {
            $title = esc_html(stripslashes($_POST['title']));
            if (empty($title)) {
                $title = esc_html(basename($src));
            }
            if ($title && $src) {
                $html = "<a href='" . esc_url($src) . "'>{$title}</a>";
            }
            $type = 'file';
            if (($ext = preg_replace('/^.+?\\.([^.]+)$/', '$1', $src)) && ($ext_type = nxt_ext2type($ext)) && ('audio' == $ext_type || 'video' == $ext_type)) {
                $type = $ext_type;
            }
            $html = apply_filters($type . '_send_to_editor_url', $html, esc_url_raw($src), $title);
        } else {
            $align = '';
            $alt = esc_attr(stripslashes($_POST['alt']));
            if (isset($_POST['align'])) {
                $align = esc_attr(stripslashes($_POST['align']));
                $class = " class='align{$align}'";
            }
            if (!empty($src)) {
                $html = "<img src='" . esc_url($src) . "' alt='{$alt}'{$class} />";
            }
            $html = apply_filters('image_send_to_editor_url', $html, esc_url_raw($src), $alt, $align);
        }
        return media_send_to_editor($html);
    }
    if (!empty($_POST)) {
        $return = media_upload_form_handler();
        if (is_string($return)) {
            return $return;
        }
        if (is_array($return)) {
            $errors = $return;
        }
    }
    if (isset($_POST['save'])) {
        $errors['upload_notice'] = __('Saved.');
        return media_upload_gallery();
    }
    if (isset($_GET['tab']) && $_GET['tab'] == 'type_url') {
        $type = 'image';
        if (isset($_GET['type']) && in_array($_GET['type'], array('video', 'audio', 'file'))) {
            $type = $_GET['type'];
        }
        return nxt_iframe('media_upload_type_url_form', $type, $errors, $id);
    }
    if ($is_iphone) {
        return nxt_iframe('media_upload_type_url_form', 'image', $errors, $id);
    } else {
        return nxt_iframe('media_upload_type_form', 'image', $errors, $id);
    }
}