Skip to content

Commit

Permalink
Merge pull request #2 from berkanumutlu/feature-2024-03-15
Browse files Browse the repository at this point in the history
Feature #1
  • Loading branch information
berkanumutlu committed Mar 21, 2024
2 parents 5151f13 + 849b219 commit 0bf516c
Show file tree
Hide file tree
Showing 9 changed files with 359 additions and 35 deletions.
75 changes: 73 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,83 @@

# PHP Sitemap Generator

This project can be used to generate sitemaps. It can build a sitemap file from a list of URLs. The URLs may have attached the last modification date, a change frequency and a priority.
This project can be used to generate sitemaps. It can build a sitemap file from a list of URLs. The URLs may have attached the last modification date, change frequency, priority and image properties.

Sitemap format: http://www.sitemaps.org/protocol.html

## Sitemap file

After creating your sitemap.xml file, you should add the XML file to your `robots.txt`.

Line for the robots.txt:

```
Sitemap: http://example.com/sitemap/sitemap.xml
```

## Output

Example output when generating a sitemap

```XML
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">
<!-- created with PHP Sitemap Generator by Berkan Ümütlü (https://github.com/berkanumutlu/php-sitemap-generator) -->
<url>
<loc>http://example.com/</loc>
<lastmod>2024-03-17</lastmod>
<priority>1</priority>
</url>
<url>
<loc>http://example.com/about-us</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
<image:image>
<image:loc>http://example.com/assets/images/pages/about-us.jpg</image:loc>
</image:image>
</url>
<url>
<loc>http://example.com/uber-uns</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>http://example.com/a-propos-de-nous</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>http://example.com/sobre-nosotros</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>http://example.com/o-nas</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>http://example.com/%D9%85%D8%B9%D9%84%D9%88%D9%85%D8%A7%D8%AA-%D8%B9%D9%86%D8%A7</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>http://example.com/chi-siamo</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
<url>
<loc>http://example.com/hakkimizda</loc>
<lastmod>2024-03-08</lastmod>
<priority>0.8</priority>
</url>
</urlset>
```

## Screenshots

![screenshot01](screenshots/screenshot01.png)
![screenshot01](screenshots/screenshot02.png)
![screenshot02](screenshots/screenshot02.png)

## License

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"require": {
"ext-json": "*",
"ext-pdo": "*"
"ext-pdo": "*",
"ext-mbstring": "*"
}
}
Binary file modified screenshots/screenshot01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified screenshots/screenshot02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 44 additions & 5 deletions src/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
$response = new \App\Library\Response();
$sitemap_generator = new SitemapGenerator();
try {
/*
* Setting form options
*/
$sitemap_generator->getSitemap()->setHttpSecure(!empty($_POST['http_secure']));
if (!empty($_POST['domain'])) {
$sitemap_generator->getSitemap()->setDomain(trim($_POST['domain']));
Expand All @@ -26,7 +29,14 @@
$sitemap_generator->getSitemap()->setFilePath(BASE_PATH.trim($_POST['file_path']));
}
if (!empty($_POST['file_name'])) {
$sitemap_generator->getSitemap()->setFileName(trim($_POST['file_name']));
$file_name = trim($_POST['file_name']);
if (!empty($_POST['file_name_unique'])) {
$file_name .= '-'.uniqid();
}
if (!empty($_POST['file_name_date'])) {
$file_name .= '-'.date('Y-m-d');
}
$sitemap_generator->getSitemap()->setFileName($file_name);
}
if (!empty($_POST['file_ext'])) {
$sitemap_generator->getSitemap()->setFileExt(trim($_POST['file_ext']));
Expand All @@ -40,26 +50,55 @@
if (!empty($_POST['file_urlset_footer'])) {
$sitemap_generator->getSitemap()->setUrlsetFooter(trim($_POST['file_urlset_footer']));
}
if (!empty($_POST['url_limit'])) {
$sitemap_generator->setUrlLimit(trim($_POST['url_limit']));
}
/*
* Adding base url
*/
$sitemap_generator->set_url_loc('');
$sitemap_generator->set_url_last_mod(date('Y-m-d'));
$sitemap_generator->set_url_priority(1);
$sitemap_generator->add_url_to_list();
/*
* Adding page urls
*/
$query_pages = $db->query("SELECT * from tbl_pages", PDO::FETCH_ASSOC);
if ($query_pages && $query_pages->rowCount()) {
$pages = $query_pages->fetchAll(PDO::FETCH_ASSOC);
foreach ($pages as $page) {
$sitemap_generator->set_url_loc($page['slug']);
$sitemap_generator->set_url_last_mod(!empty($page['updated_at']) ? $page['updated_at'] : $page['created_at']);
$sitemap_generator->set_url_loc(urlencode($page['slug']));
$date = !empty($page['updated_at']) ? $page['updated_at'] : $page['created_at'];
$sitemap_generator->set_url_last_mod(date('Y-m-d', strtotime($date)));
$sitemap_generator->set_url_priority(0.8);
if (!empty($page['image'])) {
$sitemap_generator->set_url_image_loc('assets/images/pages/'.urlencode($page['image']));
$sitemap_generator->set_url_image_title($page['name']);
}
$sitemap_generator->add_url_to_list();
}
}
/*
* Adding products urls
*/
$query_products = $db->query("SELECT * from tbl_products", PDO::FETCH_ASSOC);
if ($query_products && $query_products->rowCount()) {
$products = $query_products->fetchAll(PDO::FETCH_ASSOC);
foreach ($products as $product) {
$sitemap_generator->set_url_loc('product-detail/'.$product['slug']);
$sitemap_generator->set_url_last_mod(!empty($product['updated_at']) ? $product['updated_at'] : $product['created_at']);
$sitemap_generator->set_url_loc('product-detail/'.urlencode($product['slug']));
$date = !empty($product['updated_at']) ? $product['updated_at'] : $product['created_at'];
$sitemap_generator->set_url_last_mod(date('Y-m-d', strtotime($date)));
$sitemap_generator->set_url_priority(1);
if (!empty($product['image'])) {
$sitemap_generator->set_url_image_loc('assets/images/products/'.urlencode($product['image']));
$sitemap_generator->set_url_image_title($product['name']);
}
$sitemap_generator->add_url_to_list();
}
}
/*
* Generating sitemap
*/
$response = $sitemap_generator->generate();
} catch (\Exception $e) {
$response->setStatus(false);
Expand Down
Loading

0 comments on commit 0bf516c

Please sign in to comment.