| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* @package Logtivity |
| 5 |
* @contact logtivity.io, [email protected] |
| 6 |
* @copyright 2024-2026 Logtivity. All rights reserved |
| 7 |
* @license https://www.gnu.org/licenses/gpl.html GNU/GPL |
| 8 |
* |
| 9 |
* This file is part of Logtivity. |
| 10 |
* |
| 11 |
* Logtivity is free software: you can redistribute it and/or modify |
| 12 |
* it under the terms of the GNU General Public License as published by |
| 13 |
* the Free Software Foundation, either version 2 of the License, or |
| 14 |
* (at your option) any later version. |
| 15 |
* |
| 16 |
* Logtivity is distributed in the hope that it will be useful, |
| 17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 |
* GNU General Public License for more details. |
| 20 |
* |
| 21 |
* You should have received a copy of the GNU General Public License |
| 22 |
* along with Logtivity. If not, see <https://www.gnu.org/licenses/>. |
| 23 |
*/ |
| 24 |
|
| 25 |
class Logtivity_WP_All_Import |
| 26 |
{ |
| 27 |
protected $allowedAllImportLogs = [ |
| 28 |
'All Import Running', |
| 29 |
'All Import Completed', |
| 30 |
]; |
| 31 |
|
| 32 |
public function __construct() |
| 33 |
{ |
| 34 |
if ($this->isImporting()) { |
| 35 |
$this->logImportButDisableIndividualChanges(); |
| 36 |
} |
| 37 |
|
| 38 |
add_action('pmxi_before_xml_import', [$this, 'importRunning'], 10, 1); |
| 39 |
add_action('pmxi_after_xml_import', [$this, 'importCompleted'], 10, 2); |
| 40 |
} |
| 41 |
|
| 42 |
public function importRunning($import_id) |
| 43 |
{ |
| 44 |
Logtivity::log() |
| 45 |
->setAction('All Import Running') |
| 46 |
->setContext($import_id) |
| 47 |
->addMeta('Import ID', $import_id) |
| 48 |
->send(); |
| 49 |
} |
| 50 |
|
| 51 |
public function importCompleted($import_id, $import) |
| 52 |
{ |
| 53 |
Logtivity::log() |
| 54 |
->setAction('All Import Completed') |
| 55 |
->setContext($import_id) |
| 56 |
->addMeta('Import ID', $import_id) |
| 57 |
->send(); |
| 58 |
} |
| 59 |
|
| 60 |
public function isImporting() |
| 61 |
{ |
| 62 |
// Manually running an import |
| 63 |
$manual_run = isset( $_GET['page'] ) && ( $_GET['page'] == 'pmxi-admin-import' || $_GET['page'] == 'pmxi-admin-manage' ); |
| 64 |
|
| 65 |
// Cron jobs running an import |
| 66 |
$cron_run = isset( $_GET['import_id'] ) && isset( $_GET['action'] ) && $_GET['action'] == 'processing'; |
| 67 |
|
| 68 |
// Automatic Scheduling service running an import |
| 69 |
$automatic_scheduling_run = ( isset( $_GET['action'] ) && $_GET['action'] == 'wpai_public_api' ) && ( isset( $_GET['q'] ) && $_GET['q'] == 'scheduling/process' ); |
| 70 |
|
| 71 |
// Manually uploading an import file |
| 72 |
$manual_upload = ( isset($_GET['page']) && $_GET['page'] == 'pmxi-admin-settings' ) && ( isset( $_GET['action'] ) && $_GET['action'] == 'upload' ); |
| 73 |
|
| 74 |
// Extra check: see if import ID is numeric |
| 75 |
$import_id = is_numeric( $this->wp_all_import_get_import_id() ); |
| 76 |
|
| 77 |
// Check if they're importing |
| 78 |
if ( $manual_run || $cron_run || $automatic_scheduling_run || $manual_upload || $import_id ) { |
| 79 |
return true; |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
public function logImportButDisableIndividualChanges() |
| 84 |
{ |
| 85 |
add_action('wp_logtivity_instance', function($Logtivity_Logger) { |
| 86 |
if (!in_array($Logtivity_Logger->action, $this->allowedAllImportLogs)) { |
| 87 |
$Logtivity_Logger->stop(); |
| 88 |
} |
| 89 |
}); |
| 90 |
} |
| 91 |
|
| 92 |
public function wp_all_import_get_import_id() { |
| 93 |
global $argv; |
| 94 |
$import_id = 'new'; |
| 95 |
|
| 96 |
if ( ! empty( $argv ) ) { |
| 97 |
$import_id_arr = array_filter( $argv, function( $a ) { |
| 98 |
return ( is_numeric( $a ) ) ? true : false; |
| 99 |
}); |
| 100 |
|
| 101 |
if ( ! empty( $import_id_arr ) ) { |
| 102 |
$import_id = reset( $import_id_arr ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if ( $import_id == 'new' ) { |
| 107 |
if ( isset( $_GET['import_id'] ) ) { |
| 108 |
$import_id = intval($_GET['import_id']); |
| 109 |
// } elseif ( isset( $_GET['id'] ) ) { |
| 110 |
// $import_id = intval($_GET['id']); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
return $import_id; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
$Logtivity_WP_All_Import = new Logtivity_WP_All_Import; |
| 119 |
|