__( 'Queue and auto-publish', 'uruvo' ), /* translators: %s: number of selected items. */ 'selected' => __( '%s selected', 'uruvo' ), /* translators: %s: estimated word count. */ 'estimate' => __( 'Estimated total: %s words', 'uruvo' ), 'jobUpdate' => __( 'Translation job updated.', 'uruvo' ), 'jobsUpdate' => __( 'Translation jobs updated.', 'uruvo' ), 'manualHelp' => __( 'Queueing prepares copy for review. You publish the approved draft in WordPress.', 'uruvo' ), 'autoHelp' => __( 'New translations go live when ready. Jobs that need attention stay for review.', 'uruvo' ), 'searchLanguages' => __( 'Search languages', 'uruvo' ), 'noLanguageMatch' => __( 'No matching languages', 'uruvo' ), 'chooseLanguage' => __( 'Choose a language from the list.', 'uruvo' ), ) ); } function admin_page(): void { $job_id = absint( $_GET['job'] ?? 0 ); $view = $job_id ? 'jobs' : sanitize_key( wp_unslash( $_GET['view'] ?? '' ) ); $tabs = array( 'translate' => __( 'Translate', 'uruvo' ), 'jobs' => __( 'Jobs', 'uruvo' ) ); if ( current_user_can( 'manage_options' ) ) { $tabs['languages'] = __( 'Languages', 'uruvo' ); $tabs['settings'] = __( 'Settings', 'uruvo' ); } if ( ! isset( $tabs[ $view ] ) ) { $view = 'translate'; } printf( '

Uruvo

' . esc_html__( 'WordPress translations', 'uruvo' ) . '

', absint( get_current_user_id() ) ); render_allowance(); echo '
'; $notice_key = 'uruvo_notice_' . get_current_user_id(); $notice = get_transient( $notice_key ); if ( $notice ) { delete_transient( $notice_key ); printf( '

%s

', $notice[1] ? 'notice-error' : 'notice-success', $notice[1] ? 'alert' : 'status', esc_attr( implode( ',', $notice[2] ?? array() ) ), esc_html( $notice[0] ) ); } if ( 'languages' === $view ) { render_site_languages(); } elseif ( ! polylang_ready() ) { render_language_unavailable(); } elseif ( 'settings' === $view ) { render_settings(); } elseif ( $check = absint( $_GET['check'] ?? 0 ) ) { render_visitor_checks( $check, sanitize_key( wp_unslash( $_GET['language'] ?? '' ) ) ); } elseif ( $job_id ) { $back_to_content = 'content' === sanitize_key( wp_unslash( $_GET['from'] ?? '' ) ); $back_url = $back_to_content ? admin_url_for( source_filters( $_GET ) ) : admin_url_for( array( 'view' => 'jobs' ) ); echo '

' . esc_html( $back_to_content ? __( 'Back to content', 'uruvo' ) : __( 'Back to jobs', 'uruvo' ) ) . '

'; render_translation_steps( 3 ); start_live_job_region( $job_id ); render_job_content( $job_id ); end_live_job_region(); echo '
'; } elseif ( 'jobs' === $view ) { $bulk = bulk_job_ids( array_map( 'sanitize_text_field', (array) wp_unslash( $_GET['wct_jobs'] ?? array() ) ) ); $bulk ? render_bulk_review( $bulk ) : render_jobs(); } else { $source_id = absint( $_GET['source'] ?? 0 ); $legacy_ids = explode( ',', sanitize_text_field( wp_unslash( $_GET['sources'] ?? '' ) ) ); $batch = explode( ',', sanitize_text_field( wp_unslash( $_GET['wct_batch'] ?? '' ) ) ); $requested = array_map( 'sanitize_text_field', (array) wp_unslash( $_GET['wct_sources'] ?? array() ) ); $selection = array_merge( array( $source_id ), $legacy_ids, $batch, $requested ); $source_ids = array_unique( array_filter( array_map( 'absint', $selection ) ) ); if ( $source_ids ) { render_quote( $source_ids ); } else { render_source_picker(); } } echo '
'; } function render_translation_steps( int $step ): void { echo '
    '; foreach ( array( 1 => __( 'Choose content', 'uruvo' ), 2 => __( 'Choose languages', 'uruvo' ), 3 => __( 'Review and publish', 'uruvo' ) ) as $number => $label ) { printf( '%s', $number === $step ? ' aria-current="step"' : '', absint( $number ), esc_html( $label ) ); } echo '
'; } function render_setup_requirements(): void { $connected = translation_connection_ready(); $languages = count( pll_languages_list() ) >= 2; if ( $connected && $languages ) { return; } echo '

' . esc_html__( 'Get ready to translate', 'uruvo' ) . '

'; if ( ! current_user_can( 'manage_options' ) ) { echo '

' . esc_html__( 'Ask a site administrator to connect Uruvo and set up the site languages.', 'uruvo' ) . '

'; return; } echo ''; } function available_words(): ?int { $allowance = get_option( 'uruvo_allowance', array() ); return $allowance && empty( $allowance['unlimited'] ) ? max( 0, (int) $allowance['limit'] - (int) $allowance['delivered'] - (int) $allowance['reserved'] ) : null; } function render_allowance(): void { $allowance = get_option( 'uruvo_allowance', array() ); if ( ! $allowance ) { return; } $used = (int) $allowance['delivered']; $reserved = (int) $allowance['reserved']; $limit = (int) $allowance['limit']; if ( ! empty( $allowance['unlimited'] ) ) { printf( '
%s%s
', esc_html__( 'Single-site Unlimited', 'uruvo' ), /* translators: %s: number of words translated this year. */ esc_html( sprintf( __( '%s words translated this year', 'uruvo' ), number_format_i18n( $used ) ) ) ); return; } printf( '
%s%s%s', esc_html__( 'Shared allowance', 'uruvo' ), /* translators: %s: number of words available. */ esc_html( sprintf( __( '%s words available', 'uruvo' ), number_format_i18n( available_words() ) ) ), /* translators: 1: words used, 2: plan word allowance. */ esc_html( sprintf( __( '%1$s of %2$s used', 'uruvo' ), number_format_i18n( $used ), number_format_i18n( $limit ) ) ) ); if ( $reserved ) { echo '' . esc_html( number_format_i18n( $reserved ) ) . ' words reserved'; } echo '
'; } function render_settings(): void { echo '

' . esc_html__( 'Site languages', 'uruvo' ) . '

'; echo '

' . esc_html__( 'Choose the languages and types of content you publish.', 'uruvo' ) . '

'; echo '' . esc_html__( 'Manage languages', 'uruvo' ) . ''; echo '
'; render_hosted_connection(); echo '
'; render_connection(); echo '
'; render_store_translation_settings(); echo '
'; } function render_source_picker(): void { $all_types = array_filter( translation_post_types(), static fn ( \WP_Post_Type $type ): bool => current_user_can( $type->cap->edit_published_posts ) ); $type = sanitize_key( wp_unslash( $_GET['wct_type'] ?? '' ) ); $type = isset( $all_types[ $type ] ) ? $type : ''; $search = sanitize_text_field( wp_unslash( $_GET['wct_search'] ?? '' ) ); $language_names = translation_language_names(); $language = sanitize_key( wp_unslash( $_GET['wct_lang'] ?? '' ) ); $unassigned = UNASSIGNED_LANGUAGE_FILTER === $language && $language_names; $language = isset( $language_names[ $language ] ) || $unassigned ? $language : ''; $page = max( 1, absint( $_GET['wct_page'] ?? 1 ) ); $statuses = content_statuses(); $status = sanitize_key( wp_unslash( $_GET['wct_status'] ?? '' ) ); $status = isset( $statuses[ $status ] ) && count( $language_names ) > 1 ? $status : ''; $destination = sanitize_key( wp_unslash( $_GET['wct_target'] ?? '' ) ); $destination = isset( $language_names[ $destination ] ) ? $destination : ''; $after = absint( $_GET['wct_after'] ?? 0 ); $filters = array( 'wct_type' => $type, 'wct_search' => $search, 'wct_lang' => $language, 'wct_page' => $page, 'wct_target' => $destination, 'wct_status' => $status, 'wct_after' => $after ); $types = $type ? array( $type => $all_types[ $type ] ) : $all_types; $args = array( 'post_type' => array_keys( $types ), 'post_status' => 'publish', 'has_password' => false, 'ignore_sticky_posts' => true, 'lang' => $unassigned ? '' : $language, 's' => $search, 'search_columns' => array( 'post_title' ), 'orderby' => array( 'title' => 'ASC', 'ID' => 'ASC' ), 'posts_per_page' => 20, 'paged' => $page, ); if ( $unassigned ) { $args['tax_query'] = array( array( 'taxonomy' => 'language', 'operator' => 'NOT EXISTS' ) ); } $can_edit_others = array_filter( $types, static fn ( \WP_Post_Type $item ): bool => current_user_can( $item->cap->edit_others_posts ) ); if ( ! $can_edit_others ) { $args['author'] = get_current_user_id(); } $sources = null; $next = 0; $editable = array(); if ( $types && ( $status || $destination ) ) { list( $editable, $next ) = filtered_content( $args, $language_names, $status, $destination, $after ); } elseif ( $types ) { $sources = new \WP_Query( $args ); $editable = array_filter( $sources->posts, static fn ( \WP_Post $post ): bool => current_user_can( 'edit_post', $post->ID ) ); } render_setup_requirements(); echo '
'; render_translation_steps( 1 ); echo '

' . esc_html__( 'Choose content to translate', 'uruvo' ) . '

'; echo '

' . esc_html__( 'See what’s missing, needs an update, or is ready to review.', 'uruvo' ) . '

'; echo '
'; echo '
'; echo '
'; echo '
'; if ( $language_names ) { echo '
', esc_attr( UNASSIGNED_LANGUAGE_FILTER ), selected( $unassigned, true, false ), esc_html__( 'No language assigned', 'uruvo' ) ); } if ( count( $language_names ) > 1 ) { echo '
'; } echo ''; if ( '' !== $search || $type || $language || $status || $destination ) { echo '' . esc_html__( 'Clear filters', 'uruvo' ) . ''; } echo '
'; render_source_rows( $editable, $all_types, $filters, $language_names ); if ( ! $editable ) { $empty = $next ? __( 'Keep looking for content that matches these filters.', 'uruvo' ) : ( $unassigned ? __( 'No matching content needs a language assigned.', 'uruvo' ) : __( 'Try another filter or publish content you can edit.', 'uruvo' ) ); echo '
' . esc_html__( 'No matching content', 'uruvo' ) . '

' . esc_html( $empty ) . '

'; } $total_pages = (int) ( $sources->max_num_pages ?? 0 ); if ( ( $status || $destination ) && ( $after || $next ) ) { echo ''; } elseif ( $page > 1 || $total_pages > 1 ) { echo ''; } if ( $language_names && current_user_can( 'manage_options' ) && array_diff_key( public_custom_post_types(), translation_post_types() ) ) { echo '

' . esc_html__( 'Enable more content types', 'uruvo' ) . '

'; } echo '
'; } function render_source_rows( array $posts, array $types, array $filters, array $language_names ): void { $reviewing = 'ready' === $filters['wct_status']; echo '
'; foreach ( $filters as $key => $value ) { printf( '', esc_attr( $key ), esc_attr( $value ) ); } if ( 'stale' === $filters['wct_status'] ) { echo ''; } if ( $filters['wct_target'] ) { printf( '', esc_attr( $filters['wct_target'] ) ); } if ( $reviewing ) { echo ''; echo '
'; } else { echo ''; echo '
'; echo '
'; } if ( ! $posts ) { echo '
'; return; } echo '
'; $jobs = overview_jobs( $posts ); foreach ( $posts as $post ) { $title = get_the_title( $post ) ?: '(Untitled)'; $language = pll_get_post_language( $post->ID ); $supported = is_supported_source( $post ); echo ''; } echo '
' . esc_html__( 'Content', 'uruvo' ) . '' . esc_html__( 'Original language', 'uruvo' ) . '' . esc_html__( 'Translations', 'uruvo' ) . '' . esc_html__( 'Action', 'uruvo' ) . '
'; if ( $supported && $language && ! $reviewing ) { /* translators: %s: post title. */ printf( '', absint( $post->ID ), esc_attr( sprintf( __( 'Select %s', 'uruvo' ), $title ) ) ); } printf( '%s%s%s', esc_html( $title ), esc_html( $types[ $post->post_type ]->labels->singular_name ), esc_html( $language_names[ $language ] ?? __( 'Unassigned', 'uruvo' ) ) ); render_translation_coverage( $post, $language_names, $jobs[ $post->ID ] ?? array(), $filters ); echo ''; if ( ! $supported ) { echo 'Editor unsupported'; } elseif ( ! $language ) { $link = admin_url_for( array( 'source' => $post->ID ) + $filters ); /* translators: %s: post title. */ printf( '%s', esc_url( $link ), esc_attr( sprintf( __( 'Set the language for %s', 'uruvo' ), $title ) ), esc_html__( 'Set language', 'uruvo' ) ); } else { $link = admin_url_for( array( 'source' => $post->ID ) + $filters ); /* translators: %s: post title. */ printf( '' . esc_html__( 'Translate', 'uruvo' ) . '', esc_url( $link ), esc_attr( sprintf( __( 'Translate %s', 'uruvo' ), $title ) ) ); } echo '
'; } function render_translation_coverage( \WP_Post $post, array $names, array $jobs, array $filters ): void { $states = content_language_states( $post, $names, $jobs, $filters['wct_target'] ); $labels = content_statuses(); $missing = 0; $has_items = false; foreach ( $states as $slug => $entry ) { $status = $entry['status']; if ( $filters['wct_status'] && $status !== $filters['wct_status'] ) { continue; } $target = $entry['target']; $job = $entry['job']; if ( 'missing' === $status && ! $filters['wct_target'] && ! $filters['wct_status'] ) { ++$missing; continue; } $link = ''; $action = ''; $detail = ''; $stage = $target ? ( get_post_status_object( $target->post_status )->label ?? __( 'Linked', 'uruvo' ) ) : ''; if ( in_array( $status, array( 'ready', 'working', 'failed' ), true ) ) { $link = admin_url_for( array( 'job' => $job->id, 'from' => 'content' ) + $filters ); $action = 'ready' === $status ? __( 'Review', 'uruvo' ) : __( 'View job', 'uruvo' ); $detail = job_next_step( $job ); } elseif ( 'stale' === $status || 'missing' === $status ) { $link = admin_url_for( array( 'source' => $post->ID, 'mode' => 'stale' === $status ? 'update' : 'new', 'wct_targets' => $slug ) + $filters ); $action = 'stale' === $status ? __( 'Estimate update', 'uruvo' ) : __( 'Estimate', 'uruvo' ); } elseif ( $target ) { $link = current_user_can( 'edit_post', $target->ID ) ? visitor_check_url( $post->ID, $slug ) : ( 'publish' === $target->post_status && '' === $target->post_password ? get_permalink( $target ) : '' ); $action = __( 'Preview', 'uruvo' ); } $parts = array_filter( array( $entry['name'], $stage, $labels[ $status ] ?? ( 'current' === $status ? __( 'Up to date', 'uruvo' ) : '' ) ) ); if ( ! $has_items ) { echo ''; } if ( $missing ) { /* translators: %d: number of languages without a translation. */ echo '' . esc_html( sprintf( _n( '%d language missing', '%d languages missing', $missing, 'uruvo' ), $missing ) ) . ''; } elseif ( ! $has_items ) { echo '—'; } } function render_quote( array $source_ids ): void { $filters = source_filters( $_GET ); if ( 1 === count( $source_ids ) ) { $source = get_post( reset( $source_ids ) ); if ( is_supported_source( $source ) && current_user_can( 'edit_post', $source->ID ) && ! pll_get_post_language( $source->ID ) ) { render_source_language_picker( $source, $filters ); return; } } $versions = array(); $eligible = array(); $seen = array(); $pending_jobs = array(); $totals = array(); $titles = array(); $skipped = 0; $linked = 0; $can_publish = true; $mode = 'update' === sanitize_key( wp_unslash( $_GET['mode'] ?? '' ) ) ? 'update' : 'new'; $language_names = translation_language_names(); foreach ( $source_ids as $source_id ) { $source = get_post( $source_id ); if ( ! is_supported_source( $source ) || ! current_user_can( 'edit_post', $source_id ) || ! pll_get_post_language( $source_id ) ) { ++$skipped; continue; } $source_slug = pll_get_post_language( $source_id ); $titles[] = array( get_the_title( $source ) ?: '(Untitled)', $language_names[ $source_slug ] ?? pll_get_post_language( $source_id, 'name' ) ); $group = min( translation_group_ids( $source_id ) ); $can_publish = $can_publish && current_user_can( get_post_type_object( $source->post_type )->cap->publish_posts ); foreach ( $language_names as $slug => $name ) { if ( $source_slug === $slug || isset( $seen[ $group ][ $slug ] ) ) { continue; } $target_id = translation_target_id( $source_id, $slug ); if ( 'new' === $mode && $target_id ) { ++$linked; $seen[ $group ][ $slug ] = true; continue; } if ( 'new' === $mode && ( $pending = pending_translation( $source_id, $slug ) ) ) { $pending_jobs[ $pending->id ] = current_user_can( 'edit_post', $pending->source_id ); $seen[ $group ][ $slug ] = true; continue; } $plan = create_plan( $source_id, $slug, $mode ); if ( is_wp_error( $plan ) ) { continue; } $seen[ $group ][ $slug ] = true; $versions[ $source_id ] = $plan['source_version']; $eligible[ $source_id ][] = $slug; $totals[ $slug ] ??= array( 'words' => 0, 'items' => 0 ); $totals[ $slug ]['words'] += $plan['word_count']; ++$totals[ $slug ]['items']; } } $back = admin_url_for( $filters ); $connected = translation_connection_ready(); if ( count( $language_names ) < 2 ) { render_setup_requirements(); } echo '

' . esc_html__( '← Back to content', 'uruvo' ) . '

'; render_translation_steps( 2 ); echo '

' . esc_html__( 'Choose target languages', 'uruvo' ) . '

' . esc_html__( 'Check the estimate before sending your content for translation.', 'uruvo' ) . '

'; echo ''; if ( $linked ) { /* translators: %d: number of existing translations skipped. */ echo '

' . esc_html( sprintf( _n( '%d linked translation skipped.', '%d linked translations skipped.', $linked, 'uruvo' ), $linked ) ) . '

'; } if ( $pending_jobs ) { /* translators: %d: number of translations with an existing job. */ echo '

' . esc_html( sprintf( _n( '%d translation is already in Jobs.', '%d translations are already in Jobs.', count( $pending_jobs ), 'uruvo' ), count( $pending_jobs ) ) ); $can_open = array_keys( array_filter( $pending_jobs ) ); if ( $can_open ) { $link = admin_url_for( 1 === count( $can_open ) ? array( 'job' => $can_open[0] ) : array( 'view' => 'jobs' ) ); echo ' ' . esc_html__( 'Open existing work', 'uruvo' ) . ''; } echo '

'; } if ( $titles ) { echo '
' . esc_html__( 'Selected content', 'uruvo' ) . '
'; } if ( $skipped ) { $builder = 1 === count( $source_ids ) && $source instanceof \WP_Post ? page_builder( $source ) : ''; $message = $builder && ! builder_available( $builder ) /* translators: %s: name of the page builder. */ ? sprintf( __( 'Translation is unavailable for this %s layout. Check that your editor is active and supported by Uruvo.', 'uruvo' ), builder_name( $builder ) ) /* translators: %d: number of unavailable content items. */ : sprintf( __( '%d selection(s) are unavailable. Choose published content with a source language that you can edit.', 'uruvo' ), $skipped ); echo '

' . esc_html( $message ) . '

'; } if ( ! $totals ) { $message = 'update' === $mode ? __( 'There are no linked translations to update for this selection.', 'uruvo' ) : __( 'There are no eligible target languages in this view.', 'uruvo' ); if ( $linked || $pending_jobs ) { $message = __( 'Every available language already has a translation or a job in progress.', 'uruvo' ); } echo '

' . esc_html( $message ) . '

'; return; } $available = available_words(); $checked_targets = explode( ',', sanitize_text_field( wp_unslash( $_GET['wct_targets'] ?? '' ) ) ); if ( null !== $available ) { echo '

Last checked balance: ' . esc_html( number_format_i18n( $available ) ) . ' words available.

'; } if ( $connected ) { start_form( 'queue' ); foreach ( $filters as $key => $value ) { printf( '', esc_attr( $key ), esc_attr( $value ) ); } printf( '', esc_attr( wp_json_encode( $versions ) ) ); printf( '', esc_attr( wp_json_encode( $eligible ) ), esc_attr( $mode ) ); } printf( '
' . esc_html__( 'Target languages', 'uruvo' ) . '', esc_attr( $available ?? '' ) ); foreach ( $totals as $slug => $total ) { printf( '', esc_attr( $slug ), absint( $total['words'] ), checked( in_array( $slug, $checked_targets, true ), true, false ), esc_html( $language_names[ $slug ] ), /* translators: %d: number of items eligible for translation into the language. */ esc_html( sprintf( _n( '%d item', '%d items', $total['items'], 'uruvo' ), $total['items'] ) ), esc_html( number_format_i18n( $total['words'] ) ) ); } echo '

'; if ( $connected && 'new' === $mode && $can_publish ) { printf( '

%s

', checked( ! empty( $_GET['auto_publish'] ), true, false ), esc_html__( 'Publish new translations automatically when ready', 'uruvo' ), esc_html__( 'Results with unsupported blocks or a conflict stay in Jobs for review.', 'uruvo' ) ); } if ( $connected ) { echo '
'; echo '

' . esc_html__( 'Queueing prepares copy for review. You publish the approved draft in WordPress.', 'uruvo' ) . '

'; } else { render_connection_required(); } echo ''; } function render_connection_required(): void { echo '

' . esc_html__( 'Connect this site to Uruvo before requesting translations.', 'uruvo' ) . ' '; if ( current_user_can( 'manage_options' ) ) { echo '' . esc_html__( 'Open connection settings', 'uruvo' ) . ''; } else { esc_html_e( 'Ask a site administrator to finish setup.', 'uruvo' ); } echo '

'; } function job_status_label( object $job ): string { if ( 'leased' === $job->status && hosted_capacity_until( (int) $job->id ) ) { return __( 'Waiting for capacity', 'uruvo' ); } if ( in_array( $job->status, array( 'draft', 'applied' ), true ) && ! empty( $job->target_id ) ) { return 'publish' === get_post_status( (int) $job->target_id ) ? __( 'Published', 'uruvo' ) : __( 'Draft saved', 'uruvo' ); } return match ( $job->status ) { 'queued' => __( 'Queued', 'uruvo' ), 'leased' => __( 'Translating', 'uruvo' ), 'ready', 'applying' => __( 'Ready to review', 'uruvo' ), 'failed' => __( 'Needs attention', 'uruvo' ), 'stale' => __( 'Source changed', 'uruvo' ), 'discarded' => __( 'Canceled', 'uruvo' ), default => __( 'Finished', 'uruvo' ), }; } function translation_language_names(): array { $languages = pll_languages_list( array( 'fields' => null ) ); $counts = array_count_values( array_map( static fn ( $language ) => $language->name, $languages ) ); $catalog = site_language_catalog(); $names = array(); foreach ( $languages as $language ) { $names[ $language->slug ] = $counts[ $language->name ] > 1 ? ( $catalog[ $language->locale ]['label'] ?? $language->name ) : $language->name; } return $names; } function job_filters(): array { return array( 'all' => array( __( 'All jobs', 'uruvo' ), array() ), 'pending' => array( __( 'In progress', 'uruvo' ), array( 'queued', 'leased' ) ), 'review' => array( __( 'Ready to review', 'uruvo' ), array( 'ready', 'applying' ) ), 'attention' => array( __( 'Needs attention', 'uruvo' ), array( 'failed', 'stale' ) ), 'finished' => array( __( 'Finished', 'uruvo' ), array( 'draft', 'applied', 'discarded' ) ), ); } function job_filter( string $filter ): string { return isset( job_filters()[ $filter ] ) ? $filter : 'all'; } function bulk_job_ids( array $values ): array { // Redirects use a comma-separated list while checkboxes submit an array. $ids = array_map( 'absint', 1 === count( $values ) ? explode( ',', reset( $values ) ) : $values ); return array_slice( array_values( array_unique( array_filter( $ids ) ) ), 0, 50 ); } function render_bulk_review( array $ids ): void { $names = translation_language_names(); $from_content = 'content' === sanitize_key( wp_unslash( $_GET['from'] ?? '' ) ); $back = $from_content ? source_filters( $_GET ) : array( 'view' => 'jobs', 'status' => 'review' ); echo '

' . esc_html( $from_content ? __( 'Back to content', 'uruvo' ) : __( 'Back to Jobs', 'uruvo' ) ) . '

'; echo '

' . esc_html__( 'Approve translations', 'uruvo' ) . '

' . esc_html__( 'Open each item to check the proposed changes.', 'uruvo' ) . '

'; $selected = array(); $updates = array(); $individual = array(); $publish = true; foreach ( $ids as $id ) { $job = job_row( $id ); if ( ! $job || ! current_user_can( 'edit_post', $job->source_id ) ) { continue; } $source = get_post( (int) $job->source_id ); if ( ! $source ) { continue; } $title = get_the_title( $source ) . ' · ' . job_language_name( $job, $names ); $update = safe_batch_update( $job ); if ( $update ) { $updates[ $id ] = $update['hash']; printf( '
%s — %s', esc_html( $title ), esc_html__( 'Update to a live translation', 'uruvo' ) ); $labels = review_labels( $source ); foreach ( $update['choices'] as $field => $change ) { printf( '

%s

', esc_html( $labels[ $field ] ) ); if ( $change['protected'] ) { echo '

' . esc_html__( 'Your edited text will be kept.', 'uruvo' ) . '

'; } else { render_update_diff( $field, $change['before'], $change['after'] ); } } echo '
'; continue; } if ( ! can_approve_new_job( $job ) ) { $individual[ $id ] = $title; continue; } $selected[] = $id; $publish = $publish && can_approve_new_job( $job, true ); printf( '
%s — %s
', esc_html( $title ), esc_html__( 'New translation', 'uruvo' ) ); $proposal = review_fields( $job ); $original = source_fields( $source ); $original_body = isset( $original['builder'] ) ? implode( "\n", builder_copy( $original['builder'] ) ) : $original['content']; $proposal_body = isset( $proposal['builder'] ) ? implode( "\n", builder_copy( $proposal['builder'] ) ) : ( $proposal['content'] ?? '' ); render_review_copy( __( 'Original', 'uruvo' ), $original['title'] . "\n\n" . $original_body ); render_review_copy( __( 'Proposed translation', 'uruvo' ), ( $proposal['title'] ?? '' ) . "\n\n" . $proposal_body ); echo '
'; } if ( $individual ) { echo '
' . esc_html__( 'Review these individually', 'uruvo' ) . '
'; } if ( $selected ) { start_form( 'approve_bulk' ); foreach ( $selected as $id ) { printf( '', absint( $id ) ); } echo '
'; if ( $publish ) { echo ''; } echo '
'; } if ( $updates ) { start_form( 'apply_updates_bulk' ); foreach ( $updates as $id => $hash ) { printf( '', absint( $id ), absint( $id ), esc_attr( $hash ) ); } echo '

' . esc_html__( 'These changes will update the live translations at their existing URLs.', 'uruvo' ) . '

'; /* translators: %d: number of updates to publish. */ submit_button( sprintf( _n( 'Publish %d update', 'Publish %d updates', count( $updates ), 'uruvo' ), count( $updates ) ) ); echo ''; } echo '
'; } function render_jobs(): void { $before = absint( $_GET['before'] ?? 0 ); $filter = job_filter( sanitize_key( wp_unslash( $_GET['status'] ?? '' ) ) ); echo '

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

'; echo '

' . esc_html__( 'Open a ready job to review the copy and save a draft.', 'uruvo' ) . '

'; echo ''; start_live_job_region( 0, $before, $filter ); render_jobs_list( $before, $filter ); end_live_job_region(); echo '
'; } function start_live_job_region( int $job, int $before = 0, string $filter = 'all' ): void { printf( '
', esc_url( admin_url( 'admin-ajax.php' ) ), esc_attr( wp_create_nonce( 'uruvo_jobs' ) ), absint( $job ), absint( $before ), esc_attr( $filter ) ); } function end_live_job_region(): void { echo '

'; } function render_job_content( int $job_id ): void { $job = job_row( $job_id ); if ( ! $job || ! current_user_can( 'edit_post', $job->source_id ) ) { echo '

Translation job unavailable

Check Jobs for translations you can access.

'; return; } $pending = in_array( $job->status, array( 'queued', 'leased' ), true ); echo $pending ? '
' : '
'; render_review( $job ); if ( in_array( $job->status, array( 'failed', 'stale' ), true ) ) { $details = array( sprintf( 'Uruvo %s · WordPress %s', VERSION, get_bloginfo( 'version' ) ), sprintf( 'Job %d · %s', $job->id, job_status_label( $job ) ), sprintf( '%s: %s', __( 'Language', 'uruvo' ), job_language_name( $job, translation_language_names() ) ), sprintf( '%s: %s UTC', __( 'Last change', 'uruvo' ), $job->updated_at ), ); if ( job_next_step( $job ) ) { $details[] = job_next_step( $job ); } echo '
' . esc_html__( 'Details for support', 'uruvo' ) . '
' . esc_html( implode( "\n", $details ) ) . '
'; } if ( $pending ) { render_refresh_button( $job_id ); } echo '
'; } function render_refresh_button( int $job = 0, string $filter = 'all', int $before = 0 ): void { if ( hosted_settings() && current_user_can( 'manage_options' ) ) { start_form( 'hosted_sync' ); printf( '', absint( $job ), esc_attr( $filter ), absint( $before ) ); echo ''; } } function render_jobs_list( int $before, string $filter ): void { global $wpdb; $table = jobs_table(); $states = job_filters()[ $filter ][1]; $cutoff = $before ?: PHP_INT_MAX; $rows = $states ? $wpdb->get_results( $wpdb->prepare( 'SELECT id,source_id,target_id,target_slug,target_locale,status,word_count FROM %i WHERE id < %d AND status IN (' . implode( ',', array_fill( 0, count( $states ), '%s' ) ) . ') ORDER BY id DESC LIMIT 50', array_merge( array( $table, $cutoff ), $states ) ) ) : $wpdb->get_results( $wpdb->prepare( 'SELECT id,source_id,target_id,target_slug,target_locale,status,word_count FROM %i WHERE id < %d ORDER BY id DESC LIMIT 50', $table, $cutoff ) ); $rows = (array) $rows; $visible = array_filter( $rows, static fn ( object $row ): bool => current_user_can( 'edit_post', $row->source_id ) ); if ( ! $visible ) { echo '
No jobs in this view'; if ( 'all' === $filter && ! $before ) { echo '

Choose content to start a translation.

' . esc_html__( 'Choose content', 'uruvo' ) . ''; } else { echo '

Show all jobs

'; } echo '
'; } else { $names = translation_language_names(); $approvable = array(); $has_pending = false; foreach ( $visible as $row ) { $has_pending = $has_pending || in_array( $row->status, array( 'queued', 'leased' ), true ); $full = 'ready' === $row->status ? job_row( (int) $row->id ) : null; if ( $full && ( can_approve_new_job( $full ) || safe_batch_update( $full ) ) ) { $approvable[ $row->id ] = true; } } if ( $has_pending ) { render_refresh_button( 0, $filter, $before ); } if ( $approvable ) { echo '
'; echo '
'; } echo '
'; if ( $approvable ) { echo ''; } echo ''; foreach ( $visible as $row ) { $title = get_the_title( (int) $row->source_id ) ?: '(Untitled)'; $language = job_language_name( $row, $names ); $pending = in_array( $row->status, array( 'queued', 'leased' ), true ); $review = in_array( $row->status, array( 'ready', 'applying' ), true ); $action = $review ? __( 'Review translation', 'uruvo' ) : __( 'View job', 'uruvo' ); printf( '', $pending ? ' data-uruvo-pending' : '' ); if ( $approvable ) { echo ''; } printf( '', esc_html( $title ), esc_html( $language ), esc_attr( 'uruvo-status-' . $row->status ), esc_html( job_status_label( $row ) ), esc_html( number_format_i18n( $row->word_count ) ), esc_url( admin_url_for( array( 'job' => $row->id ) ) ), esc_attr( sprintf( '%s for %s', $action, $title ) ), esc_html( $action ) ); } echo '
' . esc_html__( 'Content', 'uruvo' ) . '' . esc_html__( 'Language', 'uruvo' ) . '' . esc_html__( 'Status', 'uruvo' ) . 'Est. words' . esc_html__( 'Action', 'uruvo' ) . '
'; if ( isset( $approvable[ $row->id ] ) ) { printf( '', absint( $row->id ), esc_attr( $title . ' · ' . $language ) ); } echo '%s%s%s%s%s
'; if ( $approvable ) { echo '
'; } } if ( $before || count( $rows ) === 50 ) { echo ''; } } function refresh_jobs(): void { check_ajax_referer( 'uruvo_jobs', 'nonce' ); if ( ! admin_capability() || ! polylang_ready() ) { wp_send_json_error( null, 403 ); } if ( current_user_can( 'manage_options' ) && hosted_settings() ) { hosted_dispatch(); } ob_start(); $job_id = absint( $_POST['job'] ?? 0 ); if ( $job_id ) { render_job_content( $job_id ); } else { render_jobs_list( absint( $_POST['before'] ?? 0 ), job_filter( sanitize_key( wp_unslash( $_POST['filter'] ?? '' ) ) ) ); } wp_send_json_success( array( 'html' => ob_get_clean() ) ); }