/*
Theme Name: Radios
Theme URI: http://underscores.me/
Author: themexriver
Author URI: http://themeforest.net/user/themexriver
Description: Radios - Electronics Shop WooCommerce Theme
Version: 1.1
Tested up to: 5.4
Requires PHP: 5.6
License: GNU General Public License v2 or later
License URI: LICENSE
Text Domain: radios
Tags: custom-background, custom-logo, custom-menu, featured-images, threaded-comments, translation-ready
*/

function taktheme_upload_svg_files( $allowed ) {
    if ( !current_user_can( 'manage_options' ) )
        return $allowed;
    $allowed['svg'] = 'image/svg+xml';
    return $allowed;
}
add_filter( 'upload_mimes', 'taktheme_upload_svg_files');



add_filter( 'use_block_editor_for_post', '__return_false' );
add_filter( 'use_widgets_block_editor', '__return_false' );

function remove_google_fonts() {
      wp_dequeue_style('google-fonts');
  }
  add_action('wp_enqueue_scripts', 'remove_google_fonts', 100);

function taktheme_preload_custom_fonts() {
    echo '<link rel="preload" href="#" as="font" type="font/woff" crossorigin="anonymous">';
    echo '<link rel="preload" href="#" as="font" type="font/woff" crossorigin="anonymous">';
}
add_action('wp_head', 'taktheme_preload_custom_fonts');

function taktheme_preload_custom_fonts() {
    echo '<link rel="preload" href="https://adishop.uk/wp-content/themes/radios/assets/fonts/fa-light-300.ttf" as="font" type="font/woff" crossorigin="anonymous">';
    echo '<link rel="preload" href="https://adishop.uk/wp-content/themes/radios/assets/fonts/fa-regular-400.ttf" as="font" type="font/woff" crossorigin="anonymous">';
	echo '<link rel="preload" href="https://adishop.uk/wp-content/themes/radios/assets/fonts/fa-solid-900.ttf">';
}
add_action('wp_head', 'taktheme_preload_custom_fonts');



add_action( 'wp_enqueue_scripts', function() {
    wp_dequeue_style( 'wp-block-library' );
    wp_dequeue_style( 'wp-block-library-theme' );
    wp_dequeue_style( 'wc-blocks-style' );
    wp_dequeue_style( 'global-styles' );

}, 20 );


 
function remove_jquery_migrate( $scripts ) {
   if ( ! is_admin() && isset( $scripts->registered['jquery'] ) ) {
        $script = $scripts->registered['jquery'];
   if ( $script->deps ) { 
// Check whether the script has any dependencies

        $script->deps = array_diff( $script->deps, array( 'jquery-migrate' ) );
 }
 }
 }
add_action( 'wp_default_scripts', 'remove_jquery_migrate' );




add_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_styles_scripts', 99 );

function dequeue_woocommerce_styles_scripts() {
    if ( function_exists( 'is_woocommerce' ) ) {
        if ( ! is_woocommerce() && ! is_cart() && ! is_checkout() ) {
            # Styles
            wp_dequeue_style( 'woocommerce-general' );
            wp_dequeue_style( 'woocommerce-layout' );
            wp_dequeue_style( 'woocommerce-smallscreen' );
            wp_dequeue_style( 'woocommerce_frontend_styles' );
            wp_dequeue_style( 'woocommerce_fancybox_styles' );
            wp_dequeue_style( 'woocommerce_chosen_styles' );
            wp_dequeue_style( 'woocommerce_prettyPhoto_css' );
            # Scripts
            wp_dequeue_script( 'wc_price_slider' );
            wp_dequeue_script( 'wc-single-product' );
            wp_dequeue_script( 'wc-add-to-cart' );
            wp_dequeue_script( 'wc-cart-fragments' );
            wp_dequeue_script( 'wc-checkout' );
            wp_dequeue_script( 'wc-add-to-cart-variation' );
            wp_dequeue_script( 'wc-single-product' );
            wp_dequeue_script( 'wc-cart' );
            wp_dequeue_script( 'wc-chosen' );
            wp_dequeue_script( 'woocommerce' );
            wp_dequeue_script( 'prettyPhoto' );
            wp_dequeue_script( 'prettyPhoto-init' );
            wp_dequeue_script( 'jquery-blockui' );
            wp_dequeue_script( 'jquery-placeholder' );
            wp_dequeue_script( 'fancybox' );
            wp_dequeue_script( 'jqueryui' );
        }
    }
}


 function taktheme_remove_specific_css_from_html($tag, $handle) {
    $handles_to_remove = [
        'wc-blocks-style-rtl',
        'wc-blocks-style'
    ];

    if (in_array($handle, $handles_to_remove)) {
        return ''; 
    }

    return $tag;
}
add_filter('style_loader_tag', 'taktheme_remove_specific_css_from_html', 10, 2);





// Add a button to the media row actions
function add_webp_convert_button($actions, $post) {
    if ($post->post_mime_type === 'image/jpeg' || $post->post_mime_type === 'image/png') {
        $url = wp_nonce_url(admin_url('admin-ajax.php?action=convert_to_webp&attachment_id=' . $post->ID), 'convert_to_webp');
        $actions['convert_to_webp'] = '<a href="' . esc_url($url) . '">Convert to WebP</a>';
    }
    return $actions;
}
add_filter('media_row_actions', 'add_webp_convert_button', 10, 2);

// Handle the conversion to WebP
function convert_image_to_webp() {
    if (!current_user_can('edit_posts')) {
        wp_die(__('You do not have permission to edit this post.'));
    }

    $attachment_id = intval($_GET['attachment_id']);
    check_admin_referer('convert_to_webp');

    $file = get_attached_file($attachment_id);
    $info = pathinfo($file);
    $new_file = $info['dirname'] . '/' . $info['filename'] . '.webp';

    if ($info['extension'] === 'jpeg' || $info['extension'] === 'jpg' || $info['extension'] === 'png') {
        if ($info['extension'] === 'jpeg' || $info['extension'] === 'jpg') {
            $image = imagecreatefromjpeg($file);
        } elseif ($info['extension'] === 'png') {
            $image = imagecreatefrompng($file);
        }

        if ($image) {
            imagewebp($image, $new_file);
            imagedestroy($image);
            update_attached_file($attachment_id, $new_file);

            // Update the attachment metadata
            $metadata = wp_generate_attachment_metadata($attachment_id, $new_file);
            wp_update_attachment_metadata($attachment_id, $metadata);

            wp_safe_redirect(admin_url('upload.php'));
            exit;
        } else {
            wp_die(__('Error converting image to WebP.'));
        }
    } else {
        wp_die(__('Unsupported image format.'));
    }
}
add_action('wp_ajax_convert_to_webp', 'convert_image_to_webp');



