Completed
Push — master-stable ( 7c8a41...632110 )
by
unknown
331:08 queued 324:11
created

sitemaps.php ➔ jetpack_news_sitemap_discovery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
					// Make all image links absolute
359
					$check_url = parse_url( $item['src'] );
360 View Code Duplication
					if( empty( $check_url['scheme'] ) && empty( $check_url['host'] ) ){
361
						$item['src'] = network_site_url( $item['src'] );
362
					}
363
					$one_image['image:loc'] = esc_url( $item['src'] );
364
					$one_image['image:title'] = sanitize_title_with_dashes( $name = pathinfo( $item['src'], PATHINFO_FILENAME ) );
365
				}
366
367
				$images[] = $one_image;
368
			}
369
370
			if ( ! empty( $images ) ) {
371
				$url['image:image'] = $images;
372
			}
373
		}
374
375
		if ( $post->post_modified_gmt && $post->post_modified_gmt != '0000-00-00 00:00:00' ) {
376
			$post_latest_mod = $post->post_modified_gmt;
377
		}
378
		if ( $post->comment_count > 0 ) {
379
			// last modified based on last comment
380
			$latest_comment_datetime = jetpack_get_approved_comments_max_datetime( $post->ID );
381
			if ( ! empty( $latest_comment_datetime ) ) {
382
				if ( is_null( $post_latest_mod ) || $latest_comment_datetime > $post_latest_mod ) {
383
					$post_latest_mod = $latest_comment_datetime;
384
				}
385
			}
386
			unset( $latest_comment_datetime );
387
		}
388
		if ( ! empty( $post_latest_mod ) ) {
389
			$latest_mod     = max( $latest_mod, $post_latest_mod );
390
			$url['lastmod'] = jetpack_w3cdate_from_mysql( $post_latest_mod );
391
		}
392
		unset( $post_latest_mod );
393
		if ( $post->post_type == 'page' ) {
394
			$url['changefreq'] = 'weekly';
395
			$url['priority']   = '0.6'; // set page priority above default priority of 0.5
396
		} else {
397
			$url['changefreq'] = 'monthly';
398
		}
399
		/**
400
		 * Filter associative array with data to build <url> node and its descendants for current post.
401
		 *
402
		 * @module sitemaps
403
		 *
404
		 * @since 3.9.0
405
		 *
406
		 * @param array $url Data to build parent and children nodes for current post.
407
		 * @param int $post_id Current post ID.
408
		 */
409
		$url_node = apply_filters( 'jetpack_sitemap_url', $url, $post->ID );
410
		jetpack_sitemap_array_to_simplexml( array( 'url' => $url_node ), $tree );
411
		unset( $url );
412
	}
413
	wp_reset_postdata();
414
	$blog_home = array(
415
		'loc'        => esc_url( get_option( 'home' ) ),
416
		'changefreq' => 'daily',
417
		'priority'   => '1.0'
418
	);
419
	if ( ! empty( $latest_mod ) ) {
420
		$blog_home['lastmod'] = jetpack_w3cdate_from_mysql( $latest_mod );
421
		header( 'Last-Modified:' . mysql2date( 'D, d M Y H:i:s', $latest_mod, 0 ) . ' GMT' );
422
	}
423
	/**
424
	 * Filter associative array with data to build <url> node and its descendants for site home.
425
	 *
426
	 * @module sitemaps
427
	 *
428
	 * @since 3.9.0
429
	 *
430
	 * @param array $blog_home Data to build parent and children nodes for site home.
431
	 */
432
	$url_node = apply_filters( 'jetpack_sitemap_url_home', $blog_home );
433
	jetpack_sitemap_array_to_simplexml( array( 'url' => $url_node ), $tree );
434
	unset( $blog_home );
435
436
	/**
437
	 * Filter data before rendering it as XML.
438
	 *
439
	 * @module sitemaps
440
	 *
441
	 * @since 3.9.0
442
	 *
443
	 * @param SimpleXMLElement $tree Data tree for sitemap.
444
	 * @param string $latest_mod Date of last modification.
445
	 */
446
	$tree = apply_filters( 'jetpack_print_sitemap', $tree, $latest_mod );
447
448
	$xml = $tree->asXML();
449
	unset( $tree );
450
	if ( ! empty( $xml ) ) {
451
		set_transient( 'jetpack_sitemap', $xml, DAY_IN_SECONDS );
452
		echo $xml;
453
	}
454
455
	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...
456
}
457
458
/**
459
 * Prints the news XML sitemap conforming to the Sitemaps.org protocol.
460
 * Outputs an XML list of up to 1000 posts published in the last 2 days.
461
 *
462
 * @module sitemaps
463
 *
464
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
465
 */
466
function jetpack_print_news_sitemap() {
467
468
	$xml = get_transient( 'jetpack_news_sitemap' );
469
470
	if ( $xml ) {
471
		header( 'Content-Type: application/xml' );
472
		echo $xml;
473
		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...
474
	}
475
476
	global $wpdb, $post;
477
478
	/**
479
	 * Filter post types to be included in news sitemap.
480
	 *
481
	 * @module sitemaps
482
	 *
483
	 * @since 3.9.0
484
	 *
485
	 * @param array $post_types Array with post types to include in news sitemap.
486
	 */
487
	$post_types = apply_filters( 'jetpack_sitemap_news_sitemap_post_types', array( 'post' ) );
488
	if ( empty( $post_types ) ) {
489
		return;
490
	}
491
492
	$post_types_in = array();
493
	foreach ( $post_types as $post_type ) {
494
		$post_types_in[] = $wpdb->prepare( '%s', $post_type );
495
	}
496
	$post_types_in_string = implode( ', ', $post_types_in );
497
498
	/**
499
	 * Filter limit of entries to include in news sitemap.
500
	 *
501
	 * @module sitemaps
502
	 *
503
	 * @since 3.9.0
504
	 *
505
	 * @param int $count Number of entries to include in news sitemap.
506
	 */
507
	$limit        = apply_filters( 'jetpack_sitemap_news_sitemap_count', 1000 );
508
	$cur_datetime = current_time( 'mysql', true );
509
510
	$query = $wpdb->prepare( "
511
		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
512
		FROM
513
			$wpdb->posts AS p LEFT JOIN $wpdb->term_relationships AS r ON p.ID = r.object_id
514
			LEFT JOIN $wpdb->term_taxonomy AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag'
515
			LEFT JOIN $wpdb->terms AS t ON tt.term_id = t.term_id
516
		WHERE
517
			post_status='publish' AND post_type IN ( {$post_types_in_string} ) AND post_date_gmt > (%s - INTERVAL 2 DAY)
518
		GROUP BY p.ID
519
		ORDER BY p.post_date_gmt DESC LIMIT %d", $cur_datetime, $limit );
520
521
	// URL to XSLT
522
	$xsl = get_option( 'permalink_structure' ) ? home_url( 'news-sitemap.xsl' ) : home_url( '/?jetpack-news-sitemap-xsl=true' );
523
524
	// Unless it's zh-cn for Simplified Chinese or zh-tw for Traditional Chinese,
525
	// trim national variety so an ISO 639 language code as required by Google.
526
	$language_code = strtolower( get_locale() );
527
	if ( in_array( $language_code, array( 'zh_tw', 'zh_cn' ) ) ) {
528
		$language_code = str_replace( '_', '-', $language_code );
529
	} else {
530
		$language_code = preg_replace( '/(_.*)$/i', '', $language_code );
531
	}
532
533
	header( 'Content-Type: application/xml' );
534
	ob_start();
535
	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
536
	echo '<?xml-stylesheet type="text/xsl" href="' . esc_url( $xsl ) . '"?>' . "\n";
537
	echo '<!-- generator="jetpack-' . JETPACK__VERSION . '" -->' . "\n";
538
	?>
539
	<!-- generator="jetpack" -->
540
	<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
541
	        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
542
	        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
543
	        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
544
	        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
545
		>
546
		<?php
547
		$posts = $wpdb->get_results( $query );
548
		foreach ( $posts as $post ):
549
			setup_postdata( $post );
550
551
			/**
552
			 * Filter condition to allow skipping specific posts in news sitemap.
553
			 *
554
			 * @module sitemaps
555
			 *
556
			 * @since 3.9.0
557
			 *
558
			 * @param bool $skip Current boolean. False by default, so no post is skipped.
559
			 * @param WP_POST $post Current post object.
560
			 */
561
			if ( apply_filters( 'jetpack_sitemap_news_skip_post', false, $post ) ) {
562
				continue;
563
			}
564
565
			$GLOBALS['post']                       = $post;
566
			$url                                   = array();
567
			$url['loc']                            = get_permalink( $post->ID );
568
			$news                                  = array();
569
			$news['news:publication']['news:name'] = get_bloginfo_rss( 'name' );
570
			$news['news:publication']['news:language'] = $language_code;
571
			$news['news:publication_date'] = jetpack_w3cdate_from_mysql( $post->post_date_gmt );
572
			$news['news:title']            = get_the_title_rss();
573
			if ( $post->keywords ) {
574
				$news['news:keywords'] = html_entity_decode( ent2ncr( $post->keywords ), ENT_HTML5 );
575
			}
576
			$url['news:news'] = $news;
577
578
			// Add image to sitemap
579
			$post_thumbnail = Jetpack_PostImages::get_image( $post->ID );
580
			if ( isset( $post_thumbnail['src'] ) ) {
581
				// Make all news image links absolute
582
				$check_url = parse_url( $post_thumbnail['src'] );
583 View Code Duplication
				if( empty( $check_url['scheme'] ) && empty( $check_url['host'] ) ){
584
					$post_thumbnail['src'] = network_site_url( $post_thumbnail['src'] );
585
				}
586
				$url['image:image'] = array( 'image:loc' => esc_url( $post_thumbnail['src'] ) );
587
			}
588
589
			/**
590
			 * Filter associative array with data to build <url> node and its descendants for current post in news sitemap.
591
			 *
592
			 * @module sitemaps
593
			 *
594
			 * @since 3.9.0
595
			 *
596
			 * @param array $url Data to build parent and children nodes for current post.
597
			 * @param int $post_id Current post ID.
598
			 */
599
			$url = apply_filters( 'jetpack_sitemap_news_sitemap_item', $url, $post );
600
601
			if ( empty( $url ) ) {
602
				continue;
603
			}
604
605
			jetpack_print_sitemap_item( $url );
606
		endforeach;
607
		wp_reset_postdata();
608
		?>
609
	</urlset>
610
	<?php
611
	$xml = ob_get_contents();
612
	ob_end_clean();
613
	if ( ! empty( $xml ) ) {
614
		set_transient( 'jetpack_news_sitemap', $xml, DAY_IN_SECONDS );
615
		echo $xml;
616
	}
617
618
	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...
619
}
620
621
/**
622
 * Absolute URL of the current blog's sitemap.
623
 *
624
 * @module sitemaps
625
 *
626
 * @return string Sitemap URL.
627
 */
628 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...
629
	global $wp_rewrite;
630
631
	if ( $wp_rewrite->using_index_permalinks() ) {
632
		$sitemap_url = home_url( '/index.php/sitemap.xml' );
633
	} else if ( $wp_rewrite->using_permalinks() ) {
634
		$sitemap_url = home_url( '/sitemap.xml' );
635
	} else {
636
		$sitemap_url = home_url( '/?jetpack-sitemap=true' );
637
	}
638
639
	/**
640
	 * Filter sitemap URL relative to home URL.
641
	 *
642
	 * @module sitemaps
643
	 *
644
	 * @since 3.9.0
645
	 *
646
	 * @param string $sitemap_url Sitemap URL.
647
	 */
648
	return apply_filters( 'jetpack_sitemap_location', $sitemap_url );
649
}
650
651
/**
652
 * Absolute URL of the current blog's news sitemap.
653
 *
654
 * @module sitemaps
655
 */
656 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...
657
	global $wp_rewrite;
658
659
	if ( $wp_rewrite->using_index_permalinks() ) {
660
		$news_sitemap_url = home_url( '/index.php/news-sitemap.xml' );
661
	} else if ( $wp_rewrite->using_permalinks() ) {
662
		$news_sitemap_url = home_url( '/news-sitemap.xml' );
663
	} else {
664
		$news_sitemap_url = home_url( '/?jetpack-news-sitemap=true' );
665
	}
666
667
	/**
668
	 * Filter news sitemap URL relative to home URL.
669
	 *
670
	 * @module sitemaps
671
	 *
672
	 * @since 3.9.0
673
	 *
674
	 * @param string $news_sitemap_url News sitemap URL.
675
	 */
676
	return apply_filters( 'jetpack_news_sitemap_location', $news_sitemap_url );
677
}
678
679
/**
680
 * Output the default sitemap URL.
681
 *
682
 * @module sitemaps
683
 */
684
function jetpack_sitemap_discovery() {
685
	echo 'Sitemap: ' . esc_url( jetpack_sitemap_uri() ) . PHP_EOL;
686
}
687
688
/**
689
 * Output the news sitemap URL.
690
 *
691
 * @module sitemaps
692
 */
693
function jetpack_news_sitemap_discovery() {
694
	echo 'Sitemap: ' . esc_url( jetpack_news_sitemap_uri() ) . PHP_EOL . PHP_EOL;
695
}
696
697
/**
698
 * Clear the sitemap cache when a sitemap action has changed.
699
 *
700
 * @module sitemaps
701
 *
702
 * @param int $post_id unique post identifier. not used.
703
 */
704
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...
705
	delete_transient( 'jetpack_sitemap' );
706
	delete_transient( 'jetpack_news_sitemap' );
707
}
708
709
/**
710
 * Clear sitemap cache when an entry changes. Make sitemaps discoverable to robots. Render sitemaps.
711
 *
712
 * @module sitemaps
713
 */
714
function jetpack_sitemap_initialize() {
715
	add_action( 'publish_post', 'jetpack_sitemap_handle_update', 12, 1 );
716
	add_action( 'publish_page', 'jetpack_sitemap_handle_update', 12, 1 );
717
	add_action( 'trash_post', 'jetpack_sitemap_handle_update', 12, 1 );
718
	add_action( 'deleted_post', 'jetpack_sitemap_handle_update', 12, 1 );
719
720
	/**
721
	 * Filter whether to make the default sitemap discoverable to robots or not.
722
	 *
723
	 * @module sitemaps
724
	 *
725
	 * @since 3.9.0
726
	 *
727
	 * @param bool $discover_sitemap Make default sitemap discoverable to robots.
728
	 */
729
	$discover_sitemap = apply_filters( 'jetpack_sitemap_generate', true );
730 View Code Duplication
	if ( $discover_sitemap ) {
731
		add_action( 'do_robotstxt', 'jetpack_sitemap_discovery', 5, 0 );
732
733
		if ( get_option( 'permalink_structure' ) ) {
734
			/** This filter is documented in modules/sitemaps/sitemaps.php */
735
			$sitemap = apply_filters( 'jetpack_sitemap_location', home_url( '/sitemap.xml' ) );
736
			$sitemap = parse_url( $sitemap, PHP_URL_PATH );
737
		} else {
738
			/** This filter is documented in modules/sitemaps/sitemaps.php */
739
			$sitemap = apply_filters( 'jetpack_sitemap_location', home_url( '/?jetpack-sitemap=true' ) );
740
			$sitemap = preg_replace( '/(=.*?)$/i', '', parse_url( $sitemap, PHP_URL_QUERY ) );
741
		}
742
743
		// Sitemap XML
744
		if ( preg_match( '#(' . $sitemap . ')$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET[ $sitemap ] ) && 'true' == $_GET[ $sitemap ] ) ) {
745
			// run later so things like custom post types have been registered
746
			add_action( 'init', 'jetpack_print_sitemap', 999 );
747
		}
748
749
		// XSLT for sitemap
750
		if ( preg_match( '#(/sitemap\.xsl)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-sitemap-xsl'] ) && 'true' == $_GET['jetpack-sitemap-xsl'] ) ) {
751
			add_action( 'init', 'jetpack_print_sitemap_xsl' );
752
		}
753
	}
754
755
	/**
756
	 * Filter whether to make the news sitemap discoverable to robots or not.
757
	 *
758
	 * @module sitemaps
759
	 *
760
	 * @since 3.9.0
761
	 *
762
	 * @param bool $discover_news_sitemap Make default news sitemap discoverable to robots.
763
	 */
764
	$discover_news_sitemap = apply_filters( 'jetpack_news_sitemap_generate', true );
765 View Code Duplication
	if ( $discover_news_sitemap ) {
766
		add_action( 'do_robotstxt', 'jetpack_news_sitemap_discovery', 5, 0 );
767
768
		if ( get_option( 'permalink_structure' ) ) {
769
			/** This filter is documented in modules/sitemaps/sitemaps.php */
770
			$sitemap = apply_filters( 'jetpack_news_sitemap_location', home_url( '/news-sitemap.xml' ) );
771
			$sitemap = parse_url( $sitemap, PHP_URL_PATH );
772
		} else {
773
			/** This filter is documented in modules/sitemaps/sitemaps.php */
774
			$sitemap = apply_filters( 'jetpack_news_sitemap_location', home_url( '/?jetpack-news-sitemap=true' ) );
775
			$sitemap = preg_replace( '/(=.*?)$/i', '', parse_url( $sitemap, PHP_URL_QUERY ) );
776
		}
777
778
		// News Sitemap XML
779
		if ( preg_match( '#(' . $sitemap . ')$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET[ $sitemap ] ) && 'true' == $_GET[ $sitemap ] ) ) {
780
			// run later so things like custom post types have been registered
781
			add_action( 'init', 'jetpack_print_news_sitemap', 999 );
782
		}
783
784
		// XSLT for sitemap
785
		if ( preg_match( '#(/news-sitemap\.xsl)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-news-sitemap-xsl'] ) && 'true' == $_GET['jetpack-news-sitemap-xsl'] ) ) {
786
			add_action( 'init', 'jetpack_print_news_sitemap_xsl' );
787
		}
788
	}
789
}
790
791
// Initialize sitemaps once themes can filter the initialization.
792
add_action( 'after_setup_theme', 'jetpack_sitemap_initialize' );
793