Inventory_Presser_Contact_Form_7::handler_vehicle( object $tag )

handler_vehicle


Parameters

$tag

(object) (Required)


Return

(void)


Source

File: includes/integrations/class-contact-form-7.php

		public function handler_vehicle( $tag ) {
			$validation_error = wpcf7_get_validation_error( $tag->name );
			$atts             = array(
				'id' => '',
			);

			if ( ! empty( $tag->options ) ) {
				array_walk(
					$tag->options,
					function ( $item ) use ( &$atts ) {
						list( $key, $val ) = explode( ':', $item );
						$atts[ $key ]      = $val;
					}
				);
			}

			// Are we on a vdp? return a hidden field.
			if ( is_singular( INVP::POST_TYPE ) ) {
				$input_atts = array(
					'type'  => 'hidden',
					'name'  => $tag->name,
					'value' => $this->prepare_value(),
				);
				if ( ! empty( $atts['id'] ) ) {
					$input_atts['id'] = $atts['id'];
				}
				$html = sprintf(
					'<input %s />',
					wpcf7_format_atts( $input_atts )
				);
				return apply_filters( 'invp_cf7_field_vehicle_html', $html . $validation_error, $atts );
			}

			/**
			 * Here's a query to get the post IDs of the vehicles we want
			 * in the order we want. The reason we are not using get_posts
			 * is because we want to sort by 3 post meta values
			 */
			global $wpdb;
			$post_ids = $wpdb->get_col(
				$wpdb->prepare(
					"
				SELECT 		DISTINCT ID
				FROM		$wpdb->posts
							LEFT JOIN $wpdb->postmeta meta1 ON $wpdb->posts.ID = meta1.post_id
							LEFT JOIN $wpdb->postmeta meta2 ON $wpdb->posts.ID = meta2.post_id
							LEFT JOIN $wpdb->postmeta meta3 ON $wpdb->posts.ID = meta3.post_id
				WHERE 		post_type = %s
							AND post_status = 'publish'
							AND meta1.meta_key = %s
							AND meta2.meta_key = %s
							AND meta3.meta_key = %s
				ORDER BY	meta1.meta_value DESC,
							meta2.meta_value ASC,
							meta3.meta_value ASC
				",
					INVP::POST_TYPE,
					apply_filters( 'invp_prefix_meta_key', 'year' ),
					apply_filters( 'invp_prefix_meta_key', 'make' ),
					apply_filters( 'invp_prefix_meta_key', 'model' )
				)
			);

			// no results? no HTML.
			if ( empty( $post_ids ) ) {
				return apply_filters( 'invp_cf7_field_vehicle_html', '' . $validation_error, $atts );
			}

			// build a drop down select.
			$select_atts = array(
				'name'  => $tag->name,
				'class' => wpcf7_form_controls_class( 'select' ),
			);
			if ( ! empty( $atts['id'] ) ) {
				$select_atts['id'] = $atts['id'];
			}
			$html = sprintf(
				'<span class="wpcf7-form-control-wrap" data-name="%s"><select %s><option value="">%s</option>',
				esc_attr( $tag->name ),
				wpcf7_format_atts( $select_atts ),
				__( 'Please choose a vehicle...', 'inventory-presser' )
			);
			foreach ( $post_ids as $post_id ) {
				// is this vehicle sold?
				if ( has_term( 'sold', 'availability', $post_id ) ) {
					continue;
				}
				$meta = get_metadata( 'post', $post_id );

				// label is "2005 Subaru Baja Turbo, Blue, #12022".
				$html .= sprintf(
					'<option value="%s" %s>%s %s %s',
					esc_attr( $this->prepare_value( $post_id ) ),
					isset( $_REQUEST['v'] ) ? selected( $post_id, $_REQUEST['v'], false ) : '',
					esc_html( $meta[ apply_filters( 'invp_prefix_meta_key', 'year' ) ][0] ),
					esc_html( $meta[ apply_filters( 'invp_prefix_meta_key', 'make' ) ][0] ),
					esc_html( $meta[ apply_filters( 'invp_prefix_meta_key', 'model' ) ][0] )
				);

				if ( isset( $meta[ apply_filters( 'invp_prefix_meta_key', 'trim' ) ][0] ) ) {
					$html .= ' ' . $meta[ apply_filters( 'invp_prefix_meta_key', 'trim' ) ][0];
				}

				if ( isset( $meta[ apply_filters( 'invp_prefix_meta_key', 'color' ) ][0] ) ) {
					$html .= ', ' . $meta[ apply_filters( 'invp_prefix_meta_key', 'color' ) ][0];
				}

				$html .= sprintf(
					', &#35;%s</option>',
					$meta[ apply_filters( 'invp_prefix_meta_key', 'stock_number' ) ][0]
				);
			}
			return apply_filters( 'invp_cf7_field_vehicle_html', $html . '</select></span>' . $validation_error, $atts );
		}