PluginProbe
Brizy – Page Builder / trunk
Brizy – Page Builder vtrunk
2.8.23 2.8.22 2.8.21 2.8.20 2.8.19 2.8.18 2.8.17 2.8.16 2.8.15 2.8.14 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.15 2.7.16 All 188 releases
brizy / admin / rule.php

rule.php in Brizy – Page Builder trunk, at admin/rule.php

515 lines 11.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Brizy_Admin_Rule extends Brizy_Admin_Serializable implements Brizy_Admin_RuleInterface {
4
5 const MODE_UNKNOWN = null;
6 const MODE_REFERENCE = 'reference';
7 const MODE_SPECIFIC = 'specific';
8
9 const TYPE_INCLUDE = 1;
10 const TYPE_EXCLUDE = 2;
11
12 const ALL = 0;
13 const POSTS = 1;
14 const TAXONOMY = 2;
15 const ARCHIVE = 4;
16 const TEMPLATE = 8; // author/search/home_page...
17 const BRIZY_TEMPLATE = 16; // brizy template
18 const WOO_SHOP_PAGE = 256;
19 const DATE_ARCHIVE = self::ARCHIVE | 512;
20 const YEAR_ARCHIVE = self::DATE_ARCHIVE | 1024;
21 const MONTH_ARCHIVE = self::DATE_ARCHIVE | 2048;
22 const DAY_ARCHIVE = self::DATE_ARCHIVE | 4096;
23
24 /**
25 * @var int
26 */
27 protected $id;
28
29 /**
30 * @var int
31 */
32 protected $type;
33
34 /**
35 * @var int
36 */
37 protected $appliedFor;
38
39 /**
40 * @var string
41 */
42 protected $entityType;
43
44 /**
45 * @var string
46 */
47 protected $mode;
48
49 /**
50 * If null the rule will be applied on all entities
51 *
52 * @var int[]
53 */
54 protected $entityValues = array();
55
56 /**
57 * @return array|mixed
58 */
59 public function jsonSerialize() {
60 return $this->convertToOptionValue();
61 }
62
63 /**
64 * Brizy_Admin_Rule constructor.
65 *
66 * @param int $id
67 * @param int $type
68 * @param int $applied_for
69 * @param int $entity_type
70 * @param array $entities
71 */
72 public function __construct( $id, $type, $applied_for, $entity_type, $entities ) {
73
74 if ( ! $id ) {
75 $this->setId( $this->generateId( $type, $applied_for, $entity_type, $this->getEntitiesAsString() ) );
76 } else {
77 $this->setId( $id );
78 }
79
80 $this->setType( $type );
81 $this->setAppliedFor( $applied_for );
82 $this->setEntityType( $entity_type );
83 $this->setEntityValues( array_filter( (array) $entities, array( $this, 'filter' ) ) );
84 }
85
86 function filter( $v ) {
87 return ! empty( $v );
88 }
89
90 /**
91 * Return true if the rule matches for the given parameters
92 *
93 * @param $applyFor
94 * @param $entityType
95 * @param $entityValues
96 *
97 * @return bool
98 */
99 public function isMatching( $applyFor, $entityType, $entityValues ) {
100
101 $ruleValues = array(
102 $this->getType(),
103 $this->getAppliedFor(),
104 $this->getEntityType(),
105 $this->getEntityValues(),
106 );
107
108 $checkValues = array(
109 self::TYPE_INCLUDE, // include
110 $applyFor,
111 $entityType,
112 $entityValues,
113 );
114
115
116 // return if the rule is for exclude
117 // if($ruleValues[0]!=$checkValues[0]) {
118 // return false;
119 // }
120
121 $entity_values = $this->getEntityValues();
122
123 // check is the rule is matching all
124 if ( $ruleValues[0] == self::TYPE_INCLUDE && ! $ruleValues[1] ) {
125 return true;
126 }
127
128 // exception for home page that has two behaviors.. as page and as a template
129 if ( $applyFor == self::TEMPLATE &&
130 $entityType == 'front_page' &&
131 $this->getAppliedFor() == self::POSTS &&
132 $this->getEntityType() == 'page' &&
133 isset( $entity_values[0] ) &&
134 $entity_values[0] == get_option( 'page_on_front' ) ) {
135 return true;
136 }
137
138 // check post author
139 if ( isset( $entity_values[0] ) && isset( $entityValues[0] ) && ( $values = explode( '|', $entity_values[0] ) ) && count( $values ) == 2 && $this->getEntityType() == $entityType ) {
140 if ( $values[0] === 'author' ) {
141 if($values[1]=='')
142 return true;
143
144 return get_post_field( 'post_author', $entityValues[0] ) == $values[1];
145 }
146
147 if ( $applyFor == self::POSTS && $this->getAppliedFor() == self::POSTS && $values[0] === 'in' ) {
148 $postTerms = wp_get_post_terms($entityValues[0],$values[1]);
149 $postTermIds = array_map(function($t){return $t->term_id;},$postTerms);
150 $allTerms = array_map(function($t){return $t->term_id;},get_terms(['taxonomy'=>$values[1] ]));;
151 return count(array_intersect($postTermIds,$allTerms));
152 }
153 }
154
155 // check if post is in a term
156 if ( isset( $entity_values[0] ) && ( $values = explode( '|', $entity_values[0] ) ) && count( $values ) == 3 && isset($entityValues[0]) && $this->getEntityType() == $entityType ) {
157
158 // POSTS
159 if ( $applyFor == self::POSTS && $this->getAppliedFor() == self::POSTS && $values[0] === 'in' ) {
160 // check if the post is in taxonomy with name $values[0] and with id $values[1]
161 return has_term( $values[2], $values[1], $entityValues[0] );
162 }
163
164 // check if post is in a term
165 if ( $applyFor == self::POSTS && $this->getAppliedFor() == self::POSTS && $values[0] === 'child' ) {
166 // check if the post is in taxonomy with name $values[0] and with id $values[1]
167 $tax = get_term_children( $values[2], $values[1] );
168
169 foreach ( $tax as $t ) {
170 if ( has_term( $t, $values[1], $entityValues[0] ) ) {
171 return true;
172 }
173 }
174
175 return false;
176 }
177
178 // TAXONOMY
179 if ( $applyFor == self::TAXONOMY && $this->getAppliedFor() == self::TAXONOMY && $values[0] === 'in' ) {
180 return $entityValues[0] == $values[2];
181 }
182
183 // check if terms is child of a term
184 if ( $applyFor == self::TAXONOMY && $this->getAppliedFor() == self::TAXONOMY && $values[0] === 'child' ) {
185 $tax = get_term_children( $values[2], $values[1] );
186
187 return in_array( $entityValues[0], $tax );
188 }
189 }
190
191 // matching archive rules
192 if ( isset( $ruleValues[1] ) && isset( $checkValues[1] ) && ( $ruleValues[1] & self::ARCHIVE ) && ( $checkValues[1] & self::ARCHIVE ) ) {
193
194 if ( $checkValues[1] == $ruleValues[1] ||
195 $ruleValues[1] == self::DATE_ARCHIVE ||
196 $ruleValues[1] == self::YEAR_ARCHIVE ||
197 $ruleValues[1] == self::MONTH_ARCHIVE ||
198 $ruleValues[1] == self::DAY_ARCHIVE ||
199 $ruleValues[1] == self::ARCHIVE ) {
200 if ( empty( $ruleValues[2] ) || $ruleValues[2] == $entityType ) {
201 return true;
202 }
203 }
204
205
206 }
207
208 foreach ( $ruleValues as $i => $value ) {
209
210 if ( $i === 0 ) {
211 continue;
212 }
213
214 if ( is_array( $value ) ) {
215 // this means that the rule accept any value
216 if ( count( $ruleValues[ $i ] ) == 0 ) {
217 break;
218 }
219
220 if(count($checkValues[ $i ])==0 && count($ruleValues[ $i ])!=0) {
221 return false;
222 }
223
224 // check if the value is contained in this rule
225 if ( count( array_diff( $checkValues[ $i ], $ruleValues[ $i ] ) ) != 0 ) {
226 return false;
227 }
228
229 } else {
230 if ( $ruleValues[ $i ] != $checkValues[ $i ] ) {
231 return false;
232 }
233 }
234 }
235
236 return true;
237 }
238
239 /**
240 * @param Brizy_Admin_Rule $rule
241 *
242 * @return bool
243 */
244 public function isEqual( $rule ) {
245 return $this->getType() == $rule->getType() &&
246 $this->getAppliedFor() == $rule->getAppliedFor() &&
247 $this->getEntityType() == $rule->getEntityType() &&
248 ( count( $rule->getEntityValues() ) == count( $this->getEntityValues() ) &&
249 count( array_diff( $rule->getEntityValues(), $this->getEntityValues() ) ) == 0 );
250 }
251
252 /**
253 * @return int
254 */
255 public function getId() {
256 return $this->id;
257 }
258
259 /**
260 * @param int $id
261 *
262 * @return Brizy_Admin_Rule
263 */
264 public function setId( $id ) {
265
266 $this->id = $id;
267
268 return $this;
269 }
270
271 /**
272 * @return int
273 */
274 public function getType() {
275 return (int) $this->type;
276 }
277
278 /**
279 * @param int $type
280 *
281 * @return Brizy_Admin_Rule
282 */
283 public function setType( $type ) {
284 $this->type = (int) $type;
285
286 return $this;
287 }
288
289 /**
290 * @return int
291 */
292 public function getAppliedFor() {
293 return $this->appliedFor;
294 }
295
296 /**
297 * @param int $appliedFor
298 *
299 * @return Brizy_Admin_Rule
300 */
301 public function setAppliedFor( $appliedFor ) {
302 if ( $val = (int) $appliedFor ) {
303 $this->appliedFor = $val;
304 }
305
306 return $this;
307 }
308
309 /**
310 * @return string
311 */
312 public function getEntityType() {
313 return $this->entityType;
314 }
315
316 /**
317 * @param string $entityType
318 *
319 * @return Brizy_Admin_Rule
320 */
321 public function setEntityType( $entityType ) {
322 $this->entityType = $entityType;
323
324 return $this;
325 }
326
327 /**
328 * @return string[]
329 */
330 public function getEntityValues() {
331 return array_map( function ( $v ) {
332 if ( is_numeric( $v ) ) {
333 return (int) $v;
334 } else {
335 return $v;
336 }
337 }, $this->entityValues );
338 }
339
340 /**
341 * @param int[] $entityValues
342 *
343 * @return Brizy_Admin_Rule
344 */
345 public function setEntityValues( $entityValues ) {
346
347 if ( ! is_array( $entityValues ) ) {
348 throw new InvalidArgumentException();
349 }
350
351 $this->entityValues = array_values( $entityValues );
352
353 return $this;
354 }
355
356 /**
357 * @return array
358 */
359 public function convertToOptionValue() {
360 return array(
361 'id' => $this->getId(),
362 'type' => $this->getType(),
363 'appliedFor' => $this->getAppliedFor(),
364 'entityType' => $this->getEntityType(),
365 'entityValues' => $this->getEntityValues(),
366 'mode' => $this->getRuleMode()
367 );
368 }
369
370
371 public function getRuleWeight( $context ) {
372
373 $weight = 0;
374
375 if ( $this->getAppliedFor() == self::TEMPLATE && $this->getEntityType() == 'front_page' ) {
376 $weight += 20;
377 }
378 if ( $this->getAppliedFor() == self::TEMPLATE ) {
379 $weight += 10;
380 }
381
382 if ( $this->getEntityType() === 'product' ) {
383 $weight += 30;
384 }
385
386 $values = array();
387
388 if ( $this->getType() ) {
389 $values[] = $weight = $this->getType();
390 }
391 if ( $this->getAppliedFor() ) {
392 $values[] = $this->getAppliedFor();
393 }
394 if ( $this->getEntityType() ) {
395 $values[] = $this->getEntityType();
396 }
397 if ( count( $this->getEntityValues() ) > 0 ) {
398 $values[] = $this->getEntityType();
399 }
400
401 $weight += count( $values );
402
403 if ( isset( $context['type'] ) && $this->getAppliedFor() === $context['type'] ) {
404 $weight += 1;
405 }
406
407 if ( isset( $context['entityType'] ) && $this->getEntityType() === $context['entityType'] ) {
408 $weight += 1;
409 }
410
411 if ( isset( $context['entityValues'] ) && $intersection = count(
412 array_intersect( $context['entityValues'], $this->getEntityValues() )
413 ) ) {
414 $weight += $intersection;
415 }
416
417 return $weight;
418 }
419
420 /**
421 * @param $data
422 *
423 * @return Brizy_Admin_Rule|void
424 * @throws Exception
425 */
426 static public function createFromSerializedData( $data ) {
427
428 if ( is_null( $data ) ) {
429 throw new Exception( 'Invalid parameter provided' );
430 }
431
432 return new self(
433 isset( $data['id'] ) ? $data['id'] : null,
434 isset( $data['type'] ) && ! empty( $data['type'] ) ? $data['type'] : null,
435 isset( $data['appliedFor'] ) && ! empty( $data['appliedFor'] ) ? $data['appliedFor'] : null,
436 isset( $data['entityType'] ) ? $data['entityType'] : null,
437 isset( $data['entityValues'] ) ? $data['entityValues'] : null
438 );
439 }
440
441 /**
442 * @param $data
443 *
444 * @return Brizy_Admin_Rule
445 * @throws Exception
446 */
447 static public function createFromRequestData( $data ) {
448
449 if ( is_null( $data ) ) {
450 throw new Exception( 'Invalid parameter provided' );
451 }
452
453 return new self(
454 isset( $data['id'] ) ? $data['id'] : null,
455 isset( $data['type'] ) ? $data['type'] : null,
456 isset( $data['appliedFor'] ) ? $data['appliedFor'] : null,
457 isset( $data['entityType'] ) ? $data['entityType'] : null,
458 isset( $data['entityValues'] ) ? $data['entityValues'] : null
459 );
460 }
461
462 static public function createFromJsonObject( $json ) {
463
464 if ( is_null( $json ) ) {
465 throw new Exception( 'Invalid parameter provided' );
466 }
467
468 return new self(
469 isset( $json->id ) ? $json->id : null,
470 isset( $json->type ) ? $json->type : null,
471 isset( $json->appliedFor ) ? $json->appliedFor : null,
472 isset( $json->entityType ) ? $json->entityType : null,
473 isset( $json->entityValues ) ? $json->entityValues : null
474 );
475 }
476
477 /**
478 * @return string
479 */
480 public function serialize() {
481 return serialize( $this->convertToOptionValue() );
482 }
483
484
485 /**
486 * @param string $delimited
487 *
488 * @return string
489 */
490 public function getEntitiesAsString( $delimited = ',' ) {
491 return implode( $delimited, $this->getEntityValues() );
492 }
493
494 /**
495 * @return string
496 */
497 private function generateId() {
498 return md5( implode( '', func_get_args() ) . rand( 1, time() ) );
499 }
500
501 private function getRuleMode() {
502 foreach ( $this->entityValues as $item ) {
503 if ( strpos( $item, "author" ) === 0 ||
504 strpos( $item, "in" ) === 0 ||
505 strpos( $item, "child" ) === 0 ) {
506 return self::MODE_REFERENCE;
507 } else {
508 return self::MODE_SPECIFIC;
509 }
510 }
511
512 return self::MODE_UNKNOWN;
513 }
514 }
515