<?php
/**
 * Custom functions that act independently of the theme templates
 *
 * Eventually, some of the functionality here could be replaced by core features
 *
 * @package Youplay
 */

if ( ! function_exists( 'yp_sanitize_class' ) ) :
function yp_sanitize_class($classes) {
    if (!is_array($classes)) {
        $classes = explode(' ', $classes);
    }

    foreach($classes as $k => $v){
        $classes[$k] = sanitize_html_class($v);
    }

    return join(' ', $classes);

    return $classes;
}
endif;


/**
 * Fix Content for Shortcodes
 */
function yp_fix_content($content) {
    // fix for stupid </p> tag in start of string
    if (substr($content, 0, strlen("</p>")) == "</p>") {
        $content = substr($content, strlen("</p>"));
    }

    // fix for stupid <p> tag in end of string
    $content = preg_replace('/<p>$/', '', $content);

    // remove some <br> tags near shortcodes
    $content = str_replace( "]<br />","]", ( substr( $content, 0 , 6 ) == "<br />" ? substr( $content, 6 ): $content ) );

    // remove some <p> tags near shortcodes
    $content = str_replace( "]</p>","]", $content );
    $content = str_replace( "<p>[","[", $content );

    return $content;
}

/**
 * Get Rating HTML
 */
function yp_get_rating($rating) {
    if(( yp_check($rating) || $rating == 0) && is_numeric($rating)) {
        $rating_tmp = "";

        // ceil num
        $r_rating = ceil($rating/0.5)*0.5;

        for($k = 1; $k <= 5; $k++) {
            if($k <= $r_rating) {
                $rating_tmp .= ' <i class="fa fa-star"></i>';
            } else {
                if($k - 0.5 == $r_rating) {
                    $rating_tmp .= ' <i class="fa fa-star-half-o"></i>';
                } else {
                    $rating_tmp .= ' <i class="fa fa-star-o"></i>';
                }
            }
        }

        return '<div class="rating" data-rating="' . esc_attr($rating) . '">' . $rating_tmp . '</div>';
    } else {
        return "";
    }
}

/**
 * Get avatar URL
 * http://wordpress.stackexchange.com/questions/59442/how-do-i-get-the-avatar-url-instead-of-an-html-img-tag-when-using-get-avatar
 */
function yp_get_avatar_url($get_avatar){
    preg_match("/src='(.*?)'/i", $get_avatar, $matches);
    return esc_url($matches[1]);
}

/* HEX to RGB colors */
/* http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/ */
function hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}

/**
 * Maintenance Mode
 */
function youplay_is_maintenance() {
    $maintenance = yp_opts('maintenance');
    $except_admin = yp_opts('maintenance_except_admin');

    if($maintenance && $except_admin && (is_admin() || is_super_admin())) {
        $maintenance = false;
    }
    return $maintenance;
}

add_action( 'template_redirect', 'youplay_maintenance_redirect' );
function youplay_maintenance_redirect() {
  if (youplay_is_maintenance()) {
    require get_template_directory() . '/maintenance.php';
    exit();
  }
}

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function youplay_body_classes( $classes ) {
	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

  foreach($classes as $key => $class) {
      if($class == 'date') {
          $classes[$key] = 'archive-date';
      }
  }

	return $classes;
}
add_filter( 'body_class', 'youplay_body_classes' );


/**
 * Title Tag filter for 404
 */
add_filter( 'wp_title', 'yp_title', 10, 2 );
function yp_title( $title ) {
    if( is_404() ) {
        $title = yp_opts('404_title');
    }
    return $title;
}


/**
 * Add 'full' to sizes list
 */
add_filter( 'intermediate_image_sizes', 'yp_intermediate_image_sizes' );
function yp_intermediate_image_sizes( $sizes ) {
    $sizes[] = 'full';
    return $sizes;
}


/**
 * Remove admin bar top margin
 */
add_action('get_header', 'remove_admin_login_header');
function remove_admin_login_header() {
    remove_action('wp_head', '_admin_bar_bump_cb');
}


/**
 * Add active classname for menu item
 */
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
     if( in_array('current-menu-item', $classes) || in_array('current-menu-ancestor', $classes) ){
          $classes[] = 'active ';
     }
     return $classes;
}


/**
 * Responsive video embed
 */
add_filter( 'embed_oembed_html', 'yp_oembed_filter', 10, 4 ) ;
function yp_oembed_filter($html, $url, $attr, $post_ID) {
    $return = '<div class="responsive-embed responsive-embed-16x9">' . $html . '</div>';
    return $return;
}