Open tiptronic opened 5 years ago
Here's another find:
I just found out that the above filter works, but generates files in the uploads directory like this:
-23-23-23x-23-23-23@-23x-23
-21-21-21x-21-21-21@-21x-21
photo-1542838687-d214d04db0aa
... (everything without extension).
The first two from the mapbox-request, the latter from unsplash.com
So it seems the error is that somewhere a filename is created, which then can't get successfully linked
@tiptronic Do they have Attachment ID's and can you add those Media Items from the Library to a post afterward?
For me $id = pods_attachment_import($url);
always returns '0' and the filename is like the pattern in my second comment above.
And the media-library doesn't see the files either... so the answer is 'no'.
Could it be, that it makes a difference, if I call it in pods_api_post_save_pod_item_test
?
I don't think it has to do with the filter; it has to do with the pods_attachment_import function. That's where it breaks; something about the dynamic URL's isn't being processed properly.
I copied Scott's code into my filter above and still get $id of '0'
The image is saved as '600' (without suffix) and shows the kitten (in Mac's Finder), but the media-library doesn't see it.
Could someone maybe try it in a filter as well (like the snippet above)? (Right now I am using WP 5.0.3 and Pods 2.7.12).
@tiptronic So, is it writing into the wp-content/uploads/ directory but not adding it to the Media Library? If it doesn't add it to Media Library, there's no Media Attachment ID.
How did you configure this field? Is it configured as Media Library or PLUpload?
Media Library
@tiptronic So what was the answer to this question:
So, is it writing into the wp-content/uploads/ directory but not adding it to the Media Library?
You said the Mac Finder, that's why I asked.
I guess the media-library doesn't see it, because it has no extension. I found a different mapbox-api where I can add an extension (it's basically the same api) and then everything works like a treat
@tiptronic Awesome! That's my guess as well (on the lack of an extension). glad to hear you're working. This still seems like something we should look into, but obviously it's less 'hot' now.
It seems odd to be I can add that Unsplash image into the WordPress Media Library using the 'Import from URL' option that is only available in the Media Modal (it's not an available way to add media using just the straight Media Library in the admin menu; it's only available when you 'add media' to a post). So something they are doing in that process, is creating an extension.
I'm not a PHP guy, but for me it bails out here:
$wp_filetype = wp_check_filetype( $filename );
if ( ! $wp_filetype['type'] || ! $wp_filetype['ext'] ) {
return 0;
}
From my understanding, the first lines of the pods_attachment_import
are a bit problematic, because they squeeze and split the passed url and compile a new $filename, without checking, if this is a possible valid file. Since this filename (in my case) doesn't have an extension, the wordpress-function above returns 0.
A simple solution would be to check the file itself (e.g. with exif_imagetype ( string $filename )
, which reads the first couple of bytes of the file and thereby detects the most common filetypes.
Another way would be to pass a default filetype and pass this to another wp-function:
wp_check_filetype_and_ext( $filename, $filename_with_extension )
I personally wanted to pass a dedicated filename anyway, so I extended the function:
function pods_attachment_import( $url, $post_parent = null, $featured = false, $requested_filename = '' )
This allows me to
a) verify the filetype based on the $requested_filename
parameter (which contains an extension)
b) renames the file before adding it to the media-library - so I have a proper filename.
Ideally I would pass a $requested_filename_and_path, so my added files are moved to a nice path inside the media-library... But I'm not sure, if there are other techniques to do this which are preferable.
For completeness sake, here's how the modified function looks like:
function pods_attachment_import( $url, $post_parent = null, $featured = false, $requested_filename ) {
if ($requested_filename && !empty($requested_filename)) {
$filename = $requested_filename;
} else {
$filename = explode( '?', $url );
$filename = $filename[0];
$filename = explode( '#', $filename );
$filename = $filename[0];
$filename = substr( $filename, ( strrpos( $filename, '/' ) ) + 1 );
}
$title = substr( $filename, 0, ( strrpos( $filename, '.' ) ) );
...
@sc0ttkclark or @pglewis Can you two look at this one, when you get a chance. @tiptronic last update gives the best details I think on what is the crux of the issue.
If you think his additions are actually valid (since they enhance the existing function, without breaking the function), this might be a good fix.
Is there any estimation on when this is going to be fixed? or... is there a workaround?
As part of the solution, note that 'post_mime_type' should be set in case there is a dynamic image
Unfortunately, the update from 2.7.12 to 2.7.15 broke it again for me... I have yet to find out what the problem is, but since this issue is still flagged as open
, it seems it has some side-effects...
For my current project, I had to go back to 2.7.12, so I will setup a test-project to see what's going on.
Issue Overview
I'm trying to load a dynamic image from somewhere (here: unsplash.com)
Expected Behavior
Image should get imported
Current Behavior
pods_attachment_import($id)
always returns '0'Steps to Reproduce (for bugs)
Possible Solution
replacing the $url with a static image found on the internet, works fine.
WordPress Environment
4.9.7 and 5.0.3