false, 'mode' => 'disable', 'sold_out_label' => __( '(Sold out)', 'fieldwren' ), 'show_remaining' => false, 'remaining_label' => __( '({n} left)', 'fieldwren' ), 'message' => __( '“{option}” is sold out. Please choose another option.', 'fieldwren' ), 'items' => array(), ), (array) ( $d['inventory'] ?? array() ) ); return $d; } /* ------------------------------------------------------------------ * * Stock * ------------------------------------------------------------------ */ /** @return array> field => option value => stock. */ public static function stock_map( $form_id ) { $inv = Form::settings( $form_id )['inventory']; $out = array(); if ( empty( $inv['enabled'] ) ) { return $out; } foreach ( (array) $inv['items'] as $it ) { if ( is_array( $it ) && ! empty( $it['field'] ) && isset( $it['stock'] ) && '' !== (string) $it['stock'] && is_numeric( $it['stock'] ) ) { $out[ (string) $it['field'] ][ Helpers::norm( $it['value'] ?? '' ) ] = max( 0, (int) $it['stock'] ); } } return $out; } /** @return array> field => option value => entries holding it. */ public static function counts( $form_id ) { $key = 'fwren_inv_' . (int) $form_id; $cached = get_transient( $key ); if ( is_array( $cached ) ) { return $cached; } global $wpdb; $e = Install::table( 'entries' ); $v = Install::table( 'entry_values' ); $out = array(); foreach ( array_keys( self::stock_map( $form_id ) ) as $field ) { $rows = $wpdb->get_col( $wpdb->prepare( "SELECT v.value FROM $v v INNER JOIN $e e ON e.id = v.entry_id WHERE v.form_id = %d AND v.field_name = %s AND v.sub_field = '' AND e.status NOT IN ('trash','spam','partial','declined')", $form_id, $field ) ); // phpcs:ignore foreach ( $rows as $val ) { $val = (string) $val; $list = ( '' !== $val && '[' === $val[0] ) ? json_decode( $val, true ) : array( $val ); foreach ( is_array( $list ) ? $list : array() as $one ) { if ( is_scalar( $one ) && '' !== (string) $one ) { $one = Helpers::norm( $one ); $out[ $field ][ $one ] = ( $out[ $field ][ $one ] ?? 0 ) + 1; } } } } set_transient( $key, $out, DAY_IN_SECONDS ); return $out; } /** @return int|null Remaining stock (null = unlimited). */ public static function remaining( $form_id, $field, $value ) { $map = self::stock_map( $form_id ); $value = Helpers::norm( $value ); if ( ! isset( $map[ $field ][ $value ] ) ) { return null; } $counts = self::counts( $form_id ); return $map[ $field ][ $value ] - (int) ( $counts[ $field ][ $value ] ?? 0 ); } /** Clear the cache. */ public static function flush( $form_id ) { delete_transient( 'fwren_inv_' . (int) $form_id ); } /** After a submission. */ public static function flush_for_insert( $entry_id, $values, $form_id ) { self::flush( $form_id ); } /** After a delete. */ public static function flush_for_entry( $id, $entry ) { if ( is_array( $entry ) && ! empty( $entry['form_id'] ) ) { self::flush( (int) $entry['form_id'] ); } } /** After a status / value change. */ public static function flush_for_update( $id, $data ) { if ( ! isset( $data['status'] ) && ! isset( $data['response'] ) ) { return; } global $wpdb; $form_id = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT form_id FROM ' . Install::table( 'entries' ) . ' WHERE id = %d', $id ) ); // phpcs:ignore if ( $form_id ) { self::flush( $form_id ); } } /* ------------------------------------------------------------------ * * Frontend * ------------------------------------------------------------------ */ /** * Mark sold-out options. * * @param array $opts Options. * @param array $s Block settings. * @param \FWREN\Field_Block $field Field block. * @return array */ public static function options( $opts, $s, $field ) { if ( Renderer::is_editor() || ! is_object( $field ) || ! method_exists( $field, 'field_key' ) || ! in_array( $field->get_field_type(), self::TYPES, true ) ) { return $opts; } $form_id = Renderer::current_form_id(); $map = $form_id ? self::stock_map( $form_id ) : array(); $key = $field->field_key( (array) $s ); if ( empty( $map[ $key ] ) ) { return $opts; } $inv = Form::settings( $form_id )['inventory']; $out = array(); foreach ( (array) $opts as $o ) { if ( ! isset( $map[ $key ][ Helpers::norm( $o['value'] ) ] ) ) { $out[] = $o; continue; } $left = (int) self::remaining( $form_id, $key, $o['value'] ); $o['stock'] = max( 0, $left ); if ( $left <= 0 ) { if ( 'hide' === $inv['mode'] ) { continue; } $o['disabled'] = true; $o['selected'] = false; $o['label'] .= ' ' . $inv['sold_out_label']; } elseif ( ! empty( $inv['show_remaining'] ) ) { $o['label'] .= ' ' . str_replace( '{n}', (string) $left, (string) $inv['remaining_label'] ); } $out[] = $o; } return $out; } /** Reject sold-out options. */ public static function validate( $errors, $values, $form_id, $schema ) { $map = self::stock_map( $form_id ); if ( ! $map ) { return $errors; } $inv = Form::settings( $form_id )['inventory']; foreach ( $map as $key => $stock ) { if ( isset( $errors[ $key ] ) || ! isset( $values[ $key ] ) ) { continue; } foreach ( is_array( $values[ $key ] ) ? $values[ $key ] : array( $values[ $key ] ) as $v ) { if ( ! isset( $stock[ Helpers::norm( $v ) ] ) ) { continue; } $left = self::remaining( $form_id, $key, $v ); if ( null !== $left && $left <= 0 ) { $label = (string) $v; foreach ( (array) ( $schema['fields'][ $key ]['options'] ?? array() ) as $o ) { if ( (string) $o['value'] === (string) $v ) { $label = wp_strip_all_tags( $o['label'] ); } } $errors[ $key ] = str_replace( '{option}', $label, (string) $inv['message'] ); break; } } } return $errors; } /* ------------------------------------------------------------------ * * Admin * ------------------------------------------------------------------ */ /** Per-form tab. */ public static function tab( $tabs, $form_id = 0 ) { $tabs['inventory'] = __( 'Inventory', 'fieldwren' ); return $tabs; } /** Tab content. */ public static function render_tab( $form_id, $settings ) { $inv = $settings['inventory']; $n = 'fwren[inventory]'; $saved = array(); foreach ( (array) $inv['items'] as $it ) { if ( is_array( $it ) && ! empty( $it['field'] ) ) { $saved[ $it['field'] ][ Helpers::norm( $it['value'] ?? '' ) ] = (string) ( $it['stock'] ?? '' ); } } self::flush( $form_id ); $counts = self::counts( $form_id ); echo ''; Helpers::row_toggle( $n . '[enabled]', ! empty( $inv['enabled'] ), __( 'Track stock', 'fieldwren' ), __( 'Limit how many entries can choose an option, e.g. seats, sizes or time slots.', 'fieldwren' ) ); Helpers::row_select( $n . '[mode]', $inv['mode'], __( 'Sold-out options', 'fieldwren' ), array( 'disable' => __( 'Show as disabled', 'fieldwren' ), 'hide' => __( 'Hide', 'fieldwren' ) ) ); Helpers::row_text( $n . '[sold_out_label]', $inv['sold_out_label'], __( 'Sold-out label', 'fieldwren' ) ); Helpers::row_toggle( $n . '[show_remaining]', ! empty( $inv['show_remaining'] ), __( 'Show the remaining quantity', 'fieldwren' ) ); Helpers::row_text( $n . '[remaining_label]', $inv['remaining_label'], __( 'Remaining label', 'fieldwren' ), __( '{n} is the quantity left.', 'fieldwren' ) ); Helpers::row_text( $n . '[message]', $inv['message'], __( 'Error when sold out', 'fieldwren' ), __( '{option} is the option label.', 'fieldwren' ) ); echo ''; echo ''; $i = 0; foreach ( Schema::fields( $form_id ) as $key => $f ) { if ( ! in_array( $f['type'], self::TYPES, true ) || empty( $f['options'] ) ) { continue; } foreach ( $f['options'] as $j => $o ) { $p = $n . '[items][' . $i . ']'; $stock = $saved[ $key ][ Helpers::norm( $o['value'] ) ] ?? ''; $taken = (int) ( $counts[ $key ][ Helpers::norm( $o['value'] ) ] ?? 0 ); echo ''; echo ''; echo ''; echo ''; $i++; } } if ( ! $i ) { echo ''; } echo '
' . esc_html__( 'Field', 'fieldwren' ) . '' . esc_html__( 'Option', 'fieldwren' ) . '' . esc_html__( 'Stock', 'fieldwren' ) . '' . esc_html__( 'Taken', 'fieldwren' ) . '' . esc_html__( 'Remaining', 'fieldwren' ) . '
' . ( 0 === $j ? '' . esc_html( Schema::label( $f ) ) . '
' . esc_html( $key ) . '' : '' ) . '
' . esc_html( wp_strip_all_tags( $o['label'] ) ) . '' . (int) $taken . '' . ( '' === $stock ? '—' : (int) max( 0, (int) $stock - $taken ) ) . '
' . esc_html__( 'Add a Dropdown, Radio or Checkboxes field to the form first.', 'fieldwren' ) . '

' . esc_html__( 'Empty stock = unlimited. Entries awaiting confirmation or approval hold their option.', 'fieldwren' ) . '

'; } /** Clean and store. */ public static function save_tab( $form_id, $post ) { if ( ! current_user_can( 'fwren_manage_forms' ) ) { return; } $in = Helpers::posted( (array) $post, 'inventory' ); $out = array( 'enabled' => Helpers::on( $in['enabled'] ?? '' ), 'mode' => 'hide' === ( $in['mode'] ?? '' ) ? 'hide' : 'disable', 'sold_out_label' => Helpers::text( $in['sold_out_label'] ?? '' ), 'show_remaining' => Helpers::on( $in['show_remaining'] ?? '' ), 'remaining_label' => Helpers::text( $in['remaining_label'] ?? '' ), 'message' => Helpers::text( $in['message'] ?? '' ), 'items' => array(), ); foreach ( (array) ( $in['items'] ?? array() ) as $it ) { if ( is_array( $it ) && ! empty( $it['field'] ) && isset( $it['stock'] ) && '' !== trim( (string) $it['stock'] ) && is_numeric( $it['stock'] ) ) { $out['items'][] = array( 'field' => sanitize_key( $it['field'] ), 'value' => Helpers::norm( $it['value'] ?? '' ), 'stock' => max( 0, (int) $it['stock'] ) ); } } Helpers::save_setting( $form_id, 'inventory', $out ); self::flush( $form_id ); } }