store = $store ?? new ContentReviewStore(); $this->applier_for = $applier_for ?? array( ContentReviewApplierFactory::class, 'forTool' ); } /** * Approve a product's queued proposal with the values the reviewer submitted. * * @param int $product_id Product ID. * @param string $tool Review tool slug. * @param array $fields Field type => submitted value and stage() options. * @param string[] $staged Field types generation staged for this product. * @param string[] $known Every field type this tool stages for a product. * @return array {success, message, code?, reason?, review_fields?, applied_free_defaults?} */ public function approve( int $product_id, string $tool, array $fields, array $staged, array $known ): array { $denied = $this->denial(); if ( null !== $denied ) { return $denied; } $targets = $this->targets( $product_id, $tool, $known ); foreach ( $targets as $target ) { if ( $target['busy'] ) { return $this->inProgress(); } } foreach ( array_intersect( $staged, $known ) as $field_type ) { if ( empty( $targets[ $field_type ]['pending_ids'] ) ) { return array( 'success' => false, 'code' => self::CODE_GONE, 'message' => __( 'This result is no longer in the review queue. It was already approved or rejected in Content Bank.', 'ai-product-tools' ), ); } } foreach ( $fields as $field_type => $field ) { if ( ! isset( $targets[ $field_type ] ) || ! is_array( $field ) || ! isset( $field['value'] ) || ! is_string( $field['value'] ) ) { continue; } if ( empty( $targets[ $field_type ]['pending_ids'] ) ) { continue; } $row = $this->store->get( $targets[ $field_type ]['pending_ids'][0] ); if ( null === $row ) { continue; } $staged_token = (string) ( $row['staged_token'] ?? '' ); if ( '' === $staged_token ) { continue; } $current_token = $this->store->currentLiveToken( (int) ( $row['product_id'] ?? $product_id ), (string) ( $row['field_type'] ?? $field_type ), (int) ( $row['subject_id'] ?? 0 ) ); if ( hash_equals( $current_token, $staged_token ) ) { continue; } return $this->targetChanged( $targets ); } $ids = array(); foreach ( $fields as $field_type => $field ) { if ( ! isset( $targets[ $field_type ] ) || ! is_array( $field ) || ! isset( $field['value'] ) || ! is_string( $field['value'] ) ) { continue; } $opts = isset( $field['opts'] ) && is_array( $field['opts'] ) ? $field['opts'] : array(); if ( ! empty( $targets[ $field_type ]['pending_ids'] ) ) { $id = $targets[ $field_type ]['pending_ids'][0]; if ( ! $this->store->restagePending( $id, $field['value'], $opts ) ) { return $this->inProgress(); } } else { $opts['variant_index'] = 0; $id = $this->store->stage( $product_id, $tool, $field_type, 0, $field['value'], $opts ); if ( null === $id ) { return $this->inProgress(); } } $ids[ $field_type ] = (int) $id; } if ( empty( $ids ) ) { return array( 'success' => false, 'code' => self::CODE_FAILED, 'message' => __( 'There is nothing to apply for this product.', 'ai-product-tools' ), ); } $applier = call_user_func( $this->applier_for, $tool ); $applied_free_defaults = array(); $applied_field_types = array(); foreach ( $ids as $field_type => $id ) { if ( ! $this->store->approve( $id, $applier ) ) { return $this->approvalFailure( $product_id, $tool, $known, $applied_field_types ); } $applied_field_types[] = $field_type; if ( $applier instanceof ReportsAppliedFreeDefaults ) { $applied_free_defaults = array_merge( $applied_free_defaults, $applier->appliedFreeDefaults() ); } } foreach ( $targets as $field_type => $target ) { foreach ( $target['pending_ids'] as $pending_id ) { if ( isset( $ids[ $field_type ] ) && $ids[ $field_type ] === $pending_id ) { continue; } $this->store->reject( $pending_id ); } } return array( 'success' => true, 'message' => '', 'applied_free_defaults' => $applied_free_defaults, ); } /** * Remove a product's pending proposals for a tool. * * @param int $product_id Product ID. * @param string $tool Review tool slug. * @param string[] $known Every field type this tool stages for a product. * @return array {success, message, code?, reason?} */ public function reject( int $product_id, string $tool, array $known ): array { $denied = $this->denial(); if ( null !== $denied ) { return $denied; } $targets = $this->targets( $product_id, $tool, $known ); foreach ( $targets as $target ) { if ( $target['busy'] ) { return $this->inProgress(); } } foreach ( $targets as $target ) { foreach ( $target['pending_ids'] as $pending_id ) { if ( ! $this->store->reject( $pending_id ) ) { return $this->inProgress(); } } } return array( 'success' => true, 'message' => '', ); } /** * The review gate's denial, or null when review actions are allowed. * * @return array|null */ private function denial(): ?array { if ( apply_filters( 'aipt_content_review_enabled', false ) ) { return null; } $reason = PlanDenial::reason( 'contentReview', PlanDenial::PLAN_BUSINESS ); return array( 'success' => false, 'code' => self::CODE_DENIED, 'reason' => $reason, 'message' => PlanDenial::message( $reason, __( 'Content review actions require a Business plan.', 'ai-product-tools' ) ), ); } /** * @return array */ private function inProgress(): array { return array( 'success' => false, 'code' => self::CODE_IN_PROGRESS, 'message' => __( 'A previous approval for this product is still in progress. Try again shortly.', 'ai-product-tools' ), ); } /** * Outcome when a target's live value changed after its proposal was * staged. Nothing was restaged, staged or approved: a stale submission * must never silently overwrite whatever changed the product since this * result was generated. * * @param array $targets Current queue state of each review target (see targets()). * @return array */ private function targetChanged( array $targets ): array { return array( 'success' => false, 'code' => self::CODE_TARGET_CHANGED, 'message' => __( 'This product changed after this result was generated, so nothing was applied. Generate it again to review the current content.', 'ai-product-tools' ), 'review_fields' => array_keys( array_filter( $targets, static function ( array $target ): bool { return $target['busy'] || ! empty( $target['pending_ids'] ); } ) ), ); } /** * Outcome of an approval the applier refused or could not complete. * * @param int $product_id Product ID. * @param string $tool Review tool slug. * @param string[] $known Every field type this tool stages for a product. * @param string[] $applied_field_types Field types already committed (product write landed, * review row gone) before the field that failed. * @return array */ private function approvalFailure( int $product_id, string $tool, array $known, array $applied_field_types = array() ): array { $refusal = $this->store->lastRefusal(); $has_reason = is_array( $refusal ) && '' !== (string) ( $refusal['reason'] ?? '' ) && ! in_array( (string) $refusal['reason'], ContentReviewApplyRefusedException::STRUCTURAL_REASONS, true ); if ( $has_reason && '' !== trim( (string) ( $refusal['message'] ?? '' ) ) ) { $message = (string) $refusal['message']; } elseif ( ! empty( $applied_field_types ) ) { $message = sprintf( /* translators: %s: comma-separated list of field types already applied (e.g. "seo_meta_title, seo_meta_description") */ __( '%s already applied and will not be resubmitted. The rest of this result could not be applied. Check Content Bank for details.', 'ai-product-tools' ), implode( ', ', $applied_field_types ) ); } else { $message = __( 'This result could not be applied. Check Content Bank for details.', 'ai-product-tools' ); } $outcome = array( 'success' => false, 'code' => self::CODE_FAILED, 'message' => $message, 'review_fields' => array_keys( array_filter( $this->targets( $product_id, $tool, $known ), static function ( array $target ): bool { return $target['busy'] || ! empty( $target['pending_ids'] ); } ) ), ); if ( ! empty( $applied_field_types ) ) { $outcome['applied_fields'] = $applied_field_types; } if ( $has_reason ) { $outcome['reason'] = (string) $refusal['reason']; } return $outcome; } /** * Current queue state of each review target. * * @param int $product_id Product ID. * @param string $tool Review tool slug. * @param string[] $known Field types. * @return array */ private function targets( int $product_id, string $tool, array $known ): array { $targets = array(); foreach ( $known as $field_type ) { $target = array( 'pending_ids' => array(), 'busy' => false, ); foreach ( $this->store->findTargetRows( $product_id, $tool, $field_type ) as $row ) { if ( 'pending' !== $row['status'] ) { $target['busy'] = true; continue; } $target['pending_ids'][] = (int) $row['id']; } $targets[ $field_type ] = $target; } return $targets; } }