| 1 |
<?php |
| 2 |
|
| 3 |
namespace FilterEverything\Filter; |
| 4 |
|
| 5 |
if ( ! defined('ABSPATH') ) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class UrlManager |
| 10 |
{ |
| 11 |
private $separator; |
| 12 |
|
| 13 |
private $em; |
| 14 |
|
| 15 |
private $wpManager; |
| 16 |
|
| 17 |
private $entityAndEnamesOrder; |
| 18 |
|
| 19 |
private $actualFilters; |
| 20 |
|
| 21 |
private $enameActualFilters; |
| 22 |
|
| 23 |
private $resetUrl; |
| 24 |
|
| 25 |
private $logicParam = 'logic'; // Should be an option |
| 26 |
|
| 27 |
private $valuesSeparator = FLRT_QUERY_TERMS_SEPARATOR; // Should be an option |
| 28 |
|
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
$this->separator = FLRT_PREFIX_SEPARATOR; |
| 32 |
$fse = Container::instance()->getFilterService(); |
| 33 |
$this->em = Container::instance()->getEntityManager(); |
| 34 |
$this->wpManager = Container::instance()->getWpManager(); |
| 35 |
$this->entityAndEnamesOrder = $fse->getFiltersOrder(); |
| 36 |
$this->actualFilters = $this->em->getSetsRelatedFilters(); |
| 37 |
|
| 38 |
// E_name actual filters |
| 39 |
foreach ( $this->actualFilters as $actualFilter ) { |
| 40 |
$this->enameActualFilters[ $actualFilter['entity'].'#'.$actualFilter['e_name'] ] = $actualFilter; |
| 41 |
} |
| 42 |
|
| 43 |
unset( $fse ); |
| 44 |
} |
| 45 |
|
| 46 |
public function getTermUrl( $termSlug = '', $e_name = '', $entity = '', $resetUrl = '', $exclude = [] ) |
| 47 |
{ |
| 48 |
$fse = Container::instance()->getFilterService(); |
| 49 |
$e_nameActualFilters = $this->enameActualFilters; |
| 50 |
$url = $baseUrl = $resetUrl ? $resetUrl : $this->getResetUrl(); |
| 51 |
$parts = []; |
| 52 |
$entity_and_e_name = $entity.'#'.$e_name; |
| 53 |
|
| 54 |
if ( empty( $e_nameActualFilters ) ) { |
| 55 |
$the_filter = $this->getSingleActualFilter( $e_name ); |
| 56 |
if( isset( $the_filter['entity'] ) && isset( $the_filter['e_name'] ) ) { |
| 57 |
$entity_and_e_name = $the_filter['entity'].'#'.$the_filter['e_name']; |
| 58 |
$e_nameActualFilters[$entity_and_e_name] = $the_filter; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
foreach( $this->entityAndEnamesOrder as $entityAndEName ) { |
| 63 |
|
| 64 |
if ( isset( $e_nameActualFilters[$entityAndEName] ) ) { |
| 65 |
|
| 66 |
$filter = $e_nameActualFilters[$entityAndEName]; |
| 67 |
|
| 68 |
if ( empty( $filter['values'] ) && $entity_and_e_name !== $entityAndEName ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
if( $entity_and_e_name === $entityAndEName ){ |
| 73 |
$queriedValues = $filter['values']; |
| 74 |
// Add only single filter value for views with single selection |
| 75 |
if( in_array( $filter['view'], array('dropdown', 'radio') ) ){ |
| 76 |
if( in_array( $termSlug, $queriedValues ) ){ |
| 77 |
$position = array_search( $termSlug, $queriedValues ); |
| 78 |
unset( $queriedValues[$position] ); |
| 79 |
}else{ |
| 80 |
$queriedValues = array( $termSlug ); |
| 81 |
} |
| 82 |
} else { |
| 83 |
// For Post Meta Num values have array index as termslug |
| 84 |
if ( in_array( $filter['entity'], [ 'post_meta_num', 'tax_numeric', 'post_date', 'post_meta_date' ] ) ) { |
| 85 |
if ( in_array( $termSlug, array_keys( $queriedValues ) ) ) { |
| 86 |
unset( $queriedValues[$termSlug] ); |
| 87 |
} else { |
| 88 |
$queriedValues[] = $termSlug; |
| 89 |
} |
| 90 |
} else if ( in_array( $filter['view'], array('rating') ) ) { |
| 91 |
if(isset($filter['selected_and_above']) && $filter['selected_and_above'] === 'yes'){ |
| 92 |
if(isset($exclude['rating_slug'])){ |
| 93 |
foreach ($queriedValues as $key => $value){ |
| 94 |
unset( $queriedValues[$key] ); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
if(!isset($exclude['rating_slug'])){ |
| 99 |
$pieces = explode("-", $termSlug); |
| 100 |
$rating = isset( $pieces[1] ) ? $pieces[1] : 0; |
| 101 |
$i = 5; |
| 102 |
while ($i >= 1) { |
| 103 |
$new_termSlug = 'rated-' . $i; |
| 104 |
if(in_array( $new_termSlug, $queriedValues )){ |
| 105 |
$position = array_search( $new_termSlug, $queriedValues ); |
| 106 |
unset( $queriedValues[$position] ); |
| 107 |
} |
| 108 |
|
| 109 |
if($i >= $rating){ |
| 110 |
$queriedValues[] = $new_termSlug; |
| 111 |
} |
| 112 |
$i--; |
| 113 |
} |
| 114 |
} |
| 115 |
} else if(in_array( $termSlug, $queriedValues ) && isset($filter['selected_and_above']) && $filter['selected_and_above'] === 'no'){ |
| 116 |
$position = array_search( $termSlug, $queriedValues ); |
| 117 |
unset( $queriedValues[$position] ); |
| 118 |
}else{ |
| 119 |
$queriedValues = array( $termSlug ); |
| 120 |
} |
| 121 |
} else { |
| 122 |
if ( in_array( $termSlug, $queriedValues ) ) { |
| 123 |
$position = array_search( $termSlug, $queriedValues ); |
| 124 |
unset( $queriedValues[$position] ); |
| 125 |
} else { |
| 126 |
$queriedValues[] = $termSlug; |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
$filter['values'] = $queriedValues; |
| 131 |
} |
| 132 |
|
| 133 |
foreach ($filter['values'] as $val_id => $term_slug){ |
| 134 |
if(!empty($exclude[$term_slug]) && $exclude[$term_slug] === true){ |
| 135 |
unset($filter['values'][$val_id]); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if( ! empty( $filter['values'] ) ) { |
| 140 |
$filter['values'] = $fse->sortTerms( $filter['values'] ); |
| 141 |
|
| 142 |
if ( in_array( $filter['entity'], ['post_meta_num', 'tax_numeric'] ) ) { |
| 143 |
foreach ($filter['values'] as $edge => $value) { |
| 144 |
$paramName = $edge . '_' . $filter['slug']; |
| 145 |
$url = flrt_add_query_arg($this->getParamName($paramName), $value, $url); |
| 146 |
} |
| 147 |
} elseif ( in_array($filter['entity'], ['post_date', 'post_meta_date'] ) ) { |
| 148 |
foreach ($filter['values'] as $edge => $date_value) { |
| 149 |
$paramName = $filter['slug'] . '_' . $edge; |
| 150 |
$date_value = str_replace(' ', FLRT_DATE_TIME_SEPARATOR, $date_value); |
| 151 |
$url = flrt_add_query_arg($this->getParamName($paramName), $date_value, $url); |
| 152 |
} |
| 153 |
} else { |
| 154 |
if ( defined('FLRT_PERMALINKS_ENABLED') && FLRT_PERMALINKS_ENABLED ) { |
| 155 |
$parts[] = $this->buildFilterSegment( $filter['values'], $filter ); |
| 156 |
} else { |
| 157 |
$url = flrt_add_query_arg( $this->getParamName($filter['slug'] ), implode($this->getValuesSeparator(), $filter['values']), $url); |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// Assembly permalinks |
| 166 |
if ( defined('FLRT_PERMALINKS_ENABLED') && FLRT_PERMALINKS_ENABLED ) { |
| 167 |
$pieces = explode('?', $baseUrl); |
| 168 |
$path = $pieces[0]; |
| 169 |
$path = trailingslashit( $path ) . implode('/', $parts ); |
| 170 |
$path = user_trailingslashit( $path ); |
| 171 |
$url = str_replace( $pieces[0], $path, $url ); |
| 172 |
} |
| 173 |
|
| 174 |
if ( ! isset( $exclude['srch'] ) ){ |
| 175 |
$search = isset( $_GET['srch'] ) ? filter_input( INPUT_GET, 'srch', FILTER_SANITIZE_SPECIAL_CHARS ) : false; |
| 176 |
if ( $search ){ |
| 177 |
$url = flrt_add_query_arg( 'srch' , $search, $url ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return apply_filters( 'wpc_filter_term_url', $url ); |
| 182 |
} |
| 183 |
|
| 184 |
public function getFormActionOrFullPageUrl( $fullPageUrl = false ) |
| 185 |
{ |
| 186 |
$query = []; |
| 187 |
$homeUrl = parse_url(home_url()); |
| 188 |
$post = Container::instance()->getThePost(); |
| 189 |
|
| 190 |
if( isset( $post['flrt_ajax_link'] ) ){ |
| 191 |
$requestedUri = $post['flrt_ajax_link']; |
| 192 |
}else{ |
| 193 |
$requestedUri = $homeUrl['scheme'].'://'.$homeUrl['host']; |
| 194 |
if( isset( $homeUrl['port'] ) && $homeUrl['port'] ){ |
| 195 |
$requestedUri .= ":".$homeUrl['port']; |
| 196 |
} |
| 197 |
$requestedUri .= $_SERVER['REQUEST_URI']; |
| 198 |
} |
| 199 |
|
| 200 |
$pieces = parse_url( $requestedUri ); |
| 201 |
$fullPath = $pieces['scheme']."://".$pieces['host']; |
| 202 |
if( isset( $pieces['port'] ) && $pieces['port'] ){ |
| 203 |
$fullPath .= ":".$pieces['port']; |
| 204 |
} |
| 205 |
$fullPath .= $pieces['path']; |
| 206 |
|
| 207 |
if( isset( $pieces['query'] ) ){ |
| 208 |
parse_str( $pieces['query'], $query ); |
| 209 |
} |
| 210 |
|
| 211 |
$formAction = $this->removePaginationBase( $fullPath ); |
| 212 |
|
| 213 |
// Add GET parameters |
| 214 |
if( $fullPageUrl && ! empty( $query ) ) { |
| 215 |
foreach ($query as $name => $value) { |
| 216 |
$formAction = flrt_add_query_arg( $name, $value, $formAction ); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return $formAction; |
| 221 |
} |
| 222 |
|
| 223 |
private function setCorrectGetKeys( $queried_values ) |
| 224 |
{ |
| 225 |
$getKeys = []; |
| 226 |
if( ! $queried_values || empty($queried_values) ){ |
| 227 |
return apply_filters( 'wpc_unnecessary_get_parameters', $getKeys ); |
| 228 |
} |
| 229 |
|
| 230 |
foreach ( $queried_values as $slug => $filter ){ |
| 231 |
if( in_array( $filter['entity'], [ 'post_meta_num', 'tax_numeric' ] ) ) { |
| 232 |
$getKeys['max_' . $slug] = $filter; |
| 233 |
$getKeys['min_' . $slug] = $filter; |
| 234 |
} else if( in_array( $filter['entity'], [ 'post_date', 'post_meta_date'] ) ) { |
| 235 |
$getKeys[$slug .'_from'] = $filter; |
| 236 |
$getKeys[$slug . '_to'] = $filter; |
| 237 |
}else{ |
| 238 |
$getKeys[$slug] = $filter; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
return apply_filters( 'wpc_unnecessary_get_parameters', $getKeys ); |
| 243 |
} |
| 244 |
|
| 245 |
public function getResetUrl() |
| 246 |
{ |
| 247 |
if( ! $this->resetUrl ) { |
| 248 |
$container = Container::instance(); |
| 249 |
$get = $container->getTheGet(); |
| 250 |
|
| 251 |
// For compatibility with some Nginx configurations |
| 252 |
unset($get['q']); |
| 253 |
|
| 254 |
$this->resetUrl = home_url( $this->wpManager->getQueryVar('wp_request', '') ); |
| 255 |
$requested = $this->wpManager->getQueryVar('queried_values', [] ); |
| 256 |
|
| 257 |
$this->resetUrl = $this->removePaginationBase( $this->resetUrl ); |
| 258 |
|
| 259 |
// Maybe add GET parameters |
| 260 |
$exclude_params = array_keys( $this->setCorrectGetKeys( $requested ) ); |
| 261 |
$exclude_params[] = 'srch'; |
| 262 |
|
| 263 |
if( ! empty( $get ) ){ |
| 264 |
foreach ( $get as $name => $value ) { |
| 265 |
if( ! in_array( $name, $exclude_params ) ){ |
| 266 |
$this->resetUrl = add_query_arg( $name, $value, $this->resetUrl ); |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return $this->resetUrl; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Removes '/page/n' from URL |
| 277 |
* @param $url |
| 278 |
* @return string |
| 279 |
*/ |
| 280 |
public function removePaginationBase( $url = '' ) |
| 281 |
{ |
| 282 |
global $wp_rewrite; |
| 283 |
|
| 284 |
$pagination_base = str_replace( "-", "\-", $wp_rewrite->pagination_base ); |
| 285 |
$comments_pagination_base = str_replace( "-", "\-", $wp_rewrite->comments_pagination_base ); |
| 286 |
|
| 287 |
$url = preg_replace('%\/'.$pagination_base.'/[0-9]+%', '', $url ); |
| 288 |
$url = preg_replace('%\/'.$comments_pagination_base.'\-[0-9]+%', '', $url ); |
| 289 |
$url = preg_replace('%\/[0-9]+[\/]?$%m', '', $url ); |
| 290 |
|
| 291 |
return apply_filters( 'wpc_remove_pagination_base', $url ); |
| 292 |
} |
| 293 |
|
| 294 |
public function getValuesSeparator() |
| 295 |
{ |
| 296 |
return $this->valuesSeparator; |
| 297 |
} |
| 298 |
|
| 299 |
public function getLogicParamName( $slug ) |
| 300 |
{ |
| 301 |
return $this->getParamName( $slug ).'_'.$this->logicParam; |
| 302 |
} |
| 303 |
|
| 304 |
public function getParamName( $slug ) |
| 305 |
{ |
| 306 |
return sanitize_title( $slug ); |
| 307 |
} |
| 308 |
|
| 309 |
private function getSingleActualFilter( $e_name ) |
| 310 |
{ |
| 311 |
$filter = $this->em->getFilterByEname( $e_name ); |
| 312 |
$filter['values'] = []; |
| 313 |
return $filter; |
| 314 |
} |
| 315 |
|
| 316 |
private function buildFilterSegment( $termSlugs, $filter ){ |
| 317 |
$segment = $filter['slug'] . $this->separator; |
| 318 |
$fse = Container::instance()->getFilterService(); |
| 319 |
|
| 320 |
if( ! is_array( $termSlugs ) ){ |
| 321 |
return false; |
| 322 |
} |
| 323 |
|
| 324 |
/** |
| 325 |
* @bug if filter is present in two sets logic gets incorrect |
| 326 |
*/ |
| 327 |
|
| 328 |
$termSlugs = $fse->sortTerms( $termSlugs ); |
| 329 |
$terms = implode( $fse->getLogicSeparator( $filter['logic'] ), $termSlugs ); |
| 330 |
$segment .= $terms; |
| 331 |
|
| 332 |
unset($fse); |
| 333 |
|
| 334 |
return $segment; |
| 335 |
} |
| 336 |
} |