|null */ public function update( int $attachment_id, array $data ): ?array { $post = get_post( $attachment_id ); if ( ! $post || 'attachment' !== $post->post_type ) { return null; } $settings = get_option( 'frkt_settings', array() ); $default = Text::sanitize_line( $settings['languages']['default'] ?? 'en_US' ); $locale = Text::sanitize_line( $data['locale'] ?? $default ); $is_default = $locale === '' || $locale === $default; if ( $is_default ) { $update = array( 'ID' => $attachment_id ); if ( array_key_exists( 'title', $data ) ) { $update['post_title'] = Text::sanitize_line( $data['title'] ); } if ( array_key_exists( 'caption', $data ) ) { $update['post_excerpt'] = Text::sanitize_multiline( $data['caption'] ); } if ( array_key_exists( 'description', $data ) ) { $update['post_content'] = Text::sanitize_multiline( $data['description'] ); } if ( count( $update ) > 1 ) { $result = wp_update_post( wp_slash( $update ), true ); if ( is_wp_error( $result ) ) { throw new \RuntimeException( esc_html( $result->get_error_message() ) ); } } if ( array_key_exists( 'altText', $data ) ) { update_post_meta( $attachment_id, '_wp_attachment_image_alt', Text::sanitize_line( $data['altText'] ) ); } } $repo = new AssetRepository(); $asset_id = ( new AssetIndexer() )->index_now( $attachment_id ); $this->upsert_locale( $asset_id, $locale !== '' ? $locale : $default, array( 'alt_text' => array_key_exists( 'altText', $data ) ? Text::sanitize_line( $data['altText'] ) : null, 'title' => array_key_exists( 'title', $data ) ? Text::sanitize_line( $data['title'] ) : null, 'caption' => array_key_exists( 'caption', $data ) ? Text::sanitize_multiline( $data['caption'] ) : null, 'description' => array_key_exists( 'description', $data ) ? Text::sanitize_multiline( $data['description'] ) : null, ) ); $repo->refresh_search_document( $attachment_id ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- frkt is the plugin prefix. do_action( 'frkt_asset_updated', $attachment_id, $data ); return $repo->find_by_attachment( $attachment_id ); } /** * @param array{alt_text: ?string, title: ?string, caption: ?string, description: ?string} $fields */ private function upsert_locale( int $asset_id, string $locale, array $fields ): void { global $wpdb; $table = TableNames::assert_plugin_table( TableNames::asset_locales() ); $now = current_time( 'mysql' ); $existing = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE asset_id = %d AND locale = %s', $table, $asset_id, $locale ), ARRAY_A ); $row = array( 'asset_id' => $asset_id, 'locale' => $locale, 'source' => 'manual', 'updated_at' => $now, ); foreach ( array( 'alt_text', 'title', 'caption', 'description' ) as $key ) { if ( $fields[ $key ] !== null ) { $row[ $key ] = $fields[ $key ]; } elseif ( $existing ) { $row[ $key ] = $existing[ $key ]; } else { $row[ $key ] = ''; } } if ( $existing ) { $wpdb->update( $table, $row, array( 'id' => (int) $existing['id'] ) ); } else { $row['created_at'] = $now; $wpdb->insert( $table, $row ); } } }