admin_head.php
9 years ago
admin_init.php
5 years ago
admin_menu.php
4 years ago
admin_notices.php
8 years ago
init.php
8 years ago
pmxe_after_export.php
8 years ago
pmxe_before_export.php
8 years ago
pmxe_exported_post.php
10 years ago
wp_ajax_dismiss_export_warnings.php
9 years ago
wp_ajax_dismiss_warnings.php
4 years ago
wp_ajax_generate_zapier_api_key.php
10 years ago
wp_ajax_redirect_after_addon_installed.php
6 years ago
wp_ajax_save_scheduling.php
8 years ago
wp_ajax_scheduling_dialog_content.php
5 years ago
wp_ajax_wpae_available_rules.php
8 years ago
wp_ajax_wpae_filtering.php
4 years ago
wp_ajax_wpae_filtering_count.php
4 years ago
wp_ajax_wpae_preview.php
8 years ago
wp_ajax_wpae_upgrade_notice.php
4 years ago
wp_ajax_wpallexport.php
8 years ago
wp_loaded.php
4 years ago
wpmu_new_blog.php
10 years ago
pmxe_after_export.php
417 lines
| 1 | <?php |
| 2 | |
| 3 | function pmxe_prepend($string, $orig_filename) { |
| 4 | $context = stream_context_create(); |
| 5 | $orig_file = fopen($orig_filename, 'r', 1, $context); |
| 6 | |
| 7 | $temp_filename = tempnam(sys_get_temp_dir(), 'php_prepend_'); |
| 8 | file_put_contents($temp_filename, $string); |
| 9 | file_put_contents($temp_filename, $orig_file, FILE_APPEND); |
| 10 | |
| 11 | fclose($orig_file); |
| 12 | unlink($orig_filename); |
| 13 | rename($temp_filename, $orig_filename); |
| 14 | } |
| 15 | |
| 16 | function pmxe_pmxe_after_export($export_id, $export) |
| 17 | { |
| 18 | if ( ! empty(PMXE_Plugin::$session) and PMXE_Plugin::$session->has_session() ) |
| 19 | { |
| 20 | PMXE_Plugin::$session->set('file', ''); |
| 21 | PMXE_Plugin::$session->save_data(); |
| 22 | } |
| 23 | |
| 24 | if ( ! $export->isEmpty()) |
| 25 | { |
| 26 | |
| 27 | $export->set( |
| 28 | array( |
| 29 | 'registered_on' => current_time( 'mysql', 1 ), |
| 30 | ) |
| 31 | )->save(); |
| 32 | |
| 33 | $splitSize = $export->options['split_large_exports_count']; |
| 34 | |
| 35 | $exportOptions = $export->options; |
| 36 | // remove previously genereted chunks |
| 37 | if ( ! empty($exportOptions['split_files_list']) and ! $export->options['creata_a_new_export_file'] ) |
| 38 | { |
| 39 | foreach ($exportOptions['split_files_list'] as $file) { |
| 40 | @unlink($file); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); |
| 45 | |
| 46 | if ( ! $is_secure_import) |
| 47 | { |
| 48 | $filepath = get_attached_file($export->attch_id); |
| 49 | } |
| 50 | else |
| 51 | { |
| 52 | $filepath = wp_all_export_get_absolute_path($export->options['filepath']); |
| 53 | } |
| 54 | |
| 55 | //TODO: Look into what is happening with this variable and what it is used for |
| 56 | $is_export_csv_headers = apply_filters('wp_all_export_is_csv_headers_enabled', true, $export->id); |
| 57 | |
| 58 | if ( isset($export->options['include_header_row']) ) { |
| 59 | $is_export_csv_headers = $export->options['include_header_row']; |
| 60 | } |
| 61 | |
| 62 | $removeHeaders = false; |
| 63 | |
| 64 | $removeHeaders = apply_filters('wp_all_export_remove_csv_headers', $removeHeaders, $export->id); |
| 65 | |
| 66 | // Remove headers row from CSV file |
| 67 | if ( (empty($is_export_csv_headers) && @file_exists($filepath) && $export->options['export_to'] == 'csv' && $export->options['export_to_sheet'] == 'csv') || $removeHeaders){ |
| 68 | |
| 69 | $tmp_file = str_replace(basename($filepath), 'iteration_' . basename($filepath), $filepath); |
| 70 | copy($filepath, $tmp_file); |
| 71 | $in = fopen($tmp_file, 'r'); |
| 72 | $out = fopen($filepath, 'w'); |
| 73 | |
| 74 | $headers = fgetcsv($in, 0, XmlExportEngine::$exportOptions['delimiter']); |
| 75 | |
| 76 | if (is_resource($in)) { |
| 77 | $lineNumber = 0; |
| 78 | while ( ! feof($in) ) { |
| 79 | $data = fgetcsv($in, 0, XmlExportEngine::$exportOptions['delimiter']); |
| 80 | if ( empty($data) ) continue; |
| 81 | $data_assoc = array_combine($headers, array_values($data)); |
| 82 | $line = array(); |
| 83 | foreach ($headers as $header) { |
| 84 | $line[$header] = ( isset($data_assoc[$header]) ) ? $data_assoc[$header] : ''; |
| 85 | } |
| 86 | if ( ! $lineNumber && XmlExportEngine::$exportOptions['include_bom']){ |
| 87 | fwrite($out, chr(0xEF).chr(0xBB).chr(0xBF)); |
| 88 | fputcsv($out, $line, XmlExportEngine::$exportOptions['delimiter']); |
| 89 | } |
| 90 | else{ |
| 91 | fputcsv($out, $line, XmlExportEngine::$exportOptions['delimiter']); |
| 92 | } |
| 93 | apply_filters('wp_all_export_after_csv_line', $out, XmlExportEngine::$exportID); |
| 94 | $lineNumber++; |
| 95 | } |
| 96 | fclose($in); |
| 97 | } |
| 98 | fclose($out); |
| 99 | @unlink($tmp_file); |
| 100 | } |
| 101 | |
| 102 | $preCsvHeaders = false; |
| 103 | $preCsvHeaders = apply_filters('wp_all_export_pre_csv_headers', $preCsvHeaders, $export->id); |
| 104 | |
| 105 | if($preCsvHeaders) { |
| 106 | pmxe_prepend($preCsvHeaders."\n", $filepath); |
| 107 | } |
| 108 | |
| 109 | // Split large exports into chunks |
| 110 | if ( $export->options['split_large_exports'] and $splitSize < $export->exported ) |
| 111 | { |
| 112 | |
| 113 | $exportOptions['split_files_list'] = array(); |
| 114 | |
| 115 | if ( @file_exists($filepath) ) |
| 116 | { |
| 117 | |
| 118 | switch ($export->options['export_to']) |
| 119 | { |
| 120 | case 'xml': |
| 121 | |
| 122 | require_once PMXE_ROOT_DIR . '/classes/XMLWriter.php'; |
| 123 | |
| 124 | switch ( $export->options['xml_template_type']) |
| 125 | { |
| 126 | case 'XmlGoogleMerchants': |
| 127 | case 'custom': |
| 128 | // Determine XML root element |
| 129 | $main_xml_tag = false; |
| 130 | preg_match_all("%<[\w]+[\s|>]{1}%", $export->options['custom_xml_template_header'], $matches); |
| 131 | if ( ! empty($matches[0]) ){ |
| 132 | $main_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0])); |
| 133 | } |
| 134 | // Determine XML recond element |
| 135 | $record_xml_tag = false; |
| 136 | preg_match_all("%<[\w]+[\s|>]{1}%", $export->options['custom_xml_template_loop'], $matches); |
| 137 | if ( ! empty($matches[0]) ){ |
| 138 | $record_xml_tag = preg_replace("%[\s|<|>]%","",array_shift($matches[0])); |
| 139 | } |
| 140 | |
| 141 | $xml_header = PMXE_XMLWriter::preprocess_xml($export->options['custom_xml_template_header']); |
| 142 | $xml_footer = PMXE_XMLWriter::preprocess_xml($export->options['custom_xml_template_footer']); |
| 143 | |
| 144 | break; |
| 145 | |
| 146 | default: |
| 147 | $main_xml_tag = apply_filters('wp_all_export_main_xml_tag', $export->options['main_xml_tag'], $export->id); |
| 148 | $record_xml_tag = apply_filters('wp_all_export_record_xml_tag', $export->options['record_xml_tag'], $export->id); |
| 149 | $xml_header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . "<".$main_xml_tag.">"; |
| 150 | $xml_footer = "</".$main_xml_tag.">"; |
| 151 | break; |
| 152 | |
| 153 | } |
| 154 | |
| 155 | |
| 156 | $records_count = 0; |
| 157 | $chunk_records_count = 0; |
| 158 | $fileCount = 1; |
| 159 | |
| 160 | $feed = $xml_header; |
| 161 | |
| 162 | if($export->options['xml_template_type'] == 'custom'){ |
| 163 | $outputFileTemplate = str_replace(basename($filepath), str_replace('.xml', '', basename($filepath)) . '-{FILE_COUNT_PLACEHOLDER}.xml', $filepath); |
| 164 | $exportOptions['split_files_list'] = wp_all_export_break_into_files($record_xml_tag, -1, $splitSize, file_get_contents($filepath), null, $outputFileTemplate); |
| 165 | |
| 166 | // Remove first file which just contains the empty data tag |
| 167 | @unlink($exportOptions['split_files_list'][0]); |
| 168 | array_shift($exportOptions['split_files_list']); |
| 169 | } |
| 170 | else { |
| 171 | $file = new PMXE_Chunk($filepath, array('element' => $record_xml_tag, 'encoding' => 'UTF-8')); |
| 172 | // loop through the file until all lines are read |
| 173 | while ($xml = $file->read()) { |
| 174 | |
| 175 | if ( ! empty($xml) ) |
| 176 | { |
| 177 | $records_count++; |
| 178 | $chunk_records_count++; |
| 179 | $feed .= $xml; |
| 180 | } |
| 181 | |
| 182 | if ( $chunk_records_count == $splitSize or $records_count == $export->exported ){ |
| 183 | $feed .= "\n".$xml_footer; |
| 184 | $outputFile = str_replace(basename($filepath), str_replace('.xml', '', basename($filepath)) . '-' . $fileCount++ . '.xml', $filepath); |
| 185 | file_put_contents($outputFile, $feed); |
| 186 | if ( ! in_array($outputFile, $exportOptions['split_files_list'])) |
| 187 | $exportOptions['split_files_list'][] = $outputFile; |
| 188 | $chunk_records_count = 0; |
| 189 | $feed = $xml_header; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | break; |
| 195 | case 'csv': |
| 196 | $in = fopen($filepath, 'r'); |
| 197 | |
| 198 | $rowCount = 0; |
| 199 | $fileCount = 1; |
| 200 | $headers = fgetcsv($in); |
| 201 | while (!feof($in)) { |
| 202 | $data = fgetcsv($in); |
| 203 | if (empty($data)) continue; |
| 204 | if (($rowCount % $splitSize) == 0) { |
| 205 | if ($rowCount > 0) { |
| 206 | fclose($out); |
| 207 | } |
| 208 | $outputFile = str_replace(basename($filepath), str_replace('.csv', '', basename($filepath)) . '-' . $fileCount++ . '.csv', $filepath); |
| 209 | if ( ! in_array($outputFile, $exportOptions['split_files_list'])) |
| 210 | $exportOptions['split_files_list'][] = $outputFile; |
| 211 | |
| 212 | $out = fopen($outputFile, 'w'); |
| 213 | } |
| 214 | if ($data){ |
| 215 | if (($rowCount % $splitSize) == 0) { |
| 216 | fputcsv($out, $headers); |
| 217 | } |
| 218 | fputcsv($out, $data); |
| 219 | } |
| 220 | $rowCount++; |
| 221 | } |
| 222 | fclose($in); |
| 223 | fclose($out); |
| 224 | |
| 225 | // convert splitted files into XLS format |
| 226 | if ( ! empty($exportOptions['split_files_list']) && ! empty($export->options['export_to_sheet']) and $export->options['export_to_sheet'] != 'csv' ) |
| 227 | { |
| 228 | require_once PMXE_Plugin::ROOT_DIR . '/classes/PHPExcel/IOFactory.php'; |
| 229 | |
| 230 | foreach ($exportOptions['split_files_list'] as $key => $file) |
| 231 | { |
| 232 | $objReader = PHPExcel_IOFactory::createReader('CSV'); |
| 233 | // If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader |
| 234 | $objReader->setDelimiter($export->options['delimiter']); |
| 235 | // If the files uses an encoding other than UTF-8 or ASCII, then tell the reader |
| 236 | $objPHPExcel = $objReader->load($file); |
| 237 | switch ($export->options['export_to_sheet']){ |
| 238 | case 'xls': |
| 239 | $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); |
| 240 | $objWriter->save(str_replace(".csv", ".xls", $file)); |
| 241 | $exportOptions['split_files_list'][$key] = str_replace(".csv", ".xls", $file); |
| 242 | break; |
| 243 | case 'xlsx': |
| 244 | $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); |
| 245 | $objWriter->save(str_replace(".csv", ".xlsx", $file)); |
| 246 | $exportOptions['split_files_list'][$key] = str_replace(".csv", ".xlsx", $file); |
| 247 | break; |
| 248 | } |
| 249 | @unlink($file); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | break; |
| 254 | |
| 255 | default: |
| 256 | |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | $export->set(array('options' => $exportOptions))->save(); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // convert CSV to XLS |
| 265 | if ( @file_exists($filepath) and $export->options['export_to'] == 'csv' && ! empty($export->options['export_to_sheet']) and $export->options['export_to_sheet'] != 'csv') |
| 266 | { |
| 267 | |
| 268 | require_once PMXE_Plugin::ROOT_DIR . '/classes/PHPExcel/IOFactory.php'; |
| 269 | |
| 270 | $objReader = PHPExcel_IOFactory::createReader('CSV'); |
| 271 | // If the files uses a delimiter other than a comma (e.g. a tab), then tell the reader |
| 272 | $objReader->setDelimiter($export->options['delimiter']); |
| 273 | // If the files uses an encoding other than UTF-8 or ASCII, then tell the reader |
| 274 | |
| 275 | $objPHPExcel = $objReader->load($filepath); |
| 276 | |
| 277 | switch ($export->options['export_to_sheet']) { |
| 278 | case 'xls': |
| 279 | $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); |
| 280 | $objWriter->save(str_replace(".csv", ".xls", $filepath)); |
| 281 | @unlink($filepath); |
| 282 | $filepath = str_replace(".csv", ".xls", $filepath); |
| 283 | break; |
| 284 | case 'xlsx': |
| 285 | $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); |
| 286 | $objWriter->save(str_replace(".csv", ".xlsx", $filepath)); |
| 287 | @unlink($filepath); |
| 288 | $filepath = str_replace(".csv", ".xlsx", $filepath); |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | $exportOptions = $export->options; |
| 293 | $exportOptions['filepath'] = wp_all_export_get_relative_path($filepath); |
| 294 | $export->set(array('options' => $exportOptions))->save(); |
| 295 | |
| 296 | $is_secure_import = PMXE_Plugin::getInstance()->getOption('secure'); |
| 297 | |
| 298 | if ( ! $is_secure_import ){ |
| 299 | $wp_uploads = wp_upload_dir(); |
| 300 | $wp_filetype = wp_check_filetype(basename($filepath), null ); |
| 301 | $attachment_data = array( |
| 302 | 'guid' => $wp_uploads['baseurl'] . '/' . _wp_relative_upload_path( $filepath ), |
| 303 | 'post_mime_type' => $wp_filetype['type'], |
| 304 | 'post_title' => preg_replace('/\.[^.]+$/', '', basename($filepath)), |
| 305 | 'post_content' => '', |
| 306 | 'post_status' => 'inherit' |
| 307 | ); |
| 308 | if ( ! empty($export->attch_id) ) |
| 309 | { |
| 310 | $attach_id = $export->attch_id; |
| 311 | $attachment = get_post($attach_id); |
| 312 | if ($attachment) |
| 313 | { |
| 314 | update_attached_file( $attach_id, $filepath ); |
| 315 | wp_update_attachment_metadata( $attach_id, $attachment_data ); |
| 316 | } |
| 317 | else |
| 318 | { |
| 319 | $attach_id = wp_insert_attachment( $attachment_data, PMXE_Plugin::$session->file ); |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | } |
| 325 | |
| 326 | // make a temporary copy of current file |
| 327 | if ( empty($export->parent_id) and @file_exists($filepath) and @copy($filepath, str_replace(basename($filepath), '', $filepath) . 'current-' . basename($filepath))) |
| 328 | { |
| 329 | $exportOptions = $export->options; |
| 330 | $exportOptions['current_filepath'] = str_replace(basename($filepath), '', $filepath) . 'current-' . basename($filepath); |
| 331 | $export->set(array('options' => $exportOptions))->save(); |
| 332 | } |
| 333 | |
| 334 | $generateBundle = apply_filters('wp_all_export_generate_bundle', true); |
| 335 | |
| 336 | if($generateBundle) { |
| 337 | |
| 338 | // genereta export bundle |
| 339 | $export->generate_bundle(); |
| 340 | |
| 341 | if ( ! empty($export->parent_id) ) |
| 342 | { |
| 343 | $parent_export = new PMXE_Export_Record(); |
| 344 | $parent_export->getById($export->parent_id); |
| 345 | if ( ! $parent_export->isEmpty() ) |
| 346 | { |
| 347 | $parent_export->generate_bundle(true); |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | |
| 353 | // send exported data to zapier.com |
| 354 | $subscriptions = get_option('zapier_subscribe', array()); |
| 355 | if ( ! empty($subscriptions) and empty($export->parent_id)) |
| 356 | { |
| 357 | |
| 358 | $wp_uploads = wp_upload_dir(); |
| 359 | |
| 360 | $fileurl = str_replace($wp_uploads['basedir'], $wp_uploads['baseurl'], $filepath); |
| 361 | |
| 362 | $response = array( |
| 363 | 'website_url' => home_url(), |
| 364 | 'export_id' => $export->id, |
| 365 | 'export_name' => $export->friendly_name, |
| 366 | 'file_name' => basename($filepath), |
| 367 | 'file_type' => wp_all_export_get_export_format($export->options), |
| 368 | 'post_types_exported' => empty($export->options['cpt']) ? $export->options['wp_query'] : implode($export->options['cpt'], ','), |
| 369 | 'export_created_date' => $export->registered_on, |
| 370 | 'export_last_run_date' => date('Y-m-d H:i:s'), |
| 371 | 'export_trigger_type' => empty($_GET['export_key']) ? 'manual' : 'cron', |
| 372 | 'records_exported' => $export->exported, |
| 373 | 'export_file' => '' |
| 374 | ); |
| 375 | |
| 376 | if (file_exists($filepath)) |
| 377 | { |
| 378 | $response['export_file_url'] = $fileurl; |
| 379 | $response['status'] = 200; |
| 380 | $response['message'] = 'OK'; |
| 381 | } |
| 382 | else |
| 383 | { |
| 384 | $response['export_file_url'] = ''; |
| 385 | $response['status'] = 300; |
| 386 | $response['message'] = 'File doesn\'t exist'; |
| 387 | } |
| 388 | |
| 389 | $response = apply_filters('wp_all_export_zapier_response', $response); |
| 390 | |
| 391 | foreach ($subscriptions as $zapier) |
| 392 | { |
| 393 | if (empty($zapier['target_url'])) continue; |
| 394 | |
| 395 | wp_remote_post( $zapier['target_url'], array( |
| 396 | 'method' => 'POST', |
| 397 | 'timeout' => 45, |
| 398 | 'redirection' => 5, |
| 399 | 'httpversion' => '1.0', |
| 400 | 'blocking' => true, |
| 401 | 'headers' => array( |
| 402 | 'Content-Type' => 'application/json' |
| 403 | ), |
| 404 | 'body' => "[".json_encode($response)."]", |
| 405 | 'cookies' => array() |
| 406 | ) |
| 407 | ); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // clean session |
| 412 | if ( ! empty(PMXE_Plugin::$session) and PMXE_Plugin::$session->has_session() ) |
| 413 | { |
| 414 | PMXE_Plugin::$session->clean_session( $export->id ); |
| 415 | } |
| 416 | } |
| 417 | } |