| 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 |
|
| 16 |
use LassoLite\Models\Model; |
| 17 |
|
| 18 |
/** |
| 19 |
* Lasso_Process_Import_All |
| 20 |
*/ |
| 21 |
class Import_All extends Process { |
| 22 |
const LIMIT = 500; |
| 23 |
const OPTION = 'lasso_lite_import_all_enable'; |
| 24 |
|
| 25 |
/** |
| 26 |
* Action name |
| 27 |
* |
| 28 |
* @var string $action |
| 29 |
*/ |
| 30 |
protected $action = 'lassolite_import_process_all'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Log name |
| 34 |
* |
| 35 |
* @var string $log_name |
| 36 |
*/ |
| 37 |
protected $log_name = 'lite_bulk_import'; |
| 38 |
|
| 39 |
/** |
| 40 |
* Task |
| 41 |
* |
| 42 |
* Override this method to perform any actions required on each |
| 43 |
* queue item. Return the modified item for further processing |
| 44 |
* in the next pass through. Or, return false to remove the |
| 45 |
* item from the queue. |
| 46 |
* |
| 47 |
* @param mixed $data Queue item to iterate over. |
| 48 |
* |
| 49 |
* @return mixed |
| 50 |
*/ |
| 51 |
public function task( $data ) { |
| 52 |
$time_start = microtime( true ); |
| 53 |
$import_id = $data['import_id'] ?? false; |
| 54 |
$post_type = $data['post_type'] ?? false; |
| 55 |
$post_title = $data['post_title'] ?? ''; |
| 56 |
$import_permalink = $data['import_permalink'] ?? ''; |
| 57 |
|
| 58 |
$lasso_import = new Lasso_Import(); |
| 59 |
if ( $import_id && $post_type ) { |
| 60 |
$lasso_import->process_single_link_data( $import_id, $post_type, $post_title, $import_permalink ); |
| 61 |
} |
| 62 |
|
| 63 |
$this->set_processing_runtime(); |
| 64 |
$time_end = microtime( true ); |
| 65 |
$execution_time = round( $time_end - $time_start, 2 ); |
| 66 |
|
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Prepare data for process |
| 72 |
* |
| 73 |
* @param string $filter_plugin Plugin name. |
| 74 |
*/ |
| 75 |
public function import( $filter_plugin = null ) { |
| 76 |
// ? check whether process is age out and make it can work on Lasso UI via ajax requests |
| 77 |
$this->is_process_age_out(); |
| 78 |
|
| 79 |
if ( $this->is_process_running() ) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
$lasso_db = new Lasso_DB(); |
| 84 |
|
| 85 |
$sql = $lasso_db->get_importable_urls_query( false, '', '', $filter_plugin ); |
| 86 |
$sql = $lasso_db->paginate( $sql, 1, self::LIMIT ); |
| 87 |
$all_imports = Model::get_results( $sql ); |
| 88 |
$count = count( $all_imports ); |
| 89 |
|
| 90 |
if ( $count <= 0 ) { |
| 91 |
update_option( self::OPTION, '0' ); |
| 92 |
return false; |
| 93 |
} |
| 94 |
update_option( self::OPTION, '1' ); |
| 95 |
|
| 96 |
foreach ( $all_imports as $import ) { |
| 97 |
$import = Lasso_Helper::format_importable_data( $import ); |
| 98 |
if ( empty( $import->id ) || empty( $import->post_type ) || 'checked' === $import->check_status ) { |
| 99 |
continue; |
| 100 |
} |
| 101 |
|
| 102 |
$this->push_to_queue( |
| 103 |
array( |
| 104 |
'import_id' => $import->id, |
| 105 |
'post_type' => $import->post_type, |
| 106 |
'post_title' => Lasso_Helper::remove_unexpected_characters_from_post_title( $import->post_title ), |
| 107 |
'import_permalink' => $import->import_permalink, |
| 108 |
) |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
$this->set_total( $count ); |
| 113 |
$this->set_log_file_name( $this->log_name ); |
| 114 |
$this->task_start_log(); |
| 115 |
// ? save queue |
| 116 |
$this->save()->dispatch(); |
| 117 |
|
| 118 |
return true; |
| 119 |
} |
| 120 |
} |
| 121 |
|