*/ private static array $cache = array(); public static function register_meta(): void { register_post_meta( 'attachment', self::META_KEY, array( 'type' => 'boolean', 'single' => true, 'show_in_rest' => true, 'sanitize_callback' => array( self::class, 'sanitize_rest_value' ), 'auth_callback' => array( self::class, 'can_edit_meta' ), ) ); register_post_meta( 'attachment', self::TYPE_META_KEY, array( 'type' => 'string', 'single' => true, 'show_in_rest' => array( 'schema' => array( 'type' => 'string', 'enum' => array( self::GENERATED_TYPE, self::MODIFIED_TYPE, self::CUSTOM_TYPE ), ), ), 'sanitize_callback' => array( self::class, 'sanitize_rest_type' ), 'auth_callback' => array( self::class, 'can_edit_meta' ), ) ); register_post_meta( 'attachment', self::CUSTOM_TEXT_META_KEY, array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'sanitize_callback' => array( self::class, 'sanitize_rest_text' ), 'auth_callback' => array( self::class, 'can_edit_meta' ), ) ); register_post_meta( 'attachment', self::CUSTOM_TEXTS_META_KEY, array( 'type' => 'object', 'single' => true, 'show_in_rest' => array( 'schema' => array( 'type' => 'object', 'properties' => array(), 'additionalProperties' => array( 'type' => 'string' ), ), ), 'sanitize_callback' => array( self::class, 'sanitize_rest_texts' ), 'auth_callback' => array( self::class, 'can_edit_meta' ), ) ); add_action( 'added_post_meta', array( self::class, 'clear_type_when_disabled' ), 10, 4 ); add_action( 'updated_post_meta', array( self::class, 'clear_type_when_disabled' ), 10, 4 ); } /** * @param mixed $value Submitted REST value. * @return bool */ public static function sanitize_rest_value( $value ): bool { return rest_sanitize_boolean( $value ); } /** * @param mixed $value Submitted REST value. * @return string */ public static function sanitize_rest_type( $value ): string { return self::normalize_type( $value ); } /** * @param mixed $value Submitted REST value. * @return string */ public static function sanitize_rest_text( $value ): string { return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : ''; } /** * @param mixed $value Submitted REST value. * @return array */ public static function sanitize_rest_texts( $value ): array { return self::normalize_texts( $value ); } /** * Allows mutation only by users who may edit the attachment. * * @param bool $allowed Existing result. * @param string $meta_key Meta key. * @param int $object_id Attachment ID. * @return bool */ public static function can_edit_meta( bool $allowed, string $meta_key, int $object_id ): bool { return current_user_can( 'edit_post', $object_id ); } public static function is_ai_generated( int $attachment_id ): bool { if ( $attachment_id <= 0 || ! wp_attachment_is_image( $attachment_id ) ) { return false; } if ( isset( self::$cache[ $attachment_id ] ) ) { return self::$cache[ $attachment_id ]; } $is_declared = '1' === (string) get_post_meta( $attachment_id, self::META_KEY, true ); self::$cache[ $attachment_id ] = (bool) apply_filters( 'aiviso_is_ai_generated', $is_declared, $attachment_id ); return self::$cache[ $attachment_id ]; } public static function get_type( int $attachment_id ): string { if ( ! self::is_ai_generated( $attachment_id ) ) { return ''; } $type = self::normalize_type( get_post_meta( $attachment_id, self::TYPE_META_KEY, true ) ); return '' !== $type ? $type : self::GENERATED_TYPE; } public static function get_custom_text( int $attachment_id ): string { $value = get_post_meta( $attachment_id, self::CUSTOM_TEXT_META_KEY, true ); return is_scalar( $value ) ? sanitize_text_field( (string) $value ) : ''; } /** @return array */ public static function get_custom_texts( int $attachment_id ): array { return self::normalize_texts( get_post_meta( $attachment_id, self::CUSTOM_TEXTS_META_KEY, true ) ); } public static function clear_cache( int $attachment_id ): void { unset( self::$cache[ $attachment_id ] ); } /** * @param array|null $custom_texts Custom text indexed by normalized locale. */ public static function update( int $attachment_id, bool $is_ai_generated, string $type = self::GENERATED_TYPE, string $custom_text = '', ?array $custom_texts = null ): void { if ( ! wp_attachment_is_image( $attachment_id ) ) { return; } if ( $is_ai_generated ) { $type = self::normalize_type( $type ) ?: self::GENERATED_TYPE; update_post_meta( $attachment_id, self::META_KEY, '1' ); update_post_meta( $attachment_id, self::TYPE_META_KEY, $type ); if ( self::CUSTOM_TYPE === $type ) { update_post_meta( $attachment_id, self::CUSTOM_TEXT_META_KEY, sanitize_text_field( $custom_text ) ); if ( null !== $custom_texts ) { update_post_meta( $attachment_id, self::CUSTOM_TEXTS_META_KEY, self::normalize_texts( $custom_texts ) ); } } } else { delete_post_meta( $attachment_id, self::META_KEY ); delete_post_meta( $attachment_id, self::TYPE_META_KEY ); delete_post_meta( $attachment_id, self::CUSTOM_TEXT_META_KEY ); delete_post_meta( $attachment_id, self::CUSTOM_TEXTS_META_KEY ); } self::$cache[ $attachment_id ] = $is_ai_generated; do_action( 'aiviso_disclosure_state_updated', $attachment_id, $is_ai_generated, $is_ai_generated ? self::get_type( $attachment_id ) : '' ); } /** * Keeps type data from remaining when the declaration is removed through REST. * * @param int $meta_id Meta ID. * @param int $object_id Attachment ID. * @param string $meta_key Updated meta key. * @param mixed $meta_value Updated meta value. */ public static function clear_type_when_disabled( int $meta_id, int $object_id, string $meta_key, $meta_value ): void { if ( self::META_KEY !== $meta_key || rest_sanitize_boolean( $meta_value ) ) { return; } delete_post_meta( $object_id, self::TYPE_META_KEY ); delete_post_meta( $object_id, self::CUSTOM_TEXT_META_KEY ); delete_post_meta( $object_id, self::CUSTOM_TEXTS_META_KEY ); self::$cache[ $object_id ] = false; do_action( 'aiviso_disclosure_state_updated', $object_id, false, '' ); } /** @param mixed $type */ private static function normalize_type( $type ): string { $type = is_scalar( $type ) ? sanitize_key( (string) $type ) : ''; return in_array( $type, array( self::GENERATED_TYPE, self::MODIFIED_TYPE, self::CUSTOM_TYPE ), true ) ? $type : ''; } /** @param mixed $texts @return array */ private static function normalize_texts( $texts ): array { if ( ! is_array( $texts ) ) { return array(); } $normalized = array(); foreach ( $texts as $locale => $text ) { $locale = is_scalar( $locale ) ? str_replace( '-', '_', trim( (string) $locale ) ) : ''; if ( ! preg_match( '/^([A-Za-z]{2,3})(?:_([A-Za-z0-9]{2,8}))?$/', $locale, $matches ) || ! is_scalar( $text ) ) { continue; } $key = strtolower( $matches[1] ) . ( isset( $matches[2] ) ? '_' . strtoupper( $matches[2] ) : '' ); $normalized[ $key ] = sanitize_text_field( (string) $text ); } return $normalized; } }