summaryrefslogtreecommitdiff
blob: ae6335ce033d509c15249ae5585b0eadd66b7651 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
 * The functions in this class provide an API for handling
 * sitemap related URIs.
 *
 * @package Jetpack
 * @since 4.8.0
 * @author Automattic
 */

/**
 * The Jetpack_Sitemap_Finder object deals with constructing
 * sitemap URIs.
 *
 * @since 4.8.0
 */
class Jetpack_Sitemap_Finder {

	/**
	 * Construct the complete URL of a sitemap file. Depends on
	 * permalink settings.
	 *
	 * @access public
	 * @since 4.8.0
	 * @since 4.8.1 Call jetpack_sitemap_uri()
	 *
	 * @param string $filename The filename of the sitemap.
	 *
	 * @return string Complete URI of the given sitemap file.
	 */
	public function construct_sitemap_url( $filename ) {
		$url = jetpack_sitemap_uri( $filename );

		if ( pathinfo( $filename, PATHINFO_EXTENSION ) === 'xsl' ) {
			// strip scheme for sites where sitemap could be access via http or https
			$url = preg_replace( '/^https?:/', '', $url );
		}

		return $url;
	}

	/**
	 * Path and query prefix of sitemap files. Depends on permalink
	 * settings.
	 *
	 * @access public
	 * @since 4.8.0
	 *
	 * @return string The path+query prefix.
	 */
	public function the_jetpack_sitemap_path_and_query_prefix() {
		global $wp_rewrite;

		// Get path fragment from home_url().
		$home = wp_parse_url( home_url() );
		if ( isset( $home['path'] ) ) {
			$home_path = $home['path'];
		} else {
			$home_path = '';
		}

		// Get additional path fragment from filter.
		$location = Jetpack_Options::get_option_and_ensure_autoload(
			'jetpack_sitemap_location',
			''
		);

		if ( $wp_rewrite->using_index_permalinks() ) {
			return $home_path . '/index.php' . $location . '/';
		} elseif ( $wp_rewrite->using_permalinks() ) {
			return $home_path . $location . '/';
		} else {
			return $home_path . $location . '/?jetpack-sitemap=';
		}
	}

	/**
	 * Examine a path+query URI fragment looking for a sitemap request.
	 *
	 * @access public
	 * @since 4.8.0
	 *
	 * @param string $raw_uri A URI (path+query only) to test for sitemap-ness.
	 *
	 * @return array @args {
	 *   @type string $sitemap_name The recognized sitemap name (or null).
	 * }
	 */
	public function recognize_sitemap_uri( $raw_uri ) {
		// The path+query where sitemaps are served.
		$sitemap_path = $this->the_jetpack_sitemap_path_and_query_prefix();

		// A regex which detects $sitemap_path at the beginning of a string.
		$path_regex = '/^' . preg_quote( $sitemap_path, '/' ) . '/';

		// Check that the request URI begins with the sitemap path.
		if ( preg_match( $path_regex, $raw_uri ) ) {
			// Strip off the $sitemap_path and any trailing slash.
			$stripped_uri = preg_replace( $path_regex, '', rtrim( $raw_uri, '/' ) );
		} else {
			$stripped_uri = '';
		}

		// Check that the stripped uri begins with one of the sitemap prefixes.
		if ( preg_match( '/^sitemap|^image-s|^news-s|^video-s/', $stripped_uri ) ) {
			$filename = $stripped_uri;
		} else {
			$filename = null;
		}

		return array(
			'sitemap_name' => $filename,
		);
	}

}