| @@ -97,8 +97,11 @@ | ||
| 97 | 97 | * @param string $url |
| 98 | 98 | * @return string |
| 99 | 99 | */ |
| 100 | 100 | public static function get_file_path($url) { |
| 101 | + $url_without_query = strtok($url, '?'); | |
| 102 | + $url = false === $url_without_query ? $url : $url_without_query; | |
| 103 | + | |
| 101 | 104 | if (is_multisite()) { |
| 102 | 105 | if (function_exists('get_main_site_id')) { |
| 103 | 106 | $site_id = get_main_site_id(); |
| 104 | 107 | } else { |
| @@ -407,8 +410,21 @@ | ||
| 407 | 410 | return esc_url(add_query_arg($utm_params, $url)); |
| 408 | 411 | } |
| 409 | 412 | |
| 410 | 413 | /** |
| 414 | + * Defines MAX_FILE_SIZE constant if not already defined. | |
| 415 | + * | |
| 416 | + * @return void | |
| 417 | + */ | |
| 418 | + public static function define_maxfile_size_constant() { | |
| 419 | + $maxfile_size = apply_filters('wpo_max_file_size', 600000); | |
| 420 | + $maxfile_size = is_numeric($maxfile_size) && $maxfile_size > 0 ? (int) $maxfile_size : 600000; | |
| 421 | + if (!defined('MAX_FILE_SIZE')) { | |
| 422 | + define('MAX_FILE_SIZE', $maxfile_size); | |
| 423 | + } | |
| 424 | + } | |
| 425 | + | |
| 426 | + /** | |
| 411 | 427 | * Check if the given array contains all key-value pairs of another array. |
| 412 | 428 | * |
| 413 | 429 | * @param array $needle The array of key-value pairs to check for. |
| 414 | 430 | * @param array $haystack The array to check within. |
| @@ -429,8 +445,65 @@ | ||
| 429 | 445 | } |
| 430 | 446 | } |
| 431 | 447 | |
| 432 | 448 | return true; |
| 449 | + } | |
| 450 | + | |
| 451 | + /** | |
| 452 | + * Check if the current user has permission to purge cache. | |
| 453 | + * | |
| 454 | + * @return bool | |
| 455 | + */ | |
| 456 | + public static function current_user_can_purge_cache() { | |
| 457 | + if (WP_Optimize::is_premium()) { | |
| 458 | + return WP_Optimize()->current_user_can() || WP_Optimize_Premium()->can_purge_the_cache(); | |
| 459 | + } else { | |
| 460 | + return WP_Optimize()->current_user_can(); | |
| 461 | + } | |
| 462 | + } | |
| 463 | + | |
| 464 | + /** | |
| 465 | + * Get URL without purge cache-related parameters. | |
| 466 | + * | |
| 467 | + * @return string | |
| 468 | + */ | |
| 469 | + public static function get_url_without_cache_purge_params() { | |
| 470 | + return remove_query_arg(self::get_cache_purge_params()); | |
| 471 | + } | |
| 472 | + | |
| 473 | + /** | |
| 474 | + * Outputs JavaScript to remove cache purge parameters from the URL. | |
| 475 | + * | |
| 476 | + * @return void | |
| 477 | + */ | |
| 478 | + public static function script_to_remove_cache_purge_params_from_url() { | |
| 479 | + printf('<script>document.addEventListener("DOMContentLoaded", function() { var params = %s; var url = new URL(window.location.href); for (var param of params) { url.searchParams.delete(param); } history.replaceState({}, "", url); });</script>', json_encode(self::get_cache_purge_params())); | |
| 480 | + } | |
| 481 | + | |
| 482 | + /** | |
| 483 | + * Returns the list of parameter names used for cache purge actions | |
| 484 | + * | |
| 485 | + * @return array | |
| 486 | + */ | |
| 487 | + private static function get_cache_purge_params() { | |
| 488 | + $original_purge_params = array('_wpo_purge', 'wpo_minify_cache_purged', '_wpo_purge_minify_cache', 'wpo_all_pages_cache_purged', 'wpo_single_page_cache_purged'); | |
| 489 | + $purge_params = apply_filters('wpo_get_cache_purge_params', $original_purge_params); | |
| 490 | + | |
| 491 | + if (!is_array($purge_params)) $purge_params = $original_purge_params; | |
| 492 | + | |
| 493 | + return $purge_params; | |
| 494 | + } | |
| 495 | + | |
| 496 | + /** | |
| 497 | + * Returns a human-readable time difference or "Never" if no timestamp exists. | |
| 498 | + * | |
| 499 | + * @param int|null $timestamp Unix timestamp, or null/0 for "Never". | |
| 500 | + * @return string Human-readable time string. | |
| 501 | + */ | |
| 502 | + public static function human_time_diff_or_never(?int $timestamp = null): string { | |
| 503 | + if (!$timestamp) return __('Never', 'wp-optimize'); | |
| 504 | + /* translators: %s = human-readable time difference */ | |
| 505 | + return sprintf(__('%s ago', 'wp-optimize'), human_time_diff($timestamp, time())); | |
| 433 | 506 | } |
| 434 | 507 | } |
| 435 | 508 | |
| 436 | 509 | endif; |