<?php
/**
 * Keyweb functions and definitions
 *
 */

function keyweb_setup() {
  
  add_theme_support( 'post-thumbnails' );
  add_image_size( 'half', 845, 500, true );
//  add_image_size( 'blog', 600, 380, true );


  register_nav_menus( array(
      'main' => __( 'Primary Menu',      'keyweb' ),
    ) );
}
add_action( 'after_setup_theme', 'keyweb_setup' );


if( function_exists('acf_add_options_page') ) {
 acf_add_options_page();
}


function keyweb_scripts_styles() {
  //global $wp_styles;

   wp_enqueue_style( 'reset', get_template_directory_uri() . '/css/reset.css');
   wp_enqueue_style( 'gfonts', 'https://fonts.googleapis.com/css?family=Lato:400,400italic,700italic,700');
   wp_enqueue_style( 'gfonts2', 'https://fonts.googleapis.com/css?family=Roboto+Slab:400,700,300');
   wp_enqueue_style( 'font', get_template_directory_uri() . '/css/font-awesome.css');
   wp_enqueue_style( 'lightbox', get_template_directory_uri() . '/css/lightbox.css');
   wp_enqueue_style( 'keyweb-style', get_stylesheet_uri() );
   wp_enqueue_style( 'programing', get_template_directory_uri() . '/css/programing.css');
   wp_enqueue_style( 'responsive', get_template_directory_uri() . '/css/responsive.css');
   wp_enqueue_style( 'keyweb', get_template_directory_uri() . '/css/keyweb.css');

  wp_enqueue_script( 'boot', get_template_directory_uri() . '/js/bootstrap.min.js');
  //wp_enqueue_script( 'lightbox', get_template_directory_uri() . '/js/lightbox.js' );
  wp_enqueue_script( 'template', get_template_directory_uri() . '/js/template.js' );
  if( is_page_template('page-contact.php') ){
    wp_enqueue_script( 'google', 'https://maps.googleapis.com/maps/api/js?key=AIzaSyD83umzPyXeeqgmPjMpRdmm0C25UF4jdv0&v=3.exp' );
  }
}
add_action( 'wp_enqueue_scripts', 'keyweb_scripts_styles' );


/*-----------------------------------------------------Post types-------------------------------------------*/

 add_action( 'init', 'create_post_type' );
  function create_post_type() {

    register_post_type( 'announcements', array(
      'label' => 'Announcements',
      'singular_label' => 'Announcement',
      'description' => 'For the announcements',
      'public' => TRUE,
      'exclude_from_search' => false,
      'publicly_queryable' => TRUE,
      'show_ui' => TRUE,
      'query_var' => TRUE,
      'rewrite' => TRUE,
      'capability_type' => 'post',
      'hierarchical' => FALSE,
      'menu_position' => NULL,
      'menu_icon'=> 'dashicons-lightbulb',
      //'taxonomy' => array('project_category'),
      'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array(
        'slug' => 'announcements',
        'with_front' => FALSE,
      ),
    ));

    register_post_type( 'amenity', array(
      'label' => 'Amenities',
      'singular_label' => 'Amenity',
      'description' => 'For the amenity',
      'public' => TRUE,
      'exclude_from_search' => false,
      'publicly_queryable' => TRUE,
      'show_ui' => TRUE,
      'query_var' => TRUE,
      'rewrite' => TRUE,
      'capability_type' => 'post',
      'hierarchical' => FALSE,
      'menu_position' => NULL,
      'menu_icon'=> 'dashicons-store',
      //'taxonomy' => array('project_category'),
      'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array(
        'slug' => 'amenity',
        'with_front' => FALSE,
      ),
    ));

    register_post_type( 'cta', array(
      'label' => 'Call to actions',
      'singular_label' => 'Call to action',
      'description' => 'For the cta',
      'public' => TRUE,
      'exclude_from_search' => false,
      'publicly_queryable' => TRUE,
      'show_ui' => TRUE,
      'query_var' => TRUE,
      'rewrite' => TRUE,
      'capability_type' => 'post',
      'hierarchical' => FALSE,
      'menu_position' => NULL,
      'menu_icon'=> 'dashicons-sticky',
      'supports' => array('title', 'editor', 'thumbnail'),
      'rewrite' => array(
        'slug' => 'cta',
        'with_front' => FALSE,
      ),
    ));

    register_post_type( 'faq', array(
      'label' => 'Faqs',
      'singular_label' => 'Faq',
      'description' => 'For the faq',
      'public' => TRUE,
      'exclude_from_search' => false,
      'publicly_queryable' => TRUE,
      'show_ui' => TRUE,
      'query_var' => TRUE,
      'rewrite' => TRUE,
      'capability_type' => 'post',
      'hierarchical' => FALSE,
      'menu_position' => NULL,
      'menu_icon'=> 'dashicons-info',
      'supports' => array('title', 'editor'),
      'rewrite' => array(
        'slug' => 'faq',
        'with_front' => FALSE,
      ),
    ));

  }

  

/*-----------------------------------------------------Witget-------------------------------------------*/

function keyweb_widgets_init() {

  register_sidebar( array(
    'name'          => __( 'Blog Sidebar', 'keyweb' ),
    'id'            => 'sidebar-1',
    'description'   => __( 'Main sidebar that appears on the left.', 'keyweb' ),
    'before_widget' => '<div id="%1$s" class="widget blog-witget %2$s">',
    'after_widget'  => '</div>',
    'before_title'  => '<h4 class="widget-title menu-title">',
    'after_title'   => '</h4>',
  ) );

  register_sidebar( array(
    'name'          => __( 'Footer Sidebar left column', 'keyweb' ),
    'id'            => 'sidebar-2',
    'description'   => __( 'Footer sidebar that appears above footer.', 'keyweb' ),
    'before_widget' => '<div id="%1$s" class="%2$s widget">',
    'after_widget'  => '</div>',
    'before_title'  => '<h3 class="widget-title menu-title">',
    'after_title'   => '</h3>',
  ) );
  register_sidebar( array(
    'name'          => __( 'Footer Sidebar center column', 'keyweb' ),
    'id'            => 'sidebar-3',
    'description'   => __( 'Footer sidebar that appears above footer.', 'keyweb' ),
    'before_widget' => '<div id="%1$s" class="%2$s widget">',
    'after_widget'  => '</div>',
    'before_title'  => '<h3 class="widget-title menu-title">',
    'after_title'   => '</h3>',
  ) );
  register_sidebar( array(
    'name'          => __( 'Footer Sidebar right column', 'keyweb' ),
    'id'            => 'sidebar-4',
    'description'   => __( 'Footer sidebar that appears above footer.', 'keyweb' ),
    'before_widget' => '<div id="%1$s" class="%2$s widget">',
    'after_widget'  => '</div>',
    'before_title'  => '<h3 class="widget-title menu-title">',
    'after_title'   => '</h3>',
  ) );
}
add_action( 'widgets_init', 'keyweb_widgets_init' );

/*-----------------------------------------------------Excerpt-------------------------------------------*/

function new_excerpt_more($more) {  
    return '...';  
}  
add_filter('excerpt_more', 'new_excerpt_more');  


function clever_excerpt( $charlength=120, $excerpt='' ){
    if($excerpt==''){ $excerpt = get_the_excerpt(); }
    $charlength++;

//    echo '<p>';
    if ( mb_strlen( $excerpt ) > $charlength ) {
        $subex = mb_substr( $excerpt, 0, $charlength - 5 );
        $exwords = explode( ' ', $subex );
        $excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
        if ( $excut < 0 ) {
            echo mb_substr( $subex, 0, $excut );
        } else {
            echo $subex;
        }
        echo '...';
    } else {
        echo $excerpt;
    }
//    echo '</p>';
}

function check_length($charlength=200, $excerpt=''){
  $excerpt= apply_filters('the_content', get_the_content());
  if ( mb_strlen( $excerpt ) > $charlength ) {
    return true;
  } 
  return false;
}

/** Remove width & height images attributes */
function remove_images_width_height($content) {
    $content = preg_replace('/<img(.*?)(width=\"\d+\")(.*?)>/iu', "<img$1 $3>", $content);
    $content = preg_replace('/<img(.*?)(height=\"\d+\")(.*?)>/iu', "<img$1 $3>", $content);
    return $content;
}
add_filter('the_content', 'remove_images_width_height');
add_filter('post_thumbnail_html', 'remove_images_width_height');

/*-----------------------------------------------------Announcements-------------------------------------------*/

function announcements_header($anns){ ?>
  <div class="panel-body">
            <?php while( $anns->have_posts() ): $anns->the_post(); ?>
            <div class="ann-post">
              <p><a href="<?php the_field('announcements_page', 'options'); echo '?id='.get_the_ID();//the_permalink(); ?>"><?php the_title(); ?></a> - <?php clever_excerpt(80); ?></p> 
              <div class="post-meta">Posted <?php the_time('M d, Y'); ?></div>
            </div>
          <?php endwhile; ?>

            <div class="text-center">
              <a href="<?php the_field('announcements_page', 'options');?>">View All</a>
            </div>
          </div>
<?php }

if( !function_exists('wp_get_attachment_image_url')){
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
  $image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
  return isset( $image['0'] ) ? $image['0'] : false;
}
}
/*-----------------------------------------------------Shortcodes-------------------------------------------*/

function list_shortcode ($atts, $content = null) {  
    // Get & Set Defaults
    $atts = shortcode_atts(array(
        "orderby" => 'date',
        "order" => 'DESC',
        "how" => -1,
        'type' => 'post',
        'meta' => ''
    ), $atts);
    extract($atts);
    $output = '';

    $args = array(
      'post_type' => $type,
      'post_status' => 'publish',
      'showposts' => $how,
      'orderby' => $orderby,
      'order' => $order
      );
    if($orderby=='custom'){
      $args['orderby']='meta_value_num';
      $args['meta_key'] = $meta;
    }
    $atts = new WP_Query($args);

        ob_start();
    if($atts->have_posts()):?>
      <?php 
      $i=1; 
      while($atts->have_posts()):
        $atts->the_post(); 
          get_template_part('content', get_post_type(get_the_ID())); ?>
        <?php $i++;
      endwhile;
      if( function_exists('wp_pagenavi') && $how>0 ){
        wp_pagenavi();
      }
      ?>
          
    <?php endif;
    
    $output .= ob_get_clean();
    return $output;
    
}
add_shortcode('list','list_shortcode');

function phone_shortcode ($atts, $content = null) {  
    return '<a href="tel:'.get_field('phone', 'options').'">'.get_field('phone', 'options').'</a>';
    
}
add_shortcode('phone','phone_shortcode');

function address_shortcode ($atts, $content = null) {  
    $output = '<a href="http://maps.google.com/?q='.get_field('address', 'options').'" target="_blank">'.get_field('address', 'options').'</a>';
    return $output;
    
}
add_shortcode('address','address_shortcode');

function fax_shortcode ($atts, $content = null) {  
    $output = '<a href="tel:'.get_field('fax', 'options').'">'.get_field('fax', 'options').'</a>';
    return $output;
    
}
add_shortcode('fax','fax_shortcode');

function email_shortcode ($atts, $content = null) {  
    $output = '<a href="mailto:'.get_field('email', 'options').'">'.get_field('email', 'options').'</a>';
    return $output;
    
}
add_shortcode('email','email_shortcode');

function pemail_shortcode ($atts, $content = null) {  
    $output = '<a href="mailto:'.get_field('pool_email', 'options').'">'.get_field('pool_email', 'options').'</a>';
    return $output;
    
}
add_shortcode('pemail','pemail_shortcode');

function poolphone_shortcode ($atts, $content = null) {  
    $output = '<a href="tel:'.get_field('pool_phone', 'options').'">'.get_field('pool_phone', 'options').'</a>';
    return $output;
    
}
add_shortcode('poolphone','poolphone_shortcode');

add_filter('widget_text', 'do_shortcode');


/*---------------------------------------------------Events------------------------------------------------------*/

add_filter( 'tribe_get_events_title', 'tribe_title', 50, 1 );

function tribe_title($title, $depth=true){

  $events_label_plural = tribe_get_event_label_plural();

    global $wp_query;
    //print_r($wp_query);

    $tribe_ecp = Tribe__Events__Main::instance();

    $title = sprintf( esc_html__( 'Upcoming %s', 'the-events-calendar' ), $events_label_plural );

    // If there's a date selected in the tribe bar, show the date range of the currently showing events
    if ( isset( $_REQUEST['tribe-bar-date'] ) && $wp_query->have_posts() ) {
      echo '1111111111111111';
      $first_returned_date = tribe_get_start_date( $wp_query->posts[0], false, Tribe__Date_Utils::DBDATEFORMAT );
      $first_event_date    = tribe_get_start_date( $wp_query->posts[0], false );
      $last_event_date     = tribe_get_end_date( $wp_query->posts[ count( $wp_query->posts ) - 1 ], false );

      // If we are on page 1 then we may wish to use the *selected* start date in place of the
      // first returned event date
      if ( 1 == $wp_query->get( 'paged' ) && $_REQUEST['tribe-bar-date'] < $first_returned_date ) {
        $first_event_date = tribe_format_date( $_REQUEST['tribe-bar-date'], false );
      }

      $title = sprintf( __( '%1$s for %2$s - %3$s', 'the-events-calendar' ), $events_label_plural, $first_event_date, $last_event_date );
    } elseif ( tribe_is_past() ) {
      $title = sprintf( esc_html__( 'Past %s', 'the-events-calendar' ), $events_label_plural );
    }

    if ( tribe_is_month() ) {
      $title = sprintf(
        esc_html__( '%1$s', 'the-events-calendar' ),
        date_i18n( tribe_get_date_option( 'monthAndYearFormat', 'F Y' ), strtotime( tribe_get_month_view_date() ) )
      );
    }

    // day view title
    if ( tribe_is_day() ) {
      $title = sprintf(
        esc_html__( '%1$s', 'the-events-calendar' ),
        date_i18n( 'F dS, Y', strtotime( $wp_query->get( 'start_date' ) ) )
      );
    }

    if ( is_tax( $tribe_ecp->get_event_taxonomy() ) && $depth ) {
      $cat = get_queried_object();
      $title = '<a href="' . esc_url( tribe_get_events_link() ) . '">' . $title . '</a>';
      $title .= ' &#8250; ' . $cat->name;
    }
   return $title;
}

