6
6
*/
7
7
8
8
class Joost_XML_Sitemap_PHP {
9
- /**
10
- * Holds our configuration.
11
- */
12
- private array $ config = [];
9
+ const CONFIG_FILE = 'xml-sitemap-config.ini ' ;
13
10
14
11
/**
15
12
* Holds the output.
16
13
*/
17
14
private string $ output = '' ;
18
15
19
16
/**
20
- * Class constructor.
17
+ * Holds the path.
18
+ */
19
+ private string $ path ;
20
+
21
+ /**
22
+ * Holds the base URL.
23
+ */
24
+ private string $ url ;
25
+
26
+ /**
27
+ * Array of filetypes we're adding to the XML sitemap.
28
+ */
29
+ private array $ filetypes ;
30
+
31
+ /**
32
+ * Relative path to the XSL file.
33
+ */
34
+ private string $ xsl = 'xml-sitemap.xsl ' ;
35
+
36
+ /**
37
+ * Files we ignore in our output.
38
+ */
39
+ private array $ ignore = [];
40
+
41
+ /**
42
+ * The change frequency of the files.
21
43
*/
22
- public function __construct () {
44
+ private string $ changefreq = '' ;
45
+
46
+ /**
47
+ * The priority of the file, between 0.1 and 1.
48
+ */
49
+ private float $ priority = 0 ;
50
+
51
+ /**
52
+ * Determines if we recurse directories or not.
53
+ */
54
+ private bool $ recursive = true ;
55
+
56
+ /**
57
+ * URLs we replace with something else.
58
+ */
59
+ private array $ replacements ;
60
+
61
+ /**
62
+ * Generates our XML sitemap.
63
+ *
64
+ * @return void
65
+ */
66
+ public function generate (): void {
23
67
$ this ->read_ini ();
24
68
$ this ->read_dir ();
25
69
$ this ->output ();
@@ -30,44 +74,34 @@ public function __construct() {
30
74
*
31
75
* @return void
32
76
*/
33
- private function read_ini () {
34
- if ( file_exists ( './xml-sitemap-config.ini ' ) ) {
35
- $ this ->config = parse_ini_file ( './xml-sitemap-config.ini ' );
36
- }
37
- else if ( file_exists ( '../xml-sitemap-config.ini ' ) ) {
38
- $ this ->config = parse_ini_file ( '../xml-sitemap-config.ini ' );
39
- }
40
- else {
41
- echo "Error: unable to load config.php, please copy config-sample.php to config.php and adjust. " . PHP_EOL ;
77
+ private function read_ini (): void {
78
+ if ( file_exists ( './ ' . self ::CONFIG_FILE ) ) {
79
+ $ config = parse_ini_file ( './ ' . self ::CONFIG_FILE );
80
+ } elseif ( file_exists ( '../ ' . self ::CONFIG_FILE ) ) {
81
+ $ config = parse_ini_file ( '../ ' . self ::CONFIG_FILE );
82
+ } else {
83
+ printf ( 'Error: unable to load %1$s, please copy xml-sitemap-config-sample.ini to %1$s and adjust. ' . PHP_EOL , self ::CONFIG_FILE );
42
84
die ( 1 );
43
85
}
44
- }
45
86
46
- /**
47
- * Kick off the script reading the dir provided in config.
48
- *
49
- * @return void
50
- */
51
- private function read_dir () {
52
- $ this ->parse_dir ( $ this ->config ['directory ' ], $ this ->config ['directory_url ' ] );
87
+ $ this ->changefreq = (string ) $ config ['changefreq ' ];
88
+ $ this ->path = (string ) $ config ['directory ' ];
89
+ $ this ->url = (string ) $ config ['directory_url ' ];
90
+ $ this ->filetypes = (array ) $ config ['filetypes ' ];
91
+ $ this ->ignore = array_merge ( $ config ['ignore ' ], [ '. ' , '.. ' , 'xml-sitemap.php ' ] );
92
+ $ this ->priority = (float ) $ config ['priority ' ];
93
+ $ this ->recursive = (bool ) $ config ['recursive ' ];
94
+ $ this ->replacements = (array ) $ config ['replacements ' ];
95
+ $ this ->xsl = (string ) $ config ['xsl ' ];
53
96
}
54
97
55
98
/**
56
- * Output our XML sitemap .
99
+ * Kick off the script reading the dir provided in config .
57
100
*
58
101
* @return void
59
102
*/
60
- private function output () {
61
- // Sent the correct header so browsers display properly, with or without XSL.
62
- header ( 'Content-Type: application/xml ' );
63
-
64
- echo '<?xml version="1.0" encoding="utf-8"?> ' . PHP_EOL ;
65
- if ( isset ( $ this ->config ['xsl ' ] ) && ! empty ( $ this ->config ['xsl ' ] ) ) {
66
- echo '<?xml-stylesheet type="text/xsl" href=" ' . $ this ->config ['directory_url ' ] . $ this ->config ['xsl ' ] . '"?> ' . PHP_EOL ;
67
- }
68
- echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"> ' . PHP_EOL ;
69
- echo $ this ->output ;
70
- echo '</urlset> ' . PHP_EOL ;
103
+ private function read_dir (): void {
104
+ $ this ->parse_dir ( $ this ->path , $ this ->url );
71
105
}
72
106
73
107
/**
@@ -78,25 +112,22 @@ private function output() {
78
112
*
79
113
* @return void
80
114
*/
81
- private function parse_dir ( string $ dir , string $ url ) {
82
- $ ignore = array_merge ( $ this ->config ['ignore ' ], array ( '. ' , '.. ' , 'xml-sitemap.php ' ) );
83
- $ filetypes = $ this ->config ['filetypes ' ];
84
-
115
+ private function parse_dir ( string $ dir , string $ url ): void {
85
116
$ handle = opendir ( $ dir );
86
117
87
118
while ( false !== ( $ file = readdir ( $ handle ) ) ) {
88
119
// Check if this file needs to be ignored, if so, skip it.
89
- if ( in_array ( utf8_encode ( $ file ), $ ignore ) ) {
120
+ if ( in_array ( utf8_encode ( $ file ), $ this -> ignore ) ) {
90
121
continue ;
91
122
}
92
123
93
- if ( $ this ->config [ ' recursive ' ] && is_dir ( $ dir . $ file ) ) {
124
+ if ( $ this ->recursive && is_dir ( $ dir . $ file ) ) {
94
125
$ this ->parse_dir ( $ dir . $ file . '/ ' , $ url . $ file . '/ ' );
95
126
}
96
127
97
128
// Check whether the file has on of the extensions allowed for this XML sitemap.
98
129
$ extension = pathinfo ( $ dir . $ file , PATHINFO_EXTENSION );
99
- if ( empty ( $ extension ) || ! in_array ( $ extension , $ filetypes ) ) {
130
+ if ( empty ( $ extension ) || ! in_array ( $ extension , $ this -> filetypes ) ) {
100
131
continue ;
101
132
}
102
133
@@ -109,19 +140,19 @@ private function parse_dir( string $dir, string $url ) {
109
140
$ mod = date ( 'c ' , $ file_mod_time );
110
141
111
142
// Replace the file with its replacement from the settings, if needed.
112
- if ( isset ( $ this ->config [ ' replacements ' ] [ $ file ] ) ) {
113
- $ file = $ this ->config [ ' replacements ' ] [ $ file ];
143
+ if ( isset ( $ this ->replacements [ $ file ] ) ) {
144
+ $ file = $ this ->replacements [ $ file ];
114
145
}
115
146
116
147
// Start creating the output
117
- $ output = '<url> ' . PHP_EOL ;
118
- $ output .= "\t" . '<loc> ' . $ url . rawurlencode ( $ file ) . '</loc> ' . PHP_EOL ;
119
- $ output .= "\t" . '<lastmod> ' . $ mod . '</lastmod> ' . PHP_EOL ;
120
- if ( ! empty ( $ this ->config [ ' changefreq ' ] ) ) {
121
- $ output .= "\t" . '<changefreq> ' . $ this ->config [ ' changefreq ' ] . '</changefreq> ' . PHP_EOL ;
148
+ $ output = '<url> ' . PHP_EOL ;
149
+ $ output .= "\t" . '<loc> ' . $ url . rawurlencode ( $ file ) . '</loc> ' . PHP_EOL ;
150
+ $ output .= "\t" . '<lastmod> ' . $ mod . '</lastmod> ' . PHP_EOL ;
151
+ if ( ! empty ( $ this ->changefreq ) ) {
152
+ $ output .= "\t" . '<changefreq> ' . $ this ->changefreq . '</changefreq> ' . PHP_EOL ;
122
153
}
123
- if ( ! empty ( $ this ->config [ ' priority ' ] ) ) {
124
- $ output .= "\t" . '<priority> ' . $ this ->config [ ' priority ' ] . '</priority> ' . PHP_EOL ;
154
+ if ( $ this -> priority !== 0 && $ this ->priority <= 1 ) {
155
+ $ output .= "\t" . '<priority> ' . $ this ->priority . '</priority> ' . PHP_EOL ;
125
156
}
126
157
$ output .= '</url> ' . PHP_EOL ;
127
158
@@ -130,6 +161,25 @@ private function parse_dir( string $dir, string $url ) {
130
161
}
131
162
closedir ( $ handle );
132
163
}
164
+
165
+ /**
166
+ * Output our XML sitemap.
167
+ *
168
+ * @return void
169
+ */
170
+ private function output (): void {
171
+ // Sent the correct header so browsers display properly, with or without XSL.
172
+ header ( 'Content-Type: application/xml ' );
173
+
174
+ echo '<?xml version="1.0" encoding="utf-8"?> ' . PHP_EOL ;
175
+ if ( ! empty ( $ this ->xsl ) ) {
176
+ echo '<?xml-stylesheet type="text/xsl" href=" ' . $ this ->url . $ this ->xsl . '"?> ' . PHP_EOL ;
177
+ }
178
+ echo '<urlset xmlns="https://www.sitemaps.org/schemas/sitemap/0.9"> ' . PHP_EOL ;
179
+ echo $ this ->output ;
180
+ echo '</urlset> ' . PHP_EOL ;
181
+ }
133
182
}
134
183
135
- new Joost_XML_Sitemap_PHP ();
184
+ $ joost_xml = new Joost_XML_Sitemap_PHP ();
185
+ $ joost_xml ->generate ();
0 commit comments