Completed
Push — add/extra-backup-api-calls ( 6e392d...6ff07c )
by
unknown
11:01
created

Jetpack_Sitemap_Finder   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

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

3 Methods

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