| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package FireBox |
| 4 |
* @version 3.1.5 |
| 5 |
* |
| 6 |
* @author FirePlugins <info@fireplugins.com> |
| 7 |
* @link https://www.fireplugins.com |
| 8 |
* @copyright Copyright © 2026 FirePlugins All Rights Reserved |
| 9 |
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl.html> or later |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace FireBox\Core\Helpers; |
| 13 |
|
| 14 |
if (!defined('ABSPATH')) |
| 15 |
{ |
| 16 |
exit; // Exit if accessed directly. |
| 17 |
} |
| 18 |
|
| 19 |
class Plugin |
| 20 |
{ |
| 21 |
public static function getLatestVersionData() |
| 22 |
{ |
| 23 |
$url = FPF_GET_LICENSE_VERSION_URL . 'firebox'; |
| 24 |
|
| 25 |
$response = wp_remote_get($url); |
| 26 |
|
| 27 |
if (!is_array($response)) |
| 28 |
{ |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 32 |
$response_decoded = null; |
| 33 |
|
| 34 |
try |
| 35 |
{ |
| 36 |
$response_decoded = json_decode($response['body'], true); |
| 37 |
} |
| 38 |
catch (Exception $ex) |
| 39 |
{ |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
if (!isset($response_decoded['version'])) |
| 44 |
{ |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
return $response_decoded; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Checks whether the plugin is outdated. |
| 53 |
* |
| 54 |
* @param int $days_old |
| 55 |
* |
| 56 |
* @return bool |
| 57 |
*/ |
| 58 |
public static function isOutdated($days_old = 120) |
| 59 |
{ |
| 60 |
if (!defined('FBOX_RELEASE_DATE')) |
| 61 |
{ |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
if (!$then = strtotime(FBOX_RELEASE_DATE)) |
| 66 |
{ |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
$days_old = (int) $days_old; |
| 71 |
$now = time(); |
| 72 |
$diff = $now - $then; |
| 73 |
$days_diff = round($diff / (60 * 60 * 24)); |
| 74 |
|
| 75 |
if ($days_diff <= $days_old) |
| 76 |
{ |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Returns the installation date. |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public static function getInstallationDate() |
| 89 |
{ |
| 90 |
$path = self::getExtensionsDataFilePath(); |
| 91 |
|
| 92 |
// If file does not exist, abort |
| 93 |
if (!file_exists($path)) |
| 94 |
{ |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
// If file exists, retrieve its contents |
| 99 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 100 |
$content = file_get_contents($path); |
| 101 |
|
| 102 |
// Decode it |
| 103 |
if (!$content = json_decode($content, true)) |
| 104 |
{ |
| 105 |
return; |
| 106 |
} |
| 107 |
|
| 108 |
// Ensure install date exists |
| 109 |
if (!isset($content['install_date'])) |
| 110 |
{ |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
return $content['install_date']; |
| 115 |
} |
| 116 |
|
| 117 |
public static function getExtensionItemValue($key = null) |
| 118 |
{ |
| 119 |
$path = self::getExtensionsDataFilePath(); |
| 120 |
|
| 121 |
// If file does not exist, abort |
| 122 |
if (!file_exists($path)) |
| 123 |
{ |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
// If file exists, retrieve its contents |
| 128 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 129 |
$content = file_get_contents($path); |
| 130 |
|
| 131 |
// Decode it |
| 132 |
if (!$content = json_decode($content, true)) |
| 133 |
{ |
| 134 |
return; |
| 135 |
} |
| 136 |
|
| 137 |
// Ensure key exists |
| 138 |
if (!isset($content[$key])) |
| 139 |
{ |
| 140 |
return; |
| 141 |
} |
| 142 |
|
| 143 |
return $content[$key]; |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Sets or updates a key-value pair in the extensions data file. |
| 148 |
* |
| 149 |
* @param string $key The key to set |
| 150 |
* @param mixed $value The value to set |
| 151 |
* @param bool $force_replace Whether to replace existing value regardless of age |
| 152 |
* @param int $max_age_days Maximum age in days before replacing existing value (only used if force_replace is false) |
| 153 |
* |
| 154 |
* @return bool True if the value was set/updated, false if key already exists and no update was needed |
| 155 |
*/ |
| 156 |
public static function setExtensionData($key, $value, $force_replace = false, $max_age_days = 0) |
| 157 |
{ |
| 158 |
$path = self::getExtensionsDataFilePath(); |
| 159 |
|
| 160 |
// If file does not exist, create it with the key-value pair |
| 161 |
if (!file_exists($path)) |
| 162 |
{ |
| 163 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 164 |
file_put_contents($path, wp_json_encode( |
| 165 |
[ |
| 166 |
$key => $value |
| 167 |
] |
| 168 |
)); |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
// If file exists, retrieve its contents |
| 173 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents |
| 174 |
$content = file_get_contents($path); |
| 175 |
|
| 176 |
// Decode it |
| 177 |
$content = json_decode($content, true); |
| 178 |
|
| 179 |
// If decoding failed, initialize as empty array |
| 180 |
if ($content === null) |
| 181 |
{ |
| 182 |
$content = []; |
| 183 |
} |
| 184 |
|
| 185 |
// Check if key already exists |
| 186 |
if (isset($content[$key])) |
| 187 |
{ |
| 188 |
// If not forcing replacement, check age conditions |
| 189 |
if (!$force_replace) |
| 190 |
{ |
| 191 |
// If max_age_days is 0, don't replace existing value |
| 192 |
if ($max_age_days <= 0) |
| 193 |
{ |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
// Check if file is older than max_age_days |
| 198 |
$file_modified_time = filemtime($path); |
| 199 |
$current_time = time(); |
| 200 |
$age_in_seconds = $current_time - $file_modified_time; |
| 201 |
$age_in_days = floor($age_in_seconds / (60 * 60 * 24)); |
| 202 |
|
| 203 |
// Only replace if file is older than max_age_days |
| 204 |
if ($age_in_days >= $max_age_days) |
| 205 |
{ |
| 206 |
$content[$key] = $value; |
| 207 |
} |
| 208 |
else |
| 209 |
{ |
| 210 |
return false; |
| 211 |
} |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
// Force replacement |
| 216 |
$content[$key] = $value; |
| 217 |
} |
| 218 |
} |
| 219 |
else |
| 220 |
{ |
| 221 |
// Key doesn't exist, set it |
| 222 |
$content[$key] = $value; |
| 223 |
} |
| 224 |
|
| 225 |
// phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents |
| 226 |
file_put_contents($path, wp_json_encode($content)); |
| 227 |
|
| 228 |
return true; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* The file path that stores all extensions data. |
| 233 |
* |
| 234 |
* @return string |
| 235 |
*/ |
| 236 |
public static function getExtensionsDataFilePath() |
| 237 |
{ |
| 238 |
return \FPFramework\Helpers\WPHelper::getPluginUploadsDirectory('firebox') . '/data.json'; |
| 239 |
} |
| 240 |
} |