distributor icon indicating copy to clipboard operation
distributor copied to clipboard

Feature Request: More hooks within set_media

Open aaronware opened this issue 2 years ago • 1 comments

Is your enhancement related to a problem? Please describe.

We're using Distributor to map products between different websites and we're facing timeouts regarding image syncing. While we can enable the featured image on posts, it doesn't necessarily work with Products as the galleries in use need to be pushed as well.

For the gallery/attached media specifically, we were thinking about deferring the image loading and utilizing Action Scheduler in order to sideload the media behind the scenes rather than waiting for 10+ images to time out. This way the images can load an update as needed.

However, there doesn't appear to be any do_action hooks defined within the set_media() function.

I propose adding hooks for dt_pre_set_media, and dt_post_set_media

Somewhat related? I know there is some work to refactor for a 2.0 release so this may already be something in the works internally. Is it worth me doing a few pull requests for this and #1018 or is it best to hold off?

Designs

Basically, I was going to filter the media out similar to the featured media_handling option check on line 700 of utils.php

Then scheduled individual cron jobs /actions for each piece of media using the Action Scheduler

Describe alternatives you've considered

I can also hook into the dt_set_media_filter but I feel that's just a workaround and not necessarily the best approach.

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

aaronware avatar Mar 20 '23 18:03 aaronware

If anyone is looking at this post, As a workaround, we ended up utilizing the following hooks along w/ the Woo action scheduler to accomplish what we needed.

// Do not sync all media automatically.
add_filter( 'dt_sync_media_delete_and_replace', "__return_false", 10, 1 );

// When saving a post, hook into the media push and attach any gallery media that is not already attached.
add_filter( 'dt_push_post_args', [ $this, 'cleanup_distribution_media' ], 10, 2 ); 
add_filter( 'dt_subscription_post_args', [ $this, 'cleanup_distribution_media' ], 10, 2 );
add_filter( 'dt_media_item_formatted', [ $this, 'filter_get_attached_media' ], 10, 2 );

// Push media to our child sites, but defer them
add_action( 'as_dt_set_media', [ __CLASS__, 'set_media' ], 10, 2 ); // Hook into action scheduler event per media item and send them individually behind the scenes.

// On attribute update, create action scheduler event `as_dt_set_media`. Use attributes as the hook since there are no actions available for media.
add_action( 'dt_process_subscription_attributes', [ $this, 'push_media_to_remote' ], 10, 2 );
add_action( 'dt_process_distributor_attributes', [ $this, 'push_media_to_remote' ], 10, 2 );

While there is a delay in syncing media we have found this solution to be pretty reliable even when there are large amounts of images. In our implementation, some products have 20-30 images. The only challenge is that a user won't exactly know when the sync is complete.

This implementation will benefit from the hooks I proposed as it wouldn't be utilizing an unintended hook

aaronware avatar Apr 06 '23 14:04 aaronware