Completed
Push — fix/add-people-link ( 0f6c6a...c735e5 )
by
unknown
486:12 queued 476:02
created

Jetpack_Sitemap_Finder   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 105
rs 10
c 0
b 0
f 0
wmc 10
lcom 0
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A construct_sitemap_url() 0 16 3
B the_jetpack_sitemap_path_and_query_prefix() 0 25 4
B recognize_sitemap_uri() 0 26 3
1
<?php
2
/**
3
 * The functions in this class provide an API for handling
4
 * sitemap related URIs.
5
 *
6
 * @package Jetpack
7
 * @since 4.7.0
8
 * @author Automattic
9
 */
10
11
/**
12
 * The Jetpack_Sitemap_Finder object deals with constructing
13
 * sitemap URIs.
14
 *
15
 * @since 4.7.0
16
 */
17
class Jetpack_Sitemap_Finder {
18
19
	/**
20
	 * Construct the complete URL of a sitemap file. Depends on
21
	 * permalink settings.
22
	 *
23
	 * @access public
24
	 * @since 4.7.0
25
	 *
26
	 * @param string $filename The filename of the sitemap.
27
	 *
28
	 * @return string Complete URI of the given sitemap file.
29
	 */
30
	public function construct_sitemap_url( $filename ) {
31
		global $wp_rewrite;
32
33
		$location = Jetpack_Options::get_option_and_ensure_autoload(
34
			'jetpack_sitemap_location',
35
			''
36
		);
37
38
		if ( $wp_rewrite->using_index_permalinks() ) {
39
			return home_url( '/index.php' . $location . '/' . $filename );
40
		} elseif ( $wp_rewrite->using_permalinks() ) {
41
			return home_url( $location . '/' . $filename );
42
		} else {
43
			return home_url( $location . '/?jetpack-sitemap=' . $filename );
44
		}
45
	}
46
47
	/**
48
	 * Path and query prefix of sitemap files. Depends on permalink
49
	 * settings.
50
	 *
51
	 * @access public
52
	 * @since 4.7.0
53
	 *
54
	 * @return string The path+query prefix.
55
	 */
56
	public function the_jetpack_sitemap_path_and_query_prefix() {
57
		global $wp_rewrite;
58
59
		// Get path fragment from home_url().
60
		$home = wp_parse_url( home_url() );
61
		if ( isset( $home['path'] ) ) {
62
			$home_path = $home['path'];
63
		} else {
64
			$home_path = '';
65
		}
66
67
		// Get additional path fragment from filter.
68
		$location = Jetpack_Options::get_option_and_ensure_autoload(
69
			'jetpack_sitemap_location',
70
			''
71
		);
72
73
		if ( $wp_rewrite->using_index_permalinks() ) {
74
			return $home_path . '/index.php' . $location . '/';
75
		} elseif ( $wp_rewrite->using_permalinks() ) {
76
			return $home_path . $location . '/';
77
		} else {
78
			return $home_path . $location . '/?jetpack-sitemap=';
79
		}
80
	}
81
82
	/**
83
	 * Examine a path+query URI fragment looking for a sitemap request.
84
	 *
85
	 * @access public
86
	 * @since 4.7.0
87
	 *
88
	 * @param string $raw_uri A URI (path+query only) to test for sitemap-ness.
89
	 *
90
	 * @return array @args {
91
	 *   @type string $sitemap_name The recognized sitemap name (or null).
92
	 * }
93
	 */
94
	public function recognize_sitemap_uri( $raw_uri ) {
95
		// The path+query where sitemaps are served.
96
		$sitemap_path = $this->the_jetpack_sitemap_path_and_query_prefix();
97
98
		// A regex which detects $sitemap_path at the beginning of a string.
99
		$path_regex = '/^' . preg_quote( $sitemap_path, '/' ) . '/';
100
101
		// Check that the request URI begins with the sitemap path.
102
		if ( preg_match( $path_regex, $raw_uri ) ) {
103
			// Strip off the $sitemap_path and any trailing slash.
104
			$stripped_uri = preg_replace( $path_regex, '', rtrim( $raw_uri, '/' ) );
105
		} else {
106
			$stripped_uri = '';
107
		}
108
109
		// Check that the stripped uri begins with one of the sitemap prefixes.
110
		if ( preg_match( '/^sitemap|^image-s|^news-s|^video-s/', $stripped_uri ) ) {
111
			$filename = $stripped_uri;
112
		} else {
113
			$filename = null;
114
		}
115
116
		return array(
117
			'sitemap_name' => $filename,
118
		);
119
	}
120
121
}
122