| 1 |
<?php |
| 2 |
|
| 3 |
class Logtivity_WP_All_Import |
| 4 |
{ |
| 5 |
protected $allowedAllImportLogs = [ |
| 6 |
'All Import Running', |
| 7 |
'All Import Completed', |
| 8 |
]; |
| 9 |
|
| 10 |
public function __construct() |
| 11 |
{ |
| 12 |
if ($this->isImporting()) { |
| 13 |
$this->logImportButDisableIndividualChanges(); |
| 14 |
} |
| 15 |
|
| 16 |
add_action('pmxi_before_xml_import', [$this, 'importRunning'], 10, 1); |
| 17 |
add_action('pmxi_after_xml_import', [$this, 'importCompleted'], 10, 2); |
| 18 |
} |
| 19 |
|
| 20 |
public function importRunning($import_id) |
| 21 |
{ |
| 22 |
Logtivity_Logger::log() |
| 23 |
->setAction('All Import Running') |
| 24 |
->setContext($import_id) |
| 25 |
->addMeta('Import ID', $import_id) |
| 26 |
->send(); |
| 27 |
} |
| 28 |
|
| 29 |
public function importCompleted($import_id, $import) |
| 30 |
{ |
| 31 |
Logtivity_Logger::log() |
| 32 |
->setAction('All Import Completed') |
| 33 |
->setContext($import_id) |
| 34 |
->addMeta('Import ID', $import_id) |
| 35 |
->send(); |
| 36 |
} |
| 37 |
|
| 38 |
public function isImporting() |
| 39 |
{ |
| 40 |
// Manually running an import |
| 41 |
$manual_run = isset( $_GET['page'] ) && ( $_GET['page'] == 'pmxi-admin-import' || $_GET['page'] == 'pmxi-admin-manage' ); |
| 42 |
|
| 43 |
// Cron jobs running an import |
| 44 |
$cron_run = isset( $_GET['import_id'] ) && isset( $_GET['action'] ) && $_GET['action'] == 'processing'; |
| 45 |
|
| 46 |
// Automatic Scheduling service running an import |
| 47 |
$automatic_scheduling_run = ( isset( $_GET['action'] ) && $_GET['action'] == 'wpai_public_api' ) && ( isset( $_GET['q'] ) && $_GET['q'] == 'scheduling/process' ); |
| 48 |
|
| 49 |
// Manually uploading an import file |
| 50 |
$manual_upload = ( isset($_GET['page']) && $_GET['page'] == 'pmxi-admin-settings' ) && ( isset( $_GET['action'] ) && $_GET['action'] == 'upload' ); |
| 51 |
|
| 52 |
// Extra check: see if import ID is numeric |
| 53 |
$import_id = is_numeric( $this->wp_all_import_get_import_id() ); |
| 54 |
|
| 55 |
// Check if they're importing |
| 56 |
if ( $manual_run || $cron_run || $automatic_scheduling_run || $manual_upload || $import_id ) { |
| 57 |
return true; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function logImportButDisableIndividualChanges() |
| 62 |
{ |
| 63 |
add_action('wp_logtivity_instance', function($Logtivity_Logger) { |
| 64 |
if (!in_array($Logtivity_Logger->action, $this->allowedAllImportLogs)) { |
| 65 |
$Logtivity_Logger->stop(); |
| 66 |
} |
| 67 |
}); |
| 68 |
} |
| 69 |
|
| 70 |
public function wp_all_import_get_import_id() { |
| 71 |
global $argv; |
| 72 |
$import_id = 'new'; |
| 73 |
|
| 74 |
if ( ! empty( $argv ) ) { |
| 75 |
$import_id_arr = array_filter( $argv, function( $a ) { |
| 76 |
return ( is_numeric( $a ) ) ? true : false; |
| 77 |
}); |
| 78 |
|
| 79 |
if ( ! empty( $import_id_arr ) ) { |
| 80 |
$import_id = reset( $import_id_arr ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( $import_id == 'new' ) { |
| 85 |
if ( isset( $_GET['import_id'] ) ) { |
| 86 |
$import_id = intval($_GET['import_id']); |
| 87 |
// } elseif ( isset( $_GET['id'] ) ) { |
| 88 |
// $import_id = intval($_GET['id']); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return $import_id; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
$Logtivity_WP_All_Import = new Logtivity_WP_All_Import; |