Inventory_Presser_Taxonomies::save_location_meta( int $term_id, int $tt_id )

save_location_meta


Description Description

When a location term is saved, put all of the values in the right term meta fields.


Top ↑

Parameters Parameters

$term_id

(int) (Required)

$tt_id

(int) (Required)


Top ↑

Return Return

(void)


Top ↑

Source Source

File: includes/class-taxonomies.php

	function save_location_meta( $term_id, $tt_id )
	{
		if ( ! isset( $_POST['hours_title'] ) || ! isset( $_POST['phone_number'] ) )
		{
			return;
		}

		// HOURS
		$hours_count = sizeof( $_POST['hours_title'] ) - 1;
		for ( $i = 0; $i <= $hours_count; $i++ )
		{
			// if this is an update, carry the id through
			$uid = '';
			if ( isset( $_POST['hours_uid'][$i] ) )
			{
				$uid = sanitize_text_field( $_POST['hours_uid'][$i] );
			}
			else
			{
				//generate a unique id for these hours
				$uid = $this->generate_location_uid( $term_id . '_hours_' . $i );
			}
			update_term_meta( $term_id, 'hours_' . strval($i+1) . '_uid', $uid );

			// title of hours set
			update_term_meta( $term_id, 'hours_' . strval($i+1) . '_title', sanitize_text_field( $_POST['hours_title'][$i] ) );

			foreach( array_keys( INVP::weekdays() ) as $d => $day )
			{
				update_term_meta( $term_id, 'hours_' . strval($i+1) . '_' . $day . '_appt', sanitize_text_field( $_POST['hours'][$d]['appt'][$i] ) );
				update_term_meta( $term_id, 'hours_' . strval($i+1) . '_' . $day . '_open', sanitize_text_field( $_POST['hours'][$d]['open'][$i] ) );
				update_term_meta( $term_id, 'hours_' . strval($i+1) . '_' . $day . '_close', sanitize_text_field( $_POST['hours'][$d]['close'][$i] ) );
			}
		}

		// PHONE NUMBERS
		$phones_count = 0;
		foreach ( $_POST['phone_number'] as $i => $phone_number )
		{
			$phone_number = sanitize_text_field( $phone_number );
			if( empty( $phone_number ) )
			{
				continue;
			}

			update_term_meta( $term_id, 'phone_' . strval($i+1) . '_number', $phone_number );
			update_term_meta( $term_id, 'phone_' . strval($i+1) . '_description', sanitize_text_field( $_POST['phone_description'][$i] ) );

			// if this is an update, carry the id through
			$uid = '';
			if ( isset( $_POST['phone_uid'][$i] ) )
			{
				$uid = sanitize_text_field( $_POST['phone_uid'][$i] );
			}
			else
			{
				//generate a unique id for this phone number
				$uid = $this->generate_location_uid( $term_id . '_phone_' . $i );
			}
			update_term_meta( $term_id, 'phone_' . strval($i+1) . '_uid', $uid );

			$phones_count++;
		}

		//delete phones and hours in slots higher than we just filled or deletes are not possible
		for( $h=$hours_count+1; $h<self::LOCATION_MAX_HOURS; $h++ )
		{
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_uid' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_title' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_sunday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_sunday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_sunday_close' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_saturday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_saturday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_saturday_close' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_friday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_friday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_friday_close' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_thursday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_thursday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_thursday_close' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_wednesday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_wednesday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_wednesday_close' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_tuesday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_tuesday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_tuesday_close' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_monday_appt' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_monday_open' );
			delete_term_meta( $term_id, 'hours_' . strval($h) . '_monday_close' );
		}
		for( $p=$phones_count+1; $p<self::LOCATION_MAX_PHONES; $p++ )
		{
			delete_term_meta( $term_id, 'phone_' . strval($p) . '_uid' );
			delete_term_meta( $term_id, 'phone_' . strval($p) . '_description' );
			delete_term_meta( $term_id, 'phone_' . strval($p) . '_number' );
		}
	}