Inventory_Presser_Plugin::save_vehicle_post_meta( int $post_id, WP_Post $post, bool $is_update )

Saves vehicle attributes into their corresponding post meta fields when the Save or Update button is clicked in the editor.


Parameters Parameters

$post_id

(int) (Required)

$post

(WP_Post) (Required)

$is_update

(bool) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: inventory-presser.php

			 * set the date_entered meta value using the post_date
			 */
			if ( ! $is_update || empty( INVP::get_meta( 'date_entered', $post_id ) ) ) {
				// use post_date to set date_entered.
				$post_date = DateTime::createFromFormat( 'Y-m-d H:i:s', $post->post_date );
				$timestamp = $post_date->format( $timestamp_format ) . ' ' . $offset;
				$key       = apply_filters( 'invp_prefix_meta_key', 'date_entered' );
				delete_post_meta( $post_id, $key );
				update_post_meta( $post_id, $key, $timestamp );
			}

			if ( empty( $_POST ) ) {
				return;
			}

			// Clear this value that is defined by a checkbox.
			update_post_meta( $post_id, apply_filters( 'invp_prefix_meta_key', 'featured' ), '0' );

			/**
			 * Loop over the post meta keys we manage and save their values
			 * if we find them coming over as part of the post to save.
			 */
			$keys   = INVP::keys();
			$keys[] = 'options_array';

			foreach ( $keys as $unprefixed_key ) {
				$key = apply_filters( 'invp_prefix_meta_key', $unprefixed_key );
				if ( isset( $_POST[ $key ] ) ) {
					if ( is_array( $_POST[ $key ] ) ) {
						// delete all meta, this is essentially for the options.
						delete_post_meta( $post->ID, $key );
						$options = array(); // collect the options to maintain a CSV field for backwards compatibility.

						foreach ( $this->sanitize_array( $_POST[ $key ] ) as $value ) {
							add_post_meta( $post->ID, $key, $value );
							if ( 'inventory_presser_options_array' === $key ) {
								$options[] = $value;
							}
						}
					} else {
						// String data.
						$value = sanitize_text_field( wp_unslash( $_POST[ $key ] ) );

						// Some values exist in post meta and taxonomy terms.
						// Check $_POST['tax_input'] in case the user checked a term box.
						if ( '' === $value && isset( $_POST['tax_input'] ) ) {
							$tax_input = $_POST['tax_input'];
							if ( isset( $overlapping_keys[ $unprefixed_key ] ) && isset( $tax_input[ $overlapping_keys[ $unprefixed_key ] ] ) ) {
								$term_id = intval( array_values( array_filter( $tax_input[ $overlapping_keys[ $unprefixed_key ] ] ) )[0] ?? '0' );
								$term    = get_term_by( 'term_taxonomy_id', $term_id, $overlapping_keys[ $unprefixed_key ] );
								if ( false !== $term ) {
									$value = $term->name;
								}
							}
						}

						// The values for keys with overlapping taxonomies might be slugs instead of term names.
						// inventory_presser_availability might arrive with a value of "for-sale" but we want to save "For Sale".
						$overlapping_keys = Inventory_Presser_Taxonomy_Overlapper::overlapping_meta_keys();
						if ( isset( $overlapping_keys[ $unprefixed_key ] ) ) {
							$term = get_term_by( 'slug', $value, $overlapping_keys[ $unprefixed_key ] );
							if ( false !== $term ) {
								$value = $term->name;
							}
						}
						update_post_meta( $post->ID, $key, sanitize_text_field( $value ) );
					}
				}
			}
		}

		/**
		 * Adds a contact form to single vehicle pages if a form is saved in the
		 * setting.
		 *
		 * @param  array $sections
		 * @return array
		 */
		public function single_sections_add_form( $sections ) {
			// Does this setting have a value?
			$settings = INVP::settings();
			if ( ! empty( $settings['singles_contact_form'] ) ) {
				// Value is a form ID prefixed with the form builder. GF_8.
				$form = explode( '_', $settings['singles_contact_form'] );
				if ( ! is_array( $form ) || 2 !== count( $form ) ) {
					return $sections;
				}
				$form_html         = '';
				$shortcode_pattern = '';
				switch ( $form[0] ) {
					case 'CF7':
						$shortcode_pattern = '[contact-form-7 id="%s"]';
						break;

					case 'GF':
						$shortcode_pattern = '

Oops! We could not locate your form.

'; break; case 'WPF': $shortcode_pattern = '[wpforms id="%s"]'; break; case 'WSF': $shortcode_pattern = '[ws_form id="%s"]'; break; } $shortcode = sprintf( $shortcode_pattern, $form[1] );