| 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'); |
| 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'); |
| 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_plugin_installed("updraftplus"), |
| 74 |
'is_ilj_installed' => $this->is_plugin_installed("internal-links"), |
| 75 |
'is_aios_installed' => $this->is_plugin_installed("all-in-one-wp-security-and-firewall"), |
| 76 |
'is_aios_prem_installed' => $this->is_plugin_installed("all-in-one-wp-security-and-firewall-premium"), |
| 77 |
)); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Attempts to find out if WPO was newly installed or updated |
| 82 |
* |
| 83 |
* @return boolean |
| 84 |
*/ |
| 85 |
private function is_new_install() { |
| 86 |
if ($this->options->get_option('newly-activated')) { |
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Dismiss the notice |
| 95 |
* |
| 96 |
* @return boolean |
| 97 |
*/ |
| 98 |
public function dismiss() { |
| 99 |
|
| 100 |
if ($this->is_new_install()) { |
| 101 |
$this->options->delete_option('newly-activated'); |
| 102 |
} |
| 103 |
|
| 104 |
if (!$this->options->update_option('install-or-update-notice-version', $this->version)) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
// Delete Notice show time option, to allow re-creating next time we need to show it. |
| 109 |
$this->options->delete_option('install-or-update-notice-show-time'); |
| 110 |
|
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Checks if a specific plugin is installed. |
| 116 |
* |
| 117 |
* @param string $plugin_name The name of the plugin to check for installation status. |
| 118 |
* @return bool True if the plugin is installed, false otherwise. |
| 119 |
*/ |
| 120 |
private function is_plugin_installed($plugin_name) { |
| 121 |
$status = WP_Optimize()->get_db_info()->get_plugin_status($plugin_name); |
| 122 |
return $status['installed']; |
| 123 |
} |
| 124 |
} |
| 125 |
|