Forked from techjewel/custom_background_queue_process.php
Created
July 20, 2021 09:41
-
-
Save nkb-bd/2507985665a0e30a1b97447325984df8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Function to push a task for background processing | |
* @param: string $callbackName - Your classback acction name | |
* @param: mixed $payload - Your payload data that you will get when procssing on callback | |
*/ | |
function my_custom_queue_on_background($callbackName, $payload) | |
{ | |
$body = [ | |
'payload' => $payload, | |
'callback_name' => $callbackName | |
]; | |
$args = array( | |
'timeout' => 0.1, | |
'blocking' => false, | |
'body' => $body, | |
'cookies' => $_COOKIE, | |
'sslverify' => apply_filters('https_local_ssl_verify', false), | |
); | |
$queryArgs = array( | |
'action' => 'my_custom_callback_for_background', | |
'nonce' => wp_create_nonce('my_custom_callback_for_background'), | |
); | |
$url = add_query_arg($queryArgs, admin_url( 'admin-ajax.php' )); | |
wp_remote_post(esc_url_raw($url), $args); | |
} | |
/* | |
* Function to catch the self ajax request and firing the requested action | |
*/ | |
function mycuctomProcessRouting() { | |
$callbackName = sanitize_text_field($_REQUEST['callback_name']); | |
if ( ! wp_verify_nonce( $_REQUEST['nonce'], 'my_custom_callback_for_background' ) ) { | |
error_log($callbackName. ' Security Check Failed'); | |
die( 'Security Check Failed' ); | |
} | |
$data = $_REQUEST['payload']; | |
do_action($callbackName, $data); | |
echo 'success'; | |
die(); | |
} | |
// Here we will catch the self ajax request and and route the handler | |
add_action('wp_ajax_my_custom_callback_for_background', 'mycuctomProcessRouting'); | |
add_action('wp_ajax_nopriv_my_custom_callback_for_background', 'mycuctomProcessRouting'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment