/**
  * Fetches a Button or Network Button with a specific name
  * @param  string $name
  * @return object||FALSE
  */
 public static function get_button_by_name($name)
 {
     if ($button = \PodloveSubscribeButton\Model\Button::find_one_by_property('name', $name)) {
         return $button;
     }
     if ($network_button = \PodloveSubscribeButton\Model\NetworkButton::find_one_by_property('name', $name)) {
         $network_button->id = $network_button->id . 'N';
         return $network_button;
     }
     return FALSE;
 }
 public static function build_models()
 {
     // Build Databases
     \PodloveSubscribeButton\Model\Button::build();
     if (is_multisite()) {
         \PodloveSubscribeButton\Model\NetworkButton::build();
     }
     // Set Button "default" values
     $default_values = array('size' => 'big', 'autowidth' => 'on', 'color' => '#599677', 'style' => 'filled', 'format' => 'rectangle');
     foreach ($default_values as $option => $default_value) {
         if (!get_option('podlove_subscribe_button_default_' . $option)) {
             update_option('podlove_subscribe_button_default_' . $option, $default_value);
         }
     }
 }
    public function form($instance)
    {
        foreach (self::$widget_settings as $setting) {
            ${$setting} = isset($instance[$setting]) ? $instance[$setting] : '';
        }
        $buttons = \PodloveSubscribeButton\Model\Button::all();
        if (is_multisite()) {
            $network_buttons = \PodloveSubscribeButton\Model\NetworkButton::all();
        }
        $buttons_as_options = function ($buttons) {
            foreach ($buttons as $subscribebutton) {
                echo "<option value='" . $subscribebutton->name . "' " . ($subscribebutton->name == $button ? 'selected=\\"selected\\"' : '') . " >" . $subscribebutton->title . " (" . $subscribebutton->name . ")</option>";
            }
        };
        ?>
		<p>
			<label for="<?php 
        echo $this->get_field_id('title');
        ?>
"><?php 
        _e('Title', 'podlove');
        ?>
</label> 
			<input class="widefat" id="<?php 
        echo $this->get_field_id('title');
        ?>
" name="<?php 
        echo $this->get_field_name('title');
        ?>
" value="<?php 
        echo $title;
        ?>
" />

			<label for="<?php 
        echo $this->get_field_id('color');
        ?>
"><?php 
        _e('Color', 'podlove');
        ?>
</label>
			<input class="podlove_subscribe_button_color" id="<?php 
        echo $this->get_field_id('color');
        ?>
" name="<?php 
        echo $this->get_field_name('color');
        ?>
" value="<?php 
        echo $color;
        ?>
" />
			<style type="text/css">
				.sp-replacer {
					display: flex;
				}
				.sp-preview {
					flex-grow: 10;
				}
			</style>

			<label for="<?php 
        echo $this->get_field_id('button');
        ?>
"><?php 
        _e('Button', 'podlove');
        ?>
</label> 
			<select class="widefat" id="<?php 
        echo $this->get_field_id('button');
        ?>
"
				      name="<?php 
        echo $this->get_field_name('button');
        ?>
">
				<?php 
        if (isset($network_buttons) && count($network_buttons) > 0) {
            ?>
					<optgroup label="<?php 
            _e('Local', 'podlove');
            ?>
">
						<?php 
            $buttons_as_options($buttons);
            ?>
					</optgroup>
					<optgroup label="<?php 
            _e('Network', 'podlove');
            ?>
">
						<?php 
            $buttons_as_options($network_buttons);
            ?>
					</optgroup>
				<?php 
        } else {
            $buttons_as_options($buttons);
        }
        ?>
			</select>

			<?php 
        $customize_options = array('size' => array('name' => 'Size', 'options' => \PodloveSubscribeButton\Model\Button::$size), 'style' => array('name' => 'Style', 'options' => \PodloveSubscribeButton\Model\Button::$style), 'format' => array('name' => 'Format', 'options' => \PodloveSubscribeButton\Model\Button::$format), 'autowidth' => array('name' => 'Autowidth', 'options' => \PodloveSubscribeButton\Model\Button::$width));
        foreach ($customize_options as $slug => $properties) {
            ?>
				<label for="<?php 
            echo $this->get_field_id($slug);
            ?>
"><?php 
            _e($properties['name'], 'podlove');
            ?>
</label> 
				<select class="widefat" id="<?php 
            echo $this->get_field_id($slug);
            ?>
" name="<?php 
            echo $this->get_field_name($slug);
            ?>
">
					<option value="default" <?php 
            echo ${$slug} == 'default' ? 'selected="selected"' : '';
            ?>
><?php 
            _e('Default ' . $properties['name'], 'podlove');
            ?>
</option>
					<optgroup>
						<?php 
            foreach ($properties['options'] as $property => $name) {
                ?>
						<option value="<?php 
                echo $property;
                ?>
" <?php 
                echo ${$slug} == $property ? 'selected="selected"' : '';
                ?>
><?php 
                _e($name, 'podlove');
                ?>
</option>
						<?php 
            }
            ?>
					</optgroup>
				</select>
			<?php 
        }
        ?>
		
			<label for="<?php 
        echo $this->get_field_id('infotext');
        ?>
"><?php 
        _e('Description', 'podlove');
        ?>
</label> 
			<textarea class="widefat" rows="10" id="<?php 
        echo $this->get_field_id('infotext');
        ?>
" name="<?php 
        echo $this->get_field_name('infotext');
        ?>
"><?php 
        echo $infotext;
        ?>
</textarea>
		</p>
		<?php 
    }
 public static function edit_template()
 {
     if (filter_input(INPUT_GET, 'network') == '1') {
         $button = \PodloveSubscribeButton\Model\NetworkButton::find_by_id(filter_input(INPUT_GET, 'button'));
     } else {
         $button = \PodloveSubscribeButton\Model\Button::find_by_id(filter_input(INPUT_GET, 'button'));
     }
     echo '<h3>' . sprintf(__('Edit Subscribe button: %s', 'podlove'), $button->title) . '</h3>';
     self::form_template($button, 'save');
 }