test_tracking_status(); } return [ 'status' => $status, 'last_test' => $last_test, ]; } /** * Get tracking status * * @return string */ public function get_tracking_status(): string { $tracking = $this->get_tracking_status_and_time(); return $tracking['status']; } /** * Test tracking status * Only returns 'error', 'rest', 'beacon' * * @return string */ public function test_tracking_status(): string { $endpoint = $this->endpoint_test_request(); // true or false //no tracking is possible on the Blueprint environment. Always return success there. if ( defined('BURST_BLUEPRINT') ) { $status = 'beacon'; } else if ( $endpoint ) { $status = 'beacon'; } else { $rest_api = $this->rest_api_test_request(); // true or false $status = $rest_api ? 'rest' : 'error'; } update_option( 'burst_tracking_status', $status, true ); return $status; } /** * Test endpoint * * @return bool */ public function endpoint_test_request(): bool { $url = burst_get_beacon_url(); $data = [ 'request' => 'test' ]; $response = wp_remote_post( $url, array( 'method' => 'POST', 'headers' => [ 'Content-type' => 'application/x-www-form-urlencoded' ], 'body' => $data, 'sslverify' => false, ) ); $status = false; if ( ! is_wp_error( $response ) && ! empty( $response['response']['code'] ) ) { $status = $response['response']['code']; } if ( $status === 200 ) { return true; } // otherwise try with file_get_contents // use key 'http' even if you send the request to https://... $options = [ 'http' => [ 'header' => "Content-type: application/x-www-form-urlencoded\r\n", 'method' => 'POST', 'content' => http_build_query( $data ), ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false, ], ]; $context = stream_context_create( $options ); @file_get_contents( $url, false, $context ); $status_line = $http_response_header[0] ?? ''; $status = false; if ( preg_match( '{HTTP\/\S*\s(\d{3})}', $status_line, $matches ) ) { $status = $matches[1]; } return $status === 200; } /** * Test REST API * * @return bool */ public function rest_api_test_request(): bool { $url = get_rest_url( null, 'burst/v1/track' ); $data = '{"request":"test"}'; $response = wp_remote_post( $url, array( 'headers' => [ 'Content-Type' => 'application/json; charset=utf-8' ], 'method' => 'POST', 'body' => json_encode( $data ), 'data_format' => 'body', 'timeout' => 5, ) ); if ( is_wp_error( $response ) ) { return false; } if ( $response['response']['code'] === 200 ) { return true; } return false; } } }