/** * 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); }
/** * Render default list field * * @since 1.3 */ function dedo_settings_default_list_field() { global $dedo_options; $lists = dedo_get_shortcode_lists(); $default_list = $dedo_options['default_list']; echo '<select name="delightful-downloads[default_list]">'; foreach ($lists as $key => $value) { $selected = $default_list == $key ? ' selected="selected"' : ''; echo '<option value="' . $key . '" ' . $selected . '>' . $value['name'] . '</option>'; } echo '</select>'; echo '<p class="description">' . sprintf(__('The default output style, when using the %s shortcode. This can be overridden on a per-list basis.', 'delightful-downloads'), '<code>[ddownload_list]</code>'); }