| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Template; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\TemplateInterface; |
| 6 |
use ImportWP\EventHandler; |
| 7 |
|
| 8 |
class CommentTemplate extends Template implements TemplateInterface |
| 9 |
{ |
| 10 |
protected $name = 'Comment'; |
| 11 |
protected $mapper = 'comment'; |
| 12 |
|
| 13 |
public function __construct(EventHandler $event_handler) |
| 14 |
{ |
| 15 |
parent::__construct($event_handler); |
| 16 |
|
| 17 |
$this->groups[] = 'comment'; |
| 18 |
|
| 19 |
$this->field_options = array_merge($this->field_options, [ |
| 20 |
'comment._parent.id' => [$this, 'get_parent_options'], |
| 21 |
]); |
| 22 |
} |
| 23 |
|
| 24 |
public function register() |
| 25 |
{ |
| 26 |
|
| 27 |
$post_types = get_post_types(); |
| 28 |
$post_type_options = [ |
| 29 |
['label' => __('Any', 'jc-importer'), 'value' => ''] |
| 30 |
]; |
| 31 |
foreach ($post_types as $post_type => $label) { |
| 32 |
$post_type_options[] = ['label' => $label, 'value' => $post_type]; |
| 33 |
} |
| 34 |
|
| 35 |
return [ |
| 36 |
$this->register_group(__('Comment', 'jc-importer'), 'comment', [ |
| 37 |
|
| 38 |
// Comment |
| 39 |
$this->register_field(__('ID', 'jc-importer'), 'comment_ID', [ |
| 40 |
'tooltip' => __('ID is only used to reference existing records', 'jc-importer') |
| 41 |
]), |
| 42 |
$this->register_field(__('Content', 'jc-importer'), 'comment_content', [ |
| 43 |
'tooltip' => __('The content of the comment.', 'jc-importer') |
| 44 |
]), |
| 45 |
$this->register_group(__('Comment Parent', 'jc-importer'), '_parent', [ |
| 46 |
|
| 47 |
$this->register_field(__('Comment Parent', 'jc-importer'), 'id', [ |
| 48 |
'default' => '', |
| 49 |
'options' => 'callback', |
| 50 |
'tooltip' => __('ID of this comment\'s parent, if any.', 'jc-importer') |
| 51 |
]), |
| 52 |
$this->register_field(__('Comment Parent Field Type', 'jc-importer'), '_id_type', [ |
| 53 |
'default' => 'id', |
| 54 |
'options' => [ |
| 55 |
['value' => 'id', 'label' => __('ID', 'jc-importer')], |
| 56 |
['value' => 'ref', 'label' => __('Comment Ref Field', 'jc-importer')], |
| 57 |
['value' => 'custom_field', 'label' => __('Custom Field', 'jc-importer')], |
| 58 |
], |
| 59 |
'type' => 'select', |
| 60 |
'tooltip' => __('Select how the post ID field should be handled', 'jc-importer') |
| 61 |
]), |
| 62 |
$this->register_field(__('Custom Field Key', 'jc-importer'), '_custom_field', [ |
| 63 |
'condition' => ['_id_type', '==', 'custom_field'], |
| 64 |
'tooltip' => __('Enter the name of the post\'s custom field.', 'jc-importer') |
| 65 |
]) |
| 66 |
|
| 67 |
]), |
| 68 |
$this->register_group(__('Comment Post', 'jc-importer'), '_post', [ |
| 69 |
$this->register_field(__('Comment Post', 'jc-importer'), 'id', [ |
| 70 |
'default' => '', |
| 71 |
'tooltip' => __('ID of the post that relates to the comment, if any.', 'jc-importer') |
| 72 |
]), |
| 73 |
$this->register_field(__('Comment Post Field Type', 'jc-importer'), '_id_type', [ |
| 74 |
'default' => 'id', |
| 75 |
'options' => [ |
| 76 |
['value' => 'id', 'label' => __('ID', 'jc-importer')], |
| 77 |
['value' => 'slug', 'label' => __('Slug', 'jc-importer')], |
| 78 |
['value' => 'name', 'label' => __('Name', 'jc-importer')], |
| 79 |
['value' => 'custom_field', 'label' => __('Custom Field', 'jc-importer')], |
| 80 |
], |
| 81 |
'type' => 'select', |
| 82 |
'tooltip' => __('Select how the post ID field should be handled', 'jc-importer') |
| 83 |
]), |
| 84 |
$this->register_field(__('Custom Field Key', 'jc-importer'), '_custom_field', [ |
| 85 |
'condition' => ['_id_type', '==', 'custom_field'], |
| 86 |
'tooltip' => __('Enter the name of the post\'s custom field.', 'jc-importer') |
| 87 |
]) |
| 88 |
]), |
| 89 |
$this->register_field(__('Comment Type', 'jc-importer'), 'comment_type', [ |
| 90 |
'default' => 'comment', |
| 91 |
'tooltip' => __('Comment type.', 'jc-importer') |
| 92 |
]), |
| 93 |
|
| 94 |
// Author |
| 95 |
$this->register_field(__('Author ID', 'jc-importer'), 'user_id', [ |
| 96 |
'tooltip' => __('Author user id.', 'jc-importer') |
| 97 |
]), |
| 98 |
$this->register_field(__('Author Name', 'jc-importer'), 'comment_author', [ |
| 99 |
'tooltip' => __('The name of the author of the comment.', 'jc-importer') |
| 100 |
]), |
| 101 |
$this->register_field(__('Author Email', 'jc-importer'), 'comment_author_email', [ |
| 102 |
'tooltip' => __('The email address of the Comment Author', 'jc-importer') |
| 103 |
]), |
| 104 |
$this->register_field(__('Author Url', 'jc-importer'), 'comment_author_url', [ |
| 105 |
'tooltip' => __('The URL address of the Comment Author.', 'jc-importer') |
| 106 |
]), |
| 107 |
$this->register_field(__('Comment Author IP', 'jc-importer'), 'comment_author_IP', [ |
| 108 |
'tooltip' => __('The IP address of the Comment Author.', 'jc-importer') |
| 109 |
]), |
| 110 |
|
| 111 |
// Meta |
| 112 |
$this->register_field(__('Ref', 'jc-importer'), '_iwp_ref_id', [ |
| 113 |
'tooltip' => __('A custom field to uniquely identify the comment.', 'jc-importer') |
| 114 |
]), |
| 115 |
$this->register_field(__('Post Type', 'jc-importer'), 'post_type', [ |
| 116 |
'options' => $post_type_options, |
| 117 |
'default' => '', |
| 118 |
'tooltip' => __('The post type the comment belongs to.', 'jc-importer') |
| 119 |
]), |
| 120 |
$this->register_field(__('Comment Approved', 'jc-importer'), 'comment_approved', [ |
| 121 |
'options' => [ |
| 122 |
['value' => '0', 'label' => __('Disapproved', 'jc-importer')], |
| 123 |
['value' => '1', 'label' => __('Approved', 'jc-importer')] |
| 124 |
], |
| 125 |
'default' => '1', |
| 126 |
'tooltip' => __('Whether the comment has been approved. 1 = Approved, 0 = Disapproved', 'jc-importer') |
| 127 |
]), |
| 128 |
$this->register_field(__('Comment Karma', 'jc-importer'), 'comment_karma', [ |
| 129 |
'default' => '0', |
| 130 |
'tooltip' => __('The karma of the comment.', 'jc-importer') |
| 131 |
]), |
| 132 |
$this->register_field(__('Comment Agent', 'jc-importer'), 'comment_agent', [ |
| 133 |
'tooltip' => __('The HTTP user agent of the Comment Author when the comment was submitted.', 'jc-importer') |
| 134 |
]), |
| 135 |
$this->register_field(__('Comment Date', 'jc-importer'), 'comment_date', [ |
| 136 |
'tooltip' => __('The date the comment was submitted.', 'jc-importer') |
| 137 |
]), |
| 138 |
]) |
| 139 |
]; |
| 140 |
} |
| 141 |
|
| 142 |
public function register_settings() |
| 143 |
{ |
| 144 |
return []; |
| 145 |
} |
| 146 |
|
| 147 |
public function register_options() |
| 148 |
{ |
| 149 |
return []; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get list of comments |
| 154 |
* |
| 155 |
* @param \ImportWP\Common\Model\ImporterModel $importer_model |
| 156 |
* |
| 157 |
* @return array |
| 158 |
*/ |
| 159 |
public function get_parent_options($importer_model) |
| 160 |
{ |
| 161 |
/** |
| 162 |
* @var \WPDB $wpdb |
| 163 |
*/ |
| 164 |
global $wpdb; |
| 165 |
|
| 166 |
$vars = []; |
| 167 |
$results = $wpdb->get_col("SELECT comment_post_ID FROM {$wpdb->comments}"); |
| 168 |
|
| 169 |
foreach ($results as $result) { |
| 170 |
$vars[] = [ |
| 171 |
'value' => $result, |
| 172 |
'label' => sprintf(__('Comment #%s', 'jc-importer'), $result) |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
return $vars; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Process data before record is importer. |
| 181 |
* |
| 182 |
* Alter data that is passed to the mapper. |
| 183 |
* |
| 184 |
* @param \ImportWP\Common\Importer\ParsedData $data |
| 185 |
* @return \ImportWP\Common\Importer\ParsedData |
| 186 |
*/ |
| 187 |
public function pre_process(\ImportWP\Common\Importer\ParsedData $data) |
| 188 |
{ |
| 189 |
$data = parent::pre_process($data); |
| 190 |
|
| 191 |
$field_map = [ |
| 192 |
'comment_ID', |
| 193 |
'comment_agent', |
| 194 |
'comment_approved', |
| 195 |
'comment_author', |
| 196 |
'comment_author_email', |
| 197 |
'comment_author_IP', |
| 198 |
'comment_author_url', |
| 199 |
'comment_content', |
| 200 |
'comment_date', |
| 201 |
'comment_date_gmt', |
| 202 |
'comment_karma', |
| 203 |
'comment_parent', |
| 204 |
'comment_post_ID', |
| 205 |
'comment_type', |
| 206 |
'user_id', |
| 207 |
'_iwp_ref_id' |
| 208 |
]; |
| 209 |
|
| 210 |
$comment_field_map = []; |
| 211 |
|
| 212 |
foreach ($field_map as $field_key) { |
| 213 |
|
| 214 |
$field_id = sprintf('comment.%s', $field_key); |
| 215 |
$value = $data->getValue($field_id, 'comment'); |
| 216 |
|
| 217 |
if ($value !== false && $this->importer->isEnabledField('comment.' . $field_key)) { |
| 218 |
$comment_field_map[$field_key] = $value; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
// Get comment post type, used for reading data |
| 223 |
$post_type = 'any'; |
| 224 |
if ($this->importer->isEnabledField('comment.post_type')) { |
| 225 |
|
| 226 |
$value = $data->getValue('comment.post_type', 'comment'); |
| 227 |
if (!empty($value)) { |
| 228 |
$post_type = $value; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
// post field |
| 233 |
if ($this->importer->isEnabledField('comment._post')) { |
| 234 |
|
| 235 |
$comment_post_ID = $data->getValue('comment._post.id'); |
| 236 |
if ($comment_post_ID !== false) { |
| 237 |
|
| 238 |
$parent_field_type = $data->getValue('comment._post._id_type'); |
| 239 |
switch ($parent_field_type) { |
| 240 |
case 'id': |
| 241 |
|
| 242 |
// ID |
| 243 |
$comment_post_ID = intval($comment_post_ID); |
| 244 |
break; |
| 245 |
case 'slug': |
| 246 |
case 'name': |
| 247 |
|
| 248 |
// name or slug |
| 249 |
$page = get_posts(array('name' => sanitize_title($comment_post_ID), 'post_type' => $post_type)); |
| 250 |
if ($page) { |
| 251 |
$comment_post_ID = intval($page[0]->ID); |
| 252 |
} |
| 253 |
|
| 254 |
break; |
| 255 |
case 'custom_field': |
| 256 |
|
| 257 |
// reference column |
| 258 |
$parent_custom_field = $data->getValue('comment._post._custom_field'); |
| 259 |
$temp_id = $this->get_post_by_cf($parent_custom_field, $comment_post_ID, $post_type); |
| 260 |
if (intval($temp_id > 0)) { |
| 261 |
$comment_post_ID = intval($temp_id); |
| 262 |
} |
| 263 |
break; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
if ($comment_post_ID > 0) { |
| 268 |
$comment_field_map['comment_post_ID'] = $comment_post_ID; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
// parent field |
| 273 |
if ($this->importer->isEnabledField('comment._parent')) { |
| 274 |
|
| 275 |
$comment_parent = $data->getValue('comment._parent.id'); |
| 276 |
if ($comment_parent !== false) { |
| 277 |
|
| 278 |
$parent_field_type = $data->getValue('comment._parent._id_type'); |
| 279 |
switch ($parent_field_type) { |
| 280 |
case 'id': |
| 281 |
|
| 282 |
// ID |
| 283 |
$comment_parent = intval($comment_parent); |
| 284 |
break; |
| 285 |
case 'ref': |
| 286 |
|
| 287 |
// reference column |
| 288 |
$temp_id = $this->get_comment_by_cf('_iwp_ref_id', $comment_parent, $post_type); |
| 289 |
if (intval($temp_id > 0)) { |
| 290 |
$comment_parent = intval($temp_id); |
| 291 |
} |
| 292 |
break; |
| 293 |
case 'custom_field': |
| 294 |
|
| 295 |
// custom field |
| 296 |
$parent_custom_field = $data->getValue('comment._parent._custom_field'); |
| 297 |
$temp_id = $this->get_comment_by_cf($parent_custom_field, $comment_parent, $post_type); |
| 298 |
if (intval($temp_id > 0)) { |
| 299 |
$comment_parent = intval($temp_id); |
| 300 |
} |
| 301 |
break; |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if ($comment_parent > 0) { |
| 306 |
$comment_field_map['comment_parent'] = $comment_parent; |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
$data->replace($comment_field_map, 'default'); |
| 311 |
return $data; |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Find existing post/page/custom by Custom field |
| 316 |
* |
| 317 |
* @param string $field Custom Field Name |
| 318 |
* @param string $value Value to check against reference |
| 319 |
* |
| 320 |
* @return bool |
| 321 |
*/ |
| 322 |
public function get_post_by_cf($field, $value, $post_type = 'any') |
| 323 |
{ |
| 324 |
|
| 325 |
$query = new \WP_Query(array( |
| 326 |
'post_type' => $post_type, |
| 327 |
'posts_per_page' => 1, |
| 328 |
'fields' => 'ids', |
| 329 |
'cache_results' => false, |
| 330 |
'update_post_meta_cache' => false, |
| 331 |
'meta_query' => array( |
| 332 |
array( |
| 333 |
'key' => $field, |
| 334 |
'value' => $value |
| 335 |
) |
| 336 |
), |
| 337 |
'post_status' => 'any' |
| 338 |
)); |
| 339 |
if ($query->have_posts()) { |
| 340 |
return $query->posts[0]; |
| 341 |
} |
| 342 |
return false; |
| 343 |
} |
| 344 |
|
| 345 |
/** |
| 346 |
* Find existing comment by Custom field |
| 347 |
* |
| 348 |
* @param string $field Custom Field Name |
| 349 |
* @param string $value Value to check against reference |
| 350 |
* |
| 351 |
* @return bool |
| 352 |
*/ |
| 353 |
public function get_comment_by_cf($field, $value, $post_type = 'any') |
| 354 |
{ |
| 355 |
$query = new \WP_Comment_Query([ |
| 356 |
'fields' => 'ids', |
| 357 |
'update_comment_meta_cache' => false, |
| 358 |
'update_comment_post_cache' => false, |
| 359 |
'no_found_rows' => true, |
| 360 |
'post_type' => $post_type, |
| 361 |
'meta_query' => array( |
| 362 |
array( |
| 363 |
'key' => $field, |
| 364 |
'value' => $value |
| 365 |
) |
| 366 |
), |
| 367 |
]); |
| 368 |
|
| 369 |
if (count($query->comments) === 1) { |
| 370 |
return $query->comments[0]; |
| 371 |
} |
| 372 |
|
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
public function get_permission_fields($importer_model) |
| 377 |
{ |
| 378 |
$permission_fields = parent::get_permission_fields($importer_model); |
| 379 |
|
| 380 |
$permission_fields['core'] = [ |
| 381 |
'comment_ID' => __('ID', 'jc-importer'), |
| 382 |
'comment_agent' => __('User Agent', 'jc-importer'), |
| 383 |
'comment_approved' => __('Approved', 'jc-importer'), |
| 384 |
'comment_author' => __('Author', 'jc-importer'), |
| 385 |
'comment_author_email' => __('Author Email', 'jc-importer'), |
| 386 |
'comment_author_IP' => __('Ip Address', 'jc-importer'), |
| 387 |
'comment_author_url' => __('Author Url', 'jc-importer'), |
| 388 |
'comment_content' => __('Content', 'jc-importer'), |
| 389 |
'comment_date' => __('Date', 'jc-importer'), |
| 390 |
'comment_date_gmt' => __('Date GMT', 'jc-importer'), |
| 391 |
'comment_karma' => __('Karma', 'jc-importer'), |
| 392 |
'comment_parent' => __('Parent', 'jc-importer'), |
| 393 |
'comment_post_ID' => __('Post ID', 'jc-importer'), |
| 394 |
'comment_type' => __('Comment Type', 'jc-importer'), |
| 395 |
'user_id' => __('User ID', 'jc-importer') |
| 396 |
]; |
| 397 |
|
| 398 |
return $permission_fields; |
| 399 |
} |
| 400 |
|
| 401 |
public function get_unique_identifier_options($importer_model, $unique_fields = []) |
| 402 |
{ |
| 403 |
$output = parent::get_unique_identifier_options($importer_model, $unique_fields); |
| 404 |
|
| 405 |
$field_map = [ |
| 406 |
'comment_ID' => 'comment.comment_ID', |
| 407 |
'comment_agent' => 'comment.comment_agent', |
| 408 |
'comment_approved' => 'comment.comment_approved', |
| 409 |
'comment_author' => 'comment.comment_author', |
| 410 |
'comment_author_email' => 'comment.comment_author_email', |
| 411 |
'comment_author_IP' => 'comment.comment_author_IP', |
| 412 |
'comment_author_url' => 'comment.comment_author_url', |
| 413 |
'comment_content' => 'comment.comment_content', |
| 414 |
'comment_date' => 'comment.comment_date', |
| 415 |
'comment_date_gmt' => 'comment.comment_date_gmt', |
| 416 |
'comment_karma' => 'comment.comment_karma', |
| 417 |
'comment_parent' => 'comment.comment_parent', |
| 418 |
'comment_post_ID' => 'comment.comment_post_ID', |
| 419 |
'comment_type' => 'comment.comment_type', |
| 420 |
'user_id' => 'comment.user_id', |
| 421 |
'_iwp_ref_id' => 'comment._iwp_ref_id', |
| 422 |
]; |
| 423 |
$optional_fields = array_keys($field_map); |
| 424 |
|
| 425 |
return array_merge( |
| 426 |
$output, |
| 427 |
$this->get_unique_identifier_options_from_map($importer_model, $unique_fields, $field_map, $optional_fields) |
| 428 |
); |
| 429 |
} |
| 430 |
} |
| 431 |
|