$file_path ) ); return ''; } $zip = new ZipArchive(); if ( true !== $zip->open( $file_path ) ) { Logger::error( 'DocxExtractor failed: unable to open zip archive.', array( 'path' => $file_path ) ); return ''; } $xml_content = $zip->getFromName( 'word/document.xml' ); $zip->close(); if ( ! $xml_content ) { Logger::error( 'DocxExtractor failed: word/document.xml not found in zip.', array( 'path' => $file_path ) ); return ''; } // Parse XML using DOMDocument. $dom = new DOMDocument(); // Disable external entity loading for security. $old_entity_loader = null; if ( function_exists( 'libxml_disable_entity_loader' ) && PHP_VERSION_ID < 80000 ) { // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated $old_entity_loader = libxml_disable_entity_loader( true ); } libxml_use_internal_errors( true ); $dom->loadXML( $xml_content ); libxml_clear_errors(); if ( $old_entity_loader !== null && PHP_VERSION_ID < 80000 ) { // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated libxml_disable_entity_loader( $old_entity_loader ); } $paragraphs = $dom->getElementsByTagName( 'p' ); $text_lines = array(); foreach ( $paragraphs as $paragraph ) { $p_text = ''; $texts = $paragraph->getElementsByTagName( 't' ); foreach ( $texts as $t ) { $p_text .= $t->nodeValue; } if ( '' !== trim( $p_text ) ) { $text_lines[] = $p_text; } } return TextHelper::clean_text( implode( "\n\n", $text_lines ) ); } }