• Resolved joeparadis2

    (@joeparadis2)


    When I use the plugin to import a file not yet included in the media library the import functions properly except the post meta “_wp_attachment_metadata” is getting an incorrect value causing other plugins (such as Imagify) to say the file doesn’t exists. From what I can tell the meta data is showing up as ‘E:/homepages/1/username/www/wp-content/uploads/2020/02/test-file.gif’ instead of ‘2020/02/test-file.gif’ like other files have that work. If I manually change this value Imagify is able to find the file and functions properly. I’m using WordPress on a Windows server.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author erolsk8

    (@erolsk8)

    That’s strange, it shouldn’t be saved as an absolute path, but just that short path (relative to the uploads directory). I just checked the code and unless media_sync_filter_before_update_metadata hook is used, the plugin simply uses these 3 WordPress functions (without altering $data)

    $id = wp_insert_attachment($attachment, $absolute_path); 
    $data = wp_generate_attachment_metadata($id, $absolute_path);
    wp_update_attachment_metadata($id, $data);

    As far as I know, it’s necessary to provide absolute paths, but those WP functions should still save relative paths.

    Maybe it’s something to do with forward slashes vs backslashes on Windows. I mean, this plugin should already handle those conversions and work on Windows servers as well. Or maybe it always worked partially since no one noticed this issue in the metadata table.

    I guess I would need a whole Windows dev environment to debug this properly, but I’m not sure when will I find time for it, so I can’t promise anything. But it’s worth looking into, it’s probably a simple fix once the origin of the issue is found.

    Thread Starter joeparadis2

    (@joeparadis2)

    Hi @eolsk8 – thanks for the prompt reply!

    I did some checking on my end to see if I could come up with anything and from what I found on line 856 of MediaSync.class.php it has:

    $attach_id = wp_insert_attachment($attachment, $absolute_path);

    According to the WP docs (https://developer.wordpress.org/reference/functions/wp_insert_attachment/) it should just be the file name. I tried hard coding the file name on that line so instead of ‘$absolute_path’ I had ‘2020/02/test-file.gif’ to match the actual file name and upload folder. This resulted in the attachment being added to the wp_posts table and the wp_postmeta and had the correct value for ‘_wp_attached_file’.

    However, in wp_postmeta, the ‘_wp_attachment_metadata’ still has the absolute path included as the ‘file’ when it shouldn’t be based on the uploaded attachments I’ve checked that were uploaded directly through WordPress. That would be in relation to line 884:

    $attach_data = wp_generate_attachment_metadata($attach_id, $absolute_path);

    Plugin Author erolsk8

    (@erolsk8)

    Oh man, these comments are so annoying! I wrote a huge explanation, clicked submit and it’s all gone (since I got logged out in the meantime). But anyway, I tried this:

    $attach_id = wp_insert_attachment($attachment, basename($absolute_path));

    And there was no error, but the imported file was broken (URL was .../uploads/file.jpg instead of .../uploads/2024/03/file.jpg).

    Documentation indeed says just “Filename.”, but if you follow it deeper (wp_insert_attachment -> wp_insert_post -> update_attached_file) documentation for that update_attached_file() says: “File path for the attachment.”, so I guess wp_insert_attachment() documentation is simply outdated/wrong 🙂

    So it’s something else. And if you maybe figure it out or have some other idea, please let me know. Since I can’t test code changes on Windows server that easily.

    Thanks @joeparadis2.

    Thread Starter joeparadis2

    (@joeparadis2)

    I just took another look and got it working correctly with my Windows server; although what I’ve changed isn’t going to work as some users might be using a custom upload directory.

    The following changes are within “media_sync_update_import_to_database”:

    $attach_id = wp_insert_attachment($attachment, $relative_path);

    $tmp_relative_path = self::media_sync_url_encode(self::media_sync_get_relative_path($absolute_path));$relative_path = str_replace(“/wp-content/uploads/”, “”, $tmp_relative_path);

    Example:
    $absolute_path = ‘E:/homepages/1/username/www/wp-content/uploads/2020/02/test-file.gif’
    $tmp_relative_path = ‘/wp-content/uploads/2020/02/test-file.gif’
    $relative_path = ‘2020/02/test-file.gif’

    It seems the “wp_insert_attachment” needs the file name and path relative to the upload directory, in my test if I had ‘/wp-content/uploads/’ included it had an error and using the absolute path caused the entire path to be included.

    Additionally, the following line was modified:

    $attach_data = wp_generate_attachment_metadata($attach_id, get_attached_file( $attach_id ));

    I used the get_attached_file() function in order to get the value from WP which is supposed to use ‘_wp_attached_file’ but it returns ‘E:\homepages\1\username\www/wp-content/uploads/2020/02/test-file.gif’ which is nearly identical to the $absolute_path except the backslashes on the first half of the path until ‘wp-content’. If I supplied $absolute_path to the wp_generate_attachment_metadata function it returned an error generating the metadata. This also can’t be the ‘$absolute_path’ as is provided currently in the plugin as it then saves the “file” in “_wp_attachment_metadata” as the full path.

    I’m not sure exactly where the plugin is converting the back slashes but maybe that is the only reason the plugin isn’t saving the post meta correctly as simply using the get_attached_file() instead of $absolute_path saved only the ‘2020/02/test-file.gif’ as the ‘file’ in ‘_wp_attachment_metadata’.

    With these changes the file is added to the database correctly and during my test it’s identical to what is inserted if I were to upload the same file in the WP upload.

    Programming in PHP isn’t something I deal with much and rarely deal with WP so I’m not familiar with all the functions, so I know the way I’m getting the file path isn’t going to be correct but I was simply trying to get it to work properly on the Windows server to allow you to check on your end.

    I’m more than willing to test any revisions to check them on a Windows environment if that helps you.

    Plugin Author erolsk8

    (@erolsk8)

    Thanks for the detailed breakdown! I now have an idea of what needs to be changed to fix this.

    I just need to be very careful not to break it for someone else who currently uses the plugin, especially for those using custom filters (e.g. media_sync_filter_before_update_metadata). So even if the relative path should have been used instead of the absolute path, I need to make sure it continues to work for everyone else.

    But once I find some time to do that (hopefully on Monday), and it works the same as before and also fixes this Windows server issue, I can send you a new version to try it out, before pushing it to everyone.

    Thread Starter joeparadis2

    (@joeparadis2)

    Sounds good – thank you!

    Plugin Author erolsk8

    (@erolsk8)

    I didn’t forget about this but when I apply your code, I get this error: “Attachment metadata could not be generated”, because $attach_data is an empty array. I tried all of the relative paths I could think of, for example:

    • 2020/02/test-file.gif
    • /2020/02/test-file.gif
    • /wp-content/uploads/2020/02/test-file.gif

    But it just doesn’t work, it needs an absolute path.

    I’ll try to find some time to replicate and fix this, but I can’t promise when. It’s crazy times at the moment 😀

    Thread Starter joeparadis2

    (@joeparadis2)

    Thanks for the update. I just looked at the plugin file I had modified, does the error your getting occur on the following try/catch:

            try {
                // Generate the metadata for the attachment
                // change line for windows server installation
                $attach_data = wp_generate_attachment_metadata($attach_id, get_attached_file($attach_id));
                // $attach_data = wp_generate_attachment_metadata($attach_id, $absolute_path);
            } catch (Exception $e) {
                return array(
                    'errorMessage' => sprintf(__('Error generating attachment metadata (wp_postmeta table) for file: %s.', 'media-sync'), $relative_path),
                    'error' => $e->getMessage()
                );
            }

    I didn’t get an errors returned from that catch, but if that’s where you see an issue, I’ll try it again. I’ve been using the modified plugin file for several files now and haven’t had any issues and the database tables have the correct format for the urls based on files uploaded directly in WP.

    I’m in no rush for this as I have it working for my install currently.

    Plugin Author erolsk8

    (@erolsk8)

    It works with only this change, but what’s the path you get from get_attached_file($attach_id)? Because for me, it’s exactly the same as $absolute_path.

    So it’s strange to use get_attached_file() (which probably does an additional database query) for item we just saved to database (especially when importing thousands of files).

    Maybe the only difference is back vs forward slashes. So I’m wondering if it will work for you (on windows server) if only this line is changed:

    $attach_data = wp_generate_attachment_metadata($attach_id, wp_normalize_path($absolute_path));
    Thread Starter joeparadis2

    (@joeparadis2)

    The path returned with get_attached_file($attach_id) is “E:\homepages\1\username\www/wp-content/uploads/2020/02/test-file.gif”.

    When I tried the provided: $attach_data = wp_generate_attachment_metadata($attach_id, wp_normalize_path($absolute_path)); It ends up saving the “_wp_attachment_metadata” field with “E:/homepages/1/username/www/wp-content/uploads/2020/02/test-file.gif” when based on all other database entries it should just be “2020/02/test-file.gif”.

    I tried the plugin again with the modified line using get_attached_file($attach_id) and it inserts as “2020/02/test-file.gif”.

    Plugin Author erolsk8

    (@erolsk8)

    Hey @joeparadis2, would you be interested in testing this in pro version? I can give you a coupon to activate pro version and try it out there. Just send me an email referring to this topic (email can be found in “Contact Support” in footer: https://mediasyncplugin.com/).

    Because code is much better there and I can also release a fix as beta version for you to try. And then if it works there, I’ll do the same in this free version.

Viewing 11 replies - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.