Skip to content

Commit

Permalink
some code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nimrod-cohen committed Oct 17, 2024
1 parent 1ddd83f commit 0bda6ad
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion fv-country-blocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: FV Country Blocker
* Plugin URI: https://github.com/nimrod-cohen/fv-country-blocker
* Description: Block visitors from specific countries using MaxMind GeoIP database.
* Version: 1.0.14
* Version: 1.0.15
* Author: nimrod-cohen
* Author URI: https://github.com/nimrod-cohen/fv-country-blocker
* License: GPL-2.0+
Expand Down
45 changes: 25 additions & 20 deletions includes/github-updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GitHubPluginUpdater {
private $plugin_slug;
private $version;
private $cache_key;
private $release_notes_cache_key;
private $author;
private $cache_allowed;
private $latest_release = null;
Expand All @@ -40,13 +41,13 @@ public function __construct() {
$file = __FILE__;
$this->plugin_slug = explode('/', plugin_basename($file))[0];
$this->cache_key = $this->plugin_slug . '_transient_data';
$this->cache_allowed = true;
$this->release_notes_cache_key = $this->plugin_slug . '_release';
$this->cache_allowed = false;
$this->get_plugin_details();

add_filter('plugins_api', [$this, 'info'], 20, 3);
add_filter('site_transient_update_plugins', [$this, 'update']);
add_action('upgrader_process_complete', [$this, 'purge'], 10, 2);
add_action('upgrader_post_install', [$this, 'post_install'], 10, 3);
add_action('upgrader_process_complete', [$this, 'finish_install'], 10, 2);
}

public function request() {
Expand Down Expand Up @@ -138,7 +139,11 @@ private function get_latest_release() {
return true;
}

$transient = get_transient($this->plugin_slug . '_latest_release');
$transient = null;
if ($this->cache_allowed) {
$transient = get_transient($this->release_notes_cache_key);
}

if ($transient) {
$this->latest_release = $transient;
return true;
Expand All @@ -154,7 +159,9 @@ private function get_latest_release() {

$this->latest_release = json_decode(wp_remote_retrieve_body($response));

set_transient($this->plugin_slug . '_latest_release', $this->latest_release, 5 * MINUTE_IN_SECONDS);
if ($this->cache_allowed) {
set_transient($this->release_notes_cache_key, $this->latest_release, 5 * MINUTE_IN_SECONDS);
}

return true;
}
Expand Down Expand Up @@ -194,28 +201,26 @@ public function update($transient) {

}

public function purge($upgrader, $options) {

public function finish_install(&$upgrader, $options) {
if (
$this->cache_allowed
&& 'update' === $options['action']
&& 'plugin' === $options['type']
'update' !== $options['action']
|| 'plugin' === $options['type']
) {
// just clean the cache when new plugin version is installed
return;
}

// just clean the cache when new plugin version is installed
if ($this->cache_allowed) {
delete_transient($this->cache_key);
delete_transient($this->plugin_slug . '_latest_release');
delete_transient($this->release_notes_cache_key);
}
}

public function post_install($true, $hook_extra, $result) {
//move the folder to the correct location
global $wp_filesystem;

// Move to proper destination
$proper_destination = WP_PLUGIN_DIR . '/' . $this->plugin_slug;
$wp_filesystem->move($result['destination'], $proper_destination);
$result['destination'] = $proper_destination;
return $result;
}
$wp_filesystem->move($upgrader->result['destination'], $proper_destination);
$upgrader->result['destination'] = $proper_destination;

}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: country blocker, IP blocker, geo-blocking, security
Requires at least: 5.0
Tested up to: 6.0
Requires PHP: 7.0
Stable tag: 1.0.14
Stable tag: 1.0.15
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down
2 changes: 1 addition & 1 deletion release.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"slug": "fv-country-blocker",
"author": "<a href='https://github.com/nimrod-cohen'>Nimrod Cohen</a>",
"author_profile": "http://profiles.wordpress.org/daberelay",
"version": "1.0.14",
"version": "1.0.15",
"requires": "3.0",
"tested": "5.8",
"requires_php": "5.3",
Expand Down

0 comments on commit 0bda6ad

Please sign in to comment.