| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if ( ! defined('ABSPATH') ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Filter |
| 10 |
{ |
| 11 |
public $filters = []; |
| 12 |
|
| 13 |
private $logicSeparators; |
| 14 |
|
| 15 |
public $sep = '#'; |
| 16 |
|
| 17 |
public function __construct(){ |
| 18 |
$this->logicSeparators = array( |
| 19 |
'or' => '-or-', |
| 20 |
'and' => '-and-' |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
public function getEmptyFilter() |
| 25 |
{ |
| 26 |
return apply_filters( 'wpc_filter_defaults', array( |
| 27 |
'ID' => '', |
| 28 |
'parent' => '', |
| 29 |
'menu_order' => '', |
| 30 |
'entity' => '', |
| 31 |
'e_name' => '', |
| 32 |
'label' => '', |
| 33 |
'slug' => '', |
| 34 |
'view' => '', |
| 35 |
'dropdown_label' => '', |
| 36 |
'date_type' => '', |
| 37 |
'date_format' => '', |
| 38 |
'show_term_names' => '', |
| 39 |
'selected_and_above' => '', |
| 40 |
'show_range_list' => '', |
| 41 |
'range_list_input' => '', |
| 42 |
'logic' => '', |
| 43 |
'orderby' => '', |
| 44 |
'in_path' => '', |
| 45 |
'exclude' => '', |
| 46 |
'include' => '', |
| 47 |
'collapse' => '', |
| 48 |
'a_labels' => '', |
| 49 |
'hierarchy' => '', |
| 50 |
'used_for_variations' => '', |
| 51 |
'range_slider' => '', |
| 52 |
'step' => '', |
| 53 |
'search' => '', |
| 54 |
'parent_filter' => '', |
| 55 |
'hide_until_parent' => '', |
| 56 |
'min_num_label' => '', |
| 57 |
'max_num_label' => '', |
| 58 |
'tooltip' => '', |
| 59 |
'more_less' => '', |
| 60 |
'show_chips' => '', |
| 61 |
'acf_fields' => '', |
| 62 |
'has_child_filter' => '', |
| 63 |
'child_values' => '', |
| 64 |
) |
| 65 |
); |
| 66 |
} |
| 67 |
|
| 68 |
public function getEntityKey( $entity, $e_name = '' ) |
| 69 |
{ |
| 70 |
if ( $entity === 'post_date' ) { |
| 71 |
return $entity . $this->sep . $entity; |
| 72 |
} |
| 73 |
|
| 74 |
// Meta field or Tax numeric |
| 75 |
if ( $e_name ) { |
| 76 |
return $entity . $this->sep . $e_name; |
| 77 |
} |
| 78 |
|
| 79 |
// Replace first "_" with $this->sep in taxonomy entity |
| 80 |
if ( mb_strpos( $entity, '_' ) !== false ) { |
| 81 |
$_position = strpos( $entity, '_' ); |
| 82 |
return substr_replace( $entity, $this->sep, $_position, 1 ); |
| 83 |
} |
| 84 |
|
| 85 |
return $entity; |
| 86 |
} |
| 87 |
|
| 88 |
public function getEntityCanonicalName( $entity ) |
| 89 |
{ |
| 90 |
if( mb_strpos( $entity, 'post_meta' ) !== false || mb_strpos( $entity, 'tax_numeric' ) !== false || mb_strpos( $entity, 'post_date' ) !== false || mb_strpos( $entity, 'post_meta_date' ) !== false ){ |
| 91 |
$canonical = explode( $this->sep, $entity, 2 ); |
| 92 |
return $canonical[0]; |
| 93 |
} else { |
| 94 |
return str_replace( $this->sep, '_', $entity ); |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
public function getEntityFullName( $entity, $e_name ) |
| 100 |
{ |
| 101 |
if( ! empty( $e_name ) ){ |
| 102 |
return $entity . $this->sep . $e_name; |
| 103 |
} |
| 104 |
return $entity; |
| 105 |
} |
| 106 |
|
| 107 |
public function getEntityEname( $entityFullName ) |
| 108 |
{ |
| 109 |
if( mb_strpos( $entityFullName, $this->sep ) !== false ){ |
| 110 |
$pieces = explode( $this->sep, $entityFullName, 2 ); |
| 111 |
return $pieces[1]; |
| 112 |
} |
| 113 |
|
| 114 |
return $entityFullName; |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Determines whether to modify or not e_name for a filter |
| 119 |
* @param $filter array with filter properties |
| 120 |
* @return bool |
| 121 |
*/ |
| 122 |
public function needEntityToModifyEname( $filter ) |
| 123 |
{ |
| 124 |
// taxonomy_pa_size |
| 125 |
// taxonomy_pa_color |
| 126 |
// author_author || author |
| 127 |
// tax_numeric |
| 128 |
if( mb_strpos( $filter['entity'], 'taxonomy' ) === false |
| 129 |
&& |
| 130 |
mb_strpos( $filter['entity'], 'author' ) === false |
| 131 |
&& |
| 132 |
! in_array( $filter['entity'], [ 'tax_numeric' , 'post_date', 'post_meta_date' ] ) ){ |
| 133 |
return false; |
| 134 |
} |
| 135 |
return true; |
| 136 |
} |
| 137 |
|
| 138 |
public function splitEntityFullNameInFilter( $filter ) |
| 139 |
{ |
| 140 |
if ( ! $this->needEntityToModifyEname( $filter ) ) { |
| 141 |
return $filter; |
| 142 |
} |
| 143 |
|
| 144 |
if ( mb_strpos( $filter['entity'], 'taxonomy' ) !== false ) { |
| 145 |
$filter['e_name'] = mb_strcut( $filter['entity'], strlen( 'taxonomy_' ) ); |
| 146 |
$filter['entity'] = 'taxonomy'; |
| 147 |
return $filter; |
| 148 |
} |
| 149 |
|
| 150 |
if ( mb_strpos( $filter['entity'], 'author' ) !== false ) { |
| 151 |
$filter['e_name'] = 'author'; |
| 152 |
$filter['entity'] = 'author'; |
| 153 |
return $filter; |
| 154 |
} |
| 155 |
|
| 156 |
if ( mb_strpos( $filter['entity'], 'post_date' ) !== false ) { |
| 157 |
$filter['e_name'] = 'post_date'; |
| 158 |
$filter['entity'] = 'post_date'; |
| 159 |
return $filter; |
| 160 |
} |
| 161 |
|
| 162 |
return $filter; |
| 163 |
} |
| 164 |
|
| 165 |
public function combineEntityNameInFilter( $filter ) |
| 166 |
{ |
| 167 |
|
| 168 |
if( ! $this->needEntityToModifyEname( $filter ) ){ |
| 169 |
return $filter; |
| 170 |
} |
| 171 |
|
| 172 |
if( mb_strpos( $filter['entity'], 'taxonomy' ) !== false ){ |
| 173 |
$filter['entity'] = $filter['entity'] . '_' . $filter['e_name']; |
| 174 |
$filter['e_name'] = ''; |
| 175 |
return $filter; |
| 176 |
} |
| 177 |
|
| 178 |
if( mb_strpos( $filter['entity'], 'author' ) !== false ){ |
| 179 |
$filter['entity'] = 'author_author'; |
| 180 |
$filter['e_name'] = ''; |
| 181 |
return $filter; |
| 182 |
} |
| 183 |
|
| 184 |
return $filter; |
| 185 |
} |
| 186 |
|
| 187 |
public function filterExists( $newFilter ){ |
| 188 |
foreach ( $this->filters as $filter ) { |
| 189 |
if( $filter['e_name'] === $newFilter['e_name'] ){ |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
if( $filter['slug'] === $newFilter['slug'] ){ |
| 194 |
return true; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
return false; |
| 199 |
} |
| 200 |
|
| 201 |
public function isFilterInPath( $filter ){ |
| 202 |
if( $filter['in_path'] === 'yes' ){ |
| 203 |
return true; |
| 204 |
} |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
public function isFilterInQueryString( $filter ){ |
| 209 |
if( $filter['in_path'] === 'no' ){ |
| 210 |
return true; |
| 211 |
} |
| 212 |
return false; |
| 213 |
} |
| 214 |
|
| 215 |
public function getFiltersOrder() |
| 216 |
{ |
| 217 |
$permalinksTab = new PermalinksTab(); |
| 218 |
return array_keys( get_option( $permalinksTab->optionName, [] ) ); |
| 219 |
} |
| 220 |
|
| 221 |
public function sortTerms( $terms = [] ){ |
| 222 |
asort( $terms ); |
| 223 |
return $terms; |
| 224 |
} |
| 225 |
|
| 226 |
public function getLogicSeparator( $logic ){ |
| 227 |
return $this->logicSeparators[ $logic ]; |
| 228 |
} |
| 229 |
|
| 230 |
} |