/**
  * Set up the widget.
  *
  * @since 1.0.0
  */
 public function load()
 {
     self::load_textdomain();
     add_action('widgets_init', array($this, 'register_widget'));
     $compat = new Flex_Widget_Legacy();
     $compat->load();
     if (is_flex_widget_legacy()) {
         return;
     }
     add_action('init', array($this, 'register_assets'));
     add_action('sidebar_admin_setup', array($this, 'enqueue_admin_assets'));
     add_filter('screen_settings', array($this, 'widgets_screen_settings'), 10, 2);
     add_action('wp_ajax_flex_widget_preferences', array($this, 'ajax_save_user_preferences'));
 }
    /**
     * Display legacy fields in the widget edit form.
     *
     * @since 1.0.0
     *
     * @param array     $instance The widget instance settings.
     * @param WP_Widget $widget   Widget instance.
     */
    public function display_fields($instance, $widget)
    {
        if (is_flex_widget_legacy() || !empty($instance['image'])) {
            ?>
			<div class="flex-widget-legacy-fields">
				<?php 
            if (!is_flex_widget_legacy()) {
                ?>
					<p>
						<em><?php 
                _e('These fields are here to maintain your data from an earlier version.', 'flex-widget');
                ?>
</em>
					</p>
					<p>
						<em><?php 
                _e('Select an image, then clear these values, and they will disappear when you save the widget.', 'flex-widget');
                ?>
</em>
					</p>
				<?php 
            }
            ?>

				<p>
					<label for="<?php 
            echo esc_attr($widget->get_field_id('image'));
            ?>
"><?php 
            _e('Image URL:', 'flex-widget');
            ?>
</label>
					<input type="text" name="<?php 
            echo esc_attr($widget->get_field_name('image'));
            ?>
" id="<?php 
            echo esc_attr($widget->get_field_id('image'));
            ?>
" value="<?php 
            echo esc_url($instance['image']);
            ?>
" class="widefat">
				</p>
				<p>
					<label for="<?php 
            echo esc_attr($widget->get_field_id('alt'));
            ?>
"><?php 
            _e('Alternate Text:', 'flex-widget');
            ?>
</label>
					<input type="text" name="<?php 
            echo esc_attr($widget->get_field_name('alt'));
            ?>
" id="<?php 
            echo esc_attr($widget->get_field_id('alt'));
            ?>
" value="<?php 
            echo esc_attr($instance['alt']);
            ?>
" class="widefat">
				</p>
			</div>
			<?php 
        }
    }
    /**
     * Display the form to edit widget settings.
     *
     * @since 1.0.0
     *
     * @param array $instance The widget settings.
     */
    public function form($instance)
    {
        $instance = wp_parse_args((array) $instance, array('widget_template' => '', 'alt' => '', 'image' => '', 'image_id' => '', 'image_size' => 'full', 'link' => '', 'link_classes' => '', 'link_title' => '', 'new_window' => '', 'title' => '', 'text' => ''));
        $instance['image_id'] = absint($instance['image_id']);
        $instance['widget_title'] = wp_strip_all_tags($instance['widget_title']);
        $button_class = array('button', 'button-hero', 'flex-widget-control-choose');
        $image_id = $instance['image_id'];
        /**
         * The list of fields to display.
         *
         * The order of fields can be modified, new fields can be registered, or
         * existing fields can be removed here. Use the widget type id to limit
         * fields to a particular type of widget.
         *
         * @since 1.0.0
         *
         * @param array  $fields  List of field ids.
         * @param string $id_base Widget type id.
         */
        $fields = (array) apply_filters('flex_widget_fields', $this->form_fields(), $this->id_base);
        ?>

		<div class="flex-widget-form">

			<?php 
        /**
         * Display additional information or HTML before the widget edit form.
         *
         * @since 1.0.0
         *
         * @param array  $instance The widget setttings.
         * @param string $id_base  Widget type id.
         */
        do_action('flex_widget_form_before', $instance, $this->id_base);
        ?>

			<p>
				<label for="<?php 
        echo str_replace('_', '-', esc_attr($this->get_field_id('widget_title')));
        ?>
"><?php 
        _e('Widget Title:', 'flex-widget');
        ?>
</label>
				<input type="text" name="<?php 
        echo esc_attr($this->get_field_name('widget_title'));
        ?>
" id="<?php 
        echo str_replace('_', '-', esc_attr($this->get_field_id('widget_title')));
        ?>
" value="<?php 
        echo esc_attr($instance['widget_title']);
        ?>
" class="widefat">
			</p>

			<?php 
        if (!is_flex_widget_legacy()) {
            ?>
				<p class="<?php 
            echo esc_attr($this->flex_field_class('image_id'));
            ?>
 flex-widget-control<?php 
            echo $image_id ? ' has-image' : '';
            ?>
"
					data-title="<?php 
            esc_attr_e('Choose an Image', 'flex-widget');
            ?>
"
					data-update-text="<?php 
            esc_attr_e('Update Image', 'flex-widget');
            ?>
"
					data-target=".image-id">
					<?php 
            if ($image_id) {
                echo wp_get_attachment_image($image_id, 'medium', false);
                unset($button_class[array_search('button-hero', $button_class)]);
            }
            ?>
					<input type="hidden" name="<?php 
            echo esc_attr($this->get_field_name('image_id'));
            ?>
" id="<?php 
            echo esc_attr($this->get_field_id('image_id'));
            ?>
" value="<?php 
            echo absint($image_id);
            ?>
" class="image-id flex-widget-control-target">
					<a href="#" class="<?php 
            echo esc_attr(join(' ', $button_class));
            ?>
"><?php 
            _e('Choose an Image', 'flex-widget');
            ?>
</a>
				</p>
			<?php 
        }
        ?>

			<?php 
        if (!empty($fields)) {
            foreach ($fields as $field) {
                switch ($field) {
                    case 'widget_template':
                        ?>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('widget_template'));
                        ?>
">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('widget_template'));
                        ?>
"><?php 
                        _e('Widget Template:', 'flex-widget');
                        ?>
</label>
								<input type="text" name="<?php 
                        echo esc_attr($this->get_field_name('widget_template'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('widget_template'));
                        ?>
" value="<?php 
                        echo esc_attr($instance['widget_template']);
                        ?>
" class="widefat">
							</p>
							<?php 
                        break;
                    case 'image_size':
                        $sizes = $this->get_image_sizes($image_id);
                        ?>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('image_size'));
                        ?>
">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('image_size'));
                        ?>
"><?php 
                        _e('Size:', 'flex-widget');
                        ?>
</label>
								<select name="<?php 
                        echo esc_attr($this->get_field_name('image_size'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('image_size'));
                        ?>
" class="widefat image-size"<?php 
                        echo sizeof($sizes) < 2 ? ' disabled="disabled"' : '';
                        ?>
>
									<?php 
                        foreach ($sizes as $id => $label) {
                            printf('<option value="%s"%s>%s</option>', esc_attr($id), selected($instance['image_size'], $id, false), esc_html($label));
                        }
                        ?>
								</select>
							</p>
							<?php 
                        break;
                    case 'link':
                        ?>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('link'));
                        ?>
">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('link'));
                        ?>
"><?php 
                        _e('Link:', 'flex-widget');
                        ?>
</label>
								<input type="text" name="<?php 
                        echo esc_attr($this->get_field_name('link'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('link'));
                        ?>
" value="<?php 
                        echo esc_url($instance['link']);
                        ?>
" class="widefat">
							</p>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('new_window'));
                        ?>
" style="margin-top: -0.75em; padding-left: 2px">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('new_window'));
                        ?>
">
									<input type="checkbox" name="<?php 
                        echo esc_attr($this->get_field_name('new_window'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('new_window'));
                        ?>
" <?php 
                        checked($instance['new_window']);
                        ?>
>
									<?php 
                        _e('Open in new window?', 'flex-widget');
                        ?>
								</label>
							</p>
							<?php 
                        break;
                    case 'link_classes':
                        ?>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('link_classes'));
                        ?>
">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('link_classes'));
                        ?>
"><?php 
                        _e('Link Classes:', 'flex-widget');
                        ?>
</label>
								<input type="text" name="<?php 
                        echo esc_attr($this->get_field_name('link_classes'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('link_classes'));
                        ?>
" value="<?php 
                        echo esc_attr($instance['link_classes']);
                        ?>
" class="widefat">
							</p>
							<?php 
                        break;
                    case 'link_title':
                        ?>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('link_title'));
                        ?>
">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('link_title'));
                        ?>
"><?php 
                        _e('Link Title:', 'flex-widget');
                        ?>
</label>
								<input type="text" name="<?php 
                        echo esc_attr($this->get_field_name('link_title'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('link_title'));
                        ?>
" value="<?php 
                        echo esc_attr($instance['link_title']);
                        ?>
" class="widefat">
							</p>
							<?php 
                        break;
                    case 'title':
                        ?>
              <p class="<?php 
                        echo esc_attr($this->flex_field_class('title'));
                        ?>
">
                <label for="<?php 
                        echo esc_attr($this->get_field_id('title'));
                        ?>
"><?php 
                        _e('Title:', 'flex-widget');
                        ?>
</label>
                <input type="text" name="<?php 
                        echo esc_attr($this->get_field_name('title'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('title'));
                        ?>
" value="<?php 
                        echo esc_attr($instance['title']);
                        ?>
" class="widefat">
              </p>
							<?php 
                        break;
                    case 'text':
                        ?>
							<p class="<?php 
                        echo esc_attr($this->flex_field_class('text'));
                        ?>
">
								<label for="<?php 
                        echo esc_attr($this->get_field_id('text'));
                        ?>
"><?php 
                        _e('Text:', 'flex-widget');
                        ?>
</label>
								<textarea name="<?php 
                        echo esc_attr($this->get_field_name('text'));
                        ?>
" id="<?php 
                        echo esc_attr($this->get_field_id('text'));
                        ?>
" rows="4" class="widefat"><?php 
                        echo esc_textarea($instance['text']);
                        ?>
</textarea>
							</p>
							<?php 
                        break;
                    default:
                        /**
                         * Display a custom field.
                         *
                         * This action will fire for custom fields
                         * registered with the 'flex_widget_fields'
                         * filter.
                         *
                         * @since 1.0.0
                         *
                         * @param array  $instance The widget setttings.
                         * @param string $widget   Widget instance.
                         */
                        do_action('flex_widget_field-' . sanitize_key($field), $instance, $this);
                }
            }
        }
        /**
         * Display additional information or HTML after the widget edit form.
         *
         * @since 1.0.0
         *
         * @param array  $instance The widget setttings.
         * @param string $id_base  Widget type id.
         */
        do_action('flex_widget_form_after', $instance, $this->id_base);
        ?>

		</div><!-- /.flex-widget-form -->
		<?php 
    }