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

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function attorneypress_body_classes( $classes ) {
	global $post;
	if ( get_page_by_path( 'attorneys', OBJECT, 'page' ) ) {
		$attorney_page_info = get_page_by_path( 'attorneys', OBJECT, 'page' );
		$attorney_page_id   = $attorney_page_info->ID;
	}

	if ( get_page_by_path( 'practice-areas', OBJECT, 'page' ) ) {
		$practice_areas_page_info = get_page_by_path( 'practice-areas', OBJECT, 'page' );
		$practice_areas_page_id   = $practice_areas_page_info->ID;
	}

	$posts_page_id = get_option( 'page_for_posts' );
	$frontpage_id = get_option( 'page_on_front' );

	if ( is_singular( 'attorneys' ) || is_tax( 'profession' ) ) {
		if ( 'breadcumbsonly' == get_post_meta( $attorney_page_id, 'bannercheck', true ) || 'nobannerbreadcumbs' == get_post_meta( $attorney_page_id, 'bannercheck', true ) ) {
			$classes[] = 'no-inner-banner';
		}
	} elseif ( is_front_page() ) {
		if ( 'breadcumbsonly' == get_post_meta( $frontpage_id, 'bannercheck', true )) {
			$classes[] = 'no-inner-banner';
		}
	} elseif ( is_search() || is_home() || is_category() || is_archive() || is_tag() || is_singular( 'post' ) ) {
		if ( 'breadcumbsonly' == get_post_meta( $posts_page_id, 'bannercheck', true ) || 'nobannerbreadcumbs' == get_post_meta( $posts_page_id, 'bannercheck', true ) ) {
			$classes[] = 'no-inner-banner';
		}
	} elseif ( is_singular( 'practice-areas' ) ) {
		if ( 'breadcumbsonly' == get_post_meta( $practice_areas_page_id, 'bannercheck', true ) || 'nobannerbreadcumbs' == get_post_meta( $practice_areas_page_id, 'bannercheck', true ) ) {
			$classes[] = 'no-inner-banner';
		}
	} elseif ( ! is_404() ) {
		if ( ( 'breadcumbsonly' == get_post_meta( $post->ID, 'bannercheck', true ) || 'nobannerbreadcumbs' == get_post_meta( $post->ID, 'bannercheck', true ) ) && ( ! is_front_page() ) && ( ! is_404() ) ) {
			$classes[] = 'no-inner-banner';
		}
	}
	return $classes;
}

add_filter( 'body_class', 'attorneypress_body_classes' );

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function attorneypress_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}

add_action( 'wp_head', 'attorneypress_pingback_header' );

/**
 * Custom function for the callback validation of Redux Framework
 */
if ( ! function_exists( 'attorneypress_excerpt_length_limit' ) ) :

	/**
	 * [attorneypress_excerpt_length_limit description]
	 *
	 * @param  [type] $field          description.
	 * @param  [type] $value          description.
	 * @param  [type] $existing_value description.
	 * @return [type]                 [description]
	 */
	function attorneypress_excerpt_length_limit( $field, $value, $existing_value ) {

		$error = false;

		if ( ! is_numeric( $value ) ) {
			$error           = true;
			$value           = $existing_value;
			$field['msg']  = esc_html__( 'You must provide a numerical value for this option.', 'attorneypress' );
		} elseif ( is_numeric( $value ) && $value > 72 ) {
			$error           = true;
			$value           = $existing_value;
			$field['msg']  = esc_html__( 'You must provide less than or equal to 72 for this option.', 'attorneypress' );
		}

		$return['value'] = $value;

		if ( true === $error ) {
			$return['error'] = $field;
		}
		return $return;
	}


endif;
