Skip to content

Commit 8f65dc1

Browse files
committed
Avoid checking for plugin updates in each page refresh
1 parent 7ed3f6a commit 8f65dc1

4 files changed

Lines changed: 58 additions & 6 deletions

File tree

qa-content/qa-admin.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ function qa_version_check(uri, version, elem, isCore)
148148
);
149149
}
150150

151+
function qa_version_check_array(versionChecks)
152+
{
153+
for (var i = 0; i < versionChecks.length; i++) {
154+
qa_version_check(versionChecks[i].uri, versionChecks[i].version, versionChecks[i].elem, false)
155+
}
156+
}
157+
151158
function qa_get_enabled_plugins_hashes()
152159
{
153160
var hashes = [];

qa-include/Q2A/Plugin/PluginManager.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ class Q2A_Plugin_PluginManager
2525
{
2626
const PLUGIN_DELIMITER = ';';
2727
const OPT_ENABLED_PLUGINS = 'enabled_plugins';
28+
const OPT_LAST_UPDATE_CHECK = 'last_plugin_update_check';
29+
const NUMBER_OF_DAYS_TO_CHECK_FOR_UPDATE = 14;
2830

2931
private $loadBeforeDbInit = array();
3032
private $loadAfterDbInit = array();
@@ -180,4 +182,27 @@ public function cleanRemovedPlugins()
180182

181183
$this->setEnabledPluginsOption($finalEnabledPlugins);
182184
}
185+
186+
/**
187+
* @return bool
188+
*/
189+
public function shouldCheckForUpdate()
190+
{
191+
$lastUpdateCheck = (int)qa_opt(self::OPT_LAST_UPDATE_CHECK);
192+
$currentTime = (int)qa_opt('db_time');
193+
194+
return $currentTime - $lastUpdateCheck > self::NUMBER_OF_DAYS_TO_CHECK_FOR_UPDATE * 24 * 60 * 60;
195+
}
196+
197+
/**
198+
* @param int|null $time
199+
*/
200+
public function performUpdateCheck($time = null)
201+
{
202+
if ($time === null) {
203+
$time = time();
204+
}
205+
206+
qa_opt(self::OPT_LAST_UPDATE_CHECK, $time);
207+
}
183208
}

qa-include/lang/qa-lang-admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
'users_registered' => 'Registered users:',
274274
'users_title' => 'Users',
275275
'users_voted' => 'Users who voted:',
276+
'version_check' => 'Check for updates',
276277
'version_get_x' => 'get ^',
277278
'version_latest' => 'latest',
278279
'version_latest_unknown' => 'latest unknown',

qa-include/pages/admin/admin-plugins.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@
6262
$enabledPluginHashesArray = explode(';', $enabledPluginHashes);
6363
$pluginDirectories = array_keys(array_intersect($pluginHashes, $enabledPluginHashesArray));
6464
$pluginManager->setEnabledPlugins($pluginDirectories);
65-
65+
qa_redirect('admin/plugins');
66+
} else if (qa_clicked('doversioncheck')) {
67+
$pluginManager->performUpdateCheck(0);
6668
qa_redirect('admin/plugins');
6769
}
6870
}
@@ -138,6 +140,9 @@
138140

139141
qa_sort_by($sortedPluginFiles, 'name');
140142

143+
$versionChecks = array();
144+
$shouldCheckForUpdate = $pluginManager->shouldCheckForUpdate();
145+
141146
$pluginIndex = -1;
142147
foreach ($sortedPluginFiles as $pluginDirectory => $metadata) {
143148
$pluginIndex++;
@@ -168,14 +173,16 @@
168173
} else
169174
$authorhtml = '';
170175

171-
if ($metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
176+
if ($shouldCheckForUpdate && $metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
172177
$elementid = 'version_check_' . md5($pluginDirectory);
173178

174-
$updatehtml = '(<span id="' . $elementid . '">...</span>)';
175-
176-
$qa_content['script_onloads'][] = array(
177-
"qa_version_check(" . qa_js($metadata['update_uri']) . ", " . qa_js($metadata['version'], true) . ", " . qa_js($elementid) . ", false);"
179+
$versionChecks[] = array(
180+
'uri' => $metadata['update_uri'],
181+
'version' => $metadata['version'],
182+
'elem' => $elementid,
178183
);
184+
185+
$updatehtml = '(<span id="' . $elementid . '">...</span>)';
179186
}
180187
else
181188
$updatehtml = '';
@@ -246,6 +253,14 @@
246253
}
247254
}
248255
}
256+
257+
if ($shouldCheckForUpdate) {
258+
$pluginManager->performUpdateCheck();
259+
260+
$qa_content['script_onloads'][] = array(
261+
sprintf('qa_version_check_array(%s);', json_encode($versionChecks)),
262+
);
263+
}
249264
}
250265

251266
$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
@@ -260,6 +275,10 @@
260275
'tags' => 'name="dosave"',
261276
'label' => qa_lang_html('admin/save_options_button'),
262277
),
278+
'doversioncheck' => array(
279+
'tags' => 'name="doversioncheck"',
280+
'label' => qa_lang_html('admin/version_check'),
281+
),
263282
),
264283

265284
'hidden' => array(

0 commit comments

Comments
 (0)