cap->edit_posts ) ) { return $type->cap->edit_posts; } } return ''; } function register_translation_bulk_actions(): void { foreach ( array_keys( translation_post_types() ) as $type ) { add_filter( 'bulk_actions-edit-' . $type, __NAMESPACE__ . '\\register_translation_bulk' ); add_filter( 'handle_bulk_actions-edit-' . $type, __NAMESPACE__ . '\\handle_translation_bulk', 10, 3 ); } } function admin_url_for( array $args = array() ): string { return add_query_arg( $args + array( 'page' => 'word-count-translator' ), admin_url( 'admin.php' ) ); } function admin_message( string $message, array $args = array(), bool $error = false, array $queued_sources = array() ): never { set_transient( 'uruvo_notice_' . get_current_user_id(), array( $message, $error, $queued_sources ), 90 ); wp_safe_redirect( admin_url_for( $args ) ); exit; } function start_form( string $task ): void { echo '
'; echo ''; wp_nonce_field( 'uruvo_' . $task ); } function admin_action(): void { $task = sanitize_key( wp_unslash( $_POST['task'] ?? '' ) ); check_admin_referer( 'uruvo_' . $task ); if ( ! admin_capability() ) { wp_die( 'You cannot manage translations.', 403 ); } switch ( $task ) { case 'add_language': case 'label_existing_content': case 'enable_content_types': $language = sanitize_text_field( wp_unslash( $_POST['language'] ?? '' ) ); $types = array_map( 'sanitize_key', (array) wp_unslash( $_POST['content_types'] ?? array() ) ); manage_site_languages( $task, $language, $types ); case 'assign_source_language': assign_source_language( absint( $_POST['source_id'] ?? 0 ), sanitize_key( wp_unslash( $_POST['language'] ?? '' ) ), source_filters( $_POST ) ); case 'store_translation': if ( ! current_user_can( 'manage_options' ) || ! class_exists( '\WooCommerce' ) || store_language_conflict() ) { wp_die( 'You cannot change WooCommerce translation settings.', '', array( 'response' => 403 ) ); } update_option( 'uruvo_store_translation', ! empty( $_POST['store_translation'] ), false ); admin_message( __( 'WooCommerce settings saved.', 'uruvo' ), array( 'view' => 'settings' ) ); case 'hosted_connect': case 'hosted_disconnect': case 'hosted_sync': if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'Only administrators can manage the hosted account.', 403 ); } if ( 'hosted_disconnect' === $task ) { hosted_disconnect(); admin_message( 'Uruvo account disconnected.', array( 'view' => 'settings' ) ); } if ( 'hosted_connect' === $task ) { $result = hosted_connect( sanitize_text_field( wp_unslash( $_POST['portal_url'] ?? '' ) ) ?: 'https://uruvo.app', sanitize_text_field( wp_unslash( $_POST['portal_token'] ?? '' ) ) ); if ( is_wp_error( $result ) ) { admin_message( $result->get_error_message(), array( 'view' => 'settings' ), true ); } } $result = hosted_sync( true ); $view = 'jobs' === sanitize_key( wp_unslash( $_POST['return_view'] ?? '' ) ) ? 'jobs' : 'settings'; $return = array( 'view' => $view ); $job = absint( $_POST['return_job'] ?? 0 ); if ( $job ) { $return['job'] = $job; } elseif ( 'jobs' === $view ) { $return['status'] = job_filter( sanitize_key( wp_unslash( $_POST['return_status'] ?? '' ) ) ); $before = absint( $_POST['return_before'] ?? 0 ); if ( $before ) { $return['before'] = $before; } } $message = 'hosted_connect' === $task ? 'Site connected to Uruvo.' : 'Translation status refreshed.'; admin_message( is_wp_error( $result ) ? $result->get_error_message() : $message, $return, is_wp_error( $result ) ); case 'rotate': case 'disconnect': case 'preferences': if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'Only administrators can manage the worker connection.', 403 ); } if ( 'preferences' === $task ) { update_option( 'uruvo_auto_queue', ! empty( $_POST['auto_queue'] ), false ); admin_message( 'Translation preferences saved.', array( 'view' => 'settings' ) ); } delete_option( 'uruvo_allowance' ); if ( 'disconnect' === $task ) { delete_option( 'uruvo_worker_token_hash' ); delete_transient( 'uruvo_pair_' . get_current_user_id() ); admin_message( 'Worker access revoked.', array( 'view' => 'settings' ) ); } $token = bin2hex( random_bytes( 32 ) ); update_option( 'uruvo_worker_token_hash', hash( 'sha256', $token ), false ); set_transient( 'uruvo_pair_' . get_current_user_id(), $token, 300 ); admin_message( 'Copy the new token into your worker configuration. This is its only display.', array( 'view' => 'settings' ) ); case 'queue': $versions = json_decode( sanitize_text_field( wp_unslash( $_POST['source_versions'] ?? '{}' ) ), true ); $targets = array_unique( array_map( 'sanitize_key', (array) wp_unslash( $_POST['targets'] ?? array() ) ) ); if ( ! $versions || ! is_array( $versions ) ) { admin_message( 'Choose content and at least one target language.', array(), true ); } $source_ids = array_map( 'absint', array_keys( $versions ) ); $mode = 'update' === sanitize_key( wp_unslash( $_POST['wct_mode'] ?? '' ) ) ? 'update' : 'new'; $publish_user = 'new' === $mode && ! empty( $_POST['auto_publish'] ) ? get_current_user_id() : 0; $eligible = json_decode( sanitize_text_field( wp_unslash( $_POST['eligible_targets'] ?? '{}' ) ), true ); $return = array( 'sources' => implode( ',', $source_ids ), 'wct_targets' => implode( ',', $targets ), 'mode' => $mode, 'auto_publish' => $publish_user ? 1 : 0 ) + source_filters( $_POST ); if ( ! translation_connection_ready() ) { admin_message( __( 'Connect this site to Uruvo before requesting translations.', 'uruvo' ), $return, true ); } if ( ! $targets ) { admin_message( 'Choose at least one target language.', $return, true ); } if ( ! polylang_ready() || ! is_array( $eligible ) ) { admin_message( __( 'Open a fresh estimate before queueing.', 'uruvo' ), $return, true ); } $plans = array(); $skipped = 0; foreach ( $versions as $source_id => $version ) { $source_id = absint( $source_id ); $source = get_post( $source_id ); if ( ! is_string( $version ) || ! is_supported_source( $source ) || ! current_user_can( 'edit_post', $source_id ) ) { admin_message( 'Choose content you can edit.', array(), true ); } if ( $publish_user && ! current_user_can( get_post_type_object( $source->post_type )->cap->publish_posts ) ) { admin_message( __( 'You need publishing permission for every selected item to auto-publish.', 'uruvo' ), $return, true ); } foreach ( array_intersect( $targets, (array) ( $eligible[ $source_id ] ?? array() ) ) as $target ) { $plan = create_plan( $source_id, $target, $mode, $publish_user ); if ( is_wp_error( $plan ) && 'coverage' === $plan->get_error_code() ) { ++$skipped; continue; } if ( is_wp_error( $plan ) || ! hash_equals( $plan['source_version'], $version ) ) { admin_message( 'The estimate changed. Review the updated totals before queueing.', $return, true ); } $plans[] = $plan; } } if ( ! $plans ) { admin_message( __( 'There are no eligible translations to queue. Check the estimate for changes.', 'uruvo' ), $return, true ); } $job_ids = array(); foreach ( $plans as $plan ) { $id = enqueue_plan( $plan ); if ( is_wp_error( $id ) ) { if ( 'coverage' === $id->get_error_code() ) { ++$skipped; continue; } admin_message( $id->get_error_message(), $return, true ); } $job_ids[] = $id; } $job_ids = array_values( array_unique( $job_ids ) ); if ( ! $job_ids ) { admin_message( __( 'There are no eligible translations to queue. Check the estimate for changes.', 'uruvo' ), $return, true ); } $destination = 1 === count( $job_ids ) ? array( 'job' => $job_ids[0] ) : array( 'view' => 'jobs' ); $message = $skipped ? __( 'Request saved. Some translations were already linked and were skipped.', 'uruvo' ) : __( 'Translation request saved.', 'uruvo' ); admin_message( $message, $destination, false, $source_ids ); case 'approve_bulk': $destination = sanitize_key( wp_unslash( $_POST['destination'] ?? '' ) ); $publish = 'publish' === $destination; $ids = bulk_job_ids( array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['wct_jobs'] ?? array() ) ) ); if ( ! in_array( $destination, array( 'publish', 'draft' ), true ) || ! $ids ) { admin_message( __( 'Choose the jobs and how you want to save them.', 'uruvo' ), array( 'view' => 'jobs', 'status' => 'review' ), true ); } $done = 0; foreach ( $ids as $id ) { $job = job_row( $id ); if ( $job && ! is_wp_error( approve_new_job( $job, $publish ) ) ) { ++$done; } } $failed = count( $ids ) - $done; $message = $publish /* translators: %d: number of translations published. */ ? sprintf( _n( '%d translation published.', '%d translations published.', $done, 'uruvo' ), $done ) /* translators: %d: number of drafts saved. */ : sprintf( _n( '%d draft saved.', '%d drafts saved.', $done, 'uruvo' ), $done ); if ( $failed ) { /* translators: %d: number of jobs that need individual attention. */ $message .= ' ' . sprintf( _n( '%d job needs individual attention. Open it from Jobs.', '%d jobs need individual attention. Open them from Jobs.', $failed, 'uruvo' ), $failed ); } admin_message( $message, array( 'view' => 'jobs', 'status' => $failed ? 'all' : 'finished' ), (bool) $failed ); case 'apply_updates_bulk': $reviewed = array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['reviewed'] ?? array() ) ); $ids = bulk_job_ids( array_map( 'sanitize_text_field', (array) wp_unslash( $_POST['wct_jobs'] ?? array() ) ) ); $done = 0; $remaining = array(); foreach ( $ids as $id ) { $job = job_row( $id ); $update = $job ? safe_batch_update( $job ) : array(); $hash = $reviewed[ $id ] ?? ''; if ( $update && is_string( $hash ) && hash_equals( $update['hash'], $hash ) && ! is_wp_error( apply_proposal( $job, $update['fields'], $hash ) ) ) { ++$done; } else { $remaining[] = $id; } } /* translators: %d: number of translation updates published. */ $message = sprintf( _n( '%d update published.', '%d updates published.', $done, 'uruvo' ), $done ); if ( $remaining ) { /* translators: %d: number of translation updates that still need review. */ $message .= ' ' . sprintf( _n( '%d update needs a fresh review.', '%d updates need a fresh review.', count( $remaining ), 'uruvo' ), count( $remaining ) ); } admin_message( $message, $remaining ? array( 'view' => 'jobs', 'wct_jobs' => implode( ',', $remaining ) ) : array( 'view' => 'jobs', 'status' => 'finished' ), (bool) $remaining ); case 'keep_current_translation': case 'apply': case 'retry': case 'cancel': $job_id = absint( $_POST['job'] ?? 0 ); $row = job_row( $job_id ); if ( ! $row || ! current_user_can( 'edit_post', $row->source_id ) ) { admin_message( 'You cannot manage this translation.', array(), true ); } if ( 'cancel' === $task ) { global $wpdb; $table = jobs_table(); $changed = $wpdb->query( $wpdb->prepare( "UPDATE %i SET status = 'discarded', notice = %s, updated_at = %s WHERE id = %d AND status IN ('queued', 'leased')", $table, 'Canceled before delivery.', current_time( 'mysql', true ), $job_id ) ); admin_message( $changed ? 'Translation canceled.' : 'The job already finished.', array( 'job' => $job_id ) ); } if ( 'retry' === $task ) { if ( ! current_source( $row ) || unexpected_translation( $row ) ) { admin_message( __( 'This job is out of date. Get a fresh estimate.', 'uruvo' ), array( 'job' => $job_id ), true ); } if ( ! translation_connection_ready() ) { admin_message( __( 'Connect this site to Uruvo before requesting translations.', 'uruvo' ), array( 'job' => $job_id ), true ); } global $wpdb; $changed = $wpdb->update( jobs_table(), array( 'status' => 'queued', 'lease_until' => 0, 'notice' => '', 'updated_at' => current_time( 'mysql', true ) ), array( 'id' => $job_id, 'status' => 'failed' ) ); $message = 1 === $changed ? __( 'Translation queued again.', 'uruvo' ) : __( 'Could not retry this job. Check its current status.', 'uruvo' ); admin_message( $message, array( 'job' => $job_id ), 1 !== $changed ); } $fields = array_map( 'sanitize_key', (array) wp_unslash( $_POST['fields'] ?? array() ) ); $hash = sanitize_text_field( wp_unslash( $_POST['target_hash'] ?? '' ) ); if ( 'keep_current_translation' === $task ) { $result = keep_current_translation( $row, $hash ); admin_message( is_wp_error( $result ) ? $result->get_error_message() : __( 'Current translation kept.', 'uruvo' ), array( 'job' => $job_id ), is_wp_error( $result ) ); } $result = apply_proposal( $row, $fields, $hash ); if ( is_wp_error( $result ) ) { admin_message( $result->get_error_message(), array( 'job' => $job_id ), true ); } $url = get_edit_post_link( $result, 'raw' ); wp_safe_redirect( $url ?: admin_url_for( array( 'job' => $job_id ) ) ); exit; } admin_message( 'Unknown translation action.', array(), true ); } function linked_target( object $job ): ?\WP_Post { $group = pll_get_post_translations( (int) $job->source_id ); $id = (int) ( $group[ $job->target_slug ] ?? 0 ); return $id && $id !== (int) $job->source_id ? get_post( $id ) : null; } function proposal_marker( object $job ): string { // Existing drafts store this value. It identifies a job across plugin versions. return 'wct_job_' . $job->job_key; } function translation_meta( int $post_id, string $name ): mixed { $key = '_uruvo_' . $name; if ( metadata_exists( 'post', $post_id, $key ) ) { return get_post_meta( $post_id, $key, true ); } return metadata_exists( 'post', $post_id, '_wct_' . $name ) ? get_post_meta( $post_id, '_wct_' . $name, true ) : null; } function draft_created_for( object $job, string $post_type ): ?\WP_Post { $marker = proposal_marker( $job ); $posts = get_posts( array( 'post_type' => $post_type, 'post_status' => array( 'draft', 'publish', 'pending', 'future', 'private' ), 'meta_query' => array( 'relation' => 'OR', array( 'key' => '_uruvo_created_from', 'value' => $marker ), array( 'key' => '_wct_created_from', 'value' => $marker ), ), 'numberposts' => 1, 'lang' => '', 'suppress_filters' => true, ) ); return $posts[0] ?? null; } function review_fields( object $job ): array { $fields = (array) json_decode( $job->proposal, true ); if ( isset( $fields['content'] ) ) { $fields['content'] = localize_internal_links( $fields['content'], $job->target_slug ); } if ( isset( $fields['builder'] ) ) { $unsupported = array(); $fields['builder'] = transform_builder( $fields['builder'], static fn( $id, $text ) => $text, $unsupported, $job->target_slug ); } return $fields; } function effective_content( ?\WP_Post $target, array $proposal ): ?array { if ( ! $target || ! isset( $proposal['content'] ) ) { return null; } $baseline = translation_meta( $target->ID, 'content_baseline' ); return is_string( $baseline ) ? merge_edited_content( $target->post_content, $baseline, $proposal['content'] ) : null; } function apply_proposal( object $job, array $selected, string $reviewed_hash ): int|\WP_Error { return with_translation_lock( (int) $job->source_id, $job->target_slug, static fn (): int|\WP_Error => apply_proposal_locked( $job, $selected, $reviewed_hash ) ); } function apply_proposal_locked( object $job, array $selected, string $reviewed_hash ): int|\WP_Error { $source = current_source( $job ); if ( ! $source || ! $job->proposal ) { return new \WP_Error( 'stale', 'The source changed. Queue the current version before applying a translation.' ); } if ( ! in_array( $job->status, array( 'ready', 'applying' ), true ) ) { return new \WP_Error( 'state', 'This result has already been applied or is still being translated.' ); } $target = linked_target( $job ); $policy = job_policy( $job ); if ( unexpected_translation( $job ) || ( 'update' === ( $policy['mode'] ?? '' ) && (int) ( $target->ID ?? 0 ) !== (int) $policy['linked'] ) ) { return new \WP_Error( 'review_changed', __( 'The linked translation changed. Open the latest translation before making updates.', 'uruvo' ) ); } $expected = $target ? target_review_hash( $target ) : 'new'; $proposal = review_fields( $job ); if ( ! hash_equals( $expected, $reviewed_hash ) ) { return new \WP_Error( 'review_changed', 'The linked translation changed since you opened the review. Review the current version before applying.' ); } if ( $target && ( 'trash' === $target->post_status || ! current_user_can( 'edit_post', $target->ID ) || wp_check_post_lock( $target->ID ) ) ) { return new \WP_Error( 'target', 'Close any other editor session and check that you can edit the linked translation.' ); } if ( $target && 'auto-draft' === $target->post_status ) { return new \WP_Error( 'target', 'Save the linked auto-draft before applying the translation.' ); } if ( ! $target && ! current_user_can( get_post_type_object( $source->post_type )->cap->create_posts ) ) { return new \WP_Error( 'target', 'You cannot create another page or post.' ); } $merge = effective_content( $target, $proposal ); $proposed = $target ? array_intersect_key( $proposal, array_fill_keys( $selected, true ) ) : $proposal; if ( $target && isset( $proposed['builder'] ) && builder_has_pending_edits( $target ) ) { return new \WP_Error( 'builder_draft', __( 'The linked Beaver Builder layout has unpublished changes. Publish or discard those changes before applying a translation update.', 'uruvo' ) ); } if ( $target && page_builder( $source ) !== page_builder( $target ) ) { return new \WP_Error( 'builder_changed', __( 'The linked translation uses a different editor. Keep it in its current editor and update it manually.', 'uruvo' ) ); } if ( isset( $proposed['content'] ) && $merge ) { $proposed['content'] = $merge['html']; } if ( ! $proposed ) { return new \WP_Error( 'fields', 'Select at least one field to update.' ); } global $wpdb; $table = jobs_table(); $now = time(); $lock = $wpdb->query( $wpdb->prepare( "UPDATE %i SET status = 'applying', lease_until = %d WHERE id = %d AND (status = 'ready' OR (status = 'applying' AND lease_until <= %d))", $table, $now + 120, $job->id, $now ) ); if ( 1 !== $lock ) { return new \WP_Error( 'busy', 'This result is being applied. Refresh the review in a moment.' ); } $post_values = array(); $mapping = array( 'title' => 'post_title', 'excerpt' => 'post_excerpt', 'content' => 'post_content' ); foreach ( $mapping as $field => $post_key ) { if ( array_key_exists( $field, $proposed ) ) { $post_values[ $post_key ] = 'title' === $field ? wp_strip_all_tags( $proposed[ $field ] ) : $proposed[ $field ]; } } foreach ( SEO_FIELDS as $field ) { if ( array_key_exists( $field, $proposed ) ) { $post_values['meta_input'][ $field ] = sanitize_text_field( $proposed[ $field ] ); } } if ( isset( $proposed['builder'] ) ) { $post_values['meta_input'] = ( $post_values['meta_input'] ?? array() ) + slash_builder_objects( builder_meta( $proposed['builder'] ) ); $post_values['post_content'] = implode( "\n\n", builder_copy( $proposed['builder'] ) ); } do_action( 'uruvo_applying_proposal', $job->id ); $recover = $target ? null : draft_created_for( $job, $source->post_type ); if ( $target ) { $id = wp_update_post( wp_slash( array( 'ID' => $target->ID ) + $post_values ), true ); } elseif ( $recover ) { $group = pll_get_post_translations( $source->ID ); $group[ $job->target_slug ] = $recover->ID; pll_set_post_language( $recover->ID, $job->target_slug ); pll_save_post_translations( $group ); $id = $recover->ID; } else { $group = pll_get_post_translations( $source->ID ); $parent = $source->post_parent ? pll_get_post( $source->post_parent, $job->target_slug ) : 0; $meta = $post_values['meta_input'] ?? array(); $meta['_uruvo_created_from'] = proposal_marker( $job ); $template = get_page_template_slug( $source->ID ); if ( $template ) { $meta['_wp_page_template'] = $template; } $thumbnail = get_post_thumbnail_id( $source->ID ); if ( $thumbnail ) { $meta['_thumbnail_id'] = pll_get_post( $thumbnail, $job->target_slug ) ?: $thumbnail; } $post_values['meta_input'] = $meta; $id = pll_insert_post( wp_slash( $post_values + array( 'post_type' => $source->post_type, 'post_status' => 'draft', 'post_author' => get_current_user_id(), 'post_parent' => $parent ?: 0, 'post_name' => sanitize_title( $post_values['post_title'] ?? '' ), 'translations' => $group, ) ), $job->target_slug ); } if ( is_wp_error( $id ) || ! $id ) { $wpdb->update( $table, array( 'status' => 'ready', 'lease_until' => 0 ), array( 'id' => $job->id, 'status' => 'applying' ) ); return is_wp_error( $id ) ? $id : new \WP_Error( 'insert', 'The translated page could not be saved.' ); } if ( isset( $proposed['builder'] ) ) { clear_builder_cache( $id, page_builder( $source ) ); } $actual = source_fields( get_post( $id ) ); $hashes = (array) translation_meta( $id, 'delivered_hashes' ); foreach ( array_keys( $proposed ) as $key ) { if ( isset( $actual[ $key ] ) ) { $hashes[ $key ] = fingerprint( $actual[ $key ] ); } } if ( isset( $proposed['content'] ) ) { $baseline = $merge && $merge['kept'] ? $proposal['content'] : $actual['content']; update_post_meta( $id, '_uruvo_content_baseline', wp_slash( $baseline ) ); $hashes['content'] = fingerprint( $baseline ); } update_post_meta( $id, '_uruvo_delivered_hashes', $hashes ); $wpdb->update( $table, array( 'status' => $target ? 'applied' : 'draft', 'target_id' => $id, 'updated_at' => current_time( 'mysql', true ), 'lease_until' => 0, ), array( 'id' => $job->id, 'status' => 'applying' ) ); return (int) $id; } function can_approve_new_job( object $job, bool $publish = false ): bool { if ( ! in_array( $job->status, array( 'ready', 'applying' ), true ) || ! $job->proposal || ! current_user_can( 'edit_post', $job->source_id ) ) { return false; } $source = current_source( $job ); if ( ! $source || linked_target( $job ) || 'update' === ( job_policy( $job )['mode'] ?? '' ) ) { return false; } $caps = get_post_type_object( $source->post_type )->cap; $plan = json_decode( $job->plan, true ); return current_user_can( $caps->create_posts ) && ( ! $publish || ( empty( $plan['unsupported'] ) && current_user_can( $caps->publish_posts ) ) ); } function approve_new_job( object $job, bool $publish ): int|\WP_Error { if ( ! can_approve_new_job( $job, $publish ) ) { return new \WP_Error( 'review', __( 'Open this job to review the latest content and permissions.', 'uruvo' ) ); } $id = apply_proposal( $job, array(), 'new' ); if ( is_wp_error( $id ) || ! $publish ) { return $id; } $can_publish = current_user_can( 'edit_post', $id ) && current_source( $job ) && $id === translation_target_id( (int) $job->source_id, $job->target_slug ); $updated = $can_publish ? wp_update_post( array( 'ID' => $id, 'post_status' => 'publish' ), true ) : 0; if ( is_wp_error( $updated ) || ! $updated || 'publish' !== get_post_status( $id ) ) { return new \WP_Error( 'publish', __( 'The draft was saved. Open it in WordPress to finish publishing.', 'uruvo' ) ); } return $id; } function maybe_auto_publish( int $job_id ): void { $job = job_row( $job_id ); if ( ! $job || ! in_array( $job->status, array( 'ready', 'applying' ), true ) || ! $job->proposal ) { return; } $plan = json_decode( $job->plan, true ); $user = absint( $plan['policy']['publish_user'] ?? 0 ); if ( ! $user || 'new' !== ( $plan['policy']['mode'] ?? '' ) ) { return; } if ( ! wp_next_scheduled( 'uruvo_auto_publish_job', array( $job_id ) ) ) { wp_schedule_single_event( time() + 150, 'uruvo_auto_publish_job', array( $job_id ) ); } $previous = get_current_user_id(); wp_set_current_user( $user ); try { $result = ! empty( $plan['unsupported'] ) ? new \WP_Error( 'blocks', __( 'Some content needs a manual review before publication.', 'uruvo' ) ) : approve_new_job( $job, true ); } finally { wp_set_current_user( $previous ); } if ( is_wp_error( $result ) && 'busy' === $result->get_error_code() ) { return; } wp_clear_scheduled_hook( 'uruvo_auto_publish_job', array( $job_id ) ); if ( is_wp_error( $result ) ) { global $wpdb; $current = job_row( $job_id ); if ( $current && in_array( $current->status, array( 'ready', 'draft' ), true ) ) { $wpdb->update( jobs_table(), array( /* translators: %s: error that stopped automatic publishing. */ 'notice' => sprintf( __( 'Automatic publishing needs attention. %s', 'uruvo' ), $result->get_error_message() ), ), array( 'id' => $job_id, 'status' => $current->status ) ); } } } function render_review( object $job ): void { $source = get_post( (int) $job->source_id ); $language = job_language_name( $job, translation_language_names() ); echo '

' . esc_html( get_the_title( $source ) . ' → ' . $language ) . '

'; printf( '

%s · %s estimated words

', esc_attr( 'uruvo-status-' . $job->status ), esc_html( job_status_label( $job ) ), esc_html( number_format_i18n( $job->word_count ) ) ); if ( $job->notice ) { echo '

' . esc_html( $job->notice ) . '

'; } $capacity_until = 'leased' === $job->status ? hosted_capacity_until( (int) $job->id ) : 0; if ( $capacity_until ) { echo '

' . esc_html( sprintf( 'Waiting for translation capacity. The queue can resume after %s. Your job will retry automatically.', wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) . ' T', $capacity_until ) ) ) . '

'; } $plan = json_decode( $job->plan, true ); if ( ! empty( $plan['unsupported'] ) ) { echo '

' . esc_html__( 'Check this content in your editor before publishing:', 'uruvo' ) . ' ' . esc_html( implode( ', ', $plan['unsupported'] ) ) . '

'; } if ( $job->target_id && current_user_can( 'edit_post', $job->target_id ) ) { $edit = get_edit_post_link( (int) $job->target_id ); if ( $edit ) { $published = 'publish' === get_post_status( (int) $job->target_id ); echo '

' . ( $published ? 'Your translation is live in WordPress.' : 'Your translation is saved. Open the WordPress editor to make final edits and publish.' ) . '

'; echo '
' . esc_html__( 'Open in WordPress editor', 'uruvo' ) . ''; $target = get_post( (int) $job->target_id ); $builder_link = $target ? builder_editor_link( $target ) : null; if ( $builder_link ) { /* translators: %s: name of the page builder. */ echo '' . esc_html( sprintf( __( 'Open in %s', 'uruvo' ), builder_name( page_builder( $target ) ) ) ) . ''; } if ( $published && $target && '' === $target->post_password ) { echo '' . esc_html__( 'View published translation', 'uruvo' ) . ''; } echo '' . esc_html__( 'Check visitor view', 'uruvo' ) . ''; echo '
'; } return; } if ( in_array( $job->status, array( 'queued', 'leased' ), true ) ) { echo '

The status updates here while you wait. When the copy is ready, you can review it here.

'; echo '
Cancel translation

This stops the job if it is still in progress.

'; start_form( 'cancel' ); echo ''; submit_button( 'Confirm cancellation', 'secondary' ); echo '
'; return; } if ( 'failed' === $job->status ) { if ( ! current_source( $job ) || unexpected_translation( $job ) ) { render_fresh_estimate_link( $job ); return; } if ( ! translation_connection_ready() ) { render_connection_required(); return; } echo '

Check the message above, then try the translation again.

'; start_form( 'retry' ); echo ''; submit_button( 'Retry translation', 'secondary' ); echo ''; return; } if ( 'stale' === $job->status ) { echo '

The source changed while the translation was running.

'; render_fresh_estimate_link( $job ); return; } if ( ! in_array( $job->status, array( 'ready', 'applying' ), true ) || ! $job->proposal ) { return; } if ( ! current_source( $job ) ) { echo '

The source changed. Review the current estimate before queueing again.

'; render_fresh_estimate_link( $job ); return; } $target = linked_target( $job ); if ( $target && unexpected_translation( $job ) ) { echo '

' . esc_html__( 'A translation is already linked for this language. Start an update if you want to change it.', 'uruvo' ) . '

'; echo '' . esc_html__( 'Estimate an update', 'uruvo' ) . ''; return; } if ( $target && ! current_user_can( 'edit_post', $target->ID ) ) { echo '

The linked translation requires edit permission.

'; return; } $original = source_fields( $source ); $expected = $target ? target_review_hash( $target ) : 'new'; $proposal = review_fields( $job ); $merge = effective_content( $target, $proposal ); $choices = $target ? update_choices( $job, $target ) : array(); $labels = review_labels( $source ); $builder = page_builder( $source ); $builder_draft = $target && $builder && builder_has_pending_edits( $target ); if ( $builder_draft ) { echo '

' . esc_html__( 'The linked Beaver Builder layout has unpublished changes. Publish or discard those changes before applying a layout update.', 'uruvo' ) . '

'; } echo '

' . esc_html( $target ? __( 'Review the changes below. Leaving a field unchecked keeps its current translation.', 'uruvo' ) : __( 'Review the translation, then save it as a WordPress draft.', 'uruvo' ) ) . '

'; if ( $target && ! $choices ) { echo '

' . esc_html__( 'The proposed copy already matches this translation.', 'uruvo' ) . '

'; render_keep_translation( $job, $expected ); return; } start_form( 'apply' ); echo ''; foreach ( $proposal as $field => $value ) { $content_merge = 'content' === $field ? $merge : null; if ( $content_merge ) { $value = $content_merge['html']; if ( $content_merge['kept'] ) { echo '

' . esc_html( sprintf( 'Kept %d edited block(s). Review %d overlap(s) in the WordPress editor.', $content_merge['kept'], $content_merge['conflicts'] ) ) . '

'; } } $change = $choices[ $field ] ?? null; if ( $target && ! $change ) { continue; } if ( ! $target && '' === trim( $value ) && '' === trim( $original[ $field ] ?? '' ) ) { continue; } echo '
'; if ( $target ) { $protected = $change['protected']; $disabled = $change['disabled']; printf( '

', esc_attr( $field ), checked( ! $protected && ! $disabled, true, false ), disabled( $disabled, true, false ), esc_html( $labels[ $field ] ) ); if ( $protected ) { echo '

' . esc_html__( 'Your edits will be kept unless you select this change.', 'uruvo' ) . '

'; } render_update_diff( $field, $change['before'], $change['after'] ); echo '
'; continue; } else { echo '

' . esc_html( $labels[ $field ] ) . '

'; } echo '
'; $before = $original[ $field ] ?? ''; if ( 'builder' === $field ) { $before = $before ? implode( '

', builder_copy( $before ) ) : ''; $value = implode( '

', builder_copy( $value ) ); } render_review_copy( __( 'Original', 'uruvo' ), $before ); render_review_copy( __( 'Proposed translation', 'uruvo' ), $value ); echo '

'; } submit_button( $target ? __( 'Apply selected updates', 'uruvo' ) : __( 'Save as WordPress draft', 'uruvo' ) ); echo ''; if ( $target ) { render_keep_translation( $job, $expected ); } } function render_keep_translation( object $job, string $hash ): void { if ( 'update' !== ( job_policy( $job )['mode'] ?? '' ) ) { return; } start_form( 'keep_current_translation' ); printf( '', absint( $job->id ), esc_attr( $hash ) ); submit_button( __( 'Keep current translation', 'uruvo' ), 'secondary', 'submit', false ); echo ''; } function render_fresh_estimate_link( object $job ): void { $args = array( 'source' => $job->source_id, 'wct_targets' => $job->target_slug, 'mode' => 'update' === ( job_policy( $job )['mode'] ?? '' ) ? 'update' : 'new' ); echo '' . esc_html__( 'Get a new estimate', 'uruvo' ) . ''; } function review_plain_text( string $copy ): string { $lines = preg_replace( '~|~i', "\n", $copy ); return trim( html_entity_decode( wp_strip_all_tags( $lines ?? $copy ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); } function render_update_diff( string $field, string $before, string $after ): void { if ( 'builder' === $field ) { $before = $before ? implode( "\n", builder_copy( $before ) ) : ''; $after = $after ? implode( "\n", builder_copy( $after ) ) : ''; } $before = review_plain_text( $before ); $after = review_plain_text( $after ); $diff = wp_text_diff( $before, $after, array( 'title_left' => __( 'Current translation', 'uruvo' ), 'title_right' => __( 'Proposed update', 'uruvo' ), ) ); if ( $diff ) { echo wp_kses_post( $diff ); } else { echo '

' . esc_html__( 'Formatting or links changed. Check the result in the page editor.', 'uruvo' ) . '

'; } } function render_review_copy( string $label, string $copy ): void { $text = review_plain_text( $copy ); printf( '
%s

%s

', esc_html( $label ), esc_html( '' === $text ? 'Empty' : $text ) ); } function render_connection(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } echo '

' . esc_html__( 'Translation preferences', 'uruvo' ) . '

'; $key = 'uruvo_pair_' . get_current_user_id(); $token = get_transient( $key ); if ( $token ) { delete_transient( $key ); echo '

Worker endpoint ' . esc_html( rest_url( REST_NAMESPACE . '/' ) ) . '

'; echo '

Worker token ' . esc_html( $token ) . '

'; } start_form( 'preferences' ); echo '

'; echo '

Updates use your shared word allowance.

'; submit_button( __( 'Save preferences', 'uruvo' ), 'secondary', 'submit', false ); echo ''; if ( hosted_settings() ) { return; } echo '
Use a self-hosted worker'; $paired = (bool) get_option( 'uruvo_worker_token_hash' ); echo '

' . ( $paired ? 'Worker access enabled.' : 'Generate a token for the operator who will translate your queued text.' ) . ' Text from selected public content is sent to the configured worker.

'; start_form( 'rotate' ); submit_button( $paired ? 'Rotate worker token' : 'Generate worker token', 'secondary', 'submit', false ); echo ''; if ( $paired ) { start_form( 'disconnect' ); submit_button( 'Revoke worker access', 'secondary', 'submit', false ); echo ''; } echo '
'; } function translation_row_action( array $actions, \WP_Post $post ): array { if ( polylang_ready() && is_supported_source( $post ) && current_user_can( 'edit_post', $post->ID ) ) { $actions['uruvo_translate'] = '' . esc_html__( 'Translate', 'uruvo' ) . ''; } return $actions; } function register_translation_bulk( array $actions ): array { if ( polylang_ready() ) { $actions['uruvo_translate'] = 'Estimate translations'; } return $actions; } function handle_translation_bulk( string $redirect, string $action, array $post_ids ): string { if ( 'uruvo_translate' !== $action ) { return $redirect; } return admin_url_for( array( 'sources' => implode( ',', array_map( 'absint', $post_ids ) ) ) ); }