$results What the product's generation returned. * @return void */ public function save( int $job_id, int $product_id, string $tool, array $results ): void { if ( ! $this->holdsGeneratedContent( $results ) ) { return; } $row_id = $this->rowId( $job_id, $product_id ); $kept = $row_id > 0 && set_transient( self::key( $job_id, $product_id ), array( 'row_id' => $row_id, 'tool' => $tool, 'results' => $results, ), StallTracker::TTL_SECONDS ); if ( ! $kept ) { Logger::warn( 'automation', 'Paid answer could not be kept', array( 'job_id' => $job_id, 'product_id' => $product_id ) ); } } /** * The answer kept for this product's row and tool. An answer kept for * another row or tool is deleted. * * @param int $job_id Job ID. * @param int $product_id Product ID. * @param string $tool The job's tool type. * @return array|null Null when nothing usable is kept. */ public function take( int $job_id, int $product_id, string $tool ): ?array { $kept = get_transient( self::key( $job_id, $product_id ) ); if ( false === $kept ) { return null; } if ( is_array( $kept ) && isset( $kept['row_id'], $kept['tool'], $kept['results'] ) && is_array( $kept['results'] ) && $tool === $kept['tool'] && (int) $kept['row_id'] === $this->rowId( $job_id, $product_id ) ) { return $kept['results']; } $this->forget( $job_id, $product_id ); return null; } /** * Delete the product's kept answer. * * @param int $job_id Job ID. * @param int $product_id Product ID. * @return void */ public function forget( int $job_id, int $product_id ): void { delete_transient( self::key( $job_id, $product_id ) ); } /** * Whether at least one item is neither a provider error nor a failed * content row, mirroring what JobExecutor::processResults() sets aside. * * @param array $results Generation results. * @return bool */ private function holdsGeneratedContent( array $results ): bool { foreach ( $results as $item ) { if ( is_array( $item ) && empty( $item['api_error'] ) && empty( $item['error'] ) ) { return true; } } return false; } /** * @param int $job_id Job ID. * @param int $product_id Product ID. * @return int Zero when the product has no row. */ private function rowId( int $job_id, int $product_id ): int { global $wpdb; $table = $wpdb->prefix . 'aipt_job_products'; // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from $wpdb->prefix; values prepared; plugin-owned table. $id = $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$table} WHERE job_id = %d AND product_id = %d", $job_id, $product_id ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return max( 0, (int) $id ); } }