<?php
/**
 * The file functions.
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/public/helper
 *
 * @since 2.0.0
 */

/**
 * Post views helper method.
 *
 * @since 2.0.0
 */
class SP_PCP_Functions {
	/**
	 * WP Version compare
	 *
	 * @param  mixed $version_to_compare version compare.
	 * @param  mixed $operator operator.
	 * @return statement
	 */
	public static function wp_version_compare( $version_to_compare, $operator = '>=' ) {
		if ( empty( $version_to_compare ) ) {
			return true;
		}
		global $wp_version;

		// Check if using WordPress version 3.7 or higher.
		return version_compare( $wp_version, $version_to_compare, $operator );
	}

	/**
	 * Post title character limit.
	 *
	 * @param string  $pcp_title The post title.
	 * @param integer $limit_length The length for the title.
	 * @param string  $pcp_after_string The string after title.
	 * @return statement
	 */
	public static function limit_post_title( $pcp_title, $limit_length, $pcp_after_string = '...' ) {
		return mb_strimwidth( $pcp_title, 0, $limit_length, apply_filters( 'pcp_post_title_ellipsis', $pcp_after_string ) );
	}

	/**
	 * Tag name to full tag conversion.
	 *
	 * @param array $meta_tag Tag option.
	 * @return string
	 */
	public static function short_tag_to_html( $meta_tag ) {
		$exclude_tag_string = '';
		foreach ( $meta_tag as $key => $value ) {
			$exclude_tag_string .= '<' . $value . '>,';
		}
		return $exclude_tag_string;
	}

	/**
	 * Limit the text.
	 *
	 * @param mixed  $text The text you want to limit.
	 * @param int    $limit The number of words to display.
	 * @param string $ellipsis The ellipsis at the end of the text.
	 * @return statement
	 */
	public static function pcp_limit_text( $text, $limit, $ellipsis = '...' ) {
		$word_arr = explode( ' ', $text );
		if ( count( $word_arr ) > $limit ) {
			$words = implode( ' ', array_slice( $word_arr, 0, $limit ) ) . $ellipsis;
			return $words;
		} else {
			return $text;
		}
	}

	/**
	 * Limit the text with html.
	 *
	 * @param mixed  $html The text you want to limit.
	 * @param int    $word_count The number of words to display.
	 * @param string $ellipsis The ellipsis at the end of the text.
	 * @return statement
	 */
	public static function pcp_extract_words_from_html( $html, $word_count = 55, $ellipsis = '...' ) {
		global $wp_embed;
		$html = $wp_embed->autoembed( $html );
		$html = do_shortcode( $html );
		if ( function_exists( 'do_blocks' ) ) {
			$html = do_blocks( $html );
		}
		// Remove extra white spaces and new lines from the HTML.
		$html = preg_replace( '/\s+/', ' ', $html );

		// Split the HTML string into individual tags and words.
		$tokens = preg_split( '/(<[^>]+>| )/', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );

		$word_counter = 0;
		$excerpt      = '';

		foreach ( $tokens as $token ) {
			if ( $word_counter >= $word_count ) {
				break;
			}

			if ( '<' === $token[0] ) {
				// Append tags as they are.
				$excerpt .= $token;
			} else {
				// Split the token into words.
				$words = preg_split( '/\s+/', $token, -1, PREG_SPLIT_NO_EMPTY );

				foreach ( $words as $word ) {
					if ( $word_counter >= $word_count ) {
						break 2;
					}

					$excerpt .= $word . ' ';
					++$word_counter;
				}
			}
		}

		// Trim any trailing space.
		$excerpt = rtrim( $excerpt );

		// Add ellipsis if necessary.
		if ( count( $tokens ) > $word_count ) {
			$excerpt .= $ellipsis;
		}

		return $excerpt;
	}

	/**
	 * Characters Limit of the text.
	 *
	 * @param mixed  $text The text you want to limit.
	 * @param int    $limit The number of words to display.
	 * @param string $ellipsis The ellipsis at the end of the text.
	 * @return statement
	 */
	public static function limit_content_chr( $text, $limit, $ellipsis = '...' ) {
		$length = $limit;
		if ( strlen( $text ) < $length + 10 ) {
			return $text;// don't cut if too short.
		}
		$visible = substr( $text, 0, $length );
		return $visible . $ellipsis;
	}

	/**
	 * Allowed tags function of the Plugin.
	 *
	 * @since 2.0.0
	 * @return array allowed tags
	 */
	public static function allowed_tags() {
		$allowed_tags           = wp_kses_allowed_html( 'post' );
		$allowed_tags['iframe'] = array(
			'src'             => array(),
			'height'          => array(),
			'width'           => array(),
			'frameborder'     => array(),
			'allowfullscreen' => array(),
			'title'           => array(),
			'alt'             => array(),
		);

		$allowed_tags['style'] = array();

		return $allowed_tags;
	}

	/**
	 * Trims content with HTML tags.
	 *
	 * This function applies content filters, strips tags for word counting, then
	 * trims the content to the desired length. If the content exceeds the
	 * desired length, it appends an ellipsis to the end. Finally, it balances
	 * any broken HTML tags.
	 *
	 * @param string $content The content to trim.
	 * @param int    $length  The desired length of the content.
	 * @param string $more    The text to append to the end of the trimmed content.
	 * @return string The trimmed content.
	 */
	public static function trim_content_with_html_tags( $content, $length = 55, $more = '...' ) {
		// Apply content filters (shortcodes, etc.).
		$content = apply_filters( 'the_content', $content );

		// Store a copy before stripping tags for word counting.
		$text_only = wp_strip_all_tags( $content );

		if ( str_word_count( $text_only ) > $length ) {
			// Get content up to the point that exceeds word count.
			$words      = preg_split( '/([\s]+)/', $content, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
			$word_count = 0;
			$excerpt    = '';

			foreach ( $words as $word ) {
				if ( $word_count >= $length && ! preg_match( '/^[\s]+$/', $word ) ) {
					break;
				}

				$excerpt .= $word;
				if ( ! preg_match( '/^[\s]+$/', $word ) ) {
					++$word_count;
				}
			}
			$content = $excerpt . $more;
		}

		// Fix any broken HTML tags.
		$content = force_balance_tags( $content );

		return $content;
	}

	/**
	 * Content function.
	 *
	 * @param array  $view_options Read more options array.
	 * @param string $type Content type.
	 * @param  mixed  $post post.
	 * @param  mixed  $pcp_select_post_type post type.
	 * @param  mixed  $pcp_select_multiple_post_type multiple post type.
	 * @return content
	 */
	public static function pcp_content( $view_options, $type, $post, $pcp_select_post_type, $pcp_select_multiple_post_type ) {
		// Extract options.
		$pcp_content_length = ! empty( $view_options['pcp_content_length_type']['all'] ) ? $view_options['pcp_content_length_type'] : array(
			'all'  => '30',
			'unit' => 'words',
		);

		$post_content_ellipsis = isset( $view_options['post_content_ellipsis'] ) ? $view_options['post_content_ellipsis'] : '';
		$pcp_strip_tags        = isset( $view_options['pcp_strip_tags'] ) ? $view_options['pcp_strip_tags'] : '';
		$pcp_allow_tag_name    = isset( $view_options['pcp_allow_tag_name'] ) ? $view_options['pcp_allow_tag_name'] : '';
		$allowed_tags          = explode( ',', $pcp_allow_tag_name );

		// Determine if content is page content.
		$is_page_content = ( 'page' === $pcp_select_post_type || 'multiple_post_type' === $pcp_select_post_type ) && ( 'page' === $pcp_select_post_type || empty( $pcp_select_multiple_post_type ) || in_array( 'page', $pcp_select_multiple_post_type, true ) );

		$is_page_content = apply_filters( 'pcp_strip_shortcode_in_page_content', $is_page_content );

		// Get post content based on type.
		if ( 'excerpt' === $type ) {
			$pcp_post_content       = get_the_excerpt( $post );
			$pcp_post_excerpt_limit = apply_filters( 'pcp_post_excerpt_limit', $pcp_content_length['all'] );
			$pcp_post_content       = self::trim_content_with_html_tags( $pcp_post_content, $pcp_post_excerpt_limit, $post_content_ellipsis );
		} else {
			$post_content = $is_page_content ? apply_filters( 'sp_pcp_the_content', strip_shortcodes( $post->post_content ) ) : apply_filters( 'sp_pcp_the_content', $post->post_content );

			// Strip or limit content based on options.
			if ( 'strip_all' === $pcp_strip_tags ) {
				$pcp_post_content = wp_strip_all_tags( $post_content );
			} elseif ( 'allow_some' === $pcp_strip_tags ) {
				$pcp_post_content = strip_tags( $post_content, self::short_tag_to_html( $allowed_tags ) );
			} else {
				$pcp_post_content = $post_content;
			}

			// Trim content based on length type.
			if ( 'limit_content' === $type ) {
				if ( isset( $pcp_content_length['unit'] ) && 'words' === $pcp_content_length['unit'] ) {
					$pcp_post_content = self::pcp_extract_words_from_html( $pcp_post_content, $pcp_content_length['all'], $post_content_ellipsis );
				} else {
					$pcp_post_content = ( 'strip_all' === $pcp_strip_tags ) ? wp_html_excerpt( $pcp_post_content, $pcp_content_length['all'], $post_content_ellipsis ) : self::limit_content_chr( $pcp_post_content, $pcp_content_length['all'], $post_content_ellipsis );
				}
				$pcp_post_content = force_balance_tags( $pcp_post_content );
			}
		}

		// Apply shortcode and return content.
		global $wp_embed;
		$pcp_post_content = do_shortcode( $wp_embed->autoembed( $pcp_post_content ) );
		return $pcp_post_content;
	}

	/**
	 * Get the ID by the Image URL.
	 *
	 * @param mixed $url The image url.
	 * @param int   $post_id The image url.
	 * @return statement
	 */
	public static function pcp_image_id_by_url( $url, $post_id = null ) {
		global $wpdb;

		// If the URL is auto-generated thumbnail, remove the sizes and get the URL of the original image.
		$url       = preg_replace( '/-\d+x\d+(?=\.(png|jp(e|g|eg)|gif|bmp|ico|webp|svg)$)/i', '', $url );
		$cache_key = 'pcp_content_img_url_to_id' . $post_id;
		$img_cache = wp_cache_get( $cache_key );
		if ( false === $img_cache ) {
			$image = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s;", $url ) );
			wp_cache_set( $cache_key, $img_cache );
		}
		if ( ! empty( $image ) ) {
			return $image[0];
		}
		return false;
	}
	/**
	 * Thumb alter text
	 *
	 * @param integer $slide_id The slide/post ID.
	 *
	 * @return string
	 */
	public static function pcp_thumb_alter_text( $slide_id ) {
		$image_id = get_post_thumbnail_id( $slide_id );
		$alt_text = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
		return $alt_text;
	}

	/**
	 * Modify query params
	 *
	 * @param  array  $query_args query_args.
	 * @param  string $keyword keyword.
	 * @param  int    $author_id author id.
	 * @param  array  $custom_fields_array custom fields array.
	 * @param  string $orderby orderby.
	 * @param  string $order order.
	 * @param  array  $selected_term_list term_list.
	 * @param  int    $post_offset offset.
	 * @param  string $relation relation operator.
	 * @param  array  $post_in post in.
	 * @param  string $lang current lang.
	 * @return array
	 */
	public static function modify_query_params( $query_args, $keyword, $author_id, $custom_fields_array, $orderby, $order, $selected_term_list, $post_offset, $relation, $post_in = array(), $lang = '' ) {

		if ( ! empty( $keyword ) ) {
			$query_args['s'] = $keyword;
			$post_offset     = 0;
		}
		if ( 'all' !== $author_id & ! empty( $author_id ) ) {
			$query_args['author__in'] = $author_id;
			$post_offset              = 0;
		}
		if ( ! empty( $lang ) ) {
			$query_args['lang'] = $lang;
		}

		$query_args['post__in'] = $post_in;
		if ( ! empty( $custom_fields_array ) ) {
			$pcp_meta_query = array();
			foreach ( $custom_fields_array as $_custom_field ) {
				$c_field_key   = $_custom_field['custom_field_key'];
				$c_field_value = $_custom_field['custom_field_value'];
				if ( 'all' !== $c_field_value ) {
					if ( strpos( $c_field_value, ' ' ) !== false ) {
						$pcp_meta_query[] = array(
							'key'   => $c_field_key,
							'value' => $c_field_value,
						);
					} else {
						if ( strpos( $c_field_value, ',' ) !== false ) {
							$c_field_value = explode( ',', $c_field_value );
						}

						$c_field_query = array(
							'key'     => $c_field_key,
							'compare' => 'IN',
							'value'   => $c_field_value,
						);

						if ( is_string( $c_field_value ) && strpos( $c_field_value, '|' ) !== false ) {
							$c_field_value            = explode( '|', $c_field_value );
							$c_field_query['type']    = 'NUMERIC';
							$c_field_query['compare'] = 'BETWEEN';
							$c_field_query['value']   = $c_field_value;
						}
						$pcp_meta_query[] = $c_field_query;
					}
				}
			}
			if ( count( $pcp_meta_query ) > 1 ) {
				$pcp_meta_query['relation'] = 'AND';
			}
			$query_args['meta_query'] = $pcp_meta_query;
		}

		if ( ! empty( $orderby ) ) {
			$query_args['orderby'] = ( 'rand' === $orderby ) ? 'rand(' . get_transient( 'sps_rand' ) . ')' : $orderby;
		}
		if ( ! empty( $order ) ) {
			$query_args['order'] = $order;
		}
		if ( ! empty( $selected_term_list ) ) {
			if ( count( $selected_term_list ) > 1 ) {
				$tax_settings['relation'] = $relation;
			}
			if ( is_array( $selected_term_list ) ) {
				foreach ( $selected_term_list as $key => $tax_type ) {
					$taxonomy = $tax_type['taxonomy'];
					$terms    = $tax_type['term_id'];
					if ( strpos( $terms, ',' ) !== false ) {
						$terms = explode( ',', $terms );
					}
					if ( $taxonomy && $terms ) {
						$tax_settings[] = array(
							'taxonomy'         => $taxonomy,
							'field'            => 'term_id',
							'terms'            => $terms,
							'operator'         => 'IN',
							'include_children' => false,
						);
					}
				}
				$query_args['tax_query'] = $tax_settings;
			}
			$post_offset = 0;
		}
		$query_args['offset'] = $post_offset;
		return $query_args;
	}

	/**
	 * Thumb Sized function
	 *
	 * @param array   $post_thumb_setting Thumbnails options array.
	 * @param integer $slide_id The slide/post ID.
	 * @param  bool    $is_attachment type attachment.
	 * @param  bool    $smart_show_id shortcode id.
	 *
	 * @return string
	 */
	public static function pcp_sized_thumb( $post_thumb_setting, $slide_id, $is_attachment = false, $smart_show_id = null ) {
		$thumb_id                  = '';
		$image                     = '';
		$pcp_thumb_src_replace     = isset( $post_thumb_setting['pcp_thumb_src_replace'] ) ? $post_thumb_setting['pcp_thumb_src_replace'] : '';
		$post_featured_thumb_found = isset( $post_thumb_setting['post_featured_thumb_found'] ) ? $post_thumb_setting['post_featured_thumb_found'] : 'no_featured_img_found';
		$show_2x_image             = isset( $post_thumb_setting['load_2x_image'] ) ? $post_thumb_setting['load_2x_image'] : false;
		// Replace the thumbnail image with the custom fields image.
		$custom_field_key    = apply_filters( 'pcp_replace_thumbnail_with_custom_field_img', '', $smart_show_id );
		$custom_field_img_id = '';
		if ( $custom_field_key ) {
			$custom_field_img_id = get_post_meta( $slide_id, $custom_field_key, true );
			if ( is_array( $custom_field_img_id ) && isset( $custom_field_img_id['ID'] ) ) {
				$custom_field_img_id = $custom_field_img_id['ID'];
			}
			if ( is_string( $custom_field_img_id ) && strlen( $custom_field_img_id ) > 20 ) {
				$custom_field_img_id = attachment_url_to_postid( $custom_field_img_id );
			}
		}
		$image_resize_2x_url = '';
		if ( 'even_featured_img_found' === $post_featured_thumb_found && is_array( $pcp_thumb_src_replace ) ) {
			$replace_thumb = self::pcp_thumb_replace( $post_thumb_setting, $slide_id );
			$thumb_id      = ! empty( $custom_field_img_id ) ? $custom_field_img_id : $replace_thumb['id'];
		} elseif ( ! has_post_thumbnail( $slide_id ) && 'no_featured_img_found' === $post_featured_thumb_found ) {
			$replace_thumb = self::pcp_thumb_replace( $post_thumb_setting, $slide_id );
			$thumb_id      = ! empty( $custom_field_img_id ) ? $custom_field_img_id : $replace_thumb['id'];
		} elseif ( has_post_thumbnail( $slide_id ) ) {
				$thumb_id = ! empty( $custom_field_img_id ) ? $custom_field_img_id : get_post_thumbnail_id( $slide_id );
		}
		$placeholder_image = get_option( 'sps_placeholder_image', 0 );

		$placeholder_img = SP_PCP_URL . 'public/assets/img/placeholder.png';
		$placeholder_img = apply_filters( 'pcp_no_thumb_placeholder', $placeholder_img );
		if ( empty( $thumb_id ) && ! empty( $placeholder_image ) ) {
			$thumb_id = $placeholder_image;
		}
		if ( $is_attachment ) {
			$thumb_id = $slide_id;
		}
		if ( ! empty( $thumb_id ) ) {
			$image_sizes       = isset( $post_thumb_setting['pcp_thumb_sizes'] ) ? $post_thumb_setting['pcp_thumb_sizes'] : 'large';
			$post_image_width  = isset( $post_thumb_setting['pcp_image_crop_size']['top'] ) ? $post_thumb_setting['pcp_image_crop_size']['top'] : '';
			$post_image_height = isset( $post_thumb_setting['pcp_image_crop_size']['right'] ) ? $post_thumb_setting['pcp_image_crop_size']['right'] : '';
			$post_image_crop   = isset( $post_thumb_setting['pcp_image_crop_size']['style'] ) ? $post_thumb_setting['pcp_image_crop_size']['style'] : '';
			$thumb_full_src    = wp_get_attachment_image_src( $thumb_id, 'full' );
			$thumb_full_src    = is_array( $thumb_full_src ) ? $thumb_full_src : array( '', '', '' );
			$image_src         = wp_get_attachment_image_src( $thumb_id, $image_sizes );
			$image_src         = is_array( $image_src ) ? $image_src : array( '', '', '' );

			if ( ( 'custom' === $image_sizes ) && ( ! empty( $post_image_width ) && $thumb_full_src[1] >= $post_image_width ) && ( ! empty( $post_image_height ) && $thumb_full_src[2] >= $post_image_height ) ) {
				$hard_crop = 'Hard-crop' === $post_image_crop ? true : false;

				$image = sp_pcp_resize( $thumb_full_src[0], $post_image_width, $post_image_height, $hard_crop );
				if ( $show_2x_image && ( $thumb_full_src[1] >= ( $post_image_width * 2 ) ) && $thumb_full_src[2] >= ( $post_image_height * 2 ) ) {
					$image_resize_2x_url = sp_pcp_resize( $thumb_full_src[0], $post_image_width * 2, $post_image_height * 2, $hard_crop );
				} elseif ( $show_2x_image && ( ( $post_image_width * 2 ) === $thumb_full_src[1] ) && ( $post_image_height * 2 ) === $thumb_full_src[2] ) {
					$image_resize_2x_url = $thumb_full_src[0];
				}
				$image_width  = $post_image_width;
				$image_height = $post_image_height;
			} else {
				$image        = ! empty( $image_src[0] ) ? $image_src[0] : $placeholder_img;
				$image_width  = ! empty( $image_src[1] ) ? $image_src[1] : 600;
				$image_height = ! empty( $image_src[2] ) ? $image_src[2] : 450;
			}
		} else {
			$image        = $placeholder_img;
			$image_width  = 600;
			$image_height = 450;
		}
		$pcp_image_attr = array(
			'src'        => $image,
			'2x_src'     => $image_resize_2x_url,
			'width'      => $image_width,
			'height'     => $image_height,
			'video'      => ! empty( $replace_thumb['video'] ) ? $replace_thumb['video'] : '',
			'audio'      => ! empty( $replace_thumb['audio'] ) ? $replace_thumb['audio'] : '',
			'aria_label' => ! empty( $replace_thumb['aria_label'] ) ? $replace_thumb['aria_label'] : 'feature_image',
		);
		return $pcp_image_attr;
	}


	/**
	 * Thumb Sized function
	 *
	 * @param  array $view_options options array.
	 * @param  int   $slide_id post id.
	 * @param  bool  $is_attachment type attachment.
	 * @return array
	 */
	public static function pcp_sized_popup_thumb( $view_options, $slide_id, $is_attachment = false ) {
		$thumb_id     = '';
		$image        = '';
		$image_height = '';
		$image_width  = '';
		if ( has_post_thumbnail( $slide_id ) ) {
			$thumb_id = get_post_thumbnail_id( $slide_id );
		}
		if ( $is_attachment ) {
			$thumb_id = $slide_id;
		}
		if ( ! empty( $thumb_id ) ) {
			$image_sizes       = isset( $view_options['pcp_popup_img_sizes'] ) ? $view_options['pcp_popup_img_sizes'] : 'medium';
			$post_image_width  = isset( $view_options['pcp_popup_image_crop_size']['top'] ) ? $view_options['pcp_popup_image_crop_size']['top'] : '';
			$post_image_height = isset( $view_options['pcp_popup_image_crop_size']['right'] ) ? $view_options['pcp_popup_image_crop_size']['right'] : '';
			$post_image_crop   = isset( $view_options['pcp_popup_image_crop_size']['style'] ) ? $view_options['pcp_popup_image_crop_size']['style'] : '';
			$thumb_full_src    = wp_get_attachment_image_src( $thumb_id, 'full' );
			$thumb_full_src    = is_array( $thumb_full_src ) ? $thumb_full_src : array( '', '', '' );
			$image_src         = wp_get_attachment_image_src( $thumb_id, $image_sizes );
			$image_src         = is_array( $image_src ) ? $image_src : array( '', '', '' );
			if ( ( 'custom' === $image_sizes ) && ( ! empty( $post_image_width ) && $thumb_full_src[1] >= $post_image_width ) && ( ! empty( $post_image_height ) && $thumb_full_src[2] >= $post_image_height ) ) {
				$hard_crop = 'Hard-crop' === $post_image_crop ? true : false;

				$image        = sp_pcp_resize( $thumb_full_src[0], $post_image_width, $post_image_height, $hard_crop );
				$image_width  = $post_image_width;
				$image_height = $post_image_height;
			} else {
				$image        = ! empty( $image_src[0] ) ? $image_src[0] : '';
				$image_width  = ! empty( $image_src[1] ) ? $image_src[1] : 600;
				$image_height = ! empty( $image_src[2] ) ? $image_src[2] : 450;
			}
		}
		return array(
			'src'    => $image,
			'width'  => $image_width,
			'height' => $image_height,
		);
	}
	/**
	 * Thumb Sized function
	 *
	 * @param array   $post_thumb_setting Thumbnails options array.
	 * @param integer $slide_id The slide/post ID.
	 *
	 * @return string
	 */
	public static function pcp_thumb_replace( $post_thumb_setting, $slide_id ) {
		$pcp_thumb_src_replace = isset( $post_thumb_setting['pcp_thumb_src_replace'] ) ? $post_thumb_setting['pcp_thumb_src_replace'] : '';

		if ( is_array( $pcp_thumb_src_replace ) ) {
			$image_src    = in_array( 'image', $pcp_thumb_src_replace, true ) ? true : false;
			$video_src    = in_array( 'video', $pcp_thumb_src_replace, true ) ? true : false;
			$audio_src    = in_array( 'audio', $pcp_thumb_src_replace, true ) ? true : false;
			$content_post = get_post( $slide_id );
			$content      = $content_post->post_content;

			$test          = preg_match_all(
				'((https?|ftp|gopher|telnet|file|notes|ms-help):' .
				'((//)|(\\\\))+[\w\d:#@%/;$()~_?\+-=\\\.&]*)',
				$content,
				$matches,
				PREG_PATTERN_ORDER
			);
			$first_content = 'image';

			if ( ! empty( $matches[0][0] ) ) {
				if ( strpos( $matches[0][0], '.jpg' ) || strpos( $matches[0][0], '.jpeg' ) || strpos( $matches[0][0], '.gif' ) || strpos( $matches[0][0], '.png' ) || strpos( $matches[0][0], '.eps' ) ) {
					$first_content = 'image';
				}
				if ( strpos( $matches[0][0], 'video' ) || strpos( $matches[0][0], 'youtube' ) || strpos( $matches[0][0], 'vimeo' ) || strpos( $matches[0][0], '.mp4' ) ) {
					$first_content = 'video';
				}
				if ( strpos( $matches[0][0], 'audio' ) || strpos( $matches[0][0], 'soundcloud' ) ) {
					$first_content = 'audio';
				}
			}
			$image_id = 'image' === $first_content ? self::pcp_get_img_from_post( $post_thumb_setting, $slide_id ) : '';
			$is_video = 'video' === $first_content ? self::pcp_get_video_from_post( $slide_id ) : '';
			$is_audio = 'audio' === $first_content ? self::pcp_get_audios_from_post( $slide_id ) : '';

			if ( $image_src && $video_src && $audio_src ) {
				$thumb_id    = $image_id;
				$video_thumb = $is_video;
				$audio_thumb = $is_audio;
			}
			if ( $image_src && $video_src ) {
				$thumb_id    = $image_id;
				$video_thumb = $is_video;
			}
			if ( $image_src && $audio_src ) {
				$thumb_id    = $image_id;
				$audio_thumb = $is_audio;
			}
			if ( $audio_src && $video_src ) {
				$video_thumb = $is_video;
				$audio_thumb = $is_audio;
			}
			if ( $image_src ) {
				$thumb_id = self::pcp_get_img_from_post( $post_thumb_setting, $slide_id );
			}
			if ( $video_src && ( empty( $matches[1][0] ) || ! $image_src ) ) {
				$video_thumb = self::pcp_get_video_from_post( $slide_id );
			}
			if ( $audio_src && empty( $video_thumb ) && ( empty( $matches[1][0] ) || ! $image_src ) ) {
				$audio_thumb = self::pcp_get_audios_from_post( $slide_id );
			}
		}
		$pcp_thumbs_replace_src = array(
			'id'         => ! empty( $thumb_id ) ? $thumb_id : '',
			'video'      => ! empty( $video_thumb ) ? $video_thumb : '',
			'audio'      => ! empty( $audio_thumb ) ? $audio_thumb : '',
			'aria_label' => ! empty( $first_content ) ? $first_content : '',
		);
		return $pcp_thumbs_replace_src;
	}

	/**
	 * Get first image from post content
	 *
	 * @since 1.0
	 * @param array  $post_thumb_setting Thumbnails options array.
	 * @param  number $slide_id Post id.
	 * @return mixed
	 */
	public static function pcp_get_img_from_post( $post_thumb_setting, $slide_id ) {
		$pcp_thumb_src = isset( $post_thumb_setting['pcp_thumb_src'] ) ? $post_thumb_setting['pcp_thumb_src'] : 'featured_image';
		$content_post  = get_post( $slide_id );
		$content       = $content_post->post_content;
		$images        = preg_match_all( '/<img[^>]* src=\"([^\"]*)\"[^>]*>/i', $content, $matches );
		if ( $images ) {
			$img_url  = 'last_img_content' === $pcp_thumb_src ? array_values( array_slice( $matches[1], -1 ) ) : $matches[1][0];
			$thumb_id = self::pcp_image_id_by_url( $img_url, $slide_id );
		}
		if ( ! empty( $thumb_id ) ) {
			return $thumb_id;
		}
	}

	/**
	 * Get first video from post content
	 *
	 * @since 1.0
	 * @param  number $slide_id Post id.
	 * @return mixed
	 */
	public static function pcp_get_video_from_post( $slide_id ) {

		$post    = get_post( $slide_id );
		$content = do_shortcode( apply_filters( 'the_content', $post->post_content ) );
		$embeds  = apply_filters( 'pcp_get_post_video', get_media_embedded_in_content( $content ) );

		if ( empty( $embeds ) ) {
			return '';
		}

		// check what is the first embed containg video tag, youtube or vimeo.
		foreach ( $embeds as $embed ) {
			if ( strpos( $embed, 'video' ) || strpos( $embed, 'youtube' ) || strpos( $embed, 'vimeo' ) || strpos( $embed, '.mp4' ) ) {
				return $embed;
			}
		}
	}

	/**
	 * Get audio files from post content
	 *
	 * @param  number $slide_id Post id.
	 * @return mixed          Iframe.
	 */
	public static function pcp_get_audios_from_post( $slide_id ) {
		// For audio post type - grab.
		$post    = get_post( $slide_id );
		$content = do_shortcode( apply_filters( 'sp_pcp_the_content', $post->post_content ) );
		$embeds  = apply_filters( 'pcp_get_post_audio', get_media_embedded_in_content( $content ) );
		if ( empty( $embeds ) ) {
			return '';
		}

		// check what is the first embed containg video tag, youtube or vimeo.
		foreach ( $embeds as $embed ) {
			if ( strpos( $embed, 'audio' ) || strpos( $embed, 'soundcloud' ) ) {
				return $embed;
			}
		}
	}

	/**
	 * Process all the post meta.
	 *
	 * @param object $post The selected post.
	 * @param array  $post_meta_fields The selected post meta to show.
	 * @param int    $visitor_count Number of visitor saw the post.
	 * @param string $meta_separator The post meta separator.
	 * @param string $is_table the table layout check.
	 * @return void
	 */
	public static function pcp_get_post_meta( $post, $post_meta_fields, $visitor_count, $meta_separator, $is_table ) {
		$meta_wrapper_start_tag = ! $is_table ? apply_filters( 'pcp_post_meta_wrapper_start', '<ul>' ) : '';
		$meta_wrapper_end_tag   = ! $is_table ? apply_filters( 'pcp_post_meta_wrapper_end', '</ul>' ) : '';
		$meta_separator         = isset( $meta_separator ) ? $meta_separator : ' ';
		echo wp_kses_post( $meta_wrapper_start_tag );
		$i = 0;
		foreach ( $post_meta_fields as $each_meta ) {
			$selected_meta        = isset( $each_meta['select_post_meta'] ) ? $each_meta['select_post_meta'] : '';
			$taxonomy_name        = isset( $each_meta['post_meta_taxonomy'] ) ? $each_meta['post_meta_taxonomy'] : '';
			$author_avatar        = isset( $each_meta['post_meta_author_avatar'] ) ? $each_meta['post_meta_author_avatar'] : 'name_with_icon';
			$meta_date_format     = isset( $each_meta['post_meta_date_format'] ) ? $each_meta['post_meta_date_format'] : 'default';
			$custom_date_format   = isset( $each_meta['pcp_custom_meta_date_format'] ) ? $each_meta['pcp_custom_meta_date_format'] : 'F j, Y';
			$word_per_minute      = isset( $each_meta['pcp_word_per_minute'] ) ? $each_meta['pcp_word_per_minute'] : 300;
			$reading_time_postfix = isset( $each_meta['reading_time_postfix'] ) ? $each_meta['reading_time_postfix'] : ' Min Read';

			$meta_icon      = ! empty( $each_meta['select_meta_icon'] ) ? sprintf( '<i class="' . $each_meta['select_meta_icon'] . '"></i>' ) : '';
			$start_tag      = $is_table ? '<td class="sp-pcp-post-meta">' : '<li class="sps-meta-type-' . $selected_meta . '">';
			$end_tag        = $is_table ? '</td>' : '</li>';
			$meta_tag_start = apply_filters( 'pcp_post_meta_html_tag_start', $start_tag );
			$meta_tag_end   = apply_filters( 'pcp_post_meta_html_tag_end', $end_tag );
			$allowed_html   = array(
				'a'    => array(
					'href'  => array(),
					'title' => array(),
				),
				'i'    => array(
					'class' => array(),
					'id'    => array(),
				),
				'span' => array(
					'class' => array(),
					'id'    => array(),
				),
				'div'  => array(
					'class' => array(),
					'id'    => array(),
				),
			);

			switch ( $selected_meta ) {
				case 'author':
					if ( 0 < $i ) {
						echo wp_kses_post( $meta_separator );
					}
					echo wp_kses_post( $meta_tag_start );
					$guest_author = get_the_terms( $post->ID, 'author' );
					$author_name  = is_array( $guest_author ) && ! empty( $guest_author[0]->name ) ? $guest_author[0]->name : get_the_author_meta( 'display_name', $post->post_author );
					$author_url   = is_array( $guest_author ) && ! empty( $guest_author[0]->name ) ? get_term_link( $guest_author[0]->term_id ) : get_author_posts_url( $post->post_author );
					$gravatar     = apply_filters( 'sp_pcp_author_gravatar', get_avatar_url( get_the_author_meta( 'ID', $post->post_author ) ), $post->ID );

					if ( 'show_name' === $author_avatar ) {
						$author_output = esc_html( $author_name );
					} elseif ( 'show_gravatar' === $author_avatar ) {
						$author_output = sprintf( '<img src="%1$s"  width="16"  height="16" title=" %2$s">', $gravatar, esc_html( $author_name ) );
					} elseif ( 'name_with_gravatar' === $author_avatar ) {
						$author_output = sprintf( '<img src="%1$s"  width="16"  height="16" title=" %2$s">%3$s', $gravatar, esc_html( $author_name ), esc_html( $author_name ) );
					} elseif ( 'name_with_icon' === $author_avatar ) {
						$meta_icon     = ! empty( $meta_icon ) ? $meta_icon : '<i class="fa fa-user"></i>';
						$author_output = sprintf( '%1$s %2$s', wp_kses( $meta_icon, $allowed_html ), esc_html( $author_name ) );
					}
					?>
					<a href="<?php echo esc_url( $author_url ); ?>" rel="author"><?php echo wp_kses_post( $author_output ); ?></a>
					<?php
					echo wp_kses_post( $meta_tag_end );
					break;
				case 'date':
					if ( 0 < $i ) {
						echo wp_kses_post( $meta_separator );
					}
					echo wp_kses_post( $meta_tag_start );
					if ( 'default' === $meta_date_format ) {
						$post_date = esc_html( date_i18n( get_option( 'date_format' ), strtotime( $post->post_date ) ) );
					} elseif ( 'time_ago' === $meta_date_format ) {
						$post_date = human_time_diff( date_i18n( 'U', strtotime( $post->post_date ) ), current_time( 'timestamp' ) ) . __( ' ago', 'smart-post-show-pro' );
					} elseif ( 'custom' === $meta_date_format ) {
						$post_date = esc_html( date_i18n( $custom_date_format, strtotime( $post->post_date ) ) );
					}
					?>
					<?php echo wp_kses( $meta_icon, $allowed_html ); ?>
					<time class="entry-date published updated"><?php echo wp_kses_post( $post_date ); ?></time>
					<?php
					echo wp_kses_post( $meta_tag_end );
					break;
				case 'taxonomy':
					if ( 'beside_meta' === $each_meta['pcp_meta_position'] ) {
						$term = self::pcp_taxonomy_terms( $taxonomy_name, $post->ID, $meta_icon );
						if ( ! empty( $term ) ) {
							if ( 0 < $i ) {
								echo wp_kses_post( $meta_separator );
							}
							echo wp_kses_post( $meta_tag_start );
							echo wp_kses_post( $term );
							echo wp_kses_post( $meta_tag_end );
						}
					}
					break;
				case 'comment_count':
					if ( ! empty( $post->comment_count ) ) {
						if ( 0 < $i ) {
							echo wp_kses_post( $meta_separator );
						}
						echo wp_kses_post( $meta_tag_start );
						?>
						<?php echo wp_kses( $meta_icon, $allowed_html ); ?>
							<a href=<?php echo esc_url( get_permalink( $post ) ); ?>><?php echo esc_html( $post->comment_count ); ?></a>
						<?php
						echo wp_kses_post( $meta_tag_end );
					} elseif ( $is_table ) {
						echo wp_kses_post( $meta_tag_start . ' ' . $meta_tag_end );
					}
					break;
				case 'view_count':
					if ( 0 < $i ) {
						echo wp_kses_post( $meta_separator );
					}
					echo wp_kses_post( $meta_tag_start );
					?>
					<?php echo wp_kses( $meta_icon, $allowed_html ); ?> <span><?php echo esc_html( $visitor_count ); ?></span>
					<?php
						echo wp_kses_post( $meta_tag_end );
					break;
				case 'like':
					if ( 0 < $i ) {
						echo wp_kses_post( $meta_separator );
					}
					echo wp_kses_post( $meta_tag_start );
					?>
					<?php echo SP_PCP_User_Like::get_pcp_likes_button( $post->ID ); ?>
					<?php
						echo wp_kses_post( $meta_tag_end );
					break;
				case 'reading_time':
					if ( 0 < $i ) {
						echo wp_kses_post( $meta_separator );
					}
					echo wp_kses_post( $meta_tag_start );
					?>
					<?php echo wp_kses( $meta_icon, $allowed_html ); ?> <span><?php self::pcp_reading_time( $post->ID, $word_per_minute, $reading_time_postfix ); ?></span>
					<?php
						echo wp_kses_post( $meta_tag_end );
					break;
			}
			++$i;
		} // End Foreach.
		echo wp_kses_post( $meta_wrapper_end_tag );
	}
	/**
	 * Pcp_get_post_meta_on_table_heading
	 *
	 * @param  mixed $post_meta_fields The selected post meta to show.
	 * @return void
	 */
	public static function pcp_get_post_meta_on_table_heading( $post_meta_fields ) {
		$i = 0;
		foreach ( (array) $post_meta_fields as $each_meta ) {
			$selected_meta = isset( $each_meta['select_post_meta'] ) ? $each_meta['select_post_meta'] : '';
			$taxonomy_name = isset( $each_meta['post_meta_taxonomy'] ) ? $each_meta['post_meta_taxonomy'] : '';
			switch ( $selected_meta ) {
				case 'author':
					?>
					<th><?php esc_html_e( 'Author', 'smart-post-show-pro' ); ?></th>
					<?php
					break;
				case 'date':
					?>
					<th><?php esc_html_e( 'Date', 'smart-post-show-pro' ); ?></th>
					<?php
					break;
				case 'taxonomy':
					if ( 'beside_meta' === $each_meta['pcp_meta_position'] ) {
						?>
					<th><?php echo $taxonomy_name; ?></th>
						<?php
					}
					break;
				case 'comment_count':
					?>
					<th><?php esc_html_e( 'Comment', 'smart-post-show-pro' ); ?></th>
						<?php

					break;
				case 'view_count':
					?>
					<th><?php esc_html_e( 'View count', 'smart-post-show-pro' ); ?></th>
						<?php
					break;
				case 'like':
					?>
					<th><?php esc_html_e( 'Like', 'smart-post-show-pro' ); ?></th>
						<?php
					break;
				case 'reading_time':
					?>
					<th><?php esc_html_e( 'Reading count', 'smart-post-show-pro' ); ?></th>
						<?php
					break;
			}
			++$i;
		} // End Foreach.
	}
	/**
	 * Show reading time for the post.
	 *
	 * @param int    $post_id The post ID.
	 * @param int    $word_per_minute Number of word can be read.
	 * @param string $reading_time_postfix Text appear after the reading time.
	 *
	 * @return void
	 */
	public static function pcp_reading_time( $post_id, $word_per_minute, $reading_time_postfix ) {
		$content      = get_post_field( 'post_content', $post_id );
		$word_count   = str_word_count( strip_tags( $content ) );
		$reading_time = ceil( $word_count / $word_per_minute );

		$total_reading_time = $reading_time . $reading_time_postfix;

		echo esc_html( $total_reading_time );
	}

	/**
	 * Process the taxonomy terms.
	 *
	 * @param array $taxonomy The taxonomy name to show terms from.
	 * @param mixed $id ids.
	 * @param mixed $meta_icon The meta icon html.
	 * @return statement
	 */
	public static function pcp_taxonomy_terms( $taxonomy, $id, $meta_icon = null ) {
		$terms = get_the_term_list( $id, $taxonomy, '', ', ' );
		if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) {
			return $meta_icon . $terms;
		}
	}

	/**
	 * Get navigation icons based on the specified type.
	 *
	 * @param string $navigation_icon The type of navigation icon.
	 * @return array An associative array with left and right arrow icons.
	 */
	public static function navigation_icon( $navigation_icon ) {
		// Initialize variables for left and right arrows.
		$left_arrow  = '';
		$right_arrow = '';

		switch ( $navigation_icon ) {
			case 'angle':
				$left_arrow  = 'sps-icon-angle-left';
				$right_arrow = 'sps-icon-angle-right';
				break;
			case 'chevron_open_big':
				$left_arrow  = 'sps-icon-left-open-big';
				$right_arrow = 'sps-icon-right-open-big';
				break;
			case 'chevron':
				$left_arrow  = 'sps-icon-left-open-2';
				$right_arrow = 'sps-icon-right-open-1';
				break;
			case 'right_open':
				$left_arrow  = 'sps-icon-left-open';
				$right_arrow = 'sps-icon-right-open';
				break;
			case 'right_open_3':
				$left_arrow  = 'sps-icon-left-open-4';
				$right_arrow = 'sps-icon-right-open-3';
				break;
			case 'right_open_outline':
				$left_arrow  = 'sps-icon-left-open-outline';
				$right_arrow = 'sps-icon-right-open-outline';
				break;
			case 'arrow':
				$left_arrow  = 'sps-icon-left';
				$right_arrow = 'sps-icon-right';
				break;
			case 'triangle':
				$left_arrow  = 'sps-icon-arrow-triangle-left';
				$right_arrow = 'sps-icon-arrow-triangle-right';
				break;
		}
		// Return an associative array with the left and right arrow icons.
		return array(
			'left'  => $left_arrow,
			'right' => $right_arrow,
		);
	}

	/**
	 * Show post tags with separator.
	 *
	 * @param integer $post_id The post ID.
	 * @return statement.
	 */
	public static function pcp_post_tags( $post_id ) {
		$post_tags = get_the_tags( $post_id );
		$separator = ', ';
		$output    = '';
		if ( ! empty( $post_tags ) ) {
			foreach ( $post_tags as $tag ) {
				$output .= '<a href="' . get_tag_link( $tag->term_id ) . '">' . $tag->name . '</a>' . $separator;
			}
			return trim( $output, $separator );
		}
	}

	/**
	 * Maximum pages.
	 *
	 * @param int $total_post Number of total posts.
	 * @param int $post_per_page Posts per page.
	 *
	 * @return void
	 */
	public static function pcp_max_pages( $total_post, $post_per_page ) {
		if ( ! $total_post ) {
			return;
		}
		$max_num_pages = ceil( $total_post / $post_per_page );
		return (int) $max_num_pages;
	}

	/**
	 * Maximum pages.
	 *
	 * @param int $total_post Number of total posts.
	 * @param int $post_per_page Posts per page.
	 * @param int $post_per_click Posts per click load.
	 *
	 * @return void
	 */
	public static function pcp_per_click_max_pages( $total_post, $post_per_page, $post_per_click ) {
		if ( ! $total_post ) {
			return;
		}
		$total_post    = $total_post - $post_per_page;
		$max_num_pages = ceil( $total_post / $post_per_click );
		return (int) $max_num_pages + 1;
	}

	/**
	 * Post per page.
	 *
	 * @param int $limit Post Limit.
	 * @param int $post_per_page post per page.
	 * @param int $page paged number.
	 *
	 * @return int
	 */
	public static function pcp_post_per_page( $limit, $post_per_page, $page ) {
		$limit               = ( empty( $limit ) || '-1' === $limit ) ? 10000000 : $limit;
		$offset              = (int) $post_per_page * ( $page - 1 );
		$final_post_per_page = $post_per_page;
		if ( intval( $post_per_page ) > $limit - $offset ) {
			$final_post_per_page = $limit - $offset;
		}
		return $final_post_per_page;
	}


	/**
	 * Advertisement adjust with post_per_page
	 *
	 * @param  array $view_options options array.
	 * @param  int   $post_per_page post per page.
	 * @return int
	 */
	public static function adv_adjust_with_post_per_page( $view_options, $post_per_page ) {
		$ads_array           = self::pcp_metabox_value( 'sps_ads', $view_options );
		$add_position_number = array();
		$ads_content         = array();
		$add_count           = 0;
		$adv_position        = self::pcp_metabox_value( 'adv_position', $view_options );
		$adv_position_number = self::pcp_metabox_value( 'adv_position_number', $view_options );
		$repeat_ads          = self::pcp_metabox_value( 'repeat_ads', $view_options );
		if ( $ads_array && is_array( $ads_array ) ) {
			foreach ( $ads_array as $key => $value ) {
				$ads_content[ $key ] = $value['ads_content'];
			}
			$add_count = count( $ads_content );
		}
		if ( 'random' == $adv_position ) {
			$repeat_ads   = (int) $repeat_ads * $add_count;
			$possible_ads = $post_per_page / 2;
			$possible_ads = ( $possible_ads >= $repeat_ads ) ? $repeat_ads : $possible_ads;
		} else {
			if ( strpos( $adv_position_number, ',' ) ) {
				$add_position_number = explode( ',', $adv_position_number );
			} else {
				$add_position_number = explode( ' ', $adv_position_number );
			}
			$repeat_ads   = count( $add_position_number );
			$possible_ads = $post_per_page / 2;
			$possible_ads = ( $possible_ads >= $repeat_ads ) ? $repeat_ads : $possible_ads;
		}
		return $post_per_page - $possible_ads;
	}


	/**
	 * Pagination last page post
	 *
	 * @param [type] $limit total post limit.
	 * @param [type] $post_per_page post per page.
	 * @param [type] $total_page last post page.
	 *
	 * @return int.
	 */
	public static function pcp_last_page_post( $limit, $post_per_page, $total_page ) {
		$limit              = ( empty( $limit ) || '-1' === $limit ) ? 10000000 : $limit;
		$offset             = $post_per_page * ( $total_page - 1 );
		$pcp_last_page_post = $limit - $offset;
		return $pcp_last_page_post;
	}

	/**
	 * Get view option from view ID
	 *
	 * @param string $smart_show_id ID of custom field.
	 *
	 * @return array
	 */
	public static function view_options( $smart_show_id ) {
		if ( ! $smart_show_id ) {
			return;
		}
		$view_options = get_post_meta( $smart_show_id, 'sp_pcp_view_options', true );
		return $view_options;
	}

	/**
	 * Get value of a setting from global settings array
	 *
	 * @param string     $field        The full name of setting to get value.
	 * @param array      $array_to_get Array to get values of wanted setting.
	 * @param mixed|null $assign       The value to assign if setting is not found.
	 */
	public static function pcp_metabox_value( $field, $array_to_get = null, $assign = null ) {
		global $smart_show_id;
		if ( empty( $array_to_get ) ) {
			$array_to_get = self::view_options( $smart_show_id );
		}
		return isset( $array_to_get[ $field ] ) ? $array_to_get[ $field ] : $assign;
	}

	/**
	 * Convert the slug text to a name.
	 *
	 * @param string $slug The slug string.
	 * @return string
	 */
	public static function slug_string_to_name( $slug ) {
		$slug = preg_replace( '/[_\-]+/', ' ', $slug );

		return ucwords( $slug );
	}

	/**
	 * Custom Template locator .
	 *
	 * @param  mixed $template_name template name .
	 * @param  mixed $template_path template path .
	 * @param  mixed $default_path default path .
	 * @return string
	 */
	public static function pcp_locate_template( $template_name, $template_path = '', $default_path = '' ) {
		if ( ! $template_path ) {
			$template_path = 'smart-post-show-pro/templates';
		}

		if ( ! $default_path ) {
			$default_path = SP_PCP_PATH . 'public/templates/';
		}
		$template = locate_template( trailingslashit( $template_path ) . $template_name );
		// Get default template.
		if ( ! $template ) {
			$template = $default_path . $template_name;
		}
		// Return what we found.
		return $template;
	}
}
