<?php 
/**
 * @package WordPress
 * @subpackage Campoal
 */

/**
* Get assign page link
*
* @since Campoal 1.9.9
*/
if( !function_exists('conikal_get_assign_page') ):
	function conikal_get_assign_page($page_assign, $endpoint = '') {
		if (empty($page_assign)) {
			return;
		}
		global $campoal_options;

		$page = isset($campoal_options[$page_assign]) ? $campoal_options[$page_assign] : 0;

		if (!empty($page)) {
			$result = array(
				'id'	=> $page,
				'title' => get_the_title($page),
				'link'	=> get_permalink($page) . $endpoint,
			);

       		$result = (object) $result;
		} else {
			$result = false;
		}

		return $result;
    }
endif;

/**
* Formatting phone number
*
* @since Campoal 1.9.9
*/
if( !function_exists('conikal_format_phone_number') ):
	function conikal_format_phone_number($phone) {
		if (empty($phone)) {
			return false;
		}

		$phone = str_replace(array('(', ')', '-', '+', ' '), '', $phone);
        $phone = ltrim(intval($phone), '0');

		return $phone;
	}
endif;

/**
* Page alignment class
*
* @since Campoal 1.9.9
*/
if( !function_exists('conikal_page_alignment_class') ) :
	function conikal_page_alignment_class() {
		global $campoal_options;

		$page_title_aligment = isset($campoal_options['conikal_page_title_aligment_field']) ? $campoal_options['conikal_page_title_aligment_field'] : 'center';

		switch ($page_title_aligment) {
			case 'left':
				$page_title_aligment_class = 'alignleft';
				break;
		
			case 'right':
				$page_title_aligment_class = 'alignright';
				break;
			
			default:
				$page_title_aligment_class = 'aligncenter';
				break;
		}

		return $page_title_aligment_class;
	}
endif;


/**
* Get post type name
*
* @since Campoal 1.9.9
*/
if( !function_exists('conikal_get_post_type_name') ) :
	function conikal_get_post_type_name($id = false, $post_type = false) {
		if (!empty($id)) {
			$post_type = get_post_type($id);
		}

		switch ($post_type) {
			case 'post':
				$post_type_name = esc_html__('Post', 'campoal');
				break;

			case 'page':
				$post_type_name = esc_html__('Page', 'campoal');
				break;
		
			case 'petition':
				$post_type_name = esc_html__('Petition', 'campoal');
				break;
			
			case 'update':
				$post_type_name = esc_html__('Update', 'campoal');
				break;

			case 'give_forms':
				$post_type_name = esc_html__('Donation', 'campoal');
				break;

			case 'team':
				$post_type_name = esc_html__('Member', 'campoal');
				break;

			case 'testimonials':
				$post_type_name = esc_html__('Review', 'campoal');
				break;

			case 'decisionmakers':
				$post_type_name = esc_html__('Decision maker', 'campoal');
				break;

			case 'citation':
				$post_type_name = esc_html__('Citation', 'campoal');
				break;

			case 'job_listing':
				$post_type_name = esc_html__('Job', 'campoal');
				break;

			case 'product':
				$post_type_name = esc_html__('Product', 'campoal');
				break;

			case 'docs':
				$post_type_name = esc_html__('Document', 'campoal');
				break;
			
			default:
				$post_type_name = ucfirst(strtolower($post_type));
				break;
		}

		return $post_type_name;
	}
endif;


/**
* Check WooCommerce class exist or not and check woocommerce pages
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_check_woocommerce_pages')) :
    function conikal_check_woocommerce_pages($page = false, $endpoint = false) {
        if (class_exists('WooCommerce') && function_exists('conikal_register_petition_type')) {
			if (empty($page) && !empty($endpoint)) {
                return is_wc_endpoint_url($endpoint);
            } else if ($page == 'product' && empty($endpoint)) {
                if (is_product()) {
                    return true;
                } else {
                    return false;
                }
            } else if ( empty($page) && empty($endpoint)) {
				if (is_shop() || is_product_category()) {
					return false;
				} else if (is_product() || is_woocommerce() || is_cart()) {
                    return true;
                } else {
                    return false;
                }
            } else {
				if (is_account_page() && is_user_logged_in()) {
					return true;
				} else {
					return false;
				}
            }
        } else if (class_exists('WooCommerce')) {
            if (is_shop() || is_product_category()) {
				return false;
			} else if (is_product() || is_woocommerce() || is_cart()) {
				return true;
			} else {
				return false;
			}
        } else {
			return false;
		}
    }
endif;


/**
* Username display on header as option
*
* @since Campoal 1.9.9
*/
if( !function_exists('conikal_username_display_header') ):
	function conikal_username_display_header($current_user, $display_name_setting = 'firstname') {
		if ($display_name_setting == 'fullname') {
			$username = $current_user->display_name;
		} elseif ($display_name_setting == 'firstname') {
			$username = $current_user->user_firstname;
		} elseif ($display_name_setting == 'lastname') {
			$username = $current_user->user_lastname;
		} elseif ($display_name_setting == 'nickname') {
			$username = $current_user->user_login;
		} else {
			$username = '';
		}

		return $username;
	}
endif;


/**
* Sort array by value
*
* @since Campoal 1.0.0
*/
if( !function_exists('conikal_array_sort') ):
	function conikal_array_sort($array, $sortby, $direction='asc') {
		$sortedArr = array();
		$tmp_Array = array();

		foreach($array as $k => $v) {
			$tmp_Array[] = strtolower($v->$sortby);
		}

		if($direction=='asc'){
			asort($tmp_Array);
		} else {
			arsort($tmp_Array);
		}

		foreach($tmp_Array as $k=>$tmp){
			$sortedArr[] = $array[$k];
		}

		return $sortedArr;
	}
endif;


/**
* Display not found post notice
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_not_found_post')) :
	function conikal_not_found_post($heading = '', $description = '', $type_message = '', $icon = 'frown outline icon') { ?>
		<div class="not-found" id="not-found">
			<?php if (!empty($type_message)) : ?>
				<div class="ui<?php echo esc_attr(!empty($type_message) ? ' ' . $type_message : '') ?> icon message">
					<i class="<?php echo esc_attr($icon) ?>"></i>
					<div class="content">
						<div class="header">
							<?php echo esc_html($heading) ?>
						</div>
						<?php echo esc_html($description) ?>
					</div>
				</div>
			<?php else : ?>
				<div class="ui centered grid">
					<div class="twelve wide mobile eight wide tablet seven wide computer center aligned column">
						<?php get_template_part('templates/svg/not-found'); ?>
					</div>
				</div>
				<div class="ui large centered header">
					<div class="content">
						<?php echo esc_html($heading) ?>
						<div class="sub header">
							<?php echo esc_html($description) ?>
						</div>
					</div>
				</div>
				<div class="ui hidden section divider"></div>
			<?php endif; ?>
        </div>
	<?php }
endif;


/**
* Check has featured post or not
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_has_featured_posts')) :
	function conikal_has_featured_posts() {
		$args = array(
			'posts_per_page'   => 1,
			'meta_key'         => 'post_featured',
			'meta_value'       => '1',
			'post__not_in'     => get_option("sticky_posts"),
			'post_status'      => 'publish'
		);
		$posts_featured = new wp_query($args);

		if ($posts_featured->have_posts()) {
			$results = true;
		} else {
			$results = false;
		}

		wp_reset_postdata();
		wp_reset_query();

		return $results;
}
endif;


/**
* Formated and compact support number
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_format_number')) :
	function conikal_format_number($format, $number, $round = false) {
		global $campoal_options;

		$compact_number = isset($campoal_options['conikal_compact_number_field']) ? $campoal_options['conikal_compact_number_field'] : 0;
		$thousands_separator = isset($campoal_options['conikal_thousands_separator_field']) ? $campoal_options['conikal_thousands_separator_field'] : ',';
		$demical_separator = isset($campoal_options['conikal_demical_separator_field']) ? $campoal_options['conikal_demical_separator_field'] : '.';
		$number = intval($number);

		if ($round == true && $compact_number == 1) {
			$strnum = strlen($number);
			if ($strnum > 3 && $strnum < 7) {
				$number = $number / 1000;
				$number = number_format($number, 1, $thousands_separator, $demical_separator);
				$number = $number . esc_html__('K', 'campoal');
			} elseif ($strnum > 6 && $strnum < 10) {
				$number = $number / 1000000;
				$number = number_format($number, 2, $thousands_separator, $demical_separator);
				$number = $number . esc_html__('M', 'campoal');
			} elseif ($strnum > 9 && $strnum < 13) {
				$number = $number / 1000000000;
				$number = number_format($number, 3, $thousands_separator, $demical_separator);
				$number = $number . esc_html__('B', 'campoal');
			} elseif ($strnum > 12 && $strnum < 16) {
				$number = $number / 1000000000000;
				$number = number_format($number, 4, $thousands_separator, $demical_separator);
				$number = $number . esc_html__('T', 'campoal');
			} else {
				$number = $number;
			}
		}

		while (true) {
			$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1' . $thousands_separator . '$2', $number);
			if ($replaced != $number) {
				$number = $replaced;
			} else {
				break;
			}
		}

		return $number;
	}
endif;


/**
* Convert hext demical color code to RGB equivalent
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_hex_rbg')) :
	function conikal_hex_rbg($hexStr, $returnAsString = false, $seperator = ',') {
		$hexStr = preg_replace("/[^0-9A-Fa-f]/", '', $hexStr); // Gets a proper hex string
		$rgbArray = array();
		if (strlen($hexStr) == 6) { //If a proper hex code, convert using bitwise operation. No overhead... faster
			$colorVal = hexdec($hexStr);
			$rgbArray['red'] = 0xFF & ($colorVal >> 0x10);
			$rgbArray['green'] = 0xFF & ($colorVal >> 0x8);
			$rgbArray['blue'] = 0xFF & $colorVal;
		} elseif (strlen($hexStr) == 3) { //if shorthand notation, need some string manipulations
			$rgbArray['red'] = hexdec(str_repeat(substr($hexStr, 0, 1), 2));
			$rgbArray['green'] = hexdec(str_repeat(substr($hexStr, 1, 1), 2));
			$rgbArray['blue'] = hexdec(str_repeat(substr($hexStr, 2, 1), 2));
		} else {
			return false; //Invalid hex color code
		}
		return $returnAsString ? implode($seperator, $rgbArray) : $rgbArray; // returns the rgb string or the associative array
	}
endif;


/**
* Detect icon font or image url
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_detect_icon_url')) :
	function conikal_detect_icon_url($icon)  {
		$string = '';
		if (wp_extract_urls($icon)) {
			$string .= '<img src="' . esc_url($icon) . '" class="custom-icon-image" />';
		} else {
			$string .= '<i class="' . esc_attr($icon) . '"></i>';
		}
		return $string;
	}
endif;


/**
* Get custom icon from settings
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_custom_icon')) :
	function conikal_custom_icon($type = '')  {
		global $campoal_options;

		$icon = '';

		switch ($type) {
			case 'submit':
				$submit_icon = isset($campoal_options['conikal_submit_button_icon_field']) ? $campoal_options['conikal_submit_button_icon_field'] : 'edit outline icon';
				$icon = conikal_detect_icon_url($submit_icon);
				break;

			case 'victory':
				$victory_icon = isset($campoal_options['conikal_victory_icon_field']) ? $campoal_options['conikal_victory_icon_field'] : 'flag icon';
				$icon = conikal_detect_icon_url($victory_icon);
				break;

			case 'victory_inverse':
				$victory_inverse_icon = isset($campoal_options['conikal_victory_inverse_icon_field']) ? $campoal_options['conikal_victory_inverse_icon_field'] : 'flag icon';
				$icon = conikal_detect_icon_url($victory_inverse_icon);
				break;

			case 'sign':
				$sign_button_icon = isset($campoal_options['conikal_sign_button_icon_field']) ? $campoal_options['conikal_sign_button_icon_field'] : 'write icon';
				$icon = conikal_detect_icon_url($sign_button_icon);
				break;

			case 'supporter':
				$supporter_icon = isset($campoal_options['conikal_supporter_icon_field']) ? $campoal_options['conikal_supporter_icon_field'] : 'user icon';
				$icon = conikal_detect_icon_url($supporter_icon);
				break;

			case 'send':
				$send_icon = isset($campoal_options['conikal_send_icon_field']) ? $campoal_options['conikal_send_icon_field'] : 'paper plane outline icon';
				$icon = conikal_detect_icon_url($send_icon);
				break;

			case 'citation':
				$citation_button_icon = isset($campoal_options['conikal_citation_button_icon_field']) ? $campoal_options['conikal_citation_button_icon_field'] : 'quote left icon';
				$icon = conikal_detect_icon_url($citation_button_icon);
				break;

			case 'agree':
				$citation_agree = isset($campoal_options['conikal_citation_agree_field']) ? $campoal_options['conikal_citation_agree_field'] : 'thumbs up icon';
				$icon = conikal_detect_icon_url($citation_agree);
				break;

			case 'disagree':
				$citation_disagree = isset($campoal_options['conikal_citation_disagree_field']) ? $campoal_options['conikal_citation_disagree_field'] : 'thumbs down icon';
				$icon = conikal_detect_icon_url($citation_disagree);
				break;

			case 'share':
				$share_icon = isset($campoal_options['conikal_share_button_icon_field']) ? $campoal_options['conikal_share_button_icon_field'] : 'share icon';
				$icon = conikal_detect_icon_url($share_icon);
				break;
			case 'donate':
				$donate_icon = isset($campoal_options['conikal_donation_button_icon_field']) ? $campoal_options['conikal_donation_button_icon_field'] : 'gift icon';
				$icon = conikal_detect_icon_url($donate_icon);
				break;
			case 'profile_activist':
				$profile_activist = isset($campoal_options['conikal_profile_activist_icon_field']) ? $campoal_options['conikal_profile_activist_icon_field'] : 'hand rock icon';
				$icon = conikal_detect_icon_url($profile_activist);
				break;
			case 'profile_legislator':
				$profile_legislator = isset($campoal_options['conikal_profile_legislator_icon_field']) ? $campoal_options['conikal_profile_legislator_icon_field'] : 'balance scale icon';
				$icon = conikal_detect_icon_url($profile_legislator);
				break;
			case 'profile_organization':
				$profile_organization = isset($campoal_options['conikal_profile_organization_icon_field']) ? $campoal_options['conikal_profile_organization_icon_field'] : 'users icon';
				$icon = conikal_detect_icon_url($profile_organization);
				break;

			default:
				$icon = $type . ' icon';
				$icon = conikal_detect_icon_url($icon);
				break;
		}

		return $icon;
	}

endif;


/**
* Check Gravatar exist or not (Degenerated)
*
* @since Campoal 1.0.0

*/

/*
if (!function_exists('conikal_validate_gravatar')) :
	function conikal_validate_gravatar($id_or_email) {
	  //id or email code borrowed from wp-includes/pluggable.php
		$email = '';
		if ( is_numeric($id_or_email) ) {
			$id = (int) $id_or_email;
			$user = get_userdata($id);
			if ( $user )
				$email = $user->user_email;
		} elseif ( is_object($id_or_email) ) {
			// No avatar for pingbacks or trackbacks
			$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
			if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
				return false;

			if ( !empty($id_or_email->user_id) ) {
				$id = (int) $id_or_email->user_id;
				$user = get_userdata($id);
				if ( $user)
					$email = $user->user_email;
			} elseif ( !empty($id_or_email->comment_author_email) ) {
				$email = $id_or_email->comment_author_email;
			}
		} else {
			$email = $id_or_email;
		}

		$hashkey = md5(strtolower(trim($email)));
		$uri = "//www.gravatar.com/avatar/$hashkey?d=404";

		$data = wp_cache_get($hashkey);
		if (false === $data) {
			$response = wp_remote_head($uri);
			if( is_wp_error($response) ) {
				$data = 'not200';
			} else {
				$data = $response['response']['code'];
			}
			wp_cache_set($hashkey, $data, $group = '', $expire = 60*5);

		}
		if ($data == '200'){
			return true;
		} else {
			return false;
		}
	}
endif;
*/

/**
* Convert facebook avatar url to fixed avatar
*
* @since Campoal 1.7.4
*/
if (!function_exists('conikal_convert_facebook_avatar_url')) :
	function conikal_convert_facebook_avatar_url($user_id, $avatar_args) {
		$upload_avatar  	= get_user_meta($user_id, 'avatar', true);
		$avatar_url_parse 	= wp_parse_url($upload_avatar);
		if ($avatar_url_parse && array_key_exists('host', $avatar_url_parse)) {
			if ($avatar_url_parse['host'] == 'platform-lookaside.fbsbx.com') {
				$query_parse 		= wp_parse_str( $avatar_url_parse['query'], $args );
				$upload_avatar 		= 'https://graph.facebook.com/' . $args['asid'] . '/picture?type=' . ($avatar_args['size'] >= 80 ? 'large' : 'square');
			}
		}

		return $upload_avatar;
	}
endif;


/**
* Get avatar from Gravatar or use default avatar from setting
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_get_avatar_url')) :
	function conikal_get_avatar_url($id_or_email, $args, $default = true) {

		// avatar uploaded by user
		if (is_email($id_or_email)) {
			$user_data    	= get_user_by('email', $id_or_email);
			$user_id = $user_data ? $user_data->ID : false;
		} else {
			$user_id = $id_or_email != 0 ? $id_or_email : false;
		}

		// avatar size base input
		if ($args['size'] <= 32) {
			$avatar_size = 'conikal-avatar-small';
		} elseif ($args['size'] <= 64) {
			$avatar_size = 'conikal-avatar-medium';
		} elseif ($args['size'] <= 128) {
			$avatar_size = 'conikal-avatar-large';
		} elseif ($args['size'] <= 256) {
			$avatar_size = 'conikal-avatar-big';
		} else {
			$avatar_size = 'full';
		}

		if ($user_id) {
			$upload_avatar_id  	= get_user_meta($user_id, 'avatar_id', true);
			if ($upload_avatar_id) {
				$upload_avatar 	= wp_get_attachment_image_src($upload_avatar_id, $avatar_size);
				$upload_avatar 	= ($upload_avatar ? $upload_avatar[0] : false);
			} else {
				$profile_id 	= get_user_meta($user_id, 'user_facebook_connection_profile_id', true);
				if (empty($profile_id)) {
					$upload_avatar 	    = conikal_convert_facebook_avatar_url($user_id, $args);
				} else {
					$upload_avatar 		= 'https://graph.facebook.com/' . $profile_id . '/picture?type=' . ($args['size'] >= 80 ? 'large' : 'square');
				}
			}
		} else {
			$upload_avatar = false;
		}

		// get avatar from gavatar
		if ($upload_avatar) {
			// check avatar is url or path
			$parse_avatar_url = wp_parse_url($upload_avatar, PHP_URL_SCHEME);
			if ($parse_avatar_url) {
				$results = $upload_avatar;
			} else {
				$upload_dir     = wp_upload_dir();
				$results = $upload_dir['baseurl'] . '/' . $upload_avatar;
			}
		} else {
			global $campoal_options;
			$default_avatar = isset($campoal_options['conikal_default_avatar_field']['url']) ? $campoal_options['conikal_default_avatar_field']['url'] : false;

			if ($default_avatar) {
				$args['default'] = ($default ? esc_url($default_avatar) : '');
			}

			$avatar = get_avatar_url( $id_or_email, $args );
			if ($avatar) {
				$results = $avatar;
			} elseif (array_key_exists('default', $args)) {
				$results = $args['default'];
			} else {
				$results = $avatar;
			}
		}

		return $results;
	}
endif;


/**
 * Get post thumbnail
 *
 * @since Campoal Tapo 2.1.0
 */
if (!function_exists('conikal_get_post_thumbnail')) :
    function conikal_get_post_thumbnail($id, $size = 'thumbnail', $placeholder = 'thumbnail-square') {
        if (has_post_thumbnail($id)) {
            $thumb_id = get_post_thumbnail_id($id);
            $thumbnail = wp_get_attachment_image_src($thumb_id, $size, true);
            $thumbnail = isset($thumbnail[0]) ? $thumbnail[0] : false;
        } else {
            if ($placeholder) {
                $thumbnail = get_template_directory_uri() . '/images/' . $placeholder . '.svg';
            } else {
                $thumbnail = false;
            }
        }

        return $thumbnail;
    }
endif;


/**
* Add upload dir url to images to complete url
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_add_dir_url_image')) :
	function conikal_add_dir_url_image($file) {
		$upload_dir       	= wp_upload_dir();
		return $upload_dir['baseurl'] . '/' . $file;
	}
endif;


/**
* Split images from gallery and complete url
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_get_gallery_images')) :
	function conikal_get_gallery_images($gallery) {
		$images = explode("~~~", $gallery);
		$images = array_map('conikal_add_dir_url_image', $images);

		return $images;
	}
endif;

/**
* Allowed html tag in petitions and comments
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_content_allowed_html')) :
	function conikal_content_allowed_html() {
		$allowed_tags = array(
			'div' => array(
				'class' => array(),
				'style' => array(),
			),
			'a' => array(
				'class' => array(),
				'href'  => array(),
				'rel'   => array(),
				'title' => array(),
				'style' => array(),
			),
			'button' => array(
				'class' => array(),
				'style' => array(),
			),
			'abbr' => array(
				'title' => array(),
				'style' => array(),
			),
			'b' => array(),
			'blockquote' => array(
				'cite'  => array(),
				'style' => array(),
			),
			'cite' => array(
				'title' => array(),
			),
			'code' => array(),
			'del' => array(
				'datetime' => array(),
				'title' => array(),
			),
			'br' => array(),
			'dd' => array(),
			'dl' => array(),
			'dt' => array(),
			'em' => array(),
			'h1' => array(),
			'h2' => array(),
			'h3' => array(),
			'h4' => array(),
			'h5' => array(),
			'h6' => array(),
			'i' => array(),
			'img' => array(
				'alt'    => array(),
				'class'  => array(),
				'height' => array(),
				'src'    => array(),
				'width'  => array(),
				'style' => array(),
			),
			'section' => array(
				'class' => array(),
				'style' => array(),
			),
			'li' => array(
				'class' => array(),
				'style' => array(),
			),
			'ol' => array(
				'class' => array(),
				'style' => array(),
			),
			'p' => array(
				'class' => array(),
				'style' => array(),
			),
			'q' => array(
				'cite' => array(),
				'title' => array(),
				'style' => array(),
			),
			'span' => array(
				'class' => array(),
				'title' => array(),
				'style' => array(),
			),
			'strike' => array(),
			'strong' => array(),
			'ul' => array(
				'class' => array(),
				'style' => array(),
			),
			'table' => array(
				'class' => array(),
				'style' => array(),
			),
			'thead' => array(),
			'tbody' => array(),
			'tr' => array(
				'class' => array(),
				'style' => array(),
			),
			'th' => array(
				'class' => array(),
				'style' => array(),
				'colspan' => array(),
			),
			'td' => array(
				'class' => array(),
				'style' => array(),
				'colspan' => array(),
			),
			'input' => array(
				'type'  => array(),
				'id'    => array(),
				'class' => array(),
				'name'  => array(),
				'value' => array(),
				'placeholder' => array(),
				'required' => array(),
				'autocomplete' => array(),
				'style' => array(),
			),
		);

		return $allowed_tags;
	}
endif;

/**
* Get lazy load placeholder
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_lazy_load_placeholder')) :
	function conikal_lazy_load_placeholder($thumbnail = 'thumbnail') {
		$lazy_placeholder = get_template_directory_uri() . '/images/' . $thumbnail . (is_rtl() ? '-rtl' : '') . '.svg';

		return $lazy_placeholder;
	}
endif;


/**
* Get author display name as researcher
*
* @since Campoal 1.5.6
*/
if (!function_exists('conikal_get_author_name')) :
	function conikal_get_author_name($author_id) {
		$author_name = get_the_author_meta('display_name', $author_id);
		$user_type = get_user_meta($author_id, 'user_type', true);
		$organization_name = get_user_meta($author_id, 'user_organization_name', true);
		if ($user_type == 'organization' && $organization_name != '') {
			$author_name = $organization_name;
		}

		return $author_name;
	}
endif;


/**
* Get author display name as researcher
*
* @since Campoal 1.5.6
*/
if (!function_exists('conikal_profile_menu_label_icon')) :
	function conikal_profile_menu_label_icon() {
		$profile_menu_icon  = 'user icon';
		$profile_menu_label = __('Edit Profile', 'campoal');
		if (class_exists('WooCommerce')) {
			if (is_wc_endpoint_url('edit-address')) {
				$profile_menu_icon  = 'home icon';
				$profile_menu_label = __('My Address', 'campoal');
			} else if (is_wc_endpoint_url('payment-methods')) {
				$profile_menu_icon  = 'credit card icon';
				$profile_menu_label = __('Payment Methods', 'campoal');
			}
		}

		$label_icon = array(
			'icon'	=> $profile_menu_icon,
			'label' => $profile_menu_label
		);

		return (object) $label_icon;
	}
endif;

/**
* Get form id by petition id
*
* @since Campoal 2.1.0
*/
if (!function_exists('conikal_get_petition_form_id')) :
	function conikal_get_petition_form_id($petition_id) {
		global $campoal_options;
		$common_donation_form  	= isset($campoal_options['conikal_common_donation_form_field']) ? $campoal_options['conikal_common_donation_form_field'] : 0;
		$common_form_id     	= isset($campoal_options['conikal_common_form_id_field']) ? $campoal_options['conikal_common_form_id_field'] : 0;

		// get fundraising mode setting
		$fundraising_mode = conikal_fundraising_mode_setting($petition_id);

		if (!empty($common_donation_form) && !empty($common_form_id) && is_numeric($common_form_id) && $fundraising_mode == 0) {
			$form_id    = $common_form_id;
		} else {
			$form_id    = get_post_meta($petition_id, 'petition_contribute', true);
			$form_id	= !empty($form_id) ? $form_id : false;
		}
		$contribute_approve = get_post_status($form_id);
		if ($contribute_approve == 'publish') {
			$form_id    = $form_id;
		} else {
			$form_id 	= false;
		}

		return $form_id;
	}
endif;



/**
* Create multidimensional array unique from any single key index
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_unique_multidim_array')) :
	function conikal_unique_multidim_array($array, $key) {
		$temp_array = array();
		$i = 0;
		$key_array = array();

		foreach($array as $val) {
			if (!in_array($val[$key], $key_array)) {
				$key_array[$i] = $val[$key];
				$temp_array[$i] = $val;
			}
			$i++;
		}
		return $temp_array;
	}
endif;


/**
* Check user is loggedin or not, if not redirect to login page
*
* @since Campoal 1.3.0
*/
if (!function_exists('conikal_redirect_login')) :
	function conikal_redirect_login($referer = '') {
		if (!is_user_logged_in()) {
			global $campoal_options;
			global $post;
		    $login_page = isset($campoal_options['conikal_page_login_field']) ? $campoal_options['conikal_page_login_field'] : 0;

		    if ($login_page != 0) {
		    	$login_page = get_permalink($login_page);
		    	$login_page = add_query_arg('referer', $post->ID, $login_page);
		        wp_redirect($login_page);
		    } else {
		        wp_redirect(home_url('/'));
		    }
		}
	}
endif;


/**
* Check Google reCaptcha configure to enable
*
* @since Campoal 1.4.0
*/
if(!function_exists('conikal_show_google_reCaptcha')) {
    function conikal_show_google_reCaptcha() {
    	global $campoal_options;
        $recaptcha_site_key      = isset($campoal_options['conikal_google_recaptcha_key_field']) ? $campoal_options['conikal_google_recaptcha_key_field'] : '';
        $recaptcha_secret_key    = isset($campoal_options['conikal_google_recaptcha_secret_field']) ? $campoal_options['conikal_google_recaptcha_secret_field'] : '';
        $enable_reCaptcha        = isset($campoal_options['conikal_google_recaptcha_field']) ? $campoal_options['conikal_google_recaptcha_field'] : 0;

        if( $enable_reCaptcha != 0 && !empty($recaptcha_site_key) && !empty($recaptcha_secret_key) ) {
            return true;
        }
        return false;
    }
}

/**
* Option Google reCaptcha visibility
*
* @since Campoal 1.9.7
*/
if(!function_exists('conikal_reCaptcha_visibility')) {
    function conikal_reCaptcha_visibility() {
    	global $campoal_options;
        $recaptcha_position      = isset($campoal_options['conikal_google_recaptcha_position_field']) ? $campoal_options['conikal_google_recaptcha_position_field'] : 'hidden';

        return $recaptcha_position;
    }
}


/**
 * Get fundraising mode setting
 *
 * @since Campoal 1.8.9
 */
if( !function_exists('conikal_fundraising_mode_setting') ):
    function conikal_fundraising_mode_setting($id) {
        if (!class_exists('Give')) {
            return false;
        }

        global $campoal_options;

        $fundraising_mode_apply = isset($campoal_options['conikal_fundraising_mode_apply_field']) ? $campoal_options['conikal_fundraising_mode_apply_field'] : 'global';
        if ($fundraising_mode_apply == 'global') {
            $fundraising_mode = isset($campoal_options['conikal_fundraising_mode_field']) ? $campoal_options['conikal_fundraising_mode_field'] : 1;
        } else {
            $petition_fundraising_mode = get_post_meta($id, 'petition_fundraising_mode', true);
            $fundraising_mode = isset($campoal_options['conikal_fundraising_mode_field']) ? ($petition_fundraising_mode != '' ? $petition_fundraising_mode : $campoal_options['conikal_fundraising_mode_field']) : 1;
        }

        return $fundraising_mode;
    }
endif;

/**
 * Get status of citation
 *
 * @since Campoal 1.5.6
 */
if( !function_exists('conikal_citation_status') ): 
    function conikal_citation_status($id) {
        global $campoal_options;

        $citation_apply = isset($campoal_options['conikal_citation_apply_field']) ? $campoal_options['conikal_citation_apply_field'] : 'single';
        if ($citation_apply == 'global') {
            $citation = isset($campoal_options['conikal_citation_field']) ? $campoal_options['conikal_citation_field'] : 1;
        } else {
            $petition_citation = get_post_meta($id, 'petition_citation_status', true);
            $citation = isset($campoal_options['conikal_citation_field']) ? ($petition_citation != '' ? $petition_citation : $campoal_options['conikal_citation_field']) : 1;
        }

        return $citation;
    }
endif;

/**
* Get age from birthday
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_get_age')) :
    function conikal_get_age($birthdate) {
        if ($birthdate) {
            $bits = explode('/', $birthdate);
            $age = date('Y') - intval($bits[2]) - 1;
         
            $arr[0] = 'm';
            $arr[1] = 'd';
         
            for ($i = 0; $i < 2; $i++) {
                $n = date($arr[$i]);
                if ($n < $bits[$i])
                    break;
                if ($n > $bits[$i]) {
                    ++$age;
                    break;
                }
            }
        } else {
            $age = false;
        }
        return $age;
    }
endif;


/**
* Check user register time less than one hour
*
* @since Campoal 1.4.2
*/
if (!function_exists('conikal_user_register_time_diff')) :
    function conikal_user_register_time_diff($user_login) {
        // get user data
        if(is_email($user_login)) {
            $user_data = get_user_by('email', $user_login);
        } else {
            $user_data = get_user_by('login', $user_login);
        }

        // user date and calculaing time less than one hour
        $register_date = strtotime($user_data->user_registered, current_time('timestamp'));
        $register_diff = (int) abs($register_date - current_time('timestamp'));

        if ( $register_diff < HOUR_IN_SECONDS ) {
            $one_hour = true;
        } else {
            $one_hour = false;
        }

        return $one_hour;
    }
endif;

/**
* Check is petition co-author permission
*
* @since Campoal 1.0.0
*/
if (!function_exists('conikal_co_author_permission')) :    
    function conikal_co_author_permission($id) {
        $current_user = wp_get_current_user();
		$post_type = get_post_type($id);

		$co_author_key = 'petition_authors';
        if ($post_type == 'give_forms') {
            $co_author_key = '_give_authors';
        }
		
        $co_authors = get_post_meta($id, $co_author_key, true);

        if ($co_authors && in_array($current_user->ID, $co_authors)) {
            $result = true;
        } else {
            $result = false;
        }

        return $result;
    }
endif;


/**
* Get country select from user option or cookie
*
* @since Campoal 1.8.8
*/
if (!function_exists('conikal_get_country_selected')) :    
	function conikal_get_country_selected($current_user = false) {
		global $campoal_options;
		if (empty($current_user)) {
			global $current_user;
		}

		$country_auto_detection = isset($campoal_options['conikal_country_auto_detection_field']) ? $campoal_options['conikal_country_auto_detection_field'] : 0;

		if ($country_auto_detection) {
			// get location of user by GeoIp
			$geoip_info = function_exists('conikal_client_geoip_info') ? conikal_client_geoip_info() : false;
			$default_country = ($geoip_info ? $geoip_info->country->isoCode : '');
		} else {
			$default_country = 'WORLD';
		}

		$remember_country_selected = isset($_COOKIE['conikal_country_selected']) ? $_COOKIE['conikal_country_selected'] : $default_country;

		if (is_user_logged_in() && !empty($current_user)) {
			$country_selected = !empty($current_user->country_selected) && !is_int($current_user->country_selected) ? $current_user->country_selected : $default_country;
		} else {
			$country_selected = $remember_country_selected;
		}

		return $country_selected;
	}
endif;

/**
 * Display ajax loader html follow theme setting.
 *
 * @since Campoal 1.0.0
 */
if( !function_exists('conikal_preloader_html') ): 
    function conikal_preloader_html($type_preloader = '') {
        switch ($type_preloader) {
            case 'chasing-dots':
                $html_preloader = '<div class="sk-chasing-dots"><div class="sk-child sk-dot1"></div><div class="sk-child sk-dot2"></div></div>';
                break;
            case 'cube-grid':
                $html_preloader = '<div class="sk-cube-grid"><div class="sk-cube sk-cube1"></div><div class="sk-cube sk-cube2"></div><div class="sk-cube sk-cube3"></div><div class="sk-cube sk-cube4"></div><div class="sk-cube sk-cube5"></div><div class="sk-cube sk-cube6"></div><div class="sk-cube sk-cube7"></div><div class="sk-cube sk-cube8"></div><div class="sk-cube sk-cube9"></div></div>';
                break;
            case 'double-bounce':
                $html_preloader = '<div class="sk-double-bounce"><div class="sk-child sk-double-bounce1"></div><div class="sk-child sk-double-bounce2"></div></div>';
                break;
            case 'fading-circle':
                $html_preloader = '<div class="sk-fading-circle"><div class="sk-circle1 sk-circle"></div><div class="sk-circle2 sk-circle"></div><div class="sk-circle3 sk-circle"></div><div class="sk-circle4 sk-circle"></div><div class="sk-circle5 sk-circle"></div><div class="sk-circle6 sk-circle"></div><div class="sk-circle7 sk-circle"></div><div class="sk-circle8 sk-circle"></div><div class="sk-circle9 sk-circle"></div><div class="sk-circle10 sk-circle"></div><div class="sk-circle11 sk-circle"></div><div class="sk-circle12 sk-circle"></div></div>';
                break;
            case 'folding-cube':
                $html_preloader = '<div class="sk-folding-cube"><div class="sk-cube1 sk-cube"></div><div class="sk-cube2 sk-cube"></div><div class="sk-cube4 sk-cube"></div><div class="sk-cube3 sk-cube"></div></div>';
                break;
            case 'pulse':
                $html_preloader = '<div class="sk-spinner sk-spinner-pulse"></div>';
                break;
            case 'rotating-plane':
                $html_preloader = '<div class="sk-rotating-plane"></div>';
                break;
            case 'three-bounce':
                $html_preloader = '<div class="sk-three-bounce"><div class="sk-child sk-bounce1"></div><div class="sk-child sk-bounce2"></div><div class="sk-child sk-bounce3"></div></div>';
                break;
            case 'wandering-cubes':
                $html_preloader = '<div class="sk-wandering-cubes"><div class="sk-cube sk-cube1"></div><div class="sk-cube sk-cube2"></div></div>';
                break;
            case 'wave':
                $html_preloader = '<div class="sk-wave"><div class="sk-rect sk-rect1"></div><div class="sk-rect sk-rect2"></div><div class="sk-rect sk-rect3"></div><div class="sk-rect sk-rect4"></div><div class="sk-rect sk-rect5"></div></div>';
                break;
            default:
                $html_preloader = '';
                break;
        }

        if ($html_preloader) {
            return $html_preloader;
        } else {
            return false;
        }
    }
endif;

/**
* Detect if you're in the “Frontend Editor” mode in Visual Composer
*
* @since Campoal 1.9.10
*/
function conikal_is_vc_build() {
    return function_exists( 'vc_is_inline' ) && vc_is_inline() ? true : false;
}

?>