AIDisclara Audit screen and CSV export. * * @package Aiviso */ namespace Aiviso\Admin; use Aiviso\Metadata\AI_State; use Aiviso\Metadata\Digital_Source_Type; use Aiviso\Metadata\Disclosure_Details; use Aiviso\Multilingual\Language_Manager; defined( 'ABSPATH' ) || exit; final class Audit_Page { private const SLUG = 'aiviso-audit'; public function register(): void { add_action( 'admin_menu', array( $this, 'menu' ) ); add_action( 'admin_post_aiviso_audit_bulk', array( $this, 'bulk' ) ); add_action( 'admin_post_aiviso_audit_export', array( $this, 'export' ) ); } private function capability(): string { return (string) apply_filters( 'aiviso_audit_capability', 'upload_files' ); } public function menu(): void { add_media_page( __( 'AIDisclara Audit', 'aiviso-ai-image-disclosure' ), __( 'AIDisclara Audit', 'aiviso-ai-image-disclosure' ), $this->capability(), self::SLUG, array( $this, 'page' ) ); } private function state(): string { $value = isset( $_REQUEST['aiviso_audit_state'] ) && is_scalar( $_REQUEST['aiviso_audit_state'] ) ? sanitize_key( (string) wp_unslash( $_REQUEST['aiviso_audit_state'] ) ) : 'all'; return in_array( $value, array( 'all', 'marked', 'unmarked', 'generated', 'modified', 'custom', 'missing_source', 'missing_translations' ), true ) ? $value : 'all'; } /** @return array */ private function query_args( string $state, int $page = 1, int $per_page = 20 ): array { $args = array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'post_mime_type' => 'image', 'posts_per_page' => $per_page, 'paged' => $page, 'orderby' => 'ID', 'order' => 'DESC', 'no_found_rows' => false, 'update_post_meta_cache' => true ); if ( 'missing_translations' === $state && ! Language_Manager::is_multilingual() ) { $args['post__in'] = array( 0 ); return $args; } if ( 'unmarked' === $state ) { $args['meta_query'] = array( array( 'key' => AI_State::META_KEY, 'compare' => 'NOT EXISTS' ) ); } elseif ( 'marked' === $state ) { $args['meta_query'] = array( array( 'key' => AI_State::META_KEY, 'value' => '1' ) ); } elseif ( in_array( $state, array( 'generated', 'modified', 'custom' ), true ) ) { $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => AI_State::META_KEY, 'value' => '1' ), array( 'key' => AI_State::TYPE_META_KEY, 'value' => $state ) ); } elseif ( 'missing_source' === $state ) { $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => AI_State::META_KEY, 'value' => '1' ), array( 'key' => AI_State::TYPE_META_KEY, 'value' => 'custom' ), array( 'relation' => 'OR', array( 'key' => Digital_Source_Type::META_KEY, 'compare' => 'NOT EXISTS' ), array( 'key' => Digital_Source_Type::META_KEY, 'value' => '' ) ) ); } elseif ( 'missing_translations' === $state ) { $missing = array( 'relation' => 'OR', array( 'key' => AI_State::CUSTOM_TEXTS_META_KEY, 'compare' => 'NOT EXISTS' ) ); foreach ( Language_Manager::languages() as $locale => $language ) { $missing[] = array( 'key' => AI_State::CUSTOM_TEXTS_META_KEY, 'value' => '"' . $locale . '"', 'compare' => 'NOT LIKE' ); } $args['meta_query'] = array( 'relation' => 'AND', array( 'key' => AI_State::META_KEY, 'value' => '1' ), array( 'key' => AI_State::TYPE_META_KEY, 'value' => 'custom' ), $missing ); } return $args; } public function page(): void { if ( ! current_user_can( $this->capability() ) ) { return; } $state = $this->state(); $page = max( 1, absint( $_GET['paged'] ?? 1 ) ); $per_page = 20; $query = new \WP_Query( $this->query_args( $state, $page, $per_page ) ); $counts = $this->counts(); $base = admin_url( 'upload.php?page=' . self::SLUG ); $states = array( 'all' => __( 'All', 'aiviso-ai-image-disclosure' ), 'marked' => __( 'Marked', 'aiviso-ai-image-disclosure' ), 'unmarked' => __( 'Unmarked', 'aiviso-ai-image-disclosure' ), 'generated' => __( 'AI-generated', 'aiviso-ai-image-disclosure' ), 'modified' => __( 'AI-modified', 'aiviso-ai-image-disclosure' ), 'custom' => __( 'Custom', 'aiviso-ai-image-disclosure' ), 'missing_source' => __( 'Missing digital source type', 'aiviso-ai-image-disclosure' ), 'missing_translations' => __( 'Missing custom translations', 'aiviso-ai-image-disclosure' ) ); echo '

' . esc_html__( 'AIDisclara Audit', 'aiviso-ai-image-disclosure' ) . '

'; foreach ( $counts as $name => $count ) { echo '
' . esc_html( (string) $count ) . '' . esc_html( $name ) . '
'; } echo '

'; echo '
'; wp_nonce_field( 'aiviso_audit_bulk' ); echo ' '; foreach ( $query->posts as $attachment ) { $this->row( $attachment ); } echo '
' . esc_html__( 'Image', 'aiviso-ai-image-disclosure' ) . '' . esc_html__( 'Disclosure type', 'aiviso-ai-image-disclosure' ) . '' . esc_html__( 'Digital source type', 'aiviso-ai-image-disclosure' ) . '' . esc_html__( 'AI tool', 'aiviso-ai-image-disclosure' ) . '' . esc_html__( 'AI model', 'aiviso-ai-image-disclosure' ) . '' . esc_html__( 'Multilingual status', 'aiviso-ai-image-disclosure' ) . '' . esc_html__( 'Last disclosure update', 'aiviso-ai-image-disclosure' ) . '
'; $export = wp_nonce_url( add_query_arg( array( 'action' => 'aiviso_audit_export', 'aiviso_audit_state' => $state ), admin_url( 'admin-post.php' ) ), 'aiviso_audit_export' ); echo '

' . esc_html__( 'Export current filter as CSV', 'aiviso-ai-image-disclosure' ) . '

'; echo paginate_links( array( 'base' => add_query_arg( array( 'paged' => '%#%', 'aiviso_audit_state' => $state ), $base ), 'format' => '', 'current' => $page, 'total' => max( 1, (int) $query->max_num_pages ) ) ); echo '
'; } private function row( \WP_Post $post ): void { $id = $post->ID; $type = AI_State::get_type( $id ); $source = Digital_Source_Type::get( $id ); $translation = $this->translation_status( $id, $type ); echo '' . wp_get_attachment_image( $id, array( 60, 60 ) ) . ' ' . esc_html( get_the_title( $id ) ) . '' . esc_html( $type ?: __( 'Unmarked', 'aiviso-ai-image-disclosure' ) ) . '' . esc_html( $source ) . '' . esc_html( (string) get_post_meta( $id, Disclosure_Details::TOOL_META_KEY, true ) ) . '' . esc_html( (string) get_post_meta( $id, Disclosure_Details::MODEL_META_KEY, true ) ) . '' . esc_html( $translation ) . '' . esc_html( get_post_modified_time( 'Y-m-d H:i', true, $id ) ) . ''; } private function translation_status( int $id, string $type ): string { if ( 'custom' !== $type || ! Language_Manager::is_multilingual() ) { return '—'; } $texts = AI_State::get_custom_texts( $id ); foreach ( Language_Manager::languages() as $locale => $language ) { if ( ! array_key_exists( $locale, $texts ) ) { return __( 'Missing', 'aiviso-ai-image-disclosure' ); } } return __( 'Complete', 'aiviso-ai-image-disclosure' ); } /** @return array */ private function counts(): array { $all = new \WP_Query( $this->query_args( 'all', 1, 1 ) ); $result = array( __( 'Total image attachments', 'aiviso-ai-image-disclosure' ) => (int) $all->found_posts ); foreach ( array( 'marked' => 'Marked', 'unmarked' => 'Unmarked', 'generated' => 'AI-generated', 'modified' => 'AI-modified', 'custom' => 'Custom', 'missing_source' => 'Missing digital source type', 'missing_translations' => 'Missing custom translations' ) as $state => $label ) { $q = new \WP_Query( $this->query_args( $state, 1, 1 ) ); $result[ __( $label, 'aiviso-ai-image-disclosure' ) ] = (int) $q->found_posts; } return $result; } public function bulk(): void { if ( ! current_user_can( $this->capability() ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'aiviso-ai-image-disclosure' ) ); } check_admin_referer( 'aiviso_audit_bulk' ); $action = isset( $_POST['aiviso_bulk_action'] ) && is_scalar( $_POST['aiviso_bulk_action'] ) ? sanitize_key( (string) wp_unslash( $_POST['aiviso_bulk_action'] ) ) : ''; $ids = isset( $_POST['attachment_ids'] ) && is_array( $_POST['attachment_ids'] ) ? $_POST['attachment_ids'] : array(); $result = Disclosure_Bulk_Actions::process( $action, $ids ); wp_safe_redirect( add_query_arg( array( 'page' => self::SLUG, 'aiviso_changed' => $result['changed'], 'aiviso_skipped' => $result['skipped'] ), admin_url( 'upload.php' ) ) ); exit; } public function export(): void { if ( ! current_user_can( $this->capability() ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'aiviso-ai-image-disclosure' ) ); } check_admin_referer( 'aiviso_audit_export' ); $state = $this->state(); nocache_headers(); header( 'Content-Type: text/csv; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=aiviso-audit.csv' ); $out = fopen( 'php://output', 'w' ); fputcsv( $out, array( 'attachment_id', 'title', 'disclosure_type', 'digital_source_type', 'ai_tool', 'ai_model', 'source_url' ) ); $page = 1; do { $query = new \WP_Query( $this->query_args( $state, $page, 100 ) ); foreach ( $query->posts as $post ) { $row = array( $post->ID, get_the_title( $post ), AI_State::get_type( $post->ID ), Digital_Source_Type::get( $post->ID ), get_post_meta( $post->ID, Disclosure_Details::TOOL_META_KEY, true ), get_post_meta( $post->ID, Disclosure_Details::MODEL_META_KEY, true ), get_post_meta( $post->ID, Disclosure_Details::SOURCE_URL_META_KEY, true ) ); fputcsv( $out, array_map( array( $this, 'csv_value' ), $row ) ); } ++$page; } while ( $page <= (int) $query->max_num_pages ); fclose( $out ); exit; } /** @param mixed $value */ public function csv_value( $value ): string { $value = is_scalar( $value ) ? (string) $value : ''; return preg_match( '/^[=+\-@]/', $value ) ? "'" . $value : $value; } }