$post->ID, $posts ); $placeholders = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT job.id,job.source_id,job.source_version,job.target_id,job.target_slug,job.target_locale,job.status,job.notice,job.updated_at FROM %i job INNER JOIN ( SELECT MAX(id) latest,MAX(CASE WHEN status IN ('draft','applied') THEN id END) delivered FROM %i WHERE source_id IN ($placeholders) AND status <> 'discarded' GROUP BY source_id,target_slug ) history ON job.id IN (history.latest,history.delivered) ORDER BY job.id DESC", array( jobs_table(), jobs_table(), ...$ids ) ) ); $by_page = array(); foreach ( $rows as $job ) { $jobs = &$by_page[ $job->source_id ][ $job->target_slug ]; $jobs['latest'] ??= $job; if ( in_array( $job->status, array( 'draft', 'applied' ), true ) ) { $jobs['delivered'] = $job; } unset( $jobs ); } return $by_page; } function content_statuses(): array { return array( 'missing' => __( 'Missing translation', 'uruvo' ), 'stale' => __( 'Needs update', 'uruvo' ), 'ready' => __( 'Ready to review', 'uruvo' ), 'failed' => __( 'Needs attention', 'uruvo' ), 'working' => __( 'In progress', 'uruvo' ), ); } function content_language_states( \WP_Post $source, array $names, array $jobs, string $destination = '' ): array { $original_language = pll_get_post_language( $source->ID ); if ( ! $original_language || ! is_supported_source( $source ) ) { return array(); } unset( $names[ $original_language ] ); if ( $destination ) { $names = array_intersect_key( $names, array( $destination => true ) ); } $linked = pll_get_post_translations( $source->ID ); $version = $jobs ? source_version( $source, source_fields( $source ) ) : ''; $states = array(); foreach ( $names as $slug => $name ) { $target = ! empty( $linked[ $slug ] ) ? get_post( $linked[ $slug ] ) : null; if ( $target && ( 'trash' === $target->post_status || $target->post_type !== $source->post_type ) ) { $target = null; } $latest = $jobs[ $slug ]['latest'] ?? null; $delivered = $jobs[ $slug ]['delivered'] ?? null; $status = $target ? 'linked' : 'missing'; if ( $target && (int) ( $delivered->target_id ?? 0 ) === $target->ID ) { $status = hash_equals( $version, $delivered->source_version ) ? 'current' : 'stale'; } if ( $latest && hash_equals( $version, $latest->source_version ) ) { $status = match ( $latest->status ) { 'ready', 'applying' => 'ready', 'queued', 'leased' => 'working', 'failed' => 'failed', default => $status, }; } $states[ $slug ] = array( 'name' => $name, 'status' => $status, 'target' => $target, 'job' => $latest ); } return $states; } /** * Filter the complete content list without loading every post into memory. * Checked IDs are excluded from the next request, including sparse results. */ function filtered_content( array $args, array $names, string $status, string $destination, int $after ): array { $matches = array(); $cursor = $after; $args['no_found_rows'] = true; $args['orderby'] = 'ID'; $args['order'] = 'ASC'; $args['posts_per_page'] = 50; unset( $args['paged'] ); for ( $batch = 0; $batch < 5; ++$batch ) { $args['uruvo_after_id'] = $cursor; $lower_bound = static function ( string $where, \WP_Query $query ): string { global $wpdb; $after_id = (int) $query->get( 'uruvo_after_id' ); return $after_id ? $where . $wpdb->prepare( " AND $wpdb->posts.ID > %d", $after_id ) : $where; }; add_filter( 'posts_where', $lower_bound, 10, 2 ); try { $posts = ( new \WP_Query( $args ) )->posts; } finally { remove_filter( 'posts_where', $lower_bound, 10 ); } $jobs = overview_jobs( $posts ); foreach ( $posts as $post ) { if ( ! current_user_can( 'edit_post', $post->ID ) ) { $cursor = $post->ID; continue; } $states = content_language_states( $post, $names, $jobs[ $post->ID ] ?? array(), $destination ); if ( $states && ( ! $status || in_array( $status, array_column( $states, 'status' ), true ) ) ) { if ( 20 === count( $matches ) ) { return array( $matches, $cursor ); } $matches[] = $post; } $cursor = $post->ID; } if ( count( $posts ) < $args['posts_per_page'] ) { return array( $matches, 0 ); } } return array( $matches, $cursor ); } function target_review_hash( \WP_Post $target ): string { return fingerprint( array( $target->ID, $target->post_status, source_fields( $target ) ) ); } function review_labels( \WP_Post $source ): array { return array( 'title' => __( 'Title', 'uruvo' ), 'excerpt' => __( 'Excerpt', 'uruvo' ), 'content' => __( 'Page content', 'uruvo' ), /* translators: %s: name of the page builder. */ 'builder' => sprintf( __( '%s content', 'uruvo' ), builder_name( page_builder( $source ) ) ), '_yoast_wpseo_title' => __( 'Search title', 'uruvo' ), '_yoast_wpseo_metadesc' => __( 'Search description', 'uruvo' ), ); } function update_choices( object $job, \WP_Post $target ): array { $current = source_fields( $target ); $proposal = review_fields( $job ); $hashes = (array) translation_meta( $target->ID, 'delivered_hashes' ); $merge = effective_content( $target, $proposal ); $choices = array(); foreach ( $proposal as $field => $value ) { $before = $current[ $field ] ?? ''; $value = 'content' === $field && $merge ? $merge['html'] : $value; if ( $before === $value ) { continue; } $protected = 'content' === $field && $merge ? (bool) $merge['conflicts'] : ( '' !== $before || isset( $hashes[ $field ] ) ) && ( $hashes[ $field ] ?? '' ) !== fingerprint( $before ); $choices[ $field ] = array( 'before' => $before, 'after' => $value, 'protected' => $protected, 'overlap' => 'content' === $field && $merge && $merge['conflicts'], 'disabled' => 'builder' === $field && builder_has_pending_edits( $target ), ); } return $choices; } function safe_batch_update( object $job ): array { if ( 'ready' !== $job->status || ! $job->proposal || 'update' !== ( job_policy( $job )['mode'] ?? '' ) || ! current_source( $job ) || ! current_user_can( 'edit_post', $job->source_id ) ) { return array(); } $target = linked_target( $job ); if ( ! $target || 'publish' !== $target->post_status || '' !== $target->post_password || ! current_user_can( 'edit_post', $target->ID ) || ! current_user_can( get_post_type_object( $target->post_type )->cap->publish_posts ) || wp_check_post_lock( $target->ID ) || unexpected_translation( $job ) ) { return array(); } $plan = json_decode( $job->plan, true ); if ( ! empty( $plan['unsupported'] ) || (int) ( $plan['policy']['linked'] ?? 0 ) !== $target->ID ) { return array(); } $choices = update_choices( $job, $target ); if ( array_filter( $choices, static fn( array $choice ): bool => $choice['overlap'] || $choice['disabled'] ) ) { return array(); } $safe = array_filter( $choices, static fn( array $choice ): bool => ! $choice['protected'] ); return $safe ? array( 'fields' => array_keys( $safe ), 'hash' => target_review_hash( $target ), 'choices' => $choices ) : array(); } function keep_current_translation( object $job, string $hash ): int|\WP_Error { return with_translation_lock( (int) $job->source_id, $job->target_slug, static function () use ( $job, $hash ): int|\WP_Error { $target = linked_target( $job ); $policy = job_policy( $job ); if ( 'update' !== ( $policy['mode'] ?? '' ) || ! current_source( $job ) || ! $target || (int) ( $policy['linked'] ?? 0 ) !== $target->ID || ! current_user_can( 'edit_post', $target->ID ) || ! hash_equals( target_review_hash( $target ), $hash ) ) { return new \WP_Error( 'review_changed', __( 'The page or its translation changed. Open a fresh review.', 'uruvo' ) ); } global $wpdb; $updated = $wpdb->update( jobs_table(), array( 'status' => 'applied', 'target_id' => $target->ID, 'updated_at' => current_time( 'mysql', true ), ), array( 'id' => $job->id, 'status' => 'ready' ) ); return 1 === $updated ? $target->ID : new \WP_Error( 'review_changed', __( 'The job changed. Open a fresh review.', 'uruvo' ) ); } ); } function job_next_step( object $job ): string { if ( in_array( $job->status, array( 'queued', 'leased' ), true ) && hosted_capacity_until( (int) $job->id ) ) { return __( 'Waiting for translation capacity. Resumes automatically.', 'uruvo' ); } if ( 'failed' === $job->status ) { return $job->notice ?: __( 'Open the job to see the error and how to retry.', 'uruvo' ); } return in_array( $job->status, array( 'queued', 'leased' ), true ) ? (string) $job->notice : ''; }