Automattic\WooCommerce\Internal\Admin\Orders

ListTable::months_filter()privateWC 1.0

Render the months filter dropdown.

Method of the class: ListTable{}

Return

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->months_filter();

ListTable::months_filter() code WC 8.7.0

private function months_filter() {
	// XXX: [review] we may prefer to move this logic outside of the ListTable class.

	/**
	 * Filters whether to remove the 'Months' drop-down from the order list table.
	 *
	 * @since 8.6.0
	 *
	 * @param bool   $disable   Whether to disable the drop-down. Default false.
	 */
	if ( apply_filters( 'woocommerce_' . $this->order_type . '_list_table_disable_months_filter', false ) ) {
		return;
	}

	global $wp_locale;
	global $wpdb;

	$orders_table = esc_sql( OrdersTableDataStore::get_orders_table_name() );
	$utc_offset   = wc_timezone_offset();

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$order_dates = $wpdb->get_results(
		"
			SELECT DISTINCT YEAR( t.date_created_local ) AS year,
							MONTH( t.date_created_local ) AS month
			FROM ( SELECT DATE_ADD( date_created_gmt, INTERVAL $utc_offset SECOND ) AS date_created_local FROM $orders_table WHERE status != 'trash' ) t
			ORDER BY year DESC, month DESC
		"
	);

	$m = isset( $_GET['m'] ) ? (int) $_GET['m'] : 0;
	echo '<select name="m" id="filter-by-date">';
	echo '<option ' . selected( $m, 0, false ) . ' value="0">' . esc_html__( 'All dates', 'woocommerce' ) . '</option>';

	foreach ( $order_dates as $date ) {
		$month           = zeroise( $date->month, 2 );
		$month_year_text = sprintf(
			/* translators: 1: Month name, 2: 4-digit year. */
			esc_html_x( '%1$s %2$d', 'order dates dropdown', 'woocommerce' ),
			$wp_locale->get_month( $month ),
			$date->year
		);

		printf(
			'<option %1$s value="%2$s">%3$s</option>\n',
			selected( $m, $date->year . $month, false ),
			esc_attr( $date->year . $month ),
			esc_html( $month_year_text )
		);
	}

	echo '</select>';
}