<?php
/* Adds Maskitto_Services widget. */
class Maskitto_Partners extends WP_Widget {

    /* Register widget with WordPress. */
    function __construct() {
        parent::__construct(
            'maskitto_partners',
            __('Maskitto: Partners', 'maskitto-light'),
            array(
                'description' => __( 'Only for page builder.', 'maskitto-light' ),
                'panels_icon' => 'dashicons dashicons-groups',
                'panels_groups' => 'theme-widgets'
            )
        );
    }


    /* Front-end display of widget. */
    public function widget( $args, $instance ) {
        $partners = wp_count_posts( 'partners' );
        if( isset( $partners->publish ) && $partners->publish > 0){
    ?>
        <div class="page-section">
            <div class="container">

                <?php if( ( isset( $instance['title'] ) && $instance['title'] ) && ( isset( $instance['subtitle'] ) && $instance['subtitle'] ) ) { ?>
                <div class="section-title text-center">
                    <?php if( $instance['title'] ){ ?>
                        <h3><?php echo $instance['title']; ?></h3>
                    <?php } ?>
                    <?php if( $instance['subtitle'] ){ ?>
                        <div class="subtitle"><p><?php echo $instance['subtitle']; ?></p></div>
                    <?php } ?>
                    <div class="section-title-line"></div>
                </div>
                <?php } ?>
                
                <div class="partners-list">
                    <?php
                        $loop = new WP_Query( array('post_type' => 'partners', 'posts_per_page' => 20 ) );
                        while ( $loop->have_posts() ) : $loop->the_post();
                        
                            $image = esc_url( get_post_meta( get_the_ID(), 'wpcf-background-image', true ));
                            $url = esc_url( get_post_meta( get_the_ID(), 'wpcf-url', true ));
                            if( !$url ) $url = '#';
                    ?>
                        <?php if( $image ) : ?>
                            <div class="partner-item">
                                <a href="<?php echo $url; ?>">
                                    <img height="100" src="<?php echo $image; ?>" class="attachment-post-thumbnail wp-post-image" alt="<?php the_title(); ?>">
                                </a>
                            </div>
                        <?php endif; ?>
                    <?php endwhile; ?>
                </div>
            </div>
        </div>

    <?php } }


    /* Back-end widget form. */
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }

        if ( isset( $instance[ 'subtitle' ] ) ) {
            $subtitle = $instance[ 'subtitle' ];
        }
        
        ?>

        <div class="widget-option">
            <div class="widget-th">
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><b><?php _e( 'Title', 'maskitto-light' ); ?></b></label> 
            </div>
            <div class="widget-td">
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
                <p><?php _e( 'This field is optional', 'maskitto-light' ); ?></p>
            </div>
            <div class="clearfix"></div>
        </div>

        <div class="widget-option">
            <div class="widget-th">
                <label for="<?php echo $this->get_field_id( 'subtitle' ); ?>"><b><?php _e( 'Subitle', 'maskitto-light' ); ?></b></label> 
            </div>
            <div class="widget-td">
                <input class="widefat" id="<?php echo $this->get_field_id( 'subtitle' ); ?>" name="<?php echo $this->get_field_name( 'subtitle' ); ?>" type="text" value="<?php echo esc_attr( $subtitle ); ?>">
                <p><?php _e( 'This field is optional', 'maskitto-light' ); ?></p>
            </div>
            <div class="clearfix"></div>
        </div>

        <link rel='stylesheet' id='options-css'  href='<?php echo get_template_directory_uri() . '/css/options.css'; ?>' type='text/css' media='all' />

        <?php 
    }


    /* Sanitize widget form values as they are saved. */
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        $instance['subtitle'] = ( ! empty( $new_instance['subtitle'] ) ) ? strip_tags( $new_instance['subtitle'] ) : '';

        return $instance;
    }

}