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 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
CacheController
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 clear_cache
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2namespace NewfoldLabs\WP\Module\Patterns\Api\Controllers;
3
4use NewfoldLabs\WP\Module\Patterns\SiteClassification;
5use NewfoldLabs\WP\Module\Data\WonderBlocks\Requests\Fetch as WonderBlocksFetchRequest;
6use NewfoldLabs\WP\Module\Data\WonderBlocks\WonderBlocks;
7use NewfoldLabs\WP\Module\Patterns\CSSUtilities;
8
9/**
10 * Controller for cache.
11 */
12class CacheController {
13
14    /**
15     * Clears the cached data based on the specified type.
16     *
17     * This endpoint handler clears cached data for patterns, templates, and/or categories
18     * depending on the type parameter. If no type is specified, all caches will be cleared.
19     * Additionally, refreshes CSS utility assets after clearing the cache.
20     *
21     * @param \WP_REST_Request $request The REST API request.
22     * @return \WP_REST_Response Response with status of cache clearing operations.
23     */
24    public static function clear_cache( \WP_REST_Request $request ) {
25
26        $type = $request->get_param( 'type' );
27
28        $primary_type   = SiteClassification::get_primary_type();
29        $secondary_type = SiteClassification::get_secondary_type();
30
31        // Initialize response
32        $response = array();
33
34        if ( ! $type || 'patterns' === $type ) {
35            // Clear cache for patterns
36            $pattern_request = new WonderBlocksFetchRequest(
37                array(
38                    'endpoint'       => 'patterns',
39                    'primary_type'   => $primary_type,
40                    'secondary_type' => $secondary_type,
41                )
42            );
43            WonderBlocks::clear_cache( $pattern_request );
44            $response['patterns'] = 'Cache cleared';
45        }
46
47        if ( ! $type || 'templates' === $type ) {
48            // Clear cache for templates
49            $template_request = new WonderBlocksFetchRequest(
50                array(
51                    'endpoint'       => 'templates',
52                    'primary_type'   => $primary_type,
53                    'secondary_type' => $secondary_type,
54                )
55            );
56            WonderBlocks::clear_cache( $template_request );
57            $response['templates'] = 'Cache cleared';
58        }
59
60        if ( ! $type || 'categories' === $type ) {
61
62            // Clear cache for categories
63            $category_request = new WonderBlocksFetchRequest(
64                array(
65                    'endpoint'       => 'categories',
66                    'slug'           => 'patterns',
67                    'primary_type'   => $primary_type,
68                    'secondary_type' => $secondary_type,
69                )
70            );
71            WonderBlocks::clear_cache( $category_request );
72
73            $category_request = new WonderBlocksFetchRequest(
74                array(
75                    'endpoint'       => 'categories',
76                    'slug'           => 'templates',
77                    'primary_type'   => $primary_type,
78                    'secondary_type' => $secondary_type,
79                )
80            );
81            WonderBlocks::clear_cache( $category_request );
82
83            $response['categories'] = 'Cache cleared';
84        }
85
86        // Refresh the CSS utilities assets.
87        CSSUtilities::get_instance()->refresh_assets();
88
89        return new \WP_REST_Response( $response, 200 );
90    }
91}