Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: gevz
- * Date: 14.09.18
- * Time: 17:47
- */
- namespace gt_;
- use MongoDB\BSON\ObjectId;
- class gtools_core
- {
- public function get_entry_file_list(){
- $path = UPLOADDIR.'/wpallimport/files';
- $files = scandir($path);
- if($files){
- $out = '<ul class="gt-filelist-ul">';
- foreach ($files as $file) {
- $type = mime_content_type($path.'/'.basename($file));
- if($file == '.' || $file == '..' || $type != 'application/xml') continue;
- $out .= '<li class="gt-files" data-param="'.$file.'">'.$file.'</li>';
- }
- $out .= '</ul>';
- return $out;
- } else {
- return 'ERROR!';
- }
- }
- public function get_file_params($filename){
- $file = UPLOADDIR.'/wpallimport/files/'.basename($filename);
- $xml = simplexml_load_file($file) or die ('Cannot create object!');
- return $xml;
- }
- public function read_file_by_string($filename){
- $file = UPLOADDIR.'/wpallimport/files/'.basename($filename);
- $i = 0;
- $descriptor = fopen($file, 'r');
- if($descriptor) {
- while(($string = fgets($descriptor)) !== false || $i <= 30) {
- echo $i.' '.$string.'<br>';
- $i++;
- }
- fclose($descriptor);
- } else {
- echo 'Cannot open file.';
- }
- }
- public function xml_r($filename, $create_cat = 'false'){
- global $product, $wpdb;
- $i = 1;
- $p = 0;
- $file = UPLOADDIR.'/wpallimport/files/'.basename($filename);
- $xml_reader = new \XMLReader();
- $xml_reader->open($file);
- $dom = new \DOMDocument();
- $out = '';
- if($create_cat === 'true') {
- $out .= '<div class="gt-cats-list"><ul>';
- $cats = $this->gt_create_cats( $file );
- foreach ( $cats as $cat ) {
- $slug = $this->gt_transliterate( $cat["id"] . '-' . $cat, 'to_lat' );
- $check_cat = get_term_by( 'slug', $slug, 'product_cat' );
- if ( !$check_cat ) {
- $create_cat = wp_insert_term(
- $cat,
- 'product_cat',
- array(
- 'description' => '',
- 'slug' => $slug
- ) );
- if ( $create_cat ) {
- $out .= '<li>' . $cat . ' created!</li>';
- }
- } else {
- $out .= '<li>' . $cat . ' already exist.</li>';
- }
- }
- $out .= '</ul></div>';
- }
- echo $out;
- echo '<ul class="gt-products-list">';
- while ($xml_reader->read() && $xml_reader->name !== 'offer');
- while ($xml_reader->name === 'offer' /* && $i <= 3 */){
- $out = '';
- $node = simplexml_import_dom($dom->importNode($xml_reader->expand(), true));
- //echo var_dump($node);
- $sku = $xml_reader->getAttribute('id');
- $pname = $node->name;
- $pdesc = $node->description;
- $price = floatval($node->price);
- $category_id = $node->categoryId;
- $pictures = $this->build_pic_array($node->picture);
- $product = $wpdb->get_var($wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku));
- if(!$product) {
- $cat = $this->gt_get_category($category_id);
- $out .= '<li class="gt-product"><div class="gtp-num">'.$i.'</div>';
- $out .= '<div class="gtp-sku">'.$sku.'</div>';
- $out .= '<div class="gtp-title">'.$pname.'</div>';
- $out .= '<div class="gtp-cat" data-cat-id="'.$cat->term_id.'">'.$cat->name.'</div>';
- $out .= '<div class="gtp-pictures">'.$pictures.'</div>';
- $out .= '<div class="gtp-desc">'.$pdesc.'</div>';
- $out .= '<div class="gtp-price">'.$price.'</div>';
- $out .= '</li>';
- $add = $this->gt_import_product( $cat, $node, $sku, $price, $node->picture );
- ob_flush();
- echo $out;
- sleep(3);
- $i++;
- } else {
- $i++;
- $p++;
- }
- $xml_reader->next('offer');
- }
- echo '<li>Proccessed: '.$i.' Passed: '.$p.'. That`s all, folks!</li>';
- echo '</ul>';
- }
- private function build_pic_array($pic_arr){
- $out = '<ul class="gtp-pic-list">';
- foreach ($pic_arr as $pic) {
- $out .= '<li><img class="gtp-imgs" src="'.$pic.'"></li>';
- }
- $out .= '</ul>';
- return $out;
- }
- private function gt_export_product($name, $description, $price, $category_id){
- }
- private function gt_create_cats($file){
- $xml_reader = new \XMLReader();
- $xml_reader->open($file);
- $dom = new \DOMDocument();
- while ($xml_reader->read() && $xml_reader->name !== 'categories');
- while ($xml_reader->name === 'categories') {
- $node = simplexml_import_dom($dom->importNode($xml_reader->expand(), true));
- return $node;
- }
- }
- private function gt_transliterate($string, $type = 'to_lat') {
- $cyr = [
- 'а','б','в','г','д','е','ё','ж','з','и','й','к','л','м','н','о','п',
- 'р','с','т','у','ф','х','ц','ч','ш','щ','ъ','ы','ь','э','ю','я',
- 'А','Б','В','Г','Д','Е','Ё','Ж','З','И','Й','К','Л','М','Н','О','П',
- 'Р','С','Т','У','Ф','Х','Ц','Ч','Ш','Щ','Ъ','Ы','Ь','Э','Ю','Я'
- ];
- $lat = [
- 'a','b','v','g','d','e','io','zh','z','i','y','k','l','m','n','o','p',
- 'r','s','t','u','f','h','ts','ch','sh','sht','a','i','y','e','yu','ya',
- 'A','B','V','G','D','E','Io','Zh','Z','I','Y','K','L','M','N','O','P',
- 'R','S','T','U','F','H','Ts','Ch','Sh','Sht','A','I','Y','e','Yu','Ya'
- ];
- $point_marks = ['.',',',';',':','!','?'];
- switch ($type){
- case 'to_lat':
- $textcyr = $string;
- $textcyr = str_replace($cyr, $lat, $textcyr);
- $textcyr = str_replace(' ','-', $textcyr);
- $textcyr = str_replace($point_marks, '', $textcyr);
- $textcyr = strtolower($textcyr);
- return $textcyr;
- break;
- case 'to_cyr':
- $textlat = $string;
- $textlat = str_replace($lat, $cyr, $textlat);
- $textlat = str_replace(' ', '-', $textlat);
- $textlat = str_replace($point_marks, '', $textlat);
- $textlat = strtolower($textlat);
- return $textlat;
- break;
- }
- }
- private function gt_get_category($parsed_cat_id){
- $args = array(
- 'taxonomy' => 'product_cat',
- 'orderby' => 'name',
- 'hide_empty' => 0
- );
- $cats = get_categories($args);
- foreach ($cats as $cat){
- $slug = $cat->slug;
- $search = '"'.$parsed_cat_id.'"';
- preg_match($search, $slug, $matches);
- if($matches){
- return $cat;
- }
- }
- }
- private function gt_import_product($cat, $import, $sku, $price, $pictures){
- $user = wp_get_current_user();
- $user_id = $user->ID;
- $wp_upload_dir = wp_upload_dir();
- $images = array();
- $post = array(
- 'post_author' => $user_id,
- 'post_content' => $import->description,
- 'post_status' => 'publish',
- 'post_title' => $import->name,
- 'post_parent' => '',
- 'post_type' => 'product'
- );
- $post_id = wp_insert_post($post, true);
- if(is_wp_error($post_id)){
- return $post_id->get_error_message();
- } else {
- //Images
- foreach($pictures as $picture) {
- $pic = file_get_contents($picture);
- $upload_file = wp_upload_bits(basename($picture), null, $pic);
- $mime = wp_check_filetype(basename($picture), null);
- $attachment = array(
- 'guid' => $wp_upload_dir['url'] . '/' . basename( $picture ),
- 'post_parent' => $post_id,
- 'post_mime_type' => $mime['type'],
- 'post_title' => basename($picture),
- 'post_content' => '',
- 'post_status' => 'inherit'
- );
- $image_id = wp_insert_attachment($attachment, $upload_file['file'], $post_id);
- array_push($images, $image_id);
- // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
- // Generate the metadata for the attachment, and update the database record.
- $attach_data = wp_generate_attachment_metadata( $image_id, $upload_file['file'] );
- wp_update_attachment_metadata( $image_id, $attach_data );
- }
- // Question
- if ( $post_id ) {
- //$attach_id = get_post_meta( $post_id, '_thumbnail_id', true );
- add_post_meta( $post_id, '_thumbnail_id', $images[0] );
- }
- wp_set_object_terms($post_id, $cat->name, 'product_cat');
- wp_set_object_terms($post_id, 'simple', 'product_type');
- // Attributes
- wp_set_object_terms( $post_id, 'termo-perenos', 'pa_application', true );
- wp_set_object_terms( $post_id, 'ukrayina', 'pa_country', true );
- wp_set_object_terms( $post_id, 'oryginal', 'pa_originality', true );
- wp_set_object_terms( $post_id, 'globuspioner', 'pa_vendor', true );
- // Attridutes value
- $thedata = array(
- 'pa_application' => array(
- 'name' => 'pa_application',
- 'value' => 'termo-perenos',
- 'is_visible' => '1',
- 'is_variation' => '0',
- 'is_taxonomy' => '1'
- ),
- 'pa_country' => array(
- 'name' => 'pa_country',
- 'value' => 'ukrayina',
- 'is_visible' => '1',
- 'is_variation' => '0',
- 'is_taxonomy' => '1'
- ),
- 'pa_originality' => array(
- 'name' => 'pa_originality',
- 'value' => 'oryginal',
- 'is_visible' => '1',
- 'is_variation' => '0',
- 'is_taxonomy' => '1'
- ),
- 'pa_vendor' => array(
- 'name' => 'pa_vendor',
- 'value' => 'globuspioner',
- 'is_visible' => '1',
- 'is_variation' => '0',
- 'is_taxonomy' => '1'
- )
- );
- update_post_meta( $post_id,'_product_attributes', $thedata);
- update_post_meta( $post_id, '_visibility', 'visible' );
- update_post_meta( $post_id, '_stock_status', 'instock');
- update_post_meta( $post_id, 'total_sales', '0');
- update_post_meta( $post_id, '_downloadable', 'no');
- update_post_meta( $post_id, '_virtual', 'yes');
- update_post_meta( $post_id, '_regular_price', $price );
- update_post_meta( $post_id, '_sale_price', "" );
- update_post_meta( $post_id, '_purchase_note', "" );
- update_post_meta( $post_id, '_featured', "no" );
- update_post_meta( $post_id, '_weight', "" );
- update_post_meta( $post_id, '_length', "" );
- update_post_meta( $post_id, '_width', "" );
- update_post_meta( $post_id, '_height', "" );
- update_post_meta( $post_id, '_sku', $sku);
- update_post_meta( $post_id, '_product_attributes', array());
- update_post_meta( $post_id, '_sale_price_dates_from', "" );
- update_post_meta( $post_id, '_sale_price_dates_to', "" );
- update_post_meta( $post_id, '_price', $price );
- update_post_meta( $post_id, '_sold_individually', "" );
- update_post_meta( $post_id, '_manage_stock', "no" );
- update_post_meta( $post_id, '_backorders', "no" );
- update_post_meta( $post_id, '_stock', "" );
- //Images
- update_post_meta( $post_id, '_product_image_gallery', implode(',',$images));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement