get_image_id(); $ids = $this->product_attachment_ids( $product ); $items = array(); foreach ( $ids as $attachment_id ) { $url = \wp_get_attachment_image_url( $attachment_id, 'thumbnail' ); if ( false === $url ) { continue; } $alt = (string) \get_post_meta( $attachment_id, self::META_KEY, true ); $items[] = array( 'attachmentId' => $attachment_id, 'url' => $url, 'alt' => $alt, 'previousAlt' => $alt, 'isFeatured' => $attachment_id === $featured_id, ); } return $items; } /** * @internal BatchSaveService performs the per-attachment edit_post capability check before calling this adapter. */ public function write( \WC_Product $product, string $field_id, $value ): array { if ( 'altText' !== $field_id || ! is_array( $value ) ) { return array( 'saved' => false, 'message' => 'Alt text value must be an array.' ); } $allowed_ids = array_flip( $this->product_attachment_ids( $product ) ); $outcomes = array(); $persisted = false; $has_failure = false; foreach ( $value as $entry ) { if ( ! is_array( $entry ) || ! isset( $entry['attachmentId'] ) ) { continue; } $attachment_id = (int) $entry['attachmentId']; if ( ! isset( $allowed_ids[ $attachment_id ] ) ) { $outcomes[] = $this->attachment_outcome( $attachment_id, 'skipped', 'Attachment is not assigned to this product.' ); $has_failure = true; continue; } $outcome = $this->write_attachment_alt( $attachment_id, $entry ); $outcomes[] = $outcome; if ( in_array( $outcome['status'], array( 'saved', 'noop' ), true ) ) { $persisted = $persisted || 'saved' === $outcome['status']; continue; } $has_failure = true; } if ( empty( $outcomes ) ) { return array( 'saved' => false, 'message' => 'No valid alt text entries were supplied.', 'attachments' => array() ); } return array( 'saved' => ! $has_failure, 'partial' => $has_failure, 'persisted' => $persisted, 'changed' => $persisted, 'message' => $has_failure ? 'Some attachment alt text entries could not be saved.' : null, 'attachments' => $outcomes, ); } private function write_attachment_alt( int $attachment_id, array $entry ): array { $desired = \sanitize_text_field( isset( $entry['alt'] ) ? (string) $entry['alt'] : '' ); $current = (string) \get_post_meta( $attachment_id, self::META_KEY, true ); if ( $current === $desired ) { return $this->attachment_outcome( $attachment_id, 'noop', null, $current ); } if ( ! array_key_exists( 'previousAlt', $entry ) ) { return $this->attachment_outcome( $attachment_id, 'conflict', 'Alt text changed since this row was loaded. Refresh and retry.' ); } $previous = (string) $entry['previousAlt']; if ( $current !== $previous ) { return $this->attachment_outcome( $attachment_id, 'conflict', 'Alt text changed since this row was loaded. Refresh and retry.' ); } if ( $this->has_meta_row( $attachment_id ) ) { return $this->conditional_update( $attachment_id, $desired, $previous ); } return $this->locked_insert( $attachment_id, $desired, $previous ); } private function conditional_update( int $attachment_id, string $desired, string $previous ): array { global $wpdb; if ( ! isset( $wpdb ) ) { return $this->attachment_outcome( $attachment_id, 'failed', 'Database connection is unavailable.' ); } $sql = $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %s WHERE post_id = %d AND meta_key = %s AND meta_value = %s", $desired, $attachment_id, self::META_KEY, $previous ); $updated = $wpdb->query( $sql ); if ( false === $updated ) { return $this->attachment_outcome( $attachment_id, 'failed', 'Failed to update attachment alt text.' ); } if ( (int) $updated > 0 ) { \wp_cache_delete( $attachment_id, 'post_meta' ); return $this->attachment_outcome( $attachment_id, 'saved', null, $desired ); } return $this->attachment_outcome( $attachment_id, 'conflict', 'Alt text changed since this row was loaded. Refresh and retry.' ); } private function locked_insert( int $attachment_id, string $desired, string $previous ): array { global $wpdb; $release = $this->acquire_attachment_lock( $attachment_id ); if ( null === $release || false === $release ) { return $this->attachment_outcome( $attachment_id, 'failed', 'Could not acquire an attachment save lock. Please try again.' ); } try { $current = (string) \get_post_meta( $attachment_id, self::META_KEY, true ); if ( $current === $desired ) { return $this->attachment_outcome( $attachment_id, 'noop', null, $current ); } if ( $current !== $previous ) { return $this->attachment_outcome( $attachment_id, 'conflict', 'Alt text changed since this row was loaded. Refresh and retry.' ); } if ( $this->has_meta_row( $attachment_id ) ) { return $this->conditional_update( $attachment_id, $desired, $previous ); } $inserted = $wpdb->insert( $wpdb->postmeta, array( 'post_id' => $attachment_id, 'meta_key' => self::META_KEY, 'meta_value' => $desired, ), array( '%d', '%s', '%s' ) ); if ( false === $inserted ) { return $this->attachment_outcome( $attachment_id, 'failed', 'Failed to insert attachment alt text.' ); } \wp_cache_delete( $attachment_id, 'post_meta' ); return $this->attachment_outcome( $attachment_id, 'saved', null, $desired ); } finally { $release(); } } private function has_meta_row( int $attachment_id ): bool { global $wpdb; if ( ! isset( $wpdb ) ) { return false; } $wpdb->last_error = ''; $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s LIMIT 1", $attachment_id, self::META_KEY ) ); if ( null === $meta_id && '' !== (string) $wpdb->last_error ) { return true; } return null !== $meta_id; } private function acquire_attachment_lock( int $attachment_id ) { global $wpdb; if ( ! isset( $wpdb ) ) { return null; } $name = 'aipt_pe_alt_' . sha1( ( defined( 'DB_NAME' ) ? \DB_NAME : '' ) . ':' . $wpdb->prefix . ':' . ( function_exists( 'get_current_blog_id' ) ? (int) \get_current_blog_id() : 0 ) . ':' . $attachment_id ); $result = $wpdb->get_var( $wpdb->prepare( 'SELECT GET_LOCK(%s, %d)', $name, 2 ) ); if ( null === $result ) { return null; } if ( '1' !== (string) $result ) { return false; } return static function () use ( $wpdb, $name ): void { $wpdb->get_var( $wpdb->prepare( 'SELECT RELEASE_LOCK(%s)', $name ) ); }; } private function attachment_outcome( int $attachment_id, string $status, ?string $message = null, ?string $alt = null ): array { if ( 'saved' === $status ) { \AIPT\Api\ProductEditor\ProductEditorConcurrencyToken::bump_revision( $attachment_id ); } $outcome = array( 'attachmentId' => $attachment_id, 'status' => $status, ); if ( null !== $message && '' !== $message ) { $outcome['message'] = $message; } if ( null !== $alt && in_array( $status, array( 'saved', 'noop' ), true ) ) { $outcome['alt'] = $alt; } return $outcome; } private function product_attachment_ids( \WC_Product $product ): array { $ids = array(); $featured_id = (int) $product->get_image_id(); if ( $featured_id > 0 ) { $ids[] = $featured_id; } foreach ( (array) $product->get_gallery_image_ids() as $attachment_id ) { $attachment_id = (int) $attachment_id; if ( $attachment_id > 0 ) { $ids[] = $attachment_id; } } return array_values( array_unique( $ids ) ); } }