VariableProductTitle
7 years ago
.gitkeep
7 years ago
WpaeInvalidPhpException.php
7 years ago
WpaeInvalidStringException.php
7 years ago
WpaeMethodNotFoundException.php
7 years ago
WpaePhpInterpreterErrorHandler.php
7 years ago
WpaeString.php
7 years ago
WpaeTooMuchRecursionException.php
7 years ago
WpaeXmlProcessor.php
7 years ago
XmlCsvExport.php
7 years ago
XmlExportACF.php
7 years ago
XmlExportComment.php
7 years ago
XmlExportCpt.php
7 years ago
XmlExportEngine.php
7 years ago
XmlExportFiltering.php
7 years ago
XmlExportMediaGallery.php
7 years ago
XmlExportTaxonomy.php
7 years ago
XmlExportUser.php
7 years ago
XmlExportWooCommerce.php
7 years ago
XmlExportWooCommerceCoupon.php
7 years ago
XmlExportWooCommerceOrder.php
7 years ago
XmlGoogleMerchants.php
7 years ago
XmlSpec.php
7 years ago
XmlCsvExport.php
882 lines
| 1 | <?php |
| 2 | |
| 3 | final Class XmlCsvExport |
| 4 | { |
| 5 | public static $main_xml_tag = ''; |
| 6 | |
| 7 | public static $node_xml_tag = ''; |
| 8 | |
| 9 | /** @var \Wpae\Csv\CsvWriter */ |
| 10 | private static $csvWriter; |
| 11 | |
| 12 | public static function export() |
| 13 | { |
| 14 | switch ( XmlExportEngine::$exportOptions['export_to'] ) |
| 15 | { |
| 16 | case 'xml': |
| 17 | self::export_xml(); |
| 18 | break; |
| 19 | |
| 20 | case XmlExportEngine::EXPORT_TYPE_CSV: |
| 21 | self::export_csv(); |
| 22 | break; |
| 23 | default: |
| 24 | # code... |
| 25 | break; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | public static function export_csv( $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0 ) |
| 30 | { |
| 31 | |
| 32 | if ( XmlExportEngine::$exportOptions['delimiter'] == '\t' ) XmlExportEngine::$exportOptions['delimiter'] = "\t"; |
| 33 | |
| 34 | ob_start(); |
| 35 | |
| 36 | $stream = fopen("php://output", 'w'); |
| 37 | |
| 38 | $headers = array(); |
| 39 | $woo = array(); |
| 40 | $woo_order = array(); |
| 41 | $acfs = array(); |
| 42 | $articles = array(); |
| 43 | |
| 44 | // self::$implode = (XmlExportEngine::$exportOptions['delimiter'] == ',') ? '|' : ','; |
| 45 | // |
| 46 | // self::$implode = apply_filters('wp_all_export_implode_delimiter', self::$implode, XmlExportEngine::$exportID); |
| 47 | |
| 48 | // [ Exporting requested data ] |
| 49 | |
| 50 | if ( XmlExportEngine::$is_user_export ) { // exporting WordPress users |
| 51 | |
| 52 | foreach ( XmlExportEngine::$exportQuery->results as $user ) { |
| 53 | $articles[] = XmlExportUser::prepare_data($user, false, $acfs, XmlExportEngine::$implode, $preview); |
| 54 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 55 | if (!$preview) do_action('pmxe_exported_post', $user->ID, XmlExportEngine::$exportRecord); |
| 56 | } |
| 57 | } |
| 58 | elseif ( XmlExportEngine::$is_comment_export ) { // exporting comments |
| 59 | global $wp_version; |
| 60 | |
| 61 | if ( version_compare($wp_version, '4.2.0', '>=') ) { |
| 62 | $comments = XmlExportEngine::$exportQuery->get_comments(); |
| 63 | } |
| 64 | else { |
| 65 | $comments = XmlExportEngine::$exportQuery; |
| 66 | } |
| 67 | |
| 68 | foreach ( $comments as $comment ) { |
| 69 | $articles[] = XmlExportComment::prepare_data($comment, false, XmlExportEngine::$implode, $preview); |
| 70 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 71 | if (!$preview) do_action('pmxe_exported_post', $comment->comment_ID, XmlExportEngine::$exportRecord); |
| 72 | } |
| 73 | } |
| 74 | elseif ( XmlExportEngine::$is_taxonomy_export ) { // exporting WordPress taxonomy terms |
| 75 | |
| 76 | add_filter('terms_clauses', 'wp_all_export_terms_clauses', 10, 3); |
| 77 | $terms = XmlExportEngine::$exportQuery->get_terms(); |
| 78 | remove_filter('terms_clauses', 'wp_all_export_terms_clauses'); |
| 79 | foreach ( $terms as $term ) { |
| 80 | $articles[] = XmlExportTaxonomy::prepare_data($term, false, $acfs, XmlExportEngine::$implode, $preview); |
| 81 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 82 | if (!$preview) do_action('pmxe_exported_post', $term->term_id, XmlExportEngine::$exportRecord); |
| 83 | } |
| 84 | } |
| 85 | else { // exporting custom post types |
| 86 | while ( XmlExportEngine::$exportQuery->have_posts() ) { |
| 87 | XmlExportEngine::$exportQuery->the_post(); |
| 88 | $record = get_post(get_the_ID()); |
| 89 | $articles[] = XmlExportCpt::prepare_data($record, XmlExportEngine::$exportOptions, false, $acfs, $woo, $woo_order, XmlExportEngine::$implode, $preview); |
| 90 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 91 | if (!$preview) do_action('pmxe_exported_post', $record->ID, XmlExportEngine::$exportRecord); |
| 92 | } |
| 93 | |
| 94 | wp_reset_postdata(); |
| 95 | } |
| 96 | // [ \Exporting requested data ] |
| 97 | |
| 98 | // [ Prepare CSV headers ] |
| 99 | if (XmlExportEngine::$exportOptions['ids']): |
| 100 | |
| 101 | foreach (XmlExportEngine::$exportOptions['ids'] as $ID => $value) |
| 102 | { |
| 103 | if ( empty(XmlExportEngine::$exportOptions['cc_name'][$ID]) or empty(XmlExportEngine::$exportOptions['cc_type'][$ID]) or ! is_numeric($ID) ) continue; |
| 104 | |
| 105 | self::prepare_csv_headers( $headers, $ID, $acfs ); |
| 106 | } |
| 107 | |
| 108 | endif; |
| 109 | |
| 110 | $headers = apply_filters( 'wp_all_export_csv_headers', $headers, XmlExportEngine::$exportID ); |
| 111 | |
| 112 | if ($is_cron) { |
| 113 | if ( ! $exported_by_cron ) { |
| 114 | self::getCsvWriter()->writeCsv($stream, array_map(array('XmlCsvExport', '_get_valid_header_name'), $headers), XmlExportEngine::$exportOptions['delimiter']); |
| 115 | apply_filters('wp_all_export_after_csv_line', $stream, XmlExportEngine::$exportID); |
| 116 | } |
| 117 | else { |
| 118 | self::merge_headers( $file_path, $headers ); |
| 119 | } |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | if ($preview or empty(PMXE_Plugin::$session->file)) { |
| 124 | self::getCsvWriter()->writeCsv($stream, array_map(array('XmlCsvExport', '_get_valid_header_name'), $headers), XmlExportEngine::$exportOptions['delimiter']); |
| 125 | apply_filters('wp_all_export_after_csv_line', $stream, XmlExportEngine::$exportID); |
| 126 | } |
| 127 | else { |
| 128 | self::merge_headers( PMXE_Plugin::$session->file, $headers ); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // [ \Prepare CSV headers ] |
| 133 | if (!empty($articles)) { |
| 134 | foreach ($articles as $article) { if (empty($article)) continue; |
| 135 | $line = array(); |
| 136 | foreach ($headers as $header) { |
| 137 | $line[$header] = (isset($article[$header])) ? $article[$header] : ''; |
| 138 | } |
| 139 | self::getCsvWriter()->writeCsv($stream, $line, XmlExportEngine::$exportOptions['delimiter']); |
| 140 | apply_filters('wp_all_export_after_csv_line', $stream, XmlExportEngine::$exportID); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | if ($preview) return ob_get_clean(); |
| 145 | |
| 146 | return self::save_csv_to_file( $file_path, $is_cron, $exported_by_cron ); |
| 147 | |
| 148 | } |
| 149 | |
| 150 | public static function export_xml( $preview = false, $is_cron = false, $file_path = false, $exported_by_cron = 0 ) |
| 151 | { |
| 152 | $is_custom_xml = ( ! empty(XmlExportEngine::$exportOptions['xml_template_type']) && in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants')) ) ? true : false; |
| 153 | |
| 154 | if ( ! empty(XmlExportEngine::$exportOptions['xml_template_type']) ){ |
| 155 | |
| 156 | switch (XmlExportEngine::$exportOptions['xml_template_type']) { |
| 157 | case 'custom': |
| 158 | case 'XmlGoogleMerchants': |
| 159 | XmlExportEngine::$exportOptions['ids'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['ids']; |
| 160 | XmlExportEngine::$exportOptions['cc_label'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_label']; |
| 161 | XmlExportEngine::$exportOptions['cc_type'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_type']; |
| 162 | XmlExportEngine::$exportOptions['cc_value'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_value']; |
| 163 | XmlExportEngine::$exportOptions['cc_name'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_name']; |
| 164 | XmlExportEngine::$exportOptions['cc_php'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_php']; |
| 165 | XmlExportEngine::$exportOptions['cc_code'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_code']; |
| 166 | XmlExportEngine::$exportOptions['cc_sql'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_sql']; |
| 167 | XmlExportEngine::$exportOptions['cc_options'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_options']; |
| 168 | XmlExportEngine::$exportOptions['cc_settings'] = XmlExportEngine::$exportOptions['custom_xml_template_options']['cc_settings']; |
| 169 | break; |
| 170 | default: |
| 171 | # code... |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | if ( XmlExportEngine::$exportOptions['delimiter'] == '\t' ) XmlExportEngine::$exportOptions['delimiter'] = "\t"; |
| 177 | |
| 178 | require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php'; |
| 179 | |
| 180 | $woo = array(); |
| 181 | $woo_order = array(); |
| 182 | $acfs = array(); |
| 183 | |
| 184 | self::$main_xml_tag = apply_filters('wp_all_export_main_xml_tag', XmlExportEngine::$exportOptions['main_xml_tag'], XmlExportEngine::$exportID); |
| 185 | self::$node_xml_tag = apply_filters('wp_all_export_record_xml_tag', XmlExportEngine::$exportOptions['record_xml_tag'], XmlExportEngine::$exportID); |
| 186 | |
| 187 | // self::$implode = (XmlExportEngine::$exportOptions['delimiter'] == ',') ? '|' : ','; |
| 188 | // |
| 189 | // if ( $is_custom_xml ) self::$implode = '#delimiter#'; |
| 190 | |
| 191 | $xmlWriter = new PMXE_XMLWriter(); |
| 192 | |
| 193 | if ( ! $is_custom_xml ) { |
| 194 | |
| 195 | $xmlWriter->openMemory(); |
| 196 | $xmlWriter->setIndent(true); |
| 197 | $xmlWriter->setIndentString("\t"); |
| 198 | $xmlWriter->startDocument('1.0', XmlExportEngine::$exportOptions['encoding']); |
| 199 | $xmlWriter->startElement(self::$main_xml_tag); |
| 200 | // add additional data after XML root element |
| 201 | self::xml_header( $xmlWriter, $is_cron, $exported_by_cron ); |
| 202 | } |
| 203 | |
| 204 | // [ Exporting requested data ] |
| 205 | |
| 206 | if ( XmlExportEngine::$is_user_export ) // exporting WordPress users |
| 207 | { |
| 208 | foreach ( XmlExportEngine::$exportQuery->results as $user ) : |
| 209 | |
| 210 | $is_export_record = apply_filters('wp_all_export_xml_rows', true, $user, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 211 | |
| 212 | if ( ! $is_export_record ) continue; |
| 213 | |
| 214 | if ( ! $is_custom_xml ) { |
| 215 | // add additional information before each node |
| 216 | self::before_xml_node( $xmlWriter, $user->ID); |
| 217 | |
| 218 | $xmlWriter->startElement(self::$node_xml_tag); |
| 219 | |
| 220 | XmlExportUser::prepare_data( $user, $xmlWriter, $acfs, XmlExportEngine::$implode, $preview ); |
| 221 | |
| 222 | $xmlWriter->closeElement(); // end post |
| 223 | |
| 224 | // add additional information after each node |
| 225 | self::after_xml_node( $xmlWriter, $user->ID); |
| 226 | } |
| 227 | else { |
| 228 | $articles = array(); |
| 229 | $articles[] = XmlExportUser::prepare_data( $user, $xmlWriter, $acfs, XmlExportEngine::$implode, $preview ); |
| 230 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 231 | |
| 232 | $xmlWriter->writeArticle( $articles ); |
| 233 | } |
| 234 | |
| 235 | if ( ! $preview) do_action('pmxe_exported_post', $user->ID, XmlExportEngine::$exportRecord ); |
| 236 | |
| 237 | endforeach; |
| 238 | |
| 239 | } |
| 240 | elseif ( XmlExportEngine::$is_taxonomy_export ) // exporting WordPress taxonomy terms |
| 241 | { |
| 242 | add_filter('terms_clauses', 'wp_all_export_terms_clauses', 10, 3); |
| 243 | $terms = XmlExportEngine::$exportQuery->get_terms(); |
| 244 | remove_filter('terms_clauses', 'wp_all_export_terms_clauses'); |
| 245 | foreach ( $terms as $term ) { |
| 246 | |
| 247 | $is_export_record = apply_filters('wp_all_export_xml_rows', true, $term, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 248 | |
| 249 | if (!$is_export_record) continue; |
| 250 | |
| 251 | if (!$is_custom_xml) { |
| 252 | // add additional information before each node |
| 253 | self::before_xml_node($xmlWriter, $term->term_id); |
| 254 | |
| 255 | $xmlWriter->startElement(self::$node_xml_tag); |
| 256 | |
| 257 | XmlExportTaxonomy::prepare_data($term, $xmlWriter, $acfs, XmlExportEngine::$implode, $preview); |
| 258 | |
| 259 | $xmlWriter->closeElement(); // end post |
| 260 | |
| 261 | // add additional information after each node |
| 262 | self::after_xml_node($xmlWriter, $term->term_id); |
| 263 | } else { |
| 264 | $articles = array(); |
| 265 | $articles[] = XmlExportTaxonomy::prepare_data($term, $xmlWriter, $acfs, XmlExportEngine::$implode, $preview); |
| 266 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 267 | |
| 268 | $xmlWriter->writeArticle($articles); |
| 269 | } |
| 270 | |
| 271 | if (!$preview) do_action('pmxe_exported_post', $term->term_id, XmlExportEngine::$exportRecord); |
| 272 | |
| 273 | } |
| 274 | |
| 275 | } |
| 276 | elseif ( XmlExportEngine::$is_comment_export ) // exporting comments |
| 277 | { |
| 278 | global $wp_version; |
| 279 | |
| 280 | if ( version_compare($wp_version, '4.2.0', '>=') ) { |
| 281 | $comments = XmlExportEngine::$exportQuery->get_comments(); |
| 282 | } |
| 283 | else { |
| 284 | $comments = XmlExportEngine::$exportQuery; |
| 285 | } |
| 286 | |
| 287 | foreach ( $comments as $comment ) { |
| 288 | |
| 289 | $is_export_record = apply_filters('wp_all_export_xml_rows', true, $comment, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 290 | |
| 291 | if (!$is_export_record) continue; |
| 292 | |
| 293 | if (!$is_custom_xml) { |
| 294 | // add additional information before each node |
| 295 | self::before_xml_node($xmlWriter, $comment->comment_ID); |
| 296 | |
| 297 | $xmlWriter->startElement(self::$node_xml_tag); |
| 298 | |
| 299 | XmlExportComment::prepare_data($comment, $xmlWriter, XmlExportEngine::$implode, $preview); |
| 300 | |
| 301 | $xmlWriter->closeElement(); // end post |
| 302 | |
| 303 | // add additional information after each node |
| 304 | self::after_xml_node($xmlWriter, $comment->comment_ID); |
| 305 | } else { |
| 306 | $articles = array(); |
| 307 | $articles[] = XmlExportComment::prepare_data($comment, $xmlWriter, XmlExportEngine::$implode, $preview); |
| 308 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 309 | |
| 310 | $xmlWriter->writeArticle($articles); |
| 311 | } |
| 312 | |
| 313 | if (!$preview) do_action('pmxe_exported_post', $comment->comment_ID, XmlExportEngine::$exportRecord); |
| 314 | |
| 315 | } |
| 316 | } |
| 317 | else {// exporting custom post types |
| 318 | while ( XmlExportEngine::$exportQuery->have_posts() ) { |
| 319 | XmlExportEngine::$exportQuery->the_post(); |
| 320 | $record = get_post(get_the_ID()); |
| 321 | |
| 322 | $is_export_record = apply_filters('wp_all_export_xml_rows', true, $record, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 323 | |
| 324 | if (!$is_export_record) continue; |
| 325 | |
| 326 | if (!$is_custom_xml) { |
| 327 | // add additional information before each node |
| 328 | self::before_xml_node($xmlWriter, $record->ID); |
| 329 | $xmlWriter->startElement(self::$node_xml_tag); |
| 330 | XmlExportCpt::prepare_data($record, XmlExportEngine::$exportOptions, $xmlWriter, $acfs, $woo, $woo_order, XmlExportEngine::$implode, $preview); |
| 331 | |
| 332 | $xmlWriter->closeElement(); // end post |
| 333 | |
| 334 | // add additional information after each node |
| 335 | self::after_xml_node($xmlWriter, $record->ID); |
| 336 | } else { |
| 337 | $articles = array(); |
| 338 | $articles[] = XmlExportCpt::prepare_data($record, XmlExportEngine::$exportOptions, $xmlWriter, $acfs, $woo, $woo_order, XmlExportEngine::$implode, $preview); |
| 339 | $articles = apply_filters('wp_all_export_csv_rows', $articles, XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 340 | |
| 341 | $xmlWriter->writeArticle($articles); |
| 342 | } |
| 343 | |
| 344 | if (!$preview) do_action('pmxe_exported_post', $record->ID, XmlExportEngine::$exportRecord); |
| 345 | } |
| 346 | wp_reset_postdata(); |
| 347 | } |
| 348 | // [ \Exporting requested data ] |
| 349 | |
| 350 | if ( ! $is_custom_xml ) $xmlWriter->closeElement(); // close root XML element |
| 351 | |
| 352 | if ($preview) { |
| 353 | return $xmlWriter->wpae_flush(); |
| 354 | } |
| 355 | |
| 356 | return self::save_xml_to_file( $xmlWriter, $file_path, $is_cron, $exported_by_cron ); |
| 357 | |
| 358 | } |
| 359 | |
| 360 | // [ XML Export Helpers ] |
| 361 | private static function xml_header($xmlWriter, $is_cron, $exported_by_cron) |
| 362 | { |
| 363 | if ($is_cron) { |
| 364 | if ( ! $exported_by_cron ) { |
| 365 | |
| 366 | $additional_data = apply_filters('wp_all_export_additional_data', array(), XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 367 | |
| 368 | if ( ! empty($additional_data)) { |
| 369 | foreach ($additional_data as $key => $value) { |
| 370 | self::addElement($xmlWriter, $key, $value); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | |
| 378 | if ( empty(PMXE_Plugin::$session->file) ){ |
| 379 | |
| 380 | $additional_data = apply_filters('wp_all_export_additional_data', array(), XmlExportEngine::$exportOptions, XmlExportEngine::$exportID); |
| 381 | |
| 382 | if ( ! empty($additional_data)) { |
| 383 | foreach ($additional_data as $key => $value) { |
| 384 | self::addElement($xmlWriter, $key, $value); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | private static function before_xml_node( $xmlWriter, $pid ) |
| 392 | { |
| 393 | $add_before_node = apply_filters('wp_all_export_add_before_node', array(), XmlExportEngine::$exportOptions, XmlExportEngine::$exportID, $pid); |
| 394 | |
| 395 | if ( ! empty($add_before_node)) { |
| 396 | foreach ($add_before_node as $key => $value) { |
| 397 | self::addElement($xmlWriter, $key, $value); |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | |
| 402 | private static function after_xml_node( $xmlWriter, $pid ) |
| 403 | { |
| 404 | $add_after_node = apply_filters('wp_all_export_add_after_node', array(), XmlExportEngine::$exportOptions, XmlExportEngine::$exportID, $pid); |
| 405 | |
| 406 | if ( ! empty($add_after_node)) { |
| 407 | foreach ($add_after_node as $key => $value) { |
| 408 | self::addElement($xmlWriter, $key, $value); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | private static function save_xml_to_file( $xmlWriter, $file_path, $is_cron, $exported_by_cron ) |
| 414 | { |
| 415 | $is_custom_xml = in_array(XmlExportEngine::$exportOptions['xml_template_type'], array('custom', 'XmlGoogleMerchants')); |
| 416 | |
| 417 | if ($is_cron) |
| 418 | { |
| 419 | if ( ! $is_custom_xml ) { |
| 420 | $xml_header = apply_filters('wp_all_export_xml_header', '<?xml version="1.0" encoding="UTF-8"?>', XmlExportEngine::$exportID); |
| 421 | |
| 422 | $xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', $xml_header, $xmlWriter->wpae_flush()); |
| 423 | } |
| 424 | else { |
| 425 | $xml_header = XmlExportEngine::$exportOptions['custom_xml_template_header']; |
| 426 | |
| 427 | $xml = ( ! $exported_by_cron ) ? PMXE_XMLWriter::preprocess_xml($xml_header) . $xmlWriter->wpae_flush() : $xmlWriter->wpae_flush(); |
| 428 | } |
| 429 | |
| 430 | if ( ! $exported_by_cron ) { |
| 431 | if ( ! $is_custom_xml ) $xml = substr($xml, 0, (strlen(self::$main_xml_tag) + 4) * (-1)); |
| 432 | |
| 433 | // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters. |
| 434 | if (XmlExportEngine::$exportOptions['include_bom']) { |
| 435 | file_put_contents($file_path, chr(0xEF).chr(0xBB).chr(0xBF).$xml); |
| 436 | } |
| 437 | else { |
| 438 | file_put_contents($file_path, $xml); |
| 439 | } |
| 440 | } |
| 441 | else { |
| 442 | $xml = ( ! $is_custom_xml ) ? substr(substr($xml, 41 + strlen(self::$main_xml_tag)), 0, (strlen(self::$main_xml_tag) + 4) * (-1)) : $xml; |
| 443 | file_put_contents($file_path, $xml, FILE_APPEND); |
| 444 | } |
| 445 | |
| 446 | return $file_path; |
| 447 | |
| 448 | } |
| 449 | else { |
| 450 | |
| 451 | if ( empty(PMXE_Plugin::$session->file) ){ |
| 452 | |
| 453 | // generate export file name |
| 454 | $export_file = wp_all_export_generate_export_file( XmlExportEngine::$exportID ); |
| 455 | |
| 456 | if ( ! $is_custom_xml ) { |
| 457 | $xml_header = apply_filters('wp_all_export_xml_header', '<?xml version="1.0" encoding="UTF-8"?>', XmlExportEngine::$exportID); |
| 458 | |
| 459 | $xml = str_replace('<?xml version="1.0" encoding="UTF-8"?>', $xml_header, $xmlWriter->wpae_flush()); |
| 460 | } |
| 461 | else { |
| 462 | $xml_header = XmlExportEngine::$exportOptions['custom_xml_template_header']; |
| 463 | |
| 464 | $xml = PMXE_XMLWriter::preprocess_xml($xml_header) . $xmlWriter->wpae_flush(); |
| 465 | } |
| 466 | |
| 467 | if ( ! $is_custom_xml ) $xml = substr($xml, 0, (strlen(self::$main_xml_tag) + 4) * (-1)); |
| 468 | |
| 469 | // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters. |
| 470 | if (XmlExportEngine::$exportOptions['include_bom']) { |
| 471 | file_put_contents($export_file, chr(0xEF).chr(0xBB).chr(0xBF).$xml); |
| 472 | } |
| 473 | else { |
| 474 | file_put_contents($export_file, $xml); |
| 475 | } |
| 476 | |
| 477 | PMXE_Plugin::$session->set('file', $export_file); |
| 478 | PMXE_Plugin::$session->save_data(); |
| 479 | |
| 480 | } |
| 481 | else { |
| 482 | $xml = ( ! $is_custom_xml ) ? substr(substr($xmlWriter->wpae_flush(), 41 + strlen(self::$main_xml_tag)), 0, (strlen(self::$main_xml_tag) + 4) * (-1)) : $xmlWriter->wpae_flush(); |
| 483 | |
| 484 | file_put_contents(PMXE_Plugin::$session->file, $xml, FILE_APPEND); |
| 485 | } |
| 486 | |
| 487 | return true; |
| 488 | |
| 489 | } |
| 490 | } |
| 491 | // [ \XML Export Helpers ] |
| 492 | |
| 493 | // [ CSV Export Helpers ] |
| 494 | public static function prepare_csv_headers( & $headers, $ID, &$acfs ) |
| 495 | { |
| 496 | $element_name = ( ! empty(XmlExportEngine::$exportOptions['cc_name'][$ID]) ) ? XmlExportEngine::$exportOptions['cc_name'][$ID] : 'untitled_' . $ID; |
| 497 | $element_name = apply_filters('wp_all_export_field_name', wp_all_export_parse_field_name($element_name), XmlExportEngine::$exportID); |
| 498 | |
| 499 | if ( strpos(XmlExportEngine::$exportOptions['cc_label'][$ID], "item_data__") !== false ) { |
| 500 | XmlExportEngine::$woo_order_export->get_element_header( $headers, XmlExportEngine::$exportOptions, $ID ); |
| 501 | return; |
| 502 | } |
| 503 | |
| 504 | switch (XmlExportEngine::$exportOptions['cc_type'][$ID]) |
| 505 | { |
| 506 | case 'woo': |
| 507 | XmlExportEngine::$woo_export->get_element_header( $headers, XmlExportEngine::$exportOptions, $ID ); |
| 508 | break; |
| 509 | |
| 510 | case 'woo_order': |
| 511 | XmlExportEngine::$woo_order_export->get_element_header( $headers, XmlExportEngine::$exportOptions, $ID ); |
| 512 | break; |
| 513 | |
| 514 | case 'acf': |
| 515 | if ( ! empty($acfs) ) { |
| 516 | $single_acf_field = array_shift($acfs); |
| 517 | |
| 518 | if ( is_array($single_acf_field)) { |
| 519 | foreach ($single_acf_field as $acf_header) { |
| 520 | if ( ! in_array($acf_header, $headers)) $headers[] = $acf_header; |
| 521 | } |
| 522 | } |
| 523 | else { |
| 524 | if ( ! in_array($single_acf_field, $headers)) $headers[] = $single_acf_field; |
| 525 | } |
| 526 | } |
| 527 | break; |
| 528 | |
| 529 | default: |
| 530 | |
| 531 | // For ID columns make first element in lowercase for Excel export |
| 532 | if ($element_name == 'ID' && ! $ID && isset(XmlExportEngine::$exportOptions['export_to_sheet']) && XmlExportEngine::$exportOptions['export_to_sheet'] != 'csv'){ |
| 533 | $element_name = 'id'; |
| 534 | } |
| 535 | |
| 536 | if ( ! in_array($element_name, $headers)) { |
| 537 | $headers[] = $element_name; |
| 538 | } |
| 539 | else { |
| 540 | $is_added = false; |
| 541 | $i = 0; |
| 542 | do { |
| 543 | $new_element_name = $element_name . '_' . md5($i); |
| 544 | |
| 545 | if ( ! in_array($new_element_name, $headers) ) { |
| 546 | $headers[] = $new_element_name; |
| 547 | $is_added = true; |
| 548 | } |
| 549 | |
| 550 | $i++; |
| 551 | } |
| 552 | while ( ! $is_added ); |
| 553 | } |
| 554 | |
| 555 | // if ( XmlExportEngine::$exportOptions['cc_label'][$ID] == 'product_type' and ! in_array('parent_id', $headers)) { |
| 556 | // $headers[] = 'parent_id'; |
| 557 | // } |
| 558 | |
| 559 | break; |
| 560 | } |
| 561 | |
| 562 | } |
| 563 | |
| 564 | public static function _get_valid_header_name( $element_name ) |
| 565 | { |
| 566 | $element_name_parts = explode("_", $element_name); |
| 567 | |
| 568 | $elementIndex = array_pop($element_name_parts); |
| 569 | |
| 570 | if (wp_all_export_isValidMd5($elementIndex)) { |
| 571 | $element_name_in_file = str_replace("_" . $elementIndex, "", $element_name); |
| 572 | } |
| 573 | else { |
| 574 | $element_name_in_file = $element_name; |
| 575 | } |
| 576 | |
| 577 | $element_name_in_file = XmlExportEngine::sanitizeFieldName($element_name_in_file); |
| 578 | |
| 579 | return $element_name_in_file; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | // Add missing ACF headers |
| 584 | public static function merge_headers( $file, &$headers ) |
| 585 | { |
| 586 | |
| 587 | $in = fopen($file, 'r'); |
| 588 | |
| 589 | $clear_old_headers = fgetcsv($in, 0, XmlExportEngine::$exportOptions['delimiter']); |
| 590 | |
| 591 | fclose($in); |
| 592 | |
| 593 | $old_headers = array(); |
| 594 | $sanitized_headers = array(); |
| 595 | $urldecoded_sanitized_headers = array(); |
| 596 | |
| 597 | foreach($headers as $header) { |
| 598 | $sanitizedHeaderValue = str_replace("'", "", str_replace('"', "", str_replace(chr(0xEF) . chr(0xBB) . chr(0xBF), "", $header))); |
| 599 | $sanitized_headers[] = $sanitizedHeaderValue; |
| 600 | $urldecoded_sanitized_headers[] = urldecode($sanitizedHeaderValue); |
| 601 | |
| 602 | } |
| 603 | foreach ($clear_old_headers as $i => $header) { |
| 604 | $header = str_replace("'", "", str_replace('"', "", str_replace(chr(0xEF).chr(0xBB).chr(0xBF), "", $header))); |
| 605 | |
| 606 | if ( ! in_array($header, $old_headers)) { |
| 607 | $old_headers[] = $header; |
| 608 | } |
| 609 | else { |
| 610 | $is_added = false; |
| 611 | $i = 0; |
| 612 | do { |
| 613 | $new_element_name = $header . '_' . md5($i); |
| 614 | |
| 615 | if ( ! in_array($new_element_name, $old_headers) ) { |
| 616 | $old_headers[] = str_replace("'", "", str_replace('"', "", str_replace(chr(0xEF).chr(0xBB).chr(0xBF), "", $new_element_name))); |
| 617 | $is_added = true; |
| 618 | } |
| 619 | |
| 620 | $i++; |
| 621 | } |
| 622 | while ( ! $is_added ); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | $is_update_headers = false; |
| 627 | |
| 628 | foreach ($sanitized_headers as $header) { |
| 629 | if ( ! in_array(XmlExportEngine::sanitizeFieldName($header), $old_headers)) { |
| 630 | $is_update_headers = true; |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | foreach ($old_headers as $old_header) { |
| 636 | if ( ! in_array(XmlExportEngine::sanitizeFieldName($old_header), $urldecoded_sanitized_headers)) { |
| 637 | $is_update_headers = true; |
| 638 | break; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | if ($is_update_headers) { |
| 643 | $tmp_headers = $headers; |
| 644 | $headers = $old_headers; |
| 645 | foreach ($tmp_headers as $theader){ |
| 646 | if (!in_array(XmlExportEngine::sanitizeFieldName($theader), $headers)) $headers[] = $theader; |
| 647 | } |
| 648 | $tmp_file = str_replace(basename($file), 'iteration_' . basename($file), $file); |
| 649 | copy($file, $tmp_file); |
| 650 | $in = fopen($tmp_file, 'r'); |
| 651 | $out = fopen($file, 'w'); |
| 652 | $headers = apply_filters('wp_all_export_csv_headers', $headers, XmlExportEngine::$exportID); |
| 653 | |
| 654 | if ( XmlExportEngine::$exportOptions['include_bom'] ) { |
| 655 | fwrite($out, chr(0xEF).chr(0xBB).chr(0xBF)); |
| 656 | self::getCsvWriter()->writeCsv($out, array_map(array('XmlCsvExport', '_get_valid_header_name'), $headers), XmlExportEngine::$exportOptions['delimiter']); |
| 657 | } |
| 658 | else { |
| 659 | self::getCsvWriter()->writeCsv($out, array_map(array('XmlCsvExport', '_get_valid_header_name'), $headers), XmlExportEngine::$exportOptions['delimiter']); |
| 660 | } |
| 661 | |
| 662 | apply_filters('wp_all_export_after_csv_line', $out, XmlExportEngine::$exportID); |
| 663 | |
| 664 | $exclude_old_headers = fgetcsv($in); |
| 665 | |
| 666 | if (is_resource($in)) { |
| 667 | while ( ! feof($in) ) { |
| 668 | $data = fgetcsv($in, 0, XmlExportEngine::$exportOptions['delimiter']); |
| 669 | if ( empty($data) ) continue; |
| 670 | $data_assoc = array_combine($old_headers, array_values($data)); |
| 671 | $line = array(); |
| 672 | foreach ($headers as $header) { |
| 673 | $line[$header] = ( isset($data_assoc[$header]) ) ? $data_assoc[$header] : ''; |
| 674 | } |
| 675 | self::getCsvWriter()->writeCsv($out, $line, XmlExportEngine::$exportOptions['delimiter']); |
| 676 | apply_filters('wp_all_export_after_csv_line', $out, XmlExportEngine::$exportID); |
| 677 | } |
| 678 | fclose($in); |
| 679 | } |
| 680 | fclose($out); |
| 681 | @unlink($tmp_file); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | private static function save_csv_to_file( $file_path, $is_cron, $exported_by_cron ) |
| 686 | { |
| 687 | if ($is_cron) { |
| 688 | if ( ! $exported_by_cron ) { |
| 689 | // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters. |
| 690 | if (XmlExportEngine::$exportOptions['include_bom']) { |
| 691 | file_put_contents($file_path, chr(0xEF).chr(0xBB).chr(0xBF).ob_get_clean()); |
| 692 | } |
| 693 | else { |
| 694 | file_put_contents($file_path, ob_get_clean()); |
| 695 | } |
| 696 | } |
| 697 | else { |
| 698 | file_put_contents($file_path, ob_get_clean(), FILE_APPEND); |
| 699 | } |
| 700 | |
| 701 | return $file_path; |
| 702 | |
| 703 | } |
| 704 | else |
| 705 | { |
| 706 | if ( empty(PMXE_Plugin::$session->file) ){ |
| 707 | |
| 708 | // generate export file name |
| 709 | $export_file = wp_all_export_generate_export_file( XmlExportEngine::$exportID ); |
| 710 | |
| 711 | // The BOM will help some programs like Microsoft Excel read your export file if it includes non-English characters. |
| 712 | if (XmlExportEngine::$exportOptions['include_bom']) { |
| 713 | file_put_contents($export_file, chr(0xEF).chr(0xBB).chr(0xBF).ob_get_clean()); |
| 714 | } |
| 715 | else { |
| 716 | file_put_contents($export_file, ob_get_clean()); |
| 717 | } |
| 718 | |
| 719 | PMXE_Plugin::$session->set('file', $export_file); |
| 720 | PMXE_Plugin::$session->save_data(); |
| 721 | |
| 722 | } |
| 723 | else { |
| 724 | file_put_contents(PMXE_Plugin::$session->file, ob_get_clean(), FILE_APPEND); |
| 725 | } |
| 726 | |
| 727 | return true; |
| 728 | } |
| 729 | } |
| 730 | // [ \CSV Export Helpers ] |
| 731 | |
| 732 | public static function auto_genetate_export_fields( $post, $errors = false ) |
| 733 | { |
| 734 | $errors or $errors = new WP_Error(); |
| 735 | |
| 736 | remove_all_filters( "wp_all_export_init_fields", 10 ); |
| 737 | remove_all_filters( "wp_all_export_default_fields", 10 ); |
| 738 | remove_all_filters( "wp_all_export_other_fields", 10 ); |
| 739 | remove_all_filters( "wp_all_export_available_sections", 10 ); |
| 740 | remove_all_filters( "wp_all_export_available_data", 10 ); |
| 741 | |
| 742 | $engine = new XmlExportEngine($post, $errors); |
| 743 | $engine->init_additional_data(); |
| 744 | |
| 745 | $auto_generate = array( |
| 746 | 'ids' => array(), |
| 747 | 'cc_label' => array(), |
| 748 | 'cc_php' => array(), |
| 749 | 'cc_code' => array(), |
| 750 | 'cc_sql' => array(), |
| 751 | 'cc_type' => array(), |
| 752 | 'cc_options' => array(), |
| 753 | 'cc_value' => array(), |
| 754 | 'cc_name' => array() |
| 755 | ); |
| 756 | |
| 757 | $available_data = $engine->init_available_data(); |
| 758 | |
| 759 | $available_sections = apply_filters("wp_all_export_available_sections", $engine->get('available_sections')); |
| 760 | |
| 761 | foreach ($available_sections as $slug => $section) |
| 762 | { |
| 763 | if ( ! empty($section['content']) and ! empty($available_data[$section['content']])) |
| 764 | { |
| 765 | foreach ($available_data[$section['content']] as $field) |
| 766 | { |
| 767 | if ( is_array($field) and (isset($field['auto']) or (is_array($post['cpt']) && ! in_array('product', $post['cpt']) ))) |
| 768 | { |
| 769 | $auto_generate['ids'][] = 1; |
| 770 | $auto_generate['cc_label'][] = is_array($field) ? $field['label'] : $field; |
| 771 | $auto_generate['cc_php'][] = 0; |
| 772 | $auto_generate['cc_code'][] = ''; |
| 773 | $auto_generate['cc_sql'][] = ''; |
| 774 | $auto_generate['cc_settings'][] = ''; |
| 775 | $auto_generate['cc_type'][] = is_array($field) ? $field['type'] : $slug; |
| 776 | $auto_generate['cc_options'][] = ''; |
| 777 | $auto_generate['cc_value'][] = is_array($field) ? $field['label'] : $field; |
| 778 | $auto_generate['cc_name'][] = is_array($field) ? $field['name'] : $field; |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | if ( ! empty($section['additional']) ) |
| 783 | { |
| 784 | foreach ($section['additional'] as $sub_slug => $sub_section) { |
| 785 | foreach ($sub_section['meta'] as $field) { |
| 786 | $field_options = ( in_array($sub_slug, array('images', 'attachments')) ) ? esc_attr('{"is_export_featured":true,"is_export_attached":true,"image_separator":"|"}') : '0'; |
| 787 | $field_name = ''; |
| 788 | switch ($sub_slug) { |
| 789 | case 'images': |
| 790 | $field_name = 'Image ' . $field['name']; |
| 791 | break; |
| 792 | case 'attachments': |
| 793 | $field_name = 'Attachment ' . $field['name']; |
| 794 | break; |
| 795 | default: |
| 796 | $field_name = $field['name']; |
| 797 | break; |
| 798 | } |
| 799 | |
| 800 | if ( is_array($field) and isset($field['auto']) ) { |
| 801 | $auto_generate['ids'][] = 1; |
| 802 | $auto_generate['cc_label'][] = is_array($field) ? $field['label'] : $field; |
| 803 | $auto_generate['cc_php'][] = 0; |
| 804 | $auto_generate['cc_code'][] = ''; |
| 805 | $auto_generate['cc_sql'][] = ''; |
| 806 | $auto_generate['cc_settings'][] = ''; |
| 807 | $auto_generate['cc_type'][] = is_array($field) ? $field['type'] : $sub_slug; |
| 808 | $auto_generate['cc_options'][] = $field_options; |
| 809 | $auto_generate['cc_value'][] = is_array($field) ? $field['label'] : $field; |
| 810 | $auto_generate['cc_name'][] = $field_name; |
| 811 | } |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if ( XmlExportWooCommerceOrder::$is_active ) { |
| 818 | foreach (XmlExportWooCommerceOrder::$order_sections as $slug => $section) { |
| 819 | if ( ! empty($section['meta']) ) { |
| 820 | foreach ($section['meta'] as $cur_meta_key => $field) { |
| 821 | $auto_generate['ids'][] = 1; |
| 822 | $auto_generate['cc_label'][] = is_array($field) ? $field['label'] : $cur_meta_key; |
| 823 | $auto_generate['cc_php'][] = 0; |
| 824 | $auto_generate['cc_code'][] = ''; |
| 825 | $auto_generate['cc_sql'][] = ''; |
| 826 | $auto_generate['cc_settings'][] = ''; |
| 827 | $auto_generate['cc_type'][] = is_array($field) ? $field['type'] : 'woo_order'; |
| 828 | $auto_generate['cc_options'][] = is_array($field) ? $field['options'] : $slug; |
| 829 | $auto_generate['cc_value'][] = is_array($field) ? $field['label'] : $cur_meta_key; |
| 830 | $auto_generate['cc_name'][] = is_array($field) ? $field['name'] : $field; |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | if ( ! XmlExportEngine::$is_comment_export ) XmlExportEngine::$acf_export->auto_generate_export_fields( $auto_generate ); |
| 837 | |
| 838 | return $auto_generate; |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * @param $xmlWriter |
| 843 | * @param $key |
| 844 | * @param $value |
| 845 | */ |
| 846 | private static function addElement($xmlWriter, $key, $value) |
| 847 | { |
| 848 | $xmlWriter->startElement(preg_replace('/[^a-z0-9_-]/i', '', $key)); |
| 849 | $xmlWriter->writeData($value, preg_replace('/[^a-z0-9_-]/i', '', $key)); |
| 850 | $xmlWriter->closeElement(); |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * @return \Wpae\Csv\CsvWriter |
| 855 | */ |
| 856 | private static function getCsvWriter() |
| 857 | { |
| 858 | if(is_null(self::$csvWriter)) { |
| 859 | |
| 860 | $csvStrategy = apply_filters('wp_all_export_csv_strategy', \Wpae\Csv\CsvWriter::CSV_STRATEGY_DEFAULT); |
| 861 | $useRcfCompliantLineEndings = apply_filters('wp_all_export_use_csv_compliant_line_endings', false); |
| 862 | |
| 863 | if($useRcfCompliantLineEndings) { |
| 864 | \Wpae\Csv\CsvRfcUtils::setDefaultWriteEol(\Wpae\Csv\CsvRfcUtils::EOL_WRITE_RFC); |
| 865 | } |
| 866 | |
| 867 | self::$csvWriter = new \Wpae\Csv\CsvWriter($csvStrategy); |
| 868 | } |
| 869 | |
| 870 | return self::$csvWriter; |
| 871 | } |
| 872 | |
| 873 | private static function isNotProductExport($cpt) { |
| 874 | |
| 875 | if(is_array($cpt)) { |
| 876 | return !in_array('product', $cpt); |
| 877 | } else { |
| 878 | return 'product' !== $cpt; |
| 879 | } |
| 880 | } |
| 881 | } |
| 882 |