<?php

    get_option('enable_post_placement');
    // Showing Default and shortcode button both in single post if enabled from settings so for this 
    // if (get_option('enable_post_placement') !== 'enable_post_placement'){
    if (get_option('enable_post_placement') === 'enable_post_placement'){
        add_filter('the_content', 'add_wishlist_icon_to_posts', 10, 1);
    }


    add_shortcode('wishlist_post', 'wishlist_post_shortcode');

    function wishlist_post_shortcode($atts, $content = null) {
    return add_wishlist_icon_to_posts('');
}



    function add_wishlist_icon_to_posts($content) {
    $all_post_name = get_option('wishev_filter_post_name');
    $post_type = get_post_type(get_the_ID());

    // Cast to array in case saved option is a single string
    $allowed_post_types = (array) $all_post_name;

    global $post;


    // ❌ Skip WooCommerce single product pages (important)
    if ( function_exists( 'is_product' ) && is_product() ) {
        return $content;
    }

    // ❌ Skip product post type anywhere
    if ( $post->post_type === 'product' ) {
        return $content;
    }

    // Skip if not allowed CPT
    if (!empty($allowed_post_types) && !in_array($post_type, $allowed_post_types, true)) {
        return $content;
    }


    if (is_single() && is_user_logged_in()) {
        $wishlist_title = get_option('wishlist_name', 'Add to Wishlist');
        $wishlist_icon = '<a href="#" class="single-post wishlist-icon" data-post-id="' . esc_attr(get_the_ID()) . '">' . esc_html($wishlist_title) . '</a>';
        return $wishlist_icon . $content;
    }

    if (is_single() && !is_user_logged_in()) {
        $login_link = '<a href="' . esc_url(wp_login_url()) . '" class="login">Login to add to wishlist</a>';
        return $login_link . $content;
    }

    return $content;
}