setMetaData( $prefix . $key, $value ); } } // A checkbox that is absent from the serialisation is unchecked. if ( ! array_key_exists( 'tax_enabled', $data ) && is_callable( [ $model, 'setMetaData' ] ) ) { $model->setMetaData( $prefix . 'tax_enabled', '0' ); } if ( ! empty( $data['items'] ) && is_array( $data['items'] ) ) { $items = []; foreach ( $data['items'] as $row ) { if ( ! is_array( $row ) ) { continue; } $name = sanitize_text_field( (string) ( $row['title'] ?? $row['name'] ?? '' ) ); $qty = (float) ( $row['quantity'] ?? 0 ); $price = (float) str_replace( ',', '', (string) ( $row['price'] ?? 0 ) ); if ( '' === $name && 0.0 === $qty && 0.0 === $price ) { continue; } $items[] = [ 'name' => $name, 'title' => $name, 'description' => sanitize_textarea_field( (string) ( $row['description'] ?? '' ) ), 'quantity' => $qty, 'price' => $price, 'amount' => round( $qty * $price, 2 ), 'total' => round( $qty * $price, 2 ), // The form is posted serialised, so an unchecked box is simply absent — // absent means not taxable, as it does when the invoice is saved. 'taxable' => ! empty( $row['taxable'] ) && '0' !== (string) $row['taxable'] ? '1' : '0', 'adjust_percentage' => (float) ( $row['adjust_percentage'] ?? $row['adjust'] ?? 0 ), ]; } if ( is_callable( [ $model, 'setItems' ] ) ) { $model->setItems( $items ); } } if ( ! empty( $data['client_id'] ) && is_callable( [ $model, 'populateClientInfo' ] ) ) { $model->populateClientInfo(); } if ( is_callable( [ $model, 'calculateTotals' ] ) ) { $model->calculateTotals(); } return $model; } }