File: /var/www/elite/sitemap.php
<?php
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');
}
define('BASE_PATH', __DIR__ . '/public/');
define('WP_USE_THEMES', false);
define('WP_SITEURL', 'https://limestate.ru');
define('WP_HOME', 'https://limestate.ru');
define('WP_CONTENT_DIR', BASE_PATH . 'wp-content/');
if (!is_dir(BASE_PATH . '/sitemaps')) {
mkdir(BASE_PATH . '/sitemaps', 0777);
}
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header, $wpdb;
require(BASE_PATH . 'wp-load.php');
function addTrailingSlash($path)
{
return rtrim($path, '/') . '/';
}
$maps = [];
// HOME PAGE
$maps['main'] = [
[
'url' => WP_HOME,
'lastmod' => date('Y-m-d'),
'changefreq' => 'daily',
'priority' => 1,
],
[
'url' => WP_HOME . '/materials/',
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.5,
],
];
$counter = 2;
echo $counter . ' - hardcoded items' . PHP_EOL;
// MENU ITEMS
$items = wp_get_nav_menu_items('Top Menu');
$counter = 0;
foreach ($items as $item) {
if (empty($tree[$item->ID]) && $item->url !== '#') {
$maps['main'][] = [
'url' => addTrailingSlash($item->url),
'lastmod' => date('Y-m-d', strtotime($item->post_modified)),
'changefreq' => $item->post_name == 39 ? 'daily' : 'weekly',
'priority' => $item->post_name == 39 ? 0.7 : 0.5,
];
echo addTrailingSlash($item->url) . PHP_EOL;
$counter++;
}
}
echo $counter . ' - menu items' . PHP_EOL;
// LANDING PAGES
$counter = 0;
$seo = get_field('seo', HOME_PAGE_ID);
$maps['buy'] = [];
if (!empty($seo)) {
foreach ($seo as $column) {
if (!empty($column['links'])) {
foreach ($column['links'] as $link) {
$maps['buy'][] = [
'url' => addTrailingSlash(WP_HOME . '/buy/' . $link['slug'] . '/'),
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.7,
];
echo addTrailingSlash(WP_HOME . '/buy/' . $link['slug'] . '/') . PHP_EOL;
$counter++;
}
}
}
}
echo $counter . ' - seo footer pages' . PHP_EOL;
$seo = get_field('seolinks', BUY_PAGE_ID);
$counter = 0;
if (!empty($seo)) {
foreach ($seo as $link) {
if (empty($link['params'])) {
continue;
}
$maps['buy'][] = [
'url' => addTrailingSlash(WP_HOME . '/buy/' . $link['slug'] . '/'),
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.7,
];
echo addTrailingSlash(WP_HOME . '/buy/' . $link['slug'] . '/') . PHP_EOL;
$counter++;
}
}
echo $counter . ' - seo buy pages' . PHP_EOL;
// Custom Complex pages
$args = [
'post_type' => 'page',
'post_parent__in' => [COMPLEX_PAGE_ID],
'posts_per_page' => -1,
];
$loop = new WP_Query($args);
$counter = 0;
$maps['complexes'] = [];
while ($loop->have_posts()) {
$loop->the_post();
$complexId = get_field('complex_id', $post->ID);
$complex = getComplexData($complexId, true);
if (empty($complex)) {
continue;
}
$maps['complexes'][] = [
'url' => addTrailingSlash(get_the_permalink($post->ID)),
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.7,
];
echo $post->ID . ' ' . addTrailingSlash(get_the_permalink($post->ID)) . PHP_EOL;
$counter++;
}
echo $counter . ' - custom complex pages' . PHP_EOL;
// Custom buy pages
$args = [
'post_type' => 'page',
'post_parent__in' => [BUY_PAGE_ID, DISTRICT_PAGE_ID],
'post__not_in' => [DISTRICT_PAGE_ID, COMPLEX_PAGE_ID, PRINT_PAGE_ID, SE_FLAT_PAGE_ID, FLAT_PAGE_ID],
'posts_per_page' => -1,
];
$loop = new WP_Query($args);
$counter = 0;
while ($loop->have_posts()) {
$loop->the_post();
$maps['buy'][] = [
'url' => addTrailingSlash(get_the_permalink($post->ID)),
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.7,
];
echo $post->ID . ' ' . addTrailingSlash(get_the_permalink($post->ID)) . PHP_EOL;
$counter++;
}
echo $counter . ' - custom buy pages' . PHP_EOL;
// NEWS&ARTICLES PAGES
$args = [
'post_type' => 'materials',
'posts_per_page' => -1,
];
$loop = new WP_Query($args);
$counter = 0;
$maps['articles'] = [];
while ($loop->have_posts()) {
$loop->the_post();
$maps['articles'][] = [
'url' => addTrailingSlash(get_the_permalink()),
'lastmod' => date('Y-m-d', strtotime($post->post_modified)),
'changefreq' => 'weekly',
'priority' => 0.5,
];
echo get_the_ID() . ' ' . addTrailingSlash(get_the_permalink()) . PHP_EOL;
$counter++;
}
echo $counter . ' - neww & articles' . PHP_EOL;
$maps['flats'] = [];
// COMPLEX
$complexes = (array) getComplexesData();
$counter = 0;
foreach ($complexes as $complex) {
$complexData = getComplexData($complex->id, true);
$complexPage = getComplexPageById($complex->id);
$url = !empty($complexPage) ? mb_strtolower(get_the_permalink($complexPage->ID)) : WP_HOME . '/buy/complex/' . (!empty($complex->alias) ? mb_strtolower($complex->alias) : $complex->id);
$maps['complexes'][] = [
'url' => addTrailingSlash($url),
'lastmod' => date('Y-m-d'),
'changefreq' => 'daily',
'priority' => 0.8,
];
echo addTrailingSlash($url) . PHP_EOL;
$counter++;
$num = 1;
foreach ($complexData->flatsStarters as $key => $row) {
$roomPageUrl = addTrailingSlash($url) . ($row->rooms == 0 ? 'studii' : $row->rooms . plural($row->rooms, ['-komnat', '-komnata', '-komnati'])) . '/';
echo ' . ' . addTrailingSlash($roomPageUrl) . PHP_EOL;
$maps['complexes'][] = [
'url' => addTrailingSlash($roomPageUrl),
'lastmod' => date('Y-m-d'),
'changefreq' => 'daily',
'priority' => 0.8,
];
$num++;
}
$offset = 0;
$limit = 50;
while ($offset == 0 || (isset($flats->data) && $flats->data->total > $offset)) {
$flats = fetchFlatResults([
'complex_id' => $complex->id,
'offset' => $offset,
'limit' => $limit,
]);
$offset += $limit;
foreach ($flats->data->flats as $flat) {
$flatPageUrl = addTrailingSlash($url) . $flat->id;
echo ' ... ' . addTrailingSlash($flatPageUrl) . PHP_EOL;
$maps['flats'][] = [
'url' => addTrailingSlash($flatPageUrl),
'lastmod' => date('Y-m-d'),
'changefreq' => 'daily',
'priority' => 0.8,
];
}
}
}
echo $counter . ' - complexes & rooms page' . PHP_EOL;
// SE FLATS
$flats = fetchFlatResults([
'category_id' => 2,
'offset' => 0,
'limit' => 1000,
]);
$counter = 0;
foreach ($flats->data->flats as $flat) {
$seFlatPageUrl = WP_HOME . '/buy/flat/' . $flat->id . PHP_EOL;
echo ' ... ' . addTrailingSlash($seFlatPageUrl) . PHP_EOL;
$maps['flats'][] = [
'url' => addTrailingSlash($seFlatPageUrl),
'lastmod' => date('Y-m-d'),
'changefreq' => 'daily',
'priority' => 0.8,
];
$counter++;
}
echo $counter . ' - se flats' . PHP_EOL;
// DISTRICTS
$districts = getBestDistrictsData(true);
$counter = 0;
$maps['districts'] = [];
foreach ($districts as $district) {
if (empty($district->alias)) {
continue;
}
$maps['districts'][] = [
'url' => addTrailingSlash(WP_HOME . '/buy/district-' . mb_strtolower($district->alias)),
'lastmod' => date('Y-m-d'),
'changefreq' => 'weekly',
'priority' => 0.5,
];
echo addTrailingSlash(WP_HOME . '/buy/district-' . mb_strtolower($district->alias)) . PHP_EOL;
$counter++;
}
echo $counter . ' - districts' . PHP_EOL;
// SITEMAP OUTPUT
$setArray = [];
$counter = 0;
foreach ($maps as $type => $map) {
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$xml .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
foreach ($map as $line) {
if (in_array($line['url'], $setArray)) {
//echo ' - ' . $line['url'] . PHP_EOL;
$counter++;
continue;
} // avoid duplicates;
$xml .= '<url>' . PHP_EOL;
$url = strtolower($line['url']);
$xml .= "<loc>{$url}</loc>" . PHP_EOL;
$xml .= "<lastmod>{$line['lastmod']}</lastmod>" . PHP_EOL;
$xml .= "<changefreq>{$line['changefreq']}</changefreq>" . PHP_EOL;
$xml .= "<priority>{$line['priority']}</priority>" . PHP_EOL;
$xml .= '</url>' . PHP_EOL;
$setArray[] = $line['url'];
}
$xml .= '</urlset>';
file_put_contents(BASE_PATH . '/sitemaps/' . $type . '-sitemap.xml', $xml);
echo $counter . ' - duplicates excluded' . PHP_EOL;
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
$xml .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . PHP_EOL;
foreach ($maps as $type => $map) {
$xml .= '<sitemap><loc>' . WP_SITEURL . '/sitemaps/' . $type . '-sitemap.xml</loc></sitemap>';
}
$xml .= '</sitemapindex>';
file_put_contents(BASE_PATH . '/sitemap.xml', $xml);
echo 'complete' . PHP_EOL;