Inventory_Presser_Shortcode_Sort_By::content( array $atts )

Creates the HTML output that replaces the shortcode.


Parameters Parameters

$atts

(array) (Required) Shortcode attributes.


Top ↑

Return Return

(string)


Top ↑

Source Source

File: includes/shortcode/class-shortcode-sort-by.php

	public function content( $atts ) {
		$atts = shortcode_atts(
			array(
				'label' => __( 'SORT', 'inventory_presser' ),
			),
			$atts,
			'invp_sort_by'
		);
		if ( ! is_post_type_archive( INVP::POST_TYPE ) ) {
			return '';
		}

		// If there are no posts shown in this archive, abort.
		global $wp_query;
		if ( 0 === $wp_query->found_posts ) {
			return '';
		}

		if ( ! wp_script_is( 'invp_sort_by', 'registered' ) ) {
			Inventory_Presser_Plugin::include_scripts_and_styles();
		}
		wp_enqueue_script( 'invp_sort_by' );

		$html = '';
		if ( ! empty( $atts['label'] ) ) {
			$html .= sprintf( '<label for="sort_by">%s</label> ', esc_html( $atts['label'] ) );
		}
		$html .= '<select class="inventory_sort" id="sort_by">';

		$options_data = apply_filters(
			'invp_sort_dropdown_options',
			array(
				'make'     => array(
					'ASC'  => __( 'Make A-Z', '_dealer' ),
					'DESC' => __( 'Make Z-A', '_dealer' ),
				),

				'price'    => array(
					'ASC'  => __( 'Price Low', '_dealer' ),
					'DESC' => __( 'Price High', '_dealer' ),
				),

				'odometer' => array(
					'ASC'  => sprintf(
						'%s %s',
						apply_filters( 'invp_odometer_word', 'Mileage' ),
						__( 'Low', '_dealer' )
					),
					'DESC' => sprintf(
						'%s %s',
						apply_filters( 'invp_odometer_word', 'Mileage' ),
						__( 'High', '_dealer' )
					),
				),

				'year'     => array(
					'ASC'  => __( 'Year Oldest', '_dealer' ),
					'DESC' => __( 'Year Newest', '_dealer' ),
				),
			)
		);

		$plugin_settings  = INVP::settings();
		$current_sort_key = isset( $_GET['orderby'] ) ? $_GET['orderby'] : ( isset( $plugin_settings['sort_vehicles_by'] ) ? $plugin_settings['sort_vehicles_by'] : '' );
		$current_sort_dir = isset( $_GET['order'] ) ? $_GET['order'] : ( isset( $plugin_settings['sort_vehicles_order'] ) ? $plugin_settings['sort_vehicles_order'] : '' );

		if ( ! empty( $current_sort_key ) ) {
			$without_prefix = apply_filters( 'invp_unprefix_meta_key', $current_sort_key );
			if ( ! in_array( $without_prefix, array_keys( $options_data ), true ) ) {
				// The current sort option isn't in the list, so add it.
				$label = ucfirst( $without_prefix );
				switch ( $without_prefix ) {
					case 'post_date':
						$label = __( 'Date entered', 'inventory-presser' );
						break;

					case 'post_modified':
						$label = __( 'Last modified', 'inventory-presser' );
						break;
				}

				$options_data[ $without_prefix ] = array(
					'ASC'  => $label . ' 🔼',
					'DESC' => $label . ' 🔽',
				);
			}
		}

		foreach ( $options_data as $key => $options ) {
			foreach ( $options as $dir => $label ) {
				$html .= sprintf(
					'<option data-order="%s" value="%s"%s>%s</option>',
					$dir,
					$key,
					selected( $key . $dir, $current_sort_key . $current_sort_dir, false ),
					$label
				);
			}
		}
		return $html . '</select>';
	}