| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ImportWP\Common\Exporter\Mapper; |
| 4 | 4 | |
| 5 | +use ImportWP\Common\Exporter\ExporterRecord; | |
| 5 | 6 | use ImportWP\Common\Exporter\MapperInterface; |
| 6 | 7 | |
| 7 | 8 | class CommentMapper extends AbstractMapper implements MapperInterface |
| 8 | 9 | { |
| @@ -15,11 +16,13 @@ | ||
| 15 | 16 | |
| 16 | 17 | public function __construct($post_type) |
| 17 | 18 | { |
| 18 | 19 | $this->post_type = $post_type; |
| 20 | + | |
| 21 | + add_filter('iwp/exporter_record/comment', [$this, 'get_record_data'], 10, 3); | |
| 19 | 22 | } |
| 20 | 23 | |
| 21 | - private function get_core_fields() | |
| 24 | + public function get_core_fields() | |
| 22 | 25 | { |
| 23 | 26 | return array( |
| 24 | 27 | 'comment_ID', |
| 25 | 28 | 'comment_post_ID', |
| @@ -49,15 +52,15 @@ | ||
| 49 | 52 | global $wpdb; |
| 50 | 53 | |
| 51 | 54 | $fields = [ |
| 52 | 55 | 'key' => 'main', |
| 53 | - 'label' => 'Comment', | |
| 56 | + 'label' => __('Comment', 'jc-importer'), | |
| 54 | 57 | 'loop' => true, |
| 55 | 58 | 'fields' => [], |
| 56 | 59 | 'children' => [ |
| 57 | 60 | 'custom_fields' => [ |
| 58 | 61 | 'key' => 'custom_fields', |
| 59 | - 'label' => 'Custom Fields', | |
| 62 | + 'label' => __('Custom Fields', 'jc-importer'), | |
| 60 | 63 | 'loop' => true, |
| 61 | 64 | 'loop_fields' => ['meta_key', 'meta_value'], |
| 62 | 65 | 'fields' => [], |
| 63 | 66 | 'children' => [] |
| @@ -69,35 +72,73 @@ | ||
| 69 | 72 | $fields['fields'] = $this->get_core_fields(); |
| 70 | 73 | |
| 71 | 74 | // get comment custom fields |
| 72 | 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', []); | |
| 73 | 77 | foreach ($meta_fields as $field) { |
| 74 | 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 | + } | |
| 75 | 88 | } |
| 76 | 89 | |
| 77 | 90 | $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/comment/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type); |
| 78 | 91 | |
| 79 | - return $fields; | |
| 92 | + return $this->parse_fields($fields); | |
| 80 | 93 | } |
| 81 | 94 | |
| 82 | - public function have_records() | |
| 95 | + public function have_records($exporter_id) | |
| 83 | 96 | { |
| 84 | - $this->query = new \WP_Comment_Query(array( | |
| 85 | - 'post_type' => $this->post_type | |
| 86 | - )); | |
| 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); | |
| 87 | 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 | + | |
| 88 | 111 | return $this->found_records() > 0; |
| 89 | 112 | } |
| 90 | 113 | |
| 91 | 114 | public function found_records() |
| 92 | 115 | { |
| 93 | - return count($this->query->comments); | |
| 116 | + return count($this->items); | |
| 94 | 117 | } |
| 95 | 118 | |
| 119 | + public function get_records() | |
| 120 | + { | |
| 121 | + return $this->items; | |
| 122 | + } | |
| 123 | + | |
| 96 | 124 | public function setup($i) |
| 97 | 125 | { |
| 98 | - $this->record = (array)$this->query->comments[$i]; | |
| 99 | - $this->record['custom_fields'] = get_comment_meta($this->record['comment_ID']); | |
| 126 | + $comment = get_comment($this->items[$i], ARRAY_A); | |
| 100 | 127 | |
| 128 | + $this->record = new ExporterRecord($comment, 'comment'); | |
| 129 | + $this->record = apply_filters('iwp/exporter/comment/setup_data', $this->record, $this->post_type); | |
| 130 | + | |
| 101 | 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; | |
| 102 | 143 | } |
| 103 | 144 | } |