Completed
Push — prepare/4.1 ( 0b4dc2...26fe0a )
by Jeremy
279:11 queued 269:16
created

sitemaps.php ➔ jetpack_sitemap_initstr()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 22
rs 8.9197
1
<?php
2
/**
3
 * Generate sitemap files in base XML as well as popular namespace extensions.
4
 *
5
 * @author Automattic
6
 * @link http://sitemaps.org/protocol.php Base sitemaps protocol.
7
 * @link http://www.google.com/support/webmasters/bin/answer.py?answer=74288 Google news sitemaps.
8
 */
9
10
11
/**
12
 * Convert a MySQL datetime string to an ISO 8601 string.
13
 *
14
 * @module sitemaps
15
 *
16
 * @link http://www.w3.org/TR/NOTE-datetime W3C date and time formats document.
17
 *
18
 * @param string $mysql_date UTC datetime in MySQL syntax of YYYY-MM-DD HH:MM:SS.
19
 *
20
 * @return string ISO 8601 UTC datetime string formatted as YYYY-MM-DDThh:mm:ssTZD where timezone offset is always +00:00.
21
 */
22
function jetpack_w3cdate_from_mysql( $mysql_date ) {
23
	return str_replace( ' ', 'T', $mysql_date ) . '+00:00';
24
}
25
26
/**
27
 * Get the maximum comment_date_gmt value for approved comments for the given post_id.
28
 *
29
 * @module sitemaps
30
 *
31
 * @param int $post_id Post identifier.
32
 *
33
 * @return string datetime MySQL value or null if no comment found.
34
 */
35
function jetpack_get_approved_comments_max_datetime( $post_id ) {
36
	global $wpdb;
37
38
	return $wpdb->get_var( $wpdb->prepare( "SELECT MAX(comment_date_gmt) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type=''", $post_id ) );
39
}
40
41
/**
42
 * Return the content type used to serve a Sitemap XML file.
43
 * Uses text/xml by default, possibly overridden by jetpack_sitemap_content_type filter.
44
 *
45
 * @module sitemaps
46
 *
47
 * @return string Internet media type for the sitemap XML.
48
 */
49
function jetpack_sitemap_content_type() {
50
	/**
51
	 * Filter the content type used to serve the XML sitemap file.
52
	 *
53
	 * @module sitemaps
54
	 *
55
	 * @since 3.9.0
56
	 *
57
	 * @param string $content_type By default, it's 'text/xml'.
58
	 */
59
	return apply_filters( 'jetpack_sitemap_content_type', 'text/xml' );
60
}
61
62
/**
63
 * Write an XML tag.
64
 *
65
 * @module sitemaps
66
 *
67
 * @param array $data Information to write an XML tag.
68
 */
69
function jetpack_print_sitemap_item( $data ) {
70
	jetpack_print_xml_tag( array( 'url' => $data ) );
71
}
72
73
/**
74
 * Write an opening tag and its matching closing tag.
75
 *
76
 * @module sitemaps
77
 *
78
 * @param array $array Information to write a tag, opening and closing it.
79
 */
80
function jetpack_print_xml_tag( $array ) {
81
	foreach ( $array as $key => $value ) {
82
		if ( is_array( $value ) ) {
83
			echo "<$key>";
84
			jetpack_print_xml_tag( $value );
85
			echo "</$key>";
86
		} else {
87
			echo "<$key>" . esc_html( $value ) . "</$key>";
88
		}
89
	}
90
}
91
92
/**
93
 * Convert an array to a SimpleXML child of the passed tree.
94
 *
95
 * @module sitemaps
96
 *
97
 * @param array $data array containing element value pairs, including other arrays, for XML contruction.
98
 * @param SimpleXMLElement $tree A SimpleXMLElement class object used to attach new children.
99
 *
100
 * @return SimpleXMLElement full tree with new children mapped from array.
101
 */
102
function jetpack_sitemap_array_to_simplexml( $data, &$tree ) {
103
	$doc_namespaces = $tree->getDocNamespaces();
104
105
	foreach ( $data as $key => $value ) {
106
		// Allow namespaced keys by use of colon in $key, namespaces must be part of the document
107
		$namespace = null;
108
		if ( false !== strpos( $key, ':' ) && 'image' != $key ) {
109
			list( $namespace_prefix, $key ) = explode( ':', $key );
110
			if ( isset( $doc_namespaces[ $namespace_prefix ] ) ) {
111
				$namespace = $doc_namespaces[ $namespace_prefix ];
112
			}
113
		}
114
115
		if ( 'image' != $key ) {
116
			if ( is_array( $value ) ) {
117
				$child = $tree->addChild( $key, null, $namespace );
118
				jetpack_sitemap_array_to_simplexml( $value, $child );
119
			} else {
120
				$tree->addChild( $key, esc_html( $value ), $namespace );
121
			}
122
		} elseif ( is_array( $value ) ) {
123
			foreach ( $value as $image ) {
124
				$child = $tree->addChild( $key, null, $namespace );
125
				jetpack_sitemap_array_to_simplexml( $image, $child );
126
			}
127
		}
128
	}
129
130
	return $tree;
131
}
132
133
/**
134
 * Define an array of attribute value pairs for use inside the root element of an XML document.
135
 * Intended for mapping namespace and namespace URI values.
136
 * Passes array through jetpack_sitemap_ns for other functions to add their own namespaces.
137
 *
138
 * @module sitemaps
139
 *
140
 * @return array array of attribute value pairs passed through the jetpack_sitemap_ns filter
141
 */
142
function jetpack_sitemap_namespaces() {
143
	/**
144
	 * Filter the attribute value pairs used for namespace and namespace URI mappings.
145
	 *
146
	 * @module sitemaps
147
	 *
148
	 * @since 3.9.0
149
	 *
150
	 * @param array $namespaces Associative array with namespaces and namespace URIs.
151
	 */
152
	return apply_filters( 'jetpack_sitemap_ns', array(
153
		'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
154
		'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
155
		'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
156
		// Mobile namespace from http://support.google.com/webmasters/bin/answer.py?hl=en&answer=34648
157
		'xmlns:mobile'       => 'http://www.google.com/schemas/sitemap-mobile/1.0',
158
		'xmlns:image'        => 'http://www.google.com/schemas/sitemap-image/1.1',
159
	) );
160
}
161
162
/**
163
 * Start sitemap XML document, writing its heading and <urlset> tag with namespaces.
164
 *
165
 * @module sitemaps
166
 *
167
 * @param $charset string Charset for current XML document.
168
 *
169
 * @return string
170
 */
171
function jetpack_sitemap_initstr( $charset ) {
172
	global $wp_rewrite;
173
	// URL to XSLT
174
	if ( $wp_rewrite->using_index_permalinks() ) {
175
		$xsl = home_url( '/index.php/sitemap.xsl' );
176
	} else if ( $wp_rewrite->using_permalinks() ) {
177
		$xsl = home_url( '/sitemap.xsl' );
178
	} else {
179
		$xsl = home_url( '/?jetpack-sitemap-xsl=true' );
180
	}
181
182
	$initstr = '<?xml version="1.0" encoding="' . $charset . '"?>' . "\n";
183
	$initstr .= '<?xml-stylesheet type="text/xsl" href="' . esc_url( $xsl ) . '"?>' . "\n";
184
	$initstr .= '<!-- generator="jetpack-' . JETPACK__VERSION . '" -->' . "\n";
185
	$initstr .= '<urlset';
186
	foreach ( jetpack_sitemap_namespaces() as $attribute => $value ) {
187
		$initstr .= ' ' . esc_html( $attribute ) . '="' . esc_attr( $value ) . '"';
188
	}
189
	$initstr .= ' />';
190
191
	return $initstr;
192
}
193
194
/**
195
 * Load XSLT for sitemap.
196
 *
197
 * @module sitemaps
198
 *
199
 * @param string $type XSLT to load.
200
 */
201
function jetpack_load_xsl( $type = '' ) {
202
203
	$transient_xsl = empty( $type ) ? 'jetpack_sitemap_xsl' : "jetpack_{$type}_sitemap_xsl";
204
205
	$xsl = get_transient( $transient_xsl );
206
207
	if ( $xsl ) {
208
		header( 'Content-Type: ' . jetpack_sitemap_content_type(), true );
209
		echo $xsl;
210
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_load_xsl() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
211
	}
212
213
	// Populate $xsl. Use $type.
214
	include_once JETPACK__PLUGIN_DIR . 'modules/sitemaps/sitemap-xsl.php';
215
216
	if ( ! empty( $xsl ) ) {
217
		set_transient( $transient_xsl, $xsl, DAY_IN_SECONDS );
218
		echo $xsl;
219
	}
220
221
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_load_xsl() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
222
}
223
224
/**
225
 * Responds with an XSLT to stylize sitemap.
226
 *
227
 * @module sitemaps
228
 */
229
function jetpack_print_sitemap_xsl() {
230
	jetpack_load_xsl();
231
}
232
233
/**
234
 * Responds with an XSLT to stylize news sitemap.
235
 *
236
 * @module sitemaps
237
 */
238
function jetpack_print_news_sitemap_xsl() {
239
	jetpack_load_xsl( 'news' );
240
}
241
242
/**
243
 * Print an XML sitemap conforming to the Sitemaps.org protocol.
244
 * Outputs an XML list of up to the latest 1000 posts.
245
 *
246
 * @module sitemaps
247
 *
248
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
249
 */
250
function jetpack_print_sitemap() {
251
	global $wpdb, $post;
252
253
	$xml = get_transient( 'jetpack_sitemap' );
254
255
	if ( $xml ) {
256
		header( 'Content-Type: ' . jetpack_sitemap_content_type(), true );
257
		echo $xml;
258
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_print_sitemap() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
259
	}
260
261
	// Compatibility with PHP 5.3 and older
262
	if ( ! defined( 'ENT_XML1' ) ) {
263
		define( 'ENT_XML1', 16 );
264
	}
265
266
	/**
267
	 * Filter the post types that will be included in sitemap.
268
	 *
269
	 * @module sitemaps
270
	 *
271
	 * @since 3.9.0
272
	 *
273
	 * @param array $post_types Array of post types.
274
	 */
275
	$post_types    = apply_filters( 'jetpack_sitemap_post_types', array( 'post', 'page' ) );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 4 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
276
277
	$post_types_in = array();
278
	foreach ( (array) $post_types as $post_type ) {
279
		$post_types_in[] = $wpdb->prepare( '%s', $post_type );
280
	}
281
	$post_types_in = join( ",", $post_types_in );
282
283
	// use direct query instead because get_posts was acting too heavy for our needs
284
	//$posts = get_posts( array( 'numberposts'=>1000, 'post_type'=>$post_types, 'post_status'=>'published' ) );
285
	$posts = $wpdb->get_results( "SELECT ID, post_type, post_modified_gmt, comment_count FROM $wpdb->posts WHERE post_status='publish' AND post_type IN ({$post_types_in}) ORDER BY post_modified_gmt DESC LIMIT 1000" );
286
	if ( empty( $posts ) ) {
287
		status_header( 404 );
288
	}
289
	header( 'Content-Type: ' . jetpack_sitemap_content_type() );
290
	$initstr = jetpack_sitemap_initstr( get_bloginfo( 'charset' ) );
291
	$tree    = simplexml_load_string( $initstr );
292
	// If we did not get a valid string, force UTF-8 and try again.
293
	if ( false === $tree ) {
294
		$initstr = jetpack_sitemap_initstr( 'UTF-8' );
295
		$tree    = simplexml_load_string( $initstr );
296
	}
297
298
	unset( $initstr );
299
	$latest_mod = '';
300
	foreach ( $posts as $post ) {
301
		setup_postdata( $post );
302
303
		/**
304
		 * Filter condition to allow skipping specific posts in sitemap.
305
		 *
306
		 * @module sitemaps
307
		 *
308
		 * @since 3.9.0
309
		 *
310
		 * @param bool $skip Current boolean. False by default, so no post is skipped.
311
		 * @param WP_POST $post Current post object.
312
		 */
313
		if ( apply_filters( 'jetpack_sitemap_skip_post', false, $post ) ) {
314
			continue;
315
		}
316
317
		$post_latest_mod = null;
318
		$url             = array( 'loc' => esc_url( get_permalink( $post->ID ) ) );
319
320
		// If this post is configured to be the site home, skip since it's added separately later
321
		if ( untrailingslashit( get_permalink( $post->ID ) ) == untrailingslashit( get_option( 'home' ) ) ) {
322
			continue;
323
		}
324
325
		// Mobile node specified in http://support.google.com/webmasters/bin/answer.py?hl=en&answer=34648
326
		$url['mobile:mobile'] = '';
327
328
		// Image node specified in http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636
329
		// These attachments were produced with batch SQL earlier in the script
330
		if ( ! post_password_required( $post->ID ) ) {
331
332
			$media = array();
333
			$methods = array(
334
				'from_thumbnail'  => false,
335
				'from_slideshow'  => false,
336
				'from_gallery'    => false,
337
				'from_attachment' => false,
338
				'from_html'       => false,
339
			);
340
			foreach ( $methods as $method => $value ) {
341
				$methods[ $method ] = true;
342
				$images_collected = Jetpack_PostImages::get_images( $post->ID, $methods );
343
				if ( is_array( $images_collected ) ) {
344
					$media = array_merge( $media, $images_collected );
345
				}
346
				$methods[ $method ] = false;
347
			}
348
349
			$images = array();
350
351
			foreach ( $media as $item ) {
352
				if ( ! isset( $item['type'] ) || 'image' != $item['type'] ) {
353
					continue;
354
				}
355
				$one_image = array();
356
357
				if ( isset( $item['src'] ) ) {
358
					$one_image['image:loc'] = esc_url( $item['src'] );
359
					$one_image['image:title'] = sanitize_title_with_dashes( $name = pathinfo( $item['src'], PATHINFO_FILENAME ) );
360
				}
361
362
				$images[] = $one_image;
363
			}
364
365
			if ( ! empty( $images ) ) {
366
				$url['image:image'] = $images;
367
			}
368
		}
369
370
		if ( $post->post_modified_gmt && $post->post_modified_gmt != '0000-00-00 00:00:00' ) {
371
			$post_latest_mod = $post->post_modified_gmt;
372
		}
373
		if ( $post->comment_count > 0 ) {
374
			// last modified based on last comment
375
			$latest_comment_datetime = jetpack_get_approved_comments_max_datetime( $post->ID );
376
			if ( ! empty( $latest_comment_datetime ) ) {
377
				if ( is_null( $post_latest_mod ) || $latest_comment_datetime > $post_latest_mod ) {
378
					$post_latest_mod = $latest_comment_datetime;
379
				}
380
			}
381
			unset( $latest_comment_datetime );
382
		}
383
		if ( ! empty( $post_latest_mod ) ) {
384
			$latest_mod     = max( $latest_mod, $post_latest_mod );
385
			$url['lastmod'] = jetpack_w3cdate_from_mysql( $post_latest_mod );
386
		}
387
		unset( $post_latest_mod );
388
		if ( $post->post_type == 'page' ) {
389
			$url['changefreq'] = 'weekly';
390
			$url['priority']   = '0.6'; // set page priority above default priority of 0.5
391
		} else {
392
			$url['changefreq'] = 'monthly';
393
		}
394
		/**
395
		 * Filter associative array with data to build <url> node and its descendants for current post.
396
		 *
397
		 * @module sitemaps
398
		 *
399
		 * @since 3.9.0
400
		 *
401
		 * @param array $url Data to build parent and children nodes for current post.
402
		 * @param int $post_id Current post ID.
403
		 */
404
		$url_node = apply_filters( 'jetpack_sitemap_url', $url, $post->ID );
405
		jetpack_sitemap_array_to_simplexml( array( 'url' => $url_node ), $tree );
406
		unset( $url );
407
	}
408
	wp_reset_postdata();
409
	$blog_home = array(
410
		'loc'        => esc_url( get_option( 'home' ) ),
411
		'changefreq' => 'daily',
412
		'priority'   => '1.0'
413
	);
414
	if ( ! empty( $latest_mod ) ) {
415
		$blog_home['lastmod'] = jetpack_w3cdate_from_mysql( $latest_mod );
416
		header( 'Last-Modified:' . mysql2date( 'D, d M Y H:i:s', $latest_mod, 0 ) . ' GMT' );
417
	}
418
	/**
419
	 * Filter associative array with data to build <url> node and its descendants for site home.
420
	 *
421
	 * @module sitemaps
422
	 *
423
	 * @since 3.9.0
424
	 *
425
	 * @param array $blog_home Data to build parent and children nodes for site home.
426
	 */
427
	$url_node = apply_filters( 'jetpack_sitemap_url_home', $blog_home );
428
	jetpack_sitemap_array_to_simplexml( array( 'url' => $url_node ), $tree );
429
	unset( $blog_home );
430
431
	/**
432
	 * Filter data before rendering it as XML.
433
	 *
434
	 * @module sitemaps
435
	 *
436
	 * @since 3.9.0
437
	 *
438
	 * @param SimpleXMLElement $tree Data tree for sitemap.
439
	 * @param string $latest_mod Date of last modification.
440
	 */
441
	$tree = apply_filters( 'jetpack_print_sitemap', $tree, $latest_mod );
442
443
	$xml = $tree->asXML();
444
	unset( $tree );
445
	if ( ! empty( $xml ) ) {
446
		set_transient( 'jetpack_sitemap', $xml, DAY_IN_SECONDS );
447
		echo $xml;
448
	}
449
450
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_print_sitemap() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
451
}
452
453
/**
454
 * Prints the news XML sitemap conforming to the Sitemaps.org protocol.
455
 * Outputs an XML list of up to 1000 posts published in the last 2 days.
456
 *
457
 * @module sitemaps
458
 *
459
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
460
 */
461
function jetpack_print_news_sitemap() {
462
463
	$xml = get_transient( 'jetpack_news_sitemap' );
464
465
	if ( $xml ) {
466
		header( 'Content-Type: application/xml' );
467
		echo $xml;
468
		die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_print_news_sitemap() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
469
	}
470
471
	global $wpdb, $post;
472
473
	/**
474
	 * Filter post types to be included in news sitemap.
475
	 *
476
	 * @module sitemaps
477
	 *
478
	 * @since 3.9.0
479
	 *
480
	 * @param array $post_types Array with post types to include in news sitemap.
481
	 */
482
	$post_types = apply_filters( 'jetpack_sitemap_news_sitemap_post_types', array( 'post' ) );
483
	if ( empty( $post_types ) ) {
484
		return;
485
	}
486
487
	$post_types_in = array();
488
	foreach ( $post_types as $post_type ) {
489
		$post_types_in[] = $wpdb->prepare( '%s', $post_type );
490
	}
491
	$post_types_in_string = implode( ', ', $post_types_in );
492
493
	/**
494
	 * Filter limit of entries to include in news sitemap.
495
	 *
496
	 * @module sitemaps
497
	 *
498
	 * @since 3.9.0
499
	 *
500
	 * @param int $count Number of entries to include in news sitemap.
501
	 */
502
	$limit        = apply_filters( 'jetpack_sitemap_news_sitemap_count', 1000 );
503
	$cur_datetime = current_time( 'mysql', true );
504
505
	$query = $wpdb->prepare( "
506
		SELECT p.ID, p.post_title, p.post_type, p.post_date, p.post_name, p.post_date_gmt, GROUP_CONCAT(t.name SEPARATOR ', ') AS keywords
507
		FROM
508
			$wpdb->posts AS p LEFT JOIN $wpdb->term_relationships AS r ON p.ID = r.object_id
509
			LEFT JOIN $wpdb->term_taxonomy AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag'
510
			LEFT JOIN $wpdb->terms AS t ON tt.term_id = t.term_id
511
		WHERE
512
			post_status='publish' AND post_type IN ( {$post_types_in_string} ) AND post_date_gmt > (%s - INTERVAL 2 DAY)
513
		GROUP BY p.ID
514
		ORDER BY p.post_date_gmt DESC LIMIT %d", $cur_datetime, $limit );
515
516
	// URL to XSLT
517
	$xsl = get_option( 'permalink_structure' ) ? home_url( 'news-sitemap.xsl' ) : home_url( '/?jetpack-news-sitemap-xsl=true' );
518
519
	// Unless it's zh-cn for Simplified Chinese or zh-tw for Traditional Chinese,
520
	// trim national variety so an ISO 639 language code as required by Google.
521
	$language_code = strtolower( get_locale() );
522
	if ( in_array( $language_code, array( 'zh_tw', 'zh_cn' ) ) ) {
523
		$language_code = str_replace( '_', '-', $language_code );
524
	} else {
525
		$language_code = preg_replace( '/(_.*)$/i', '', $language_code );
526
	}
527
528
	header( 'Content-Type: application/xml' );
529
	ob_start();
530
	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
531
	echo '<?xml-stylesheet type="text/xsl" href="' . esc_url( $xsl ) . '"?>' . "\n";
532
	echo '<!-- generator="jetpack-' . JETPACK__VERSION . '" -->' . "\n";
533
	?>
534
	<!-- generator="jetpack" -->
535
	<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
536
	        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
537
	        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
538
	        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
539
	        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
540
		>
541
		<?php
542
		$posts = $wpdb->get_results( $query );
543
		foreach ( $posts as $post ):
544
			setup_postdata( $post );
545
546
			/**
547
			 * Filter condition to allow skipping specific posts in news sitemap.
548
			 *
549
			 * @module sitemaps
550
			 *
551
			 * @since 3.9.0
552
			 *
553
			 * @param bool $skip Current boolean. False by default, so no post is skipped.
554
			 * @param WP_POST $post Current post object.
555
			 */
556
			if ( apply_filters( 'jetpack_sitemap_news_skip_post', false, $post ) ) {
557
				continue;
558
			}
559
560
			$GLOBALS['post']                       = $post;
561
			$url                                   = array();
562
			$url['loc']                            = get_permalink( $post->ID );
563
			$news                                  = array();
564
			$news['news:publication']['news:name'] = get_bloginfo_rss( 'name' );
565
			$news['news:publication']['news:language'] = $language_code;
566
			$news['news:publication_date'] = jetpack_w3cdate_from_mysql( $post->post_date_gmt );
567
			$news['news:title']            = get_the_title_rss();
568
			if ( $post->keywords ) {
569
				$news['news:keywords'] = html_entity_decode( ent2ncr( $post->keywords ), ENT_HTML5 );
570
			}
571
			$url['news:news'] = $news;
572
573
			// Add image to sitemap
574
			$post_thumbnail = Jetpack_PostImages::get_image( $post->ID );
575
			if ( isset( $post_thumbnail['src'] ) ) {
576
				$url['image:image'] = array( 'image:loc' => esc_url( $post_thumbnail['src'] ) );
577
			}
578
579
			/**
580
			 * Filter associative array with data to build <url> node and its descendants for current post in news sitemap.
581
			 *
582
			 * @module sitemaps
583
			 *
584
			 * @since 3.9.0
585
			 *
586
			 * @param array $url Data to build parent and children nodes for current post.
587
			 * @param int $post_id Current post ID.
588
			 */
589
			$url = apply_filters( 'jetpack_sitemap_news_sitemap_item', $url, $post );
590
591
			if ( empty( $url ) ) {
592
				continue;
593
			}
594
595
			jetpack_print_sitemap_item( $url );
596
		endforeach;
597
		wp_reset_postdata();
598
		?>
599
	</urlset>
600
	<?php
601
	$xml = ob_get_contents();
602
	ob_end_clean();
603
	if ( ! empty( $xml ) ) {
604
		set_transient( 'jetpack_news_sitemap', $xml, DAY_IN_SECONDS );
605
		echo $xml;
606
	}
607
608
	die();
0 ignored issues
show
Coding Style Compatibility introduced by
The function jetpack_print_news_sitemap() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
609
}
610
611
/**
612
 * Absolute URL of the current blog's sitemap.
613
 *
614
 * @module sitemaps
615
 *
616
 * @return string Sitemap URL.
617
 */
618 View Code Duplication
function jetpack_sitemap_uri() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
619
	global $wp_rewrite;
620
621
	if ( $wp_rewrite->using_index_permalinks() ) {
622
		$sitemap_url = home_url( '/index.php/sitemap.xml' );
623
	} else if ( $wp_rewrite->using_permalinks() ) {
624
		$sitemap_url = home_url( '/sitemap.xml' );
625
	} else {
626
		$sitemap_url = home_url( '/?jetpack-sitemap=true' );
627
	}
628
629
	/**
630
	 * Filter sitemap URL relative to home URL.
631
	 *
632
	 * @module sitemaps
633
	 *
634
	 * @since 3.9.0
635
	 *
636
	 * @param string $sitemap_url Sitemap URL.
637
	 */
638
	return apply_filters( 'jetpack_sitemap_location', $sitemap_url );
639
}
640
641
/**
642
 * Absolute URL of the current blog's news sitemap.
643
 *
644
 * @module sitemaps
645
 */
646 View Code Duplication
function jetpack_news_sitemap_uri() {
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
647
	global $wp_rewrite;
648
649
	if ( $wp_rewrite->using_index_permalinks() ) {
650
		$news_sitemap_url = home_url( '/index.php/news-sitemap.xml' );
651
	} else if ( $wp_rewrite->using_permalinks() ) {
652
		$news_sitemap_url = home_url( '/news-sitemap.xml' );
653
	} else {
654
		$news_sitemap_url = home_url( '/?jetpack-news-sitemap=true' );
655
	}
656
657
	/**
658
	 * Filter news sitemap URL relative to home URL.
659
	 *
660
	 * @module sitemaps
661
	 *
662
	 * @since 3.9.0
663
	 *
664
	 * @param string $news_sitemap_url News sitemap URL.
665
	 */
666
	return apply_filters( 'jetpack_news_sitemap_location', $news_sitemap_url );
667
}
668
669
/**
670
 * Output the default sitemap URL.
671
 *
672
 * @module sitemaps
673
 */
674
function jetpack_sitemap_discovery() {
675
	echo 'Sitemap: ' . esc_url( jetpack_sitemap_uri() ) . PHP_EOL;
676
}
677
678
/**
679
 * Output the news sitemap URL.
680
 *
681
 * @module sitemaps
682
 */
683
function jetpack_news_sitemap_discovery() {
684
	echo 'Sitemap: ' . esc_url( jetpack_news_sitemap_uri() ) . PHP_EOL . PHP_EOL;
685
}
686
687
/**
688
 * Clear the sitemap cache when a sitemap action has changed.
689
 *
690
 * @module sitemaps
691
 *
692
 * @param int $post_id unique post identifier. not used.
693
 */
694
function jetpack_sitemap_handle_update( $post_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
695
	delete_transient( 'jetpack_sitemap' );
696
	delete_transient( 'jetpack_news_sitemap' );
697
}
698
699
/**
700
 * Clear sitemap cache when an entry changes. Make sitemaps discoverable to robots. Render sitemaps.
701
 *
702
 * @module sitemaps
703
 */
704
function jetpack_sitemap_initialize() {
705
	add_action( 'publish_post', 'jetpack_sitemap_handle_update', 12, 1 );
706
	add_action( 'publish_page', 'jetpack_sitemap_handle_update', 12, 1 );
707
	add_action( 'trash_post', 'jetpack_sitemap_handle_update', 12, 1 );
708
	add_action( 'deleted_post', 'jetpack_sitemap_handle_update', 12, 1 );
709
710
	/**
711
	 * Filter whether to make the default sitemap discoverable to robots or not.
712
	 *
713
	 * @module sitemaps
714
	 *
715
	 * @since 3.9.0
716
	 *
717
	 * @param bool $discover_sitemap Make default sitemap discoverable to robots.
718
	 */
719
	$discover_sitemap = apply_filters( 'jetpack_sitemap_generate', true );
720 View Code Duplication
	if ( $discover_sitemap ) {
721
		add_action( 'do_robotstxt', 'jetpack_sitemap_discovery', 5, 0 );
722
723
		if ( get_option( 'permalink_structure' ) ) {
724
			/** This filter is documented in modules/sitemaps/sitemaps.php */
725
			$sitemap = apply_filters( 'jetpack_sitemap_location', home_url( '/sitemap.xml' ) );
726
			$sitemap = parse_url( $sitemap, PHP_URL_PATH );
727
		} else {
728
			/** This filter is documented in modules/sitemaps/sitemaps.php */
729
			$sitemap = apply_filters( 'jetpack_sitemap_location', home_url( '/?jetpack-sitemap=true' ) );
730
			$sitemap = preg_replace( '/(=.*?)$/i', '', parse_url( $sitemap, PHP_URL_QUERY ) );
731
		}
732
733
		// Sitemap XML
734
		if ( preg_match( '#(' . $sitemap . ')$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET[ $sitemap ] ) && 'true' == $_GET[ $sitemap ] ) ) {
735
			// run later so things like custom post types have been registered
736
			add_action( 'init', 'jetpack_print_sitemap', 999 );
737
		}
738
739
		// XSLT for sitemap
740
		if ( preg_match( '#(/sitemap\.xsl)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-sitemap-xsl'] ) && 'true' == $_GET['jetpack-sitemap-xsl'] ) ) {
741
			add_action( 'init', 'jetpack_print_sitemap_xsl' );
742
		}
743
	}
744
745
	/**
746
	 * Filter whether to make the news sitemap discoverable to robots or not.
747
	 *
748
	 * @module sitemaps
749
	 *
750
	 * @since 3.9.0
751
	 *
752
	 * @param bool $discover_news_sitemap Make default news sitemap discoverable to robots.
753
	 */
754
	$discover_news_sitemap = apply_filters( 'jetpack_news_sitemap_generate', true );
755 View Code Duplication
	if ( $discover_news_sitemap ) {
756
		add_action( 'do_robotstxt', 'jetpack_news_sitemap_discovery', 5, 0 );
757
758
		if ( get_option( 'permalink_structure' ) ) {
759
			/** This filter is documented in modules/sitemaps/sitemaps.php */
760
			$sitemap = apply_filters( 'jetpack_news_sitemap_location', home_url( '/news-sitemap.xml' ) );
761
			$sitemap = parse_url( $sitemap, PHP_URL_PATH );
762
		} else {
763
			/** This filter is documented in modules/sitemaps/sitemaps.php */
764
			$sitemap = apply_filters( 'jetpack_news_sitemap_location', home_url( '/?jetpack-news-sitemap=true' ) );
765
			$sitemap = preg_replace( '/(=.*?)$/i', '', parse_url( $sitemap, PHP_URL_QUERY ) );
766
		}
767
768
		// News Sitemap XML
769
		if ( preg_match( '#(' . $sitemap . ')$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET[ $sitemap ] ) && 'true' == $_GET[ $sitemap ] ) ) {
770
			// run later so things like custom post types have been registered
771
			add_action( 'init', 'jetpack_print_news_sitemap', 999 );
772
		}
773
774
		// XSLT for sitemap
775
		if ( preg_match( '#(/news-sitemap\.xsl)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-news-sitemap-xsl'] ) && 'true' == $_GET['jetpack-news-sitemap-xsl'] ) ) {
776
			add_action( 'init', 'jetpack_print_news_sitemap_xsl' );
777
		}
778
	}
779
}
780
781
// Initialize sitemaps once themes can filter the initialization.
782
add_action( 'after_setup_theme', 'jetpack_sitemap_initialize' );
783