deliciousbrains / wp-amazon-s3-and-cloudfront

Automatically copies media uploads to Amazon S3 for delivery. Optionally configure Amazon CloudFront for even faster delivery.
https://wordpress.org/plugins/amazon-s3-and-cloudfront/
304 stars 148 forks source link

Add Metadata tool hangs on 0% locking the WP admin controls with endless repeating API response showing no progress #648

Closed conkonig closed 2 months ago

conkonig commented 2 months ago

Add Metadata tool hangs on 0% with endless repeating API response showing no progress. I have manually added the metadata to the table wp_as3cf_items with a short script manually instead. Still the tool shows the same issue hanging on 0%

The response pictured keeps repeating and stays on 0% progress: image

The admin area of the plugin is locked out while the loop runs endlessly see below: image

Steps to reproduce: 1) Install plugin and activate pro license 2) Connect to existing bucket with existing media on digitalocean spaces 3) Enable the add metadata from existing media tool by adding define( 'AS3CF_SHOW_ADD_METADATA_TOOL', true ); to wp-config.php 4) Try to run the add metadata tool 5) Observe hanging and locked out of plugin admin area

Further steps: 6) Cure lockout by clearing rows from wp_options 7) Run WP CLI command in plugin below to manually rectify bucket URL's for existing bucket media

Hanging and lockout can be cured by manually deleting the rows from wp_options relating to the add_metadata_tool. Just search for rows containing "%as3cf%"

It would be nice to have this working for those who paid for a pro license and have to work with it across multiple websites with multiple staging / production environments. Otherwise have to rely on hacky script workaround from chatgpt below:

<?php
/*
Plugin Name: Fix as3cf Plugin Attachment URL Migration Plugin
Description: A plugin to add a WP CLI command to migrate attachment data to work with as3cf for S3 bucket URLs because add metadata tool wont work. 
Version: 1.0
Author: Your Name
*/

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

if (defined('WP_CLI') && WP_CLI) {

    function attachment_migration_command($args, $assoc_args)
    {
        global $wpdb;

        // Query to fetch all attachments from WordPress media library
        $query = "SELECT ID, post_title, post_name, post_date, post_mime_type, guid 
                  FROM {$wpdb->posts} 
                  WHERE post_type = 'attachment'";

        $attachments = $wpdb->get_results($query, ARRAY_A);

        // If there are attachments, process each one
        if ($attachments) {
            $total_attachments = count($attachments);
            $current_attachment = 0;

            WP_CLI::line("Starting migration of {$total_attachments} attachments...");

            foreach ($attachments as $attachment) {
                $current_attachment++;

                // Extract necessary details
                $attachment_id = $attachment['ID'];
                $attachment_title = $attachment['post_title'];
                $attachment_filename = basename($attachment['guid']); // Extract filename from URL
                $attachment_date = $attachment['post_date'];

                $tablename = "wp_as3cf_items";
                $bucketname = "bucketname ";

                // Construct S3 bucket URL based on your pattern
                $bucket_url = "https://$bucketname.ams3.digitaloceanspaces.com"; // Replace with your S3 bucket URL
                $year_month = date('Y/m', strtotime($attachment_date)); // Get year and month from attachment date

                // Construct paths
                $path = "uploads/{$year_month}/{$attachment_filename}";
                $source_path = "{$year_month}/{$attachment_filename}";
                $original_source_path = "{$year_month}/{$attachment_filename}";

                // Prepare data to insert into your custom table
                $data = array(
                    'provider' => 'do',
                    'region' => 'ams3',
                    'bucket' => $bucketname, 
                    'path' => $path,
                    'original_path' => $path,
                    'is_private' => 0, // Adjust as necessary
                    'source_type' => 'media-library',
                    'source_id' => $attachment_id,
                    'source_path' => $source_path,
                    'original_source_path' => $original_source_path,
                    'extra_info' => '', // You can add additional information if needed
                    'originator' => 0,
                    'is_verified' => 1 // Adjust as necessary
                );

                // Insert data into your custom table
                $wpdb->insert($tablename, $data);

                // Output progress information
                WP_CLI::line("Processed attachment {$current_attachment} of {$total_attachments}");
            }

            WP_CLI::success("All attachments migrated successfully.");
        } else {
            WP_CLI::error("No attachments found in the WordPress media library.");
        }
    }

    WP_CLI::add_command('attachment-migration migrate_attachments', 'attachment_migration_command');
}

Finally if no one has time to debug this can someone confirm if workaround script above will be okay with the plugin and not cause further issues? Thank you

ianmjones commented 2 months ago

Hey @conkonig,

Please do not use that script, there's a number of issues with it, and the Add Metadata tool is built to properly verify the metadata it creates.

As you're a licensed customer, please contact support through the plugin's Support tab for help with getting the Add Metadata tool working.

Although, given that your site is reporting that all media is offloaded, I suspect you do not need to use the Add Metadata tool. It may be that you've found a bug whereby the tool gets confused as there's nothing to be processed, so please explain to support why you're trying to run the tool when everything is already offloaded so that they can help you better.