| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('Access denied.'); |
| 4 |
|
| 5 |
if (!class_exists('WP_Optimize_Bypass')) : |
| 6 |
|
| 7 |
/** |
| 8 |
* WP_Optimize_Bypass class |
| 9 |
* |
| 10 |
* Handles bypass functionality to disable WP-Optimize optimizations |
| 11 |
* when the config constant WPO_DISABLE_MODE_URL_PARAM is present |
| 12 |
* |
| 13 |
* @since 4.4.0 |
| 14 |
*/ |
| 15 |
class WP_Optimize_Bypass { |
| 16 |
|
| 17 |
/** |
| 18 |
* Singleton instance |
| 19 |
* |
| 20 |
* @var WP_Optimize_Bypass|null |
| 21 |
*/ |
| 22 |
private static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Get a singleton instance |
| 26 |
* |
| 27 |
* @return WP_Optimize_Bypass |
| 28 |
*/ |
| 29 |
public static function instance() { |
| 30 |
if (null === self::$instance) { |
| 31 |
self::$instance = new self(); |
| 32 |
} |
| 33 |
return self::$instance; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Check if we should bypass WP-Optimize optimizations |
| 38 |
* |
| 39 |
* @return bool True to indicate should bypass; false otherwise. |
| 40 |
*/ |
| 41 |
public function should_bypass() { |
| 42 |
$bypass = false; |
| 43 |
|
| 44 |
if (is_admin()) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
// Check $_GET directly |
| 49 |
if ($this->is_bypass_mode_enabled()) { |
| 50 |
$bypass = true; |
| 51 |
} |
| 52 |
|
| 53 |
return $bypass; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check if WP-Optimize bypass mode is enabled via secret URL parameter. |
| 58 |
* |
| 59 |
* @return bool |
| 60 |
*/ |
| 61 |
private function is_bypass_mode_enabled() { |
| 62 |
// Feature is OFF unless explicitly enabled |
| 63 |
if (!defined('WPO_DISABLE_MODE_URL_PARAM') || empty(WPO_DISABLE_MODE_URL_PARAM)) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
$param = WPO_DISABLE_MODE_URL_PARAM; |
| 68 |
|
| 69 |
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only flag, executes early |
| 70 |
return isset($_GET[$param]); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Show bypass admin notice |
| 75 |
* Called early in the request |
| 76 |
*/ |
| 77 |
public function show_admin_notice() { |
| 78 |
// Hook into early actions to set a bypass flag |
| 79 |
add_action('plugins_loaded', array($this, 'maybe_add_admin_notice'), 5); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Add admin bar notice when bypass is active |
| 84 |
*/ |
| 85 |
public function maybe_add_admin_notice() { |
| 86 |
if (current_user_can('manage_options')) { |
| 87 |
add_action('admin_bar_menu', array($this, 'add_bypass_notice_to_admin_bar'), 999); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Add bypass notice to admin bar |
| 93 |
* |
| 94 |
* @param WP_Admin_Bar $wp_admin_bar |
| 95 |
*/ |
| 96 |
public function add_bypass_notice_to_admin_bar($wp_admin_bar) { |
| 97 |
$wp_admin_bar->add_node(array( |
| 98 |
'id' => 'wpo-bypass-notice', |
| 99 |
'title' => '<span style="color: #ff6600;">⚠ ' . esc_html__('WP-Optimize bypassed', 'wp-optimize') . '</span>', |
| 100 |
'href' => false, |
| 101 |
'meta' => array( |
| 102 |
'title' => __('WP-Optimize optimizations are currently bypassed via a custom URL parameter defined by WPO_DISABLE_MODE_URL_PARAM', 'wp-optimize'), |
| 103 |
), |
| 104 |
)); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
endif; |
| 109 |
|