admin_head.php
11 years ago
admin_init.php
11 years ago
admin_menu.php
11 years ago
admin_notices.php
11 years ago
delete_post.php
11 years ago
plugins_loaded.php
11 years ago
wp.php
11 years ago
wp_ajax_auto_detect_cf.php
11 years ago
wp_ajax_auto_detect_sf.php
11 years ago
wp_ajax_import_failed.php
11 years ago
wp_ajax_nested_merge.php
11 years ago
wp_ajax_nested_xpath.php
11 years ago
wp_ajax_parse_nested_file.php
11 years ago
wp_ajax_test_images.php
11 years ago
wp_ajax_unmerge_file.php
11 years ago
wp_loaded.php
11 years ago
wp_ajax_parse_nested_file.php
155 lines
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * Ajax action that will parse nested XML/CSV files |
| 5 | * |
| 6 | */ |
| 7 | function pmxi_wp_ajax_parse_nested_file(){ |
| 8 | |
| 9 | extract($_POST); |
| 10 | |
| 11 | $result = array(); |
| 12 | |
| 13 | $wp_uploads = wp_upload_dir(); |
| 14 | |
| 15 | if ( ! empty($_POST['nested_type']) ){ |
| 16 | |
| 17 | $root_element = ''; |
| 18 | $feed_type = ''; |
| 19 | $errors = new WP_Error(); |
| 20 | |
| 21 | switch ($_POST['nested_type']){ |
| 22 | |
| 23 | case 'upload': |
| 24 | |
| 25 | $uploader = new PMXI_Upload($_POST['nested_filepath'], $errors); |
| 26 | $upload_result = $uploader->upload(); |
| 27 | |
| 28 | if ($upload_result instanceof WP_Error){ |
| 29 | $errors = $upload_result; |
| 30 | } |
| 31 | else{ |
| 32 | $source = $upload_result['source']; |
| 33 | $filePath = $upload_result['filePath']; |
| 34 | if ( ! empty($upload_result['root_element'])) |
| 35 | $root_element = $upload_result['root_element']; |
| 36 | } |
| 37 | |
| 38 | break; |
| 39 | |
| 40 | case 'url': |
| 41 | |
| 42 | $uploader = new PMXI_Upload($_POST['nested_url'], $errors); |
| 43 | $upload_result = $uploader->url(); |
| 44 | |
| 45 | if ($upload_result instanceof WP_Error){ |
| 46 | $errors = $upload_result; |
| 47 | } |
| 48 | else{ |
| 49 | $source = $upload_result['source']; |
| 50 | $filePath = $upload_result['filePath']; |
| 51 | if ( ! empty($upload_result['root_element'])) |
| 52 | $root_element = $upload_result['root_element']; |
| 53 | $feed_type = $upload_result['feed_type']; |
| 54 | } |
| 55 | |
| 56 | break; |
| 57 | |
| 58 | case 'file': |
| 59 | |
| 60 | $uploader = new PMXI_Upload($_POST['nested_file'], $errors); |
| 61 | $upload_result = $uploader->file(); |
| 62 | |
| 63 | if ($upload_result instanceof WP_Error){ |
| 64 | $errors = $upload_result; |
| 65 | } |
| 66 | else{ |
| 67 | $source = $upload_result['source']; |
| 68 | $filePath = $upload_result['filePath']; |
| 69 | if ( ! empty($upload_result['root_element'])) |
| 70 | $root_element = $upload_result['root_element']; |
| 71 | } |
| 72 | |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if ( $errors->get_error_codes() ) |
| 78 | { |
| 79 | $msgs = $errors->get_error_messages(); |
| 80 | ob_start(); |
| 81 | ?> |
| 82 | <?php foreach ($msgs as $msg): ?> |
| 83 | <div class="error"><p><?php echo $msg ?></p></div> |
| 84 | <?php endforeach ?> |
| 85 | <?php |
| 86 | exit(json_encode(array( |
| 87 | 'success' => false, |
| 88 | 'errors' => ob_get_clean() |
| 89 | ))); die; |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | |
| 94 | $xml_tree = ''; |
| 95 | |
| 96 | if ( @file_exists($filePath) ){ |
| 97 | |
| 98 | $file = new PMXI_Chunk($filePath, array('element' => $root_element)); |
| 99 | |
| 100 | if ( ! empty($file->options['element']) ) { |
| 101 | $customXpath = "/".$file->options['element']; |
| 102 | $elements_cloud = $file->cloud; |
| 103 | } |
| 104 | |
| 105 | $root_element = $file->options['element']; |
| 106 | |
| 107 | $file = new PMXI_Chunk($filePath, array('element' => $root_element, 'encoding' => 'UTF-8')); |
| 108 | |
| 109 | $tagno = 0; |
| 110 | $loop = 0; |
| 111 | $count = 0; |
| 112 | |
| 113 | while ($xml = $file->read()) { |
| 114 | |
| 115 | if ( ! empty($xml) ) |
| 116 | { |
| 117 | PMXI_Import_Record::preprocessXml($xml); |
| 118 | $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "\n" . $xml; |
| 119 | |
| 120 | if ( '' != $customXpath){ |
| 121 | $dom = new DOMDocument('1.0', 'UTF-8'); |
| 122 | $old = libxml_use_internal_errors(true); |
| 123 | $dom->loadXML($xml); |
| 124 | libxml_use_internal_errors($old); |
| 125 | $xpath = new DOMXPath($dom); |
| 126 | if (($elements = $xpath->query($customXpath)) and $elements->length){ |
| 127 | $loop++; |
| 128 | $count += $elements->length; |
| 129 | if ( ! $tagno or $loop == $tagno ){ |
| 130 | ob_start(); |
| 131 | PMXI_Render::render_xml_element($elements->item(0), true); |
| 132 | $xml_tree = ob_get_clean(); |
| 133 | $tagno = 1; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | else break; |
| 138 | } |
| 139 | } |
| 140 | unset($file); |
| 141 | } |
| 142 | |
| 143 | exit(json_encode(array( |
| 144 | 'success' => true, |
| 145 | 'source' => $source, |
| 146 | 'realpath' => $source['path'], |
| 147 | 'filePath' => $filePath, |
| 148 | 'root_element' => $root_element, |
| 149 | 'xml_tree' => $xml_tree, |
| 150 | 'xpath' => $customXpath, |
| 151 | 'count' => (($count) ? sprintf("<p class='green pmxi_counter'>" . __('Elements founded', 'pmxi_pligun') . " <strong>%s</strong></p>", $count) : "<p class='red pmxi_counter'>" . __('Elements not found', 'pmxi_pligun') . "</p>") |
| 152 | ))); die; |
| 153 | } |
| 154 | |
| 155 | } |