| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace FilterEverything\Filter; |
| 5 |
|
| 6 |
if ( ! defined('ABSPATH') ) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
class Validator |
| 11 |
{ |
| 12 |
|
| 13 |
private $fse; |
| 14 |
|
| 15 |
private $em; |
| 16 |
|
| 17 |
public function __construct() |
| 18 |
{ |
| 19 |
$this->em = Container::instance()->getEntityManager(); |
| 20 |
$this->fse = Container::instance()->getFilterService(); |
| 21 |
} |
| 22 |
|
| 23 |
public function validateEmptyPrefix( $prefix, $prefixesList, $prefixEntityKey ) |
| 24 |
{ |
| 25 |
if( empty( $prefix ) ){ |
| 26 |
if( ! isset( $prefixesList[ $prefixEntityKey ] ) ){ |
| 27 |
return false; |
| 28 |
} |
| 29 |
} |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
public function validateExistingPrefix( $prefix, $prefixesList, $prefixEntityKey ) |
| 34 |
{ |
| 35 |
if( $prefixesList ){ |
| 36 |
if( $entityKey = array_search( $prefix, $prefixesList ) ){ |
| 37 |
if( $prefixEntityKey !== $entityKey ){ |
| 38 |
return false; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
public function validateAlphabCharsExists($prefix ) |
| 47 |
{ |
| 48 |
if( ! preg_match('/[a-z]/i', $prefix ) ) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
return true; |
| 53 |
} |
| 54 |
// @todo fix bug, when $prefixesList is not array |
| 55 |
public function validatePrefixHyphens( $prefix, $prefixesList ) |
| 56 |
{ |
| 57 |
if( strpos( $prefix, '-' ) !== false ){ |
| 58 |
if( is_array( $prefixesList ) ){ |
| 59 |
$pieces = explode('-', $prefix, 2 ); |
| 60 |
$firstPart = $pieces[0]; |
| 61 |
|
| 62 |
if( $exist = array_search( $firstPart, $prefixesList ) ){ |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
return true; |
| 69 |
} |
| 70 |
|
| 71 |
public function validateAllowedPrefixes( $prefix, $filter ) |
| 72 |
{ |
| 73 |
/** |
| 74 |
* @todo check forbidden prefixes only of GET parameters. |
| 75 |
* we need totally forbidden prefixes list and list only for GET parameters |
| 76 |
*/ |
| 77 |
$prefix = trim($prefix); |
| 78 |
$taxonomyNames = get_taxonomies( [], 'names'); |
| 79 |
|
| 80 |
foreach ( $taxonomyNames as $name ){ |
| 81 |
if( mb_strpos( $name, $prefix . '-' ) !== false ){ |
| 82 |
return false; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
// Check for existing slug |
| 87 |
if( flrt_get_term_by_slug( $prefix ) ){ |
| 88 |
return false; |
| 89 |
} |
| 90 |
|
| 91 |
$forbiddenPrefixes = flrt_get_forbidden_prefixes(); |
| 92 |
|
| 93 |
if( in_array( $prefix, $forbiddenPrefixes ) ){ |
| 94 |
return false; |
| 95 |
} |
| 96 |
|
| 97 |
return true; |
| 98 |
} |
| 99 |
|
| 100 |
public function validateDuplicates( $prefixesList ) |
| 101 |
{ |
| 102 |
return ( ! flrt_array_contains_duplicate( $prefixesList ) ); |
| 103 |
} |
| 104 |
|
| 105 |
public function validateEscAttrCharacters($string ) |
| 106 |
{ |
| 107 |
if( $string !== esc_attr( $string ) ){ |
| 108 |
return false; |
| 109 |
} |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
public function validatePossibleEntity( $entity ) |
| 114 |
{ |
| 115 |
$entities = $this->em->getPossibleEntities(); |
| 116 |
$flatEntities = array_keys( $this->em->getFlatEntities( $entities ) ); |
| 117 |
|
| 118 |
if ( ! in_array( $entity, $flatEntities, true ) ) { |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
|
| 125 |
public function validateExcludeTerms( $terms, $filter ) |
| 126 |
{ |
| 127 |
$entityTermIds = []; |
| 128 |
$_filter = $this->fse->splitEntityFullNameInFilter( $filter ); |
| 129 |
$entity = $this->em->getEntityByFilter( $_filter ); |
| 130 |
|
| 131 |
if( $entity ){ |
| 132 |
$entityTermIds = array_keys( $entity->getTermsForSelect() ); |
| 133 |
} |
| 134 |
|
| 135 |
foreach( (array) $terms as $termId ) { |
| 136 |
|
| 137 |
if ( ! in_array( $termId, $entityTermIds ) ){ |
| 138 |
return false; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
public function validateView( $filter, $viewOptions ) |
| 146 |
{ |
| 147 |
if( ! in_array( $filter['view'], $viewOptions, true ) ){ |
| 148 |
return false; |
| 149 |
} |
| 150 |
|
| 151 |
if( $filter['view'] === 'range' && ! in_array( $filter['entity'], [ 'post_meta_num', 'tax_numeric' ] ) ){ |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return true; |
| 156 |
} |
| 157 |
} |