| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Meta_Box\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Get conditions |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
* |
| 17 |
* @return array |
| 18 |
*/ |
| 19 |
function gamipress_meta_box_get_value_conditions() { |
| 20 |
|
| 21 |
return array( |
| 22 |
// String |
| 23 |
'equal' => __( 'is equal to', 'gamipress' ), |
| 24 |
'not_equal' => __( 'is not equal to', 'gamipress' ), |
| 25 |
'contains' => __( 'contains', 'gamipress' ), |
| 26 |
'not_contains' => __( 'does not contains', 'gamipress' ), |
| 27 |
'start_with' => __( 'starts with', 'gamipress' ), |
| 28 |
'not_start_with' => __( 'does not starts with', 'gamipress' ), |
| 29 |
'ends_with' => __( 'ends with', 'gamipress' ), |
| 30 |
'not_ends_with' => __( 'does not ends with', 'gamipress' ), |
| 31 |
// Number |
| 32 |
'less_than' => __( 'is less than', 'gamipress' ), |
| 33 |
'greater_than' => __( 'is greater than', 'gamipress' ), |
| 34 |
'less_or_equal' => __( 'is less or equal to', 'gamipress' ), |
| 35 |
'greater_or_equal' => __( 'is greater or equal to', 'gamipress' ), |
| 36 |
); |
| 37 |
|
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Utility function to get the condition option parameter |
| 42 |
* |
| 43 |
* @since 1.4.5 |
| 44 |
* |
| 45 |
* @param mixed $a Element to match |
| 46 |
* @param mixed $b Element to compare |
| 47 |
* @param string $condition The condition to compare elements |
| 48 |
* |
| 49 |
* @return bool |
| 50 |
*/ |
| 51 |
function gamipress_meta_box_condition_matches( $a, $b, $condition ) { |
| 52 |
|
| 53 |
if( empty( $condition ) ) { |
| 54 |
$condition = 'equal'; |
| 55 |
} |
| 56 |
|
| 57 |
$matches = false; |
| 58 |
|
| 59 |
// Ensure that the element to compare is a string |
| 60 |
if( is_array( $b ) ) { |
| 61 |
$b = implode( ',', $b ); |
| 62 |
} |
| 63 |
|
| 64 |
$a = strval( $a ); |
| 65 |
$b = strval( $b ); |
| 66 |
|
| 67 |
// If not is a string condition and elements to compare are numerics, turn them to float |
| 68 |
if( ! gamipress_meta_box_is_string_condition( $condition ) ) { |
| 69 |
if( is_numeric( $a ) ) { |
| 70 |
$a = (float) $a; |
| 71 |
} |
| 72 |
|
| 73 |
if( is_numeric( $b ) ) { |
| 74 |
$b = (float) $b; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
switch( $condition ) { |
| 79 |
case 'equal': |
| 80 |
case '=': |
| 81 |
case '==': |
| 82 |
case '===': |
| 83 |
$matches = ( $a == $b ); |
| 84 |
break; |
| 85 |
case 'not_equal': |
| 86 |
case '!=': |
| 87 |
case '!==': |
| 88 |
$matches = ( $a != $b ); |
| 89 |
break; |
| 90 |
case 'less_than': |
| 91 |
case '<': |
| 92 |
$matches = ( $a < $b ); |
| 93 |
break; |
| 94 |
case 'greater_than': |
| 95 |
case '>': |
| 96 |
$matches = ( $a > $b ); |
| 97 |
break; |
| 98 |
case 'less_or_equal': |
| 99 |
case '<=': |
| 100 |
$matches = ( $a <= $b ); |
| 101 |
break; |
| 102 |
case 'greater_or_equal': |
| 103 |
case '>=': |
| 104 |
$matches = ( $a >= $b ); |
| 105 |
break; |
| 106 |
case 'contains': |
| 107 |
$matches = ( strpos( $a, strval( $b ) ) !== false ); |
| 108 |
break; |
| 109 |
case 'not_contains': |
| 110 |
$matches = ( strpos( $a, strval( $b ) ) === false ); |
| 111 |
break; |
| 112 |
case 'start_with': |
| 113 |
$matches = ( gamipress_starts_with( $a, $b ) ); |
| 114 |
break; |
| 115 |
case 'not_start_with': |
| 116 |
$matches = ( ! gamipress_starts_with( $a, $b ) ); |
| 117 |
break; |
| 118 |
case 'ends_with': |
| 119 |
$matches = ( gamipress_ends_with( $a, $b ) ); |
| 120 |
break; |
| 121 |
case 'not_ends_with': |
| 122 |
$matches = ( ! gamipress_ends_with( $a, $b ) ); |
| 123 |
break; |
| 124 |
} |
| 125 |
|
| 126 |
return $matches; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Utility function to meet if condition is related to string |
| 132 |
* |
| 133 |
* @since 1.7.6 |
| 134 |
* |
| 135 |
* @param string $condition The condition to check |
| 136 |
* |
| 137 |
* @return bool |
| 138 |
*/ |
| 139 |
function gamipress_meta_box_is_string_condition( $condition ) { |
| 140 |
|
| 141 |
$return = false; |
| 142 |
|
| 143 |
switch( $condition ) { |
| 144 |
case 'contains': |
| 145 |
case 'not_contains': |
| 146 |
case 'start_with': |
| 147 |
case 'not_start_with': |
| 148 |
case 'ends_with': |
| 149 |
case 'not_ends_with': |
| 150 |
$return = true; |
| 151 |
break; |
| 152 |
} |
| 153 |
|
| 154 |
return $return; |
| 155 |
|
| 156 |
} |