File: //var/www/innodrive/cron/sitemap.php
<?php
define('CRON_ROOT', __DIR__ . '/');
define('SHORTINIT', false);
define( 'BASE_PATH', realpath(CRON_ROOT . '../public/') . '/' );
define('WP_USE_THEMES', false);
if( php_sapi_name() !== 'cli' ) {
die("Meant to be run from command line");
}
if (!empty($argv[1])) {
define('APPLICATION_ENV', $argv[1]);
} else {
define('APPLICATION_ENV', 'production');
}
require(CRON_ROOT . 'wp-mock-const.php');
require(BASE_PATH . 'wp-load.php');
require(BASE_PATH . 'wp-content/plugins/polylang/polylang.php');
require(BASE_PATH . 'wp-content/plugins/import/import.php');
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb;
$lang = new Polylang();
$lang->init();
$excludeList = ['compare', 'search'];
// Pages
$args = [
'exclude' => [4400, 4402],
'sort_order' => 'ASC',
'sort_column' => 'post_title',
'hierarchical' => 1,
'post_type' => 'page',
'post_status' => 'publish',
'lang' => 'ru',
];
$pages = get_pages( $args );
function fixUrl($url) {
return str_replace('http://', 'https://', $url);
}
$items = [];
foreach( $pages as $post ){
setup_postdata($post);
if (!in_array($post->post_name, $excludeList)) {
$item = [
'title' => $post->post_title,
'url' => fixUrl(get_the_permalink($post->ID)),
'slug' => $post->post_name,
'url2' => get_the_permalink(pll_get_post($post->ID, 'en')),
];
$items[] = $item;
}
wp_reset_postdata();
}
$args = [
'post_type' => 'news',
'lang' => 'ru',
];
$loop = new WP_Query( $args );
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$item = [
'title' => $post->post_title,
'url' => 'https://innodrive.ru/articles/news/' . $post->post_name,
'slug' => $post->post_name,
'url2' => get_the_permalink(pll_get_post($post->ID, 'en')),
];
$items[] = $item;
}
}
$args = [
'lang' => 'ru',
];
$loop = new WP_Query( $args );
if ($loop->have_posts()) {
while ($loop->have_posts()) {
$loop->the_post();
$item = [
'title' => $post->post_title,
'url' => fixUrl(get_the_permalink($post->ID)),
'slug' => $post->post_name,
'url2' => get_the_permalink(pll_get_post($post->ID, 'en')),
];
$items[] = $item;
}
}
$products = Product::getList();
if (!empty($products)) {
foreach($products as $product) {
$brand = Brand::get($product->brand_id);
$category = Category::get($product->category_id);
$linkParts = ['catalog'];
$linkParts[] = $brand->slug;
$linkParts[] = $category->slug;
$linkParts[] = $product->id;
$item = [
'title' => $post->post_title,
'url' => 'https://innodrive.ru/' . implode('/' , $linkParts) . '/',
'slug' => $post->post_name,
'url2' => 'https://innodrive.ru/en/' . implode('/' , $linkParts) . '/',
];
$items[] = $item;
}
}
ob_start();
echo '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
/** <xhtml:link rel="alternate" hreflang="en" href="<?= $item['url2']; ?>"/> **/
foreach($items as $item) {
?>
<url>
<loc><?= $item['url']; ?></loc>
<changefreq>weekly</changefreq>
<priority>1.0</priority>
</url>
<?php
}
echo '</urlset>';
$sitemapFile = ob_get_clean();
file_put_contents(BASE_PATH . 'sitemap.xml', $sitemapFile);
exit;