<?php

class ApiciusRecipeTypeWidget extends WP_Widget {

    /**
     * Sets up the widgets name etc
     */
    public function __construct() {
        $widget_ops = array(
            'classname'   => 'apicius_recipe_type',
            'description' => 'Displays a dropdown of recipe types, which takes the user to a filtered version of the recipe archive. Requires the hash feature to be on, and the recipe archive page set in the Apicius Widgets settings.',
        );
        parent::__construct('apicius_recipe_type', 'Apicius Recipe Type Widget', $widget_ops);
    }

    /**
     * Outputs the content of the widget
     *
     * @param array $args
     * @param array $instance
     */
    public function widget($args, $instance) {
        // outputs the content of the widget

        wp_enqueue_script('apicius-widgets-js');
        wp_enqueue_script('apicius-nice-select-js');

        wp_enqueue_style('apicius-nice-select-css');
        wp_enqueue_style('apicius-widget-css');

        $apicius_settings   = get_option('apicius_recipe');
        $widget_settings    = get_option('apicius_widgets');
        $recipe_archive_url = get_the_permalink($widget_settings['archive_location']);

        // if the recipe archive url is empty, or if the hash setting is off, don't show this. it won't work.
        if ($recipe_archive_url != "" && $apicius_settings['use_filter_hash'] === "1") {

            echo $args['before_widget'];
            if (!empty($instance['title'])) {
                echo $args['before_title'].apply_filters('widget_title', $instance['title']).$args['after_title'];
            }
            ?>
			<div id="apicius_widget">
				<select id="widget_recipe_type_select" style="width:100%;" data-archive="<?=$recipe_archive_url?>">
					<option value="">Choose A Recipe Type</option>
                    <?php
                    // get all recipe types (with posts in them)
                    $all_types = get_terms('recipe_type', array(
                        'orderby' => 'name'
                    ));
                    foreach ($all_types as $tag) {
                        echo '<option value=".type-'.$tag->slug.'-dropdown">'.$tag->name.'</option>'.PHP_EOL;
                    }
                    ?>
				</select>
			</div>
            <?php
        }
        echo $args['after_widget'];
    }

    /**
     * Outputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form($instance) {
        // outputs the options form on admin
//        $title = !empty($instance['title']) ? $instance['title'] : esc_html__('View Recipes By Type', 'text_domain');
        $title = !empty($instance['title']) ? $instance['title'] : "";
        ?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Title:', 'text_domain'); ?></label>
			<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>"
			       name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text"
			       value="<?php echo esc_attr($title); ?>">
		</p>
        <?php
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     */
    public function update($new_instance, $old_instance) {
        // processes widget options to be saved
        $instance          = array();
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';

        return $instance;
    }
}


add_action('widgets_init',
    create_function('', 'return register_widget("ApiciusRecipeTypeWidget");')
);
