get_id() ) ) { return; } $this->fields_for( $product ); } /** * Print the fields of every bookable product inside a group. * * @since 1.0.0 */ public function render_grouped() { global $product; if ( ! $product || ! $product->is_type( 'grouped' ) ) { return; } foreach ( $product->get_children() as $child_id ) { if ( ! Settings::is_bookable( $child_id ) ) { continue; } $child = wc_get_product( $child_id ); if ( $child ) { $this->fields_for( $child, true ); } } } /** * Hand the page the rules of each variation, so the fields follow the one picked. * * @since 1.0.1 * @param array $data Variation data sent to the page. * @param \WC_Product $product Variable product. * @param \WC_Product_Variation $variation Variation described. * @return array */ public function variation_rules( $data, $product, $variation ) { if ( ! Settings::is_bookable( $product->get_id() ) ) { return $data; } $data['abfw'] = array(); foreach ( array( 'dates', 'mode', 'duration', 'min', 'max' ) as $key ) { $data['abfw'][ $key ] = Settings::get( $key, $product->get_id(), $variation->get_id() ); } return $data; } /** * Print one set of fields. * * @since 1.0.0 * @param \WC_Product $product Product being booked. * @param bool $named Whether the fields carry the product identifier, * which a group of products needs. */ private function fields_for( $product, $named = false ) { $product_id = $product->get_id(); $rules = Settings::for_product( $product_id ); $suffix = $named ? '_' . $product_id : ''; $today = Dates::today(); $min = $today->modify( '+' . (int) $rules['first'] . ' days' )->format( 'Y-m-d' ); $max = $rules['last'] > 0 ? $today->modify( '+' . (int) $rules['last'] . ' days' )->format( 'Y-m-d' ) : ''; $style = sprintf( '--abfw-paper: %s; --abfw-accent: %s; --abfw-ink: %s;', $rules['background'], $rules['accent'], $rules['text'] ); echo '
'; if ( 'dropdown' === $rules['display'] ) { echo '
' . esc_html__( 'Choose your dates', 'abogo-booking' ) . ''; } if ( $named ) { echo '

' . esc_html( $product->get_name() ) . '

'; } echo '
'; $this->field( 'abfw_start' . $suffix, 'two' === $rules['dates'] ? __( 'Arrival', 'abogo-booking' ) : __( 'Date', 'abogo-booking' ), $this->posted( 'abfw_start' . $suffix ), $min, $max ); // Always printed, because a variation may ask for a departure its product does not. $this->field( 'abfw_end' . $suffix, __( 'Departure', 'abogo-booking' ), $this->posted( 'abfw_end' . $suffix ), $min, $max, 'two' !== $rules['dates'] ); echo '
'; echo '

'; /** * Fires inside the date fields of a product, so an extension can add to them. * * @since 1.1.0 * @param \WC_Product $product Product being booked. */ do_action( 'abfw_form_fields', $product ); if ( 'dropdown' === $rules['display'] ) { echo '
'; } echo '
'; } /** * Print one date field. * * @since 1.0.0 * @param string $name Field name. * @param string $label Visible label. * @param string $value Value to keep after a failed submission. * @param string $min Earliest date that can be picked. * @param string $max Latest date that can be picked. * @param bool $hidden Whether the field starts hidden. */ private function field( $name, $label, $value, $min, $max, $hidden = false ) { echo '

'; echo ''; echo ''; echo '

'; } /** * What the customer had typed before a refusal sent them back. * * @since 1.0.0 * @param string $name Field name. * @return string */ private function posted( $name ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return isset( $_REQUEST[ $name ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $name ] ) ) : ''; } }