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

		/**
		 * Sanitizes every member of an array at every level with
		 * sanitize_text_field()
		 *
		 * @param  array $arr
		 * @return array
		 */
		protected function sanitize_array( $arr ) {
			foreach ( $arr as $key => $value ) {
				if ( is_array( $value ) ) {
					$arr[ $key ] = $this->sanitize_array( $value );
				} else {
					$arr[ $key ] = sanitize_text_field( $value );
				}
			}
			return $arr;
		}

		/**
		 * Saves vehicle attributes into their corresponding post meta fields when
		 * the Save or Update button is clicked in the editor.
		 *
		 * @param  int     $post_id
		 * @param  WP_Post $post
		 * @param  bool    $is_update
		 * @return void
		 */
		public function save_vehicle_post_meta( $post_id, $post, $is_update ) {
			/**
			 * Do not continue if the post is being moved to the trash or if this is
			 * an auto-draft.
			 */
			if ( in_array( $post->post_status, array( 'trash', 'auto-draft' ), true ) ) {
				return;
			}

			// Abort if autosave or AJAX/quick edit.
			if ( ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
				|| ( defined( 'DOING_AJAX' ) && DOING_AJAX )
			) {
				return;
			}

			// is this a vehicle?
			if ( ! empty( $_POST['post_type'] ) && INVP::POST_TYPE !== $_POST['post_type'] ) {
				// no, don't create any meta data for vehicles.
				return;
			}

			/**
			 * Tick the last modified date of this vehicle since we're saving changes.
			 * It looks like this: Tue, 06 Sep 2016 09:26:12 -0400
			 */
			$offset           = sprintf( '%+03d00', intval( get_option( 'gmt_offset' ) ) );
			$timestamp_format = 'D, d M Y h:i:s';
			// use post_modified to set last_modified.
			$post_modified = DateTime::createFromFormat( 'Y-m-d H:i:s', $post->post_modified );
			$timestamp     = $post_modified->format( $timestamp_format ) . ' ' . $offset;
			$key           = apply_filters( 'invp_prefix_meta_key', 'last_modified' );
			delete_post_meta( $post_id, $key );
			update_post_meta( $post_id, $key, $timestamp );

			/**
			 * 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' );