60, 'display' => 'Every minute' ); return $schedules; } function hosted_settings(): array { return (array) get_option( 'uruvo_hosted_account', array() ); } function hosted_loopback( string $url ): bool { return defined( 'WCT_TEST_LOCAL_PORTAL' ) && WCT_TEST_LOCAL_PORTAL && in_array( wp_parse_url( $url, PHP_URL_HOST ), array( 'localhost', '127.0.0.1', '[::1]' ), true ); } function hosted_url( string $url ): string { $url = untrailingslashit( esc_url_raw( $url, array( 'https', 'http' ) ) ); $parts = $url ? wp_parse_url( $url ) : array(); if ( ! $parts || empty( $parts['host'] ) || ! empty( $parts['user'] ) || ! empty( $parts['pass'] ) || isset( $parts['query'] ) || isset( $parts['fragment'] ) || ! empty( $parts['path'] ) ) { return ''; } return 'https' === $parts['scheme'] || hosted_loopback( $url ) ? $url : ''; } function hosted_call( array $settings, string $action, array $payload = array() ): array|\WP_Error { $url = $settings['url'] . '/api/bridge/' . $action; $args = array( 'headers' => array( 'Authorization' => 'Bearer ' . $settings['token'], 'Content-Type' => 'application/json', ), 'body' => wp_json_encode( array( 'origin' => home_url() ) + $payload ), 'timeout' => 12, 'redirection' => 0, 'limit_response_size' => 524288, ); $response = hosted_loopback( $url ) ? wp_remote_post( $url, $args ) : wp_safe_remote_post( $url, $args ); if ( is_wp_error( $response ) ) { return new \WP_Error( 'wct_portal_connection', 'Could not reach the hosted account. Check the URL and try again.' ); } $value = json_decode( wp_remote_retrieve_body( $response ), true ); $status = wp_remote_retrieve_response_code( $response ); if ( $status < 200 || $status > 299 || ! is_array( $value ) ) { return new \WP_Error( sanitize_key( $value['code'] ?? 'wct_portal_connection' ), sanitize_text_field( $value['error'] ?? 'The hosted account could not complete this request.' ) ); } return $value; } /** @return true|\WP_Error */ function hosted_connect( string $url, string $token ): bool|\WP_Error { $url = hosted_url( $url ); if ( ! $url || ! preg_match( '/^wct_[a-f0-9]{64}$/D', $token ) ) { return new \WP_Error( 'wct_portal_config', 'Enter the account HTTPS URL and the connection key shown in your dashboard.' ); } $settings = array( 'url' => $url, 'token' => $token ); $result = hosted_call( $settings, 'connect' ); if ( is_wp_error( $result ) ) { return $result; } if ( hosted_settings() !== $settings ) { hosted_disconnect(); } update_option( 'uruvo_hosted_account', $settings, false ); hosted_schedule(); return true; } function hosted_schedule(): void { if ( hosted_settings() && ! wp_next_scheduled( HOSTED_EVENT ) ) { wp_schedule_event( time() + 60, HOSTED_INTERVAL, HOSTED_EVENT ); } } function hosted_disconnect(): void { $settings = hosted_settings(); $inflight = (array) get_option( 'uruvo_hosted_inflight', array() ); if ( $settings && $inflight ) { hosted_call( $settings, 'drop', array( 'key' => $inflight['key'] ) ); set_claim_state( (int) $inflight['id'], $inflight['lease'], 'queued', 'Reconnect to resume translation.' ); } delete_option( 'uruvo_hosted_inflight' ); delete_option( 'uruvo_hosted_account' ); delete_option( 'uruvo_allowance' ); wp_clear_scheduled_hook( HOSTED_EVENT ); } function hosted_dispatch(): void { $result = hosted_sync(); if ( is_wp_error( $result ) ) { update_option( 'uruvo_hosted_notice', $result->get_error_message(), false ); } else { delete_option( 'uruvo_hosted_notice' ); } } /** @return true|\WP_Error */ function hosted_sync( bool $force_balance = false ): bool|\WP_Error { $settings = hosted_settings(); if ( ! $settings ) { return true; } // Give a request started before the storage upgrade time to release its lock. if ( (int) get_option( 'wct_hosted_sync_lock', 0 ) >= time() - 150 ) { return true; } $lock = (int) get_option( 'uruvo_hosted_sync_lock', 0 ); if ( $lock && $lock < time() - 150 ) { delete_option( 'uruvo_hosted_sync_lock' ); } if ( ! add_option( 'uruvo_hosted_sync_lock', time(), '', false ) ) { return true; } try { return hosted_sync_locked( $settings, $force_balance ); } finally { delete_option( 'uruvo_hosted_sync_lock' ); } } /** @return true|\WP_Error */ function hosted_sync_locked( array $settings, bool $force_balance = false ): bool|\WP_Error { if ( ! polylang_ready() ) { return new \WP_Error( 'wct_dependency', 'Configure your site languages before syncing translations.' ); } $inflight = (array) get_option( 'uruvo_hosted_inflight', array() ); if ( $inflight ) { $finished = hosted_poll( $settings, $inflight ); if ( is_wp_error( $finished ) ) { return $finished; } if ( ! $finished ) { return true; } delete_option( 'uruvo_hosted_inflight' ); } $claim = claim_job( new \WP_REST_Request( 'POST' ) ); if ( is_wp_error( $claim ) ) { return $claim; } $job = $claim->get_data()['job']; if ( $job ) { update_option( 'uruvo_hosted_inflight', $job, false ); $result = hosted_submit( $settings, $job ); if ( is_wp_error( $result ) ) { return $result; } } $previous = (array) get_option( 'uruvo_allowance', array() ); if ( ! $force_balance && ! $job && ! $inflight && (int) ( $previous['checked_at'] ?? 0 ) > time() - 300 ) { return true; } $usage = hosted_call( $settings, 'usage' ); if ( ! is_wp_error( $usage ) && array_key_exists( 'limit', $usage ) && isset( $usage['used'], $usage['reserved'] ) ) { update_option( 'uruvo_allowance', array( 'period' => ! empty( $usage['periodEnd'] ) ? 'Annual plan' : 'Account', 'period_end' => (int) ( $usage['periodEnd'] ?? 0 ), 'limit' => (int) $usage['limit'], 'unlimited' => null === $usage['limit'], 'delivered' => (int) $usage['used'], 'reserved' => (int) $usage['reserved'], 'checked_at' => time(), ), false ); } return is_wp_error( $usage ) ? $usage : true; } /** @return true|\WP_Error */ function hosted_submit( array $settings, array $job ): bool|\WP_Error { $result = hosted_call( $settings, 'submit', array( 'key' => $job['key'], 'source_locale' => $job['source_locale'], 'target_locale' => $job['target_locale'], 'segments' => (object) $job['segments'], ) ); if ( is_wp_error( $result ) && in_array( $result->get_error_code(), array( 'allowance', 'plan_required' ), true ) ) { set_claim_state( (int) $job['id'], $job['lease'], 'queued', $result->get_error_message(), 300 ); delete_option( 'uruvo_hosted_inflight' ); } return is_wp_error( $result ) ? $result : true; } function hosted_poll( array $settings, array $job ): bool|\WP_Error { $row = job_row( (int) $job['id'] ); $key = array( 'key' => $job['key'] ); if ( $row && $row->results ) { maybe_auto_publish( (int) $row->id ); $ack = hosted_call( $settings, 'ack', $key ); return is_wp_error( $ack ) ? $ack : true; } if ( ! $row || 'leased' !== $row->status || ! hash_equals( $row->lease_token, $job['lease'] ) || ! current_source( $row ) ) { if ( $row && 'leased' === $row->status ) { set_claim_state( (int) $row->id, $job['lease'], 'stale', 'The source changed. Queue the latest page.' ); } $drop = hosted_call( $settings, 'drop', $key ); return is_wp_error( $drop ) ? $drop : true; } if ( unexpected_translation( $row ) ) { set_claim_state( (int) $row->id, $job['lease'], 'discarded', 'A linked translation already exists. This job was skipped.' ); $drop = hosted_call( $settings, 'drop', $key ); return is_wp_error( $drop ) ? $drop : true; } global $wpdb; $wpdb->update( jobs_table(), array( 'lease_until' => time() + LEASE_SECONDS ), array( 'id' => $row->id, 'status' => 'leased', 'lease_token' => $job['lease'] ) ); $result = hosted_call( $settings, 'result', $key ); if ( is_wp_error( $result ) ) { if ( 'missing_job' !== $result->get_error_code() ) { return $result; } $submitted = hosted_submit( $settings, $job ); return is_wp_error( $submitted ) ? $submitted : false; } if ( in_array( $result['status'] ?? '', array( 'failed', 'dropped', 'acknowledged' ), true ) ) { set_claim_state( (int) $row->id, $job['lease'], 'failed', 'The hosted job ended before WordPress saved its result. Retry the translation to continue.' ); return true; } if ( 'delivered' !== ( $result['status'] ?? '' ) ) { $capacity_until = absint( $result['capacityUntil'] ?? 0 ); if ( $capacity_until !== (int) ( $job['capacity_until'] ?? 0 ) ) { $job['capacity_until'] = $capacity_until; update_option( 'uruvo_hosted_inflight', $job, false ); } return false; } $local = new \WP_REST_Request( 'POST' ); $local->set_param( 'id', (int) $row->id ); $local->set_param( 'lease', $job['lease'] ); $local->set_param( 'translations', $result['translations'] ?? array() ); $saved = complete_job( $local ); if ( is_wp_error( $saved ) ) { return $saved; } $ack = hosted_call( $settings, 'ack', $key ); return is_wp_error( $ack ) ? $ack : true; } function hosted_capacity_until( int $job_id ): int { $inflight = (array) get_option( 'uruvo_hosted_inflight', array() ); $until = (int) ( $inflight['capacity_until'] ?? 0 ); return (int) ( $inflight['id'] ?? 0 ) === $job_id && $until > time() ? $until : 0; } function translation_connection_ready(): bool { return (bool) ( hosted_settings() || get_option( 'uruvo_worker_token_hash' ) ); } function render_hosted_connection(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $active = hosted_settings(); echo '
' . esc_html__( 'Connected', 'uruvo' ) . ' ยท ' . esc_html( $active['url'] ) . '
'; $checked = (int) ( get_option( 'uruvo_allowance', array() )['checked_at'] ?? 0 ); if ( $checked ) { /* translators: %s: time elapsed since the last usage check. */ echo '' . esc_html( sprintf( __( 'Usage checked %s ago.', 'uruvo' ), human_time_diff( $checked ) ) ) . '
'; } $notice = get_option( 'uruvo_hosted_notice' ); if ( $notice ) { echo 'Connection needs attention. ' . esc_html( $notice ) . '
'; } start_form( 'hosted_sync' ); echo ''; echo 'New hosted translations pause until you reconnect.
'; start_form( 'hosted_disconnect' ); submit_button( 'Disconnect site', 'secondary', 'submit', false ); echo 'Get a connection key in your Uruvo dashboard (opens in a new tab). Add this site, then paste its key below.
'; start_form( 'hosted_connect' ); echo ''; submit_button( __( 'Connect account', 'uruvo' ), 'primary', 'submit', false ); echo '