600, 'display' => 'Каждые 10 минут' ); $raspisanie['one_minutes'] = array( 'interval' => 60, 'display' => 'Каждую минут' ); $raspisanie['five_minutes'] = array( 'interval' => 300, 'display' => 'Каждые 5 минут' ); $raspisanie['there_minutes'] = array( 'interval' => 180, 'display' => 'Каждые 3 минут' ); $raspisanie['there_hours'] = array( 'interval' => 10800, 'display' => 'Каждые 3 часа' ); $raspisanie['hours'] = array( 'interval' => 3600, 'display' => 'Каждый час' ); return $raspisanie; } //hourly if( !wp_next_scheduled('action_generateXML') ) wp_schedule_event( time(), 'hours', 'action_generateXML'); add_action( 'action_generateXML', 'generateXML', 10); function generateXML() { global $wpdb, $post; $max_second=(int)ini_get('max_execution_time'); ini_set("memory_limit", '3000M'); $start = time(); $tmp_path=ABSPATH.'wp-content/uploads/temp_xml'; $tmp_feeds=$tmp_path.'/all_feeds/'; $tmp_file=$tmp_path.'/xml.tmp'; $feeds_path=ABSPATH.'/feed/'; require_once(get_template_directory().'/includes/classes/feed.php'); $feed=new Feed($tmp_file); $start_size=$feed->getSize(); sleep(1); if($start_size!=$feed->getSize()) { echo 'Процесс уже идет!'; die(); } $max_size=250000000; $step=150000; $term_str=''; // $terms=get_terms( ['taxonomy'=>'product_cat','child_of'=>19,'hide_empty'=>false]); // $terms[]=get_term(19); $terms=get_terms( ['taxonomy'=>'product_cat','hide_empty'=>false, 'exclude' => [6963, 6964]]); // Исключаем категорию Misc $term_ids=array(); foreach ($terms as $term) { $term_ids[]=$term->term_id; $term_str .="'".$term->term_id."',"; } $term_str=trim($term_str,','); if($feed->getSize()==0) { $feed->new_string(''); $feed->new_string(''); $feed->new_string(''); $feed->new_string(''); $feed->tags('name', get_bloginfo('name')); $feed->tags('company', get_bloginfo('name')); $feed->tags('url', home_url()); $feed->tags('phone', 'Россия: '.get_field('phone','options')); $feed->tags('platform', 'Yandex.YML'); $feed->tags('version', '1.7.3'); $feed->new_string(''); $feed->new_string(''); $feed->new_string(''); $feed->new_string(''); foreach($terms as $term) { $parent_id=$term->parent ? 'parentId="'.$term->parent.'"' : 'parentId="0"'; $feed->new_string(''.trim($term->name).''); } $feed->new_string(''); $feed->new_string(''); } unset($terms); $query_str="SELECT ID FROM $wpdb->posts p left join $wpdb->term_relationships tr ON (p.ID = tr.object_id) where p.ID > %d and p.post_type = 'product' and p.post_status = 'publish' "; if($term_str) { $query_str .=" and tr.term_taxonomy_id IN (".$term_str.") "; } $query_str .=" group by p.ID ORDER BY p.ID limit %d "; $current_product=get_option('xml_current_product') ? get_option('xml_current_product') : 0; $query_str=$wpdb->prepare($query_str, $current_product,$step ); foreach ($wpdb->get_col($query_str) as $ID) { $post=get_post($ID); setup_postdata($post); $feed->new_string(''); $feed->tags('url', get_the_permalink()); $feed->tags('price', round(get_post_meta( $ID, '_price', true ))); $feed->tags('currencyId', 'RUB'); foreach (array_reverse(wp_get_object_terms( array($ID), 'product_cat', array('fields'=>'ids') )) as $term_id) { if(in_array($term_id,$term_ids)) { $feed->tags('categoryId', $term_id); break; } } $feed->tags('delivery', 'true'); $feed->tags('name', Feed::prepareField(get_the_title())); $desc=get_the_content(); if($desc) { $feed->tags('description', Feed::cdata($desc)); } //$feed->tags('vendor', Feed::prepareField($array['BRAND'])); foreach (return_post_meta_and_name ($ID) as $name=>$value) { $feed->tags('param', Feed::prepareField($value), array('type'=>'name', 'text'=>Feed::prepareField($name))); } $feed->tags('picture', get_the_post_thumbnail_url( $ID, 'full' )); $feed->new_string(''); $end_work_time= 600 - (time() - $start); // update_option( 'end_work_time', $end_work_time,false); if($end_work_time < 7) { update_option( 'xml_current_product', $ID,false); die(); } if($feed->getSize() > $max_size) { $feed->new_string(''); $feed->new_string(''); $feed->new_string(''); $k=1; $new_feed=$tmp_feeds.'yml_'.$k.'.xml'; while(file_exists($new_feed)) { $k++; $new_feed=$tmp_feeds.'yml_'.$k.'.xml'; } unset($feed); rename($tmp_file, $new_feed); //$wpdb update_option( 'xml_current_product', $ID,false); die(); } // array_map('unlink',glob(ABSPATH.'/feed/*')); //glob($tmp_feeds.'*'); //file_exists } $feed->new_string(''); $feed->new_string(''); $feed->new_string(''); $k=1; $new_feed=$tmp_feeds.'yml_'.$k.'.xml'; while(file_exists($new_feed)) { $k++; $new_feed=$tmp_feeds.'yml_'.$k.'.xml'; } unset($feed); rename($tmp_file, $new_feed); array_map('unlink',glob($feeds_path.'*')); $links=new Feed($feeds_path.'link.txt'); foreach (glob($tmp_feeds.'*') as $item) { $name_file=basename($item); rename($item, $feeds_path.$name_file); $links->new_string(home_url().'/feed/'.$name_file); } unset($links); delete_option('xml_current_product'); } ?>