| 1 |
<?php |
| 2 |
/** |
| 3 |
* Declare class Revert_All |
| 4 |
* |
| 5 |
* @package Revert_All |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace LassoLite\Classes\Processes; |
| 9 |
|
| 10 |
use LassoLite\Classes\Import as Lasso_Import; |
| 11 |
use LassoLite\Classes\Lasso_DB; |
| 12 |
use LassoLite\Classes\Processes\Process; |
| 13 |
use LassoLite\Classes\Processes\Import_All; |
| 14 |
|
| 15 |
use LassoLite\Models\Model; |
| 16 |
|
| 17 |
/** |
| 18 |
* Revert_All |
| 19 |
*/ |
| 20 |
class Revert_All extends Process { |
| 21 |
const LIMIT = 500; |
| 22 |
const OPTION = 'lasso_lite_revert_all_enable'; |
| 23 |
|
| 24 |
/** |
| 25 |
* Action name |
| 26 |
* |
| 27 |
* @var string $action |
| 28 |
*/ |
| 29 |
protected $action = 'lassolite_revert_process_all'; |
| 30 |
|
| 31 |
/** |
| 32 |
* Log name |
| 33 |
* |
| 34 |
* @var string $log_name |
| 35 |
*/ |
| 36 |
protected $log_name = 'lite_bulk_revert'; |
| 37 |
|
| 38 |
/** |
| 39 |
* Revert_All constructor. |
| 40 |
*/ |
| 41 |
public function __construct() { |
| 42 |
parent::__construct(); |
| 43 |
add_action( self::COMPLETE_ACTION_KEY, array( $this, 'complete_action' ), 10, 1 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Task |
| 48 |
* |
| 49 |
* Override this method to perform any actions required on each |
| 50 |
* queue item. Return the modified item for further processing |
| 51 |
* in the next pass through. Or, return false to remove the |
| 52 |
* item from the queue. |
| 53 |
* |
| 54 |
* @param mixed $data Queue item to iterate over. |
| 55 |
* |
| 56 |
* @return mixed |
| 57 |
*/ |
| 58 |
public function task( $data ) { |
| 59 |
$start_time = microtime( true ); |
| 60 |
|
| 61 |
$lasso_import = new Lasso_Import(); |
| 62 |
$lasso_import->process_single_link_revert( $data['revert_id'], $data['source'] ); |
| 63 |
|
| 64 |
$this->set_processing_runtime(); |
| 65 |
$time_end = microtime( true ); |
| 66 |
$execution_time = round( $time_end - $start_time, 2 ); |
| 67 |
|
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Prepare data for process |
| 73 |
* |
| 74 |
* @param string $filter_plugin Plugin name. Default to null (all plugins). |
| 75 |
*/ |
| 76 |
public function revert( $filter_plugin = null ) { |
| 77 |
// ? check whether process is age out and make it can work on Lasso UI via ajax requests |
| 78 |
$this->is_process_age_out(); |
| 79 |
|
| 80 |
if ( $this->is_process_running() ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
$lasso_db = new Lasso_DB(); |
| 85 |
|
| 86 |
$sql = $lasso_db->get_revertable_urls_query( $filter_plugin ); |
| 87 |
$sql = $lasso_db->paginate( $sql, 1, self::LIMIT ); |
| 88 |
$all_reverts = Model::get_results( $sql ); |
| 89 |
$count = count( $all_reverts ); |
| 90 |
|
| 91 |
if ( $count <= 0 ) { |
| 92 |
update_option( self::OPTION, '0' ); |
| 93 |
return false; |
| 94 |
} |
| 95 |
update_option( self::OPTION, '1' ); |
| 96 |
|
| 97 |
// ? disable/remove import all process |
| 98 |
$import_all_process = new Import_All(); |
| 99 |
$import_all_process->remove_process(); |
| 100 |
update_option( Import_All::OPTION, '0' ); |
| 101 |
delete_option( Import_All::FILTER_PLUGIN ); |
| 102 |
|
| 103 |
foreach ( $all_reverts as $revert ) { |
| 104 |
if ( empty( $revert->import_id ) || empty( $revert->import_source ) ) { |
| 105 |
continue; |
| 106 |
} |
| 107 |
$this->push_to_queue( |
| 108 |
array( |
| 109 |
'revert_id' => $revert->import_id, |
| 110 |
'source' => $revert->import_source, |
| 111 |
) |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
$this->set_total( $count ); |
| 116 |
$this->set_log_file_name( $this->log_name ); |
| 117 |
$this->task_start_log(); |
| 118 |
// ? save queue |
| 119 |
$this->save()->dispatch(); |
| 120 |
|
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Complete action |
| 126 |
* |
| 127 |
* @param string $action Action name. |
| 128 |
* @return $this |
| 129 |
*/ |
| 130 |
public function complete_action( $action ) { |
| 131 |
// ? If there is nothing to revert, we disable the revert all process at this time instead of waiting for the next cron(15 minutes for Lite) |
| 132 |
if ( $action === $this->action ) { |
| 133 |
$lasso_db = new Lasso_DB(); |
| 134 |
$sql = $lasso_db->get_revertable_urls_query( null ); |
| 135 |
$sql = $lasso_db->paginate( $sql, 1, 1 ); |
| 136 |
$all_reverts = Model::get_results( $sql ); |
| 137 |
$count = count( $all_reverts ); |
| 138 |
|
| 139 |
if ( $count <= 0 ) { |
| 140 |
update_option( self::OPTION, '0' ); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
return $this; |
| 145 |
} |
| 146 |
} |
| 147 |
|