| 1 |
<?php |
| 2 |
|
| 3 |
namespace AcyMailing\WpInit; |
| 4 |
|
| 5 |
class Update |
| 6 |
{ |
| 7 |
public function __construct() |
| 8 |
{ |
| 9 |
add_filter('site_transient_update_plugins', [$this, 'checkUpdates'], 10, 1); |
| 10 |
add_action('upgrader_process_complete', [$this, 'after_update'], 20, 2); |
| 11 |
wp_cache_delete('plugins', 'plugins'); |
| 12 |
} |
| 13 |
|
| 14 |
public function checkUpdates($transient) |
| 15 |
{ |
| 16 |
$plugin_slug = plugin_basename(dirname(__DIR__).'/index.php'); |
| 17 |
if (!empty($transient->no_update[$plugin_slug])) { |
| 18 |
return $transient; |
| 19 |
} |
| 20 |
|
| 21 |
$config = acym_config(); |
| 22 |
$downloadURL = $config->get('downloadurl', ''); |
| 23 |
$lastCheck = $config->get('lastupdatecheck', 0); |
| 24 |
|
| 25 |
if (!empty($transient->response[$plugin_slug])) { |
| 26 |
if (empty($downloadURL) && !acym_level(ACYM_ESSENTIAL)) { |
| 27 |
$downloadURL = $transient->response[$plugin_slug]->package; |
| 28 |
} else { |
| 29 |
$transient->response[$plugin_slug]->package = $downloadURL; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
if (!empty($downloadURL)) { |
| 34 |
return $transient; |
| 35 |
} |
| 36 |
|
| 37 |
if ($lastCheck > time() - 86400 && (empty($_REQUEST['force-check']) || $_REQUEST['force-check'] != 1)) { |
| 38 |
return $transient; |
| 39 |
} |
| 40 |
|
| 41 |
static $alreadyChecked = false; |
| 42 |
if ($alreadyChecked) { |
| 43 |
return $transient; |
| 44 |
} |
| 45 |
$alreadyChecked = true; |
| 46 |
|
| 47 |
$url = ACYM_UPDATEME_API_URL.'public/updatexml/component?extension=acymailing&cms=wordpress&version=latest&level=starter'; |
| 48 |
if (acym_level(ACYM_ESSENTIAL)) { |
| 49 |
$url .= '&website='.urlencode(ACYM_LIVE); |
| 50 |
} |
| 51 |
|
| 52 |
$updateInformation = acym_fileGetContent($url); |
| 53 |
$xmlPos = strpos($updateInformation, '<?xml'); |
| 54 |
if ($xmlPos === false) { |
| 55 |
return $transient; |
| 56 |
} |
| 57 |
|
| 58 |
$updateInformation = substr($updateInformation, $xmlPos); |
| 59 |
|
| 60 |
try { |
| 61 |
$xml = new \SimpleXMLElement($updateInformation); |
| 62 |
$latestVersion = (string)$xml->update[0]->version; |
| 63 |
$downloadURL = (string)$xml->update[0]->downloadurl; |
| 64 |
} catch (\Exception $err) { |
| 65 |
return $transient; |
| 66 |
} |
| 67 |
|
| 68 |
$currentVersion = $config->get('version'); |
| 69 |
if (!empty($currentVersion) && version_compare($currentVersion, $latestVersion, '>=')) { |
| 70 |
if (!empty($transient->response[$plugin_slug])) { |
| 71 |
unset($transient->response[$plugin_slug]); |
| 72 |
} |
| 73 |
|
| 74 |
return $transient; |
| 75 |
} |
| 76 |
|
| 77 |
if (strpos($downloadURL, 'http') === false) { |
| 78 |
$downloadURL = ''; |
| 79 |
add_action('admin_notices', [$this, 'notice_update'], 110); |
| 80 |
} |
| 81 |
|
| 82 |
if (empty($transient->response[$plugin_slug])) { |
| 83 |
if (empty($transient) && !is_object($transient)) { |
| 84 |
$transient = new \stdClass(); |
| 85 |
} |
| 86 |
|
| 87 |
if (empty($transient->response) && (!isset($transient->response) || !is_array($transient->response))) { |
| 88 |
$transient->response = []; |
| 89 |
} |
| 90 |
|
| 91 |
$transient->response[$plugin_slug] = (object)[ |
| 92 |
'new_version' => $latestVersion, |
| 93 |
'package' => $downloadURL, |
| 94 |
'slug' => $plugin_slug, |
| 95 |
'icons' => [ |
| 96 |
'1x' => ACYM_IMAGES.'logos/logo_icon.png', |
| 97 |
], |
| 98 |
'url' => 'https://wordpress.org/plugins/acymailing/', |
| 99 |
]; |
| 100 |
} else { |
| 101 |
$transient->response[$plugin_slug]->package = $downloadURL; |
| 102 |
} |
| 103 |
|
| 104 |
$newConfig = new \stdClass(); |
| 105 |
$newConfig->lastupdatecheck = time(); |
| 106 |
$newConfig->latestversion = $latestVersion; |
| 107 |
$newConfig->downloadurl = $downloadURL; |
| 108 |
|
| 109 |
if (is_multisite()) { |
| 110 |
$currentBlog = get_current_blog_id(); |
| 111 |
$sites = function_exists('get_sites') ? get_sites() : wp_get_sites(); |
| 112 |
|
| 113 |
foreach ($sites as $site) { |
| 114 |
if (is_object($site)) { |
| 115 |
$site = get_object_vars($site); |
| 116 |
} |
| 117 |
switch_to_blog($site['blog_id']); |
| 118 |
$config->save($newConfig); |
| 119 |
} |
| 120 |
|
| 121 |
switch_to_blog($currentBlog); |
| 122 |
} else { |
| 123 |
$config->save($newConfig); |
| 124 |
} |
| 125 |
|
| 126 |
if (acym_level(ACYM_ESSENTIAL)) { |
| 127 |
acym_checkVersion(); |
| 128 |
} |
| 129 |
|
| 130 |
return $transient; |
| 131 |
} |
| 132 |
|
| 133 |
public function after_update($upgrader_object, $options) |
| 134 |
{ |
| 135 |
if ($options['action'] != 'update' || $options['type'] != 'plugin') return; |
| 136 |
|
| 137 |
$current_plugin_path_name = plugin_basename(dirname(__DIR__).'/index.php'); |
| 138 |
|
| 139 |
if (!empty($options['plugin']) && $options['plugin'] == $current_plugin_path_name) { |
| 140 |
$config = acym_config(); |
| 141 |
|
| 142 |
$newConfig = new \stdClass(); |
| 143 |
$newConfig->downloadurl = ''; |
| 144 |
$newConfig->lastupdatecheck = 0; |
| 145 |
$config->save($newConfig); |
| 146 |
} elseif (!empty($options['plugins'])) { |
| 147 |
foreach ($options['plugins'] as $each_plugin) { |
| 148 |
if ($each_plugin != $current_plugin_path_name) continue; |
| 149 |
$config = acym_config(); |
| 150 |
|
| 151 |
$downloadURL = $config->get('downloadurl'); |
| 152 |
if (empty($downloadURL)) { |
| 153 |
$dummyTransient = new \stdClass(); |
| 154 |
$this->checkUpdates($dummyTransient); |
| 155 |
|
| 156 |
$downloadURL = $config->get('downloadurl'); |
| 157 |
if (empty($downloadURL)) { |
| 158 |
echo acym_translation('ACYM_PAID_VERSION_NEED_UPDATE_ERROR_LICENSE_ATTACH'); |
| 159 |
exit; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
$newConfig = new \stdClass(); |
| 164 |
$newConfig->downloadurl = ''; |
| 165 |
$newConfig->lastupdatecheck = 0; |
| 166 |
$config->save($newConfig); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public function notice_update() |
| 172 |
{ |
| 173 |
global $pagenow; |
| 174 |
if (!in_array($pagenow, ['update-core.php', 'plugins.php'])) return; |
| 175 |
|
| 176 |
echo '<div class="notice notice-error is-dismissible"> |
| 177 |
<p>'.acym_translation('ACYM_PAID_VERSION_NEED_UPDATE_ERROR_LICENSE_ATTACH').'</p> |
| 178 |
</div>'; |
| 179 |
} |
| 180 |
} |
| 181 |
|