Skip to content

Avoid checking for plugin updates in each page refresh #769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: bugfix
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions qa-content/qa-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ function qa_version_check(uri, version, elem, isCore)
);
}

function qa_version_check_array(versionChecks)
{
for (var i = 0; i < versionChecks.length; i++) {
qa_version_check(versionChecks[i].uri, versionChecks[i].version, versionChecks[i].elem, false)
}
}

function qa_get_enabled_plugins_hashes()
{
var hashes = [];
Expand Down
25 changes: 25 additions & 0 deletions qa-include/Q2A/Plugin/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Q2A_Plugin_PluginManager
{
const PLUGIN_DELIMITER = ';';
const OPT_ENABLED_PLUGINS = 'enabled_plugins';
const OPT_LAST_UPDATE_CHECK = 'last_plugin_update_check';
const NUMBER_OF_DAYS_TO_CHECK_FOR_UPDATE = 14;

private $loadBeforeDbInit = array();
private $loadAfterDbInit = array();
Expand Down Expand Up @@ -180,4 +182,27 @@ public function cleanRemovedPlugins()

$this->setEnabledPluginsOption($finalEnabledPlugins);
}

/**
* @return bool
*/
public function shouldCheckForUpdate()
{
$lastUpdateCheck = (int)qa_opt(self::OPT_LAST_UPDATE_CHECK);
$currentTime = (int)qa_opt('db_time');

return $currentTime - $lastUpdateCheck > self::NUMBER_OF_DAYS_TO_CHECK_FOR_UPDATE * 24 * 60 * 60;
}

/**
* @param int|null $time
*/
public function performUpdateCheck($time = null)
{
if ($time === null) {
$time = time();
}

qa_opt(self::OPT_LAST_UPDATE_CHECK, $time);
}
}
1 change: 1 addition & 0 deletions qa-include/lang/qa-lang-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
'users_registered' => 'Registered users:',
'users_title' => 'Users',
'users_voted' => 'Users who voted:',
'version_check' => 'Check for updates',
'version_get_x' => 'get ^',
'version_latest' => 'latest',
'version_latest_unknown' => 'latest unknown',
Expand Down
31 changes: 25 additions & 6 deletions qa-include/pages/admin/admin-plugins.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
$enabledPluginHashesArray = explode(';', $enabledPluginHashes);
$pluginDirectories = array_keys(array_intersect($pluginHashes, $enabledPluginHashesArray));
$pluginManager->setEnabledPlugins($pluginDirectories);

qa_redirect('admin/plugins');
} else if (qa_clicked('doversioncheck')) {
$pluginManager->performUpdateCheck(0);
qa_redirect('admin/plugins');
}
}
Expand Down Expand Up @@ -138,6 +140,9 @@

qa_sort_by($sortedPluginFiles, 'name');

$versionChecks = array();
$shouldCheckForUpdate = $pluginManager->shouldCheckForUpdate();

$pluginIndex = -1;
foreach ($sortedPluginFiles as $pluginDirectory => $metadata) {
$pluginIndex++;
Expand Down Expand Up @@ -168,14 +173,16 @@
} else
$authorhtml = '';

if ($metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
if ($shouldCheckForUpdate && $metaver && isset($metadata['update_uri']) && strlen($metadata['update_uri'])) {
$elementid = 'version_check_' . md5($pluginDirectory);

$updatehtml = '(<span id="' . $elementid . '">...</span>)';

$qa_content['script_onloads'][] = array(
"qa_version_check(" . qa_js($metadata['update_uri']) . ", " . qa_js($metadata['version'], true) . ", " . qa_js($elementid) . ", false);"
$versionChecks[] = array(
'uri' => $metadata['update_uri'],
'version' => $metadata['version'],
'elem' => $elementid,
);

$updatehtml = '(<span id="' . $elementid . '">...</span>)';
}
else
$updatehtml = '';
Expand Down Expand Up @@ -246,6 +253,14 @@
}
}
}

if ($shouldCheckForUpdate) {
$pluginManager->performUpdateCheck();

$qa_content['script_onloads'][] = array(
sprintf('qa_version_check_array(%s);', json_encode($versionChecks)),
);
}
}

$qa_content['navigation']['sub'] = qa_admin_sub_navigation();
Expand All @@ -260,6 +275,10 @@
'tags' => 'name="dosave"',
'label' => qa_lang_html('admin/save_options_button'),
),
'doversioncheck' => array(
'tags' => 'name="doversioncheck"',
'label' => qa_lang_html('admin/version_check'),
),
),

'hidden' => array(
Expand Down