| 1 |
<?php |
| 2 |
|
| 3 |
namespace Leadin\admin; |
| 4 |
|
| 5 |
use Leadin\data\Portal_Options; |
| 6 |
use Leadin\data\User_Metadata; |
| 7 |
use Leadin\admin\client\Contacts_Api_Client; |
| 8 |
|
| 9 |
|
| 10 |
/** |
| 11 |
* Class responsible for controlling if review banner should show. |
| 12 |
*/ |
| 13 |
class ReviewController { |
| 14 |
|
| 15 |
const CONTACTS_CREATED_SINCE_ACTIVATION = 5; |
| 16 |
const REVIEW_BANNER_INTRO_PERIOD = 15; |
| 17 |
const DAYS_SINCE_LAST_FETCH = 1; |
| 18 |
|
| 19 |
/** |
| 20 |
* Checks if user has enough contacts created since plugin activation. |
| 21 |
*/ |
| 22 |
public static function has_contacts_created_since_activation() { |
| 23 |
if ( self::has_min_contacts() ) { |
| 24 |
return true; |
| 25 |
} elseif ( ! self::should_fetch_contacts() ) { |
| 26 |
return false; |
| 27 |
} |
| 28 |
try { |
| 29 |
User_Metadata::set_review_banner_last_call( time() ); |
| 30 |
$client = new Contacts_Api_Client(); |
| 31 |
$contacts = $client->get_contacts_from_timestamp( Portal_Options::get_activation_time() ); |
| 32 |
$has_min_contacts = count( $contacts->results ) >= self::CONTACTS_CREATED_SINCE_ACTIVATION; |
| 33 |
User_Metadata::set_has_min_contacts( $has_min_contacts ); |
| 34 |
return $has_min_contacts; |
| 35 |
} catch ( \Exception $e ) { |
| 36 |
return false; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Check to see if current time is after introductary period. |
| 42 |
*/ |
| 43 |
public static function is_after_introductary_period() { |
| 44 |
$activation_time = new \DateTime(); |
| 45 |
$activation_time->setTimestamp( Portal_Options::get_activation_time() ); |
| 46 |
$diff = $activation_time->diff( new \DateTime() ); |
| 47 |
return $diff->days >= self::REVIEW_BANNER_INTRO_PERIOD; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Check SKIP_REVIEW meta data for a user. |
| 52 |
*/ |
| 53 |
public static function is_reviewed_or_skipped() { |
| 54 |
return ! empty( User_Metadata::get_skip_review() ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Check if contacts have been fetched at the current day. |
| 59 |
*/ |
| 60 |
public static function should_fetch_contacts() { |
| 61 |
$last_call_ts = User_Metadata::get_review_banner_last_call(); |
| 62 |
if ( empty( $last_call_ts ) ) { |
| 63 |
return true; |
| 64 |
} |
| 65 |
$last_call_date = new \DateTime(); |
| 66 |
$last_call_date->setTimestamp( $last_call_ts ); |
| 67 |
$diff = $last_call_date->diff( new \DateTime() ); |
| 68 |
return $diff->days >= self::DAYS_SINCE_LAST_FETCH; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Check if contacts minimun have already been fulfilled . |
| 73 |
*/ |
| 74 |
public static function has_min_contacts() { |
| 75 |
return User_Metadata::get_has_min_contacts(); |
| 76 |
} |
| 77 |
|
| 78 |
} |
| 79 |
|