| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
abstract class Updraft_Notices_1_2 { |
| 6 |
|
| 7 |
protected $notices_content; |
| 8 |
|
| 9 |
// These variables are just short-hands to be used in advert content. |
| 10 |
protected $dashboard_top = array('top'); |
| 11 |
|
| 12 |
protected $dashboard_top_or_report = array('top', 'report', 'report-plain'); |
| 13 |
|
| 14 |
protected $dashboard_bottom_or_report = array('bottom', 'report', 'report-plain'); |
| 15 |
|
| 16 |
protected $anywhere = array('top', 'bottom', 'report', 'report-plain'); |
| 17 |
|
| 18 |
protected $autobackup = array('autobackup'); |
| 19 |
|
| 20 |
protected $autobackup_bottom_or_report = array('autobackup', 'bottom', 'report', 'report-plain'); |
| 21 |
|
| 22 |
/** |
| 23 |
* Global adverts that appear in all products will be returned to the child to display |
| 24 |
* |
| 25 |
* @return array |
| 26 |
*/ |
| 27 |
protected function populate_notices_content() { |
| 28 |
return array(); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Call this method to setup the notices. |
| 33 |
*/ |
| 34 |
abstract protected function notices_init(); |
| 35 |
|
| 36 |
/** |
| 37 |
* Checks if the plugin is installed and checks status if needed. |
| 38 |
* |
| 39 |
* @param null $product - Plugin to check |
| 40 |
* @param boolean $also_require_active - bool to indicate if active status is required or not |
| 41 |
* |
| 42 |
* @return boolean Returns true, if plugin is installed otherwise false |
| 43 |
*/ |
| 44 |
protected function is_plugin_installed($product = null, $also_require_active = false) { |
| 45 |
if ($also_require_active) return class_exists($product); |
| 46 |
if (!function_exists('get_plugins')) include_once(ABSPATH.'wp-admin/includes/plugin.php'); |
| 47 |
$plugins = get_plugins(); |
| 48 |
foreach ($plugins as $value) { |
| 49 |
if ($value['TextDomain'] == $product) { |
| 50 |
// We have found the plugin so return false so that we do not display this advert. |
| 51 |
return false; |
| 52 |
} |
| 53 |
} |
| 54 |
return true; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Checks if translation is needed or not |
| 59 |
* |
| 60 |
* @param string $plugin_base_dir - Base directory of plugin |
| 61 |
* @param string $product_name - Plugin name |
| 62 |
* |
| 63 |
* @return boolean Returns true if translation is needed, otherwise false |
| 64 |
*/ |
| 65 |
protected function translation_needed($plugin_base_dir, $product_name) { |
| 66 |
$wplang = get_locale(); |
| 67 |
if (strlen($wplang) < 1 || 'en_US' == $wplang || 'en_GB' == $wplang) return false; |
| 68 |
if (defined('WP_LANG_DIR') && is_file(WP_LANG_DIR.'/plugins/'.$product_name.'-'.$wplang.'.mo')) return false; |
| 69 |
if (is_file($plugin_base_dir.'/languages/'.$product_name.'-'.$wplang.'.mo')) return false; |
| 70 |
return true; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Generates the start of a HTML URL |
| 75 |
* |
| 76 |
* @param boolean $html_allowed - indicates if HTML is allowed or not |
| 77 |
* @param string $url - the URL |
| 78 |
* @param boolean $https - the protocol to use |
| 79 |
* @param string $website_home - the product website name |
| 80 |
* |
| 81 |
* @return string returns a partial HTML URL |
| 82 |
*/ |
| 83 |
protected function url_start($html_allowed, $url, $https = false, $website_home = null) { |
| 84 |
$proto = ($https) ? 'https' : 'http'; |
| 85 |
if (strpos($url, $website_home) !== false) { |
| 86 |
return $html_allowed ? "<a href=".apply_filters(str_replace('.', '_', $website_home).'_link', $proto.'://'.$url).'>' : ''; |
| 87 |
} else { |
| 88 |
return $html_allowed ? '<a href="'.$proto.'://'.$url.'">' : ''; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Generate the end of a HTML URL |
| 94 |
* |
| 95 |
* @param boolean $html_allowed - indicates if HTML is allowed or not |
| 96 |
* @param string $url - the URL |
| 97 |
* @param boolean $https - the protocol to use |
| 98 |
* |
| 99 |
* @return string returns a partial HTML URL |
| 100 |
*/ |
| 101 |
protected function url_end($html_allowed, $url, $https = false) { |
| 102 |
$proto = $https ? 'https' : 'http'; |
| 103 |
return $html_allowed ? '</a>' : ' ('.$proto.'://'.$url.')'; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Renders notice |
| 108 |
* |
| 109 |
* @param mixed $notice - a specific notice to render or false |
| 110 |
* @param string $position - position of the notice |
| 111 |
* @param boolean $return_instead_of_echo - indicates if we should echo notice or return as string |
| 112 |
* |
| 113 |
* @return mixed Returns string or echos notice |
| 114 |
*/ |
| 115 |
public function do_notice($notice = false, $position = 'top', $return_instead_of_echo = false) { |
| 116 |
|
| 117 |
$this->notices_init(); |
| 118 |
|
| 119 |
if (false === $notice) $notice = apply_filters('updraft_notices_force_id', false, $this); |
| 120 |
|
| 121 |
$notice_content = $this->get_notice_data($notice, $position); |
| 122 |
|
| 123 |
if (false != $notice_content) { |
| 124 |
return $this->render_specified_notice($notice_content, $return_instead_of_echo, $position); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* This method will return a notice ready for display. |
| 130 |
* |
| 131 |
* @param boolean $notice - a specific notice to render or false |
| 132 |
* @param string $position - position of the notice |
| 133 |
* |
| 134 |
* @return array returns notice data |
| 135 |
*/ |
| 136 |
protected function get_notice_data($notice = false, $position = 'top') { |
| 137 |
|
| 138 |
// If a specific notice has been passed to this method then return that notice. |
| 139 |
if ($notice) { |
| 140 |
if (!isset($this->notices_content[$notice])) return false; |
| 141 |
|
| 142 |
// Does the notice support the position specified? |
| 143 |
if (isset($this->notices_content[$notice]['supported_positions']) && !in_array($position, $this->notices_content[$notice]['supported_positions'])) return false; |
| 144 |
|
| 145 |
// 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. |
| 146 |
$dismiss_time = $this->notices_content[$notice]['dismiss_time']; |
| 147 |
|
| 148 |
$dismiss = $this->check_notice_dismissed($dismiss_time); |
| 149 |
|
| 150 |
if ($dismiss) return false; |
| 151 |
|
| 152 |
// If the advert has a validity function, then require the advert to be valid |
| 153 |
if (!empty($this->notices_content[$notice]['validity_function']) && !call_user_func(array($this, $this->notices_content[$notice]['validity_function']))) return false; |
| 154 |
|
| 155 |
return $this->notices_content[$notice]; |
| 156 |
} |
| 157 |
|
| 158 |
// 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. |
| 159 |
$available_notices = array(); |
| 160 |
|
| 161 |
// If Advert wasn't passed then next we should check to see if a seasonal advert can be returned. |
| 162 |
foreach ($this->notices_content as $notice_id => $notice_data) { |
| 163 |
// Does the notice support the position specified? |
| 164 |
if (isset($this->notices_content[$notice_id]['supported_positions']) && !in_array($position, $this->notices_content[$notice_id]['supported_positions'])) continue; |
| 165 |
|
| 166 |
// If the advert has a validity function, then require the advert to be valid. |
| 167 |
if (!empty($notice_data['validity_function']) && !call_user_func(array($this, $notice_data['validity_function']))) continue; |
| 168 |
|
| 169 |
|
| 170 |
if (isset($notice_data['valid_from']) && isset($notice_data['valid_to'])) { |
| 171 |
if ($this->skip_seasonal_notices($notice_data)) return $notice_data; |
| 172 |
} else { |
| 173 |
$dismiss_time = $this->notices_content[$notice_id]['dismiss_time']; |
| 174 |
$dismiss = $this->check_notice_dismissed($dismiss_time); |
| 175 |
|
| 176 |
if (!$dismiss) $available_notices[$notice_id] = $notice_data; |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
if (empty($available_notices)) return false; |
| 181 |
|
| 182 |
// If a seasonal advert can't be returned then we will return a random advert. |
| 183 |
|
| 184 |
// Here we give a 25% chance for the rate advert to be returned before selecting a random advert from the entire collection which also includes the rate advert |
| 185 |
if (0 == rand(0, 3) && isset($available_notices['rate'])) return $available_notices['rate']; |
| 186 |
|
| 187 |
// 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. |
| 188 |
shuffle($available_notices); |
| 189 |
return $available_notices[0]; |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Skip seasonal notices |
| 194 |
* |
| 195 |
* @param array $notice_data - an array of data for the chosen notice |
| 196 |
* |
| 197 |
* @return boolean |
| 198 |
*/ |
| 199 |
protected function skip_seasonal_notices($notice_data) {// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 200 |
return false; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Returns affiliate ID |
| 205 |
* |
| 206 |
* @return mixed Returns affiliate ID |
| 207 |
*/ |
| 208 |
public function get_affiliate_id() { |
| 209 |
return $this->self_affiliate_id; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Checks if the notice has been dismissed |
| 214 |
* |
| 215 |
* @param string $dismiss_time - dismiss time |
| 216 |
* |
| 217 |
* @return mixed |
| 218 |
*/ |
| 219 |
abstract protected function check_notice_dismissed($dismiss_time); |
| 220 |
} |
| 221 |
|