MortgageNews_Widget

Class MortgageNews_Widget.


Description Description

See also See also


Top ↑

Source Source

File: mortgage-news-widget/inc/MortgageNews_Widget.php

class MortgageNews_Widget extends \WP_Widget{

	/**
	 * MortgageNews_Widgets constructor.
	 * @since  1.0.0
	 * @access public
	 */
	public function __construct() {
		parent::__construct(
			'mortgage_news_widget',
			__( 'Mortgage News', 'MortgageNewsWidget' ),
			array( 'description' => __( 'Mortgage news', 'MortgageNewsWidget' ) )
		);
	}

	/**
	 * Widget init.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param array $args     arguments
	 * @param array $instance instance
	 */
	public function widget( $args, $instance ) {

		extract( $args );
		/**
		 * Widget title filter.
		 *
		 * @param mixed $instance ['title'] title
		 */
		$title = apply_filters( 'widget_title', $instance['title'] );
		// before and after widget arguments are defined by themes

		$type = $instance['type'];
		echo  $args['before_widget'] ;
		if ( $title ) {
			echo $before_title  . esc_html( $title ) . $after_title ;
		}

		$embedded = new MortgageNews_Embedded();
		$enqueue = new MortgageNews_Enqueue();
		$news   = new MortgageNews;

		$news_data = $news->get_news();

		if ( $type == 'bullets' ) {
			$embedded->embedded_bullets_html();
			$enqueue->enqueue_bullets();
		}
		if ( $type == 'scroller-1' ) {
			$embedded->scroller_html_type1( $news_data );
			wp_enqueue_style( 'mortgage-news-scroller-1', plugins_url( '../assets/css/mortgage-news-scroller-1.css', __FILE__ ), array(), '1.0.0' );
		}
		if ( $type == 'scroller-2' ) {
			$embedded->scroller_html_type2( $news_data );
			wp_enqueue_style( 'mortgage-news-scroller-2', plugins_url( '../assets/css/mortgage-news-scroller-2.css', __FILE__ ), array(), '1.0.0' );
		}
		if ( $type == 'scroller-3' ) {
			$embedded->scroller_html_type3( $news_data );
			wp_enqueue_style( 'mortgage-news-scroller-3', plugins_url( '../assets/css/mortgage-news-scroller-3.css', __FILE__ ), array(), '1.0.0' );
		}
		if ( $type == 'scroller-4' ) {
			$embedded->scroller_html_type4( $news_data );
			wp_enqueue_style( 'mortgage-news-scroller-4', plugins_url( '../assets/css/mortgage-news-scroller-4.css', __FILE__ ), array(), '1.0.0' );
		}

		if ('scroller-1'===$type || 'scroller-2'===$type || 'scroller-3'===$type || 'scroller-4'===$type){
			wp_enqueue_script( 'breakingNews-scroller', plugins_url( '../assets/js/breakingNews-scroller.js', __FILE__ ), array( 'jquery' ), '1.0.0', true );
			$data = ' if (jQuery(".newsTicker-' . $type . '").length) {
                                jQuery(".newsTicker-' . $type . '").breakingNews()
                            }';
			wp_add_inline_script( 'breakingNews-scroller', $data, 'after' );
        }
		if ( 'list' === $type || 'plaintext' === $type ) {
			$embedded->plain_text($news_data, $type);
		}


		echo $args['after_widget'] ;

	}

	/**
	 * Widget Backend.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param array $instance instance
	 */
	public function form( $instance ) {

		$defaults = array(
			'title'         => ' ',
			'type' => 'bullets'
		);
		$instance = wp_parse_args( (array) $instance, $defaults );
		?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title', 'MortgageNewsWidget' ); ?></label>
			<input id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
			       name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"
			       value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" type="text"/>
		</p>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'type' ) ); ?>"><?php echo esc_html__( 'Mortgage news type', 'MortgageNewsWidget' ); ?></label>
			<select id="<?php echo $this->get_field_id('type'); ?>" class="widefat" name="<?php echo esc_attr( $this->get_field_name( 'type' ) ); ?>">
				<option <?php selected( $instance['type'], 'bullets' ); ?> value="bullets">bullets</option>
				<option <?php selected( $instance['type'], 'scroller-1' ); ?> value="scroller-1">scroller-1</option>
				<option <?php selected( $instance['type'], 'scroller-2' ); ?> value="scroller-2">scroller-2</option>
				<option <?php selected( $instance['type'], 'scroller-3' ); ?> value="scroller-3">scroller-3</option>
				<option <?php selected( $instance['type'], 'scroller-4' ); ?> value="scroller-4">scroller-4</option>
				<option <?php selected( $instance['type'], 'list' ); ?> value="list">text</option>
				<option <?php selected( $instance['type'], 'plaintext' ); ?> value="plaintext">plaintext</option>
			</select>
		</p>
		<?php
	}


	/**
	 * Updating widget replacing old instances with new.
	 * @since  1.0.0
	 * @access public
	 *
	 * @param array $new_instance new instance
	 * @param array $old_instance old instance
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		$instance                  = $old_instance;
		$instance['title']         = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
		$instance['type'] = stripslashes( $new_instance['type'] );

		return $instance;
	}

}

Top ↑

Methods Methods

  • __construct — MortgageNews_Widgets constructor.
  • form — Widget Backend.
  • update — Updating widget replacing old instances with new.
  • widget — Widget init.

Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0 Introduced.