Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
2 / 5
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CacheExclusion
40.00% covered (danger)
40.00%
2 / 5
33.33% covered (danger)
33.33%
1 / 3
4.94
0.00% covered (danger)
0.00%
0 / 1
 normalize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 add_to_runtime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace NewfoldLabs\WP\Module\Performance\Cache;
3
4use NewfoldLabs\WP\ModuleLoader\Container;
5
6use function NewfoldLabs\WP\Module\Performance\get_default_cache_exclusions;
7
8/**
9 * Cache Exclusion Class
10 */
11class CacheExclusion {
12    /**
13     * Dependency injection container.
14     *
15     * @var Container
16     */
17    protected $container;
18
19    /**
20     * Option used to store all pages should be excluded from cache.
21     *
22     * @var string
23     */
24    const OPTION_CACHE_EXCLUSION = 'nfd_performance_cache_exclusion';
25
26    /**
27     * Regex for validating cache exclusion value. Must match frontend (CacheExclusion section).
28     * Allows: empty string, or lowercase letters, numbers, commas, hyphens only.
29     *
30     * @var string
31     */
32    const CACHE_EXCLUSION_VALIDATE_REGEX = '/^[a-z0-9,-]*$/';
33
34    /**
35     * Normalize cache exclusion input to match frontend: strip all whitespace, remove trailing comma.
36     *
37     * @param string $value Raw cache exclusion value.
38     * @return string Normalized value.
39     */
40    public static function normalize( $value ) {
41        $normalized = preg_replace( '/\s+/', '', (string) $value );
42        return preg_replace( '/,$/', '', $normalized );
43    }
44
45    /**
46     * Constructor.
47     *
48     * @param Container $container the container
49     */
50    public function __construct( Container $container ) {
51        $this->container = $container;
52
53        add_filter( 'newfold-runtime', array( $this, 'add_to_runtime' ) );
54    }
55    /**
56     * Add values to the runtime object.
57     *
58     * @param array $sdk The runtime object.
59     *
60     * @return array
61     */
62    public function add_to_runtime( $sdk ) {
63        return array_merge( $sdk, array( 'cacheExclusion' => get_option( self::OPTION_CACHE_EXCLUSION, get_default_cache_exclusions() ) ) );
64    }
65}