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