按月过滤-为“过滤器栏”创建自定义过滤器

时间:2018-10-20 21:17:56

标签: javascript php wordpress filtering

关于Events Calendar Pro的Filter Bar插件,我试图创建一个自定义过滤器;也就是说,我想实现按月份过滤的功能。

根据此guide/documentation,毫不奇怪,建议您从现有的过滤器开始,然后从那里进行修改。但是,我才刚刚开始熟悉php,而且我肯定正在为这项工作而苦苦挣扎。

如果没有别的,任何人都可以让我入门,或者为我指明正确的方向吗?

在所有现有过滤器中,我认为从现有的“星期几”过滤器开始在逻辑上最有意义。在下面,我从下面的Day_Of_Week.php文件中粘贴了代码。希望它可以作为起点;现有的唯一其他可能有用的过滤器是“时间”过滤器。

如果有任何其他帮助,here是到我正在开发的开发站点上即将到来的日历的列表视图的链接。

要清楚一点-是的,实际上我可以从列表>月份更改“视图”,但这并不能使我达到我希望的位置。实际上,一旦我有了“过滤器栏”按月过滤功能,最终,我实际上更愿意完全放弃月视图功能。

<?php

/**
 * Class Tribe__Events__Filterbar__Filters__Day_Of_Week
 */
class Tribe__Events__Filterbar__Filters__Day_Of_Week extends Tribe__Events__Filterbar__Filter {

	public $type = 'checkbox';

	protected function get_values() {
		$day_of_week_array = array(
			__( 'Sunday', 'tribe-events-filter-view' ),
			__( 'Monday', 'tribe-events-filter-view' ),
			__( 'Tuesday', 'tribe-events-filter-view' ),
			__( 'Wednesday', 'tribe-events-filter-view' ),
			__( 'Thursday', 'tribe-events-filter-view' ),
			__( 'Friday', 'tribe-events-filter-view' ),
			__( 'Saturday', 'tribe-events-filter-view' ),
		);

		// Get WordPress system value for the start of the week, keep in mind that on WordPress it
		// starts on 0 instead of 1 like we did above.
		$sys_week_start = absint( get_option( 'start_of_week', 0 ) );

		$sorted = range( 0, 6 );

		// Push the items of the array until the start_of_week to the end
		for ( $i = 0; $i < $sys_week_start; $i ++ ) {
			array_push( $sorted, array_shift( $sorted ) );
		}

		$day_of_week_values = array();
		foreach ( $sorted as $n ) {
			$day_of_week_values[] = array(
				'name'  => $day_of_week_array[ $n ],
				'value' => $n + 1,
			);
		}

		return $day_of_week_values;
	}

	/**
	 * Add modifications to the query
	 *
	 * @return void
	 */
	protected function setup_query_filters() {
		global $wp_query;
		// band-aid for month view
		if ( $wp_query->is_main_query() && $wp_query->get( 'eventDisplay' ) == 'month' ) {
			$wp_query->set(
				'meta_query', array(
					array(
						'key'  => '_EventStartDate',
						'type' => 'DATETIME',
					),
				)
			);
		}
		parent::setup_query_filters();
	}

	protected function setup_join_clause() {
		add_filter( 'posts_join', array( 'Tribe__Events__Query', 'posts_join' ), 10, 2 );

		// Default behavior is to *not* force local TZ; so let's reset to the default behavior
		// to make sure we don't interfere with queries other than the Day-filter one.
		add_filter( 'tribe_events_query_force_local_tz', '__return_false' );
	}

	protected function setup_where_clause() {

		/** @var wpdb $wpdb */
		global $wpdb;
		$clauses = array();
		$values = array_map( 'intval', $this->currentValue );
		$values = implode( ',', $values );

		$eod_cutoff = tribe_get_option( 'multiDayCutoff', '00:00' );
		if ( $eod_cutoff != '00:00' ) {
			$eod_time_difference = Tribe__Date_Utils::time_between( '1/1/2014 00:00:00', "1/1/2014 {$eod_cutoff}:00" );
			$start_date = "DATE_SUB({$wpdb->postmeta}.meta_value, INTERVAL {$eod_time_difference} SECOND)";
			$end_date = "DATE_SUB(tribe_event_end_date.meta_value, INTERVAL {$eod_time_difference} SECOND)";
		} else {
			$start_date = "{$wpdb->postmeta}.meta_value";
			$end_date = 'tribe_event_end_date.meta_value';
		}

		$clauses[] = "(DAYOFWEEK($start_date) IN ($values))";

		// is it on at least 7 days (first day is 0)
		$clauses[] = "(DATEDIFF($end_date, $start_date) >=6)";

		// determine if the start of the nearest matching day is between the start and end dates
		$distance_to_day = array();
		foreach ( $this->currentValue as $day_of_week_index ) {
			$day_of_week_index = (int) $day_of_week_index;
			$distance_to_day[] = "MOD( 7 + $day_of_week_index - DAYOFWEEK($start_date), 7 )";
		}
		if ( count( $distance_to_day ) > 1 ) {
			$distance_to_next_matching_day = 'LEAST(' . implode( ',', $distance_to_day ) . ')';
		} else {
			$distance_to_next_matching_day = reset( $distance_to_day );
		}
		$clauses[] = "(DATE(DATE_ADD($start_date, INTERVAL $distance_to_next_matching_day DAY)) < $end_date)";

		$this->whereClause = ' AND (' . implode( ' OR ', $clauses ) . ')';

		// Forces the query to use _EventStartDate and _EventEndDate as the times to base results
		// off of, instead of _EventStartDateUTC, _EventEventDateUTC which can produce weird results.
		add_filter( 'tribe_events_query_force_local_tz', '__return_true' );
	}
}

0 个答案:

没有答案