exists() ? $theme_obj->get_stylesheet_directory() : trailingslashit( get_theme_root() ) . $text_domain; } $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( $actual_src_dir . '/languages/' . $pot_file_name ); $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( $actual_src_dir . '/language/' . $pot_file_name ); $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( $actual_src_dir . '/' . $pot_file_name ); } if ( ! empty( $fallback_po ) && file_exists( $fallback_po ) ) { $fallback_dir = dirname( $fallback_po ); $parent_dir = dirname( $fallback_dir ); if ( ! empty( $text_domain ) ) { $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( trailingslashit( $fallback_dir ) . $pot_file_name ); $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( trailingslashit( $parent_dir ) . $pot_file_name ); } if ( is_dir( $fallback_dir ) && false === strpos( $fallback_po, 'af-translate' ) ) { $languages_pots = glob( trailingslashit( $fallback_dir ) . '*.pot' ); if ( ! empty( $languages_pots ) && is_array( $languages_pots ) ) { foreach ( $languages_pots as $f ) { $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( $f ); } } } if ( is_dir( $parent_dir ) && false === strpos( $fallback_po, 'af-translate' ) ) { $root_pots = glob( trailingslashit( $parent_dir ) . '*.pot' ); if ( ! empty( $root_pots ) && is_array( $root_pots ) ) { foreach ( $root_pots as $f ) { $pot_candidates[] = AF_Translate_Path_Validator::resolve_canonical_path( $f ); } } } } foreach ( $pot_candidates as $candidate ) { if ( ! empty( $candidate ) && is_string( $candidate ) ) { $canonical_pot = AF_Translate_Path_Validator::resolve_canonical_path( $candidate ); if ( file_exists( $canonical_pot ) ) { $pot_file = $canonical_pot; break; } } } $sandbox_sync_pot = $target_path . '/' . $pot_file_name; if ( empty( $pot_file ) ) { $generated = false; if ( class_exists( 'AF_Translate_POT_Generator' ) ) { $generated = AF_Translate_POT_Generator::generate_master_pot( $text_domain, $bundle_type, $sandbox_sync_pot ); } if ( ! $generated && ! empty( $fallback_po ) && file_exists( $fallback_po ) ) { if ( class_exists( 'AF_Translate_PO_Processor' ) ) { $parsed_entries = AF_Translate_PO_Processor::parse_file( $fallback_po ); if ( is_array( $parsed_entries ) ) { foreach ( $parsed_entries as &$entry ) { if ( isset( $entry['msgstr'] ) ) { $entry['msgstr'] = is_array( $entry['msgstr'] ) ? array_fill( 0, count( $entry['msgstr'] ), '' ) : ''; } } unset( $entry ); $pot_content = AF_Translate_PO_Processor::generate_po_string( $parsed_entries ); if ( ! empty( $pot_content ) && false !== file_put_contents( $sandbox_sync_pot, $pot_content ) ) { $generated = true; } } } } if ( $generated && file_exists( $sandbox_sync_pot ) ) { $pot_file = $sandbox_sync_pot; } } if ( empty( $pot_file ) && ! empty( $fallback_po ) && file_exists( $fallback_po ) ) { if ( class_exists( 'AF_Translate_PO_Processor' ) ) { $parsed_entries = AF_Translate_PO_Processor::parse_file( $fallback_po ); if ( is_array( $parsed_entries ) ) { foreach ( $parsed_entries as &$entry ) { if ( isset( $entry['msgstr'] ) ) { $entry['msgstr'] = is_array( $entry['msgstr'] ) ? array_fill( 0, count( $entry['msgstr'] ), '' ) : ''; } } unset( $entry ); $pot_content = AF_Translate_PO_Processor::generate_po_string( $parsed_entries ); if ( ! empty( $pot_content ) && false !== file_put_contents( $sandbox_sync_pot, $pot_content ) ) { $pot_file = $sandbox_sync_pot; } } } } if ( empty( $pot_file ) || ! file_exists( $pot_file ) ) { wp_send_json_error( esc_html__( 'Synchronization failed: Could not locate a valid master template source for this item.', 'af-translate' ) ); } if ( ! AF_Translate_Path_Validator::validate_containment( $pot_file, false ) ) { wp_send_json_error( esc_html__( 'Master template reference catalog path constraint violation.', 'af-translate' ) ); } $sync_results = AF_Translate_Compiler_Engine::sync_pot_into_po( $po_file, $pot_file ); if ( false !== $sync_results ) { $mo_file = str_replace( '.po', '.mo', $po_file ); if ( AF_Translate_Path_Validator::validate_containment( $mo_file, true ) ) { AF_Translate_Compiler_Engine::compile_po_to_mo( $po_file, $mo_file ); } $log_context = ! empty( $text_domain ) ? $text_domain : $bundle_type; $log_message = sprintf( __( 'Synchronized template layout for "%1$s" (+%2$d strings added, -%3$d obsolete tracking metrics dropped).', 'af-translate' ), strtoupper( $locale ), intval( $sync_results['new'] ), intval( $sync_results['obsolete'] ) ); if ( class_exists( 'AF_Translate_Admin' ) && method_exists( 'AF_Translate_Admin', 'log_activity' ) ) { AF_Translate_Admin::log_activity( $log_message, $log_context ); } $message = sprintf( esc_html__( 'OK: Merged from template. %1$d new strings added. %2$d obsolete strings removed.', 'af-translate' ), intval( $sync_results['new'] ), intval( $sync_results['obsolete'] ) ); wp_send_json_success( $message ); } else { wp_send_json_error( esc_html__( 'Synchronization failed: Unable to parse file stream data structures.', 'af-translate' ) ); } } }