/* Decoded by unphp.net */ settings['site_id']; if ($site_id){ if ( ! file_exists ( WCAS_XML_DIR.'/site_'.$site_id.'.yml' ) ){ error_log(date('d.m.Y H:i:s').' WCAS: no main yml file found. Trying to download it.'); $WCAS_Integration->download_xml($site_id); } } $files = scandir(WCAS_XML_DIR); $xml_roots = array(); $xml_cats = array(); $xml_names = array(); foreach ($files as $file){ if (preg_match('|^site_\d+\.yml$|',$file)) { $yml_catalog = simplexml_load_file( WCAS_XML_DIR.'/'.$file ); foreach ($yml_catalog->shop->categories->category as $category){ $attr = $category->attributes(); if ( empty($attr['id']) ) continue; $xml_cat_id = (string)$attr['id']; $xml_names[$xml_cat_id] = wp_unslash(trim((string)$category)); if ( empty($attr['parentId']) ){ $parent_cat_id = '0'; if ( !in_array($xml_cat_id, $xml_roots) ) $xml_roots[] = $xml_cat_id; } else{ $parent_cat_id = (string)$attr['parentId']; if ( empty($xml_cats[$parent_cat_id]) ) $xml_cats[$parent_cat_id]=array(); if ( !in_array($xml_cat_id, $xml_cats[$parent_cat_id]) ) $xml_cats[$parent_cat_id][] = $xml_cat_id; } } } } foreach ( $xml_roots as $tmp=>$root_id ){ if ( in_multiarray($root_id, $xml_cats) ) unset ( $xml_roots[$tmp] ); } $all_cats = $xml_cats; $all_cats[0] = $xml_roots; foreach ( $all_cats as $cats_arr ){ $tmp_names = array(); foreach ( $cats_arr as $xml_cat_id ){ $tmp_names[$xml_cat_id] = $xml_names[$xml_cat_id]; } $arr_cnt = array_count_values($tmp_names); foreach ( $arr_cnt as $title=>$cnt){ if ( $cnt > 1 ) { $found = array_keys( $tmp_names, $title ); for( $i=1, $max=count($found); $i<$max; $i++ ){ $xml_names[$found[$i]] .= ' '.$i; } } } } if(!count($xml_roots)) die('cant find any root categories in xml'); $final = make_tree($xml_roots); $xml_roots= null; $xml_cats = null; $all_cats = null; global $assigned, $wp_parent; $assigned = array(); $wp_parent = array(); $wp_cats = get_terms( 'product_cat', array( 'hide_empty' => 0, 'fields' => 'id=>parent', ) ); foreach ( $wp_cats as $cat_id=>$parent_cat_id ){ $term_meta = get_option( "taxonomy_".$cat_id ); if ( !empty($term_meta['as_cat_id']) ) { $assigned[$cat_id] = $term_meta['as_cat_id']; $wp_parent[$cat_id] = $parent_cat_id; } } $wp_cats = null; foreach ( $final as $xml_cat=>$child ){ fix_cat($xml_cat, 0, $child); } error_log('WCAS: catalogue sync finished'); function fix_cat($xml_cat, $xml_parent_cat, $child){ global $xml_names, $assigned, $wp_parent; $wp_cat_id = array_search( $xml_cat, $assigned ); if ( !$wp_cat_id ){ if ( $xml_parent_cat ) $wp_parent_cat_id = array_search( $xml_parent_cat, $assigned ); else $wp_parent_cat_id = 0; $res = wp_insert_term( $xml_names[$xml_cat], 'product_cat', $args = array('parent'=>$wp_parent_cat_id, 'slug'=>wcas_str2slug($xml_names[$xml_cat]) ) ); if ( $res instanceof WP_Error ){ error_log ('ERROR adding cat with apishops_id='.$xml_cat.'. Name: '.$xml_names[$xml_cat]); error_log ( print_r($res,1) ); error_log ('EXIT'); exit; } $term_meta = get_option( "taxonomy_".$res['term_id'] ); $term_meta['as_cat_id'] = $xml_cat; update_option( "taxonomy_".$res['term_id'], $term_meta ); $assigned[$res['term_id']] = $xml_cat; $wp_parent[$res['term_id']] = $xml_parent_cat; error_log ("OK. New catalogue added. Cat id (AS):".$xml_cat.", cat id (site): ".$res['term_id'] .", name: ".$xml_names[$xml_cat] .( !empty($xml_names[$xml_parent_cat]) ? " (parent: ".$xml_names[$xml_parent_cat].")" : "" ) ); } else { $curr_wp_parent_cat_id = $wp_parent[$wp_cat_id]; if( $xml_parent_cat ) $must_wp_parent_cat_id = array_search( $xml_parent_cat, $assigned ); else $must_wp_parent_cat_id = 0; if ( $curr_wp_parent_cat_id != $must_wp_parent_cat_id){ $term = get_term_by('id',$wp_cat_id,'product_cat'); $res = wp_update_term( $term->term_id, 'product_cat', $args = array('parent' => $must_wp_parent_cat_id) ); if ( $res instanceof WP_Error ){ error_log ('ERROR moving cat '.$term->term_id); error_log ( print_r($res,1) ); error_log ('EXIT'); exit; } $wp_parent[$wp_cat_id] = $must_wp_parent_cat_id; error_log ('OK. Fix parent for catalogue. Cat id (AS):'.$xml_cat.', cat id (site): '.$wp_cat_id); } } if ( !empty($child) && is_array($child) && count($child) ) { foreach ( $child as $xml_cat_2=>$child_2){ fix_cat($xml_cat_2, $xml_cat, $child_2); } } } function make_tree ( $work_arr ){ global $xml_cats; foreach ( $work_arr as $cat_id ){ if (!empty($xml_cats[$cat_id])) $final[$cat_id] = make_tree($xml_cats[$cat_id] ); else $final[$cat_id] = ''; } return $final; } function in_multiarray ( $value, $array ){ if( empty($value) ) return FALSE; if( !is_array($array) || !count($array) ) return FALSE; foreach ( $array as $arr ){ if ( !is_array($arr) ){ if ( $arr==$value ) return TRUE; } else{ if ( in_multiarray ( $value, $arr ) ) return TRUE; } } return FALSE; } function wcas_str2slug($string) { $converter = array( 'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo', 'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'shch', 'ь' => '', 'ы' => 'y', 'ъ' => '', 'э' => 'eh', 'ю' => 'yu', 'я' => 'ya', 'А' => 'a', 'Б' => 'b', 'В' => 'v', 'Г' => 'g', 'Д' => 'd', 'Е' => 'e', 'Ё' => 'yo', 'Ж' => 'zh', 'З' => 'z', 'И' => 'i', 'Й' => 'j', 'К' => 'k', 'Л' => 'l', 'М' => 'm', 'Н' => 'n', 'О' => 'o', 'П' => 'p', 'Р' => 'r', 'С' => 's', 'Т' => 't', 'У' => 'u', 'Ф' => 'f', 'Х' => 'h', 'Ц' => 'c', 'Ч' => 'ch', 'Ш' => 'sh', 'Щ' => 'shch', 'Ь' => '', 'Ы' => 'y', 'Ъ' => '', 'Э' => 'eh', 'Ю' => 'yu', 'Я' => 'ya', ); $str = strtr($string, $converter); $str = strtolower($str); $str = preg_replace('|[^-a-z0-9_]+|', '-', $str); $str = preg_replace('|-{2,}|', '-', $str); $str = trim($str, "-"); return $str; } ?>