| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) || die( 'Cheatin’ uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Run an async job to optimize images in background. |
| 6 |
* |
| 7 |
* @param array $body Contains the usual $_POST. |
| 8 |
* |
| 9 |
* @since 1.4 |
| 10 |
*/ |
| 11 |
function imagify_do_async_job( $body ) { |
| 12 |
$args = [ |
| 13 |
'timeout' => 0.01, |
| 14 |
'blocking' => false, |
| 15 |
'body' => $body, |
| 16 |
'cookies' => isset( $_COOKIE ) && is_array( $_COOKIE ) ? $_COOKIE : [], |
| 17 |
'sslverify' => apply_filters( 'https_local_ssl_verify', false ), |
| 18 |
]; |
| 19 |
|
| 20 |
/** |
| 21 |
* Filter the arguments used to launch an async job. |
| 22 |
* |
| 23 |
* @since 1.6.6 |
| 24 |
* @author Grégory Viguier |
| 25 |
* |
| 26 |
* @param array $args An array of arguments passed to wp_remote_post(). |
| 27 |
*/ |
| 28 |
$args = apply_filters( 'imagify_do_async_job_args', $args ); |
| 29 |
|
| 30 |
/** |
| 31 |
* It can be a XML-RPC request. The problem is that XML-RPC doesn't use cookies. |
| 32 |
*/ |
| 33 |
if ( defined( 'XMLRPC_REQUEST' ) && get_current_user_id() ) { |
| 34 |
/** |
| 35 |
* In old WP versions, the field "option_name" in the wp_options table was limited to 64 characters. |
| 36 |
* From 64, remove 19 characters for "_transient_timeout_" = 45. |
| 37 |
* Then remove 12 characters for "imagify_rpc_" (transient name) = 33. |
| 38 |
* Luckily, a md5 is 32 characters long. |
| 39 |
*/ |
| 40 |
$rpc_id = md5( maybe_serialize( $body ) ); |
| 41 |
|
| 42 |
// Send the request to our RPC bridge instead. |
| 43 |
$args['body']['imagify_rpc_action'] = $args['body']['action']; |
| 44 |
$args['body']['action'] = 'imagify_rpc'; |
| 45 |
$args['body']['imagify_rpc_id'] = $rpc_id; |
| 46 |
$args['body']['imagify_rpc_nonce'] = wp_create_nonce( 'imagify_rpc_' . $rpc_id ); |
| 47 |
|
| 48 |
// Since we can't send cookies to keep the user logged in, store the user ID in a transient. |
| 49 |
set_transient( 'imagify_rpc_' . $rpc_id, get_current_user_id(), 30 ); |
| 50 |
} |
| 51 |
|
| 52 |
$url = admin_url( 'admin-ajax.php' ); |
| 53 |
|
| 54 |
/** |
| 55 |
* Filter the URL to use for async jobs. |
| 56 |
* |
| 57 |
* @since 1.9.5 |
| 58 |
* @author Grégory Viguier |
| 59 |
* |
| 60 |
* @param string $url An URL. |
| 61 |
* @param array $args An array of arguments passed to wp_remote_post(). |
| 62 |
*/ |
| 63 |
$url = apply_filters( 'imagify_async_job_url', $url, $args ); |
| 64 |
|
| 65 |
wp_remote_post( $url, $args ); |
| 66 |
} |
| 67 |
|