Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.29% covered (warning)
85.29%
29 / 34
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Sitemap_Finder
90.62% covered (success)
90.62%
29 / 32
33.33% covered (danger)
33.33%
1 / 3
11.10
0.00% covered (danger)
0.00%
0 / 1
 construct_sitemap_url
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 the_jetpack_sitemap_path_and_query_prefix
84.62% covered (warning)
84.62%
11 / 13
0.00% covered (danger)
0.00%
0 / 1
4.06
 recognize_sitemap_uri
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
5
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * The functions in this class provide an API for handling
4 * sitemap related URIs.
5 *
6 * @package automattic/jetpack
7 * @since 4.8.0
8 * @author Automattic
9 */
10
11if ( ! defined( 'ABSPATH' ) ) {
12    exit( 0 );
13}
14
15/**
16 * The Jetpack_Sitemap_Finder object deals with constructing
17 * sitemap URIs.
18 *
19 * @since 4.8.0
20 */
21class Jetpack_Sitemap_Finder {
22
23    /**
24     * Construct the complete URL of a sitemap file. Depends on
25     * permalink settings.
26     *
27     * @access public
28     * @since 4.8.0
29     * @since 4.8.1 Call jetpack_sitemap_uri()
30     *
31     * @param string $filename The filename of the sitemap.
32     *
33     * @return string Complete URI of the given sitemap file.
34     */
35    public function construct_sitemap_url( $filename ) {
36        $url = jetpack_sitemap_uri( $filename );
37
38        if ( in_array( pathinfo( $filename, PATHINFO_EXTENSION ), array( 'xsl', 'css' ), true ) ) {
39            // Strip scheme for sites where sitemap could be access via http or https.
40            $url = preg_replace( '/^https?:/', '', $url );
41        }
42
43        return $url;
44    }
45
46    /**
47     * Path and query prefix of sitemap files. Depends on permalink
48     * settings.
49     *
50     * @access public
51     * @since 4.8.0
52     *
53     * @return string The path+query prefix.
54     */
55    public function the_jetpack_sitemap_path_and_query_prefix() {
56        global $wp_rewrite;
57
58        // Get path fragment from home_url().
59        $home = wp_parse_url( home_url() );
60        if ( isset( $home['path'] ) ) {
61            $home_path = $home['path'];
62        } else {
63            $home_path = '';
64        }
65
66        // Get additional path fragment from filter.
67        $location = Jetpack_Options::get_option_and_ensure_autoload(
68            'jetpack_sitemap_location',
69            ''
70        );
71
72        if ( $wp_rewrite->using_index_permalinks() ) {
73            return $home_path . '/index.php' . $location . '/';
74        } elseif ( $wp_rewrite->using_permalinks() ) {
75            return $home_path . $location . '/';
76        } else {
77            return $home_path . $location . '/?jetpack-sitemap=';
78        }
79    }
80
81    /**
82     * Examine a path+query URI fragment looking for a sitemap request.
83     *
84     * @access public
85     * @since 4.8.0
86     *
87     * @param string $raw_uri A URI (path+query only) to test for sitemap-ness.
88     *
89     * @return array @args {
90     *   @type string $sitemap_name The recognized sitemap name (or null).
91     * }
92     */
93    public function recognize_sitemap_uri( $raw_uri ) {
94        $query = array();
95        wp_parse_str( (string) wp_parse_url( $raw_uri, PHP_URL_QUERY ), $query );
96        if ( isset( $query['jetpack-sitemap'] ) && 'sitemap-labels.css' === $query['jetpack-sitemap'] ) {
97            return array( 'sitemap_name' => 'sitemap-labels.css' );
98        }
99
100        // The path+query where sitemaps are served.
101        $sitemap_path = $this->the_jetpack_sitemap_path_and_query_prefix();
102
103        // A regex which detects $sitemap_path at the beginning of a string.
104        $path_regex = '/^' . preg_quote( $sitemap_path, '/' ) . '/';
105
106        // Check that the request URI begins with the sitemap path.
107        if ( preg_match( $path_regex, $raw_uri ) ) {
108            // Strip off the $sitemap_path and any trailing slash.
109            $stripped_uri = preg_replace( $path_regex, '', rtrim( $raw_uri, '/' ) );
110        } else {
111            $stripped_uri = '';
112        }
113
114        // Check that the stripped uri begins with one of the sitemap prefixes.
115        if ( preg_match( '/^sitemap|^image-s|^news-s|^video-s/', $stripped_uri ) ) {
116            $filename = $stripped_uri;
117        } else {
118            $filename = null;
119        }
120
121        return array(
122            'sitemap_name' => $filename,
123        );
124    }
125}