PluginProbe ʕ •ᴥ•ʔ
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel / trunk
WP All Export – Drag & Drop Export to Any Custom CSV, XML & Excel vtrunk
trunk 0.9.0 0.9.1 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.2.0 1.2.1 1.2.10 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 1.4.13 1.4.14 1.4.15 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0
wp-all-export / libraries / XmlExportTaxonomy.php
wp-all-export / libraries Last commit date
VariableProductTitle 4 weeks ago .gitkeep 10 years ago WpaeInvalidPhpException.php 9 years ago WpaeInvalidStringException.php 9 years ago WpaeMethodNotFoundException.php 9 years ago WpaePhpInterpreterErrorHandler.php 4 weeks ago WpaeString.php 8 years ago WpaeTooMuchRecursionException.php 9 years ago XmlCsvExport.php 4 weeks ago XmlExportCpt.php 4 weeks ago XmlExportCustomRecord.php 4 weeks ago XmlExportEngine.php 4 weeks ago XmlExportFiltering.php 4 weeks ago XmlExportMediaGallery.php 4 weeks ago XmlExportTaxonomy.php 4 weeks ago
XmlExportTaxonomy.php
499 lines
1 <?php
2
3 // phpcs:ignoreFile WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound,WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound,WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- legitimate plugin prefixes (pmxe/PMXE/wpae/Wpae/wp_all_export/wpallexport/XmlExport/CdataStrategy/VariableProductTitle/Soflyy/GF_Export); Plugin Check does not honor phpcs.xml prefix declaration
4 defined( 'ABSPATH' ) || exit;
5
6
7 if ( ! class_exists('XmlExportTaxonomy') )
8 {
9 final class XmlExportTaxonomy
10 {
11 private $init_fields = array(
12 array(
13 'label' => 'term_id',
14 'name' => 'Term ID',
15 'type' => 'term_id'
16 ),
17 array(
18 'label' => 'term_name',
19 'name' => 'Term Name',
20 'type' => 'term_name'
21 ),
22 array(
23 'label' => 'term_slug',
24 'name' => 'Term Slug',
25 'type' => 'term_slug'
26 )
27 );
28
29 private $default_fields = array(
30 array(
31 'label' => 'term_id',
32 'name' => 'Term ID',
33 'type' => 'term_id'
34 ),
35 array(
36 'label' => 'term_name',
37 'name' => 'Term Name',
38 'type' => 'term_name'
39 ),
40 array(
41 'label' => 'term_slug',
42 'name' => 'Term Slug',
43 'type' => 'term_slug'
44 ),
45 array(
46 'label' => 'term_description',
47 'name' => 'Description',
48 'type' => 'term_description'
49 ),
50 array(
51 'label' => 'term_parent_id',
52 'name' => 'Parent ID',
53 'type' => 'term_parent_id'
54 ),
55 array(
56 'label' => 'term_parent_name',
57 'name' => 'Parent Name',
58 'type' => 'term_parent_name'
59 ),
60 array(
61 'label' => 'term_parent_slug',
62 'name' => 'Parent Slug',
63 'type' => 'term_parent_slug'
64 ),
65 array(
66 'label' => 'term_posts_count',
67 'name' => 'Count',
68 'type' => 'term_posts_count'
69 ),
70 );
71
72 private $advanced_fields = array(
73
74 );
75
76 public static $is_active = true;
77
78 public function __construct()
79 {
80
81 if ( XmlExportEngine::$exportOptions['export_type'] == 'specific' and ! in_array('taxonomies', XmlExportEngine::$post_types) or XmlExportEngine::$exportOptions['export_type'] == 'advanced'){
82 self::$is_active = false;
83 return;
84 }
85
86 add_filter("wp_all_export_available_sections", array( &$this, "filter_available_sections" ), 10, 1);
87 add_filter("wp_all_export_init_fields", array( &$this, "filter_init_fields"), 10, 1);
88 add_filter("wp_all_export_default_fields", array( &$this, "filter_default_fields"), 10, 1);
89 add_filter("wp_all_export_other_fields", array( &$this, "filter_other_fields"), 10, 1);
90 }
91
92 // [FILTERS]
93
94 /**
95 *
96 * Filter Init Fields
97 *
98 */
99 public function filter_init_fields($init_fields){
100 return $this->init_fields;
101 }
102
103 /**
104 *
105 * Filter Default Fields
106 *
107 */
108 public function filter_default_fields($default_fields){
109 return $this->default_fields;
110 }
111
112 /**
113 *
114 * Filter Other Fields
115 *
116 */
117 public function filter_other_fields($other_fields){
118 return $this->advanced_fields;
119 }
120
121 /**
122 *
123 * Filter Sections in Available Data
124 *
125 */
126 public function filter_available_sections($sections){
127
128 unset($sections['media']['additional']['attachments']);
129 unset($sections['cats']);
130 unset($sections['other']);
131
132 $sections['cf']['title'] = __("Term Meta", "wp-all-export");
133
134 return $sections;
135 }
136
137 // [\FILTERS]
138
139 public function init( & $existing_meta_keys = array() )
140 {
141 if ( ! self::$is_active ) return;
142
143 if ( ! empty(XmlExportEngine::$exportQuery)){
144 $terms = XmlExportEngine::$exportQuery->get_terms();
145 }
146
147 if ( ! empty( $terms ) ) {
148 foreach ( $terms as $term ) {
149 $term_meta = get_term_meta($term->term_id, '');
150 if ( ! empty($term_meta)){
151 foreach ($term_meta as $record_meta_key => $record_meta_value) {
152 if ( ! in_array($record_meta_key, $existing_meta_keys) ){
153 $to_add = true;
154 foreach ($this->default_fields as $default_value) {
155 if ( $record_meta_key == $default_value['name'] || $record_meta_key == $default_value['type'] ){
156 $to_add = false;
157 break;
158 }
159 }
160 if ( $to_add ){
161 foreach ($this->advanced_fields as $advanced_value) {
162 if ( $record_meta_key == $advanced_value['name'] || $record_meta_key == $advanced_value['type']){
163 $to_add = false;
164 break;
165 }
166 }
167 }
168 if ( $to_add ) $existing_meta_keys[] = $record_meta_key;
169 }
170 }
171 }
172 }
173 }
174 }
175
176 public static function prepare_data( $term, $xmlWriter, &$acfs, $implode_delimiter, $preview )
177 {
178 $article = array();
179
180 // associate exported comment with import
181 if ( wp_all_export_is_compatible() and XmlExportEngine::$exportOptions['is_generate_import'] and XmlExportEngine::$exportOptions['import_id'])
182 {
183 $postRecord = new PMXI_Post_Record();
184 $postRecord->clear();
185 $postRecord->getBy(array(
186 'post_id' => $term->term_id,
187 'import_id' => XmlExportEngine::$exportOptions['import_id'],
188 ));
189
190 if ($postRecord->isEmpty()){
191 $postRecord->set(array(
192 'post_id' => $term->term_id,
193 'import_id' => XmlExportEngine::$exportOptions['import_id'],
194 'unique_key' => $term->term_id
195 ))->save();
196 }
197 unset($postRecord);
198 }
199
200 $is_xml_export = false;
201
202 if ( ! empty($xmlWriter) and XmlExportEngine::$exportOptions['export_to'] == 'xml' and ! in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants')) ){
203 $is_xml_export = true;
204 }
205
206 foreach (XmlExportEngine::$exportOptions['ids'] as $ID => $value)
207 {
208 $fieldName = apply_filters('wp_all_export_field_name', wp_all_export_parse_field_name(XmlExportEngine::$exportOptions['cc_name'][$ID]), XmlExportEngine::$exportID);
209 $fieldValue = XmlExportEngine::$exportOptions['cc_value'][$ID];
210 $fieldLabel = XmlExportEngine::$exportOptions['cc_label'][$ID];
211 $fieldSql = XmlExportEngine::$exportOptions['cc_sql'][$ID];
212 $fieldPhp = XmlExportEngine::$exportOptions['cc_php'][$ID];
213 $fieldCode = XmlExportEngine::$exportOptions['cc_code'][$ID];
214 $fieldType = XmlExportEngine::$exportOptions['cc_type'][$ID];
215 $fieldOptions = XmlExportEngine::$exportOptions['cc_options'][$ID];
216
217 if ( empty($fieldName) or empty($fieldType) or ! is_numeric($ID)) continue;
218
219 $element_name = ( ! empty($fieldName) ) ? $fieldName : 'untitled_' . $ID;
220
221 $element_name_ns = '';
222
223 if ( $is_xml_export )
224 {
225 $element_name = ( ! empty($fieldName) ) ? preg_replace('/[^a-z0-9_:-]/i', '', $fieldName) : 'untitled_' . $ID;
226
227 if (strpos($element_name, ":") !== false)
228 {
229 $element_name_parts = explode(":", $element_name);
230 $element_name_ns = (empty($element_name_parts[0])) ? '' : $element_name_parts[0];
231 $element_name = (empty($element_name_parts[1])) ? 'untitled_' . $ID : preg_replace('/[^a-z0-9_-]/i', '', $element_name_parts[1]);
232 }
233 }
234
235 $fieldSnipped = ( ! empty($fieldPhp ) and ! empty($fieldCode)) ? $fieldCode : false;
236
237 $addons = XmlExportEngine::get_addons();
238 $addonFieldOptions = maybe_unserialize($fieldOptions);
239
240 if (in_array($fieldType, $addons)) {
241 $article = apply_filters(
242 "pmxe_{$fieldType}_addon_export_field",
243 $article,
244 $addonFieldOptions,
245 XmlExportEngine::$exportOptions,
246 $ID,
247 $term,
248 $term->term_id,
249 $xmlWriter,
250 $element_name,
251 $element_name_ns,
252 $fieldSnipped,
253 $preview
254 );
255 }
256
257 switch ($fieldType)
258 {
259 case 'term_id':
260 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_id', pmxe_filter($term->term_id, $fieldSnipped), $term->term_id) );
261 break;
262 case 'term_name':
263 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_name', pmxe_filter($term->name, $fieldSnipped), $term->term_id) );
264 break;
265 case 'term_slug':
266 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_slug', pmxe_filter($term->slug, $fieldSnipped), $term->term_id) );
267 break;
268 case 'term_description':
269 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_description', pmxe_filter($term->description, $fieldSnipped), $term->term_id) );
270 break;
271 case 'term_parent_id':
272 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_parent', pmxe_filter($term->parent, $fieldSnipped), $term->term_id) );
273 break;
274 case 'term_parent_name':
275 $term_parent_name = '';
276 if ($term->parent){
277 $parent_term = get_term($term->parent, $term->taxonomy);
278 if ($parent_term){
279 $term_parent_name = $parent_term->name;
280 }
281 }
282 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_parent', pmxe_filter($term_parent_name, $fieldSnipped), $term->term_id) );
283 break;
284 case 'term_parent_slug':
285 $term_parent_slug = '';
286 if ($term->parent){
287 $parent_term = get_term($term->parent, $term->taxonomy);
288 if ($parent_term){
289 $term_parent_slug = $parent_term->slug;
290 }
291 }
292 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_parent', pmxe_filter($term_parent_slug, $fieldSnipped), $term->term_id) );
293 break;
294 case 'term_posts_count':
295 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_term_count', pmxe_filter($term->count, $fieldSnipped), $term->term_id) );
296 break;
297 // Media Images
298 case 'media':
299 case 'image_id':
300 case 'image_url':
301 case 'image_filename':
302 case 'image_path':
303 case 'image_title':
304 case 'image_caption':
305 case 'image_description':
306 case 'image_alt':
307
308 $field_options = json_decode($fieldOptions, true);
309
310 XmlExportMediaGallery::getInstance($term->term_id);
311
312 $images_data = XmlExportMediaGallery::get_images($fieldType, $field_options);
313
314 $images_separator = empty($field_options['image_separator']) ? $implode_delimiter : $field_options['image_separator'];
315
316 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_' . $fieldType, pmxe_filter( implode($images_separator, $images_data), $fieldSnipped), $term->term_id) );
317
318 break;
319 case 'cf':
320 if ( ! empty($fieldValue) ){
321 $cur_meta_values = get_term_meta($term->term_id, $fieldValue);
322 if (!empty($cur_meta_values) and is_array($cur_meta_values)){
323 $val = "";
324 foreach ($cur_meta_values as $key => $cur_meta_value) {
325 if (empty($val)){
326 $val = apply_filters('pmxe_custom_field', pmxe_filter(maybe_serialize($cur_meta_value), $fieldSnipped), $fieldValue, $term->term_id);
327 }
328 else{
329 $val = apply_filters('pmxe_custom_field', pmxe_filter($val . $implode_delimiter . maybe_serialize($cur_meta_value), $fieldSnipped), $fieldValue, $term->term_id);
330 }
331 }
332 wp_all_export_write_article( $article, $element_name, $val );
333 }
334
335 if (empty($cur_meta_values)){
336 if (empty($article[$element_name])){
337 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_custom_field', pmxe_filter('', $fieldSnipped), $fieldValue, $term->term_id) );
338 }
339 }
340 }
341 break;
342 case 'acf':
343
344 if ( ! empty($fieldLabel) and class_exists( 'acf' ) )
345 {
346 global $acf;
347
348 $field_options = unserialize($fieldOptions);
349
350 if ( ! $is_xml_export )
351 {
352 switch ($field_options['type']) {
353 case 'textarea':
354 case 'oembed':
355 case 'wysiwyg':
356 case 'wp_wysiwyg':
357 case 'date_time_picker':
358 case 'date_picker':
359
360 $field_value = get_field($fieldLabel, $term->taxonomy . "_" . $term->term_id, false);
361
362 break;
363
364 default:
365
366 $field_value = get_field($fieldLabel, $term->taxonomy . "_" . $term->term_id);
367
368 break;
369 }
370 }
371 else
372 {
373 $field_value = get_field($fieldLabel, $term->taxonomy . "_" . $term->term_id);
374 }
375
376 XmlExportACF::export_acf_field(
377 $field_value,
378 XmlExportEngine::$exportOptions,
379 $ID,
380 $term->taxonomy . "_" . $term->term_id,
381 $article,
382 $xmlWriter,
383 $acfs,
384 $element_name,
385 $element_name_ns,
386 $fieldSnipped,
387 $field_options['group_id'],
388 $preview
389 );
390
391 }
392
393 break;
394 case 'sql':
395
396 if ( ! empty($fieldSql) )
397 {
398 global $wpdb;
399 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared,WordPress.DB.PreparedSQLPlaceholders.UnsupportedPlaceholder,WordPress.DB.PreparedSQLPlaceholders.UnsupportedIdentifierPlaceholder,WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,PluginCheck.Security.DirectDB.UnescapedDBParameter -- $fieldSql is admin-saved custom SQL from export template options (cc_sql); editing requires the manage_options capability; %%ID%% token is replaced with %d placeholder before being bound through $wpdb->prepare() with $term->term_id
400 $val = $wpdb->get_var( $wpdb->prepare( stripcslashes(str_replace("%%ID%%", "%d", $fieldSql)), $term->term_id ));
401 if ( ! empty($fieldPhp) and !empty($fieldCode) )
402 {
403 // if shortcode defined
404 if (strpos($fieldCode, '[') === 0)
405 {
406 $val = do_shortcode(str_replace("%%VALUE%%", $val, $fieldCode));
407 }
408 else
409 {
410 // phpcs:ignore Generic.PHP.ForbiddenFunctions.Found -- intentional: executes saved WP_Query argument string
411 $val = eval('return ' . stripcslashes(str_replace("%%VALUE%%", $val, $fieldCode)) . ';');
412 }
413 }
414 wp_all_export_write_article( $article, $element_name, apply_filters('pmxe_sql_field', $val, $element_name, $term->term_id) );
415 }
416 break;
417 default:
418 # code...
419 break;
420 }
421
422 if ( $is_xml_export and isset($article[$element_name]) )
423 {
424 $element_name_in_file = XmlCsvExport::_get_valid_header_name( $element_name );
425
426 $xmlWriter = apply_filters('wp_all_export_add_before_element', $xmlWriter, $element_name_in_file, XmlExportEngine::$exportID, $term->term_id);
427
428 $xmlWriter->beginElement($element_name_ns, $element_name_in_file, null);
429 $xmlWriter->writeData($article[$element_name], $element_name_in_file);
430 $xmlWriter->closeElement();
431
432 $xmlWriter = apply_filters('wp_all_export_add_after_element', $xmlWriter, $element_name_in_file, XmlExportEngine::$exportID, $term->term_id);
433 }
434 }
435 return $article;
436 }
437
438 public static function prepare_import_template( $exportOptions, &$templateOptions, $element_name, $ID)
439 {
440
441 $options = $exportOptions;
442
443 $element_type = $options['cc_type'][$ID];
444
445 $is_xml_template = $options['export_to'] == 'xml';
446
447 $implode_delimiter = XmlExportEngine::$implode;
448
449 switch ($element_type)
450 {
451 // Export Taxonomy Terms
452 case 'term_id':
453 $templateOptions['unique_key'] = '{'. $element_name .'[1]}';
454 $templateOptions['tmp_unique_key'] = '{'. $element_name .'[1]}';
455 break;
456 case 'term_name':
457 $templateOptions['title'] = '{'. $element_name .'[1]}';
458 $templateOptions['is_update_title'] = 1;
459 break;
460 case 'term_slug':
461 $templateOptions['taxonomy_slug'] = 'xpath';
462 $templateOptions['taxonomy_slug_xpath'] = '{'. $element_name .'[1]}';
463 $templateOptions['is_update_slug'] = 1;
464 break;
465 case 'term_description':
466 $templateOptions['content'] = '{'. $element_name .'[1]}';
467 $templateOptions['is_update_content'] = 1;
468 break;
469 case 'term_parent_slug':
470 $templateOptions['taxonomy_parent'] = '{'. $element_name .'[1]}';
471 $templateOptions['is_update_parent'] = 1;
472 break;
473
474 }
475 }
476
477 /**
478 * __get function.
479 *
480 * @access public
481 * @param mixed $key
482 * @return mixed
483 */
484 public function __get( $key ) {
485 return $this->get( $key );
486 }
487
488 /**
489 * Get a session variable
490 *
491 * @param string $key
492 * @param mixed $default used if the session variable isn't set
493 * @return mixed value of session variable
494 */
495 public function get( $key, $default = null ) {
496 return isset( $this->{$key} ) ? $this->{$key} : $default;
497 }
498 }
499 }