Inventory_Presser_Shortcode_Sort_By::content( array $atts )

Creates the HTML output that replaces the shortcode.

On This Page


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'
		);

		global $wp_query;
		// If there are no posts, abort.
		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', 'inventory-presser' ),
					'DESC' => __( 'Make Z-A', 'inventory-presser' ),
				),

				'price'    => array(
					'ASC'  => __( 'Price Low', 'inventory-presser' ),
					'DESC' => __( 'Price High', 'inventory-presser' ),
				),

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

				'year'     => array(
					'ASC'  => __( 'Year Oldest', 'inventory-presser' ),
					'DESC' => __( 'Year Newest', 'inventory-presser' ),
				),
			)
		);

		$plugin_settings  = INVP::settings();
		$current_sort_key = isset( $_GET['orderby'] ) ? sanitize_text_field( wp_unslash( $_GET['orderby'] ) ) : ( isset( $plugin_settings['sort_vehicles_by'] ) ? $plugin_settings['sort_vehicles_by'] : '' );
		$current_sort_dir = isset( $_GET['order'] ) ? sanitize_text_field( wp_unslash( $_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>';
	}