<?php
add_action('widgets_init', 'serpentsoft_soundcloud_widget_init');

function serpentsoft_soundcloud_widget_init() {
    register_widget('serpentsoft_soundcloud_widget');
}

class serpentsoft_soundcloud_widget extends WP_Widget {

    function serpentsoft_soundcloud_widget() {
        $widget_ops = array('classname' => 'w-audio');
        $control_ops = array('width' => 250, 'height' => 350, 'id_base' => 'id-widget-soundcloud');
        $this->WP_Widget('id-widget-soundcloud', theme_name . ' - SoundCloud', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        extract($args);

        $title = apply_filters('widget_title', $instance['widget_title']);

        $soundcloud_url = $instance['soundcloud_url'];
        $soundcloud_url = preg_replace('/width="([3-9][0-9]{2,}|[1-9][0-9]{3,})"/', 'width="100%"', $soundcloud_url);
        $soundcloud_url = preg_replace('/height="([0-9]*)"/', 'height="200"', $soundcloud_url);

        $soundcloud_auto_play = $instance['soundcloud_auto_play'];
        $auto_play = false;

        if ($soundcloud_auto_play == true) {
            $auto_play = true;
        }

        if ($soundcloud_url) {
            echo $before_widget;

            if ($title) {
                echo $before_title;
                echo $title;
                echo $after_title;
            }

            $output = serpentsoft_soundcloud($soundcloud_url, $soundcloud_auto_play);
            $output .= $after_widget;
            
            echo $output;
        }
    }

    function update($new_instance, $old_instance) {
        $instance = $old_instance;
        $instance['widget_title'] = strip_tags($new_instance['widget_title']);
        $instance['soundcloud_url'] = strip_tags($new_instance['soundcloud_url']);
        $instance['soundcloud_auto_play'] = strip_tags($new_instance['soundcloud_auto_play']);

        return $instance;
    }

    function form($instance) {
        $defaults = array('widget_title' => __('Sound Cloud', 'serpentsoft'), 'soundcloud_url' => '', 'soundcloud_auto_play' => '');
        $instance = wp_parse_args((array) $instance, $defaults);
        ?>

        <style>
            .video-code-text{
                display: block;
                width: 228px;
            }

        </style>
        <p>
            <label for="<?php echo $this->get_field_id('widget_title'); ?>"><?php _e('Title', 'serpentsoft'); ?></label>
            <input class="video-code-text" id="<?php echo $this->get_field_id('widget_title'); ?>" name="<?php echo $this->get_field_name('widget_title'); ?>" value="<?php echo $instance['widget_title']; ?>" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('soundcloud_url'); ?>"><?php _e('SoundCloud URL', 'serpentsoft'); ?></label>
            <input class="video-code-text" id="<?php echo $this->get_field_id('soundcloud_url'); ?>" name="<?php echo $this->get_field_name('soundcloud_url'); ?>" value="<?php echo $instance['soundcloud_url']; ?>" type="text" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id('soundcloud_auto_play'); ?>"><?php _e('Autoplay?', 'serpentsoft'); ?></label>
            <input id="<?php echo $this->get_field_id('soundcloud_auto_play'); ?>" name="<?php echo $this->get_field_name('soundcloud_auto_play'); ?>" value="true" <?php if ($instance['soundcloud_auto_play']) echo 'checked="checked"'; ?> type="checkbox" />
        </p>

        <?php
    }

}
?>