admin_head.php
4 weeks ago
admin_init.php
4 weeks ago
admin_menu.php
4 weeks ago
admin_notices.php
4 weeks ago
init.php
4 weeks ago
pmxe_after_export.php
4 weeks ago
pmxe_before_export.php
4 weeks ago
pmxe_exported_post.php
4 weeks ago
wp_ajax_dismiss_export_warnings.php
4 weeks ago
wp_ajax_dismiss_review_modal.php
4 weeks ago
wp_ajax_dismiss_warnings.php
4 weeks ago
wp_ajax_generate_zapier_api_key.php
4 weeks ago
wp_ajax_redirect_after_addon_installed.php
4 weeks ago
wp_ajax_save_scheduling.php
4 weeks ago
wp_ajax_scheduling_dialog_content.php
4 weeks ago
wp_ajax_scheduling_subscribe_dialog_content.php
4 weeks ago
wp_ajax_send_feedback.php
4 weeks ago
wp_ajax_wpae_available_rules.php
4 weeks ago
wp_ajax_wpae_filtering.php
4 weeks ago
wp_ajax_wpae_filtering_count.php
4 weeks ago
wp_ajax_wpae_get_scheduling_connection_icon.php
4 weeks ago
wp_ajax_wpae_preview.php
4 weeks ago
wp_ajax_wpae_upgrade_notice.php
4 weeks ago
wp_ajax_wpallexport.php
4 weeks ago
wp_loaded.php
4 weeks ago
wpmu_new_blog.php
4 weeks ago
pmxe_after_export.php
285 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 | function pmxe_prepend($string, $orig_filename) { |
| 8 | $context = stream_context_create(); |
| 9 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 10 | $orig_file = fopen($orig_filename, 'r', 1, $context); |
| 11 | |
| 12 | $temp_filename = tempnam(sys_get_temp_dir(), 'php_prepend_'); |
| 13 | file_put_contents($temp_filename, $string); |
| 14 | file_put_contents($temp_filename, $orig_file, FILE_APPEND); |
| 15 | |
| 16 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 17 | fclose($orig_file); |
| 18 | wp_delete_file($orig_filename); |
| 19 | // phpcs:ignore WordPress.WP.AlternativeFunctions.rename_rename -- export plugin manipulates its own temp files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 20 | rename($temp_filename, $orig_filename); |
| 21 | } |
| 22 | |
| 23 | function pmxe_pmxe_after_export($export_id, $export) |
| 24 | { |
| 25 | if ( ! empty(PMXE_Plugin::$session) and PMXE_Plugin::$session->has_session() ) |
| 26 | { |
| 27 | PMXE_Plugin::$session->set('file', ''); |
| 28 | PMXE_Plugin::$session->save_data(); |
| 29 | } |
| 30 | |
| 31 | if ( ! $export->isEmpty()) |
| 32 | { |
| 33 | |
| 34 | $export->set( |
| 35 | array( |
| 36 | 'registered_on' => current_time( 'mysql', 1 ), |
| 37 | ) |
| 38 | )->save(); |
| 39 | |
| 40 | $splitSize = $export->options['split_large_exports_count']; |
| 41 | |
| 42 | $exportOptions = $export->options; |
| 43 | // remove previously genereted chunks |
| 44 | if ( ! empty($exportOptions['split_files_list']) and ! $export->options['creata_a_new_export_file'] ) |
| 45 | { |
| 46 | foreach ($exportOptions['split_files_list'] as $file) { |
| 47 | wp_delete_file($file); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); |
| 52 | |
| 53 | if ( ! $is_secure_import) |
| 54 | { |
| 55 | $filepath = get_attached_file($export->attch_id); |
| 56 | } |
| 57 | else |
| 58 | { |
| 59 | $filepath = wp_all_export_get_absolute_path($export->options['filepath']); |
| 60 | } |
| 61 | |
| 62 | //TODO: Look into what is happening with this variable and what it is used for |
| 63 | $is_export_csv_headers = apply_filters('wp_all_export_is_csv_headers_enabled', true, $export->id); |
| 64 | |
| 65 | if ( isset($export->options['include_header_row']) ) { |
| 66 | $is_export_csv_headers = $export->options['include_header_row']; |
| 67 | } |
| 68 | |
| 69 | $removeHeaders = false; |
| 70 | |
| 71 | $removeHeaders = apply_filters('wp_all_export_remove_csv_headers', $removeHeaders, $export->id); |
| 72 | |
| 73 | // Remove headers row from CSV file |
| 74 | if ( (empty($is_export_csv_headers) && @file_exists($filepath) && $export->options['export_to'] == 'csv' && $export->options['export_to_sheet'] == 'csv') || $removeHeaders){ |
| 75 | |
| 76 | $tmp_file = str_replace(basename($filepath), 'iteration_' . basename($filepath), $filepath); |
| 77 | copy($filepath, $tmp_file); |
| 78 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 79 | $in = fopen($tmp_file, 'r'); |
| 80 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 81 | $out = fopen($filepath, 'w'); |
| 82 | |
| 83 | $headers = fgetcsv($in, 0, XmlExportEngine::$exportOptions['delimiter'], '"', '\\'); |
| 84 | |
| 85 | if (is_resource($in)) { |
| 86 | $lineNumber = 0; |
| 87 | while ( ! feof($in) ) { |
| 88 | $data = fgetcsv($in, 0, XmlExportEngine::$exportOptions['delimiter'], '"', '\\'); |
| 89 | if ( empty($data) ) continue; |
| 90 | $data_assoc = array_combine($headers, array_values($data)); |
| 91 | $line = array(); |
| 92 | foreach ($headers as $header) { |
| 93 | $line[$header] = ( isset($data_assoc[$header]) ) ? $data_assoc[$header] : ''; |
| 94 | } |
| 95 | if ( ! $lineNumber && XmlExportEngine::$exportOptions['include_bom']){ |
| 96 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fwrite -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 97 | fwrite($out, chr(0xEF).chr(0xBB).chr(0xBF)); |
| 98 | fputcsv($out, $line, XmlExportEngine::$exportOptions['delimiter'], '"', '\\'); |
| 99 | } |
| 100 | else{ |
| 101 | fputcsv($out, $line, XmlExportEngine::$exportOptions['delimiter'], '"', '\\'); |
| 102 | } |
| 103 | apply_filters('wp_all_export_after_csv_line', $out, XmlExportEngine::$exportID); |
| 104 | $lineNumber++; |
| 105 | } |
| 106 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 107 | fclose($in); |
| 108 | } |
| 109 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 110 | fclose($out); |
| 111 | wp_delete_file($tmp_file); |
| 112 | } |
| 113 | |
| 114 | $preCsvHeaders = false; |
| 115 | $preCsvHeaders = apply_filters('wp_all_export_pre_csv_headers', $preCsvHeaders, $export->id); |
| 116 | |
| 117 | if($preCsvHeaders) { |
| 118 | pmxe_prepend($preCsvHeaders."\n", $filepath); |
| 119 | } |
| 120 | |
| 121 | // Split large exports into chunks |
| 122 | if ( $export->options['split_large_exports'] and $splitSize < $export->exported ) |
| 123 | { |
| 124 | |
| 125 | $exportOptions['split_files_list'] = array(); |
| 126 | |
| 127 | if ( @file_exists($filepath) ) |
| 128 | { |
| 129 | |
| 130 | switch ($export->options['export_to']) |
| 131 | { |
| 132 | case 'xml': |
| 133 | |
| 134 | require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php'; |
| 135 | |
| 136 | switch ( $export->options['xml_template_type']) |
| 137 | { |
| 138 | case 'XmlGoogleMerchants': |
| 139 | case 'custom': |
| 140 | // Determine XML root element |
| 141 | $main_xml_tag = false; |
| 142 | preg_match_all("%<[\w]+[\s|>]{1}%", $export->options['custom_xml_template_header'], $matches); |
| 143 | if ( ! empty($matches[0]) ){ |
| 144 | $main_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0])); |
| 145 | } |
| 146 | // Determine XML recond element |
| 147 | $record_xml_tag = false; |
| 148 | preg_match_all("%<[\w]+[\s|>]{1}%", $export->options['custom_xml_template_loop'], $matches); |
| 149 | if ( ! empty($matches[0]) ){ |
| 150 | $record_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0])); |
| 151 | } |
| 152 | |
| 153 | $xml_header = PMXE_XMLWriter::preprocess_xml($export->options['custom_xml_template_header']); |
| 154 | $xml_footer = PMXE_XMLWriter::preprocess_xml($export->options['custom_xml_template_footer']); |
| 155 | |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $export->options['main_xml_tag'], $export->id); |
| 160 | $record_xml_tag = apply_filters('wp_all_export_record_xml_tag', $export->options['record_xml_tag'], $export->id); |
| 161 | $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . "<".$main_xml_tag.">"; |
| 162 | $xml_footer = "</".$main_xml_tag.">"; |
| 163 | break; |
| 164 | |
| 165 | } |
| 166 | |
| 167 | |
| 168 | $records_count = 0; |
| 169 | $chunk_records_count = 0; |
| 170 | $fileCount = 1; |
| 171 | |
| 172 | $feed = $xml_header; |
| 173 | |
| 174 | if($export->options['xml_template_type'] == 'custom'){ |
| 175 | $outputFileTemplate = str_replace(basename($filepath), str_replace('.xml', '', basename($filepath)) . '-{FILE_COUNT_PLACEHOLDER}.xml', $filepath); |
| 176 | $exportOptions['split_files_list'] = wp_all_export_break_into_files($record_xml_tag, -1, $splitSize, file_get_contents($filepath), null, $outputFileTemplate); |
| 177 | |
| 178 | // Remove first file which just contains the empty data tag |
| 179 | wp_delete_file($exportOptions['split_files_list'][0]); |
| 180 | array_shift($exportOptions['split_files_list']); |
| 181 | } |
| 182 | else { |
| 183 | $file = new PMXE_Chunk($filepath, array('element' => $record_xml_tag, 'encoding' => 'UTF-8')); |
| 184 | // loop through the file until all lines are read |
| 185 | while ($xml = $file->read()) { |
| 186 | |
| 187 | if ( ! empty($xml) ) |
| 188 | { |
| 189 | $records_count++; |
| 190 | $chunk_records_count++; |
| 191 | $feed .= $xml; |
| 192 | } |
| 193 | |
| 194 | if ( $chunk_records_count == $splitSize or $records_count == $export->exported ){ |
| 195 | $feed .= "\n".$xml_footer; |
| 196 | $outputFile = str_replace(basename($filepath), str_replace('.xml', '', basename($filepath)) . '-' . $fileCount++ . '.xml', $filepath); |
| 197 | file_put_contents($outputFile, $feed); |
| 198 | if ( ! in_array($outputFile, $exportOptions['split_files_list'])) |
| 199 | $exportOptions['split_files_list'][] = $outputFile; |
| 200 | $chunk_records_count = 0; |
| 201 | $feed = $xml_header; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | break; |
| 207 | case 'csv': |
| 208 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 209 | $in = fopen($filepath, 'r'); |
| 210 | |
| 211 | $rowCount = 0; |
| 212 | $fileCount = 1; |
| 213 | $headers = fgetcsv($in, 0, ',', '"', '\\'); |
| 214 | while (!feof($in)) { |
| 215 | $data = fgetcsv($in, 0, ',', '"', '\\'); |
| 216 | if (empty($data)) continue; |
| 217 | if (($rowCount % $splitSize) == 0) { |
| 218 | if ($rowCount > 0) { |
| 219 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 220 | fclose($out); |
| 221 | } |
| 222 | $outputFile = str_replace(basename($filepath), str_replace('.csv', '', basename($filepath)) . '-' . $fileCount++ . '.csv', $filepath); |
| 223 | if ( ! in_array($outputFile, $exportOptions['split_files_list'])) |
| 224 | $exportOptions['split_files_list'][] = $outputFile; |
| 225 | |
| 226 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 227 | $out = fopen($outputFile, 'w'); |
| 228 | } |
| 229 | if ($data){ |
| 230 | if (($rowCount % $splitSize) == 0) { |
| 231 | fputcsv($out, $headers, ',', '"', '\\'); |
| 232 | } |
| 233 | fputcsv($out, $data, ',', '"', '\\'); |
| 234 | } |
| 235 | $rowCount++; |
| 236 | } |
| 237 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 238 | fclose($in); |
| 239 | // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- export plugin reads/writes its own files in uploads dir, WP_Filesystem not viable for streamed CSV/XML output |
| 240 | fclose($out); |
| 241 | |
| 242 | break; |
| 243 | |
| 244 | default: |
| 245 | |
| 246 | break; |
| 247 | } |
| 248 | |
| 249 | $export->set(array('options' => $exportOptions))->save(); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // make a temporary copy of current file |
| 254 | if ( empty($export->parent_id) and @file_exists($filepath) and @copy($filepath, str_replace(basename($filepath), '', $filepath) . 'current-' . basename($filepath))) |
| 255 | { |
| 256 | $exportOptions = $export->options; |
| 257 | $exportOptions['current_filepath'] = str_replace(basename($filepath), '', $filepath) . 'current-' . basename($filepath); |
| 258 | $export->set(array('options' => $exportOptions))->save(); |
| 259 | } |
| 260 | |
| 261 | $generateBundle = apply_filters('wp_all_export_generate_bundle', true); |
| 262 | |
| 263 | if($generateBundle) { |
| 264 | |
| 265 | // genereta export bundle |
| 266 | $export->generate_bundle(); |
| 267 | |
| 268 | if ( ! empty($export->parent_id) ) |
| 269 | { |
| 270 | $parent_export = new PMXE_Export_Record(); |
| 271 | $parent_export->getById($export->parent_id); |
| 272 | if ( ! $parent_export->isEmpty() ) |
| 273 | { |
| 274 | $parent_export->generate_bundle(true); |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // clean session |
| 280 | if ( ! empty(PMXE_Plugin::$session) and PMXE_Plugin::$session->has_session() ) |
| 281 | { |
| 282 | PMXE_Plugin::$session->clean_session( $export->id ); |
| 283 | } |
| 284 | } |
| 285 | } |