badge = $badge; } public function register(): void { add_filter( 'the_content', array( $this, 'wrap_content_videos' ), 21 ); } /** * Adds disclosure overlays only to declared local video attachments in the * queried singular post's main content. */ public function wrap_content_videos( string $content ): string { if ( is_admin() || '' === $content || ! is_singular() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return $content; } $post_id = get_the_ID(); if ( $post_id <= 0 || $post_id !== get_queried_object_id() ) { return $content; } $options = Settings_Page::get(); $post_type = get_post_type( $post_id ); $type_object = $post_type ? get_post_type_object( $post_type ) : null; if ( empty( $options['video_disclosure_enabled'] ) || ! $type_object || ! $type_object->public || ! Post_Type_Scope::allows( $post_id, $options ) ) { return $content; } if ( class_exists( '\DOMDocument' ) ) { return $this->augment_dom( $content, $post_id, $options ); } return $this->augment_without_dom( $content, $post_id, $options ); } /** @param array $options */ private function augment_dom( string $content, int $post_id, array $options ): string { $document = new \DOMDocument( '1.0', 'UTF-8' ); $previous = libxml_use_internal_errors( true ); $root_id = 'aiviso-video-root'; $loaded = $document->loadHTML( '
' . $content . '
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); $root = $document->getElementById( $root_id ); if ( ! $loaded || ! $root ) { return $content; } $videos = array(); foreach ( $root->getElementsByTagName( 'video' ) as $video ) { $videos[] = $video; } foreach ( $videos as $video ) { if ( ! $video instanceof \DOMElement || $video->hasAttribute( 'data-aiviso-ai-generated' ) || $this->has_processed_ancestor( $video ) ) { continue; } $attachment_id = $this->resolve_attachment_id( $video ); if ( ! AI_State::is_ai_generated( $attachment_id ) || 0 !== strpos( (string) get_post_mime_type( $attachment_id ), 'video/' ) ) { continue; } $badge = $this->badge->render_badge( $post_id, $attachment_id, $options ); if ( '' === $badge ) { continue; } $this->set_machine_attributes( $video, $attachment_id ); $host = $this->find_existing_host( $video, $root ); if ( $host ) { $this->add_class( $host, 'aiviso-video-host' ); $this->set_machine_attributes( $host, $attachment_id ); $this->append_badge( $document, $host, $badge ); continue; } $parent = $video->parentNode; if ( ! $parent ) { continue; } $wrapper = $document->createElement( 'span' ); $wrapper->setAttribute( 'class', 'aiviso-video-host aiviso-video-wrapper' ); $this->set_machine_attributes( $wrapper, $attachment_id ); $parent->replaceChild( $wrapper, $video ); $wrapper->appendChild( $video ); $this->append_badge( $document, $wrapper, $badge ); } $html = ''; foreach ( $root->childNodes as $child ) { $html .= $document->saveHTML( $child ); } return $html; } private function resolve_attachment_id( \DOMElement $video ): int { foreach ( array( 'data-attachment-id', 'data-id' ) as $attribute ) { $value = $video->getAttribute( $attribute ); if ( preg_match( '/^[0-9]+$/', $value ) ) { return absint( $value ); } } if ( preg_match( '/(?:^|\s)(?:wp-video|attachment)-([0-9]+)(?:\s|$)/', $video->getAttribute( 'class' ), $matches ) ) { return absint( $matches[1] ); } $urls = array( $video->getAttribute( 'src' ) ); foreach ( $video->getElementsByTagName( 'source' ) as $source ) { if ( $source instanceof \DOMElement ) { $urls[] = $source->getAttribute( 'src' ); } } foreach ( $urls as $url ) { if ( '' !== trim( $url ) ) { $attachment_id = $this->badge->attachment_id_from_local_url( html_entity_decode( trim( $url ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); if ( $attachment_id > 0 ) { return $attachment_id; } } } return 0; } private function find_existing_host( \DOMElement $video, \DOMElement $root ): ?\DOMElement { for ( $node = $video->parentNode; $node instanceof \DOMElement && $node !== $root; $node = $node->parentNode ) { $classes = preg_split( '/\s+/', trim( $node->getAttribute( 'class' ) ) ) ?: array(); if ( 'figure' === strtolower( $node->tagName ) || in_array( 'wp-video', $classes, true ) || in_array( 'wp-block-video', $classes, true ) ) { return $node; } } return null; } private function has_processed_ancestor( \DOMElement $video ): bool { for ( $node = $video->parentNode; $node instanceof \DOMElement; $node = $node->parentNode ) { if ( in_array( 'aiviso-video-host', preg_split( '/\s+/', trim( $node->getAttribute( 'class' ) ) ) ?: array(), true ) ) { return true; } } return false; } private function add_class( \DOMElement $element, string $class ): void { $classes = preg_split( '/\s+/', trim( $element->getAttribute( 'class' ) ) ) ?: array(); if ( ! in_array( $class, $classes, true ) ) { $classes[] = $class; $element->setAttribute( 'class', trim( implode( ' ', $classes ) ) ); } } private function set_machine_attributes( \DOMElement $element, int $attachment_id ): void { $element->setAttribute( 'data-aiviso-ai-generated', 'true' ); $element->setAttribute( 'data-aiviso-type', AI_State::get_type( $attachment_id ) ); $source_type = Digital_Source_Type::get( $attachment_id ); if ( '' !== $source_type ) { $element->setAttribute( 'data-aiviso-digital-source-type', $source_type ); } } private function append_badge( \DOMDocument $document, \DOMElement $host, string $badge ): void { if ( false !== strpos( $document->saveHTML( $host ), 'aiviso-badge' ) ) { return; } $fragment = $document->createDocumentFragment(); $previous = libxml_use_internal_errors( true ); $appended = $fragment->appendXML( $badge ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); if ( $appended ) { $host->appendChild( $fragment ); return; } $fallback = new \DOMDocument( '1.0', 'UTF-8' ); $previous = libxml_use_internal_errors( true ); $loaded = $fallback->loadHTML( '
' . $badge . '
', LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD ); libxml_clear_errors(); libxml_use_internal_errors( $previous ); $container = $fallback->getElementById( 'aiviso-video-badge-fragment' ); if ( ! $loaded || ! $container ) { return; } foreach ( iterator_to_array( $container->childNodes ) as $child ) { $host->appendChild( $document->importNode( $child, true ) ); } } /** * Conservative fallback for hosts without DOMDocument. It processes complete * video elements and leaves markup untouched unless a local attachment resolves. * * @param array $options */ private function augment_without_dom( string $content, int $post_id, array $options ): string { return (string) preg_replace_callback( '~]*)>(.*?)~is', function ( array $matches ) use ( $post_id, $options ): string { $video = (string) $matches[0]; if ( false !== stripos( $video, 'data-aiviso-ai-generated' ) ) { return $video; } $attachment_id = 0; if ( preg_match_all( '/\bsrc\s*=\s*(["\'])(.*?)\1/is', $video, $sources ) ) { foreach ( $sources[2] as $source ) { $attachment_id = $this->badge->attachment_id_from_local_url( html_entity_decode( trim( (string) $source ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); if ( $attachment_id > 0 ) { break; } } } if ( ! AI_State::is_ai_generated( $attachment_id ) || 0 !== strpos( (string) get_post_mime_type( $attachment_id ), 'video/' ) ) { return $video; } $badge = $this->badge->render_badge( $post_id, $attachment_id, $options ); if ( '' === $badge ) { return $video; } $video = preg_replace( '/' . $video . $badge . ''; }, $content ); } }