/**
 * Get File Icon
 *
 * Return the correct file icon for a file type.
 *
 * @since   1.5
 *
 * @param string $file url/path.
 * @param boolen $url return full icon url.
 * @return string.
 */
function dedo_get_file_icon($file, $url = true)
{
    $ext = dedo_get_file_ext($file);
    switch ($ext) {
        case 'aac':
            $icon = 'aac.png';
            break;
        case 'ai':
            $icon = 'ai.png';
            break;
        case 'aiff':
            $icon = 'aiff.png';
            break;
        case 'avi':
            $icon = 'avi.png';
            break;
        case 'bmp':
            $icon = 'bmp.png';
            break;
        case 'c':
            $icon = 'c.png';
            break;
        case 'cpp':
            $icon = 'cpp.png';
            break;
        case 'css':
            $icon = 'css.png';
            break;
        case 'dat':
            $icon = 'dat.png';
            break;
        case 'dmg':
            $icon = 'dmg.png';
            break;
        case 'doc':
            $icon = 'doc.png';
            break;
        case 'dotx':
            $icon = 'dotx.png';
            break;
        case 'dwg':
            $icon = 'dwg.png';
            break;
        case 'dxf':
            $icon = 'dxf.png';
            break;
        case 'eps':
            $icon = 'eps.png';
            break;
        case 'exe':
            $icon = 'exe.png';
            break;
        case 'flv':
            $icon = 'flv.png';
            break;
        case 'gif':
            $icon = 'gif.png';
            break;
        case 'h':
            $icon = 'h.png';
            break;
        case 'hpp':
            $icon = 'hpp.png';
            break;
        case 'html':
            $icon = 'html.png';
            break;
        case 'ics':
            $icon = 'ics.png';
            break;
        case 'iso':
            $icon = 'iso.png';
            break;
        case 'java':
            $icon = 'java.png';
            break;
        case 'jpg':
            $icon = 'jpg.png';
            break;
        case 'js':
            $icon = 'js.png';
            break;
        case 'key':
            $icon = 'key.png';
            break;
        case 'less':
            $icon = 'less.png';
            break;
        case 'mid':
            $icon = 'mid.png';
            break;
        case 'mp3':
            $icon = 'mp3.png';
            break;
        case 'mp4':
            $icon = 'mp4.png';
            break;
        case 'mpg':
            $icon = 'mpg.png';
            break;
        case 'odf':
            $icon = 'odf.png';
            break;
        case 'ods':
            $icon = 'ods.png';
            break;
        case 'odt':
            $icon = 'odt.png';
            break;
        case 'otp':
            $icon = 'otp.png';
            break;
        case 'ots':
            $icon = 'ots.png';
            break;
        case 'ott':
            $icon = 'ott.png';
            break;
        case 'pdf':
            $icon = 'pdf.png';
            break;
        case 'php':
            $icon = 'php.png';
            break;
        case 'png':
            $icon = 'png.png';
            break;
        case 'ppt':
            $icon = 'ppt.png';
            break;
        case 'psd':
            $icon = 'psd.png';
            break;
        case 'py':
            $icon = 'py.png';
            break;
        case 'qt':
            $icon = 'qt.png';
            break;
        case 'rar':
            $icon = 'rar.png';
            break;
        case 'rb':
            $icon = 'rb.png';
            break;
        case 'rtf':
            $icon = 'rtf.png';
            break;
        case 'sass':
            $icon = 'sass.png';
            break;
        case 'scss':
            $icon = 'scss.png';
            break;
        case 'sql':
            $icon = 'sql.png';
            break;
        case 'tga':
            $icon = 'tga.png';
            break;
        case 'tgz':
            $icon = 'tgz.png';
            break;
        case 'tiff':
            $icon = 'tiff.png';
            break;
        case 'txt':
            $icon = 'txt.png';
            break;
        case 'wav':
            $icon = 'wav.png';
            break;
        case 'xls':
            $icon = 'xls.png';
            break;
        case 'xlsx':
            $icon = 'xlsx.png';
            break;
        case 'xml':
            $icon = 'xml.png';
            break;
        case 'yml':
            $icon = 'yml.png';
            break;
        case 'zip':
            $icon = 'zip.png';
            break;
        default:
            $icon = '_blank.png';
            break;
    }
    if ($url) {
        return DEDO_PLUGIN_URL . 'assets/icons/' . $icon;
    } else {
        return $icon;
    }
}
/**
 * Downloads List Shortcode
 *
 * Displays a list of downloads based on user defined attributes.
 *
 * @since   1.3
 */
function dedo_shortcode_ddownload_list($atts)
{
    global $dedo_options;
    // Attributes
    extract(shortcode_atts(array('limit' => 0, 'orderby' => 'title', 'order' => 'ASC', 'categories' => '', 'tags' => '', 'exclude_categories' => '', 'exclude_tags' => '', 'relation' => 'AND', 'style' => $dedo_options['default_list'], 'cache' => true), $atts, 'ddownload_list'));
    // Default query args
    $query_args = array('post_type' => 'dedo_download', 'post_status' => 'publish');
    // Validate and set limit
    $limit = abs($limit);
    $query_args['posts_per_page'] = 0 === $limit ? -1 : $limit;
    // Validate and set orderby
    if (!in_array(strtolower($orderby), array('title', 'date', 'count', 'filesize', 'random'))) {
        return __('Invalid orderby attribute.', 'delightful-downloads');
    } else {
        switch ($orderby) {
            case 'title':
                $query_args['orderby'] = 'title';
                break;
            case 'date':
                $query_args['orderby'] = 'date';
                break;
            case 'count':
                $query_args['meta_key'] = '_dedo_file_count';
                $query_args['orderby'] = 'meta_value_num';
                break;
            case 'filesize':
                $query_args['meta_key'] = '_dedo_file_size';
                $query_args['orderby'] = 'meta_value_num';
                break;
            case 'random':
                $query_args['orderby'] = 'rand';
                break;
        }
    }
    // Validate and set order
    if (!in_array(strtoupper($order), array('ASC', 'DESC'))) {
        return __('Invalid order attribute.', 'delightful-downloads');
    } else {
        $query_args['order'] = strtoupper($order);
    }
    // Validate relation
    if (!in_array(strtoupper($relation), array('AND', 'OR'))) {
        return __('Invalid relation attribute.', 'delightful-downloads');
    } else {
        $relation = strtoupper($relation);
    }
    // Validate and set categories/tags
    $tax_class = '';
    if (!empty($categories) || !empty($tags) || !empty($exclude_categories) || !empty($exclude_tags)) {
        $query_args['tax_query'] = array('relation' => $relation);
        if (!empty($categories)) {
            $categories_array = explode(',', $categories);
            $categories_array = array_map('trim', $categories_array);
            $query_args['tax_query'][] = array('taxonomy' => 'ddownload_category', 'field' => 'slug', 'terms' => $categories_array);
            // Set taxonomy class
            $tax_class .= ' category-' . implode(' category-', $categories_array);
        }
        if (!empty($tags)) {
            $tags_array = explode(',', $tags);
            $tags_array = array_map('trim', $tags_array);
            $query_args['tax_query'][] = array('taxonomy' => 'ddownload_tag', 'field' => 'slug', 'terms' => $tags_array);
            // Set taxonomy class
            $tax_class .= ' tag-' . implode(' tag-', $tags_array);
        }
        if (!empty($exclude_categories)) {
            $exclude_categories_array = explode(',', $exclude_categories);
            $exclude_categories_array = array_map('trim', $exclude_categories_array);
            $query_args['tax_query'][] = array('taxonomy' => 'ddownload_category', 'field' => 'slug', 'terms' => $exclude_categories_array, 'operator' => 'NOT IN');
        }
        if (!empty($exclude_tags)) {
            $exclude_tags_array = explode(',', $exclude_tags);
            $exclude_tags_array = array_map('trim', $exclude_tags_array);
            $query_args['tax_query'][] = array('taxonomy' => 'ddownload_tag', 'field' => 'slug', 'terms' => $exclude_tags_array, 'operator' => 'NOT IN');
        }
    }
    // Check style against registered styles
    $registered_styles = dedo_get_shortcode_lists();
    if (array_key_exists($style, $registered_styles)) {
        $style_format = $registered_styles[$style]['format'];
    } else {
        return __('Invalid style attribute.', 'delightful-downloads');
    }
    // Supply correct boolean for cache
    $cache = in_array($cache, array('true', 'yes')) ? true : false;
    // First check for cached data
    $key = md5($limit . $orderby . $order . $categories . $tags . $exclude_categories . $exclude_tags . $relation . $style);
    $key = substr('dedo_shortcode_list_' . $key, 0, 45);
    $dedo_cache = new DEDO_Cache($key);
    if (true == $cache && false !== ($cached_data = $dedo_cache->get())) {
        $output = $cached_data;
    } else {
        // Run query
        $downloads_list = new WP_Query($query_args);
        // Begin output
        if ($downloads_list->have_posts()) {
            ob_start();
            echo '<ul class="ddownloads_list' . $tax_class . '">';
            while ($downloads_list->have_posts()) {
                $downloads_list->the_post();
                // Add classes
                $classes = 'id-' . get_the_ID();
                // Download id
                $classes .= ' ext-' . dedo_get_file_ext(get_post_meta(get_the_ID(), '_dedo_file_url', true));
                // File extension
                $new_style_format = str_replace('%class%', $classes, $style_format);
                echo '<li>' . dedo_search_replace_wildcards($new_style_format, get_the_ID()) . '</li>';
                // Reset classes for next itteration
                unset($classes);
                unset($new_style_format);
            }
            echo '</ul>';
            $output = ob_get_clean();
            wp_reset_postdata();
            // Save to cache
            if (true == $cache) {
                $dedo_cache->set($output);
            }
        } else {
            return __('No downloads found.', 'delightful-downloads');
        }
    }
    return apply_filters('dedo_shortcode_ddownload_list', $output);
}
/**
 * Settings Page Actions Import
 *
 * @since  1.5
 */
function dedo_settings_actions_import()
{
    global $dedo_notices;
    // Verfiy nonce
    check_admin_referer('dedo_import_settings', 'dedo_import_settings_nonce');
    // Admins only
    if (!current_user_can('manage_options')) {
        return;
    }
    // Check file is uploaded
    if (isset($_FILES['json_file']) && $_FILES['json_file']['size'] > 0) {
        // Check file extension
        if ('json' !== dedo_get_file_ext($_FILES['json_file']['name'])) {
            $dedo_notices->add('error', __('Invalid settings file.', 'delightful-downloads'));
            return;
        }
        // Import and display success
        $import = json_decode(file_get_contents($_FILES['json_file']['tmp_name']), true);
        update_option('delightful-downloads', $import);
        $dedo_notices->add('updated', __('Settings have been successfully imported.', 'delightful-downloads'));
        // Redirect page to remove action from URL
        wp_redirect(admin_url('edit.php?post_type=dedo_download&page=dedo_settings'));
        exit;
    } else {
        $dedo_notices->add('error', __('No file uploaded.', 'delightful-downloads'));
        return;
    }
}