productId = $productId; $this->fieldType = $fieldType; $this->subjectId = $subjectId; $this->isAttachment = $isAttachment; $this->isTerm = $isTerm; $this->taxonomy = $taxonomy; } /** * Create from a snapshot row array. * * The ONE place that decides product-vs-attachment-vs-term: subject_type * === 'term' is checked FIRST (a term row's subject_id is the term id, * never an attachment id); then the existing image_alt-with-positive- * subject_id sniff for attachment-domain; everything else is product-domain. * * @param array $row Must contain product_id, field_type, and optionally subject_id, subject_type, taxonomy. * @return self */ public static function fromRow( array $row ): self { $field_type = isset( $row['field_type'] ) ? (string) $row['field_type'] : ''; $subject_id = isset( $row['subject_id'] ) ? (int) $row['subject_id'] : 0; $subject_type = isset( $row['subject_type'] ) ? (string) $row['subject_type'] : 'product'; if ( 'term' === $subject_type ) { return new self( isset( $row['product_id'] ) ? (int) $row['product_id'] : 0, $field_type, $subject_id, false, true, isset( $row['taxonomy'] ) ? (string) $row['taxonomy'] : '' ); } return new self( isset( $row['product_id'] ) ? (int) $row['product_id'] : 0, $field_type, $subject_id, 'image_alt' === $field_type && $subject_id > 0 ); } /** * The id whose post_modified + revision drives the token. * * For image_alt rows this is the subject_id (attachment id); for every * other field type (including a term row, which carries no post at all) * it is the product_id — 0 for a term row, since terms have no owning * product. Term callers must use {@see termId()} instead. * * @return int */ public function postId(): int { return $this->isAttachment ? $this->subjectId : $this->productId; } /** * The term id this subject identifies. Only meaningful when {@see isTerm()}. * * @return int */ public function termId(): int { return $this->subjectId; } /** * Compute the domain-correct optimistic-concurrency token. * * @param string $postModified The post's post_modified string (Y-m-d H:i:s). Ignored for a term subject, which has no post_modified equivalent. * @return string */ public function token( string $postModified = '' ): string { if ( $this->isTerm ) { return TermEditorConcurrencyToken::for_term_id( $this->subjectId ); } return ProductEditorConcurrencyToken::for_product_id( $this->postId(), $postModified ); } /** * Bump the editor revision(s) so the token changes after a restore. * * A term subject bumps ONLY its own term revision — there is no owning * product to also bump. The product revision is always bumped for a * product/attachment subject (meta/tag writes don't bump post_modified). * For image_alt, the attachment revision is also bumped because the * attachment token is independent of the product token. * * @return void */ public function bump(): void { if ( $this->isTerm ) { TermEditorConcurrencyToken::bump_revision( $this->subjectId ); return; } ProductEditorConcurrencyToken::bump_revision( $this->productId ); if ( $this->isAttachment && $this->subjectId > 0 ) { ProductEditorConcurrencyToken::bump_revision( $this->subjectId ); } } /** * A stable map key distinguishing product-domain, attachment-domain, and * term-domain even when the numeric ids collide. The term key includes * the taxonomy so per-request staging never mixes a term with a product * (or a term in a different taxonomy) of the same numeric id. * * @return string "p:{product_id}", "a:{subject_id}", or "t:{taxonomy}:{subject_id}" */ public function key(): string { if ( $this->isTerm ) { return "t:{$this->taxonomy}:{$this->subjectId}"; } return $this->isAttachment ? "a:{$this->subjectId}" : "p:{$this->productId}"; } /** * Whether this subject is attachment-domain. * * @return bool */ public function isAttachment(): bool { return $this->isAttachment; } /** * Whether this subject is term-domain (a category/taxonomy description row). * * @since 2.8.0 * @return bool */ public function isTerm(): bool { return $this->isTerm; } /** * The taxonomy slug this term belongs to. Empty for a non-term subject. * * @since 2.8.0 * @return string */ public function taxonomy(): string { return $this->taxonomy; } }