Hello @alexfurr,
Just as a start, we welcome Pull Requests on our public GitHub repo for such nice new features ??
Coming back to your request, I believe you might achieve this on the current version of EMR because most of the things seem to be in place already.
Just as a side reference, here’s how we have done the integration with WP Offload Media in our other plugin, SPIO: https://github.com/short-pixel-optimizer/shortpixel-image-optimiser/blob/master/class/external/wp-offload-media.php
The filesystem module can handle virtual (non-existent) files via a hook. In the SPIO implementation, it checks in the hook if the file is offloaded and returns true if so, then the file is regarded as existing.
The hook + function in the above file to look at:
add_filter(‘shortpixel/image/urltopath’, array($this, ‘checkIfOffloaded’), 10,2);
In EMR both source and target files are built from the result of get_attached_file
, which by default gets the unfiltered result. This should point to the file location where the file once was. If so, then this will work OK.
Secondly, when replacing, the remote file should be removed (can be renamed, etc.) and there is a remove_remote
function in the above file from SPIO.
EMR fires a hook before starting the replacer – do_action(‘wp_handle_replace’, array(‘post_id’ => $post_id)); so it could be hooked into that.
Note that the Offloading plugin doesn’t allow sending individual files. Everything is bundled as a Media Library item with an attachment ID. This means that anything there will be removed, the replaced file metadata should be regenerated and that result should be offloaded again. The code in replace.php aims to replace data in the installation to reflect changes and compensate for new/missing thumbnail filenames and dimensions.
When all is done, EMR fires a done hook: do_action(“enable-media-replace-upload-done”, $this->target_url, $this->source_url, $this->post_id);
This could be used to hook the image_upload function (above file) and offload the result.
After implementing all this, it should be tested if everything works. Crucial variables are both the source_file
and source_url
generated by EMR in Replace.php construct (which could be tested easily by logging it) and checking if this mechanism works as intended in EMR.
Again, this would make a very nice improvement through a PR and we can obviously continue the development discussion there.
For any other questions, don’t hesitate to write to us!
Thank you!