| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
/** |
| 5 |
* Install or update notice |
| 6 |
* Manages the notice shown when the user activates or updates WPO |
| 7 |
* |
| 8 |
* The class is included during `admin_init` |
| 9 |
*/ |
| 10 |
class WP_Optimize_Install_Or_Update_Notice { |
| 11 |
|
| 12 |
/** |
| 13 |
* Notice Version. |
| 14 |
* If we need to later show a similar notice with a different content, |
| 15 |
* We can reuse this by changing the version number and updating the content in the template file. |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
private $version = '1.1'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Options object |
| 23 |
* |
| 24 |
* @var WP_Optimize_Options |
| 25 |
*/ |
| 26 |
protected $options; |
| 27 |
|
| 28 |
public function __construct() { |
| 29 |
$this->options = WP_Optimize()->get_options(); |
| 30 |
|
| 31 |
if ($this->show_current_notice()) { |
| 32 |
add_action('wpo_admin_after_header', array($this, 'output_notice'), 20); |
| 33 |
} |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Determines if the current notice was shown |
| 39 |
* |
| 40 |
* @return boolean |
| 41 |
*/ |
| 42 |
public function show_current_notice() { |
| 43 |
// Check the option |
| 44 |
$latest_saved_notice = $this->options->get_option('install-or-update-notice-version', false); |
| 45 |
if ($latest_saved_notice && version_compare($latest_saved_notice, $this->version, '>=')) { |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
$notice_show_time = $this->options->get_option('install-or-update-notice-show-time', false); |
| 50 |
|
| 51 |
// If notice has been showing for more than 14days, automatically dismiss it. |
| 52 |
if ($notice_show_time && (time() - $notice_show_time) > (14 * 86400)) { |
| 53 |
$this->dismiss(); |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
if (!$notice_show_time) { |
| 58 |
// Save the first time the notice was shown |
| 59 |
$this->options->update_option('install-or-update-notice-show-time', time()); |
| 60 |
} |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Outputs the notice |
| 66 |
* |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
public function output_notice() { |
| 70 |
WP_Optimize()->include_template('notices/install-or-update-notice.php', false, array( |
| 71 |
'is_new_install' => $this->is_new_install(), |
| 72 |
'is_premium' => WP_Optimize::is_premium(), |
| 73 |
'is_updraftplus_installed' => $this->is_updraftplus_installed() |
| 74 |
)); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Attempts to find out if WPO was newly installed or updated |
| 79 |
* |
| 80 |
* @return boolean |
| 81 |
*/ |
| 82 |
private function is_new_install() { |
| 83 |
if ($this->options->get_option('newly-activated', false)) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Dismiss the notice |
| 92 |
* |
| 93 |
* @return boolean |
| 94 |
*/ |
| 95 |
public function dismiss() { |
| 96 |
|
| 97 |
if ($this->is_new_install()) { |
| 98 |
$this->options->delete_option('newly-activated'); |
| 99 |
} |
| 100 |
|
| 101 |
if (!$this->options->update_option('install-or-update-notice-version', $this->version)) { |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
// Delete Notice show time option, to allow re-creating next time we need to show it. |
| 106 |
$this->options->delete_option('install-or-update-notice-show-time'); |
| 107 |
|
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Check if updraftplus is installed. |
| 113 |
* |
| 114 |
* @return bool |
| 115 |
*/ |
| 116 |
private function is_updraftplus_installed() { |
| 117 |
$status = WP_Optimize()->get_db_info()->get_plugin_status('updraftplus'); |
| 118 |
|
| 119 |
return $status['installed']; |
| 120 |
} |
| 121 |
} |
| 122 |
|