Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
58.57% |
41 / 70 |
|
40.00% |
2 / 5 |
CRAP | |
0.00% |
0 / 1 |
| EntitlementsApi | |
58.57% |
41 / 70 |
|
40.00% |
2 / 5 |
25.02 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_routes | |
90.91% |
20 / 22 |
|
0.00% |
0 / 1 |
1.00 | |||
| set_the_transient | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_entitlements_data | |
28.57% |
8 / 28 |
|
0.00% |
0 / 1 |
31.32 | |||
| activate_plugins | |
56.25% |
9 / 16 |
|
0.00% |
0 / 1 |
2.33 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Solutions; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\HiiveConnection; |
| 6 | use WP_Error; |
| 7 | use WP_HTTP_Response; |
| 8 | use WP_REST_Controller; |
| 9 | use WP_REST_Response; |
| 10 | use WP_REST_Server; |
| 11 | |
| 12 | /** |
| 13 | * Class EntitlementsApi |
| 14 | */ |
| 15 | class EntitlementsApi { |
| 16 | |
| 17 | /** |
| 18 | * Transient name where data is stored. |
| 19 | */ |
| 20 | const TRANSIENT = 'newfold_solutions'; |
| 21 | |
| 22 | /** |
| 23 | * Hiive API endpoint for fetching site entitlements. |
| 24 | */ |
| 25 | const HIIVE_API_ENTITLEMENTS_ENDPOINT = '/sites/v1/entitlements'; |
| 26 | |
| 27 | /** |
| 28 | * Instance of the HiiveConnection class. |
| 29 | * |
| 30 | * @var HiiveConnection |
| 31 | */ |
| 32 | private $hiive; |
| 33 | |
| 34 | /** |
| 35 | * REST namespace |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | private $namespace; |
| 40 | |
| 41 | /** |
| 42 | * REST base |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | private $rest_base; |
| 47 | |
| 48 | /** |
| 49 | * Default empty response. |
| 50 | * |
| 51 | * @var array |
| 52 | */ |
| 53 | public static $default_response = array( |
| 54 | 'message' => 'Not allowed to load entitlements from server.', |
| 55 | 'solution' => false, |
| 56 | 'categories' => array(), |
| 57 | 'solutions' => array(), |
| 58 | 'entitlements' => array(), |
| 59 | 'premium' => array(), |
| 60 | ); |
| 61 | |
| 62 | |
| 63 | /** |
| 64 | * EntitilementsApi constructor. |
| 65 | * |
| 66 | * @param HiiveConnection $hiive Instance of the HiiveConnection class. |
| 67 | */ |
| 68 | public function __construct( HiiveConnection $hiive ) { |
| 69 | $this->hiive = $hiive; |
| 70 | $this->namespace = 'newfold-solutions/v1'; |
| 71 | $this->rest_base = '/entitlements'; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Register Entitlement routes. |
| 76 | */ |
| 77 | public function register_routes() { |
| 78 | |
| 79 | // Add route for fetching entitlements |
| 80 | register_rest_route( |
| 81 | $this->namespace, |
| 82 | $this->rest_base, |
| 83 | array( |
| 84 | 'methods' => \WP_REST_Server::READABLE, |
| 85 | 'callback' => array( $this, 'get_entitlements_data' ), |
| 86 | 'permission_callback' => function () { |
| 87 | return current_user_can( 'manage_options' ); |
| 88 | }, |
| 89 | ) |
| 90 | ); |
| 91 | |
| 92 | // Add route for activating plugins |
| 93 | register_rest_route( |
| 94 | $this->namespace, |
| 95 | '/activate_plugin', |
| 96 | array( |
| 97 | 'methods' => \WP_REST_Server::CREATABLE, |
| 98 | 'callback' => array( $this, 'activate_plugins' ), |
| 99 | 'permission_callback' => function () { |
| 100 | return current_user_can( 'manage_options' ); |
| 101 | }, |
| 102 | ) |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set the transient where entitlements are stored (6 Hours). |
| 108 | * |
| 109 | * @param array $data Data to be stored |
| 110 | * @param float|int $expiration Transient expiration. |
| 111 | */ |
| 112 | public function set_the_transient( $data, $expiration = 21600 ) { |
| 113 | set_transient( self::TRANSIENT, $data, $expiration ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get entitlements of a site. |
| 118 | * |
| 119 | * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure. |
| 120 | */ |
| 121 | public function get_entitlements_data() { |
| 122 | $entitlements = get_transient( self::TRANSIENT ); |
| 123 | |
| 124 | if ( false === $entitlements ) { |
| 125 | |
| 126 | // TODO: update response to be available without connection and return solutions categories and premium |
| 127 | // If there is no Hiive connection, bail. |
| 128 | if ( ! HiiveConnection::is_connected() ) { |
| 129 | // If no connection, give an empty response. |
| 130 | return new WP_REST_Response( |
| 131 | self::$default_response, |
| 132 | 200 |
| 133 | ); |
| 134 | } |
| 135 | |
| 136 | // Get fresh entitlements data from Hiive API |
| 137 | $response = wp_remote_get( |
| 138 | NFD_HIIVE_URL . self::HIIVE_API_ENTITLEMENTS_ENDPOINT, |
| 139 | array( |
| 140 | 'headers' => array( |
| 141 | 'Content-Type' => 'application/json', |
| 142 | 'Accept' => 'application/json', |
| 143 | 'Authorization' => 'Bearer ' . HiiveConnection::get_auth_token(), |
| 144 | ), |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | if ( is_wp_error( $response ) ) { |
| 149 | return new WP_REST_Response( array( 'message' => 'An error occurred with the entitlements response.' ), 500 ); |
| 150 | } |
| 151 | |
| 152 | $body = wp_remote_retrieve_body( $response ); |
| 153 | $data = json_decode( $body, true ); |
| 154 | if ( |
| 155 | $data && |
| 156 | is_array( $data ) && |
| 157 | array_key_exists( 'solutions', $data ) && |
| 158 | array_key_exists( 'categories', $data ) |
| 159 | ) { |
| 160 | $entitlements = $data; |
| 161 | $this->set_the_transient( $entitlements ); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return new WP_REST_Response( $entitlements, 200 ); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Activate the plugin based on Plugin path |
| 170 | * |
| 171 | * @param \WP_REST_REQUEST $request Data to be stored |
| 172 | */ |
| 173 | public function activate_plugins( $request ) { |
| 174 | $plugin_path = json_decode( $request->get_body() )->plugin; |
| 175 | if ( $plugin_path ) { |
| 176 | activate_plugin( $plugin_path ); |
| 177 | return new \WP_REST_Response( |
| 178 | array( |
| 179 | 'message' => __( 'Activated the plugin successfully!', 'wp-module-solutions' ), |
| 180 | ), |
| 181 | 201 |
| 182 | ); |
| 183 | } |
| 184 | return new \WP_Error( |
| 185 | 'nfd_module_solution_error', |
| 186 | __( 'Please send valid plugin', 'wp-module-solutions' ), |
| 187 | array( |
| 188 | 'status' => 400, |
| 189 | ), |
| 190 | ); |
| 191 | } |
| 192 | } |