Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.00% covered (warning)
76.00%
114 / 150
41.18% covered (danger)
41.18%
7 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
HiiveConnection
76.00% covered (warning)
76.00%
114 / 150
41.18% covered (danger)
41.18%
7 / 17
82.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 register_verification_hooks
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 rest_api_init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 ajax_verify
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 verify_token
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 is_connected
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 connect
90.32% covered (success)
90.32%
28 / 31
0.00% covered (danger)
0.00%
0 / 1
7.04
 reconnect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 throttle
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_throttle_interval
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 is_throttled
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 send_event
83.33% covered (warning)
83.33%
10 / 12
0.00% covered (danger)
0.00%
0 / 1
3.04
 notify
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
6
 hiive_request
96.67% covered (success)
96.67%
29 / 30
0.00% covered (danger)
0.00%
0 / 1
9
 get_auth_token
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 get_core_data
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
3
 add_plugin_name_version_to_user_agent
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace NewfoldLabs\WP\Module\Data;
4
5use NewfoldLabs\WP\Module\Data\Helpers\Plugin as PluginHelper;
6use NewfoldLabs\WP\Module\Data\Helpers\Transient;
7use WP_Error;
8use function NewfoldLabs\WP\ModuleLoader\container;
9
10/**
11 * Manages a Hiive connection instance and interactions with it
12 */
13class HiiveConnection implements SubscriberInterface {
14
15    /**
16     * Hiive API url
17     *
18     * @var string
19     */
20    private $api;
21
22    /**
23     * Authentication token for data api
24     *
25     * @var string
26     */
27    private $token;
28
29
30    /**
31     * Whether connection attempts are currently throttled
32     *
33     * @var bool
34     */
35    private $throttled;
36
37    /**
38     * The throttle
39     *
40     * @var bool
41     */
42    protected $throttle;
43
44    /**
45     * Construct
46     */
47    public function __construct() {
48
49        if ( ! defined( 'NFD_HIIVE_URL' ) ) {
50            define( 'NFD_HIIVE_URL', 'https://hiive.cloud/api' );
51        }
52
53        $this->api = constant( 'NFD_HIIVE_URL' );
54    }
55
56    /**
57     * Register the hooks required for site verification
58     *
59     * @return void
60     */
61    public function register_verification_hooks() {
62        add_action( 'rest_api_init', array( $this, 'rest_api_init' ) );
63        add_action( 'wp_ajax_nopriv_nfd-hiive-verify', array( $this, 'ajax_verify' ) );
64    }
65
66    /**
67     * Set up REST API routes
68     *
69     * @hooked rest_api_init
70     */
71    public function rest_api_init(): void {
72        $controller = new API\Verify( $this );
73        $controller->register_routes();
74    }
75
76    /**
77     * Process the admin-ajax request
78     *
79     * Hiive will first attempt to verify using the REST API, and fallback to this AJAX endpoint on error.
80     *
81     * Token is generated in {@see self::connect()} using {@see md5()}.
82     *
83     * @hooked wp_ajax_nopriv_nfd-hiive-verify
84     *
85     * @return never
86     */
87    public function ajax_verify() {
88        // PHPCS: Ignore the nonce verification here – the token _is_ a nonce.
89        // @phpcs:ignore WordPress.Security.NonceVerification.Recommended
90        $token = $_REQUEST['token'];
91
92        $is_valid = $this->verify_token( $token );
93        $status   = ( $is_valid ) ? 200 : 400;
94
95        $data = array(
96            'token' => $token,
97            'valid' => $is_valid,
98        );
99        \wp_send_json( $data, $status );
100    }
101
102    /**
103     * Confirm whether verification token is valid
104     *
105     * Token is generated in {@see self::connect()} using {@see md5()}.
106     *
107     * @param string $token Token to verify
108     */
109    public function verify_token( string $token ): bool {
110        $saved_token = Transient::get( 'nfd_data_verify_token' );
111
112        if ( $saved_token && $saved_token === $token ) {
113            Transient::delete( 'nfd_data_verify_token' );
114
115            return true;
116        }
117
118        return false;
119    }
120
121    /**
122     * Check whether site has established connection to hiive
123     *
124     * This is cleared whenever Hiive returns 401 unauthenticated {@see Data::delete_token_on_401_response()}.
125     *
126     * @used-by Data::init()
127     */
128    public static function is_connected(): bool {
129        return (bool) ( self::get_auth_token() );
130    }
131
132    /**
133     * Attempt to connect to Hiive
134     *
135     * @used-by Data::init()
136     * @used-by HiiveConnection::reconnect()
137     *
138     * @param string  $path Additional path to append to the API URL
139     * @param ?string $authorization The authorization header
140     * @return bool Success
141     */
142    public function connect( string $path = '/sites/v2/connect', ?string $authorization = null ): bool {
143
144        if ( $this->is_throttled() ) {
145            return false;
146        }
147
148        $this->throttle();
149
150        $token = md5( \wp_generate_password() );
151        Transient::set( 'nfd_data_verify_token', $token, 5 * constant( 'MINUTE_IN_SECONDS' ) );
152
153        if ( Transient::get( 'nfd_data_verify_token' ) !== $token ) {
154            return false;
155        }
156
157        $data                 = $this->get_core_data();
158        $data['verify_token'] = $token;
159        $data['plugins']      = ( new PluginHelper() )->collect_installed();
160
161        $args = array(
162            'body'     => \wp_json_encode( $data ),
163            'headers'  => array(
164                'Content-Type' => 'application/json',
165                'Accept'       => 'application/json',
166            ),
167            'blocking' => true,
168            'timeout'  => 30,
169        );
170
171        if ( $authorization ) {
172            $args['headers']['Authorization'] = $authorization;
173        }
174
175        $attempts = intval( get_option( 'nfd_data_connection_attempts', 0 ) );
176        \update_option( 'nfd_data_connection_attempts', $attempts + 1 );
177
178        $response = \wp_remote_post( $this->api . $path, $args );
179        $status   = \wp_remote_retrieve_response_code( $response );
180
181        // Created = 201; Updated = 200
182        if ( 201 === $status || 200 === $status ) {
183            $body = json_decode( \wp_remote_retrieve_body( $response ) );
184            if ( ! empty( $body->token ) ) {
185
186                // Token is auto-encrypted using the `pre_update_option_nfd_data_token` hook.
187                \update_option( 'nfd_data_token', $body->token );
188                return true;
189            }
190        }
191        return false;
192    }
193
194    /**
195     * Rename the site URL in Hiive.
196     *
197     * This performs almost the same request as {@see self::connect} but includes the Site authorization token,
198     * to verify this site is the owner of the existing site in Hiive, and Hiive pings back the new URL to verify
199     * the DNS points to this site.
200     */
201    public function reconnect(): bool {
202        return $this->connect( '/sites/v2/reconnect', 'Bearer ' . self::get_auth_token() );
203    }
204
205    /**
206     * Set the connection throttle
207     *
208     * @return void
209     */
210    public function throttle() {
211        $interval = $this->get_throttle_interval();
212
213        $this->throttle = Transient::set( 'nfd_data_connection_throttle', true, $interval );
214    }
215
216    /**
217     * Determine the throttle interval based off number of connection attempts
218     *
219     * @return integer Time to wait until next connection attempt
220     */
221    public function get_throttle_interval() {
222
223        $attempts = intval( \get_option( 'nfd_data_connection_attempts', 0 ) );
224
225        // Throttle intervals step-up:
226        // Hourly for 4 hours
227        // Twice a day for 3 days
228        // Once a day for 3 days
229        // Every 3 days for 3 times
230        // Once a week
231        if ( $attempts <= 4 ) {
232            return HOUR_IN_SECONDS;
233        } elseif ( $attempts <= 10 ) {
234            return 12 * HOUR_IN_SECONDS;
235        } elseif ( $attempts <= 13 ) {
236            return DAY_IN_SECONDS;
237        } elseif ( $attempts <= 16 ) {
238            return 3 * DAY_IN_SECONDS;
239        } else {
240            return WEEK_IN_SECONDS;
241        }
242    }
243
244    /**
245     * Check whether connection is throttled
246     *
247     * @return boolean
248     */
249    public function is_throttled() {
250        $this->throttled = Transient::get( 'nfd_data_connection_throttle' );
251
252        return $this->throttled;
253    }
254
255    /**
256     * Synchronously send a single event and return the notifications.
257     *
258     * @used-by Events::create_item()
259     *
260     * @param Event $event the event
261     *
262     * @phpstan-type Notification_Array array{id:string,locations:array,query:string|null,expiration:int,content:string}
263     * @return array<Notification_Array>|WP_Error
264     */
265    public function send_event( Event $event ) {
266
267        $payload = array(
268            'environment' => $this->get_core_data(),
269            'events'      => array( $event ),
270        );
271
272        $hiive_response = $this->hiive_request( 'sites/v1/events', $payload );
273
274        if ( is_wp_error( $hiive_response ) ) {
275            return $hiive_response;
276        }
277
278        $status_code = \wp_remote_retrieve_response_code( $hiive_response );
279
280        if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
281            return new \WP_Error( $status_code, \wp_remote_retrieve_response_message( $hiive_response ) );
282        }
283
284        /**
285         * Sample shape.
286         *
287         * @var array{data:array{id:string,locations:array,query:string|null,expiration:int,content:string}} $response_payload
288         * */
289        $response_payload = json_decode( \wp_remote_retrieve_body( $hiive_response ), true );
290
291        return $response_payload['data'] ?? array();
292    }
293
294    /**
295     * Send events to the v2 events endpoint and return the list of successes and list of failures.
296     *
297     * @see SubscriberInterface::notify()
298     * @used-by EventManager::send()
299     *
300     * @param Event[] $events Array of Event objects representing the actions that occurred.
301     *
302     * @return array{succeededEvents:array,failedEvents:array}|WP_Error
303     */
304    public function notify( $events ) {
305
306        $payload = array(
307            'environment' => $this->get_core_data(),
308            'events'      => $events,
309        );
310
311        $hiive_response = $this->hiive_request( 'sites/v2/events', $payload );
312
313        if ( \is_wp_error( ( $hiive_response ) ) ) {
314            return $hiive_response;
315        }
316
317        if ( ! in_array( \wp_remote_retrieve_response_code( $hiive_response ), array( 200, 201, 500 ), true ) ) {
318            return new WP_Error( \wp_remote_retrieve_response_code( $hiive_response ), \wp_remote_retrieve_response_message( $hiive_response ) );
319        }
320
321        $response_body = json_decode( wp_remote_retrieve_body( $hiive_response ), true );
322
323        // If the response from Hiive is not shaped as expected, e.g. a more serious 500 error, return as an error, not as the expected array.
324        if ( ! is_array( $response_body ) || ! array_key_exists( 'succeededEvents', $response_body ) || ! array_key_exists( 'failedEvents', $response_body ) ) {
325            return new WP_Error( 'hiive_response', 'Response body does not contain succeededEvents and failedEvents keys.' );
326        }
327
328        return $response_body;
329    }
330
331    /**
332     * Send an HTTP request to Hiive and return the body of the request.
333     *
334     * Handles throttling and reconnection, clients should handle queueing if necessary.
335     *
336     * Defaults to POST. Override with `$args = array('method' => 'GET')`.
337     *
338     * @param string     $path The Hiive api path (after /api/).
339     * @param array|null $payload the payload
340     * @param array|null $args and args for the request
341     *
342     * @return array|WP_Error The response array or a WP_Error when no Hiive connection, no network connection, network requests disabled.
343     */
344    public function hiive_request( string $path, ?array $payload = array(), ?array $args = array() ) {
345
346        /**
347         * Add plugin name/version to user agent
348         *
349         * @see \WP_Http::request()
350         * @see https://developer.wordpress.org/reference/hooks/http_headers_useragent/
351         */
352        add_filter( 'http_headers_useragent', array( $this, 'add_plugin_name_version_to_user_agent' ), 10, 2 );
353
354        // If for some reason we are not connected, bail out now.
355        // If we are not connected, the throttling logic should eventually reconnect.
356        if ( ! self::is_connected() ) {
357            return new WP_Error( 'hiive_connection', __( 'This site is not connected to the hiive.', 'wp-module-data' ) );
358        }
359
360        $defaults = array(
361            'method'  => 'POST',
362            'headers' => array(
363                'Content-Type'  => 'application/json',
364                'Accept'        => 'application/json',
365                'Authorization' => 'Bearer ' . self::get_auth_token(),
366            ),
367            'timeout' => \wp_is_serving_rest_request() ? 15 : 60, // If we're responding to the frontend, we need to be quick.
368        );
369
370        $parsed_args = \wp_parse_args( $args ?? array(), $defaults );
371        $url         = "{$this->api}/{$path}";
372
373        if ( ! empty( $payload ) ) {
374            $parsed_args['body'] = 'GET' === $parsed_args['method']
375                ? $payload
376                : \wp_json_encode( $payload );
377        }
378
379        $request_response = \wp_remote_request( $url, $parsed_args );
380
381        // E.g. Hiive is down, or the site has disabled HTTP requests.
382        if ( \is_wp_error( $request_response ) ) {
383            return $request_response;
384        }
385
386        // Authentication token is valid for Hiive but not for the resource or Site.
387        if ( 403 === $request_response['response']['code'] ) {
388            $body = json_decode( $request_response['body'], true );
389            if ( 'Invalid token for url' === $body['message'] ) {
390                if ( $this->reconnect() ) {
391                    $this->hiive_request( $path, $payload, $args );
392                } else {
393                    return new WP_Error( 'hiive_connection', __( 'This site is not connected to the hiive.', 'wp-module-data' ) );
394                }
395            }
396        }
397
398        \remove_filter( 'http_headers_useragent', array( $this, 'add_plugin_name_version_to_user_agent' ) );
399
400        return $request_response;
401    }
402
403    /**
404     * Try to return the auth token
405     *
406     * This is cleared whenever Hiive returns 401 unauthenticated {@see Data::delete_token_on_401_response()}.
407     *
408     * @return string|false The decrypted token if it's set
409     */
410    public static function get_auth_token() {
411        // Check if a custom token is defined via constant
412        if ( defined( 'NFD_HIIVE_AUTH_TOKEN' ) ) {
413            return NFD_HIIVE_AUTH_TOKEN;
414        }
415
416        // Fall back to the stored option
417        return \get_option( 'nfd_data_token' );
418    }
419
420    /**
421     * Get core site data for initial connection
422     *
423     * @return array
424     */
425    public function get_core_data() {
426        global $wpdb, $wp_version;
427        $container = container();
428
429        $data = array(
430            'brand'       => \sanitize_title( $container->plugin()->brand ),
431            'cache_level' => intval( \get_option( 'newfold_cache_level', 2 ) ),
432            'cloudflare'  => \get_option( 'newfold_cloudflare_enabled', false ),
433            'data'        => defined( 'NFD_DATA_MODULE_VERSION' ) ? constant( 'NFD_DATA_MODULE_VERSION' ) : '0.0',
434            'email'       => \get_option( 'admin_email' ),
435            'hostname'    => gethostname(),
436            'mysql'       => $wpdb->db_version(),
437            'origin'      => $container->plugin()->get( 'id', 'error' ),
438            'php'         => phpversion(),
439            'plugin'      => $container->plugin()->get( 'version', '0' ),
440            'url'         => \get_site_url(),
441            'username'    => get_current_user(),
442            'wp'          => $wp_version,
443            'server_path' => \untrailingslashit( defined( 'ABSPATH' ) ? constant( 'ABSPATH' ) : '' ),
444        );
445
446        return apply_filters( 'newfold_wp_data_module_core_data_filter', $data );
447    }
448
449    /**
450     * Add the plugin name and version to the user agent string
451     *
452     * @param string $user_agent E.g. "WordPress/6.4.3; https://example.org".
453     * @param string $url   E.g. "https://hiive.cloud/api/sites/v2/events".
454     *
455     * @return string E.g. "WordPress/6.4.3; bluehost/1.2.3; https://example.org".
456     */
457    public function add_plugin_name_version_to_user_agent( string $user_agent, string $url ): string {
458        $container      = container();
459        $plugin_brand   = \sanitize_title( $container->plugin()->brand );
460        $plugin_version = $container->plugin()->get( 'version', '0' );
461
462        $user_agent_parts = array_map( 'trim', explode( ';', $user_agent ) );
463
464        array_splice( $user_agent_parts, 1, 0, "{$plugin_brand}/{$plugin_version}" );
465
466        return implode( '; ', $user_agent_parts );
467    }
468}