| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\Exception\MapperException; |
| 6 |
use ImportWP\Common\Importer\PermissionInterface; |
| 7 |
use ImportWP\Common\Importer\Template\Template; |
| 8 |
use ImportWP\Common\Importer\TemplateInterface; |
| 9 |
use ImportWP\Common\Model\ImporterModel; |
| 10 |
|
| 11 |
class AbstractMapper |
| 12 |
{ |
| 13 |
protected $ID; |
| 14 |
/** |
| 15 |
* Importer Template |
| 16 |
* |
| 17 |
* @var TemplateInterface |
| 18 |
*/ |
| 19 |
protected $template; |
| 20 |
/** |
| 21 |
* Importer Data |
| 22 |
* |
| 23 |
* @var ImporterModel |
| 24 |
*/ |
| 25 |
protected $importer; |
| 26 |
/** |
| 27 |
* Importer Permissions |
| 28 |
* |
| 29 |
* @var PermissionInterface |
| 30 |
*/ |
| 31 |
protected $permission; |
| 32 |
|
| 33 |
public function __construct(ImporterModel $importer, Template $template, PermissionInterface $permission = null) |
| 34 |
{ |
| 35 |
$this->importer = $importer; |
| 36 |
$this->template = $template; |
| 37 |
$this->permission = $permission; |
| 38 |
} |
| 39 |
|
| 40 |
public function permission() |
| 41 |
{ |
| 42 |
if (is_null($this->permission)) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
return $this->permission; |
| 47 |
} |
| 48 |
|
| 49 |
public function getUniqueIdentifiers($unique_fields = []) |
| 50 |
{ |
| 51 |
|
| 52 |
// set via importer interface |
| 53 |
$unique_identifier = $this->importer->getSetting('unique_identifier'); |
| 54 |
if (empty($unique_identifier)) { |
| 55 |
return $unique_fields; |
| 56 |
} |
| 57 |
|
| 58 |
$parts = array_filter(array_map('trim', explode(',', $unique_identifier))); |
| 59 |
if (empty($parts)) { |
| 60 |
return $unique_fields; |
| 61 |
} |
| 62 |
|
| 63 |
return $parts; |
| 64 |
} |
| 65 |
|
| 66 |
public function is_session_tag_enabled() |
| 67 |
{ |
| 68 |
$db_version = intval(get_site_option('iwp_db_version', 0)); |
| 69 |
$config_data = get_site_option('iwp_importer_config_' . $this->importer->getId(), []); |
| 70 |
|
| 71 |
if ($db_version >= 7 && isset($config_data['features'], $config_data['features']['session_table']) && $config_data['features']['session_table']) { |
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Update or insert record in session table |
| 80 |
* |
| 81 |
* Session table keeps track of what records where modified during the import |
| 82 |
* |
| 83 |
* @return void |
| 84 |
*/ |
| 85 |
public function add_session_tag($type) |
| 86 |
{ |
| 87 |
/** |
| 88 |
* @var \WPDB $wpdb |
| 89 |
*/ |
| 90 |
global $wpdb; |
| 91 |
|
| 92 |
$data = [ |
| 93 |
'importer_id' => $this->importer->getId(), |
| 94 |
'item_id' => $this->ID, |
| 95 |
'item_type' => $type, |
| 96 |
]; |
| 97 |
$data_validation = ['%d', '%d', '%s']; |
| 98 |
|
| 99 |
if (is_multisite()) { |
| 100 |
$data['site_id'] = $wpdb->siteid; |
| 101 |
$data_validation[] = '%d'; |
| 102 |
} |
| 103 |
|
| 104 |
$updated = $wpdb->update($wpdb->prefix . 'iwp_sessions', [ |
| 105 |
'session' => $this->importer->getStatusId() |
| 106 |
], $data, ['%s'], $data_validation); |
| 107 |
|
| 108 |
if (!$updated) { |
| 109 |
|
| 110 |
$wpdb->insert($wpdb->prefix . 'iwp_sessions', array_merge($data, [ |
| 111 |
'session' => $this->importer->getStatusId() |
| 112 |
])); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Remove record from session table |
| 118 |
* |
| 119 |
* @param int $id |
| 120 |
* @param string $type |
| 121 |
* @return void |
| 122 |
*/ |
| 123 |
public function remove_session_tag($id, $type) |
| 124 |
{ |
| 125 |
/** |
| 126 |
* @var \WPDB $wpdb |
| 127 |
*/ |
| 128 |
global $wpdb; |
| 129 |
|
| 130 |
$data = [ |
| 131 |
'importer_id' => $this->importer->getId(), |
| 132 |
'item_id' => $id, |
| 133 |
'item_type' => $type, |
| 134 |
]; |
| 135 |
$data_validation = ['%d', '%d', '%s']; |
| 136 |
|
| 137 |
if (is_multisite()) { |
| 138 |
$data['site_id'] = $wpdb->siteid; |
| 139 |
$data_validation[] = '%d'; |
| 140 |
} |
| 141 |
|
| 142 |
$wpdb->delete($wpdb->prefix . 'iwp_sessions', $data, $data_validation); |
| 143 |
} |
| 144 |
|
| 145 |
public function get_ids_without_session_tag($type) |
| 146 |
{ |
| 147 |
/** |
| 148 |
* @var \WPDB $wpdb |
| 149 |
*/ |
| 150 |
global $wpdb; |
| 151 |
|
| 152 |
$query = "SELECT item_id FROM `{$wpdb->prefix}iwp_sessions` WHERE importer_id={$this->importer->getId()} AND item_type='{$type}' AND `session` != '{$this->importer->getStatusId()}'"; |
| 153 |
|
| 154 |
if (is_multisite()) { |
| 155 |
$query .= " AND site_id='{$wpdb->siteid}'"; |
| 156 |
} |
| 157 |
|
| 158 |
$item_ids = $wpdb->get_col($query); |
| 159 |
return $item_ids; |
| 160 |
} |
| 161 |
} |
| 162 |
|