| 1 |
<?php |
| 2 |
/** |
| 3 |
* Abstract Facet Renderer class. |
| 4 |
* |
| 5 |
* @since 4.7.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\Feature\Facets; |
| 10 |
|
| 11 |
/** |
| 12 |
* Abstract Facet Renderer class. |
| 13 |
*/ |
| 14 |
abstract class Renderer { |
| 15 |
/** |
| 16 |
* Whether the term count should be displayed or not. |
| 17 |
* |
| 18 |
* @var bool |
| 19 |
*/ |
| 20 |
protected $display_count; |
| 21 |
|
| 22 |
/** |
| 23 |
* Method to render the facet. |
| 24 |
* |
| 25 |
* @param array $args Widget args |
| 26 |
* @param array $instance Instance settings |
| 27 |
*/ |
| 28 |
abstract public function render( $args, $instance ); |
| 29 |
|
| 30 |
/** |
| 31 |
* Whether the facet should be rendered or not. |
| 32 |
* |
| 33 |
* @return bool |
| 34 |
*/ |
| 35 |
protected function should_render() : bool { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Given an array of values, reorder them. |
| 41 |
* |
| 42 |
* @param array $values Multidimensional array of values. Each value should have (string) `name`, (int) `count`, and (bool) `is_selected`. |
| 43 |
* @param string $orderby Key to be used to order. |
| 44 |
* @param string $order ASC or DESC. |
| 45 |
* @return array |
| 46 |
*/ |
| 47 |
protected function order_values( array $values, string $orderby = 'count', $order = 'desc' ) : array { |
| 48 |
$orderby = strtolower( $orderby ); |
| 49 |
$orderby = in_array( $orderby, [ 'name', 'count' ], true ) ? $orderby : 'count'; |
| 50 |
|
| 51 |
$order = strtoupper( $order ); |
| 52 |
$order = in_array( $order, [ 'ASC', 'DESC' ], true ) ? $order : 'DESC'; |
| 53 |
|
| 54 |
$values = wp_list_sort( $values, $orderby, $order, true ); |
| 55 |
|
| 56 |
$selected = []; |
| 57 |
foreach ( $values as $key => $value ) { |
| 58 |
if ( $value['is_selected'] ) { |
| 59 |
$selected[ $key ] = $value; |
| 60 |
unset( $values[ $key ] ); |
| 61 |
} |
| 62 |
} |
| 63 |
$values = $selected + $values; |
| 64 |
|
| 65 |
return $values; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Get the markup for an individual facet item. |
| 70 |
* |
| 71 |
* @param array|object $item Facet item. |
| 72 |
* @param string $url URL for the facet item. |
| 73 |
* @return string|null |
| 74 |
*/ |
| 75 |
public function get_facet_item_value_html( $item, string $url ) { |
| 76 |
return null; |
| 77 |
} |
| 78 |
} |
| 79 |
|