Skip to content

Instantly share code, notes, and snippets.

@lswilson
Created January 3, 2014 16:10
Show Gist options
  • Save lswilson/8240534 to your computer and use it in GitHub Desktop.
Save lswilson/8240534 to your computer and use it in GitHub Desktop.
Added find a rental widget: widgets.php
<?php
add_action( 'widgets_init', 'realtor_register_widgets' );
function realtor_register_widgets() {
register_widget( 'realtor_social_widget' );
register_widget( 'realtor_ad_widget' );
register_widget( 'realtor_buzz_widget' );
register_widget( 'realtor_find_home_widget' );
register_widget( 'realtor_find_rental_widget' );
register_widget( 'realtor_ask_question_widget' );
register_widget( 'realtor_featured_posts_widget' );
}
/**
* Realtor social widget
*/
class realtor_social_widget extends WP_Widget {
//process the new widget
function realtor_social_widget() {
$widget_ops = array(
'classname' => 'realtor_social_widget unit-connect-to-us group',
'description' => 'Display social icons'
);
$this->WP_Widget( 'realtor_social_widget', 'Realtor Social Icons', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array(
'title' => 'Connect to us',
'facebook' => 'http://www.facebook.com/realtor.com',
'twitter' => 'realtordotcom',
'pinterest' => 'http://pinterest.com/realtordotcom',
'googleplus' => 'https://plus.google.com/114090562056915879429',
'rss' => 'http://www.realtor.com/blogs/feed/',
);
$instance = wp_parse_args( (array) $instance, $defaults );
extract( $instance, EXTR_SKIP );
?>
<p><label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<p><label>Facebook URL: <input class="widefat" name="<?php echo $this->get_field_name( 'facebook' ); ?>" type="text" value="<?php echo esc_attr( $facebook ); ?>" /></label></p>
<p><label>Twitter Username: (without "@" symbol) <input class="widefat" name="<?php echo $this->get_field_name( 'twitter' ); ?>" type="text" value="<?php echo esc_attr( $twitter ); ?>" /></label></p>
<p><label>Pinterest URL: <input class="widefat" name="<?php echo $this->get_field_name( 'pinterest' ); ?>" type="text" value="<?php echo esc_attr( $pinterest ); ?>" /></label></p>
<p><label>Google Plus URL: <input class="widefat" name="<?php echo $this->get_field_name( 'googleplus' ); ?>" type="text" value="<?php echo esc_attr( $googleplus ); ?>" /></label></p>
<p><label>RSS Feed URL: <input class="widefat" name="<?php echo $this->get_field_name( 'rss' ); ?>" type="text" value="<?php echo esc_attr( $rss ); ?>" /></label></p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['facebook'] = esc_url( $new_instance['facebook'] );
$instance['twitter'] = sanitize_title( $new_instance['twitter'] );
$instance['pinterest'] = esc_url( $new_instance['pinterest'] );
$instance['googleplus'] = esc_url( $new_instance['googleplus'] );
$instance['rss'] = esc_url( $new_instance['rss'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
// if ( !empty( $title ) ) { $title = $before_title . $title . $after_title; };
if ( $fb = $instance['facebook'] ) $social['facebook'] = $fb;
if ( $tw = $instance['twitter'] ) $social['twitter'] = 'http://twitter.com/'. $tw;
if ( $fb = $instance['pinterest'] ) $social['pinterest'] = $fb;
if ( $ct = $instance['googleplus'] ) $social['googleplus'] = $ct;
if ( $rss = $instance['rss'] ) $social['rss'] = $rss;
if ( !empty( $social ) ) {
echo '<h3>'. $title .'</h3><div class="unit-body"><ul class="list list-horizontal">';
$counter = 0;
$our_domain = parse_url( site_url() );
foreach ( $social as $name => $value ) {
$domain = parse_url( $value );
if ( $domain['host'] && $domain['host'] != $our_domain['host'] ) {
$target = 'target="_blank"';
} else {
$target = '';
}
if ( $counter % 2 ) $class = ' odd';
else $class = '';
echo '<li><a class="i-'. $name . $class .' i-small i-advice" alt="Follow Realtor.com on '. ucfirst( $name ) .'" href="'. $value .'" '. $target .'>'. ucfirst( $name ) .'</a></li>';
// echo '<a class="icons-'. $name . $class .'" href="'. $value .'" '. $target .'>'. $name .'</a>';
$counter++;
}
echo '</ul></div>';
}
echo $after_widget;
}
}
/**
* Realtor ad 300x250 widget
*/
class realtor_ad_widget extends WP_Widget {
//process the new widget
function realtor_ad_widget() {
$widget_ops = array(
'classname' => 'realtor_ad_widget',
'description' => 'Display 300x250 Ad. Changes based on post category.'
);
$this->WP_Widget( 'realtor_ad_widget', 'Realtor 300x250 Ad Rotator', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => '' );
$instance = wp_parse_args( (array) $instance, $defaults );
extract( $instance, EXTR_SKIP );
?>
<p><label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
do_action( 'realtor_med_rect_ad' );
echo $after_widget;
}
}
/**
* Realtor.com Buzz widget (contents to be populated by Realtor.com devs)
*/
class realtor_buzz_widget extends WP_Widget {
//process the new widget
function realtor_buzz_widget() {
$widget_ops = array(
'classname' => 'realtor_buzz_widget',
'description' => 'Displays Realtor.com Buzz'
);
$this->WP_Widget( 'realtor_buzz_widget', 'realtor.com<sup>&reg;</sup> Buzz', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => 'realtor.com<sup>&reg;</sup> Buzz' );
$instance = wp_parse_args( (array) $instance, $defaults );
extract( $instance, EXTR_SKIP );
?>
<p><label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
//echo '<iframe src="http://storify.com/realtordotcom/the-buzz-about-realtorcom/embed?" name="realtordotcom-the-buzz-about-realtorcom" scrolling="no" frameborder="no" allowtransparency="true" style="display: block; background-color: transparent; border: none; overflow: hidden; width: 100%; max-width: 308px; height: 334px; min-height: 334px; "></iframe>';
require_once('Buzz.php');
Buzz::GetBuzzSidebar();
echo $after_widget;
}
}
/**
* Realtor.com Find A Home widget (contents to be populated by Realtor.com devs)
*/
class realtor_find_home_widget extends WP_Widget {
//process the new widget
function realtor_find_home_widget() {
$widget_ops = array(
'classname' => 'realtor_find_home_widget unit-find-a-home unit-lg-bg-img unit-double-body',
'description' => 'Displays Realtor.com Find a Home search box'
);
$this->WP_Widget( 'realtor_find_home_widget', 'Realtor.com "Find a Home"', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => 'Find a Home' );
$instance = wp_parse_args( (array) $instance, $defaults );
extract( $instance, EXTR_SKIP );
?>
<p><label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
//save nearby cities to a session so we don't have to look this up every page load
if(empty($_SESSION['nearby_cities'])){
require_once('Util.php');
//setcookie('nearby_cities',Util::GetNearbyCitiesByIP(), time()+86400);
$_SESSION['nearby_cities'] = Util::GetNearbyCitiesByIP();
}
include( 'includes/find_a_home.php' );
echo $after_widget;
}
}
/**
* Realtor.com Find A Rental widget
*/
class realtor_find_rental_widget extends WP_Widget {
//process the new widget
function realtor_find_rental_widget() {
$widget_ops = array(
'classname' => 'realtor_find_rental_widget unit-find-a-rental unit-lg-bg-img unit-double-body',
'description' => 'Displays Realtor.com Find a Rental search box'
);
$this->WP_Widget( 'realtor_find_rental_widget', 'Realtor.com "Find a Rental"', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => 'Find a Rental' );
$instance = wp_parse_args( (array) $instance, $defaults );
extract( $instance, EXTR_SKIP );
?>
<p><label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
include( 'includes/find_a_rental.php' );
echo $after_widget;
}
}
/**
* Realtor.com Find A Home widget (contents to be populated by Realtor.com devs)
*/
class realtor_ask_question_widget extends WP_Widget {
//process the new widget
function realtor_ask_question_widget() {
$widget_ops = array(
'classname' => 'realtor_ask_question_widget unit-qanda unit-color-backwards',
'description' => 'Displays Realtor.com Ask a Question box'
);
$this->WP_Widget( 'realtor_ask_question_widget', 'Realtor.com "Ask a Question"', $widget_ops );
}
//build the widget settings form
function form($instance) {
$defaults = array( 'title' => 'Ask a Question' );
$instance = wp_parse_args( (array) $instance, $defaults );
extract( $instance, EXTR_SKIP );
?>
<p><label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
extract($args);
echo $before_widget;
$title = apply_filters( 'widget_title', $instance['title'] );
//if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '<div class="unit-header group"><h3 class="unit-title icon-before icon-pseudo icon-qanda">Ask a Question</h3><span class="unit-subcaption">Get Answers from Experts and Locals</span></div>';
include( 'includes/ask_a_question.php' );
echo $after_widget;
}
}
/**
* RDC Featured Posts
*/
class realtor_featured_posts_widget extends WP_Widget {
protected $defaults;
function __construct() {
$this -> defaults = array(
'title' => 'Featured In ',
'num_posts' => '3',
'cat_title_placement' => 'after', // before, after, false
'show_title' => 'true',
'show_image' => 'true',
'show_cats' => 'true'
);
$widget_ops = array(
'classname' => 'featuredpost',
'description' => 'Display featured posts from the same categories'
);
$this->WP_Widget( 'realtor_featured_posts_widget', 'realtor.com Featured Posts', $widget_ops );
}
//build the widget settings form
function form($instance) {
$instance = wp_parse_args( (array) $instance, $this->defaults );
extract( $instance, EXTR_SKIP );
?>
<p>
<label>Title: <input class="widefat" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label>
</p>
<p>
<label>Place category name:
<select name="<?php echo $this->get_field_name( 'cat_title_placement' ); ?>" type="text">
<option value="after" <?=$cat_title_placement == 'after' ? 'selected="selected"' : ''?>>After title</option>
<option value="before" <?=$cat_title_placement == 'before' ? 'selected="selected"' : ''?>>Before title</option>
<option value="false" <?=$cat_title_placement == 'false' ? 'selected="selected"' : ''?>>Don't show</option>
</select>
</label>
</p>
<p>
<label>Number of posts to show:
<select name="<?php echo $this->get_field_name( 'num_posts' ); ?>" type="text">
<?
for($i = 0; $i < 11; $i++){
$selected = $num_posts === $i ? 'selected="selected"' : '';
echo "<option value='$i' $selected>$i</option>";
}
?>
</select>
</label>
</p>
<p>
<label>
<input name="<?php echo $this->get_field_name( 'show_title' ); ?>" type="checkbox" value="true" <?=$show_title == 'true' ? 'checked="checked"' : ''?>>
Show Title
</label>
</p>
<p>
<label>
<input name="<?php echo $this->get_field_name( 'show_image' ); ?>" type="checkbox" value="true" <?=$show_image == 'true' ? 'checked="checked"' : ''?>>
Show Image
</label>
</p>
<p>
<label>
<input name="<?php echo $this->get_field_name( 'show_cats' ); ?>" type="checkbox" value="true" <?=$show_cats == 'true' ? 'checked="checked"' : ''?>>
Show Categories
</label>
</p>
<?php
}
//save the widget settings
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = esc_attr( $new_instance['title'] );
$instance['num_posts'] = intval( $new_instance['num_posts'] );
$instance['cat_title_placement'] = esc_attr( $new_instance['cat_title_placement'] );
$instance['show_title'] = esc_attr( $new_instance['show_title'] );
$instance['show_image'] = esc_attr( $new_instance['show_image'] );
$instance['show_cats'] = esc_attr( $new_instance['show_cats'] );
return $instance;
}
//display the widget
function widget($args, $instance) {
if(!is_single() && !is_category())
return;
extract( $args, EXTR_SKIP );
/** Merge with defaults */
$instance = wp_parse_args( (array) $instance, $this->defaults );
// create a widget for each top level category
foreach ($this->getCategories() as $cat) {
// only show top level categories
if(isset($cat->category_parent) && $cat->category_parent !== '0')
return;
$widget_html = $this -> createWidgetOutput($args, $instance, $cat);
if($widget_html)
echo $widget_html;
}
}
function createWidgetOutput($args, $instance, $cat){
global $post;
extract( $args, EXTR_SKIP );
/* Merge with defaults */
$instance = wp_parse_args( (array) $instance, $this->defaults );
$output = $before_widget;
$title = $this -> getTitle($instance['title'], $cat->name, $instance['cat_title_placement']);
if (!empty($title))
$output .= $before_title . $title . $after_title;
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'editorials',
'field' => 'slug',
'terms' => 'featured-widget'
)
),
'post_type' => 'post',
'cat' => $cat->term_id,
'showposts' => $instance['num_posts'],
'orderby' => 'menu_order',
'order' => 'ASC'
);
// exclude the current post
if(is_single() && $post -> ID)
$args['post__not_in'] = array($post -> ID);
$featured_posts = new WP_Query($args);
// loop
if($featured_posts->have_posts()){
while($featured_posts->have_posts()){
$featured_posts->the_post();
// copied from genesis featured widget
$output .= '<div class="' . implode( ' ', get_post_class() ) . '">';
$image = genesis_get_image(array(
'format' => 'html',
'size' => 'single-slide-sm'
));
if ($instance['show_image'] === 'true' && $image)
$output .= sprintf('<a href="%s" title="%s" class="alignleft">%s</a>', get_permalink(), the_title_attribute('echo=0'), $image);
if ($instance['show_title'] === 'true')
$output .= sprintf('<h2><a href="%s" title="%s">%s</a></h2>', get_permalink(), the_title_attribute('echo=0'), get_the_title());
if ($instance['show_cats'] === 'true')
$output .= sprintf('<p class="post-meta">%s</p>', do_shortcode('[post_categories]'));
$output .= '</div><!--end post_class()-->'."\n\n";
}
}else{
return false;
}
$output .= $after_widget;
wp_reset_postdata();
return $output;
}
function getTitle($widgetTitle, $catName, $placement){
if($placement === 'after')
$title = $widgetTitle . ' ' . $catName;
elseif($placement === 'before')
$title = $catName . ' ' . $widgetTitle;
else
$title = $widgetTitle;
return apply_filters( 'widget_title', $title );
}
function getCategories(){
global $wp_query;
$cats = array();
if(is_category()){
$cat = $wp_query->get_queried_object();
if($cat)
$cats[] = $cat;
}elseif(is_single()){
$cats = get_the_category();
}
return $cats;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment