Loading...
Complete WooCommerce plugin. Auto-enhance product images, bulk processing, and background removal.
<?php
/**
* Plugin Name: ButterflyAPI for WooCommerce
* Description: AI-powered product image enhancement for WooCommerce
* Version: 1.0.0
* Author: ButterflyAPI
* Requires Plugins: woocommerce
*/
defined('ABSPATH') || exit;
class ButterflyAPI_WooCommerce {
private $api_key;
private $api_base = 'https://butterflyapi.com/api/v1';
public function __construct() {
$this->api_key = get_option('butterflyapi_woo_key', '');
// Admin
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_init', [$this, 'register_settings']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin_scripts']);
// Product hooks
add_action('woocommerce_process_product_meta', [$this, 'process_product_images'], 20);
add_action('added_post_meta', [$this, 'on_product_image_set'], 10, 4);
// Bulk actions
add_filter('bulk_actions-edit-product', [$this, 'add_bulk_actions']);
add_filter('handle_bulk_actions-edit-product', [$this, 'handle_bulk_actions'], 10, 3);
// AJAX handlers
add_action('wp_ajax_butterflyapi_process_product', [$this, 'ajax_process_product']);
add_action('wp_ajax_butterflyapi_bulk_process', [$this, 'ajax_bulk_process']);
// Auto-process on upload (if enabled)
if (get_option('butterflyapi_woo_auto_process', false)) {
add_filter('wp_handle_upload', [$this, 'auto_process_upload'], 10, 2);
}
}
public function add_admin_menu() {
add_submenu_page(
'woocommerce',
'ButterflyAPI Settings',
'ButterflyAPI',
'manage_woocommerce',
'butterflyapi-woo-settings',
[$this, 'settings_page']
);
}
public function register_settings() {
register_setting('butterflyapi_woo_settings', 'butterflyapi_woo_key');
register_setting('butterflyapi_woo_settings', 'butterflyapi_woo_auto_process');
register_setting('butterflyapi_woo_settings', 'butterflyapi_woo_default_action');
register_setting('butterflyapi_woo_settings', 'butterflyapi_woo_auto_bg_remove');
}
public function settings_page() {
?>
<div class="wrap">
<h1>ButterflyAPI for WooCommerce</h1>
<form method="post" action="options.php">
<?php settings_fields('butterflyapi_woo_settings'); ?>
<table class="form-table">
<tr>
<th>API Key</th>
<td>
<input type="password" name="butterflyapi_woo_key"
value="<?php echo esc_attr(get_option('butterflyapi_woo_key')); ?>"
class="regular-text" />
<p class="description">
<a href="https://butterflyapi.com/dashboard/keys" target="_blank">Get your API key</a>
</p>
</td>
</tr>
<tr>
<th>Auto-Process Uploads</th>
<td>
<label>
<input type="checkbox" name="butterflyapi_woo_auto_process" value="1"
<?php checked(get_option('butterflyapi_woo_auto_process'), 1); ?> />
Automatically enhance images on upload
</label>
</td>
</tr>
<tr>
<th>Auto Background Removal</th>
<td>
<label>
<input type="checkbox" name="butterflyapi_woo_auto_bg_remove" value="1"
<?php checked(get_option('butterflyapi_woo_auto_bg_remove'), 1); ?> />
Remove backgrounds from product images
</label>
</td>
</tr>
<tr>
<th>Default Processing</th>
<td>
<select name="butterflyapi_woo_default_action">
<option value="image-enhance" <?php selected(get_option('butterflyapi_woo_default_action'), 'image-enhance'); ?>>
Enhance Quality
</option>
<option value="upscale" <?php selected(get_option('butterflyapi_woo_default_action'), 'upscale'); ?>>
Upscale 2x
</option>
<option value="background-remove" <?php selected(get_option('butterflyapi_woo_default_action'), 'background-remove'); ?>>
Remove Background
</option>
</select>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
<hr />
<h2>Bulk Process Products</h2>
<p>Process all product images in your catalog:</p>
<div id="butterflyapi-bulk-controls">
<select id="butterflyapi-bulk-action">
<option value="image-enhance">Enhance All</option>
<option value="background-remove">Remove Backgrounds</option>
<option value="upscale">Upscale 2x</option>
</select>
<button type="button" class="button button-primary" id="butterflyapi-start-bulk">
Start Bulk Processing
</button>
</div>
<div id="butterflyapi-bulk-progress" style="display:none; margin-top: 20px;">
<div style="background: #e0e0e0; height: 20px; border-radius: 4px;">
<div id="butterflyapi-progress-bar" style="background: #0073aa; height: 100%; width: 0; border-radius: 4px; transition: width 0.3s;"></div>
</div>
<p id="butterflyapi-progress-text">0 / 0 products processed</p>
</div>
</div>
<?php
}
public function process_image($image_url, $action, $options = []) {
if (empty($this->api_key)) {
return new WP_Error('no_api_key', 'API key not configured');
}
$body = array_merge([
'api' => $action,
'imageUrl' => $image_url,
], $options);
$response = wp_remote_post($this->api_base . '/run', [
'headers' => [
'Authorization' => 'Bearer ' . $this->api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode($body),
'timeout' => 120,
]);
if (is_wp_error($response)) {
return $response;
}
$result = json_decode(wp_remote_retrieve_body($response), true);
if (isset($result['error'])) {
return new WP_Error('api_error', $result['error']);
}
return $result;
}
public function process_product_images($product_id) {
$product = wc_get_product($product_id);
if (!$product) return;
$image_id = $product->get_image_id();
$gallery_ids = $product->get_gallery_image_ids();
$all_images = array_filter(array_merge([$image_id], $gallery_ids));
$action = get_option('butterflyapi_woo_default_action', 'image-enhance');
foreach ($all_images as $attachment_id) {
$this->process_attachment($attachment_id, $action);
}
}
private function process_attachment($attachment_id, $action) {
$image_url = wp_get_attachment_url($attachment_id);
if (!$image_url) return;
// Check if already processed
if (get_post_meta($attachment_id, '_butterflyapi_processed', true)) {
return;
}
$result = $this->process_image($image_url, $action);
if (is_wp_error($result)) {
return;
}
// Download and replace image
$processed_url = $result['outputUrl'];
$this->replace_attachment_file($attachment_id, $processed_url);
// Mark as processed
update_post_meta($attachment_id, '_butterflyapi_processed', time());
update_post_meta($attachment_id, '_butterflyapi_action', $action);
}
private function replace_attachment_file($attachment_id, $new_url) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
$tmp = download_url($new_url);
if (is_wp_error($tmp)) return;
$file_path = get_attached_file($attachment_id);
@rename($tmp, $file_path);
// Regenerate thumbnails
wp_update_attachment_metadata(
$attachment_id,
wp_generate_attachment_metadata($attachment_id, $file_path)
);
}
public function add_bulk_actions($bulk_actions) {
$bulk_actions['butterflyapi_enhance'] = 'ButterflyAPI: Enhance Images';
$bulk_actions['butterflyapi_removebg'] = 'ButterflyAPI: Remove Backgrounds';
$bulk_actions['butterflyapi_upscale'] = 'ButterflyAPI: Upscale 2x';
return $bulk_actions;
}
public function handle_bulk_actions($redirect_to, $doaction, $post_ids) {
$actions_map = [
'butterflyapi_enhance' => 'image-enhance',
'butterflyapi_removebg' => 'background-remove',
'butterflyapi_upscale' => 'upscale',
];
if (!isset($actions_map[$doaction])) {
return $redirect_to;
}
foreach ($post_ids as $product_id) {
$product = wc_get_product($product_id);
if (!$product) continue;
$image_id = $product->get_image_id();
if ($image_id) {
$this->process_attachment($image_id, $actions_map[$doaction]);
}
}
return add_query_arg('butterflyapi_processed', count($post_ids), $redirect_to);
}
public function ajax_process_product() {
check_ajax_referer('butterflyapi_nonce', 'nonce');
$product_id = intval($_POST['product_id']);
$action = sanitize_text_field($_POST['action_type']);
$product = wc_get_product($product_id);
if (!$product) {
wp_send_json_error('Product not found');
}
$image_id = $product->get_image_id();
if (!$image_id) {
wp_send_json_error('No product image');
}
$this->process_attachment($image_id, $action);
wp_send_json_success(['message' => 'Image processed']);
}
public function auto_process_upload($upload, $context) {
if (!isset($upload['type']) || strpos($upload['type'], 'image') === false) {
return $upload;
}
// Only process in product context
if (isset($_POST['post_id'])) {
$post = get_post($_POST['post_id']);
if ($post && $post->post_type !== 'product') {
return $upload;
}
}
$action = get_option('butterflyapi_woo_default_action', 'image-enhance');
$result = $this->process_image($upload['url'], $action);
if (!is_wp_error($result) && isset($result['outputUrl'])) {
$tmp = download_url($result['outputUrl']);
if (!is_wp_error($tmp)) {
@rename($tmp, $upload['file']);
}
}
return $upload;
}
public function enqueue_admin_scripts($hook) {
if (strpos($hook, 'butterflyapi-woo') === false && $hook !== 'edit.php') {
return;
}
wp_enqueue_script('butterflyapi-woo-admin', plugins_url('admin.js', __FILE__), ['jquery'], '1.0.0', true);
wp_localize_script('butterflyapi-woo-admin', 'butterflyapiWoo', [
'ajax_url' => admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('butterflyapi_nonce'),
]);
}
}
new ButterflyAPI_WooCommerce();wp-content/plugins/butterflyapi-woocommerce/Copy butterflyapi-woocommerce.php and admin.js
WordPress Admin → Plugins → Activate ButterflyAPI for WooCommerce
WooCommerce → ButterflyAPI → Enter your API key
Need help with WooCommerce integration?