$text ) { if ( ! is_string( $text ) ) { continue; } $text = wplng_text_esc( $text ); if ( '' !== $text ) { $texts_clear[] = $text; } } $texts = array_unique( $texts_clear ); // Remove duplicate /** * Update args */ wplng_args_setup( $args ); /** * Get all translations for all languages */ $translations_all_languages = wplng_get_translations(); /** * Get unknow texts & Separate page translations */ $texts_unknow = array(); $translations_in_page = array(); foreach ( $texts as $text ) { $is_in = false; $detection = wplng_api_feature_is_allow( 'detection' ); $array_index = (string) mb_substr( $text, 0, 1 ); $array_index .= (string) strlen( $text ); if ( ! empty( $translations_all_languages[ $array_index ] ) ) { foreach ( $translations_all_languages[ $array_index ] as $translation ) { // $source = $translation['source']; if ( $text === $translation['source'] && isset( $translation['translations'][ $args['language_target'] ] ) && is_string( $translation['translations'][ $args['language_target'] ] ) && isset( $translation['review'] ) && is_array( $translation['review'] ) && isset( $translation['post_id'] ) && is_int( $translation['post_id'] ) ) { $is_in = true; $review = in_array( $args['language_target'], $translation['review'], true ); $translated_text = wplng_text_esc( $translation['translations'][ $args['language_target'] ] ); $translations_in_page[] = array( 'source' => $text, 'post_id' => $translation['post_id'], 'review' => $review, 'translation' => $translated_text, ); break; } } } if ( ! $is_in && $detection ) { $texts_unknow[] = $text; } } $args['texts_unknow'] = $texts_unknow; /** * Limit $texts_unknow for a total of 4200 char */ $current_length = 0; $texts_unknow_limited = array(); foreach ( $texts_unknow as $text ) { $text_length = strlen( $text ); if ( $current_length + $text_length > WPLNG_MAX_TRANSLATIONS_CHAR ) { break; } $texts_unknow_limited[] = $text; $current_length += $text_length + 8; } /** * Get count_texts */ $args['count_texts'] = count( $texts ); $args['count_texts_unknow'] = count( $texts_unknow ); /** * Check if the current page is overloaded */ $args['overloaded'] = wplng_get_api_overloaded() && ! empty( $args['count_texts_unknow'] ); /** * Define $max_translations */ $max_translations = WPLNG_MAX_TRANSLATIONS_STR + 1; if ( $args['load'] === 'translated' ) { /** * This is the page reloaded after "Load in progress" was complete */ $max_translations = 0; wplng_clear_website_cache(); } elseif ( $args['load'] === 'enabled' && $args['count_texts_unknow'] > 10 && ! $args['overloaded'] && wplng_api_feature_is_allow( 'detection' ) && current_user_can( 'edit_posts' ) ) { /** * Current page identified as requiring "in progress" mode */ $args['load'] = 'progress'; $max_translations = 0; } else { $args['load'] = 'disabled'; } $texts_unknow_limited = array_splice( $texts_unknow_limited, 0, $max_translations ); /** * Get new translated text */ $texts_translated = wplng_api_call_translate( $texts_unknow_limited, $args['language_source'], $args['language_target'] ); $texts_unknow = array_diff_assoc( $texts_unknow, $texts_unknow_limited ); $args['texts_unknow'] = $texts_unknow; /** * Save new translation as wplng_translation CPT */ $translations_new = array(); foreach ( $texts_unknow_limited as $key => $text_source ) { if ( isset( $texts_translated[ $key ] ) ) { $translations_new[] = array( 'source' => $text_source, 'translation' => $texts_translated[ $key ], ); } } $translations_new = wplng_save_translations( $translations_new, $args['language_target'] ); /** * Merge know and new translations */ $args['translations'] = array_merge( $translations_in_page, $translations_new ); }