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 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SiteClassification
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
210
0.00% covered (danger)
0.00%
0 / 1
 get
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
 fetch_from_worker
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
30
 fetch_from_static_file
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace NewfoldLabs\WP\Module\Data\SiteClassification;
4
5use NewfoldLabs\WP\Module\Data\HiiveWorker;
6
7/**
8 * Class SiteClassification
9 *
10 * Class that handles fetching and caching of site classification data.
11 *
12 * @package NewfoldLabs\WP\Module\Data
13 */
14class SiteClassification {
15    /**
16     * Name of the site classification data transient.
17     *
18     * @var string
19     */
20    public static $transient_name = 'nfd_data_site_classification';
21
22    /**
23     * Retrieves the site classification data.
24     *
25     * @return array
26     */
27    public static function get() {
28        // Get the current locale of the site.
29        $locale = str_replace( '_', '-', get_locale() );
30
31        $transient_name = self::$transient_name . '-' . $locale;
32        // Checks the transient for data.
33        $classification = get_transient( $transient_name );
34        if ( false !== $classification ) {
35            return $classification;
36        }
37
38        // Fetch data from the worker.
39        $classification = self::fetch_from_worker( $locale );
40        if ( ! empty( $classification ) ) {
41            set_transient( $transient_name, $classification, DAY_IN_SECONDS );
42            return $classification;
43        }
44
45        // Fetch data from the static JSON file.
46        $classification = self::fetch_from_static_file( $locale );
47        if ( ! empty( $classification ) ) {
48            $classification['static'] = true;
49            set_transient( $transient_name, $classification, HOUR_IN_SECONDS );
50            return $classification;
51        }
52
53        // Cache an empty array for an hour if no data could be retrieved.
54        set_transient( $transient_name, array(), HOUR_IN_SECONDS );
55        return array();
56    }
57
58    /**
59     * Fetch site classification data from the CF worker.
60     *
61     * @param string $locale The locale in kebab case.
62     * @return array
63     */
64    public static function fetch_from_worker( $locale = 'en-US' ) {
65        $worker   = new HiiveWorker( 'site-classification' );
66        $response = $worker->request(
67            'GET',
68            array(
69                'headers' => array(
70                    'Accept' => 'application/json',
71                ),
72                'body'    => array(
73                    'locale' => $locale,
74                ),
75            )
76        );
77
78        if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
79            return array();
80        }
81
82        $body = wp_remote_retrieve_body( $response );
83        $data = json_decode( $body, true );
84        if ( ! $data || ! is_array( $data ) ) {
85            return array();
86        }
87
88        return $data;
89    }
90
91    /**
92     * Fetch site classification data from the static JSON file.
93     *
94     * @param string $locale The locale in kebab case.
95     * @return array
96     */
97    public static function fetch_from_static_file( $locale = 'en-US' ) {
98        $filename = realpath( __DIR__ . "/../Data/Static/site-classification-{$locale}.json" );
99
100        if ( ! file_exists( $filename ) ) {
101            if ( 'en-US' === $locale ) {
102                return array();
103            }
104
105            // If the file does not exist and the locale is not en-US, then default to the en-US file.
106            $filename = realpath( __DIR__ . '/../Data/Static/site-classification-en-US.json' );
107            if ( ! file_exists( $filename ) ) {
108                return array();
109            }
110        }
111
112        $data = json_decode( file_get_contents( $filename ), true );
113        if ( ! $data ) {
114            return array();
115        }
116
117        return $data;
118    }
119}