<?php

defined( 'ABSPATH' ) || exit;

/**
 * Convert dimensions string like '1x1' to array [1, 1].
 * Return [1, 1] if $dimension_string is invalid.
 *
 * @since 6.7.0
 *
 * @param string $dimension_string
 *
 * @return array
 */
function the7_shortcode_decode_image_dimension( $dimension_string ) {
	$dimension_string = strtolower( $dimension_string );
	if ( strpos( $dimension_string, 'x' ) === false ) {
		return array( 1, 1 );
	}

	$dimension_array = array();
	foreach ( array_slice( explode( 'x', $dimension_string ), 0, 2 ) as $dimension ) {
		$dimension_array[] = max( (int) $dimension, 1 );
	}

	return $dimension_array;
}

/**
 * Decode typical shortcode responsive columns attribute and add it to $data_atts_array.
 * By default attribute value is empty string.
 * Added attributes:
 * [
 *  'desktop-columns-num' => '',
 *  'v-tablet-columns-num'' => '',
 *  'h-tablet-columns-num'' => '',
 *  'phone-columns-num'' => '',
 * ]
 *
 * @since 6.7.0
 * @uses  DT_VCResponsiveColumnsParam::decode_columns()
 * @uses  absint()
 *
 * @param array  $data_atts_array
 * @param string $encoded_columns
 *
 * @return array
 */
function the7_shortcode_add_responsive_columns_data_attributes( $data_atts_array, $encoded_columns ) {
	$columns     = DT_VCResponsiveColumnsParam::decode_columns( $encoded_columns );
	$columns_map = array(
		'desktop'  => 'desktop',
		'v_tablet' => 'v-tablet',
		'h_tablet' => 'h-tablet',
		'phone'    => 'phone',
	);

	foreach ( $columns_map as $column_name => $data_att_name ) {
		$data_atts_array["{$data_att_name}-columns-num"] = isset( $columns[ $column_name ] ) ? absint( $columns[ $column_name ] ) : '';
	}

	return $data_atts_array;
}

/**
 * Prepare a WPBakery link parameter for shortcode HTML attributes.
 *
 * @param string $link                Shortcode link attribute.
 * @param string $default_url         Default URL.
 * @param bool   $fallback_to_raw_url Use raw link as URL when WPBakery parser returns no URL.
 *
 * @return array
 */
function the7_shortcode_prepare_vc_link( $link, $default_url = '#', $fallback_to_raw_url = false ) {
	$link   = (string) $link;
	$url    = $default_url;
	$title  = '';
	$target = '';
	$rel    = '';

	if ( '' !== $link ) {
		if ( function_exists( 'vc_build_link' ) ) {
			$vc_link = vc_build_link( $link );

			if ( ! empty( $vc_link['url'] ) ) {
				$url    = (string) $vc_link['url'];
				$title  = isset( $vc_link['title'] ) ? (string) $vc_link['title'] : '';
				$target = isset( $vc_link['target'] ) ? trim( (string) $vc_link['target'] ) : '';
				$rel    = isset( $vc_link['rel'] ) ? trim( (string) $vc_link['rel'] ) : '';
			} elseif ( $fallback_to_raw_url ) {
				$url = $link;
			}
		} else {
			$url = $link;
		}
	}

	$rel = the7_shortcode_prepare_link_rel( $rel, $target );

	return array(
		'href'          => esc_url( $url, the7_shortcode_get_link_allowed_protocols() ),
		'title'         => $title,
		'target'        => $target,
		'rel'           => $rel,
		'title_attr'    => ( '' === $title ? '' : sprintf( ' title="%s"', esc_attr( $title ) ) ),
		'target_attr'   => ( '' === $target ? '' : sprintf( ' target="%s"', esc_attr( $target ) ) ),
		'rel_attr'      => ( '' === $rel ? '' : sprintf( ' rel="%s"', esc_attr( $rel ) ) ),
		'button_target' => ( '' === $target ? '' : esc_attr( $target ) ),
	);
}

/**
 * Return protocols allowed in shortcode link attributes.
 *
 * @return string[]
 */
function the7_shortcode_get_link_allowed_protocols() {
	$protocols = array_merge(
		wp_allowed_protocols(),
		array(
			'skype',
			'telegram',
			'tg',
			'viber',
			'whatsapp',
		)
	);

	return array_values( array_unique( apply_filters( 'the7_shortcode_link_allowed_protocols', $protocols ) ) );
}

/**
 * Preserve custom rel values and add noopener for new-window links.
 *
 * @param string $rel    Link rel attribute value.
 * @param string $target Link target attribute value.
 *
 * @return string
 */
function the7_shortcode_prepare_link_rel( $rel, $target ) {
	$rel = trim( (string) $rel );

	if ( '_blank' !== strtolower( trim( (string) $target ) ) ) {
		return $rel;
	}

	$rel_parts = array_filter( explode( ' ', $rel ) );

	foreach ( $rel_parts as $rel_part ) {
		if ( 'noopener' === strtolower( $rel_part ) ) {
			return $rel;
		}
	}

	$rel_parts[] = 'noopener';

	return implode( ' ', $rel_parts );
}

/**
 * Return array of custom icons stylesheets.
 *
 * @since 7.0.0
 *
 * @param array $icons_stylesheets
 *
 * @return array
 */
function the7_get_custom_icons_stylesheets( $icons_stylesheets = array() ) {
	$custom_icons = (array) get_option( 'smile_fonts', array() );
	$upload_dir   = wp_get_upload_dir();

	foreach ( $custom_icons as $icon ) {
		if ( isset( $icon['style'] ) ) {
			$icons_stylesheets[] = $upload_dir['baseurl'] . '/smile_fonts/' . $icon['style'];
		}
	}

	return $icons_stylesheets;
}

/**
 * Return shortcode uid based on $tag and $atts.
 *
 * @since 7.1.3
 *
 * @param string $tag  Shortcode tag.
 * @param array  $atts Shortcode atts array.
 *
 * @return string Shortcode UID.
 */
function the7_get_shortcode_uid( $tag, $atts = array() ) {
	return md5( $tag . json_encode( $atts ) );
}
