PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.0
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Importer / Template / AttachmentTemplate.php

AttachmentTemplate.php in Import WP – CSV & XML Import Export for WordPress 2.15.0, at class/Common/Importer/Template/AttachmentTemplate.php

241 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Importer\Template;
4
5 use ImportWP\Common\Importer\ParsedData;
6 use ImportWP\Common\Importer\TemplateInterface;
7 use ImportWP\EventHandler;
8
9 class AttachmentTemplate extends PostTemplate implements TemplateInterface
10 {
11 protected $name = 'Attachment';
12 protected $mapper = 'attachment';
13
14 protected $field_map = [
15 'ID' => 'post.ID',
16 'post_name' => 'post.post_name',
17 'post_title' => 'post.post_title',
18 'post_content' => 'post.post_content',
19 'post_excerpt' => 'post.post_excerpt',
20 'post_status' => 'post.post_status',
21 'menu_order' => 'post.menu_order',
22 'post_password' => 'post.post_password',
23 'post_date' => 'post.post_date',
24 'comment_status' => 'post.comment_status',
25 'ping_status' => 'post.ping_status',
26 'post_parent' => 'post._parent.parent',
27 '_wp_attachment_image_alt' => 'post._wp_attachment_image_alt',
28 ];
29
30 protected $optional_fields = [];
31
32 public function __construct(EventHandler $event_handler)
33 {
34 parent::__construct($event_handler);
35
36 $this->groups[] = 'post';
37 $this->default_template_options['unique_field'] = ['ID', 'post_name', 'src'];
38 $this->default_template_options['post_type'] = 'attachment';
39
40 $this->optional_fields[] = '_wp_attachment_image_alt';
41 }
42
43 public function register()
44 {
45 return [
46 $this->register_group(__('Attachment', 'jc-importer'), 'post', [
47 $this->register_field(__('ID', 'jc-importer'), 'ID', [
48 'tooltip' => __('ID is only used to reference existing records', 'jc-importer')
49 ]),
50 $this->register_field(__('Title', 'jc-importer'), 'post_title', [
51 'tooltip' => __('Title of the post.', 'jc-importer')
52 ]),
53 $this->register_field(__('Description', 'jc-importer'), 'post_content', [
54 'tooltip' => __('Main WYSIWYG editor content of the post.', 'jc-importer')
55 ]),
56 $this->register_field(__('Caption', 'jc-importer'), 'post_excerpt', [
57 'tooltip' => __('A custom short extract for the post.', 'jc-importer')
58 ]),
59 $this->register_field(__('Alt Text', 'jc-importer'), '_wp_attachment_image_alt', [
60 'tooltip' => __('The slug is the user friendly and URL valid name of the post.', 'jc-importer')
61 ]),
62 $this->register_field(__('Slug', 'jc-importer'), 'post_name', [
63 'tooltip' => __('The slug is the user friendly and URL valid name of the post.', 'jc-importer')
64 ]),
65 $this->register_attachment_fields(__('File', 'jc-importer'), 'file', __('Media File', 'jc-importer'), [], ['disabled_fields' => ['_meta', '_featured']]),
66 $this->register_field(__('Date', 'jc-importer'), 'post_date', [
67 'tooltip' => __('The date of the post , enter in the format "YYYY-MM-DD HH:ii:ss"', 'jc-importer')
68 ]),
69 $this->register_group(__('Parent Settings', 'jc-importer'), '_parent', [
70 $this->register_field(__('Parent', 'jc-importer'), 'parent', [
71 'default' => '',
72 'options' => 'callback',
73 'tooltip' => __('Set this for the post it belongs to', 'jc-importer')
74 ]),
75 $this->register_field(__('Parent Field Type', 'jc-importer'), '_parent_type', [
76 'default' => 'id',
77 'options' => [
78 ['value' => 'id', 'label' => __('ID', 'jc-importer')],
79 ['value' => 'slug', 'label' => __('Slug', 'jc-importer')],
80 ['value' => 'name', 'label' => __('Name', 'jc-importer')],
81 ['value' => 'column', 'label' => __('Reference Column', 'jc-importer')],
82 ],
83 'type' => 'select',
84 'tooltip' => __('Select how the parent field should be handled', 'jc-importer')
85 ]),
86 $this->register_field(__('Parent Reference Column', 'jc-importer'), '_parent_ref', [
87 'condition' => ['_parent_type', '==', 'column'],
88 'tooltip' => __('Select the column/node that the parent field is referencing', 'jc-importer')
89 ])
90 ]),
91 $this->register_field(__('Order', 'jc-importer'), 'menu_order', [
92 'tooltip' => __('The order the post should be displayed in', 'jc-importer')
93 ]),
94 $this->register_group(__('Author Settings', 'jc-importer'), '_author', [
95 $this->register_field(__('Author', 'jc-importer'), 'post_author', [
96 'tooltip' => __('The user of who added this post', 'jc-importer')
97 ]),
98 $this->register_field(__('Author Field Type', 'jc-importer'), '_author_type', [
99 'default' => 'id',
100 'options' => [
101 ['value' => 'id', 'label' => __('ID', 'jc-importer')],
102 ['value' => 'login', 'label' => __('Login', 'jc-importer')],
103 ['value' => 'email', 'label' => __('Email', 'jc-importer')],
104 ],
105 'tooltip' => __('Select how the author field should be handled', 'jc-importer')
106 ])
107 ]),
108 $this->register_field(__('Password', 'jc-importer'), 'post_password', [
109 'tooltip' => __('The password to access the post', 'jc-importer')
110 ]),
111 $this->register_field(__('Allow Comments', 'jc-importer'), 'comment_status', [
112 'options' => [
113 ['value' => '0', 'label' => __('Disabled', 'jc-importer')],
114 ['value' => '1', 'label' => __('Enabled', 'jc-importer')]
115 ],
116 'default' => '0',
117 'tooltip' => __('Whether the post can accept comments', 'jc-importer')
118 ]),
119 $this->register_field(__('Allow Pingbacks', 'jc-importer'), 'ping_status', [
120 'options' => [
121 ['value' => 'closed', 'label' => __('Closed', 'jc-importer')],
122 ['value' => 'open', 'label' => __('Open', 'jc-importer')]
123 ],
124 'default' => 'closed',
125 'tooltip' => __('Whether the post can accept pings', 'jc-importer')
126 ])
127 ]),
128 $this->register_taxonomy_fields()
129 ];
130 }
131
132 public function register_settings()
133 {
134 return [];
135 }
136
137 public function register_options()
138 {
139 return [];
140 }
141
142 public function pre_process(ParsedData $data)
143 {
144 $data = parent::pre_process($data);
145
146 $field_map = $data->getData();
147
148 $field_id = sprintf('post.%s', '_wp_attachment_image_alt');
149 $value = $data->getValue($field_id, 'post');
150 if ($value !== false && $this->importer->isEnabledField($field_id)) {
151 $field_map['_wp_attachment_image_alt'] = $value;
152 }
153
154 $file_location = $data->getValue('post.file.location');
155 if ($file_location !== false && $this->importer->isEnabledField('post.file')) {
156
157 // Added so it can be used as a unique identifier
158 // But not in default so it isnt automatically inserted.
159 $data->add(['src' => $file_location], 'post');
160 }
161
162 $data->replace($field_map, 'default');
163
164 return $data;
165 }
166
167 /**
168 * Convert fields/headings to data map
169 *
170 * @param mixed $fields
171 * @param ImporterModel $importer
172 * @return array
173 */
174 public function generate_field_map($fields, $importer)
175 {
176 $result = parent::generate_field_map($fields, $importer);
177 $map = $result['map'];
178 $enabled = $result['enabled'];
179
180 foreach ($fields as $index => $field) {
181
182 if ($field === 'custom_fields._wp_attachment_image_alt') {
183
184 $field_key = $this->field_map[$field];
185 $map[$field_key] = sprintf('{%s}', $index);
186 $enabled[] = $field_key;
187
188 add_filter('iwp/importer/generate_field_map/custom_fields', function ($custom_fields) {
189
190 if (isset($custom_fields['_wp_attachment_image_alt'])) {
191 unset($custom_fields['_wp_attachment_image_alt']);
192 }
193
194 return $custom_fields;
195 });
196 } elseif ($field === 'url') {
197
198
199 $map['post.file.location'] = sprintf('{%s}', $index);
200 $enabled[] = 'post.file';
201 }
202 }
203
204 return [
205 'map' => $map,
206 'enabled' => $enabled
207 ];
208 }
209
210 public function get_permission_fields($importer_model)
211 {
212 $permission_fields = parent::get_permission_fields($importer_model);
213
214 $permission_fields['core']['_wp_attachment_image_alt'] = __('Alt Text', 'jc-importer');
215 $permission_fields['core']['file'] = __('Attachment File', 'jc-importer');
216
217 return $permission_fields;
218 }
219
220 public function get_unique_identifier_options($importer_model, $unique_fields = [])
221 {
222 $output = parent::get_unique_identifier_options($importer_model, $unique_fields);
223
224 $mapped_data = $importer_model->getMap();
225
226 if (isset($mapped_data['post.file.location']) && !empty($mapped_data['post.file.location']) && $importer_model->isEnabledField('post.file')) {
227 $output['src'] = [
228 'value' => 'src',
229 'label' => 'src',
230 'uid' => true,
231 'active' => true,
232 ];
233 }
234
235 return array_merge(
236 $output,
237 $this->get_unique_identifier_options_from_map($importer_model, $unique_fields, $this->field_map, $this->optional_fields)
238 );
239 }
240 }
241