Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| RedisCredentialsProvisioner | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
110 | |
0.00% |
0 / 1 |
| provision_enable_redis_via_hosting_api | |
0.00% |
0 / 31 |
|
0.00% |
0 / 1 |
110 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace NewfoldLabs\WP\Module\Performance\Helpers; |
| 4 | |
| 5 | use NewfoldLabs\WP\Module\Data\HiiveConnection; |
| 6 | use NewfoldLabs\WP\Module\Performance\Cache\Types\ObjectCacheErrorCodes; |
| 7 | |
| 8 | /** |
| 9 | * Provisions Redis credentials by calling Hosting UAPI, using Hiive customer context. |
| 10 | */ |
| 11 | final class RedisCredentialsProvisioner { |
| 12 | |
| 13 | /** |
| 14 | * Attempt to enable Redis at the host layer (writes wp-config constants via GT). |
| 15 | * |
| 16 | * @return true|\WP_Error |
| 17 | */ |
| 18 | public static function provision_enable_redis_via_hosting_api() { |
| 19 | if ( ! HiiveConnection::is_connected() ) { |
| 20 | return new \WP_Error( |
| 21 | ObjectCacheErrorCodes::HIIVE_NOT_CONNECTED, |
| 22 | __( 'Object cache cannot be enabled automatically right now. Please contact support.', 'wp-module-performance' ) |
| 23 | ); |
| 24 | } |
| 25 | |
| 26 | $hiive = new HiiveHelper( '/sites/v1/customer', array(), 'GET' ); |
| 27 | $resp = $hiive->send_request(); |
| 28 | |
| 29 | if ( is_wp_error( $resp ) ) { |
| 30 | return $resp; |
| 31 | } |
| 32 | |
| 33 | $data = json_decode( (string) $resp, true ); |
| 34 | if ( ! is_array( $data ) ) { |
| 35 | return new \WP_Error( |
| 36 | ObjectCacheErrorCodes::HUAPI_ERROR, |
| 37 | __( 'Could not enable object cache right now. Please try again later.', 'wp-module-performance' ) |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | $token = isset( $data['huapi_token'] ) ? (string) $data['huapi_token'] : ''; |
| 42 | $site_id = isset( $data['site_id'] ) ? (string) $data['site_id'] : ''; |
| 43 | |
| 44 | if ( '' === $token ) { |
| 45 | return new \WP_Error( |
| 46 | ObjectCacheErrorCodes::HUAPI_TOKEN_UNAVAILABLE, |
| 47 | __( 'Could not enable object cache right now. Please try again later.', 'wp-module-performance' ) |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | if ( '' === $site_id || ! ctype_digit( $site_id ) ) { |
| 52 | return new \WP_Error( |
| 53 | ObjectCacheErrorCodes::HAL_SITE_ID_MISSING, |
| 54 | __( 'Could not enable object cache right now. Please try again later.', 'wp-module-performance' ) |
| 55 | ); |
| 56 | } |
| 57 | |
| 58 | $result = HostingUapiClient::put_site_performance_redis( $token, $site_id, true ); |
| 59 | |
| 60 | if ( is_wp_error( $result ) ) { |
| 61 | return $result; |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | } |