RatingNotice.php
76 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Basic\Notices; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Exception; |
| 7 | |
| 8 | class RatingNotice |
| 9 | { |
| 10 | /** @var string */ |
| 11 | const OPTION_NAME = 'wpstg_rating'; |
| 12 | |
| 13 | /** @var int */ |
| 14 | const DAYS_TO_SHOW_RATING_NOTICE_AFTER = 7; |
| 15 | |
| 16 | public function shouldShowRatingNotice() |
| 17 | { |
| 18 | return $this->canShow(self::OPTION_NAME, self::DAYS_TO_SHOW_RATING_NOTICE_AFTER) && $this->getCurrentPage() !== 'page' && $this->getCurrentPage() !== 'post'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Check if notice should be shown after certain days of installation or if it is dismissed |
| 23 | * @param int $days default 10 |
| 24 | * @return bool |
| 25 | */ |
| 26 | private function canShow($option, $days = 10) |
| 27 | { |
| 28 | // Do not show notice |
| 29 | if (empty($option)) { |
| 30 | return false; |
| 31 | } |
| 32 | |
| 33 | $dbOption = get_option($option); |
| 34 | |
| 35 | // Do not show notice |
| 36 | if ($dbOption === "no") { |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | $now = new DateTime("now"); |
| 41 | |
| 42 | // Check if user clicked on "rate later" button and if there is a valid 'later' date |
| 43 | if (wpstg_is_valid_date($dbOption)) { |
| 44 | // Do not show before this date |
| 45 | $show = new DateTime($dbOption); |
| 46 | if ($now < $show) { |
| 47 | return false; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Show X days after installation |
| 52 | $installDate = new DateTime(get_option("wpstg_installDate")); |
| 53 | |
| 54 | // get number of days between installation date and today |
| 55 | $difference = $now->diff($installDate)->days; |
| 56 | |
| 57 | return $days <= $difference; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get current page. |
| 62 | * Note: This can not be moved to wpAdapter class as it is only available very late |
| 63 | * at add admin_init and not available most of the time. |
| 64 | * |
| 65 | * @return string post, page |
| 66 | */ |
| 67 | private function getCurrentPage() |
| 68 | { |
| 69 | if (function_exists('\get_current_screen')) { |
| 70 | return \get_current_screen()->post_type; |
| 71 | } |
| 72 | |
| 73 | throw new Exception('Function get_current_screen does not exist. WP < 3.0.1.'); |
| 74 | } |
| 75 | } |
| 76 |