Gift Media snippets

PHOTO EMBED

Mon Nov 13 2023 23:18:05 GMT+0000 (Coordinated Universal Time)

Saved by @FOrestNAtion

/*gift_slide_front_all MAIN SLIDE MEDIA*/
function gift_slide_front_all($atts) {

        // Define default attributes
        $attributes = shortcode_atts(
            array(
                'gift_slide' => 'one',
            ), 
            $atts
        );
    
        $gift_slide = $attributes['gift_slide'];
        
        if ($gift_slide == 'one') { $gift_slide_number = 'slide1_type'; }
        if ($gift_slide == 'two') { $gift_slide_number = 'slide2_type'; }
        if ($gift_slide == 'three') { $gift_slide_number = 'slide3_type'; }
        if ($gift_slide == 'four') { $gift_slide_number = 'slide4_type'; }
    $slidetype = get_field($gift_slide_number, get_the_ID());
    if ( $slidetype == 'photo editor') {
        if ($gift_slide == 'one') { $gift_field = 'gift_image'; }
        if ($gift_slide == 'two') { $gift_field = 'gift_image_2'; }
        if ($gift_slide == 'three') { $gift_field = 'gift_image_3'; }
        if ($gift_slide == 'four') { $gift_field = 'gift_image_4'; }
    	$img_id = get_field($gift_field, get_the_ID());
		if (!empty($img_id)) {
			$media_url = wp_get_attachment_image_url($img_id, 'full');      
			if (!empty($media_url)) {
              return $media_url;
		}
	}
} else {
        if ($gift_slide == 'one') { $gift_field = 'gif1'; }
        if ($gift_slide == 'two') { $gift_field = 'gif2'; }
        if ($gift_slide == 'three') { $gift_field = 'gif3'; }
        if ($gift_slide == 'four') { $gift_field = 'gif4'; }
    	$media_id = get_field($gift_field, get_the_ID());
        if (!empty($media_id) && is_numeric($media_id)){
    $media_url = wp_get_attachment_url($media_id);
    if (function_exists('get_url_without_url_params')) {
        $media_url = get_url_without_url_params( $media_url );
    }
    $filetype = wp_check_filetype($media_url);
        if ($filetype['ext'] == 'mp4') {                    
           return '<video id="media'. $gift_field .'" autoplay muted>
            <source src="' . $media_url . '" type="audio/mpeg">
            Your browser does not support the audio tag.
            </video>';
        }else{
           return '<img id="media'. $gift_field .'" src="' . $media_url . '">';
        }
}
if (!empty($media_id) && !is_numeric($media_id)){
    $media_url = $media_id; 
    $filetype = wp_check_filetype($media_id);
       if ($filetype['ext'] == 'mp4') {
        return '<video id="media'. $gift_field .'" autoplay muted>
            <source src="' . $media_url . '" type="audio/mpeg">
            Your browser does not support the audio tag.
            </video>';
        }else{
           return '<img id="media'. $gift_field .'" src="' . $media_url . '">';                    
        }
}
}
}
// register shortcode
add_shortcode('gift_slide_front_all', 'gift_slide_front_all');




/*gift_slide1_bk_all BACKGROUND SLIDE MEDIA*/
function gift_slide_bkg_all($gift_slide) {
    $ua = strtolower($_SERVER["HTTP_USER_AGENT"]);
    $isMob = is_numeric(strpos($ua, "mobile"));
    if ($isMob != 'mobile') {
        if ($gift_slide == 'one') { $gift_slide_number = 'slide1_type'; }
        if ($gift_slide == 'two') { $gift_slide_number = 'slide2_type'; }
        if ($gift_slide == 'three') { $gift_slide_number = 'slide3_type'; }
        if ($gift_slide == 'four') { $gift_slide_number = 'slide4_type'; }
        $slidetype = get_field($gift_slide_number, get_the_ID());
        if ($slidetype == 'photo editor') {
            if ($gift_slide == 'one') { $gift_field = 'gift_image'; }
            if ($gift_slide == 'two') { $gift_field = 'gift_image_2'; }
            if ($gift_slide == 'three') { $gift_field = 'gift_image_3'; }
            if ($gift_slide == 'four') { $gift_field = 'gift_image_4'; }
            $img_id = get_field($gift_field, get_the_ID());
            if (!empty($img_id)) {
                $media_url = wp_get_attachment_image_url($img_id, 'full');
                if (!empty($media_url)) {
                    return $media_url;
                }
            }
        } else {
            if ($gift_slide == 'one') { $gift_field = 'gif1'; }
            if ($gift_slide == 'two') { $gift_field = 'gif2'; }
            if ($gift_slide == 'three') { $gift_field = 'gif3'; }
            if ($gift_slide == 'four') { $gift_field = 'gif4'; }
            $media_id = get_field($gift_field, get_the_ID());

            if (!empty($media_id) && is_numeric($media_id)) {
                // Try fetching from the _cloudinary meta field
                $cloudinary_data = get_post_meta($media_id, '_cloudinary', true);
                if (isset($cloudinary_data['_cloudinary_url'])) {
                    $media_url = $cloudinary_data['_cloudinary_url'];
                } else {
                    $media_url = wp_get_attachment_url($media_id);
                }
                $filetype = wp_check_filetype($media_url);
                if ($filetype['ext'] == 'mp4') {
                    if (strpos($media_url, "cloudinary.com") !== false) {
                        $parts = explode('/upload/', $media_url);
                        $transformed_media_url = $parts[0]. '/upload/f_auto,q_auto,so_2/'.$parts[1];
                        $transformed_media_url = str_replace(".mp4", ".jpg", $transformed_media_url);
                        return $transformed_media_url;
                    }
                } else {
                    return $media_url;
                }
            }
        }
        if (!empty($media_id) && !is_numeric($media_id)) {
            $media_url = $media_id;
            $filetype = wp_check_filetype($media_id);
            if ($filetype['ext'] == 'mp4') {
                $media_name = basename(get_attached_file($media_id));
                $media_name_jpg = str_replace(".mp4", ".jpg", "$media_name");
                return "https://res.cloudinary.com/drt2tlz1j/video/upload/fn/$media_name_jpg";
            } else {
                return $media_url;
            }
        }
    }
}
// register shortcode
add_shortcode('gift_slide_bkg_all', 'gift_slide_bkg_all');
content_copyCOPY