/**
 * Add Modal Window to Footer
 *
 * @since  1.0
 */
function dedo_media_modal()
{
    global $pagenow;
    // Only run in post/page creation and edit screens
    if (in_array($pagenow, array('post.php', 'page.php', 'post-new.php', 'post-edit.php'))) {
        // Get published downloads
        $downloads = get_posts(array('post_type' => 'dedo_download', 'post_status' => 'publish', 'orderby' => 'title', 'order' => 'ASC', 'posts_per_page' => -1));
        // Get registered styles
        $styles = dedo_get_shortcode_styles();
        // Get registered buttons
        $buttons = dedo_get_shortcode_buttons();
        ?>
			
			<div id="dedo-shortcode-modal" class="dedo-modal" style="display: none; width: 30%; left: 50%; margin-left: -15%;">
				<a href="#" class="dedo-modal-close" title="<?php 
        _e('Close', 'delightful-downloads');
        ?>
"><span class="media-modal-icon"></span></a>
				<div class="dedo-modal-content">
					<h1><?php 
        _e('Insert Download', 'delightful-downloads');
        ?>
</h1>
							
					<?php 
        if ($downloads) {
            ?>
						<p>
							<label><span><?php 
            _e('Download', 'delightful-downloads');
            ?>
</span>
								<select id="dedo-select-download-dropdown">
									<?php 
            foreach ($downloads as $download) {
                ?>
										<option value="<?php 
                echo $download->ID;
                ?>
"><?php 
                echo $download->post_title;
                ?>
</option>
									<?php 
            }
            ?>
								</select>
							</label>
						</p>
						<p class="clear">
							<label id="dedo-style-dropdown-container" class="column-2"><span><?php 
            _e('Style', 'delightful-downloads');
            ?>
</span>
								<select id="dedo-select-style-dropdown">
									<optgroup label="<?php 
            _e('Global', 'delightful-downloads');
            ?>
">
										<option value=""><?php 
            _e('Inherit', 'delightful-downloads');
            ?>
</option>
									</optgroup>
									<optgroup label="<?php 
            _e('Styles', 'delightful-downloads');
            ?>
">
										<?php 
            foreach ($styles as $key => $value) {
                ?>
											<option value="<?php 
                echo $key;
                ?>
"><?php 
                echo $value['name'];
                ?>
</option>
										<?php 
            }
            ?>
									</optgroup>
								</select>
							</label>

							<label id="dedo-button-dropdown-container" class="column-2"><span><?php 
            _e('Button', 'delightful-downloads');
            ?>
</span>
								<select id="dedo-select-button-dropdown">
									<optgroup label="<?php 
            _e('Global', 'delightful-downloads');
            ?>
">
										<option value=""><?php 
            _e('Inherit', 'delightful-downloads');
            ?>
</option>
									</optgroup>
									<optgroup label="<?php 
            _e('Buttons', 'delightful-downloads');
            ?>
">
										<?php 
            foreach ($buttons as $key => $value) {
                ?>
											<option value="<?php 
                echo $key;
                ?>
"><?php 
                echo $value['name'];
                ?>
</option>
										<?php 
            }
            ?>
									</optgroup>
								</select>
							</label>
						</p>
						<p>
							<label><span><?php 
            _e('Text', 'delightful-downloads');
            ?>
</span>	
								<input id="dedo-custom-text" type="text" placeholder="<?php 
            _e('Inherit', 'delightful-downloads');
            ?>
" />
							</label>
						</p>
						<p class="buttons clear">
							<a href="#" id="dedo-insert" class="button button-large button-primary"><?php 
            _e('Insert', 'delightful-downloads');
            ?>
</a>
							<a href="#" id="dedo-file-size" class="button button-large right"><?php 
            _e('File Size', 'delightful-downloads');
            ?>
</a>
							<a href="#" id="dedo-download-count" class="button button-large right"><?php 
            _e('Download Count', 'delightful-downloads');
            ?>
</a>
						</p>
					<?php 
        } else {
            ?>
						<p><?php 
            echo sprintf(__('Please %sadd%s a new download.', 'delightful-downloads'), '<a href="' . admin_url('post-new.php?post_type=dedo_download') . '" target="_blank">', '</a>');
            ?>
</p>
					<?php 
        }
        ?>

				</div>
			</div>

		<?php 
    }
}
/**
 * Download Shortcode.
 *
 * Outputs a single download based on user defined attributes.
 *
 * @since   1.0
 */
function dedo_shortcode_ddownload($atts)
{
    /**
     * Use get option again so that WPML can filter default text
     * for translation, get_option() is cached so should not
     * affect performance.
     *
     */
    global $dedo_default_options;
    $dedo_options = wp_parse_args(get_option('delightful-downloads'), $dedo_default_options);
    // Attributes
    extract(shortcode_atts(array('id' => '', 'text' => $dedo_options['default_text'], 'style' => $dedo_options['default_style'], 'button' => '', 'color' => '', 'class' => ''), $atts, 'ddownload'));
    // Validate download id
    if ($id == '' || !dedo_download_valid($id)) {
        return __('Invalid download ID.', 'delightful-downloads');
    }
    // Check style against registered styles
    $registered_styles = dedo_get_shortcode_styles();
    if (array_key_exists($style, $registered_styles)) {
        $style_format = $registered_styles[$style]['format'];
    } else {
        return __('Invalid style attribute.', 'delightful-downloads');
    }
    // Check for deprecated color att
    if (!empty($color) && empty($button)) {
        $button = $color;
    }
    // Check button against registered buttons
    if ($style == 'button') {
        $button = empty($button) ? $dedo_options['default_button'] : $button;
        $registered_buttons = dedo_get_shortcode_buttons();
        if (array_key_exists($button, $registered_buttons)) {
            $button_class = $registered_buttons[$button]['class'];
        } else {
            return __('Invalid button attribute.', 'delightful-downloads');
        }
    }
    // Generate correct class and add user defined
    $classes = 'ddownload-' . $style;
    // Output style
    $classes .= isset($button_class) ? ' ' . $button_class : '';
    // Button style
    $classes .= ' id-' . $id;
    // Download id
    $classes .= ' ext-' . dedo_get_file_ext(get_post_meta($id, '_dedo_file_url', true));
    // File extension
    $classes .= !empty($class) ? ' ' . $class : '';
    // User defined
    // Replace text and class att
    $replace = array('%text%' => $text, '%class%' => $classes);
    foreach ($replace as $key => $value) {
        $style_format = str_replace($key, $value, $style_format);
    }
    // Search and replace wildcards
    $output = dedo_search_replace_wildcards($style_format, $id);
    // Enqueue frontend CSS if option is enabled
    if ($dedo_options['enable_css'] && 'button' == $style) {
        wp_enqueue_style('dedo-css');
    }
    return apply_filters('dedo_shortcode_ddownload', $output);
}
/**
 * Render default button field
 *
 * @since  1.3
 */
function dedo_settings_default_button_field()
{
    global $dedo_options;
    $colors = dedo_get_shortcode_buttons();
    $default_color = $dedo_options['default_button'];
    echo '<select name="delightful-downloads[default_button]">';
    foreach ($colors as $key => $value) {
        $selected = $default_color == $key ? ' selected="selected"' : '';
        echo '<option value="' . $key . '" ' . $selected . '>' . $value['name'] . '</option>';
    }
    echo '</select>';
    echo '<p class="description">' . sprintf(__('The default button style, when using the %s shortcode. This can be overridden on a per-download basis.', 'delightful-downloads'), '<code>[ddownload]</code>');
}