Loading...
Complete WordPress plugin with copy-paste installation. Enhance images on upload, bulk process your media library, and integrate with WooCommerce.
<?php
/**
* Plugin Name: ButterflyAPI Image Processor
* Description: AI-powered image processing for WordPress
* Version: 1.0.0
* Author: ButterflyAPI
* License: GPL v2 or later
*/
defined('ABSPATH') || exit;
class ButterflyAPI_Plugin {
private $api_key;
private $api_base = 'https://butterflyapi.com/api/v1';
public function __construct() {
$this->api_key = get_option('butterflyapi_key', '');
add_action('admin_menu', [$this, 'add_admin_menu']);
add_action('admin_init', [$this, 'register_settings']);
add_filter('attachment_fields_to_edit', [$this, 'add_process_button'], 10, 2);
add_action('wp_ajax_butterflyapi_process', [$this, 'ajax_process_image']);
if (get_option('butterflyapi_auto_process', false)) {
add_filter('wp_handle_upload', [$this, 'auto_process_upload']);
}
add_action('admin_enqueue_scripts', [$this, 'enqueue_scripts']);
}
public function add_admin_menu() {
add_options_page('ButterflyAPI Settings', 'ButterflyAPI', 'manage_options', 'butterflyapi-settings', [$this, 'settings_page']);
add_media_page('Bulk Process Images', 'ButterflyAPI Bulk', 'upload_files', 'butterflyapi-bulk', [$this, 'bulk_process_page']);
}
public function register_settings() {
register_setting('butterflyapi_settings', 'butterflyapi_key');
register_setting('butterflyapi_settings', 'butterflyapi_auto_process');
register_setting('butterflyapi_settings', 'butterflyapi_default_action');
register_setting('butterflyapi_settings', 'butterflyapi_keep_original');
}
public function settings_page() {
?>
<div class="wrap">
<h1>ButterflyAPI Settings</h1>
<form method="post" action="options.php">
<?php settings_fields('butterflyapi_settings'); ?>
<table class="form-table">
<tr>
<th>API Key</th>
<td>
<input type="password" name="butterflyapi_key" value="<?php echo esc_attr(get_option('butterflyapi_key')); ?>" class="regular-text" />
<p class="description">Get your API key from <a href="https://butterflyapi.com/dashboard/keys" target="_blank">ButterflyAPI Dashboard</a></p>
</td>
</tr>
<tr>
<th>Auto Process Uploads</th>
<td><label><input type="checkbox" name="butterflyapi_auto_process" value="1" <?php checked(get_option('butterflyapi_auto_process'), 1); ?> /> Automatically process images on upload</label></td>
</tr>
<tr>
<th>Default Action</th>
<td>
<select name="butterflyapi_default_action">
<option value="enhance" <?php selected(get_option('butterflyapi_default_action'), 'enhance'); ?>>Enhance</option>
<option value="upscale" <?php selected(get_option('butterflyapi_default_action'), 'upscale'); ?>>Upscale 2x</option>
<option value="background-remove" <?php selected(get_option('butterflyapi_default_action'), 'background-remove'); ?>>Remove Background</option>
</select>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</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 ajax_process_image() {
check_ajax_referer('butterflyapi_nonce', 'nonce');
$attachment_id = intval($_POST['attachment_id']);
$action = sanitize_text_field($_POST['action_type']);
$image_url = wp_get_attachment_url($attachment_id);
$result = $this->process_image($image_url, $action);
if (is_wp_error($result)) wp_send_json_error($result->get_error_message());
wp_send_json_success(['message' => 'Image processed', 'url' => $result['outputUrl']]);
}
}
new ButterflyAPI_Plugin();Need help with installation?