-
Notifications
You must be signed in to change notification settings - Fork 75
Open
Description
Hello I'm using your package to implement async tasks.The basic idea is that I'm giving the option to the admin from the dashboard to create a post with some related videos. The videos are stored on the server but when he creates the post I want to move the videos that he selected to another platform and after the upload finish to save the response on a table with a foreign key of the post ID.
My problem is that instead of saving the post and then create an async task to upload the video on the platform it keeps wait for the video to be uploaded and then to refresh the page.
Here is my code
function init_async_task() {
if (!class_exists('Upload_Videos_Async', false)) {
new Upload_Videos_Async(WP_Async_Task::LOGGED_IN);
}
}
add_action('plugins_loaded', 'init_async_task');
add_action('wp_async_save_post', 'move_videos_to_platform', 10, 2);
function move_videos_to_platform($id) {
//upload here the videos
}
class Upload_Videos_Async extends WP_Async_Task {
protected $action = 'save_post';
protected $argument_count = 1;
public function __construct($auth_level = 0) {
parent::__construct(parent::LOGGED_IN);
}
/**
* Prepare data for the asynchronous request
*
* @throws Exception If for any reason the request should not happen
*
* @param array $data An array of data sent to the hook
*
* @return array
*/
protected function prepare_data($data) {
$returnData = ['post_id' => $data[0]];
$files = [];
$http = herbert('http');
if ($http->has('file_name')) {
foreach ($http->get('file_name') as $key => $value) {
$tempKey = str_replace('.mp4', '', $value);
$files[$tempKey] = $value;
}
}
if (count($files) > 0) {
$returnData['file_name'] = http_build_query($files);
}
return $returnData;
}
/**
* Run the async task action
*/
protected function run_action() {
$http = herbert('http');
$post = get_post($http->get('post_id'));
if ($post) {
if ($post->post_type == 'futures-video')
// Assuming $this->action is 'save_post'
{
if ($http->has('file_name')) {
$files = [];
foreach (glob($_SERVER['DOCUMENT_ROOT'] . '/*.mp4') as $file) {
$tempKey = str_replace('.mp4', '', basename($file));
$tempKey = str_replace('.', '_', $tempKey);
$files[$tempKey] = $file;
}
$urlFiles = [];
parse_str($http->get('file_name'), $urlFiles);
$count = 0;
foreach ($urlFiles as $key => $filepath) {
$key = array_search(basename($filepath), $urlFiles);
if ($key !== false) {
$videos = new Videos;
$videos->name = $key;
$videos->post_id = $post->ID;
$videos->status = 'pending';
$videos->save();
$count++;
}
}
if ($count > 0) {
do_action("wp_async_$this->action", $post->ID, $post);
}
}
}
}
}
}
At the end if the videos didn't uploaded for any reason I want to execute the same tasks to re-upload them. How I can call the async task?
any help?
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels