/** * Renders an HTML Dropdown of all the Products (Downloads) * * @access public * @since 1.5 * @param array $args Arguments for the dropdown * @return string $output Product dropdown */ public function product_dropdown($args = array()) { $defaults = array('name' => 'products', 'id' => 'products', 'class' => '', 'multiple' => false, 'selected' => 0, 'chosen' => false, 'number' => 30, 'bundles' => true, 'variations' => false, 'placeholder' => sprintf(__('Choose a %s', 'easy-digital-downloads'), edd_get_label_singular()), 'data' => array('search-type' => 'download')); $args = wp_parse_args($args, $defaults); $product_args = array('post_type' => 'download', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => $args['number']); // Maybe disable bundles if (!$args['bundles']) { $product_args['meta_query'] = array('relation' => 'AND', array('key' => '_edd_product_type', 'value' => 'bundle', 'compare' => 'NOT EXISTS')); } $products = get_posts($product_args); $options = array(); $options[0] = ''; if ($products) { foreach ($products as $product) { $options[absint($product->ID)] = esc_html($product->post_title); if ($args['variations'] && edd_has_variable_prices($product->ID)) { $prices = edd_get_variable_prices($product->ID); foreach ($prices as $key => $value) { $name = !empty($value['name']) ? $value['name'] : ''; $index = !empty($value['index']) ? $value['index'] : $key; if ($name && $index) { $options[absint($product->ID) . '_' . $index] = esc_html($product->post_title . ': ' . $name); } } } } } // This ensures that any selected products are included in the drop down if (is_array($args['selected'])) { foreach ($args['selected'] as $item) { if (!array_key_exists($item, $options)) { $parsed_item = edd_parse_product_dropdown_value($item); if ($parsed_item['price_id'] !== false) { $prices = edd_get_variable_prices((int) $parsed_item['download_id']); foreach ($prices as $key => $value) { $name = isset($value['name']) ? $value['name'] : ''; $index = isset($value['index']) ? $value['index'] : $key; if ($name && $index && (int) $parsed_item['price_id'] === (int) $index) { $options[absint($product->ID) . '_' . $index] = esc_html(get_the_title((int) $parsed_item['download_id']) . ': ' . $name); } } } else { $options[$parsed_item['download_id']] = get_the_title($parsed_item['download_id']); } } } } elseif (false !== $args['selected'] && $args['selected'] !== 0) { if (!array_key_exists($args['selected'], $options)) { $parsed_item = edd_parse_product_dropdown_value($args['selected']); if ($parsed_item['price_id'] !== false) { $prices = edd_get_variable_prices((int) $parsed_item['download_id']); foreach ($prices as $key => $value) { $name = isset($value['name']) ? $value['name'] : ''; $index = isset($value['index']) ? $value['index'] : $key; if ($name && $index && (int) $parsed_item['price_id'] === (int) $index) { $options[absint($product->ID) . '_' . $index] = esc_html(get_the_title((int) $parsed_item['download_id']) . ': ' . $name); } } } else { $options[$parsed_item['download_id']] = get_the_title($parsed_item['download_id']); } } } if (!$args['bundles']) { $args['class'] .= ' no-bundles'; } if ($args['variations']) { $args['class'] .= ' variations'; } $output = $this->select(array('name' => $args['name'], 'selected' => $args['selected'], 'id' => $args['id'], 'class' => $args['class'], 'options' => $options, 'chosen' => $args['chosen'], 'multiple' => $args['multiple'], 'placeholder' => $args['placeholder'], 'show_option_all' => false, 'show_option_none' => false, 'data' => $args['data'])); return $output; }
/** * Allows parsing of the values saved by the product drop down. * * @since 2.6.9 * @param array $values Parse the values from the product dropdown into a readable array * @return array A parsed set of values for download_id and price_id */ function edd_parse_product_dropdown_values($values = array()) { $parsed_values = array(); if (is_array($values)) { foreach ($values as $value) { $value = edd_parse_product_dropdown_value($value); $parsed_values[] = array('download_id' => $value['download_id'], 'price_id' => $value['price_id']); } } else { $value = edd_parse_product_dropdown_value($values); $parsed_values[] = array('download_id' => $value['download_id'], 'price_id' => $value['price_id']); } return $parsed_values; }