Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/clone/wp-conte.../plugins/wp-smush.../core/modules/async
File: class-abstract-async.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* WP Asynchronous Tasks
[2] Fix | Delete
* Version: 1.0
[3] Fix | Delete
* Description: Creates an abstract class to execute asynchronous tasks
[4] Fix | Delete
* Author: 10up, Eric Mann, Luke Gedeon, John P. Bloch
[5] Fix | Delete
* License: MIT
[6] Fix | Delete
* Note: Modified to return metadata at the end of the launch function
[7] Fix | Delete
*
[8] Fix | Delete
* @package Smush\Core\Modules\Async
[9] Fix | Delete
*/
[10] Fix | Delete
[11] Fix | Delete
namespace Smush\Core\Modules\Async;
[12] Fix | Delete
[13] Fix | Delete
use Exception;
[14] Fix | Delete
use Smush\Core\Helper;
[15] Fix | Delete
[16] Fix | Delete
if ( ! defined( 'WPINC' ) ) {
[17] Fix | Delete
die;
[18] Fix | Delete
}
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Class Abstract_Async
[22] Fix | Delete
*/
[23] Fix | Delete
abstract class Abstract_Async {
[24] Fix | Delete
[25] Fix | Delete
/**
[26] Fix | Delete
* Constant identifier for a task that should be available to logged-in users
[27] Fix | Delete
*
[28] Fix | Delete
* See constructor documentation for more details.
[29] Fix | Delete
*/
[30] Fix | Delete
const LOGGED_IN = 1;
[31] Fix | Delete
[32] Fix | Delete
/**
[33] Fix | Delete
* Constant identifier for a task that should be available to logged-out users
[34] Fix | Delete
*
[35] Fix | Delete
* See constructor documentation for more details.
[36] Fix | Delete
*/
[37] Fix | Delete
const LOGGED_OUT = 2;
[38] Fix | Delete
[39] Fix | Delete
/**
[40] Fix | Delete
* Constant identifier for a task that should be available to all users regardless of auth status
[41] Fix | Delete
*
[42] Fix | Delete
* See constructor documentation for more details.
[43] Fix | Delete
*/
[44] Fix | Delete
const BOTH = 3;
[45] Fix | Delete
[46] Fix | Delete
/**
[47] Fix | Delete
* This is the argument count for the main action set in the constructor. It
[48] Fix | Delete
* is set to an arbitrarily high value of twenty, but can be overridden if
[49] Fix | Delete
* necessary
[50] Fix | Delete
*
[51] Fix | Delete
* @var int
[52] Fix | Delete
*/
[53] Fix | Delete
protected $argument_count = 20;
[54] Fix | Delete
[55] Fix | Delete
/**
[56] Fix | Delete
* Priority to fire intermediate action.
[57] Fix | Delete
*
[58] Fix | Delete
* @var int
[59] Fix | Delete
*/
[60] Fix | Delete
protected $priority = 10;
[61] Fix | Delete
[62] Fix | Delete
/**
[63] Fix | Delete
* Action name.
[64] Fix | Delete
*
[65] Fix | Delete
* @var string
[66] Fix | Delete
*/
[67] Fix | Delete
protected $action;
[68] Fix | Delete
[69] Fix | Delete
/**
[70] Fix | Delete
* Request body data.
[71] Fix | Delete
*
[72] Fix | Delete
* @var array
[73] Fix | Delete
*/
[74] Fix | Delete
protected $body_data;
[75] Fix | Delete
[76] Fix | Delete
/**
[77] Fix | Delete
* Constructor to wire up the necessary actions
[78] Fix | Delete
*
[79] Fix | Delete
* Which hooks the asynchronous postback happens on can be set by the
[80] Fix | Delete
* $auth_level parameter. There are essentially three options: logged-in users
[81] Fix | Delete
* only, logged-out users only, or both. Set this when you instantiate an
[82] Fix | Delete
* object by using one of the three class constants to do so:
[83] Fix | Delete
* - LOGGED_IN
[84] Fix | Delete
* - LOGGED_OUT
[85] Fix | Delete
* - BOTH
[86] Fix | Delete
* $auth_level defaults to BOTH
[87] Fix | Delete
*
[88] Fix | Delete
* @throws Exception If the class' $action value hasn't been set.
[89] Fix | Delete
*
[90] Fix | Delete
* @param int $auth_level The authentication level to use (see above).
[91] Fix | Delete
*/
[92] Fix | Delete
public function __construct( $auth_level = self::BOTH ) {
[93] Fix | Delete
if ( empty( $this->action ) ) {
[94] Fix | Delete
throw new Exception( 'Action not defined for class ' . __CLASS__ );
[95] Fix | Delete
}
[96] Fix | Delete
[97] Fix | Delete
// Handle the actual action.
[98] Fix | Delete
add_action( $this->action, array( $this, 'launch' ), $this->priority, $this->argument_count );
[99] Fix | Delete
[100] Fix | Delete
if ( $auth_level & self::LOGGED_IN ) {
[101] Fix | Delete
add_action( "admin_post_wp_async_$this->action", array( $this, 'handle_postback' ) );
[102] Fix | Delete
}
[103] Fix | Delete
[104] Fix | Delete
if ( $auth_level & self::LOGGED_OUT ) {
[105] Fix | Delete
add_action( "admin_post_nopriv_wp_async_$this->action", array( $this, 'handle_postback' ) );
[106] Fix | Delete
}
[107] Fix | Delete
}
[108] Fix | Delete
[109] Fix | Delete
/**
[110] Fix | Delete
* Add the shutdown action for launching the real postback if we don't
[111] Fix | Delete
* get an exception thrown by prepare_data().
[112] Fix | Delete
*
[113] Fix | Delete
* @uses func_get_args() To grab any arguments passed by the action
[114] Fix | Delete
*
[115] Fix | Delete
* @return mixed|void
[116] Fix | Delete
*/
[117] Fix | Delete
public function launch() {
[118] Fix | Delete
$data = func_get_args();
[119] Fix | Delete
$result = isset( $data[0] ) ? $data[0] : null;
[120] Fix | Delete
try {
[121] Fix | Delete
$data = $this->prepare_data( $data );
[122] Fix | Delete
if ( ! $this->should_run( $data ) ) {
[123] Fix | Delete
return $result;
[124] Fix | Delete
}
[125] Fix | Delete
} catch ( Exception $e ) {
[126] Fix | Delete
Helper::logger()->error( sprintf( 'Async Smush: Error in prepare_data: %s', $e->getMessage() ) );
[127] Fix | Delete
return;
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
$data['action'] = "wp_async_$this->action";
[131] Fix | Delete
$data['_nonce'] = $this->create_async_nonce();
[132] Fix | Delete
[133] Fix | Delete
$this->body_data = $data;
[134] Fix | Delete
[135] Fix | Delete
$has_shutdown_action = has_action( 'shutdown', array( $this, 'process_request' ) );
[136] Fix | Delete
$is_upload_attachment_action = ! empty( $_POST['action'] ) && 'upload-attachment' === $_POST['action'];
[137] Fix | Delete
$is_post_id_non_empty = ! empty( $_POST ) && isset( $_POST['post_id'] ) && $_POST['post_id'];
[138] Fix | Delete
$is_async_upload = isset( $_POST['post_id'] ) && empty( $_POST['post_id'] ) && isset( $_FILES['async-upload'] );
[139] Fix | Delete
$should_hook_to_shutdown = $is_upload_attachment_action || $is_post_id_non_empty || $is_async_upload;
[140] Fix | Delete
[141] Fix | Delete
// Do not use this, as in case of importing, only the last image gets processed
[142] Fix | Delete
// It's very important that all the Media uploads, are handled via shutdown action, else, sometimes the image meta updated
[143] Fix | Delete
// by smush is earlier, and then original meta update causes discrepancy.
[144] Fix | Delete
if ( $should_hook_to_shutdown && ! $has_shutdown_action ) {
[145] Fix | Delete
add_action( 'shutdown', array( $this, 'process_request' ) );
[146] Fix | Delete
} else {
[147] Fix | Delete
// Send a ajax request to process image and return image metadata, added for compatibility with plugins like
[148] Fix | Delete
// WP All Import, and RSS aggregator, which upload multiple images at once.
[149] Fix | Delete
$this->process_request();
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
// If we have image metadata return it.
[153] Fix | Delete
return $result;
[154] Fix | Delete
}
[155] Fix | Delete
[156] Fix | Delete
protected function should_run( $data ) {
[157] Fix | Delete
return true;
[158] Fix | Delete
}
[159] Fix | Delete
[160] Fix | Delete
/**
[161] Fix | Delete
* Launch the request on the WordPress shutdown hook
[162] Fix | Delete
*
[163] Fix | Delete
* On VIP we got into data races due to the postback sometimes completing
[164] Fix | Delete
* faster than the data could propogate to the database server cluster.
[165] Fix | Delete
* This made WordPress get empty data sets from the database without
[166] Fix | Delete
* failing. On their advice, we're moving the actual firing of the async
[167] Fix | Delete
* postback to the shutdown hook. Supposedly that will ensure that the
[168] Fix | Delete
* data at least has time to get into the object cache.
[169] Fix | Delete
*
[170] Fix | Delete
* @uses $_COOKIE To send a cookie header for async postback
[171] Fix | Delete
* @uses apply_filters()
[172] Fix | Delete
* @uses admin_url()
[173] Fix | Delete
* @uses wp_remote_post()
[174] Fix | Delete
*/
[175] Fix | Delete
public function process_request() {
[176] Fix | Delete
if ( ! empty( $this->body_data ) ) {
[177] Fix | Delete
$request_args = array(
[178] Fix | Delete
'timeout' => apply_filters( 'smush_async_time_out', 0 ),
[179] Fix | Delete
'blocking' => false,
[180] Fix | Delete
'sslverify' => false,
[181] Fix | Delete
'body' => $this->body_data,
[182] Fix | Delete
'cookies' => wp_unslash( $_COOKIE ),
[183] Fix | Delete
);
[184] Fix | Delete
[185] Fix | Delete
$url = admin_url( 'admin-post.php' );
[186] Fix | Delete
[187] Fix | Delete
wp_remote_post( $url, $request_args );
[188] Fix | Delete
}
[189] Fix | Delete
}
[190] Fix | Delete
[191] Fix | Delete
/**
[192] Fix | Delete
* Verify the postback is valid, then fire any scheduled events.
[193] Fix | Delete
*
[194] Fix | Delete
* @uses $_POST['_nonce']
[195] Fix | Delete
* @uses is_user_logged_in()
[196] Fix | Delete
* @uses add_filter()
[197] Fix | Delete
* @uses wp_die()
[198] Fix | Delete
*/
[199] Fix | Delete
public function handle_postback() {
[200] Fix | Delete
if ( isset( $_POST['_nonce'] ) && $this->verify_async_nonce( $_POST['_nonce'] ) ) {
[201] Fix | Delete
$this->run_action();
[202] Fix | Delete
}
[203] Fix | Delete
[204] Fix | Delete
add_filter( 'wp_die_handler', array( $this, 'handle_die' ) );
[205] Fix | Delete
wp_die();
[206] Fix | Delete
}
[207] Fix | Delete
[208] Fix | Delete
/**
[209] Fix | Delete
* Handle Die
[210] Fix | Delete
*/
[211] Fix | Delete
public function handle_die() {
[212] Fix | Delete
die();
[213] Fix | Delete
}
[214] Fix | Delete
[215] Fix | Delete
/**
[216] Fix | Delete
* Create a random, one time use token.
[217] Fix | Delete
*
[218] Fix | Delete
* Based entirely on wp_create_nonce() but does not tie the nonce to the
[219] Fix | Delete
* current logged-in user.
[220] Fix | Delete
*
[221] Fix | Delete
* @uses wp_nonce_tick()
[222] Fix | Delete
* @uses wp_hash()
[223] Fix | Delete
*
[224] Fix | Delete
* @return string The one-time use token
[225] Fix | Delete
*/
[226] Fix | Delete
protected function create_async_nonce() {
[227] Fix | Delete
$action = $this->get_nonce_action();
[228] Fix | Delete
$i = wp_nonce_tick();
[229] Fix | Delete
[230] Fix | Delete
return substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 );
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
/**
[234] Fix | Delete
* Verify that the correct nonce was used within the time limit.
[235] Fix | Delete
*
[236] Fix | Delete
* @uses wp_nonce_tick()
[237] Fix | Delete
* @uses wp_hash()
[238] Fix | Delete
*
[239] Fix | Delete
* @param string $nonce Nonce to be verified.
[240] Fix | Delete
*
[241] Fix | Delete
* @return bool Whether the nonce check passed or failed
[242] Fix | Delete
*/
[243] Fix | Delete
protected function verify_async_nonce( $nonce ) {
[244] Fix | Delete
$action = $this->get_nonce_action();
[245] Fix | Delete
$i = wp_nonce_tick();
[246] Fix | Delete
[247] Fix | Delete
// Nonce generated 0-12 hours ago.
[248] Fix | Delete
if ( substr( wp_hash( $i . $action . get_class( $this ), 'nonce' ), - 12, 10 ) === $nonce ) {
[249] Fix | Delete
return 1;
[250] Fix | Delete
}
[251] Fix | Delete
[252] Fix | Delete
// Nonce generated 12-24 hours ago.
[253] Fix | Delete
if ( substr( wp_hash( ( $i - 1 ) . $action . get_class( $this ), 'nonce' ), - 12, 10 ) === $nonce ) {
[254] Fix | Delete
return 2;
[255] Fix | Delete
}
[256] Fix | Delete
[257] Fix | Delete
// Invalid nonce.
[258] Fix | Delete
return false;
[259] Fix | Delete
}
[260] Fix | Delete
[261] Fix | Delete
/**
[262] Fix | Delete
* Get a nonce action based on the $action property of the class
[263] Fix | Delete
*
[264] Fix | Delete
* @return string The nonce action for the current instance
[265] Fix | Delete
*/
[266] Fix | Delete
protected function get_nonce_action() {
[267] Fix | Delete
$action = $this->action;
[268] Fix | Delete
if ( substr( $action, 0, 7 ) === 'nopriv_' ) {
[269] Fix | Delete
$action = substr( $action, 7 );
[270] Fix | Delete
}
[271] Fix | Delete
[272] Fix | Delete
return "wp_async_$action";
[273] Fix | Delete
}
[274] Fix | Delete
[275] Fix | Delete
/**
[276] Fix | Delete
* Prepare any data to be passed to the asynchronous postback
[277] Fix | Delete
*
[278] Fix | Delete
* The array this function receives will be a numerically keyed array from
[279] Fix | Delete
* func_get_args(). It is expected that you will return an associative array
[280] Fix | Delete
* so that the $_POST values used in the asynchronous call will make sense.
[281] Fix | Delete
*
[282] Fix | Delete
* The array you send back may or may not have anything to do with the data
[283] Fix | Delete
* passed into this method. It all depends on the implementation details and
[284] Fix | Delete
* what data is needed in the asynchronous postback.
[285] Fix | Delete
*
[286] Fix | Delete
* Do not set values for 'action' or '_nonce', as those will get overwritten
[287] Fix | Delete
* later in launch().
[288] Fix | Delete
*
[289] Fix | Delete
* @throws Exception If the postback should not occur for any reason.
[290] Fix | Delete
*
[291] Fix | Delete
* @param array $data The raw data received by the launch method.
[292] Fix | Delete
*
[293] Fix | Delete
* @return array The prepared data
[294] Fix | Delete
*/
[295] Fix | Delete
abstract protected function prepare_data( $data );
[296] Fix | Delete
[297] Fix | Delete
/**
[298] Fix | Delete
* Run the do_action function for the asynchronous postback.
[299] Fix | Delete
*
[300] Fix | Delete
* This method needs to fetch and sanitize any and all data from the $_POST
[301] Fix | Delete
* superglobal and provide them to the do_action call.
[302] Fix | Delete
*
[303] Fix | Delete
* The action should be constructed as "wp_async_task_$this->action"
[304] Fix | Delete
*/
[305] Fix | Delete
abstract protected function run_action();
[306] Fix | Delete
[307] Fix | Delete
}
[308] Fix | Delete
[309] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function