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

$post_id

(int) (Required)

$post

(WP_Post) (Required)

$is_update

(bool) (Required)


Return

(void)


Source

File: inventory-presser.php

			/**
			 * If this is not an update or there is no date entered post meta value,
			 * 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 $key ) {
				$key = apply_filters( 'invp_prefix_meta_key', $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.
						update_post_meta( $post->ID, $key, sanitize_text_field( $_POST[ $key ] ) );
					}
				}
			}
		}

		/**
		 * Saves custom taxonomy terms when vehicles are saved
		 *
		 * @param  int  $post_id   A post ID.
		 * @param  bool $is_update True if this is a post update rather than an insert.
		 * @return void
		 */
		public function save_vehicle_taxonomy_terms( $post_id, $is_update ) {
			foreach ( Inventory_Presser_Taxonomies::slugs_array() as $slug ) {
				$taxonomy_name = $slug;
				switch ( $slug ) {
					case 'style':
						$slug = 'body_style';
						break;
					case 'model_year':
						$slug = 'year';
						break;
				}
				Inventory_Presser_Taxonomies::save_taxonomy_term( $post_id, $taxonomy_name, apply_filters( 'invp_prefix_meta_key', $slug ) );
			}
		}
	}

	$inventory_presser = new Inventory_Presser_Plugin();
	$inventory_presser->add_hooks();
}