$fields Attachment fields. * @param \WP_Post $post Attachment post. * @return array */ public function add_field( array $fields, \WP_Post $post ): array { if ( ! wp_attachment_is_image( $post->ID ) || ! current_user_can( 'edit_post', $post->ID ) ) { return $fields; } $is_ai = AI_State::is_ai_generated( $post->ID ); $type = AI_State::get_type( $post->ID ); $type = '' !== $type ? $type : 'generated'; $custom_text_fields = $this->custom_text_fields( $post->ID, $type ); $fields['aiviso_ai_generated'] = array( 'label' => __( 'AI-generated image', 'aiviso-ai-image-disclosure' ), 'input' => 'html', 'html' => sprintf( '
%6$s

%12$s

%4$s

', (int) $post->ID, checked( $is_ai, true, false ), esc_html__( 'AI-generated image', 'aiviso-ai-image-disclosure' ), esc_html__( 'Mark this image if it was generated or substantially created using artificial intelligence.', 'aiviso-ai-image-disclosure' ), $is_ai ? '' : 'hidden', esc_html__( 'AI disclosure type', 'aiviso-ai-image-disclosure' ), checked( $type, 'generated', false ), checked( $type, 'modified', false ), esc_html__( 'AI-modified image', 'aiviso-ai-image-disclosure' ), checked( $type, 'custom', false ), esc_html__( 'Custom disclosure', 'aiviso-ai-image-disclosure' ), $custom_text_fields ), ); $fields['aiviso_disclosure_details'] = array( 'label' => __( 'Disclosure details', 'aiviso-ai-image-disclosure' ), 'input' => 'html', 'html' => $this->details_fields( $post->ID ), ); return $fields; } private function details_fields( int $attachment_id ): string { $values = array( 'ai_tool' => get_post_meta( $attachment_id, Disclosure_Details::TOOL_META_KEY, true ), 'ai_model' => get_post_meta( $attachment_id, Disclosure_Details::MODEL_META_KEY, true ), 'creation_date' => get_post_meta( $attachment_id, Disclosure_Details::DATE_META_KEY, true ), 'source_url' => get_post_meta( $attachment_id, Disclosure_Details::SOURCE_URL_META_KEY, true ), 'reviewed_by' => get_post_meta( $attachment_id, Disclosure_Details::REVIEWED_BY_META_KEY, true ), 'review_date' => get_post_meta( $attachment_id, Disclosure_Details::REVIEW_DATE_META_KEY, true ), 'public_note' => get_post_meta( $attachment_id, Disclosure_Details::PUBLIC_NOTE_META_KEY, true ), ); $labels = array( 'ai_tool' => __( 'AI tool', 'aiviso-ai-image-disclosure' ), 'ai_model' => __( 'AI model or version', 'aiviso-ai-image-disclosure' ), 'creation_date' => __( 'Creation or modification date', 'aiviso-ai-image-disclosure' ), 'source_url' => __( 'Source URL', 'aiviso-ai-image-disclosure' ), 'reviewed_by' => __( 'Reviewed by', 'aiviso-ai-image-disclosure' ), 'review_date' => __( 'Review date', 'aiviso-ai-image-disclosure' ), 'public_note' => __( 'Public disclosure note', 'aiviso-ai-image-disclosure' ) ); $html = '
'; foreach ( $labels as $key => $label ) { $type = in_array( $key, array( 'creation_date', 'review_date' ), true ) ? 'date' : 'text'; if ( 'public_note' === $key ) { if ( Language_Manager::is_multilingual() ) { $notes = Disclosure_Details::sanitize_notes( get_post_meta( $attachment_id, Disclosure_Details::PUBLIC_NOTES_META_KEY, true ) ); foreach ( Language_Manager::languages() as $locale => $language ) { $value = array_key_exists( $locale, $notes ) ? $notes[ $locale ] : ( $locale === Language_Manager::default_locale() ? (string) $values[ $key ] : '' ); $html .= '

'; } } else { $html .= '

'; } } else { $html .= '

'; } } $options = ''; $current = Digital_Source_Type::sanitize( get_post_meta( $attachment_id, Digital_Source_Type::META_KEY, true ) ); foreach ( Digital_Source_Type::choices() as $uri => $label ) { $options .= ''; } $html .= '

'; return $html; } private function custom_text_fields( int $attachment_id, string $type ): string { $languages = Language_Manager::languages(); $is_multilingual = count( $languages ) > 1; $custom_text = AI_State::get_custom_text( $attachment_id ); $custom_texts = AI_State::get_custom_texts( $attachment_id ); $hidden = 'custom' === $type ? '' : 'hidden'; $html = '
'; $html .= '

' . esc_html__( 'Custom disclosure text', 'aiviso-ai-image-disclosure' ) . '

'; if ( $is_multilingual ) { $default_locale = Language_Manager::default_locale(); foreach ( $languages as $locale => $language ) { $has_text = array_key_exists( $locale, $custom_texts ); $value = $has_text ? $custom_texts[ $locale ] : ( $locale === $default_locale ? $custom_text : '' ); $label = (string) $language['name']; if ( $locale === $default_locale ) { $label .= ' ' . __( '(default)', 'aiviso-ai-image-disclosure' ); } $html .= sprintf( '

', $attachment_id, esc_attr( $locale ), esc_html( $label ), esc_attr( $value ) ); } $description = __( 'Enter the badge text for this image. Leave a language empty to show the logo only.', 'aiviso-ai-image-disclosure' ); } else { $html .= sprintf( '

', $attachment_id, esc_html__( 'Custom disclosure text', 'aiviso-ai-image-disclosure' ), esc_attr( $custom_text ) ); $description = __( 'Enter the badge text for this image. Leave empty to show the logo only.', 'aiviso-ai-image-disclosure' ); } $html .= '

' . esc_html( $description ) . '

'; return $html; } /** * Saves through WordPress's authenticated attachment editing flow. * * @param array $post Attachment post data. * @param array $attachment Submitted attachment fields. * @return array */ public function save_field( array $post, array $attachment ): array { $attachment_id = isset( $post['ID'] ) ? absint( $post['ID'] ) : 0; if ( ! $attachment_id || ! wp_attachment_is_image( $attachment_id ) || ! current_user_can( 'edit_post', $attachment_id ) ) { return $post; } $type = isset( $attachment['aiviso_ai_type'] ) && is_scalar( $attachment['aiviso_ai_type'] ) ? (string) $attachment['aiviso_ai_type'] : 'generated'; $custom_text = AI_State::get_custom_text( $attachment_id ); $custom_texts = null; $languages = Language_Manager::languages(); if ( count( $languages ) > 1 ) { if ( isset( $attachment['aiviso_ai_custom_texts'] ) && is_array( $attachment['aiviso_ai_custom_texts'] ) ) { $custom_texts = AI_State::get_custom_texts( $attachment_id ); foreach ( $languages as $locale => $language ) { if ( array_key_exists( $locale, $attachment['aiviso_ai_custom_texts'] ) && is_scalar( $attachment['aiviso_ai_custom_texts'][ $locale ] ) ) { $custom_texts[ $locale ] = sanitize_text_field( (string) $attachment['aiviso_ai_custom_texts'][ $locale ] ); } } $default_locale = Language_Manager::default_locale(); if ( '' !== $default_locale && array_key_exists( $default_locale, $custom_texts ) ) { $custom_text = (string) $custom_texts[ $default_locale ]; } } } elseif ( isset( $attachment['aiviso_ai_custom_text'] ) && is_scalar( $attachment['aiviso_ai_custom_text'] ) ) { $custom_text = sanitize_text_field( (string) $attachment['aiviso_ai_custom_text'] ); } AI_State::update( $attachment_id, isset( $attachment['aiviso_ai_generated'] ) && '1' === (string) $attachment['aiviso_ai_generated'], $type, $custom_text, $custom_texts ); $map = array( 'aiviso_ai_tool' => array( Disclosure_Details::TOOL_META_KEY, array( Disclosure_Details::class, 'sanitize_text' ) ), 'aiviso_ai_model' => array( Disclosure_Details::MODEL_META_KEY, array( Disclosure_Details::class, 'sanitize_text' ) ), 'aiviso_creation_date' => array( Disclosure_Details::DATE_META_KEY, array( Disclosure_Details::class, 'sanitize_date' ) ), 'aiviso_source_url' => array( Disclosure_Details::SOURCE_URL_META_KEY, array( Disclosure_Details::class, 'sanitize_url' ) ), 'aiviso_reviewed_by' => array( Disclosure_Details::REVIEWED_BY_META_KEY, array( Disclosure_Details::class, 'sanitize_text' ) ), 'aiviso_review_date' => array( Disclosure_Details::REVIEW_DATE_META_KEY, array( Disclosure_Details::class, 'sanitize_date' ) ), 'aiviso_public_note' => array( Disclosure_Details::PUBLIC_NOTE_META_KEY, array( Disclosure_Details::class, 'sanitize_note' ) ), 'aiviso_digital_source_type' => array( Digital_Source_Type::META_KEY, array( Digital_Source_Type::class, 'sanitize' ) ), ); foreach ( $map as $input_key => $definition ) { if ( array_key_exists( $input_key, $attachment ) && is_scalar( $attachment[ $input_key ] ) ) { update_post_meta( $attachment_id, $definition[0], call_user_func( $definition[1], $attachment[ $input_key ] ) ); } } if ( isset( $attachment['aiviso_public_notes'] ) && is_array( $attachment['aiviso_public_notes'] ) ) { $notes = Disclosure_Details::sanitize_notes( $attachment['aiviso_public_notes'] ); update_post_meta( $attachment_id, Disclosure_Details::PUBLIC_NOTES_META_KEY, $notes ); $default_locale = Language_Manager::default_locale(); if ( '' !== $default_locale && array_key_exists( $default_locale, $notes ) ) { update_post_meta( $attachment_id, Disclosure_Details::PUBLIC_NOTE_META_KEY, $notes[ $default_locale ] ); } } return $post; } /** @param string[] $columns @return string[] */ public function add_column( array $columns ): array { $columns['aiviso_ai'] = __( 'AI', 'aiviso-ai-image-disclosure' ); return $columns; } public function render_column( string $column, int $attachment_id ): void { if ( 'aiviso_ai' !== $column || ! AI_State::is_ai_generated( $attachment_id ) ) { return; } $type = AI_State::get_type( $attachment_id ); if ( 'custom' === $type ) { $label = __( 'Marked with custom AI disclosure', 'aiviso-ai-image-disclosure' ); } elseif ( 'modified' === $type ) { $label = __( 'Marked as AI-modified', 'aiviso-ai-image-disclosure' ); } else { $label = __( 'Marked as AI-generated', 'aiviso-ai-image-disclosure' ); } echo '' . esc_html( $label ) . 'AI'; } public function enqueue_assets(): void { $css_path = 'assets/css/admin.css'; $css_file = AIVISO_PATH . $css_path; wp_enqueue_style( 'aiviso-admin', AIVISO_URL . $css_path, array(), is_readable( $css_file ) ? (string) filemtime( $css_file ) : AIVISO_VERSION ); $relative_path = 'assets/js/attachment-fields.js'; $file = AIVISO_PATH . $relative_path; wp_enqueue_script( 'aiviso-attachment-fields', AIVISO_URL . $relative_path, array(), is_readable( $file ) ? (string) filemtime( $file ) : AIVISO_VERSION, true ); } }