massedge / wordpress-plugin-export-media-library

WordPress plugin that allows users to export media library files as a compressed zip archive.
https://wordpress.org/plugins/export-media-library/
GNU General Public License v3.0
10 stars 3 forks source link

[Compatibility] Download Real Media Library folder #9

Closed matzeeable closed 5 years ago

matzeeable commented 5 years ago

Hi there!

I am the developer of the WordPress Real Media Library Plugin (https://codecanyon.net/item/wordpress-real-media-library-media-categories-folders/13155134). I got often asked for a feature to download a folder as zip file. Instead of reinventing the wheel I am a fan of Extensions and Add-Ons. I see your plugin is doing the download job weel.

Before making a PR to your plugin I want to ask you what do you think about that? The compatibility will be very lightweight.

Best regards, Matthew :smile:

andrejpavlovic commented 5 years ago

Sure, you can submit a PR no problem. Aim for something generic that other plugins can hook into if they want to.

matzeeable commented 5 years ago

Started to fork the plugin. I will let you know asap I have finished the compatibility but here is a quick insight:

andrejpavlovic commented 5 years ago

Have a look at the latest version of the files. You should be able to do everything you need by calling

use MassEdge\WordPress\Plugin\ExportMediaLibrary\API as ExportMediaLibraryAPI;

$options = ExportMediaLibraryAPI::defaultExportOptions();
$options = array_merge($options, [ /* your options go here */ ]);

ExportMediaLibraryAPI::export($options);
matzeeable commented 5 years ago

Thanks for implementing this API, @andrejpavlovic !

I have finished the coding in my plugin so far. One thing is missing: "As hierarchical .zip file (RML structure)". To gain good performance I need to read attachment infos within one single SQL query so I need to know all attachment ids in the add_attachment_callback:

// opportunity to manipulate adding of attachment to zip
$result = $options['add_attachment_callback']([
    'name' => $file,
    'path' => $attachmentPath,
    'options' => [
        'time' => $time,
    ],
], [
    'attachment_id' => $attachmentId,
], $attachmentIds);

Can you please adjust this? :-)

andrejpavlovic commented 5 years ago

I was thinking about that, but you are calling ExportMediaLibraryAPI::export($options); directly aren't you?

So you can do something like:

$options = ExportMediaLibraryAPI::defaultExportOptions();

$query = new \WP_Query();
$attachmentIds = $query->query($options['query_args']);

$options['add_attachment_callback'] = function($data, $params) use ($attachmentIds) {
    // your logic with access to $attachmentIds
   return $data;
};

ExportMediaLibraryAPI::export($options);

I kind of don't want to expose access to all attachment ids at once, since this should in theory be a "streaming" api, meaning at no point do we try to hold reference to a large chunk of data to avoid running out of memory.

matzeeable commented 5 years ago

Yeah, I am using ExportMediaLibraryAPI::export($options). For the first it should be OK to call the WP_Query twice (once in my code and once in your export method). Perhaps it should be also possible to pass the attachment ids as $options parameter so duplicate queries can be avoided, nor?

andrejpavlovic commented 5 years ago

I agree with the issue of double db query, but lets leave it as is for now. One extra call is not that expensive in the grand scheme of things.

Down the road I can add support for a custom iterator/generator to allow for a more dynamic way of specifying attachment ids without necessarily loading them all up in memory at once.

Anyway I've pushed out version 1.1.0 and 2.2.0, so you should be able to test out your code integration in production.

matzeeable commented 5 years ago

Thank you for your cooperation! The compatibility works great. I will release version 4.5.0 of my plugin at the end of this month. 😄