Completed
Push — add/80443split ( 25998f...41be19 )
by
unknown
09:21
created

sitemaps.php ➔ jetpack_print_sitemap()   F

Complexity

Conditions 30
Paths > 20000

Size

Total Lines 224
Code Lines 101

Duplication

Lines 6
Ratio 2.68 %
Metric Value
cc 30
eloc 101
nc 47169
nop 0
dl 6
loc 224
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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, ':' ) ) {
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 ( is_array( $value ) ) {
116
			$child = $tree->addChild( $key, null, $namespace );
117
			jetpack_sitemap_array_to_simplexml( $value, $child );
118
		} else {
119
			$tree->addChild( $key, esc_html( $value ), $namespace );
120
		}
121
	}
122
123
	return $tree;
124
}
125
126
/**
127
 * Define an array of attribute value pairs for use inside the root element of an XML document.
128
 * Intended for mapping namespace and namespace URI values.
129
 * Passes array through jetpack_sitemap_ns for other functions to add their own namespaces.
130
 *
131
 * @module sitemaps
132
 *
133
 * @return array array of attribute value pairs passed through the jetpack_sitemap_ns filter
134
 */
135
function jetpack_sitemap_namespaces() {
136
	/**
137
	 * Filter the attribute value pairs used for namespace and namespace URI mappings.
138
	 *
139
	 * @module sitemaps
140
	 *
141
	 * @since 3.9.0
142
	 *
143
	 * @param array $namespaces Associative array with namespaces and namespace URIs.
144
	 */
145
	return apply_filters( 'jetpack_sitemap_ns', array(
146
		'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
147
		'xsi:schemaLocation' => 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
148
		'xmlns'              => 'http://www.sitemaps.org/schemas/sitemap/0.9',
149
		// Mobile namespace from http://support.google.com/webmasters/bin/answer.py?hl=en&answer=34648
150
		'xmlns:mobile'       => 'http://www.google.com/schemas/sitemap-mobile/1.0',
151
		'xmlns:image'        => 'http://www.google.com/schemas/sitemap-image/1.1',
152
	) );
153
}
154
155
/**
156
 * Start sitemap XML document, writing its heading and <urlset> tag with namespaces.
157
 *
158
 * @module sitemaps
159
 *
160
 * @param $charset string Charset for current XML document.
161
 *
162
 * @return string
163
 */
164
function jetpack_sitemap_initstr( $charset ) {
165
	// URL to XSLT
166
	$xsl = get_option( 'permalink_structure' ) ? home_url( '/sitemap.xsl' ) : home_url( '/?jetpack-sitemap-xsl=true' );
167
168
	$initstr = '<?xml version="1.0" encoding="' . $charset . '"?>' . "\n";
169
	$initstr .= '<?xml-stylesheet type="text/xsl" href="' . esc_url( $xsl ) . '"?>' . "\n";
170
	$initstr .= '<!-- generator="jetpack-' . JETPACK__VERSION . '" -->' . "\n";
171
	$initstr .= '<urlset';
172
	foreach ( jetpack_sitemap_namespaces() as $attribute => $value ) {
173
		$initstr .= ' ' . esc_html( $attribute ) . '="' . esc_attr( $value ) . '"';
174
	}
175
	$initstr .= ' />';
176
177
	return $initstr;
178
}
179
180
/**
181
 * Load XSLT for sitemap.
182
 *
183
 * @module sitemaps
184
 *
185
 * @param string $type XSLT to load.
186
 */
187
function jetpack_load_xsl( $type = '' ) {
188
189
	$transient_xsl = empty( $type ) ? 'jetpack_sitemap_xsl' : "jetpack_{$type}_sitemap_xsl";
190
191
	$xsl = get_transient( $transient_xsl );
192
193
	if ( $xsl ) {
194
		header( 'Content-Type: ' . jetpack_sitemap_content_type(), true );
195
		echo $xsl;
196
		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...
197
	}
198
199
	// Populate $xsl. Use $type.
200
	include_once JETPACK__PLUGIN_DIR . 'modules/sitemaps/sitemap-xsl.php';
201
202
	if ( ! empty( $xsl ) ) {
203
		set_transient( $transient_xsl, $xsl, DAY_IN_SECONDS );
204
		echo $xsl;
205
	}
206
207
	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...
208
}
209
210
/**
211
 * Responds with an XSLT to stylize sitemap.
212
 *
213
 * @module sitemaps
214
 */
215
function jetpack_print_sitemap_xsl() {
216
	jetpack_load_xsl();
217
}
218
219
/**
220
 * Responds with an XSLT to stylize news sitemap.
221
 *
222
 * @module sitemaps
223
 */
224
function jetpack_print_news_sitemap_xsl() {
225
	jetpack_load_xsl( 'news' );
226
}
227
228
/**
229
 * Print an XML sitemap conforming to the Sitemaps.org protocol.
230
 * Outputs an XML list of up to the latest 1000 posts.
231
 *
232
 * @module sitemaps
233
 *
234
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
235
 */
236
function jetpack_print_sitemap() {
237
	global $wpdb;
238
239
	$xml = get_transient( 'jetpack_sitemap' );
240
241
	if ( $xml ) {
242
		header( 'Content-Type: ' . jetpack_sitemap_content_type(), true );
243
		echo $xml;
244
		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...
245
	}
246
247
	// Compatibility with PHP 5.3 and older
248
	if ( ! defined( 'ENT_XML1' ) ) {
249
		define( 'ENT_XML1', 16 );
250
	}
251
252
	/**
253
	 * Filter the post types that will be included in sitemap.
254
	 *
255
	 * @module sitemaps
256
	 *
257
	 * @since 3.9.0
258
	 *
259
	 * @param array $post_types Array of post types.
260
	 */
261
	$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...
262
263
	$post_types_in = array();
264
	foreach ( (array) $post_types as $post_type ) {
265
		$post_types_in[] = $wpdb->prepare( '%s', $post_type );
266
	}
267
	$post_types_in = join( ",", $post_types_in );
268
269
	// use direct query instead because get_posts was acting too heavy for our needs
270
	//$posts = get_posts( array( 'numberposts'=>1000, 'post_type'=>$post_types, 'post_status'=>'published' ) );
271
	$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" );
272
	if ( empty( $posts ) ) {
273
		status_header( 404 );
274
	}
275
	header( 'Content-Type: ' . jetpack_sitemap_content_type() );
276
	$initstr = jetpack_sitemap_initstr( get_bloginfo( 'charset' ) );
277
	$tree    = simplexml_load_string( $initstr );
278
	// If we did not get a valid string, force UTF-8 and try again.
279
	if ( false === $tree ) {
280
		$initstr = jetpack_sitemap_initstr( 'UTF-8' );
281
		$tree    = simplexml_load_string( $initstr );
282
	}
283
284
	// Acquire necessary attachment data for all of the posts in a performant manner
285
	$attachment_parents = wp_list_pluck( $posts, 'ID' );
286
	$post_attachments   = array();
287
	while ( $sub_posts = array_splice( $attachment_parents, 0, 100 ) ) {
288
		$post_parents = implode( ',', array_map( 'intval', $sub_posts ) );
289
290
		// Get the attachment IDs for all posts. We need to see how many
291
		// attachments each post parent has and limit it to 5.
292
		$query                = "SELECT ID, post_parent FROM {$wpdb->posts} WHERE post_parent IN ({$post_parents}) AND post_type='attachment' AND ( post_mime_type='image/jpeg' OR post_mime_type='image/png' ) LIMIT 0,1000;";
293
		$all_attachments      = $wpdb->get_results( $query );
294
		$selected_attachments = array();
295
		$attachment_count     = array();
296
297
		foreach ( $all_attachments as $attachment ) {
298
			if ( ! isset( $attachment_count[ $attachment->post_parent ] ) ) {
299
				$attachment_count[ $attachment->post_parent ] = 0;
300
			}
301
302
			// Skip this particular attachment if we already have 5 for the post
303
			if ( $attachment_count[ $attachment->post_parent ] >= 5 ) {
304
				continue;
305
			}
306
307
			$selected_attachments[] = $attachment->ID;
308
			$attachment_count[ $attachment->post_parent ] ++;
309
		}
310
311
		// bail if there weren't any attachments to avoid an extra query
312
		if ( empty( $selected_attachments ) ) {
313
			continue;
314
		}
315
316
		// Get more of the attachment object for the attachments we actually care about
317
		$attachment_ids   = implode( ',', array_map( 'intval', $selected_attachments ) );
318
		$query            = "SELECT p.ID, p.post_parent, p.post_title, p.post_excerpt, p.guid FROM {$wpdb->posts} as p WHERE p.ID IN ({$attachment_ids}) AND p.post_type='attachment' AND ( p.post_mime_type='image/jpeg' OR p.post_mime_type='image/png' ) LIMIT 500;";
319
		$attachments      = $wpdb->get_results( $query );
320
		$post_attachments = array_merge( $post_attachments, $attachments );
321
	}
322
323
	unset( $initstr );
324
	$latest_mod = '';
325
	foreach ( $posts as $post ) {
326
327
		/**
328
		 * Filter condition to allow skipping specific posts in sitemap.
329
		 *
330
		 * @module sitemaps
331
		 *
332
		 * @since 3.9.0
333
		 *
334
		 * @param bool $skip Current boolean. False by default, so no post is skipped.
335
		 * @param WP_POST $post Current post object.
336
		 */
337
		if ( apply_filters( 'jetpack_sitemap_skip_post', false, $post ) ) {
338
			continue;
339
		}
340
341
		$post_latest_mod = null;
342
		$url             = array( 'loc' => esc_url( get_permalink( $post->ID ) ) );
343
344
		// If this post is configured to be the site home, skip since it's added separately later
345
		if ( untrailingslashit( get_permalink( $post->ID ) ) == untrailingslashit( get_option( 'home' ) ) ) {
346
			continue;
347
		}
348
349
		// Mobile node specified in http://support.google.com/webmasters/bin/answer.py?hl=en&answer=34648
350
		$url['mobile:mobile'] = '';
351
352
		// Image node specified in http://support.google.com/webmasters/bin/answer.py?hl=en&answer=178636
353
		// These attachments were produced with batch SQL earlier in the script
354
		if ( ! post_password_required( $post->ID ) && $attachments = wp_filter_object_list( $post_attachments, array( 'post_parent' => $post->ID ) ) ) {
355
356
			$url['image:image'] = array();
357
358
			foreach ( $attachments as $attachment ) {
359
				$attachment_url = wp_get_attachment_url( $attachment->ID );
360
361
				if ( $attachment_url ) {
362
					$url['image:image']['loc'] = esc_url( $attachment_url );
363
				}
364
365
				// Only include title if not empty.
366
				/** This filter is documented in wp-includes/feed.php */
367 View Code Duplication
				if ( $attachment_title = apply_filters( 'the_title_rss', $attachment->post_title ) ) {
368
					$url['image:image']['title'] = html_entity_decode( esc_html( $attachment_title ), ENT_XML1 );
369
				}
370
371
				// Only include caption if not empty.
372
				/** This filter is documented in wp-includes/feed.php */
373 View Code Duplication
				if ( $attachment_caption = apply_filters( 'the_excerpt_rss', $attachment->post_excerpt ) ) {
374
					$url['image:image']['caption'] = html_entity_decode( esc_html( $attachment_caption ), ENT_XML1 );
375
				}
376
			}
377
		}
378
379
		if ( $post->post_modified_gmt && $post->post_modified_gmt != '0000-00-00 00:00:00' ) {
380
			$post_latest_mod = $post->post_modified_gmt;
381
		}
382
		if ( $post->comment_count > 0 ) {
383
			// last modified based on last comment
384
			$latest_comment_datetime = jetpack_get_approved_comments_max_datetime( $post->ID );
385
			if ( ! empty( $latest_comment_datetime ) ) {
386
				if ( is_null( $post_latest_mod ) || $latest_comment_datetime > $post_latest_mod ) {
387
					$post_latest_mod = $latest_comment_datetime;
388
				}
389
			}
390
			unset( $latest_comment_datetime );
391
		}
392
		if ( ! empty( $post_latest_mod ) ) {
393
			$latest_mod     = max( $latest_mod, $post_latest_mod );
394
			$url['lastmod'] = jetpack_w3cdate_from_mysql( $post_latest_mod );
395
		}
396
		unset( $post_latest_mod );
397
		if ( $post->post_type == 'page' ) {
398
			$url['changefreq'] = 'weekly';
399
			$url['priority']   = '0.6'; // set page priority above default priority of 0.5
400
		} else {
401
			$url['changefreq'] = 'monthly';
402
		}
403
		/**
404
		 * Filter associative array with data to build <url> node and its descendants for current post.
405
		 *
406
		 * @module sitemaps
407
		 *
408
		 * @since 3.9.0
409
		 *
410
		 * @param array $url Data to build parent and children nodes for current post.
411
		 * @param int $post_id Current post ID.
412
		 */
413
		$url_node = apply_filters( 'jetpack_sitemap_url', $url, $post->ID );
414
		jetpack_sitemap_array_to_simplexml( array( 'url' => $url_node ), $tree );
415
		unset( $url );
416
	}
417
	$blog_home = array(
418
		'loc'        => esc_url( get_option( 'home' ) ),
419
		'changefreq' => 'daily',
420
		'priority'   => '1.0'
421
	);
422
	if ( ! empty( $latest_mod ) ) {
423
		$blog_home['lastmod'] = jetpack_w3cdate_from_mysql( $latest_mod );
424
		header( 'Last-Modified:' . mysql2date( 'D, d M Y H:i:s', $latest_mod, 0 ) . ' GMT' );
425
	}
426
	/**
427
	 * Filter associative array with data to build <url> node and its descendants for site home.
428
	 *
429
	 * @module sitemaps
430
	 *
431
	 * @since 3.9.0
432
	 *
433
	 * @param array $blog_home Data to build parent and children nodes for site home.
434
	 */
435
	$url_node = apply_filters( 'jetpack_sitemap_url_home', $blog_home );
436
	jetpack_sitemap_array_to_simplexml( array( 'url' => $url_node ), $tree );
437
	unset( $blog_home );
438
439
	/**
440
	 * Filter data before rendering it as XML.
441
	 *
442
	 * @module sitemaps
443
	 *
444
	 * @since 3.9.0
445
	 *
446
	 * @param SimpleXMLElement $tree Data tree for sitemap.
447
	 * @param string $latest_mod Date of last modification.
448
	 */
449
	$tree = apply_filters( 'jetpack_print_sitemap', $tree, $latest_mod );
450
451
	$xml = $tree->asXML();
452
	unset( $tree );
453
	if ( ! empty( $xml ) ) {
454
		set_transient( 'jetpack_sitemap', $xml, DAY_IN_SECONDS );
455
		echo $xml;
456
	}
457
458
	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...
459
}
460
461
/**
462
 * Prints the news XML sitemap conforming to the Sitemaps.org protocol.
463
 * Outputs an XML list of up to 1000 posts published in the last 2 days.
464
 *
465
 * @module sitemaps
466
 *
467
 * @link http://sitemaps.org/protocol.php Sitemaps.org protocol.
468
 */
469
function jetpack_print_news_sitemap() {
470
471
	$xml = get_transient( 'jetpack_news_sitemap' );
472
473
	if ( $xml ) {
474
		header( 'Content-Type: application/xml' );
475
		echo $xml;
476
		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...
477
	}
478
479
	global $wpdb;
480
481
	/**
482
	 * Filter post types to be included in news sitemap.
483
	 *
484
	 * @module sitemaps
485
	 *
486
	 * @since 3.9.0
487
	 *
488
	 * @param array $post_types Array with post types to include in news sitemap.
489
	 */
490
	$post_types = apply_filters( 'jetpack_sitemap_news_sitemap_post_types', array( 'post' ) );
491
	if ( empty( $post_types ) ) {
492
		return;
493
	}
494
495
	$post_types_in = array();
496
	foreach ( $post_types as $post_type ) {
497
		$post_types_in[] = $wpdb->prepare( '%s', $post_type );
498
	}
499
	$post_types_in_string = implode( ', ', $post_types_in );
500
501
	/**
502
	 * Filter limit of entries to include in news sitemap.
503
	 *
504
	 * @module sitemaps
505
	 *
506
	 * @since 3.9.0
507
	 *
508
	 * @param int $count Number of entries to include in news sitemap.
509
	 */
510
	$limit        = apply_filters( 'jetpack_sitemap_news_sitemap_count', 1000 );
511
	$cur_datetime = current_time( 'mysql', true );
512
513
	$query = $wpdb->prepare( "
514
		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
515
		FROM
516
			$wpdb->posts AS p LEFT JOIN $wpdb->term_relationships AS r ON p.ID = r.object_id
517
			LEFT JOIN $wpdb->term_taxonomy AS tt ON r.term_taxonomy_id = tt.term_taxonomy_id AND tt.taxonomy = 'post_tag'
518
			LEFT JOIN $wpdb->terms AS t ON tt.term_id = t.term_id
519
		WHERE
520
			post_status='publish' AND post_type IN ( {$post_types_in_string} ) AND post_date_gmt > (%s - INTERVAL 2 DAY)
521
		GROUP BY p.ID
522
		ORDER BY p.post_date_gmt DESC LIMIT %d", $cur_datetime, $limit );
523
524
	// URL to XSLT
525
	$xsl = get_option( 'permalink_structure' ) ? home_url( 'news-sitemap.xsl' ) : home_url( '/?jetpack-news-sitemap-xsl=true' );
526
527
	header( 'Content-Type: application/xml' );
528
	ob_start();
529
	echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
530
	echo '<?xml-stylesheet type="text/xsl" href="' . esc_url( $xsl ) . '"?>' . "\n";
531
	echo '<!-- generator="jetpack-' . JETPACK__VERSION . '" -->' . "\n";
532
	?>
533
	<!-- generator="jetpack" -->
534
	<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
535
	        xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
536
	        xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
537
	        xmlns:news="http://www.google.com/schemas/sitemap-news/0.9"
538
	        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
539
		>
540
		<?php
541
		$posts = $wpdb->get_results( $query );
542
		foreach ( $posts as $post ):
543
544
			/**
545
			 * Filter condition to allow skipping specific posts in news sitemap.
546
			 *
547
			 * @module sitemaps
548
			 *
549
			 * @since 3.9.0
550
			 *
551
			 * @param bool $skip Current boolean. False by default, so no post is skipped.
552
			 * @param WP_POST $post Current post object.
553
			 */
554
			if ( apply_filters( 'jetpack_sitemap_news_skip_post', false, $post ) ) {
555
				continue;
556
			}
557
558
			$GLOBALS['post']                       = $post;
559
			$url                                   = array();
560
			$url['loc']                            = get_permalink( $post->ID );
561
			$news                                  = array();
562
			$news['news:publication']['news:name'] = get_bloginfo_rss( 'name' );
563
			if ( function_exists( 'get_blog_lang_code' ) ) {
564
				$news['news:publication']['news:language'] = get_blog_lang_code();
565
			}
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
		?>
598
	</urlset>
599
	<?php
600
	$xml = ob_get_contents();
601
	ob_end_clean();
602
	if ( ! empty( $xml ) ) {
603
		set_transient( 'jetpack_news_sitemap', $xml, DAY_IN_SECONDS );
604
		echo $xml;
605
	}
606
607
	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...
608
}
609
610
/**
611
 * Absolute URL of the current blog's sitemap.
612
 *
613
 * @module sitemaps
614
 *
615
 * @return string Sitemap URL.
616
 */
617 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...
618
	if ( get_option( 'permalink_structure' ) ) {
619
		$sitemap_url = home_url( '/sitemap.xml' );
620
	} else {
621
		$sitemap_url = home_url( '/?jetpack-sitemap=true' );
622
	}
623
	/**
624
	 * Filter sitemap URL relative to home URL.
625
	 *
626
	 * @module sitemaps
627
	 *
628
	 * @since 3.9.0
629
	 *
630
	 * @param string $sitemap_url Sitemap URL.
631
	 */
632
	return apply_filters( 'jetpack_sitemap_location', $sitemap_url );
633
}
634
635
/**
636
 * Absolute URL of the current blog's news sitemap.
637
 *
638
 * @module sitemaps
639
 */
640 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...
641
	if ( get_option( 'permalink_structure' ) ) {
642
		$news_sitemap_url = home_url( '/news-sitemap.xml' );
643
	} else {
644
		$news_sitemap_url = home_url( '/?jetpack-news-sitemap=true' );
645
	}
646
	/**
647
	 * Filter news sitemap URL relative to home URL.
648
	 *
649
	 * @module sitemaps
650
	 *
651
	 * @since 3.9.0
652
	 *
653
	 * @param string $news_sitemap_url News sitemap URL.
654
	 */
655
	return apply_filters( 'jetpack_news_sitemap_location', $news_sitemap_url );
656
}
657
658
/**
659
 * Output the default sitemap URL.
660
 *
661
 * @module sitemaps
662
 */
663
function jetpack_sitemap_discovery() {
664
	echo 'Sitemap: ' . esc_url( jetpack_sitemap_uri() ) . PHP_EOL;
665
}
666
667
/**
668
 * Output the news sitemap URL.
669
 *
670
 * @module sitemaps
671
 */
672
function jetpack_news_sitemap_discovery() {
673
	echo 'Sitemap: ' . esc_url( jetpack_news_sitemap_uri() ) . PHP_EOL . PHP_EOL;
674
}
675
676
/**
677
 * Clear the sitemap cache when a sitemap action has changed.
678
 *
679
 * @module sitemaps
680
 *
681
 * @param int $post_id unique post identifier. not used.
682
 */
683
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...
684
	delete_transient( 'jetpack_sitemap' );
685
	delete_transient( 'jetpack_news_sitemap' );
686
}
687
688
/**
689
 * Clear sitemap cache when an entry changes. Make sitemaps discoverable to robots. Render sitemaps.
690
 *
691
 * @module sitemaps
692
 */
693
function jetpack_sitemap_initialize() {
694
	add_action( 'publish_post', 'jetpack_sitemap_handle_update', 12, 1 );
695
	add_action( 'publish_page', 'jetpack_sitemap_handle_update', 12, 1 );
696
	add_action( 'trash_post', 'jetpack_sitemap_handle_update', 12, 1 );
697
	add_action( 'deleted_post', 'jetpack_sitemap_handle_update', 12, 1 );
698
699
	/**
700
	 * Filter whether to make the default sitemap discoverable to robots or not.
701
	 *
702
	 * @module sitemaps
703
	 *
704
	 * @since 3.9.0
705
	 *
706
	 * @param bool $discover_sitemap Make default sitemap discoverable to robots.
707
	 */
708
	$discover_sitemap = apply_filters( 'jetpack_sitemap_generate', true );
709 View Code Duplication
	if ( $discover_sitemap ) {
710
		add_action( 'do_robotstxt', 'jetpack_sitemap_discovery', 5, 0 );
711
712
		// Sitemap XML
713
		if ( preg_match( '#(/sitemap\.xml)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-sitemap'] ) && 'true' == $_GET['jetpack-sitemap'] ) ) {
714
			// run later so things like custom post types have been registered
715
			add_action( 'init', 'jetpack_print_sitemap', 999 );
716
		}
717
718
		// XSLT for sitemap
719
		if ( preg_match( '#(/sitemap\.xsl)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-sitemap-xsl'] ) && 'true' == $_GET['jetpack-sitemap-xsl'] ) ) {
720
			add_action( 'init', 'jetpack_print_sitemap_xsl' );
721
		}
722
	}
723
724
	/**
725
	 * Filter whether to make the news sitemap discoverable to robots or not.
726
	 *
727
	 * @module sitemaps
728
	 *
729
	 * @since 3.9.0
730
	 *
731
	 * @param bool $discover_news_sitemap Make default news sitemap discoverable to robots.
732
	 */
733
	$discover_news_sitemap = apply_filters( 'jetpack_news_sitemap_generate', true );
734 View Code Duplication
	if ( $discover_news_sitemap ) {
735
		add_action( 'do_robotstxt', 'jetpack_news_sitemap_discovery', 5, 0 );
736
737
		// News Sitemap XML
738
		if ( preg_match( '#(/news-sitemap\.xml)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-news-sitemap'] ) && 'true' == $_GET['jetpack-news-sitemap'] ) ) {
739
			// run later so things like custom post types have been registered
740
			add_action( 'init', 'jetpack_print_news_sitemap', 999 );
741
		}
742
743
		// XSLT for sitemap
744
		if ( preg_match( '#(/news-sitemap\.xsl)$#i', $_SERVER['REQUEST_URI'] ) || ( isset( $_GET['jetpack-news-sitemap-xsl'] ) && 'true' == $_GET['jetpack-news-sitemap-xsl'] ) ) {
745
			add_action( 'init', 'jetpack_print_news_sitemap_xsl' );
746
		}
747
	}
748
}
749
750
// Initialize sitemaps once themes can filter the initialization.
751
add_action( 'after_setup_theme', 'jetpack_sitemap_initialize' );