| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\ExporterRecord; |
| 6 |
use ImportWP\Common\Exporter\MapperInterface; |
| 7 |
|
| 8 |
class CommentMapper extends AbstractMapper implements MapperInterface |
| 9 |
{ |
| 10 |
private $post_type; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var \WP_Comment_Query |
| 14 |
*/ |
| 15 |
private $query; |
| 16 |
|
| 17 |
public function __construct($post_type) |
| 18 |
{ |
| 19 |
$this->post_type = $post_type; |
| 20 |
|
| 21 |
add_filter('iwp/exporter_record/comment', [$this, 'get_record_data'], 10, 3); |
| 22 |
} |
| 23 |
|
| 24 |
public function get_core_fields() |
| 25 |
{ |
| 26 |
return array( |
| 27 |
'comment_ID', |
| 28 |
'comment_post_ID', |
| 29 |
'comment_author', |
| 30 |
'comment_author_email', |
| 31 |
'comment_author_url', |
| 32 |
'comment_author_IP', |
| 33 |
'comment_date', |
| 34 |
'comment_date_gmt', |
| 35 |
'comment_content', |
| 36 |
'comment_karma', |
| 37 |
'comment_approved', |
| 38 |
'comment_agent', |
| 39 |
'comment_type', |
| 40 |
'comment_parent', |
| 41 |
'user_id', |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
public function get_fields() |
| 47 |
{ |
| 48 |
|
| 49 |
/** |
| 50 |
* @var \WPDB |
| 51 |
*/ |
| 52 |
global $wpdb; |
| 53 |
|
| 54 |
$fields = [ |
| 55 |
'key' => 'main', |
| 56 |
'label' => 'Comment', |
| 57 |
'loop' => true, |
| 58 |
'fields' => [], |
| 59 |
'children' => [ |
| 60 |
'custom_fields' => [ |
| 61 |
'key' => 'custom_fields', |
| 62 |
'label' => 'Custom Fields', |
| 63 |
'loop' => true, |
| 64 |
'loop_fields' => ['meta_key', 'meta_value'], |
| 65 |
'fields' => [], |
| 66 |
'children' => [] |
| 67 |
] |
| 68 |
] |
| 69 |
|
| 70 |
]; |
| 71 |
|
| 72 |
$fields['fields'] = $this->get_core_fields(); |
| 73 |
|
| 74 |
// get comment custom fields |
| 75 |
$meta_fields = $wpdb->get_col($wpdb->prepare("SELECT DISTINCT cm.meta_key FROM " . $wpdb->comments . " as c INNER JOIN " . $wpdb->commentmeta . " as cm ON c.comment_ID = cm.comment_id WHERE c.comment_post_ID IN (SELECT ID from " . $wpdb->posts . " WHERE post_type=%s)", [$this->post_type])); |
| 76 |
$custom_file_fields = apply_filters('iwp/exporter/comment/custom_file_id_fields', []); |
| 77 |
foreach ($meta_fields as $field) { |
| 78 |
$fields['children']['custom_fields']['fields'][] = $field; |
| 79 |
|
| 80 |
if (in_array($field, $custom_file_fields)) { |
| 81 |
$fields['children']['custom_fields']['fields'][] = $field . '::id'; |
| 82 |
$fields['children']['custom_fields']['fields'][] = $field . '::url'; |
| 83 |
$fields['children']['custom_fields']['fields'][] = $field . '::title'; |
| 84 |
$fields['children']['custom_fields']['fields'][] = $field . '::alt'; |
| 85 |
$fields['children']['custom_fields']['fields'][] = $field . '::caption'; |
| 86 |
$fields['children']['custom_fields']['fields'][] = $field . '::description'; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
$fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/comment/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type); |
| 91 |
|
| 92 |
return $fields; |
| 93 |
} |
| 94 |
|
| 95 |
public function have_records($exporter_id) |
| 96 |
{ |
| 97 |
$query_args = []; |
| 98 |
$query_args = apply_filters('iwp/exporter/comment_query', $query_args); |
| 99 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/comment_query', $exporter_id), $query_args); |
| 100 |
$query_args = apply_filters('iwp/exporter/comment_query/' . $this->post_type, $query_args); |
| 101 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/comment_query/%s', $exporter_id, $this->post_type), $query_args); |
| 102 |
|
| 103 |
$query_args = wp_parse_args($query_args, [ |
| 104 |
'post_type' => $this->post_type, |
| 105 |
'fields' => 'ids' |
| 106 |
]); |
| 107 |
|
| 108 |
$this->query = new \WP_Comment_Query($query_args); |
| 109 |
$this->items = $this->query->comments; |
| 110 |
|
| 111 |
return $this->found_records() > 0; |
| 112 |
} |
| 113 |
|
| 114 |
public function found_records() |
| 115 |
{ |
| 116 |
return count($this->items); |
| 117 |
} |
| 118 |
|
| 119 |
public function get_records() |
| 120 |
{ |
| 121 |
return $this->items; |
| 122 |
} |
| 123 |
|
| 124 |
public function setup($i) |
| 125 |
{ |
| 126 |
$comment = get_comment($this->items[$i], ARRAY_A); |
| 127 |
|
| 128 |
$this->record = new ExporterRecord($comment, 'comment'); |
| 129 |
$this->record = apply_filters('iwp/exporter/comment/setup_data', $this->record, $this->post_type); |
| 130 |
|
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
public function get_record_data($value, $key, $record) |
| 135 |
{ |
| 136 |
switch ($key) { |
| 137 |
case 'custom_fields': |
| 138 |
$value = get_comment_meta($record['comment_ID']); |
| 139 |
$value = $this->modify_custom_field_data($value, 'comment'); |
| 140 |
break; |
| 141 |
} |
| 142 |
return $value; |
| 143 |
} |
| 144 |
} |
| 145 |
|