Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.00% covered (success)
96.00%
24 / 25
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SiteCapabilities
96.00% covered (success)
96.00%
24 / 25
80.00% covered (warning)
80.00%
4 / 5
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 all
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 exists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fetch
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2
3namespace NewfoldLabs\WP\Module\Data;
4
5use NewfoldLabs\WP\Module\Data\Helpers\Transient;
6
7/**
8 * Class SiteCapabilities
9 *
10 * Class that handles fetching, caching, and checking of site capabilities.
11 *
12 * @package NewfoldLabs\WP\Module\Data
13 */
14class SiteCapabilities {
15
16    /**
17     * Implementation of transient functionality which uses the WordPress options table when an object cache is present.
18     *
19     * @var Transient
20     */
21    protected $transient;
22
23    /**
24     * Constructor.
25     *
26     * @param ?Transient $transient Inject instance of Transient class.
27     */
28    public function __construct( ?Transient $transient = null ) {
29        $this->transient = $transient ?? new Transient();
30    }
31
32    /**
33     * Get the value of a capability.
34     *
35     * @used-by \NewfoldLabs\WP\Module\AI\SiteGen\SiteGen::check_capabilities()
36     * @used-by \NewfoldLabs\WP\Module\AI\Utils\AISearchUtil::check_capabilities()
37     * @used-by \NewfoldLabs\WP\Module\AI\Utils\AISearchUtil::check_help_capability()
38     * @used-by \NewfoldLabs\WP\Module\ECommerce\ECommerce::__construct()
39     * @used-by \NewfoldLabs\WP\Module\HelpCenter\CapabilityController::get_capability()
40     * @used-by \NewfoldLabs\WP\Module\Onboarding\Data\Config::get_site_capability()
41     *
42     * @param string $capability Capability name.
43     */
44    public function get( string $capability ): bool {
45        return $this->exists( $capability ) && $this->all()[ $capability ];
46    }
47
48    /**
49     * Get all capabilities.
50     *
51     * @used-by \NewfoldLabs\WP\Module\Runtime\Runtime::prepareRuntime()
52     */
53    public function all(): array {
54        $capabilities = $this->transient->get( 'nfd_site_capabilities' );
55        if ( false === $capabilities ) {
56            $capabilities = $this->fetch();
57            $this->transient->set( 'nfd_site_capabilities', $capabilities, 4 * constant( 'HOUR_IN_SECONDS' ) );
58        }
59
60        return is_array( $capabilities ) ? $capabilities : array();
61    }
62
63    /**
64     * Check if a capability exists.
65     *
66     * @param string $capability Capability name.
67     */
68    protected function exists( string $capability ): bool {
69        return array_key_exists( $capability, $this->all() );
70    }
71
72    /**
73     * Fetch all capabilities from Hiive.
74     *
75     * @return array<string, bool>
76     */
77    protected function fetch(): array {
78        $capabilities = array();
79
80        $response = wp_remote_get(
81            constant( 'NFD_HIIVE_URL' ) . '/sites/v1/capabilities',
82            array(
83                'headers' => array(
84                    'Content-Type'  => 'application/json',
85                    'Accept'        => 'application/json',
86                    'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(),
87                ),
88            )
89        );
90
91        if ( wp_remote_retrieve_response_code( $response ) === 200 && ! is_wp_error( $response ) ) {
92            $body = wp_remote_retrieve_body( $response );
93            $data = json_decode( $body, true );
94            if ( $data && is_array( $data ) ) {
95                $capabilities = $data;
96            }
97        }
98
99        return $capabilities;
100    }
101}