<file_sep><?php
/**
Plugin Name: <NAME>'s WP Widgets & Shit.
Plugin URI: http://randydaniel.me
Description: Custom theme widgets & shit.
Version: 1.0
Author: <NAME>
Author URI: http://randydaniel.me
*/




// Add class (img-responsive) to get_avatar reference, used in agent/broker widget.
add_filter('get_avatar','change_avatar_css');

  function change_avatar_css($class) {
    $class = str_replace("class='avatar", "class='avatar img-responsive ", $class) ;
  return $class;
}




// Creating the agent/broker widget
class rdwp_widget_broker extends WP_Widget {

/** Widget Setup (Constructor Form) **/
  function __construct() {
  parent::__construct(
    // Base ID of your widget
    'rdwp_widget_broker',

    // Widget name will appear in UI
    __('Broker Information', 'rdwp_widget_broker_domain'),

    // Widget description
    array( 'description' => __( 'Display broker information for current &amp; similar listings. (Useful ONLY in "Property Listing" sidebar.)', 'rdwp_widget_broker_domain' ), )
    );
  }




  // Creating widget front-end
  // This is where the action happens
  public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );

    // before and after widget arguments are defined by themes
    echo $args['before_widget'];
      if ( ! empty( $title ) )
    echo $args['before_title'] . $title . $args['after_title'];

    // This is where you run the code and display the output
    echo '<div id="agent-info">';

      echo get_avatar( get_the_author_meta( 'ID' ) );
      echo '<h4>' . get_the_author_meta('display_name') . '</h4>';
      // #widget-broker

    echo '</div>';
    echo '<ul class="well">';

      if( get_the_author_meta('twitter') ):
        echo '<li><i class="fa fa-twitter"></i>&nbsp;<a href="http://twitter.com/' . get_the_author_meta('twitter') . ' ">' . '@' . get_the_author_meta('twitter') . '</a></li>';
      endif;

      if( get_the_author_meta('user_email') ):
        echo '<li><i class="fa fa-envelope-o"></i>&nbsp;<a href="mailto:' . get_the_author_meta('user_email') . ' ">' . get_the_author_meta('user_email') . '</a></li>';
      endif;

      if( get_the_author_meta('phone') ):
        echo '<li><i class="fa fa-phone"></i>&nbsp;' . get_the_author_meta('phone') . '</li>';
      endif;

    echo '</ul>';

    if( get_theme_mod( 'crossings_brand_image', 'value' ) ):
      echo '<div id="broker-info">';
        echo '<a href="' . home_url() . '"><img src="' . get_theme_mod( 'crossings_brand_image', 'value' ) . ' " class="img-responsive"></a>';
    endif;

    echo $args['after_widget'];

  }




  // Widget Backend
  public function form( $instance ) {

  if ( isset( $instance[ 'title' ] ) ) {
    $title = $instance[ 'title' ];
  }

  else {
    $title = __( 'Agent Information', 'rdwp_widget_broker_domain' );
  }

  // Widget admin form
  ?>

  <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
    <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
  }

  // Updating widget replacing old instances with new
  public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
      return $instance;
  }

} // Class rdwp_widget_broker ends here




// ***********************************************************************************************************



// Custom excerpt length
function custom_excerpt_length( $length ) {
  return 10;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );




// Add 'read more' link to excerpt
function new_excerpt_more( $more ) {
  return ' <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">' . __('[...]', 'your-text-domain') . '</a>';
}
add_filter( 'excerpt_more', 'new_excerpt_more' );




// Creating the recent posts widget
class rdwp_widget_recent extends WP_Widget {

/** Widget Setup (Constructor Form) **/
  function __construct() {
  parent::__construct(
    // Base ID of your widget
    'rdwp_widget_recent',

    // Widget name will appear in UI
    __('Recent Listings', 'rdwp_widget_recent_domain'),

    // Widget description
    array( 'description' => __( 'Display (3) recent property listings', 'rdwp_widget_recent_domain' ), )
    );
  }




  // Creating widget front-end
  // This is where the action happens
  public function widget( $args, $instance ) {
    $title = apply_filters( 'widget_title', $instance['title'] );

    // before and after widget arguments are defined by themes
    echo $args['before_widget'];
      if ( ! empty( $title ) )
    echo $args['before_title'] . $title . $args['after_title'];

    // This is where you run the code and display the output

    $the_query = new WP_Query( 'showposts=3' );
      while ($the_query -> have_posts()) : $the_query -> the_post();
        echo '<div class="featured-property">';

          if (has_post_thumbnail( $post->ID ) ):
            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
          echo '<a href="';
            echo the_permalink();
          echo '"><img src="' . $image[0] . '" class="img-responsive" />';
          endif;

          echo '<a href="';
            echo the_permalink();
          echo '">';
            echo '<h4>';
            echo the_title();
            echo '</h4></a>';

          echo the_excerpt();

          echo '<div class="featured-details row">';
            echo '<div class="col-xs-4">';
              echo the_field('bedrooms') . '&nbsp;BR</div>';
            echo '<div class="col-xs-4">';
              echo the_field('bathrooms') . '&nbsp;BA</div>';
            echo '<div class="col-xs-4">';
              $category = get_the_category();
              echo $category[0]->cat_name . '</div>';
          echo '</div>';

        echo '</div>';

      endwhile;

    echo $args['after_widget'];

  }




  // Widget Backend
  public function form( $instance ) {

  if ( isset( $instance[ 'title' ] ) ) {
    $title = $instance[ 'title' ];
  }

  else {
    $title = __( 'Recent Listings', 'rdwp_widget_recent_domain' );
  }

  // Widget admin form
  ?>

  <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
    <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
  }

  // Updating widget replacing old instances with new
  public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
      return $instance;
  }

} // Class rdwp_widget_recent ends here




// Register and load the widgets
function rdwp_load_widget() {
  register_widget( 'rdwp_widget_broker' );
  register_widget( 'rdwp_widget_recent' );
}
add_action( 'widgets_init', 'rdwp_load_widget' );





// Add + Remove Profile Fields to User Form
function rdwp_user_fields($profile_fields) {

  // Add new fields
  $profile_fields['twitter'] = 'Twitter Handle/Username';
  $profile_fields['phone'] = 'Phone Number';

  return $profile_fields;

}
add_filter('user_contactmethods', 'rdwp_user_fields');




// Load CSS & JS
function rdwp_post_gallery_cssjs() {
  $plugin_url = plugin_dir_url( __FILE__ );

  // CSS
  wp_enqueue_style( 'rdwp-style', $plugin_url . 'assets/css/style.css', array(), '1.0', 'all' );
  wp_enqueue_style( 'fa-style', $plugin_url . 'assets/css/font-awesome.min.css', array(), '4.0.3', 'all' );

  // JS (Boolean = include in footer?)
  wp_enqueue_script( 'rdwp-js',  $plugin_url . 'assets/js/app.js', array( 'jquery' ), '1.0', true );
}

add_action( 'wp_enqueue_scripts', 'rdwp_post_gallery_cssjs' );

?>
