Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Page_Cache_Entry
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 get
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 set
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 sanitize_value
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
12
 sanitize_wildcards
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Data_Sync;
4
5use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Get;
6use Automattic\Jetpack\WP_JS_Data_Sync\Contracts\Entry_Can_Set;
7use Automattic\Jetpack_Boost\Modules\Optimizations\Page_Cache\Pre_WordPress\Boost_Cache_Settings;
8
9class Page_Cache_Entry implements Entry_Can_Get, Entry_Can_Set {
10    public function get( $_fallback = false ) {
11        $cache_settings = Boost_Cache_Settings::get_instance();
12
13        $settings = array(
14            'bypass_patterns' => $cache_settings->get_bypass_patterns(),
15            'logging'         => $cache_settings->get_logging(),
16        );
17
18        return $settings;
19    }
20
21    public function set( $value ) {
22        $cache_settings = Boost_Cache_Settings::get_instance();
23
24        $value['bypass_patterns'] = $this->sanitize_value( $value['bypass_patterns'] );
25
26        $cache_settings->set( $value );
27    }
28
29    /**
30     * Sanitizes the given value, ensuring that it is list of valid patterns.
31     *
32     * @param mixed $value The value to sanitize.
33     *
34     * @return array The sanitized value.
35     */
36    private function sanitize_value( $value ) {
37        if ( is_array( $value ) ) {
38            $value = array_values( array_unique( array_filter( array_map( 'trim', array_map( 'strtolower', $value ) ) ) ) );
39
40            $home_url = home_url( '/' );
41
42            foreach ( $value as &$path ) {
43                // Strip home URL (both secure and non-secure).
44                $path = str_ireplace(
45                    array(
46                        $home_url,
47                        str_replace( 'http:', 'https:', $home_url ),
48                    ),
49                    array(
50                        '/',
51                        '/',
52                    ),
53                    $path
54                );
55
56                // Remove double shashes.
57                $path = str_replace( '//', '/', $path );
58
59                // Remove symbols, as they are included in the regex check.
60                $path = ltrim( $path, '^' );
61                $path = rtrim( $path, '$' );
62                $path = preg_replace( '/\/\?$/', '', $path );
63
64                // Make sure there's a leading slash.
65                $path = '/' . ltrim( $path, '/' );
66
67                // Fix up any wildcards.
68                $path = $this->sanitize_wildcards( $path );
69            }
70
71            $value = array_values( array_unique( array_filter( $value ) ) );
72        } else {
73            $value = array();
74        }
75
76        return $value;
77    }
78
79    /**
80     * Sanitize wildcards in a given path.
81     *
82     * @param string $path The path to sanitize.
83     * @return string The sanitized path.
84     */
85    private function sanitize_wildcards( $path ) {
86        if ( ! $path ) {
87            return '';
88        }
89
90        $path_components = explode( '/', $path );
91        $arr             = array(
92            '.*'   => '(.*)',
93            '*'    => '(.*)',
94            '(*)'  => '(.*)',
95            '(.*)' => '(.*)',
96        );
97
98        foreach ( $path_components as &$path_component ) {
99            $path_component = strtr( $path_component, $arr );
100        }
101        $path = implode( '/', $path_components );
102
103        return $path;
104    }
105}