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