Untitled

mail@pastecode.io avatar
unknown
php
3 years ago
4.4 kB
0
Indexable
Never
<?

// Affichage correct de la date => YYYY-MM-DD
add_filter( "geodir_custom_field_output_custom_var_event_end_date" ,"geo_change_event_end_date_format", 11, 5 );
function geo_change_event_end_date_format( $html, $location, $cf, $output, $_gd_post ) {
	global $post;

	if ( ! empty( $_gd_post ) ) {
		$gd_post = $_gd_post;
	} else {
		global $gd_post;
	}

	if ( ! ( ! empty( $gd_post->post_type ) && GeoDir_Post_types::supports( $gd_post->post_type, 'events' ) ) ) {
		return $html;
	}

	$schedule = array();
	if ( isset( $gd_post->start_date ) ) {
		$schedule = $gd_post;
	} elseif ( ! empty( $post ) && $gd_post->ID == $post->ID && isset( $post->start_date ) ) {
		$schedule = $post;
	} elseif ( ( $schedules = GeoDir_Event_Schedules::get_schedules( $gd_post->ID, 'upcoming', 1 ) ) ) {
		$schedule = $schedules[0];
	} elseif ( ( $schedules = GeoDir_Event_Schedules::get_schedules( $gd_post->ID, '', 1 ) ) ) {
		$schedule = $schedules[0];
	}

	if ( empty( $schedule ) ) {
		return $html;
	}

	$htmlvar_name = $cf['htmlvar_name'];

	if ( ! empty( $schedule->start_date ) && $schedule->start_date != '0000-00-00' ) {
		$date_format = geodir_event_date_format();
		$time_format = geodir_event_time_format();
		$date_time_format = geodir_event_date_time_format();

		$start_date = $schedule->start_date;
		$start_time = $schedule->start_time;
		$start_date_time = $start_date . ' ' . $start_time;
		$end_date = $schedule->end_date;
		$end_time = $schedule->end_time;
		$end_date_time = $end_date . ' ' . $end_time;

		$format = '';
		if ( $cf['extra_fields'] != '' ) {
			$_date_format = stripslashes_deep( maybe_unserialize( $cf['extra_fields'] ) );

			if ( ! empty( $_date_format['date_format'] ) ) {
				$format = $_date_format['date_format'];
			}
		}

		$value = '';
		$value_raw = '';

		switch ( $htmlvar_name ) {
			case 'event_start_date':
				if ( empty( $format ) ) {
					$format = $date_format;
				}

				$value = $start_date_time;
				$value_raw = $start_date;
				break;
			case 'event_start_time':
				if ( empty( $format ) ) {
					$format = $time_format;
				}

				$value = $start_date_time;
				$value_raw = $start_time;
				break;
			case 'event_start_date_time':
				if ( empty( $format ) ) {
					$format = $date_time_format;
				}

				$value = $start_date_time;
				$value_raw = $start_date_time;
				break;
			case 'event_end_date':
				$format = 'Y-m-d';
				
				$value = $end_date_time;
				$value_raw = $end_date;
				break;
			case 'event_end_time':
				if ( empty( $format ) ) {
					$format = $time_format;
				}

				$value = $end_date_time;
				$value_raw = $end_time;
				break;
			case 'event_end_date_time':
				if ( empty( $format ) ) {
					$format = $date_time_format;
				}

				$value = $end_date_time;
				$value_raw = $end_date_time;
				break;
		}
		$value_raw = date_i18n( $format, strtotime( $value_raw ) );
		if ( ! empty( $output ) && ( isset( $output['raw'] ) || isset( $output['strip'] ) ) ) {
			// Stripped value.
			return $value_raw;
		}

		$value = date_i18n( $format, strtotime( $value ) );

		$field_icon = geodir_field_icon_proccess( $cf );
		$output = geodir_field_output_process( $output );
		if ( strpos( $field_icon, 'http' ) !== false ) {
			$field_icon_af = '';
		} elseif ( $field_icon == '' ) {
			$field_icon_af = '<i class="fas fa-calendar" aria-hidden="true"></i>';
		} else {
			$field_icon_af = $field_icon;
			$field_icon = '';
		}

		if ( ( ! empty( $output ) && isset( $output['raw'] ) ) || ( ! empty( $output ) && isset( $output['strip'] ) ) ) {
			// Database value / stripped value.
			return $value_raw;
		}

		$html = '<div class="geodir_post_meta ' . $cf['css_class'] . ' geodir-field-' . $cf['htmlvar_name'] . '">';

		 if ( $output=='' || isset( $output['icon'] ) ) $html .= '<span class="geodir_post_meta_icon geodir-i-datepicker" style="' . $field_icon . '">' . $field_icon_af;
		if ( $output=='' || isset( $output['label'] ) ) $html .= trim( $cf['frontend_title'] ) != '' ? '<span class="geodir_post_meta_title" >' . __( $cf['frontend_title'], 'geodirectory' ) . ': </span>' : '';
		if ( $output=='' || isset( $output['icon'] ) ) $html .= '</span>';
		if ( $output=='' || isset( $output['value'] ) ) $html .= $value;

		$html .= '</div>';
	}

	return $html;
}