Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
24 / 30
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_Sitemap_Finder
85.71% covered (warning)
85.71%
24 / 28
33.33% covered (danger)
33.33%
1 / 3
9.24
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
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
4.20
 recognize_sitemap_uri
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
3
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 ( pathinfo( $filename, PATHINFO_EXTENSION ) === 'xsl' ) {
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        // The path+query where sitemaps are served.
95        $sitemap_path = $this->the_jetpack_sitemap_path_and_query_prefix();
96
97        // A regex which detects $sitemap_path at the beginning of a string.
98        $path_regex = '/^' . preg_quote( $sitemap_path, '/' ) . '/';
99
100        // Check that the request URI begins with the sitemap path.
101        if ( preg_match( $path_regex, $raw_uri ) ) {
102            // Strip off the $sitemap_path and any trailing slash.
103            $stripped_uri = preg_replace( $path_regex, '', rtrim( $raw_uri, '/' ) );
104        } else {
105            $stripped_uri = '';
106        }
107
108        // Check that the stripped uri begins with one of the sitemap prefixes.
109        if ( preg_match( '/^sitemap|^image-s|^news-s|^video-s/', $stripped_uri ) ) {
110            $filename = $stripped_uri;
111        } else {
112            $filename = null;
113        }
114
115        return array(
116            'sitemap_name' => $filename,
117        );
118    }
119}