MPSUM_Admin.php
7 years ago
MPSUM_Admin_Advanced.php
7 years ago
MPSUM_Admin_Advanced_Preview.php
7 years ago
MPSUM_Admin_Ajax.php
7 years ago
MPSUM_Admin_Bar.php
7 years ago
MPSUM_Admin_Core.php
7 years ago
MPSUM_Admin_Dashboard.php
7 years ago
MPSUM_Admin_Help.php
7 years ago
MPSUM_Admin_Logs.php
7 years ago
MPSUM_Admin_Plugins.php
7 years ago
MPSUM_Admin_Screen_Options.php
7 years ago
MPSUM_Admin_Themes.php
7 years ago
MPSUM_Advanced_Premium.php
7 years ago
MPSUM_Check_Plugin_Install_Status.php
7 years ago
MPSUM_Check_Theme_Install_Status.php
7 years ago
MPSUM_Commands.php
7 years ago
MPSUM_Disable_Updates.php
7 years ago
MPSUM_Disable_Updates_All.php
7 years ago
MPSUM_Disable_Updates_Plugins.php
7 years ago
MPSUM_Disable_Updates_Themes.php
7 years ago
MPSUM_Disable_Updates_Translations.php
7 years ago
MPSUM_Disable_Updates_WordPress.php
7 years ago
MPSUM_Exclude_Users.php
7 years ago
MPSUM_Force_Updates.php
7 years ago
MPSUM_List_Table.php
7 years ago
MPSUM_Logs.php
7 years ago
MPSUM_Logs_List_Table.php
7 years ago
MPSUM_Plugins_List_Table.php
7 years ago
MPSUM_Reset_Options.php
7 years ago
MPSUM_Themes_List_Table.php
7 years ago
MPSUM_UpdraftCentral.php
7 years ago
MPSUM_UpdraftCentral_EUM_Commands.php
7 years ago
MPSUM_Utils.php
7 years ago
easy-updates-manager-notices.php
7 years ago
updraft-notices.php
7 years ago
updraft-notices.php
194 lines
| 1 | <?php |
| 2 | |
| 3 | if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 | |
| 5 | /** |
| 6 | * If we ever change the API of the Updraft_Notices class, then we'll need to rename and version it, e.g. Updraft_Notices_1_0, because otherwise a plugin may find that it's loaded an older instance than it wanted from another plugin. |
| 7 | */ |
| 8 | abstract class Updraft_Notices_1_0 { |
| 9 | |
| 10 | protected $notices_content; |
| 11 | |
| 12 | /** |
| 13 | * These variables are just short-hands to be used in advert content. |
| 14 | * |
| 15 | * @var array |
| 16 | */ |
| 17 | protected $dashboard_top = array('top'); |
| 18 | |
| 19 | protected $dashboard_top_or_report = array('top', 'report', 'report-plain'); |
| 20 | |
| 21 | protected $dashboard_bottom_or_report = array('bottom', 'report', 'report-plain'); |
| 22 | |
| 23 | protected $anywhere = array('top', 'bottom', 'report', 'report-plain'); |
| 24 | |
| 25 | protected $autobackup = array('autobackup'); |
| 26 | |
| 27 | protected $autobackup_bottom_or_report = array('autobackup', 'bottom', 'report', 'report-plain'); |
| 28 | |
| 29 | /** |
| 30 | * Populates notices content |
| 31 | * |
| 32 | * @return array |
| 33 | */ |
| 34 | protected function populate_notices_content() { |
| 35 | // Global adverts that appear in all products will be returned to the child to display. |
| 36 | return array(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Call this method to setup the notices. |
| 41 | */ |
| 42 | abstract protected function notices_init(); |
| 43 | |
| 44 | /** |
| 45 | * Checks whether plugin is installed and checks status if needed. |
| 46 | * |
| 47 | * @param null $product Plugin to check |
| 48 | * @param bool $also_require_active Whether to active status is required or not |
| 49 | * |
| 50 | * @return bool Returns true, if plugin is installed otherwise false |
| 51 | */ |
| 52 | protected function is_plugin_installed($product = null, $also_require_active = false) { |
| 53 | if ($also_require_active) return class_exists($product); |
| 54 | if (!function_exists('get_plugins')) include_once(ABSPATH.'wp-admin/includes/plugin.php'); |
| 55 | $plugins = get_plugins(); |
| 56 | $product_file = false; |
| 57 | foreach ($plugins as $key => $value) { |
| 58 | if ($value['TextDomain'] == $product) { |
| 59 | // We have found the plugin so return false so that we do not display this advert. |
| 60 | return false; |
| 61 | } |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Checks whether translation is needed or not |
| 68 | * |
| 69 | * @param string $plugin_base_dir Base directory of plugin |
| 70 | * @param string $product_name Plugin name |
| 71 | * |
| 72 | * @return bool Returns true if translation is needed, otherwise false |
| 73 | */ |
| 74 | protected function translation_needed($plugin_base_dir, $product_name) { |
| 75 | $wplang = get_locale(); |
| 76 | if (strlen($wplang) < 1 || 'en_US' == $wplang || 'en_GB' == $wplang) return false; |
| 77 | if (defined('WP_LANG_DIR') && is_file(WP_LANG_DIR.'/plugins/'.$product_name.'-'.$wplang.'.mo')) return false; |
| 78 | if (is_file($plugin_base_dir.'/languages/'.$product_name.'-'.$wplang.'.mo')) return false; |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Renders notice |
| 84 | * |
| 85 | * @param bool $notice Whether to render notice or not |
| 86 | * @param string $position Notice rendering position |
| 87 | * @param bool $return_instead_of_echo Whether echo notice or return as string |
| 88 | * |
| 89 | * @return mixed Returns string or echos notice |
| 90 | */ |
| 91 | public function do_notice($notice = false, $position = 'top', $return_instead_of_echo = false) { |
| 92 | |
| 93 | $enable_notices = get_site_option('easy_updates_manager_enable_notices', 'on'); |
| 94 | if ('off' === $enable_notices) { |
| 95 | return ''; |
| 96 | } |
| 97 | |
| 98 | $this->notices_init(); |
| 99 | |
| 100 | if (false === $notice) $notice = apply_filters('updraft_notices_force_id', false, $this); |
| 101 | |
| 102 | $notice_content = $this->get_notice_data($notice, $position); |
| 103 | |
| 104 | if (false != $notice_content) { |
| 105 | return $this->render_specified_notice($notice_content, $return_instead_of_echo, $position); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * This method will return a notice ready for display. |
| 111 | * |
| 112 | * @param boolean $notice Sends True or False if there are notices to show. |
| 113 | * @param string $position Which screen position the notice is. |
| 114 | * @return array Returns Notice data. |
| 115 | */ |
| 116 | protected function get_notice_data($notice = false, $position = 'top') { |
| 117 | |
| 118 | // If a specific notice has been passed to this method then return that notice. |
| 119 | if ($notice) { |
| 120 | if (!isset($this->notices_content[$notice])) return false; |
| 121 | |
| 122 | // Does the notice support the position specified? |
| 123 | if (isset($this->notices_content[$notice]['supported_positions']) && !in_array($position, $this->notices_content[$notice]['supported_positions'])) return false; |
| 124 | |
| 125 | // First check if the advert passed can be displayed and hasn't been dismissed, we do this by checking what dismissed value we should be checking. |
| 126 | $dismiss_time = $this->notices_content[$notice]['dismiss_time']; |
| 127 | |
| 128 | $dismiss = $this->check_notice_dismissed($dismiss_time); |
| 129 | |
| 130 | if ($dismiss) return false; |
| 131 | |
| 132 | return $this->notices_content[$notice]; |
| 133 | } |
| 134 | |
| 135 | // Create an array to add non-seasonal adverts to so that if a seasonal advert can't be returned we can choose a random advert from this array. |
| 136 | $available_notices = array(); |
| 137 | |
| 138 | // If Advert wasn't passed then next we should check to see if a seasonal advert can be returned. |
| 139 | foreach ($this->notices_content as $notice_id => $notice_data) { |
| 140 | // Does the notice support the position specified? |
| 141 | if (isset($this->notices_content[$notice_id]['supported_positions']) && !in_array($position, $this->notices_content[$notice_id]['supported_positions'])) continue; |
| 142 | |
| 143 | // If the advert has a validity function, then require the advert to be valid. |
| 144 | if (!empty($notice_data['validity_function']) && !call_user_func(array($this, $notice_data['validity_function']))) continue; |
| 145 | |
| 146 | |
| 147 | if (isset($notice_data['valid_from']) && isset($notice_data['valid_to'])) { |
| 148 | if ($this->skip_seasonal_notices($notice_data)) return $notice_data; |
| 149 | } else { |
| 150 | $dismiss_time = $this->notices_content[$notice_id]['dismiss_time']; |
| 151 | $dismiss = $this->check_notice_dismissed($dismiss_time); |
| 152 | |
| 153 | if (!$dismiss) $available_notices[$notice_id] = $notice_data; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (empty($available_notices)) return false; |
| 158 | |
| 159 | // If a seasonal advert can't be returned then we will return a random advert. |
| 160 | // Using shuffle here as something like rand which produces a random number and uses that as the array index fails, this is because in future an advert may not be numbered and could have a string as its key which will then cause errors. |
| 161 | shuffle($available_notices); |
| 162 | return $available_notices[0]; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Skip seasonal notices |
| 167 | * |
| 168 | * @param string $notice_data Notice data |
| 169 | * |
| 170 | * @return bool |
| 171 | */ |
| 172 | protected function skip_seasonal_notices($notice_data) { |
| 173 | return false; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Returns Affiliate ID |
| 178 | * |
| 179 | * @return mixed Returns Affiliate ID |
| 180 | */ |
| 181 | public function get_affiliate_id() { |
| 182 | return $this->self_affiliate_id; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Checks whether notice is dismissed |
| 187 | * |
| 188 | * @param string $dismiss_time Dismiss time |
| 189 | * |
| 190 | * @return mixed |
| 191 | */ |
| 192 | abstract protected function check_notice_dismissed($dismiss_time); |
| 193 | } |
| 194 |