Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
SiteCapabilities
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
7 / 7
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 update
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 all
100.00% covered (success)
100.00%
8 / 8
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%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace NewfoldLabs\WP\Module\Data;
4
5use NewfoldLabs\WP\Module\Data\API\Capabilities;
6use NewfoldLabs\WP\Module\Data\Helpers\Transient;
7
8/**
9 * Class SiteCapabilities
10 *
11 * Class that handles fetching, caching, and checking of site capabilities.
12 *
13 * @package NewfoldLabs\WP\Module\Data
14 */
15class SiteCapabilities {
16
17    /**
18     * Implementation of transient functionality which uses the WordPress options table when an object cache is present.
19     *
20     * @var Transient
21     */
22    protected $transient;
23
24    /**
25     * Hiive connection manager
26     *
27     * @var HiiveConnection
28     */
29    protected $hiive;
30
31    /**
32     * Constructor.
33     *
34     * @param ?Transient $transient Inject instance of Transient class.
35     * @param ?HiiveConnection $hiive Inject instance of the hiive connection manager.
36     */
37    public function __construct( ?Transient $transient = null, ?HiiveConnection $hiive = null ) {
38        $this->transient = $transient ?? new Transient();
39        $this->hiive     = $hiive ?? new HiiveConnection();
40    }
41
42    /**
43     * Get the value of a capability.
44     *
45     * @used-by \NewfoldLabs\WP\Module\AI\SiteGen\SiteGen::check_capabilities()
46     * @used-by \NewfoldLabs\WP\Module\AI\Utils\AISearchUtil::check_capabilities()
47     * @used-by \NewfoldLabs\WP\Module\AI\Utils\AISearchUtil::check_help_capability()
48     * @used-by \NewfoldLabs\WP\Module\ECommerce\ECommerce::__construct()
49     * @used-by \NewfoldLabs\WP\Module\HelpCenter\CapabilityController::get_capability()
50     * @used-by \NewfoldLabs\WP\Module\Onboarding\Data\Config::get_site_capability()
51     *
52     * @param string $capability Capability name.
53     */
54    public function get( string $capability ): bool {
55        return $this->exists( $capability ) && $this->all()[ $capability ];
56    }
57
58    /**
59     * Merge a new list of capabilities into the existing list and save.
60     *
61     * @used-by Capabilities::update()
62     *
63     * @param array<string, bool> $capabilities The capabilities array.
64     *
65     * @return bool True if the value was changed, false otherwise.
66     */
67    public function update( array $capabilities ): bool {
68        $updated_capabilities = array_merge( $this->all( false ), $capabilities );
69        return $this->set( $updated_capabilities );
70    }
71
72    /**
73     * Save a list of capabilities, overwriting the existing list.
74     *
75     * @used-by self::fetch()
76     * @used-by Capabilities::update()
77     *
78     * @param array<string, bool> $capabilities The capabilities array.
79     *
80     * @return bool True if the value was set, false otherwise.
81     */
82    public function set( array $capabilities ): bool {
83        return $this->transient->set( 'nfd_site_capabilities', $capabilities, 4 * constant( 'HOUR_IN_SECONDS' ) );
84    }
85
86    /**
87     * Get all capabilities.
88     *
89     * @param bool $fetch_when_absent Make a request to Hiive to fetch capabilities when not present in cache (default: `true`).
90     *
91     * @return array<string, bool> List of capabilities and if they are enabled or not.
92     */
93    public function all( bool $fetch_when_absent = true ): array {
94        $capabilities = $this->transient->get( 'nfd_site_capabilities' );
95
96        if ( is_array( $capabilities ) ) {
97            return $capabilities;
98        }
99
100        if ( $fetch_when_absent ) {
101            $capabilities = $this->fetch();
102            $this->set( $capabilities );
103            return $capabilities;
104        }
105
106        return array();
107    }
108
109    /**
110     * Check if a capability exists.
111     *
112     * @param string $capability Capability name.
113     */
114    protected function exists( string $capability ): bool {
115        return array_key_exists( $capability, $this->all() );
116    }
117
118    /**
119     * Fetch all capabilities from Hiive.
120     *
121     * @return array<string, bool>
122     */
123    protected function fetch(): array {
124
125        $response = $this->hiive->hiive_request(
126            'sites/v1/capabilities',
127            null,
128            array(
129                'method' => 'GET',
130            )
131        );
132
133        if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
134            return array();
135        }
136
137        $body = wp_remote_retrieve_body( $response );
138        $data = json_decode( $body, true );
139
140        return is_array( $data ) ? $data : array();
141    }
142}