/**
 * Get Discount By Code
 *
 * Retrieves all details for a discount by its code.
 *
 * @param string $code
 *
 * @since       1.0
 * @return      int
 */
function edd_get_discount_by_code($code)
{
    $discounts = edd_get_discounts(array('meta_key' => '_edd_discount_code', 'meta_value' => $code, 'posts_per_page' => 1));
    if ($discounts) {
        return $discounts[0];
    }
    return false;
}
 /**
  * Renders an HTML Dropdown of all the Discounts
  *
  * @access public
  * @since 1.5.2
  * @param string $name Name attribute of the dropdown
  * @param int    $selected Discount to select automatically
  * @param string $status Discount post_status to retrieve
  * @return string $output Discount dropdown
  */
 public function discount_dropdown($name = 'edd_discounts', $selected = 0, $status = '')
 {
     $args = array('nopaging' => true);
     if (!empty($status)) {
         $args['post_status'] = $status;
     }
     $discounts = edd_get_discounts($args);
     $options = array();
     if ($discounts) {
         foreach ($discounts as $discount) {
             $options[absint($discount->ID)] = esc_html(get_the_title($discount->ID));
         }
     } else {
         $options[0] = __('No discounts found', 'edd');
     }
     $output = $this->select(array('name' => $name, 'selected' => $selected, 'options' => $options, 'show_option_all' => false, 'show_option_none' => false));
     return $output;
 }
 /**
  * Renders an HTML Dropdown of all the Discounts
  *
  * @access public
  * @since 1.5.2
  * @param string $name Name attribute of the dropdown
  * @param int    $selected Discount to select automatically
  * @param string $status Discount post_status to retrieve
  * @return string $output Discount dropdown
  */
 public function discount_dropdown($name = 'edd_discounts', $selected = 0, $status = '')
 {
     $args = array('nopaging' => true);
     if (!empty($status)) {
         $args['post_status'] = $status;
     }
     $discounts = edd_get_discounts($args);
     $output = '<select name="' . esc_attr($name) . '" id="' . esc_attr($name) . '">';
     if ($discounts) {
         foreach ($discounts as $discount) {
             $output .= '<option value="' . absint($discount->ID) . '"' . selected($selected, $discount->ID, false) . '>' . esc_html(get_the_title($discount->ID)) . '</option>';
         }
     } else {
         $output .= '<option value="0">' . __('No discounts found', 'edd') . '</option>';
     }
     $output .= '</select>';
     return $output;
 }
/**
 * Discounts short code
 *
 * Displays a list of all the active discounts. The active discounts can be configured
 * from the Discount Codes admin screen.
 *
 * @since 1.0.8.2
 * @param array $atts Shortcode attributes
 * @param string $content
 * @uses edd_get_discounts()
 * @return string $discounts_lists List of all the active discount codes
 */
function edd_discounts_shortcode($atts, $content = null)
{
    $discounts = edd_get_discounts();
    if (!$discounts && edd_has_active_discounts()) {
        return;
    }
    $discounts_list = '<ul id="edd_discounts_list">';
    foreach ($discounts as $discount) {
        if (edd_is_discount_active($discount->ID)) {
            $discounts_list .= '<li class="edd_discount">';
            $discounts_list .= '<span class="edd_discount_name">' . edd_get_discount_code($discount->ID) . '</span>';
            $discounts_list .= '<span class="edd_discount_separator"> - </span>';
            $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)) . '</span>';
            $discounts_list .= '</li>';
        }
    }
    $discounts_list .= '</ul>';
    return $discounts_list;
}
 /**
  * Process Get Discounts API Request
  *
  * @access public
  * @since 1.6
  * @global object $wpdb Used to query the database using the WordPress
  *   Database API
  * @param int $discount Discount ID
  * @return array $discounts Multidimensional array of the discounts
  */
 public function get_discounts($discount = null)
 {
     $discount_list = array();
     if (!user_can($this->user_id, 'manage_shop_discounts') && !$this->override) {
         return $discount_list;
     }
     $error = array();
     if (empty($discount)) {
         global $wpdb;
         $paged = $this->get_paged();
         $per_page = $this->per_page();
         $discounts = edd_get_discounts(array('posts_per_page' => $per_page, 'paged' => $paged));
         $count = 0;
         if (empty($discounts)) {
             $error['error'] = __('No discounts found!', 'easy-digital-downloads');
             return $error;
         }
         foreach ($discounts as $discount) {
             $discount_list['discounts'][$count]['ID'] = $discount->ID;
             $discount_list['discounts'][$count]['name'] = $discount->post_title;
             $discount_list['discounts'][$count]['code'] = edd_get_discount_code($discount->ID);
             $discount_list['discounts'][$count]['amount'] = edd_get_discount_amount($discount->ID);
             $discount_list['discounts'][$count]['min_price'] = edd_get_discount_min_price($discount->ID);
             $discount_list['discounts'][$count]['type'] = edd_get_discount_type($discount->ID);
             $discount_list['discounts'][$count]['uses'] = edd_get_discount_uses($discount->ID);
             $discount_list['discounts'][$count]['max_uses'] = edd_get_discount_max_uses($discount->ID);
             $discount_list['discounts'][$count]['start_date'] = edd_get_discount_start_date($discount->ID);
             $discount_list['discounts'][$count]['exp_date'] = edd_get_discount_expiration($discount->ID);
             $discount_list['discounts'][$count]['status'] = $discount->post_status;
             $discount_list['discounts'][$count]['product_requirements'] = edd_get_discount_product_reqs($discount->ID);
             $discount_list['discounts'][$count]['requirement_condition'] = edd_get_discount_product_condition($discount->ID);
             $discount_list['discounts'][$count]['global_discount'] = edd_is_discount_not_global($discount->ID);
             $discount_list['discounts'][$count]['single_use'] = edd_discount_is_single_use($discount->ID);
             $count++;
         }
     } else {
         if (is_numeric($discount) && get_post($discount)) {
             $discount_list['discounts'][0]['ID'] = $discount;
             $discount_list['discounts'][0]['name'] = get_post_field('post_title', $discount);
             $discount_list['discounts'][0]['code'] = edd_get_discount_code($discount);
             $discount_list['discounts'][0]['amount'] = edd_get_discount_amount($discount);
             $discount_list['discounts'][0]['min_price'] = edd_get_discount_min_price($discount);
             $discount_list['discounts'][0]['type'] = edd_get_discount_type($discount);
             $discount_list['discounts'][0]['uses'] = edd_get_discount_uses($discount);
             $discount_list['discounts'][0]['max_uses'] = edd_get_discount_max_uses($discount);
             $discount_list['discounts'][0]['start_date'] = edd_get_discount_start_date($discount);
             $discount_list['discounts'][0]['exp_date'] = edd_get_discount_expiration($discount);
             $discount_list['discounts'][0]['status'] = get_post_field('post_status', $discount);
             $discount_list['discounts'][0]['product_requirements'] = edd_get_discount_product_reqs($discount);
             $discount_list['discounts'][0]['requirement_condition'] = edd_get_discount_product_condition($discount);
             $discount_list['discounts'][0]['global_discount'] = edd_is_discount_not_global($discount);
             $discount_list['discounts'][0]['single_use'] = edd_discount_is_single_use($discount);
         } else {
             $error['error'] = sprintf(__('Discount %s not found!', 'easy-digital-downloads'), $discount);
             return $error;
         }
     }
     return $discount_list;
 }
/**
 * Discounts short code
 *
 * Displays a list of all the active discounts. The active discounts can be configured
 * from the Discount Codes admin screen.
 *
 * @since 1.0.8.2
 * @param array $atts Shortcode attributes
 * @param string $content
 * @uses edd_get_discounts()
 * @return string $discounts_lists List of all the active discount codes
 */
function edd_discounts_shortcode($atts, $content = null)
{
    $discounts = edd_get_discounts();
    $discounts_list = '<ul id="edd_discounts_list">';
    if (!empty($discounts) && edd_has_active_discounts()) {
        foreach ($discounts as $discount) {
            if (edd_is_discount_active($discount->ID)) {
                $discounts_list .= '<li class="edd_discount">';
                $discounts_list .= '<span class="edd_discount_name">' . edd_get_discount_code($discount->ID) . '</span>';
                $discounts_list .= '<span class="edd_discount_separator"> - </span>';
                $discounts_list .= '<span class="edd_discount_amount">' . edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)) . '</span>';
                $discounts_list .= '</li>';
            }
        }
    } else {
        $discounts_list .= '<li class="edd_discount">' . __('No discounts found', 'easy-digital-downloads') . '</li>';
    }
    $discounts_list .= '</ul>';
    return $discounts_list;
}
/**
 * Retrieve discount by a given field
 *
 * @since       2.0
 * @param       string $field The field to retrieve the discount with
 * @param       mixed $value The value for $field
 * @return      mixed
 */
function edd_get_discount_by($field = '', $value = '')
{
    if (empty($field) || empty($value)) {
        return false;
    }
    if (!is_string($field)) {
        return false;
    }
    switch (strtolower($field)) {
        case 'code':
            $discount = edd_get_discounts(array('meta_key' => '_edd_discount_code', 'meta_value' => $value, 'posts_per_page' => 1, 'post_status' => 'any'));
            if ($discount) {
                $discount = $discount[0];
            }
            break;
        case 'id':
            $discount = edd_get_discount($value);
            break;
        case 'name':
            $discount = get_posts(array('post_type' => 'edd_discount', 'name' => $value, 'posts_per_page' => 1, 'post_status' => 'any'));
            if ($discount) {
                $discount = $discount[0];
            }
            break;
        default:
            return false;
    }
    if (!empty($discount)) {
        return $discount;
    }
    return false;
}
 /**
  * Retrieve all the data for all the discount codes
  *
  * @access public
  * @since 1.4
  * @return array $discount_codes_data Array of all the data for the discount codes
  */
 public function discount_codes_data()
 {
     $discount_codes_data = array();
     $per_page = $this->per_page;
     $orderby = isset($_GET['orderby']) ? $_GET['orderby'] : 'ID';
     $order = isset($_GET['order']) ? $_GET['order'] : 'DESC';
     $status = isset($_GET['status']) ? $_GET['status'] : array('active', 'inactive');
     $meta_key = isset($_GET['meta_key']) ? $_GET['meta_key'] : null;
     $search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : null;
     $discounts = edd_get_discounts(array('posts_per_page' => $per_page, 'paged' => isset($_GET['paged']) ? $_GET['paged'] : 1, 'orderby' => $orderby, 'order' => $order, 'post_status' => $status, 'meta_key' => $meta_key, 's' => $search));
     if ($discounts) {
         foreach ($discounts as $discount) {
             if (edd_get_discount_max_uses($discount->ID)) {
                 $uses = edd_get_discount_uses($discount->ID) . '/' . edd_get_discount_max_uses($discount->ID);
             } else {
                 $uses = edd_get_discount_uses($discount->ID);
             }
             if (edd_get_discount_max_uses($discount->ID)) {
                 $max_uses = edd_get_discount_max_uses($discount->ID) ? edd_get_discount_max_uses($discount->ID) : __('unlimited', 'edd');
             } else {
                 $max_uses = __('Unlimited', 'edd');
             }
             $start_date = edd_get_discount_start_date($discount->ID);
             if (!empty($start_date)) {
                 $discount_start_date = date_i18n(get_option('date_format'), strtotime($start_date));
             } else {
                 $discount_start_date = __('No start date', 'edd');
             }
             if (edd_get_discount_expiration($discount->ID)) {
                 $expiration = date_i18n(get_option('date_format'), strtotime(edd_get_discount_expiration($discount->ID)));
             } else {
                 $expiration = __('No expiration', 'edd');
             }
             $discount_codes_data[] = array('ID' => $discount->ID, 'name' => get_the_title($discount->ID), 'code' => edd_get_discount_code($discount->ID), 'amount' => edd_format_discount_rate(edd_get_discount_type($discount->ID), edd_get_discount_amount($discount->ID)), 'uses' => $uses, 'max_uses' => $max_uses, 'start_date' => $discount_start_date, 'expiration' => $expiration, 'status' => edd_is_discount_expired($discount->ID) ? 'expired' : $discount->post_status);
         }
     }
     return $discount_codes_data;
 }
/**
 * Updates discounts that are expired or at max use (that are not already marked as so) as inactive or expired
 *
 * @since 2.6
 * @return void
*/
function edd_discount_status_cleanup()
{
    global $wpdb;
    // We only want to get 25 active discounts to check their status per step here
    $cron_discount_number = apply_filters('edd_discount_status_cleanup_count', 25);
    $discount_ids_to_update = array();
    $needs_inactive_meta = array();
    $needs_expired_meta = array();
    // start by getting the last 25 that hit their maximum usage
    $args = array('suppress_filters' => false, 'post_status' => array('active'), 'posts_per_page' => $cron_discount_number, 'order' => 'ASC', 'meta_query' => array('relation' => 'AND', array('key' => '_edd_discount_uses', 'value' => 'mt1.meta_value', 'compare' => '>=', 'type' => 'NUMERIC'), array('key' => '_edd_discount_max_uses', 'value' => array('', 0), 'compare' => 'NOT IN'), array('key' => '_edd_discount_max_uses', 'compare' => 'EXISTS')));
    add_filter('posts_request', 'edd_filter_discount_code_cleanup');
    $discounts = edd_get_discounts($args);
    remove_filter('posts_request', 'edd_filter_discount_code_cleanup');
    if ($discounts) {
        foreach ($discounts as $discount) {
            $discount_ids_to_update[] = (int) $discount->ID;
            $needs_inactive_meta[] = (int) $discount->ID;
        }
    }
    // Now lets look at the last 25 that hit their expiration without hitting their limit
    $args = array('post_status' => array('active'), 'posts_per_page' => $cron_discount_number, 'order' => 'ASC', 'meta_query' => array('relation' => 'AND', array('key' => '_edd_discount_expiration', 'value' => '', 'compare' => '!='), array('key' => '_edd_discount_expiration', 'value' => date('m/d/Y H:i:s', current_time('timestamp')), 'compare' => '<')));
    $discounts = edd_get_discounts($args);
    if ($discounts) {
        foreach ($discounts as $discount) {
            $discount_ids_to_update[] = (int) $discount->ID;
            if (!in_array($discount->ID, $needs_inactive_meta)) {
                $needs_expired_meta[] = (int) $discount->ID;
            }
        }
    }
    $discount_ids_to_update = array_unique($discount_ids_to_update);
    if (!empty($discount_ids_to_update)) {
        $discount_ids_string = "'" . implode("','", $discount_ids_to_update) . "'";
        $sql = "UPDATE {$wpdb->posts} SET post_status = 'inactive' WHERE ID IN ({$discount_ids_string})";
        $wpdb->query($sql);
    }
    $needs_inactive_meta = array_unique($needs_inactive_meta);
    if (!empty($needs_inactive_meta)) {
        $inactive_ids = "'" . implode("','", $needs_inactive_meta) . "'";
        $sql = "UPDATE {$wpdb->postmeta} SET meta_value = 'inactive' WHERE meta_key = '_edd_discount_status' AND post_id IN ({$inactive_ids})";
        $wpdb->query($sql);
    }
    $needs_expired_meta = array_unique($needs_expired_meta);
    if (!empty($needs_expired_meta)) {
        $expired_ids = "'" . implode("','", $needs_expired_meta) . "'";
        $sql = "UPDATE {$wpdb->postmeta} SET meta_value = 'inactive' WHERE meta_key = '_edd_discount_status' AND post_id IN ({$expired_ids})";
        $wpdb->query($sql);
    }
}
/**
 * Increase Discount Usage
 *
 * Increases the use count of a discount code.
 *
 * @access      public
 * @since       1.0 
 * @param       $code string - the discount code to be incremented
 * @return      int - the new use count
*/
function edd_increase_discount_usage($code)
{
    $discount_id = edd_get_discount_id_by_code($code);
    $discounts = edd_get_discounts();
    $uses = isset($discounts[$discount_id]['uses']) ? $discounts[$discount_id]['uses'] : false;
    if ($uses) {
        $uses++;
    } else {
        $uses = 1;
    }
    $discounts[$discount_id]['uses'] = $uses;
    return update_option('edd_discounts', $discounts);
}
 /**
  * Settings
  *
  * @since 2.0
  */
 public function settings($settings)
 {
     // make sure we only show active discounts
     $args = array('post_status' => 'active');
     $discounts = edd_get_discounts($args);
     if ($discounts) {
         $discount_options = array(0 => __('Select discount', 'edd-social-discounts'));
         foreach ($discounts as $discount) {
             $discount_options[$discount->ID] = $discount->post_title;
         }
     } else {
         $discount_options = array(0 => __('No discounts found', 'edd-social-discounts'));
     }
     $plugin_settings = array(array('id' => 'edd_sd_header', 'name' => '<strong>' . __('Social Discounts', 'edd-social-discounts') . '</strong>', 'type' => 'header'), array('id' => 'edd_sd_services', 'name' => __('Social Services To Enable', 'edd-social-discounts'), 'desc' => __('', 'edd-social-discounts'), 'type' => 'multicheck', 'options' => apply_filters('edd_social_discounts_settings_services', array('twitter' => __('Twitter', 'edd-social-discounts'), 'facebook' => __('Facebook', 'edd-social-discounts'), 'googleplus' => __('Google+', 'edd-social-discounts'), 'linkedin' => __('LinkedIn', 'edd-social-discounts')))), array('id' => 'edd_sd_display_services', 'name' => __('Display Sharing Services', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Sharing services can be positioned on a per download basis by using the [edd_social_discount] shortcode.', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_display_services', array('before' => __('Before content', 'edd-social-discounts'), 'after' => __('After content', 'edd-social-discounts'), 'none' => __('Disable automatic display (use shortcode instead)', 'edd-social-discounts'))), 'std' => 'after'), array('id' => 'edd_sd_discount_code', 'name' => __('Discount Code', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Select the EDD discount that will be applied to the checkout. Leave as default to use plugin as simple sharing service.', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => $discount_options), array('id' => 'edd_sd_title', 'name' => __('Social Discount Title', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the title that will appear above the sharing services.', 'edd-social-discounts') . '</p>', 'type' => 'text', 'std' => __('Share for a discount', 'edd-social-discounts')), array('id' => 'edd_sd_message', 'name' => __('Social Discount Message', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the message that will appear underneath the Social Discount Title.', 'edd-social-discounts') . '</p>', 'type' => 'textarea', 'std' => __('Simply share this and a discount will be applied to your purchase at checkout.', 'edd-social-discounts')), array('id' => 'edd_sd_success_title', 'name' => __('Social Discount Success Title', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the title that will appear above the sharing services when the product has been shared.', 'edd-social-discounts') . '</p>', 'type' => 'text', 'std' => __('Thanks for sharing!', 'edd-social-discounts')), array('id' => 'edd_sd_success_message', 'name' => __('Social Discount Success Message', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the message that will appear underneath the Social Discount Title when the product has been shared.', 'edd-social-discounts') . '</p>', 'type' => 'textarea', 'std' => __('Add this product to your cart and the discount will be applied.', 'edd-social-discounts')), array('id' => 'edd_sd_twitter_username', 'name' => __('Twitter Username', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the Twitter username you want the Follow button to use. Leave blank to disable.', 'edd-social-discounts') . '</p>', 'type' => 'text', 'std' => ''), array('id' => 'edd_sd_twitter_count_box', 'name' => __('Twitter Count Box Position', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Displays how the count box is positioned with the button.', 'edd-social-discounts') . '</p>	', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_twitter_count_box', array('horizontal' => __('Horizontal', 'edd-social-discounts'), 'vertical' => __('Vertical', 'edd-social-discounts'), 'none' => __('None', 'edd-social-discounts'))), 'std' => 'vertical'), array('id' => 'edd_sd_twitter_button_size', 'name' => __('Twitter Button Size', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Note: the count box cannot show when large is selected.', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_sd_twitter_button_size', array('medium' => __('Medium', 'edd-social-discounts'), 'large' => __('Large', 'edd-social-discounts'))), 'std' => 'medium'), array('id' => 'edd_sd_twitter_locale', 'name' => __('Twitter Locale', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the language code, eg en.', 'edd-social-discounts') . '</p>', 'type' => 'text', 'std' => 'en'), array('id' => 'edd_sd_facebook_button_layout', 'name' => __('Facebook Button Layout', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Layout of the button and count.', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_facebook_button_layout', array('button_count' => __('Button Count', 'edd-social-discounts'), 'box_count' => __('Box Count', 'edd-social-discounts'))), 'std' => 'box_count'), array('id' => 'edd_sd_facebook_locale', 'name' => __('Facebook Locale', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the language code, eg en_US. Facebook uses ISO country codes.', 'edd-social-discounts') . '</p>', 'type' => 'text', 'std' => 'en_US'), array('id' => 'edd_sd_googleplus_button_annotation', 'name' => __('Google+ Button Annotation', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('The style of the annotation that displays next to the button.', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_googleplus_button_annotation', array('bubble' => __('Bubble', 'edd-social-discounts'), 'inline' => __('Inline', 'edd-social-discounts'), 'none' => __('None', 'edd-social-discounts'))), 'std' => 'bubble'), array('id' => 'edd_sd_googleplus_button_size', 'name' => __('Google+ Button Size', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('The size of the button', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_googleplus_button_size', array('small' => __('Small', 'edd-social-discounts'), 'medium' => __('Medium', 'edd-social-discounts'), 'standard' => __('Standard', 'edd-social-discounts'), 'tall' => __('Tall', 'edd-social-discounts'))), 'std' => 'tall'), array('id' => 'edd_sd_googleplus_button_recommendations', 'name' => __('Google+ Button Recommendations', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Show recommendations within the +1 hover bubble', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_googleplus_button_recommendations', array('true' => __('Yes', 'edd-social-discounts'), 'false' => __('No', 'edd-social-discounts'))), 'std' => 'true'), array('id' => 'edd_sd_googleplus_locale', 'name' => __('Google+ Locale', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the language code, eg en-US. ', 'edd-social-discounts') . sprintf('<a title="%s" href="%s" target="_blank">' . __('List of supported languages', 'edd-social-discounts') . '</a>.', __('List of supported languages', 'edd-social-discounts'), 'https://developers.google.com/+/web/api/supported-languages') . '</p>', 'type' => 'text', 'std' => 'en-US'), array('id' => 'edd_sd_linkedin_counter', 'name' => __('LinkedIn Counter', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Whether or not to show the the share count and where it gets displayed', 'edd-social-discounts') . '</p>', 'type' => 'select', 'options' => apply_filters('edd_social_discounts_settings_linkedin_counter', array('top' => __('Top', 'edd-social-discounts'), 'right' => __('Right', 'edd-social-discounts'), '' => __('None', 'edd-social-discounts'))), 'std' => 'top'), array('id' => 'edd_sd_linkedin_locale', 'name' => __('LinkedIn Locale', 'edd-social-discounts'), 'desc' => '<p class="description">' . __('Enter the language code, eg en_US. ', 'edd-social-discounts') . '</p>', 'type' => 'text', 'std' => 'en_US'));
     return array_merge($settings, $plugin_settings);
 }
/**
 * Discounts Page
 *
 * Renders the discount page contents.
 *
 * @access      private
 * @since       1.0
 * @return      void
*/
function edd_discounts_page()
{
    global $edd_options;
    $current_page = get_bloginfo('wpurl') . '/wp-admin/admin.php?edit.php?post_type=download&page=edd-discounts';
    ?>
	<div class="wrap">
		
		<?php 
    if (isset($_GET['edd-action']) && $_GET['edd-action'] == 'edit_discount') {
        ?>

			<?php 
        include_once EDD_PLUGIN_DIR . 'includes/admin-pages/forms/edit-discount.php';
        ?>

		<?php 
    } else {
        ?>
			<h2><?php 
        _e('Discount Codes', 'edd');
        ?>
</h2>
			<?php 
        $discounts = edd_get_discounts();
        ?>
			<table class="wp-list-table widefat fixed posts edd-discounts">
				<thead>
					<tr>
						<th><?php 
        _e('Name', 'edd');
        ?>
</th>
						<th><?php 
        _e('Code', 'edd');
        ?>
</th>
						<th><?php 
        _e('Amount', 'edd');
        ?>
</th>
						<th><?php 
        _e('Uses', 'edd');
        ?>
</th>
						<th><?php 
        _e('Max Uses', 'edd');
        ?>
</th>
						<th><?php 
        _e('Start Date', 'edd');
        ?>
</th>
						<th><?php 
        _e('Expiration', 'edd');
        ?>
</th>
						<th><?php 
        _e('Status', 'edd');
        ?>
</th>
						<th><?php 
        _e('Actions', 'edd');
        ?>
</th>
					</tr>
				</thead>
				<tfoot>
					<tr>
						<th><?php 
        _e('Name', 'edd');
        ?>
</th>
						<th><?php 
        _e('Code', 'edd');
        ?>
</th>
						<th><?php 
        _e('Amount', 'edd');
        ?>
</th>
						<th><?php 
        _e('Uses', 'edd');
        ?>
</th>
						<th><?php 
        _e('Max Uses', 'edd');
        ?>
</th>
						<th><?php 
        _e('Start Date', 'edd');
        ?>
</th>
						<th><?php 
        _e('Expiration', 'edd');
        ?>
</th>
						<th><?php 
        _e('Status', 'edd');
        ?>
</th>
						<th><?php 
        _e('Actions', 'edd');
        ?>
</th>
					</tr>
				</tfoot>
				<tbody>
					<?php 
        if ($discounts) {
            ?>
						<?php 
            foreach ($discounts as $discount_key => $discount) {
                ?>
							<tr>
								<td><?php 
                if (isset($discount['name'])) {
                    echo $discount['name'];
                }
                ?>
</td>
								<td><?php 
                if (isset($discount['code'])) {
                    echo $discount['code'];
                }
                ?>
</td>
								<td><?php 
                if (isset($discount['amount'])) {
                    echo edd_format_discount_rate($discount['type'], $discount['amount']);
                }
                ?>
</td>
								<td>
									<?php 
                if (isset($discount['uses']) && isset($discount['max']) && $discount['uses'] != '' && $discount['max'] != '') {
                    echo $discount['uses'] == '' ? 0 : $discount['uses'] . '/' . $discount['max'];
                } else {
                    echo isset($discount['uses']) ? $discount['uses'] : 0;
                }
                ?>
								</td>
								<td>
									<?php 
                if (isset($discount['max'])) {
                    echo $discount['max'] == '' ? __('unlimited', 'edd') : $discount['max'];
                } else {
                    _e('unlimited', 'edd');
                }
                ?>
								</td>
								<td>
								<?php 
                if (isset($discount['start']) && $discount['start'] != '') {
                    echo date(get_option('date_format'), strtotime($discount['start']));
                } else {
                    _e('No start date', 'edd');
                }
                ?>
								</td>
								<td>
								<?php 
                if (isset($discount['expiration']) && $discount['expiration'] != '') {
                    echo edd_is_discount_expired($discount_key) ? __('Expired', 'edd') : $discount['expiration'];
                } else {
                    _e('no expiration', 'edd');
                }
                ?>
								</td>
								<td><?php 
                if (isset($discount['status'])) {
                    echo $discount['status'];
                }
                ?>
</td>
								<td>
									<a href="<?php 
                echo add_query_arg('edd-action', 'edit_discount', add_query_arg('discount', $discount_key, $current_page));
                ?>
"><?php 
                _e('Edit', 'edd');
                ?>
</a> |
									<?php 
                if (edd_is_discount_active($discount_key)) {
                    ?>
									<a href="<?php 
                    echo add_query_arg('edd-action', 'deactivate_discount', add_query_arg('discount', $discount_key, $current_page));
                    ?>
"><?php 
                    _e('Deactivate', 'edd');
                    ?>
</a> |
									<?php 
                } else {
                    ?>
										<a href="<?php 
                    echo add_query_arg('edd-action', 'activate_discount', add_query_arg('discount', $discount_key, $current_page));
                    ?>
"><?php 
                    _e('Activate', 'edd');
                    ?>
</a> |
									<?php 
                }
                ?>
									<a href="<?php 
                echo add_query_arg('edd-action', 'delete_discount', add_query_arg('discount', $discount_key, $current_page));
                ?>
"><?php 
                _e('Delete', 'edd');
                ?>
</a>
								</td>
							</tr>
						<?php 
            }
            ?>
					<?php 
        } else {
            ?>
					<tr><td colspan=10><?php 
            _e('No discount codes have been created.', 'edd');
            ?>
</td>
					<?php 
        }
        ?>
				</tbody>
			</table>		
			<?php 
        do_action('edd_discounts_below_table');
        ?>
	

			<?php 
        include_once EDD_PLUGIN_DIR . 'includes/admin-pages/forms/add-discount.php';
        ?>
		
		<?php 
    }
    ?>
		
	</div><!--end wrap-->
	<?php 
}