<?php

include_once '/vhosts/vetandlife.ru/wp-load.php';


date_default_timezone_set('Europe/Moscow');


//Яндекс Новости
create_rss_yandex();

//Дзен
//create_rss_dzen();


exit;


function create_rss_yandex(){

    $posts = get_posts( array(
    'numberposts' => 20,
    'orderby'     => 'date',
    'order'       => 'DESC',
    'include'     => array(),
    'exclude'     => array(),
    'post_type'   => 'post',
    'suppress_filters' => true,
    'meta_query'  => array(
        array(
            'key'     => '_send_to_zen_news',
            'value'   => '1',
            'compare' => '='
        )
    )
));

        
    // Яндекс Новости
    create_rss_file($posts, 'views/rss/yanews.php', '/vhosts/vetandlife.ru/rss/yanews.xml');
    
    // Общая лента
    create_rss_file($posts, 'views/rss/rssnews.php', '/vhosts/vetandlife.ru/rss/news.xml');

    //Турбо
    create_rss_file($posts, 'views/rss/yaturbo.php', '/vhosts/vetandlife.ru/rss/turbo.xml');

    return;


}



function create_rss_dzen(){

    $subcategories = get_categories(array(
        'child_of' => 14, // pets
    ));

    $subcategories_ids = array($parent_category_id);

    foreach ($subcategories as $subcategory) {
        $subcategories_ids[] = $subcategory->term_id;
    }

    $args = array(
        'category__in' => $subcategories_ids,
        'post_type' => 'post',
        'post_status' => 'publish',
        'posts_per_page' => 30,
        'orderby' => 'date',
        'order' => 'DESC',
    );

    $posts = get_posts($args);

    
    create_rss_file($posts, 'views/rss/dzen.php', '/vhosts/vetandlife.ru/rss/dzen.xml');
  
        
    return;

}


function create_rss_file($posts, $temp, $file){

    if( $posts ){

        ob_start();
            include($temp);
            $result = ob_get_contents();
        ob_end_clean();
    
        $result = trim($result);
    
        if ($result !== ''){
            file_put_contents($file, trim($result));
        }    
    
    
    }



}


function getMimeTypeFromExtension($imageUrl) {

    $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);

    switch (strtolower($extension)) {
        case 'jpg':
        case 'jpeg':
            return 'image/jpeg';
        case 'png':
            return 'image/png';
        case 'gif':
            return 'image/gif';
        case 'webp':
            return 'image/webp';
        default:
            return false; // Неизвестное расширение
    }

}

function filterHtml($html) {

    $html = trim($html);
    $html = preg_replace('/(\>)\s*(\<)/m', '$1$2', $html);
    return preg_replace('/<!--(.*?)-->/', '', $html);

}


function filtercontent($html){

    $content =  str_replace('<br /><br />', '</p><p>', $content);
	$content =  str_replace('<br />', '</p><p>', $content);
    $content =  preg_replace('/<\/p>\s*<p>/', '</p><p>', $content);
	$content =  str_replace('<strong>', '<b>', $content);
	$content =  str_replace('</strong>', '</b>', $content);
	$content =  str_replace('<b></b>', '', $content);
	$content =  str_replace('<p></p>', '', $content);
    $content = strip_tags($content, '<p><a><b><i><u><ul><ol><li><h1><h2><h3><h4><blockquote><iframe><img><figure>');

    return $content;
}


  
 
 
