<?php

/**
 * 
 * Development Functions
 * http://4nton.com/
 * Anthony Carbon
 * Revision 0.0.4
 * 
 **/
 
if ( ! defined( 'ABSPATH' ) ){ exit; }

if ( ! function_exists( 'wpjoin' ) ){
	function wpjoin( $array, $separator = ", ", $echo = false ){
		if( $echo == true ){
			echo join( "{$separator}", array_filter( (array) $array ) );
			return;
		}
	   	return join( "{$separator}", array_filter( (array) $array ) );
	}
}

if ( ! function_exists( 'wpstring' ) ){
	function wpstring( $string, $echo = false, $separator = "-" ){
		$string = strtolower( $string );
		$string = str_replace( ' ', '-', $string );
		$string = preg_replace( '/[^A-Za-z0-9\-]/', '', $string );
		if( $echo == true ){
			echo preg_replace( '/-+/', $separator, $string );
			return;
		}
	   	return preg_replace( '/-+/', $separator, $string );
	}
}

if ( ! function_exists( 'wpimgsrc' ) ){
	function wpimgsrc( $thumbnail_id = 0, $size = 'full'  ){
		$image = array();
		if( $thumbnail_id ){
			$image = wp_get_attachment_image_src( $thumbnail_id, $size );
		}else{
			$image = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), $size );
		}
		if( ! $image ){
			return;
		}
		if( $image ){
			return wpkeyvalue( $image, 0 );
		}
		return;
	}
}

if ( ! function_exists( 'wpifisset' ) ){
	function wpifisset( $var, $key  ){
		if( isset( $var[$key] ) ){
			return true;
		}
		return false;
	}
}

if ( function_exists( 'wc' ) && ! function_exists( 'wcaddedtocart' ) ){
	function wcaddedtocart( $id ){
		$carts = wc()->cart->get_cart();
		if( $carts ){
			foreach( $carts as $cart ){
				if( $id == wpkeyvalue( $cart, 'product_id' ) ) {
					return true;
				}
			}
		}
		return false;
	}
}

if ( ! function_exists( 'wpserializetoarray' ) ){
	function wpserializetoarray( $key, $user_id = 0, $method = 'user' ){
		$user_meta = get_user_meta( get_current_user_id(), $key, true );
		if( $user_id ){
			$user_meta = get_user_meta( $user_id, $key, true );
		}
		$output = (array) $user_meta;
		return array_filter( $output );
	}
}

if ( ! function_exists( 'wpstdclass' ) ){
	function wpstdclass( $var, $level1, $level2 = false, $level3 = false ) {
		if( empty( $var->$level1 ) && !$level2 ){
			return;
		}
		if( empty( $var->$level1->$level2 ) && $level2 ){
			return;
		}
		if( ! empty( $var->$level1->$level2 ) && $level2 ){
			return $var->$level1->$level2;
		}
		if( empty( $var->$level1->$level2->$level3 ) && $level3 ){
			return;
		}
		if( ! empty( $var->$level1->$level2->$level3 ) && $level3 ){
			return $var->$level1->$level2->$level3;
		}
		return $var->$level1;
	}
}

if ( ! function_exists( 'wpkeyvalue' ) ){
	function wpkeyvalue( $var, $key, $echo = false ){
		if( isset( $var[$key] ) && ! $echo ){
			return $var[$key];
		}
		if( isset( $var[$key] ) && $echo ){
			echo $var[$key];
		}
		return;
	}
}

if ( ! function_exists( 'wpget' ) ){
	function wpget( $key, $echo = false, $sanitize = false ){
		if( isset( $_GET[$key] ) && ! $echo ){
			return wpsanitizeswitch( $_GET[$key], $sanitize );
		}
		if( isset( $_GET[$key] ) && $echo ){
			echo wpsanitizeswitch( $_GET[$key], $sanitize );
		}
		return;
	}
}

if ( ! function_exists( 'wprequest' ) ){
	function wprequest( $key, $echo = false, $sanitize = false ){
		if( isset( $_REQUEST[$key] ) && ! $echo ){
			return wpsanitizeswitch( $_REQUEST[$key], $sanitize );
		}
		if( isset( $_REQUEST[$key] ) && $echo ){
			echo wpsanitizeswitch( $_REQUEST[$key], $sanitize );
		}
		return;
	}
}

if ( ! function_exists( 'wpsanitizeswitch' ) ){
	function wpsanitizeswitch( $output, $sanitize = false ){
		switch( $sanitize ){
			case 'email':
				return sanitize_email( $output );
				break;
			case 'file_name':
				return sanitize_file_name( $output );
				break;
			case 'html_class':
				return sanitize_html_class( esc_attr( $output ) );
				break;
			case 'key':
				return sanitize_key( $output );
				break;
			case 'title':
				return sanitize_title( $output );
				break;
			case 'title_for_query':
				return sanitize_title_for_query( $output );
				break;
			case 'title_with_dashes':
				return sanitize_title_with_dashes( $output );
				break;
			case 'user':
				return sanitize_user( $output );
				break;
			case 'textarea_field':
				return sanitize_textarea_field( $output );
				break;
			default:
				return sanitize_text_field( $output );
		}
	}
}

if ( ! function_exists( 'wppost' ) ){
	function wppost( $key, $echo = false, $sanitize = 'text_field' ){
		if( isset( $_POST[$key] ) && ! $echo ){
			return wpsanitizeswitch( $_POST[$key], $sanitize );
		}
		if( isset( $_POST[$key] ) && $echo ){
			echo wpsanitizeswitch( $_POST[$key], $sanitize );
		}
		return;
	}
}

if ( ! function_exists( 'wprequest' ) ){
	function wprequest( $key, $echo = false, $sanitize = 'text_field' ){
		if( isset( $_REQUEST[$key] ) && ! $echo ){
			return wpsanitizeswitch( $_REQUEST[$key], $sanitize );
		}
		if( isset( $_REQUEST[$key] ) && $echo ){
			echo wpsanitizeswitch( $_REQUEST[$key], $sanitize );
		}
		return;
	}
}