Inventory_Presser_Location_Hours::widget( array $args, array $instance )

widget


Description

Outputs the widget front-end HTML


Parameters

$args

(array) (Required)

$instance

(array) (Required)


Return

(void)


Source

File: includes/widget/class-widget-hours.php

	public function widget( $args, $instance ) {
		if ( empty( $instance['cb_display'] ) || ! is_array( $instance['cb_display'] ) ) {
			return;
		}

		$cb_showclosed = ( isset( $instance['cb_showclosed'] ) && $instance['cb_showclosed'] == 'true' );

		/**
		 * Hold the HTML that begins the widget until we're sure we're going to
		 * output something.
		 */
		$html = '';

		// before and after widget arguments are defined by themes
		$html .= $args['before_widget'];

		$title = apply_filters( 'widget_title', $instance['title'] );
		if ( ! empty( $title ) ) {
			$html .= $args['before_title'] . $title . $args['after_title'];
		}
		$html .= '<div class="invp-hours">';

		// get all locations
		$location_info = get_terms(
			'location',
			array(
				'fields'     => 'id=>name',
				'hide_empty' => false,
			)
		);

		// loop through each location
		foreach ( $location_info as $term_id => $name ) {
			// Does this address even have hours displayed by this instance of this widget?
			if ( empty( $instance['cb_display'][ $term_id ] ) ) {
				// No
				continue;
			}

			for ( $h = 1; $h <= INVP::LOCATION_MAX_HOURS; $h++ ) {
				$hours_uid = get_term_meta( $term_id, 'hours_' . $h . '_uid', true );
				if ( ! $hours_uid ) {
					break;
				}

				// There are hours in is slot $h, has the user configured this widget to display it?
				if ( ! in_array( $hours_uid, $instance['cb_display'][ $term_id ] ) ) {
					// No
					continue;
				}

				/**
				 * The first time we reach this block, we can be sure we have
				 * some hours content to output in the widget, so dump the
				 * preamble $html.
				 */
				if ( ! empty( $html ) ) {
					echo $html;
					$html = '';
				}

				$hours_title = '';
				if ( ! empty( $instance['cb_title'][ $term_id ] )
					&& is_array( $instance['cb_title'][ $term_id ] )
					&& in_array( $hours_uid, $instance['cb_title'][ $term_id ] )
				) {
					$hours_title = sprintf( '<strong>%s</strong>', get_term_meta( $term_id, 'hours_' . $h . '_title', true ) );
				}
				echo apply_filters( 'invp_hours_title', $hours_title, $hours_uid );

				// get current day number, starting on a monday
				$current_weekday = date( 'w' ) - 1;
				$current_weekday = ( $current_weekday == -1 ) ? 6 : $current_weekday;

				$start_of_week = get_option( 'start_of_week' ) - 1;

				$hours_sets = INVP::get_hours( $term_id );
				$hours_set  = array();
				foreach ( $hours_sets as $set ) {
					if ( $hours_uid == $set['uid'] ) {
						$hours_set = $set;
						break;
					}
				}
				$days          = Inventory_Presser_Shortcode_Hours_Today::create_days_array_from_hours_array( $hours_set );
				$next_open_day = Inventory_Presser_Shortcode_Hours_Today::find_next_open_day( $days );

				echo '<table>';

				// output a row for each day
				$highlighted_a_row = false;
				for ( $z = $start_of_week; $z < ( $start_of_week + 7 ); $z++ ) {
					$i = ( $z > 6 ) ? $z - 7 : $z;

					// do a check to make sure we want to output this row
					$echo_row = false;

					$open_by_appt = get_term_meta( $term_id, 'hours_' . $h . '_' . INVP::weekdays( $i ) . '_appt', true );
					$open         = get_term_meta( $term_id, 'hours_' . $h . '_' . INVP::weekdays( $i ) . '_open', true );
					$close        = get_term_meta( $term_id, 'hours_' . $h . '_' . INVP::weekdays( $i ) . '_close', true );

					if ( ( 1 == $open_by_appt || ! empty( $open ) && ! empty( $close ) ) || $cb_showclosed ) {
						$echo_row = true;
					} elseif ( $i < 6 ) {
						// check the remaining days, output current day if there are other displayed days following
						for ( $r = ( $i + 1 ); $r < 7; $r++ ) {
							$future_open_by_appt = get_term_meta( $term_id, 'hours_' . $h . '_' . INVP::weekdays( $r ) . '_appt', true );
							$future_open         = get_term_meta( $term_id, 'hours_' . $h . '_' . INVP::weekdays( $r ) . '_open', true );
							$future_close        = get_term_meta( $term_id, 'hours_' . $h . '_' . INVP::weekdays( $r ) . '_close', true );
							if ( 1 == $future_open_by_appt || ( ! empty( $future_open ) && ! empty( $future_close ) ) ) {
								$echo_row = true;
								break;
							}
						}
						// if there are no remaining days to display, break out of loop
						if ( ! $echo_row ) {
							break;
						}
					}

					// output row
					if ( $echo_row ) {
						/**
						 * Highlight this row if it's today and we have not
						 * passed closing time, or highlight it if we're
						 * not open today or closed for the day and $i is
						 * $next_open_day
						 */
						$current_row_class = '';
						if ( ! $highlighted_a_row
							&& ( ( ! empty( $days[ $i ] )
							&& $days[ $i ]->open_right_now()
							&& $current_weekday == $i ) // if it's today and we're open, highlight the row
							|| ( $current_weekday != $i
							&& ! empty( $next_open_day )
							&& $next_open_day->weekday - 1 == $i ) // it's not today, it's the next open day though
							)
						) {
							$current_row_class = ' class="day-highlight"';
							$highlighted_a_row = true;
						}

						printf(
							'<tr%s><th>%s</th>',
							$current_row_class,
							array_values( INVP::weekdays() )[ $i ]
						);

						if ( 1 == $open_by_appt && ! empty( $open ) && ! empty( $close ) ) {
							printf(
								'<td colspan="2">%s - %s &amp; %s</td>',
								$open,
								$close,
								__( 'Appointment', 'inventory-presser' )
							);
						} elseif ( 1 == $open_by_appt ) {
							printf( '<td colspan="2">%s</td>', __( 'Appointment Only', 'inventory-presser' ) );
						} elseif ( ! empty( $open ) && ! empty( $close ) ) {
							printf(
								'<td>%s</td><td>%s</td>',
								$open,
								$close
							);
						} else {
							echo '<td colspan="2">Closed</td>';
						}
						echo '</tr>';
					}
				}
				echo '</table>';
			}
		}
		// Only output HTML here if $html has been emptied, that means it was echoed
		if ( empty( $html ) ) {
			echo '</div>' . $args['after_widget'];
		}
	}