base.php
3 weeks ago
exporter.php
3 weeks ago
helpers.php
3 weeks ago
resolver.php
3 weeks ago
updater.php
3 weeks ago
view.php
3 weeks ago
exporter.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Wpae\AddonAPI; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) exit; |
| 6 | |
| 7 | class PMXE_Addon_Exporter { |
| 8 | public $appended_articles = []; |
| 9 | public PMXE_Addon_Base $addon; |
| 10 | |
| 11 | public function __construct( PMXE_Addon_Base $addon ) { |
| 12 | $this->addon = $addon; |
| 13 | } |
| 14 | |
| 15 | public static function isCustomXmlExport() { |
| 16 | $options = \XmlExportEngine::$exportOptions; |
| 17 | |
| 18 | return $options['export_to'] == 'xml' && in_array( $options['xml_template_type'], [ 'custom' ] ); |
| 19 | } |
| 20 | |
| 21 | public static function isXmlExport( $xmlWriter ) { |
| 22 | $options = \XmlExportEngine::$exportOptions; |
| 23 | |
| 24 | return ! empty( $xmlWriter ) && $options['export_to'] == 'xml' && |
| 25 | ! in_array( $options['xml_template_type'], [ 'custom', 'XmlGoogleMerchants' ] ); |
| 26 | } |
| 27 | |
| 28 | public function getFieldSettings( $exportOptions, $id ) { |
| 29 | if ( empty( $exportOptions['cc_settings'][ $id ] ) ) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | $data = json_decode( $exportOptions['cc_settings'][ $id ], true ); |
| 34 | if ( $data ) { |
| 35 | return $data; |
| 36 | } |
| 37 | |
| 38 | return $exportOptions['cc_settings'][ $id ]; |
| 39 | } |
| 40 | |
| 41 | /* |
| 42 | * Called by the Engine to export a specific field |
| 43 | */ |
| 44 | public function run( |
| 45 | $article, |
| 46 | $fieldData, |
| 47 | $exportOptions, |
| 48 | $ID, |
| 49 | $entry, |
| 50 | $entryID, |
| 51 | $xmlWriter, |
| 52 | $elementName, |
| 53 | $elementNameNs, |
| 54 | $phpFunction, |
| 55 | $preview |
| 56 | ) { |
| 57 | $settings = $this->getFieldSettings( $exportOptions, $ID ); |
| 58 | $resolver = new PMXE_Addon_Resolver( $this->addon, $entry, $entryID ); |
| 59 | |
| 60 | $field = PMXE_Addon_Field::from( |
| 61 | $fieldData, |
| 62 | $resolver, |
| 63 | $settings, |
| 64 | $elementName, |
| 65 | $elementNameNs, |
| 66 | $phpFunction |
| 67 | ); |
| 68 | |
| 69 | // Change the value of an field inside the article |
| 70 | $article = $field->modifyArticle( $article, $xmlWriter, $preview ); |
| 71 | |
| 72 | // Add new articles to the export file (usually for repeater fields) |
| 73 | if ( isset($exportOptions['export_to']) && $exportOptions['export_to'] == 'csv' ) { |
| 74 | $new_articles = $field->appendNewArticles( $article ); |
| 75 | |
| 76 | if ( ! empty( $new_articles ) ) { |
| 77 | $this->appended_articles = array_merge( $this->appended_articles, $new_articles ); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | return $article; |
| 82 | } |
| 83 | |
| 84 | // Append the new articles to the export file |
| 85 | public function filterCsvRows( $articles, $options, $exportId ) { |
| 86 | if ( ! empty( $this->appended_articles ) && $options['export_to'] == 'csv' ) { |
| 87 | $base_article = $articles[ count( $articles ) - 1 ]; |
| 88 | |
| 89 | foreach ( $this->appended_articles as $article ) { |
| 90 | if ( $article['settings']['repeater_field_fill_empty_columns'] ) { |
| 91 | foreach ( $article['content'] as $key => $value ) { |
| 92 | unset( $base_article[ $key ] ); |
| 93 | } |
| 94 | |
| 95 | $articles[] = @array_merge( $base_article, $article['content'] ); |
| 96 | } else { |
| 97 | $articles[] = $article['content']; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | $this->appended_articles = []; |
| 102 | } |
| 103 | |
| 104 | return $articles; |
| 105 | } |
| 106 | |
| 107 | public function getHeaders( |
| 108 | $headers, |
| 109 | $fieldData, |
| 110 | $exportOptions, |
| 111 | $entryID, |
| 112 | $elementName |
| 113 | ) { |
| 114 | $settings = $this->getFieldSettings( $exportOptions, $entryID ); |
| 115 | $resolver = new PMXE_Addon_Resolver( $this->addon, null, null ); |
| 116 | |
| 117 | $field = PMXE_Addon_Field::from( |
| 118 | $fieldData, |
| 119 | $resolver, |
| 120 | $settings, |
| 121 | $elementName |
| 122 | ); |
| 123 | |
| 124 | $field_headers = $field->getHeaders(); |
| 125 | $field_headers = is_array( $field_headers ) ? $field_headers : [ $field_headers ]; |
| 126 | |
| 127 | // Append the field's headers if present |
| 128 | if ( ! empty( $field_headers ) ) { |
| 129 | foreach ( $field_headers as $header ) { |
| 130 | // Remove the header if it starts with a dash |
| 131 | if ( strpos( $header, '-' ) === 0 && in_array( substr( $header, 1 ), $headers ) ) { |
| 132 | $header_index = array_search( substr( $header, 1 ), $headers ); |
| 133 | if ( $header_index !== false ) { |
| 134 | unset( $headers[ $header_index ] ); |
| 135 | } |
| 136 | } else if ( ! in_array( $header, $headers ) ) { |
| 137 | $headers[] = $header; |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return $headers; |
| 143 | } |
| 144 | } |
| 145 |