first commit
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Word_Count_Background_Process_Factory {
|
||||
|
||||
const PREFIX = 'wpml_tm';
|
||||
const ACTION_REQUESTED_TYPES = 'word_count_background_process_requested_types';
|
||||
|
||||
/**
|
||||
* @return WPML_TM_Word_Count_Background_Process_Requested_Types
|
||||
*/
|
||||
public function create_requested_types() {
|
||||
$records_factory = new WPML_TM_Word_Count_Records_Factory();
|
||||
$records = $records_factory->create();
|
||||
|
||||
$setters_factory = new WPML_TM_Word_Count_Setters_Factory();
|
||||
$setters = $setters_factory->create();
|
||||
|
||||
$requested_types_items = new WPML_TM_Word_Count_Queue_Items_Requested_Types( $records );
|
||||
|
||||
return new WPML_TM_Word_Count_Background_Process_Requested_Types( $requested_types_items, $setters, $records );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
class WPML_TM_Word_Count_Background_Process_Requested_Types extends WPML_TM_Word_Count_Background_Process {
|
||||
|
||||
/** @var WPML_TM_Word_Count_Queue_Items_Requested_Types $queue */
|
||||
protected $queue;
|
||||
|
||||
/** @var WPML_TM_Word_Count_Records $records */
|
||||
private $records;
|
||||
|
||||
/**
|
||||
* @param WPML_TM_Word_Count_Queue_Items_Requested_Types $queue_items
|
||||
* @param IWPML_TM_Word_Count_Set[] $setters
|
||||
*/
|
||||
public function __construct(
|
||||
WPML_TM_Word_Count_Queue_Items_Requested_Types $queue_items,
|
||||
array $setters,
|
||||
WPML_TM_Word_Count_Records $records
|
||||
) {
|
||||
/** We need to set the action before constructing the parent class `WP_Async_Request` */
|
||||
$this->action = WPML_TM_Word_Count_Background_Process_Factory::ACTION_REQUESTED_TYPES;
|
||||
parent::__construct( $queue_items, $setters );
|
||||
$this->records = $records;
|
||||
|
||||
add_filter(
|
||||
'wpml_tm_word_count_background_process_requested_types_memory_exceeded',
|
||||
array(
|
||||
$this,
|
||||
'memory_exceeded_filter',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function init( $requested_types ) {
|
||||
$this->queue->reset( $requested_types );
|
||||
$this->records->reset_all( $requested_types );
|
||||
$this->dispatch();
|
||||
}
|
||||
|
||||
public function dispatch() {
|
||||
update_option(
|
||||
WPML_TM_Word_Count_Hooks_Factory::OPTION_KEY_REQUESTED_TYPES_STATUS,
|
||||
WPML_TM_Word_Count_Hooks_Factory::PROCESS_IN_PROGRESS
|
||||
);
|
||||
|
||||
parent::dispatch();
|
||||
}
|
||||
|
||||
public function complete() {
|
||||
update_option(
|
||||
WPML_TM_Word_Count_Hooks_Factory::OPTION_KEY_REQUESTED_TYPES_STATUS,
|
||||
WPML_TM_Word_Count_Hooks_Factory::PROCESS_COMPLETED
|
||||
);
|
||||
|
||||
parent::complete();
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter result of memory_exceeded() function in WP_Background_Process class.
|
||||
* Used by it get_memory_limit() function of WP_Background_Process class contains a number of bugs,
|
||||
* producing wrong result when 'memory_limit' setting in php.ini is in human readable format like '1G'.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function memory_exceeded_filter() {
|
||||
$memory_limit = $this->get_memory_limit() * 0.9; // 90% of max memory
|
||||
$current_memory = memory_get_usage( true );
|
||||
|
||||
return $current_memory >= $memory_limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get memory limit in bytes.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
protected function get_memory_limit() {
|
||||
if ( function_exists( 'ini_get' ) ) {
|
||||
$memory_limit = ini_get( 'memory_limit' );
|
||||
} else {
|
||||
// Sensible default.
|
||||
$memory_limit = '128M';
|
||||
}
|
||||
|
||||
if ( ! $memory_limit || - 1 === intval( $memory_limit ) ) {
|
||||
// Unlimited, set to 32GB.
|
||||
$memory_limit = '32000M';
|
||||
}
|
||||
|
||||
return $this->convert_shorthand_to_bytes( $memory_limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a shorthand byte value to an integer byte value.
|
||||
*
|
||||
* @param string $value A (PHP ini) byte value, either shorthand or ordinary.
|
||||
* @return int An integer byte value.
|
||||
*/
|
||||
protected function convert_shorthand_to_bytes( $value ) {
|
||||
$value = strtolower( trim( $value ) );
|
||||
$bytes = (int) $value;
|
||||
|
||||
if ( false !== strpos( $value, 'g' ) ) {
|
||||
$bytes *= 1024 * 1024 * 1024;
|
||||
} elseif ( false !== strpos( $value, 'm' ) ) {
|
||||
$bytes *= 1024 * 1024;
|
||||
} elseif ( false !== strpos( $value, 'k' ) ) {
|
||||
$bytes *= 1024;
|
||||
}
|
||||
|
||||
// Deal with large (float) values which run into the maximum integer size.
|
||||
return min( $bytes, PHP_INT_MAX );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
abstract class WPML_TM_Word_Count_Background_Process extends WP_Background_Process {
|
||||
|
||||
/** @var IWPML_TM_Word_Count_Queue_Items $queue */
|
||||
protected $queue;
|
||||
|
||||
/** @var IWPML_TM_Word_Count_Set[] $setters */
|
||||
private $setters;
|
||||
|
||||
/**
|
||||
* @param IWPML_TM_Word_Count_Queue_Items $queue
|
||||
* @param IWPML_TM_Word_Count_Set[] $setters
|
||||
*/
|
||||
public function __construct( IWPML_TM_Word_Count_Queue_Items $queue, array $setters ) {
|
||||
/** We need to set the prefix and the identifier before constructing the parent class `WP_Async_Request` */
|
||||
$this->prefix = WPML_TM_Word_Count_Background_Process_Factory::PREFIX;
|
||||
$this->action = WPML_TM_Word_Count_Background_Process_Factory::ACTION_REQUESTED_TYPES;
|
||||
|
||||
parent::__construct();
|
||||
|
||||
$this->queue = $queue;
|
||||
$this->setters = $setters;
|
||||
}
|
||||
|
||||
/**
|
||||
* This abstract method is not implemented because we override the `handle` method.
|
||||
*/
|
||||
protected function task( $item ) {}
|
||||
|
||||
protected function handle() {
|
||||
$this->lock_process();
|
||||
|
||||
while ( ! $this->time_exceeded() && ! $this->memory_exceeded() && ! $this->queue->is_completed() ) {
|
||||
|
||||
list( $id, $type ) = $this->queue->get_next();
|
||||
|
||||
if ( $id && $type ) {
|
||||
$this->setters[ $type ]->process( $id );
|
||||
$this->queue->remove( $id, $type );
|
||||
}
|
||||
}
|
||||
|
||||
$this->queue->save();
|
||||
$this->unlock_process();
|
||||
|
||||
if ( $this->queue->is_completed() ) {
|
||||
$this->complete();
|
||||
} else {
|
||||
$this->dispatch();
|
||||
}
|
||||
|
||||
wp_die();
|
||||
}
|
||||
|
||||
protected function is_queue_empty() {
|
||||
return $this->queue->is_completed();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user