/
class-kml-file.php
266 lines (229 loc) · 7.87 KB
/
class-kml-file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* The KML File.
*
* @since 1.0.24
* @package RankMath
* @subpackage RankMath\Local_Seo
* @author Rank Math <support@rankmath.com>
*/
namespace RankMath\Local_Seo;
use RankMath\Helper;
use RankMath\Traits\Ajax;
use RankMath\Traits\Hooker;
use RankMath\Helpers\Str;
use RankMath\Sitemap\Router;
use RankMath\Helpers\Param;
defined( 'ABSPATH' ) || exit;
/**
* KML_File class.
*/
class KML_File {
use Ajax, Hooker;
/**
* The Constructor.
*/
public function __construct() {
$this->action( 'init', 'init', 1 );
$this->filter( 'rank_math/sitemap/http_headers', 'remove_x_robots_tag' );
$this->filter( 'rank_math/sitemap/index', 'add_local_sitemap' );
$this->filter( 'rank_math/sitemap/local/content', 'local_sitemap_content' );
$this->filter( 'rank_math/sitemap/locations/content', 'kml_file_content' );
$this->action( 'cmb2_save_options-page_fields_rank-math-options-titles_options', 'update_sitemap', 25, 2 );
}
/**
* Set up rewrite rules.
*/
public function init() {
add_rewrite_rule( Router::get_sitemap_base() . 'locations\.kml$', 'index.php?sitemap=locations', 'top' );
}
/**
* Filter function to remove x-robots tag from Locations KML file.
*
* @param array $headers HTTP headers.
*/
public function remove_x_robots_tag( $headers ) {
if ( ! isset( $headers['X-Robots-Tag'] ) ) {
return $headers;
}
$url = array_filter( explode( '/', Param::server( 'REQUEST_URI' ) ) );
if ( 'locations.kml' !== end( $url ) ) {
return $headers;
}
unset( $headers['X-Robots-Tag'] );
return $headers;
}
/**
* Add the Local SEO Sitemap to the sitemap index.
*
* @param string $xml String to append to sitemaps index.
*
* @return string $xml The sitemap index with the Local SEO Sitemap added.
*/
public function add_local_sitemap( $xml ) {
$item = $this->do_filter(
'sitemap/index/entry',
[
'loc' => Router::get_base_url( 'local-sitemap.xml' ),
'lastmod' => $this->get_modified_date(),
],
'local',
);
if ( ! $item ) {
return $xml;
}
$xml .= $this->newline( '<sitemap>', 1 );
$xml .= $this->newline( '<loc>' . htmlspecialchars( $item['loc'] ) . '</loc>', 2 );
$xml .= empty( $item['lastmod'] ) ? '' : $this->newline( '<lastmod>' . htmlspecialchars( $item['lastmod'] ) . '</lastmod>', 2 );
$xml .= $this->newline( '</sitemap>', 1 );
return $xml;
}
/**
* The content of the Local SEO Sitemap.
*
* @return string $urlset Local SEO Sitemap XML content.
*/
public function local_sitemap_content() {
$item = $this->do_filter(
'sitemap/entry',
[
'loc' => Router::get_base_url( 'locations.kml' ),
'mod' => $this->get_modified_date(),
],
'local',
[]
);
if ( ! $item ) {
return '';
}
$output = $this->newline( '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">', 1 );
$output .= $this->newline( '<url>', 2 );
$output .= $this->newline( '<loc>' . htmlspecialchars( $item['loc'] ) . '</loc>', 3 );
$output .= empty( $item['mod'] ) ? '' : $this->newline( '<lastmod>' . htmlspecialchars( $item['mod'] ) . '</lastmod>', 3 );
$output .= $this->newline( '</url>', 2 );
$output .= $this->newline( '</urlset>', 1 );
return $output;
}
/**
* Generate the KML file contents.
*
* @return string $kml KML file content.
*/
public function kml_file_content() {
$locations = $this->get_local_seo_data();
if ( empty( $locations ) ) {
return;
}
$business_name = Helper::get_settings( 'titles.knowledgegraph_name' );
$business_url = Helper::get_settings( 'titles.url' );
$kml = $this->newline( '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">' );
$kml .= $this->newline( '<Document>', 1 );
$kml .= $this->newline( '<name>Locations for ' . esc_html( $business_name ) . '</name>', 2 );
$kml .= $this->newline( '<open>1</open>', 2 );
$kml .= $this->newline( '<Folder>', 2 );
if ( ! empty( $business_url ) ) {
$kml .= $this->newline( '<atom:link href="' . $business_url . '" />', 3 );
}
foreach ( $locations as $location ) {
$address = ! empty( $location['address'] ) ? Helper::replace_vars( implode( ', ', array_filter( $location['address'] ) ) ) : '';
$has_coord = ! empty( $location['coords']['latitude'] ) && ! empty( $location['coords']['longitude'] );
$kml .= $this->newline( '<Placemark>', 3 );
$kml .= $this->newline( '<name><![CDATA[' . html_entity_decode( $location['name'] ) . ']]></name>', 4 );
$kml .= $this->newline( '<description><![CDATA[' . html_entity_decode( $location['description'] ) . ']]></description>', 4 );
$kml .= $this->newline( '<address><![CDATA[' . $address . ']]></address>', 4 );
$kml .= $this->newline( '<phoneNumber><![CDATA[' . $location['phone'] . ']]></phoneNumber>', 4 );
$kml .= $this->newline( '<atom:link href="' . $location['url'] . '"/>', 4 );
$kml .= $this->newline( '<LookAt>', 4 );
if ( $has_coord ) {
$kml .= $this->newline( '<latitude>' . $location['coords']['latitude'] . '</latitude>', 5 );
$kml .= $this->newline( '<longitude>' . $location['coords']['longitude'] . '</longitude>', 5 );
}
$kml .= $this->newline( '<altitude>0</altitude>', 5 );
$kml .= $this->newline( '<range></range>', 5 );
$kml .= $this->newline( '<tilt>0</tilt>', 5 );
$kml .= $this->newline( '</LookAt>', 4 );
$kml .= $this->newline( '<Point>', 4 );
if ( $has_coord ) {
$kml .= $this->newline( '<coordinates>' . $location['coords']['longitude'] . ',' . $location['coords']['latitude'] . '</coordinates>', 5 );
}
$kml .= $this->newline( '</Point>', 4 );
$kml .= $this->newline( '</Placemark>', 3 );
}
$kml .= $this->newline( '</Folder>', 2 );
$kml .= $this->newline( '</Document>', 1 );
$kml .= $this->newline( '</kml>' );
return $kml;
}
/**
* Update the sitemap when the Local SEO settings are changed.
*
* @param int $object_id The ID of the current object.
* @param array $updated Array of field IDs that were updated.
* Will only include field IDs that had values change.
*/
public function update_sitemap( $object_id, $updated ) { // phpcs:ignore
$local_seo_fields = [
'knowledgegraph_name',
'url',
'email',
'local_address',
'local_business_type',
'opening_hours',
'phone_numbers',
'price_range',
'geo',
];
if ( count( array_intersect( $local_seo_fields, $updated ) ) ) {
update_option( 'rank_math_local_seo_update', date( 'c' ) );
}
}
/**
* Get the Local SEO data.
*
* @return array
*/
private function get_local_seo_data() {
$geo = Str::to_arr( Helper::get_settings( 'titles.geo' ) );
$cords = [
'latitude' => isset( $geo[0] ) ? $geo[0] : '',
'longitude' => isset( $geo[1] ) ? $geo[1] : '',
];
$phone_numbers = Helper::get_settings( 'titles.phone_numbers' );
$number = ! empty( $phone_numbers ) && isset( $phone_numbers[0]['number'] ) ? $phone_numbers[0]['number'] : '';
$locations = [
[
'name' => Helper::get_settings( 'titles.knowledgegraph_name' ),
'description' => get_option( 'blogname' ) . ' - ' . get_option( 'blogdescription' ),
'email' => Helper::get_settings( 'titles.email' ),
'phone' => $number,
'url' => Helper::get_settings( 'titles.url' ),
'address' => Helper::get_settings( 'titles.local_address' ),
'coords' => $cords,
'author' => get_option( 'blogname' ),
],
];
return $this->do_filter( 'sitemap/locations/data', $locations );
}
/**
* Get the Modified Date.
*
* @return $date
*/
private function get_modified_date() {
if ( ! $date = get_option( 'rank_math_local_seo_update' ) ) { // phpcs:ignore
$date = date( 'c' );
}
return $date;
}
/**
* Write a newline with indent count.
*
* @param string $content Content to write.
* @param integer $indent Count of indent.
*
* @return string
*/
private function newline( $content, $indent = 0 ) {
return str_repeat( "\t", $indent ) . $content . "\n";
}
}