




/**
 * 1. Add "Sync Images to Vend" to the top Bulk Actions dropdown.
 */
add_filter( 'bulk_actions-edit-product', 'bbs_register_vend_image_sync_bulk_action' );
function bbs_register_vend_image_sync_bulk_action( $bulk_actions ) {
    $bulk_actions['bbs_sync_images_to_vend'] = 'Sync Images to Vend';
    return $bulk_actions;
}

/**
 * 2. Handle the execution when the bulk action is applied.
 */
add_filter( 'handle_bulk_actions-edit-product', 'bbs_handle_vend_image_sync_bulk_action', 10, 3 );
function bbs_handle_vend_image_sync_bulk_action( $redirect_to, $action, $post_ids ) {
    if ( $action !== 'bbs_sync_images_to_vend' ) {
        return $redirect_to;
    }

    // Ensure user has permissions
    if ( !current_user_can( 'edit_products' ) ) {
        return $redirect_to;
    }

    $synced_count = 0;

    foreach ( $post_ids as $post_id ) {
        // Run your sync function for each selected product ID
        bbs_sync_product_image_to_vend( $post_id );
        $synced_count++;
    }

    // Append a query arg to show a notice optionally
    $redirect_to = add_query_arg( 'bbs_vend_synced', $synced_count, $redirect_to );
    return $redirect_to;
}

/**
 * 3. Optional admin notice to show how many products were processed.
 */
add_action( 'admin_notices', 'bbs_vend_sync_bulk_admin_notice' );
function bbs_vend_sync_bulk_admin_notice() {
    if ( !empty( $_GET['bbs_vend_synced'] ) ) {
        $count = intval( $_GET['bbs_vend_synced'] );
        echo '<div class="notice notice-success is-dismissible"><p>' . sprintf( 'Image sync triggered for %d product(s) to Vend.', $count ) . '</p></div>';
    }
}

/**
 * 4. The main sync function (accessible to any admin/shop manager).
 */
function bbs_sync_product_image_to_vend( $id ) {
    if ( !is_admin() ) {
        return;
    }
    
    if ( !current_user_can( 'edit_products' ) ) {
        return;
    }
    
    // Retrieve the product and ensure it exists.
    $product = wc_get_product( $id );
    if ( !$product || $product->is_type('variation') ) {
        return;
    }
    
    if ( 'product_variation' === get_post_type( $id ) ) {
        return;
    }
    
    if ( !$product->has_child() && !$product->is_type('variable') ) {
        return;
    }
    
    // Get the Vend product ID from the first variation if it's a variable product
    $vend_product_id = '';
    $children = $product->get_children();
    if ( !empty($children) ) {
        $first_variation_id = reset($children);
        $vend_product_id = get_post_meta( $first_variation_id, '_vend_product_id', true );
    }
    
    if ( empty( $vend_product_id ) ) {
        return;
    }

    // Get the image path from WooCommerce
    $image_id = $product->get_image_id();
    $image_path = get_attached_file( $image_id );

    if ( !$image_path || !file_exists($image_path) ) {
        error_log( 'BBS Sync: Local product image not found for product ID ' . $id );
        return;
    }

    $vend_api_key = defined('VEND_API_KEY') ? VEND_API_KEY : '';
    $url = "https://thecreamstoreltd.retail.lightspeed.app/api/2026-07/products/{$vend_product_id}/actions/image_upload";
    
    $cfile = new CURLFile( $image_path );

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt( $ch, CURLOPT_POST, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, [
        'Authorization: Bearer ' . $vend_api_key,
    ]);
    curl_setopt( $ch, CURLOPT_POSTFIELDS, [
        'image' => $cfile
    ]);

    $response = curl_exec( $ch );
    
    if ( $response === false ) {
        error_log( 'BBS Sync cURL error for Product ID ' . $id . ': ' . curl_error($ch) );
    } else {
        error_log( 'BBS Sync Response for Product ID ' . $id . ': ' . $response );
    }
    
    curl_close( $ch );
}<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="//thecreamstore.com/wp-content/plugins/wordpress-seo/css/main-sitemap.xsl"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
	<sitemap>
		<loc>https://thecreamstore.com/post-sitemap.xml</loc>
		<lastmod>2026-07-03T18:58:50+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/page-sitemap.xml</loc>
		<lastmod>2026-07-19T11:43:05+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product-sitemap.xml</loc>
		<lastmod>2026-08-16T12:29:49+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product-sitemap2.xml</loc>
		<lastmod>2026-06-21T09:24:57+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product-sitemap3.xml</loc>
		<lastmod>2026-06-21T09:47:21+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product-sitemap4.xml</loc>
		<lastmod>2026-07-18T13:04:21+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product-sitemap5.xml</loc>
		<lastmod>2026-08-14T17:28:55+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product-sitemap6.xml</loc>
		<lastmod>2026-08-16T12:29:49+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/category-sitemap.xml</loc>
		<lastmod>2026-07-03T18:58:50+00:00</lastmod>
	</sitemap>
	<sitemap>
		<loc>https://thecreamstore.com/product_cat-sitemap.xml</loc>
		<lastmod>2026-08-16T12:29:49+00:00</lastmod>
	</sitemap>
</sitemapindex>
<!-- XML Sitemap generated by Yoast SEO -->