Inventory_Presser_Admin_Location_Meta::save_location_meta( int $term_id )

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


Parameters

$term_id

(int) (Required) Term ID.


Return

(void)


Source

File: includes/admin/class-admin-location-meta.php

	public function save_location_meta( $term_id ) {

		// Verify the nonce. What is the current hook name?
		switch ( current_action() ) {
			case 'created_location':
				if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce_add-tag'] ?? '' ) ), 'add-tag' ) ) {
					return;
				}
				break;
			case 'edited_location':
				if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ?? '' ) ), 'update-tag_' . $term_id ) ) {
					return;
				}
				break;
		}

		// Street address pieces.
		$keys = array(
			'address_street',
			'address_street_line_two',
			'address_city',
			'address_state',
			'address_zip',
		);
		foreach ( $keys as $key ) {
			if ( ! empty( $_POST[ $key ] ) ) {
				update_term_meta( $term_id, $key, sanitize_text_field( wp_unslash( $_POST[ $key ] ) ) );
			}
		}

		/**
		 * Now that the street address has been updated, repopulate the term
		 * description based on these pieces in term meta so the full street
		 * address in the term description always matches the pieces of the
		 * address.
		 */
		$this->update_location_term_description( $term_id );

		// HOURS.
		if ( ! empty( $_POST['hours_title'] ) ) {
			$hours_count = count( $_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( wp_unslash( $_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( wp_unslash( $_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( wp_unslash( $_POST['hours'][ $d ]['appt'][ $i ] ?? '' ) ) );
					update_term_meta( $term_id, 'hours_' . strval( $i + 1 ) . '_' . $day . '_open', sanitize_text_field( wp_unslash( $_POST['hours'][ $d ]['open'][ $i ] ?? '' ) ) );
					update_term_meta( $term_id, 'hours_' . strval( $i + 1 ) . '_' . $day . '_close', sanitize_text_field( wp_unslash( $_POST['hours'][ $d ]['close'][ $i ] ?? '' ) ) );
				}
			}

			// delete hours in slots higher than we just filled or deletes are not possible.
			for ( $h = $hours_count + 1; $h < INVP::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' );
			}
		}

		// PHONE NUMBERS.
		if ( ! empty( $_POST['phone_number'] ) ) {
			$phones_count  = 0;
			$phone_numbers = array_map( 'sanitize_text_field', wp_unslash( $_POST['phone_number'] ) );
			foreach ( $phone_numbers 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( wp_unslash( $_POST['phone_description'][ $i ] ?? '' ) ) );

				// if this is an update, carry the id through.
				$uid = '';
				if ( isset( $_POST['phone_uid'][ $i ] ) ) {
					$uid = sanitize_text_field( wp_unslash( $_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 in slots higher than we just filled or deletes are not possible.
			for ( $p = $phones_count + 1; $p < INVP::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' );
			}
		}
	}