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