|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Mark |
|
5
|
|
|
* Date: 22/05/2016 |
|
6
|
|
|
* Time: 07:50. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace App\Classes; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* This is the second version of our Sitemap Generation Class. |
|
13
|
|
|
* This allows you to store the data before returning and lets you |
|
14
|
|
|
* decide what you want to do with it, which allows you to create |
|
15
|
|
|
* for example, an array or xml as required. (greater control). |
|
16
|
|
|
* |
|
17
|
|
|
* Class SitemapGenerator |
|
18
|
|
|
*/ |
|
19
|
|
|
class SitemapGenerator |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Data for the sitemap to work upon. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
private $data = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Store a sample of data to the sitemap generator for usage later on. |
|
30
|
|
|
* |
|
31
|
|
|
* @param $url_location - The url where the item can be found or viewed. |
|
|
|
|
|
|
32
|
|
|
* @param null $last_modified - The date at which the piece was last modified. (allows model timestamps). |
|
33
|
|
|
* @param null $change_frequency - Define how often this should be checked by SEO. |
|
34
|
|
|
* @param null $priority - Priorty of importance that the piece is brought to SEO attention. |
|
35
|
|
|
*/ |
|
36
|
|
|
public function store($url_location, $last_modified = null, $change_frequency = null, $priority = null) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->data[] = new SitemapGeneratorDataObject($url_location, $last_modified, $change_frequency, $priority); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Turns the stored data into a readable and usable xml for SEO Optimization. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function generateXML() |
|
47
|
|
|
{ |
|
48
|
|
|
// headers for xml documentation type reading. |
|
49
|
|
|
$document = '<?xml version="1.0" encoding="UTF-8"?>'; |
|
50
|
|
|
|
|
51
|
|
|
$document .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">'; |
|
52
|
|
|
|
|
53
|
|
|
// create xml tags from the stored class data. |
|
54
|
|
|
foreach ($this->data as $data) { |
|
55
|
|
|
/* @var SitemapGeneratorDataObject $data */ |
|
56
|
|
|
$document .= '<url>'; |
|
57
|
|
|
|
|
58
|
|
|
$document .= $data->url_location ? '<loc>'.$data->url_location.'</loc>' : null; |
|
59
|
|
|
|
|
60
|
|
|
$document .= $data->last_modified ? '<lastmod>'.$data->last_modified.'</lastmod>' : null; |
|
61
|
|
|
|
|
62
|
|
|
$document .= $data->change_frequency ? '<changefreq>'.$data->change_frequency.'</changefreq>' : null; |
|
63
|
|
|
|
|
64
|
|
|
$document .= $data->priority ? '<priority>'.$data->priority.'</priority>' : null; |
|
65
|
|
|
|
|
66
|
|
|
$document .= '</url>'; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$document .= '</urlset>'; |
|
70
|
|
|
|
|
71
|
|
|
return $document; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Turns the stored data into a usable array in which can be used for user reading and browsing. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function generateArray() |
|
80
|
|
|
{ |
|
81
|
|
|
$document = []; |
|
82
|
|
|
|
|
83
|
|
|
/** @var SitemapGeneratorDataObject $data */ |
|
84
|
|
|
foreach ($this->data as $data) { |
|
85
|
|
|
$document[] = ['url_location'=>$data->url_location, 'last_modified'=>$data->last_modified, 'change_frequency'=>$data->change_frequency, 'priority'=>$data->priority]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $document; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Class SitemapGeneratorDataObject. |
|
94
|
|
|
*/ |
|
95
|
|
|
class SitemapGeneratorDataObject |
|
96
|
|
|
{ |
|
97
|
|
|
public $url_location; |
|
98
|
|
|
public $last_modified; |
|
99
|
|
|
public $change_frequency; |
|
100
|
|
|
public $priority; |
|
101
|
|
|
|
|
102
|
|
|
public function __construct($url_location, $last_modified, $change_frequency, $priority) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->url_location = $url_location; |
|
105
|
|
|
$this->last_modified = $last_modified->format('Y-m-d'); |
|
106
|
|
|
$this->change_frequency = $change_frequency; |
|
107
|
|
|
$this->priority = $priority; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|