: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
namespace Smush\Core\Modules\Background;
use Smush\Core\Server_Utils;
* Abstract WP_Background_Process class.
abstract class Background_Process extends Async_Request {
const TASKS_PER_REQUEST_UNLIMITED = - 1;
* Start time of current process.
private $cron_hook_identifier;
* Cron_interval_identifier
private $cron_interval_identifier;
* @var Background_Logger_Container
private $logger_container;
* @var Background_Process_Status
private $tasks_per_request = self::TASKS_PER_REQUEST_UNLIMITED;
* Initiate new background process
public function __construct( $identifier ) {
parent::__construct( $identifier );
$this->cron_hook_identifier = $this->identifier . '_cron';
$this->cron_interval_identifier = $this->identifier . '_cron_interval';
add_action( $this->cron_hook_identifier, array( $this, 'handle_cron_healthcheck' ) );
add_filter( 'cron_schedules', array( $this, 'schedule_cron_healthcheck' ) );
$this->logger_container = new Background_Logger_Container( $this->identifier );
$this->status = new Background_Process_Status( $this->identifier );
$this->utils = new Background_Utils();
$this->server_utils = new Server_Utils();
private function generate_unique_id() {
return md5( microtime() . rand() );
* @return array|\WP_Error
public function dispatch( $instance_id ) {
$this->logger()->info( "Dispatching a new request for instance $instance_id." );
// Schedule the cron healthcheck.
return parent::dispatch( $instance_id );
public function spawn() {
$instance_id = $this->generate_unique_id();
$this->logger()->info( "Spawning a brand new instance (ID: $instance_id) for the process." );
$this->set_active_instance_id( $instance_id );
$this->dispatch( $instance_id );
* @param array $tasks An array of tasks.
private function update_queue( $tasks ) {
if ( ! empty( $tasks ) ) {
update_site_option( $this->get_queue_key(), $tasks );
private function delete_queue() {
delete_site_option( $this->get_queue_key() );
* Generates a unique key based on microtime. Queue items are
* given a unique key so that they can be merged upon save.
protected function get_queue_key() {
return $this->identifier . '_queue';
* Checks whether data exists within the queue and that
* the process is not already running.
public function maybe_handle() {
// Don't lock up other requests while processing
$this->mutex( function () {
$instance_id = empty( $_GET['instance_id'] )
: wp_unslash( $_GET['instance_id'] );
if ( $this->is_queue_empty() ) {
$this->logger()->warning( "Handler called with instance ID $instance_id but the queue is empty. Killing this instance." );
if ( ! $instance_id || ! $this->is_active_instance( $instance_id ) ) {
// We thought the process died, so we spawned a new instance.
// Kill this instance and let the new one continue.
$active_instance_id = $this->get_active_instance_id();
$this->logger()->warning( "Handler called with instance ID $instance_id but the active instance ID is $active_instance_id. Killing $instance_id so $active_instance_id can continue." );
if ( ! check_ajax_referer( $this->identifier, 'nonce', false ) ) {
$this->handle( $instance_id );
protected function is_queue_empty() {
return empty( $this->get_queue() );
* Check whether the current process is already running
* in a background process.
protected function is_process_running() {
if ( get_site_transient( $this->get_last_run_transient_key() ) ) {
// Process already running.
protected function update_timestamp( $instance_id ) {
$this->start_time = $timestamp; // Set start time of current process.
$this->get_last_run_transient_key(),
$this->get_instance_expiry_duration_seconds()
$human_readable_timestamp = wp_date( 'Y-m-d H:i:s', $timestamp );
$this->logger()->info( "Setting last run timestamp for instance ID $instance_id to $human_readable_timestamp" );
* @return array Return the first queue from the queue
protected function get_queue() {
$queue = $this->utils->get_site_option( $this->get_queue_key(), array() );
return empty( $queue ) || ! is_array( $queue )
* Pass each queue item to the task handler, while remaining
* within server memory and time limit constraints.
protected function handle( $instance_id ) {
$this->logger()->info( "Handling instance ID $instance_id." );
$this->update_timestamp( $instance_id );
$queue = $this->get_queue();
$processed_tasks_count = 0;
foreach ( $queue as $key => $value ) {
$this->logger()->info( "Executing task $value." );
$task = $this->task( $value );
$this->status->task_successful();
$this->status->task_failed();
if ( $this->status->is_cancelled() ) {
$this->logger()->info( "While we were busy doing the task $value, the process got cancelled. Clean up and stop." );
if ( $this->should_update_queue_after_task() ) {
$this->update_queue( $queue );
$processed_tasks_count ++;
if ( $this->task_limit_reached( $processed_tasks_count ) ) {
$tasks_per_request = $this->get_tasks_per_request();
$this->logger()->info( "Stopping because we are only supposed to perform $tasks_per_request tasks in a single request and we have reached that limit." );
if ( $this->time_exceeded() || $this->memory_exceeded() ) {
$this->logger()->warning( "Time/Memory limits reached, save the queue and dispatch a new request." );
if ( ! $this->should_update_queue_after_task() ) {
$this->update_queue( $queue );
$this->dispatch( $instance_id );
* Ensures the process never exceeds 90%
* of the maximum WordPress memory.
protected function memory_exceeded() {
$memory_limit = $this->server_utils->get_memory_limit() * 0.75; // 75% of max memory
$current_memory = $this->server_utils->get_memory_usage();
if ( $current_memory >= $memory_limit ) {
return apply_filters( $this->identifier . '_memory_exceeded', $return );
* Ensures the process never exceeds a sensible time limit.
* A timeout limit of 30s is common on shared hosting.
protected function time_exceeded() {
$finish = $this->start_time + $this->get_time_limit();
if ( time() >= $finish ) {
return apply_filters( $this->identifier . '_time_exceeded', $return );
* Override if applicable, but ensure that the below actions are
* performed, or, call parent::complete().
protected function complete() {
$this->do_action( 'completed' );
$this->logger()->info( "Process completed." );
$this->status->complete();
* Schedule cron healthcheck
* @param mixed $schedules Schedules.
public function schedule_cron_healthcheck( $schedules ) {
$interval = $this->get_cron_interval_seconds();
// Adds every 5 minutes to the existing schedules.
$schedules[ $this->identifier . '_cron_interval' ] = array(
/* translators: %s: Cron interval in minutes */
'display' => sprintf( __( 'Every %d Minutes', 'wp-smushit' ), $interval / MINUTE_IN_SECONDS ),
* Handle cron healthcheck
* Restart the background process if not already running
* and data exists in the queue.
public function handle_cron_healthcheck() {
$mutex = new Mutex( $this->identifier . '_cron_healthcheck' );
$mutex->set_break_on_timeout( true )
->set_timeout( 1 ) // We don't want two health checks running
$this->logger()->info( "Running scheduled health check." );
if ( $this->is_process_running() ) {
$this->logger()->info( "Health check: Process seems healthy, no action required." );
if ( $this->is_queue_empty() ) {
$this->logger()->info( "Health check: Process not in progress but the queue is empty, no action required." );
$this->clear_scheduled_event();
if ( $this->status->is_cancelled() ) {
$this->logger()->info( "Health check: Process has been cancelled already, no action required." );
$this->clear_scheduled_event();
if ( ! $this->is_revival_limit_reached() ) {
$this->logger()->warning( "Health check: Process instance seems to have died. Spawn a new instance." );
$this->logger()->warning( "Health check: Process instance seems to have died. Restart disabled, marking the process as dead." );
private function revive_process() {
$this->do_action( 'revived' );
$this->increment_revival_count();
protected function mark_as_dead() {
$this->do_action( 'dead' );
$this->status->mark_as_dead();
protected function schedule_event() {
$hook = $this->cron_hook_identifier;
if ( ! wp_next_scheduled( $hook ) ) {
$interval = $this->cron_interval_identifier;
$next_run = time() + $this->get_cron_interval_seconds();
wp_schedule_event( $next_run, $interval, $hook );
$this->logger()->info( "Scheduling new event with hook $hook to run $interval." );
protected function clear_scheduled_event() {
$hook = $this->cron_hook_identifier;
$this->logger()->info( "Cancelling event with hook $hook." );
wp_clear_scheduled_hook( $hook );
* Stop processing queue items, clear cronjob and delete queue.
private function cancel_process() {
$this->logger()->info( "Process cancelled." );
public function cancel() {
// Update the cancel flag first
$active_instance_id = $this->get_active_instance_id();
$this->logger()->info( "Starting cancellation (Instance: $active_instance_id)." );
// Since actual cancellation involves deletion of the queue and the handler
// might be in the middle of updating the queue, we need to use a mutex
$mutex = new Mutex( $this->get_handler_mutex_id() );
->set_break_on_timeout( false ) // Since this is a user operation, we must cancel, even if there is a timeout
->set_timeout( $this->get_time_limit() ) // Shouldn't take more time than the time allocated to the process itself
->execute( function () use ( $active_instance_id ) {
// Do this before cleanup, so we still have data available to us
$this->do_action( 'cancelled' );
$this->logger()->info( "Cancelling the process (Instance: $active_instance_id)." );
$this->logger()->info( "Cancellation completed (Instance: $active_instance_id)." );
* Override this method to perform any actions required on each
* queue item. Return the modified item for further processing
* in the next pass through. Or, return false to remove the
* @param mixed $task Queue item to iterate over.
abstract protected function task( $task );
private function is_active_instance( $instance_id ) {
return $instance_id === $this->get_active_instance_id();
* Save the unique ID of the process we are presuming to be dead, so we can prevent it from coming back.
private function set_active_instance_id( $instance_id ) {
update_site_option( $this->get_active_instance_option_id(), $instance_id );
private function get_active_instance_id() {
return get_site_option( $this->get_active_instance_option_id(), '' );
private function get_active_instance_option_id() {