| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 6.17 |
| 8 |
*/ |
| 9 |
class FrmApiHelper { |
| 10 |
|
| 11 |
/** |
| 12 |
* Check if an API item matches the current site license target. |
| 13 |
* |
| 14 |
* @since 6.17 |
| 15 |
* |
| 16 |
* @param array $item Inbox or Sale item. |
| 17 |
* |
| 18 |
* @return bool |
| 19 |
*/ |
| 20 |
public static function is_for_user( $item ) { |
| 21 |
if ( ! isset( $item['who'] ) || $item['who'] === 'all' ) { |
| 22 |
return true; |
| 23 |
} |
| 24 |
|
| 25 |
$who = (array) $item['who']; |
| 26 |
|
| 27 |
if ( self::is_for_everyone( $who ) ) { |
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
if ( self::is_user_type( $who ) ) { |
| 32 |
return true; |
| 33 |
} |
| 34 |
|
| 35 |
if ( in_array( 'free_first_30', $who, true ) && self::is_free_first_30() ) { |
| 36 |
return true; |
| 37 |
} |
| 38 |
|
| 39 |
if ( in_array( 'free_not_first_30', $who, true ) && self::is_free_not_first_30() ) { |
| 40 |
return true; |
| 41 |
} |
| 42 |
|
| 43 |
return self::check_free_segments( $who ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* @since 6.17 |
| 48 |
* |
| 49 |
* @param array $who |
| 50 |
* |
| 51 |
* @return bool |
| 52 |
*/ |
| 53 |
private static function is_for_everyone( $who ) { |
| 54 |
return in_array( 'all', $who, true ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @since 6.17 |
| 59 |
* |
| 60 |
* @param array $who |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
private static function is_user_type( $who ) { |
| 65 |
return in_array( self::get_user_type(), $who, true ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @since 6.17 |
| 70 |
* |
| 71 |
* @return string |
| 72 |
*/ |
| 73 |
private static function get_user_type() { |
| 74 |
if ( ! FrmAppHelper::pro_is_installed() ) { |
| 75 |
return 'free'; |
| 76 |
} |
| 77 |
|
| 78 |
return FrmAddonsController::license_type(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Check if user is still using the Lite version only, and within |
| 83 |
* the first 30 days of activation. |
| 84 |
* |
| 85 |
* @since 6.17 |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
private static function is_free_first_30() { |
| 90 |
return self::is_free() && self::is_first_30(); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @since 6.17 |
| 95 |
* |
| 96 |
* @return bool |
| 97 |
*/ |
| 98 |
private static function is_first_30() { |
| 99 |
$activation_timestamp = get_option( 'frm_first_activation' ); |
| 100 |
|
| 101 |
if ( false === $activation_timestamp ) { |
| 102 |
// If the option does not exist, assume that it is |
| 103 |
// Because the user was active before this option was introduced. |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
$cutoff = strtotime( '-30 days' ); |
| 108 |
return $activation_timestamp > $cutoff; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* @since 6.17 |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
private static function is_free_not_first_30() { |
| 117 |
return self::is_free() && ! self::is_first_30(); |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Check if the Pro plugin is active. If not, consider the user to be on the free version. |
| 122 |
* |
| 123 |
* @since 6.17 |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
private static function is_free() { |
| 128 |
return ! FrmAppHelper::pro_is_included(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @since 6.18 |
| 133 |
* |
| 134 |
* @param array $who |
| 135 |
* |
| 136 |
* @return bool |
| 137 |
*/ |
| 138 |
private static function check_free_segments( $who ) { |
| 139 |
$segments = array( |
| 140 |
'free_first_1', |
| 141 |
'free_first_2_3', |
| 142 |
'free_first_4_7', |
| 143 |
'free_first_8_11', |
| 144 |
'free_first_12_19', |
| 145 |
'free_first_20_30', |
| 146 |
); |
| 147 |
$intersecting_keys = array_intersect( $segments, $who ); |
| 148 |
|
| 149 |
if ( ! $intersecting_keys ) { |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
if ( ! self::is_free() || ! self::is_first_30() ) { |
| 154 |
return false; |
| 155 |
} |
| 156 |
|
| 157 |
$activation_timestamp = get_option( 'frm_first_activation' ); |
| 158 |
|
| 159 |
foreach ( $intersecting_keys as $key ) { |
| 160 |
if ( self::matches_segment( $key, $activation_timestamp ) ) { |
| 161 |
return true; |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
return false; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @since 6.18 |
| 170 |
* |
| 171 |
* @param string $key |
| 172 |
* @param int $activation_timestamp |
| 173 |
* |
| 174 |
* @return bool |
| 175 |
*/ |
| 176 |
private static function matches_segment( $key, $activation_timestamp ) { |
| 177 |
$range_part = str_replace( 'free_first_', '', $key ); |
| 178 |
$range_parts = explode( '_', $range_part ); |
| 179 |
|
| 180 |
if ( ! $range_parts ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
$current_day = (int) floor( ( time() - $activation_timestamp ) / DAY_IN_SECONDS ) + 1; |
| 185 |
$start = (int) $range_parts[0]; |
| 186 |
$end = 1 === count( $range_parts ) ? $start : (int) $range_parts[1]; |
| 187 |
|
| 188 |
return $current_day >= $start && $current_day <= $end; |
| 189 |
} |
| 190 |
} |
| 191 |
|