| Total Complexity | 48 |
| Total Lines | 301 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like WordPress_Security_Txt_Public often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WordPress_Security_Txt_Public, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class WordPress_Security_Txt_Public |
||
| 23 | { |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The ID of this plugin. |
||
| 27 | * |
||
| 28 | * @since 1.0.0 |
||
| 29 | * @access private |
||
| 30 | * @var string $plugin_name The ID of this plugin. |
||
| 31 | */ |
||
| 32 | private $plugin_name; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The version of this plugin. |
||
| 36 | * |
||
| 37 | * @since 1.0.0 |
||
| 38 | * @access private |
||
| 39 | * @var string $version The current version of this plugin. |
||
| 40 | */ |
||
| 41 | private $version; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The plugin options. |
||
| 45 | * |
||
| 46 | * @since 1.0.0 |
||
| 47 | * @access private |
||
| 48 | * @var array $options The plugin options. |
||
| 49 | */ |
||
| 50 | private $options; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * The plugin cache cleared flag. |
||
| 54 | * |
||
| 55 | * @since 1.0.0 |
||
| 56 | * @access private |
||
| 57 | * @var bool $cache_cleared The plugin cache cleared flag. |
||
| 58 | */ |
||
| 59 | private static $cache_cleared; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Initialize the class and set its properties. |
||
| 63 | * |
||
| 64 | * @since 1.0.0 |
||
| 65 | * |
||
| 66 | * @param string $plugin_name The name of the plugin. |
||
| 67 | * @param string $version The version of this plugin. |
||
| 68 | */ |
||
| 69 | public function __construct($plugin_name, $version) |
||
| 70 | { |
||
| 71 | $this->plugin_name = $plugin_name; |
||
| 72 | $this->version = $version; |
||
| 73 | |||
| 74 | self::$cache_cleared = get_transient('WORDPRESS_SECURITY_TXT_CACHE_CLEARED'); |
||
|
|
|||
| 75 | |||
| 76 | if ($this->version != WORDPRESS_SECURITY_TXT_VERSION) { |
||
| 77 | throw new Exception('Internal version mismatch in plugin wordpress-security-txt; it needs to be reinstalled.'); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Hijacks requests for enabled routes |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | public function route() |
||
| 87 | { |
||
| 88 | if (! isset($_SERVER) || ! isset($_SERVER['REQUEST_URI']) || ! isset($_SERVER['HTTP_HOST'])) { |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | |||
| 92 | $this->options = WordPress_Security_Txt_Admin::get_options($this->plugin_name); |
||
| 93 | |||
| 94 | if (! is_array($this->options) || ! isset($this->options['enable']) || ! $this->options['enable']) { |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | |||
| 98 | $request = ($this->is_secure() ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
||
| 99 | $site = get_site_url(); |
||
| 100 | |||
| 101 | if (strpos($request, $site) !== 0) { |
||
| 102 | return; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->apply_routes(substr($request, strlen($site))); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Applies plugin routes to a given URI. |
||
| 110 | * |
||
| 111 | * @param string $uri |
||
| 112 | */ |
||
| 113 | private function apply_routes($uri) |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Determines if current request is made via HTTPS. |
||
| 138 | * |
||
| 139 | * @since 1.0.0 |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | private function is_secure() |
||
| 143 | { |
||
| 144 | return (! empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Redirects a request to the correct location. |
||
| 149 | * |
||
| 150 | * @since 1.0.0 |
||
| 151 | * |
||
| 152 | * @param string $document |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | private function redirect($document) |
||
| 157 | { |
||
| 158 | header('Location: ' . get_site_url() . '/.well-known/' . $document); |
||
| 159 | |||
| 160 | exit; |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets the cache file for the plugin. |
||
| 165 | * |
||
| 166 | * @since 1.0.0 |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public static function cache_file() |
||
| 171 | { |
||
| 172 | return get_temp_dir() . DIRECTORY_SEPARATOR . 'wordpress-security-txt-cache.txt'; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Removes the cache file for the plugin. |
||
| 177 | * |
||
| 178 | * @since 1.0.0 |
||
| 179 | * |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | public static function cache_clear() |
||
| 183 | { |
||
| 184 | $cache_file = self::cache_file(); |
||
| 185 | |||
| 186 | if (is_file($cache_file)) { |
||
| 187 | $result = unlink($cache_file); |
||
| 188 | set_transient('WORDPRESS_SECURITY_TXT_CACHE_CLEARED', $result, 5); |
||
| 189 | self::$cache_cleared = get_transient('WORDPRESS_SECURITY_TXT_CACHE_CLEARED'); |
||
| 190 | } |
||
| 191 | |||
| 192 | return false; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Indicates if the cache was cleared during the current request. |
||
| 197 | * |
||
| 198 | * @since 1.0.0 |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public static function cache_cleared($reset = false) |
||
| 203 | { |
||
| 204 | $result = is_null(self::$cache_cleared) ? false : self::$cache_cleared; |
||
| 205 | |||
| 206 | if ($reset) { |
||
| 207 | delete_transient('WORDPRESS_SECURITY_TXT_CACHE_CLEARED'); |
||
| 208 | } |
||
| 209 | |||
| 210 | return $result; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Displays a document, assuming it can be rendered correctly. |
||
| 215 | * |
||
| 216 | * @since 1.0.0 |
||
| 217 | * |
||
| 218 | * @param string $document |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | private function show($document) |
||
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Renders gpg.txt for display |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | private function render_gpg_txt() |
||
| 248 | { |
||
| 249 | return $this->options['encryption']; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Renders security.txt for display |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | private function render_security_txt() |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Write to the security.txt cache. |
||
| 289 | * |
||
| 290 | * @param string $data |
||
| 291 | * @return void |
||
| 292 | */ |
||
| 293 | private function write_security_txt_cache($data) |
||
| 298 | } |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get the security.txt cache. |
||
| 303 | * |
||
| 304 | * @return mixed |
||
| 305 | */ |
||
| 306 | private function get_security_txt_cache() |
||
| 323 | } |
||
| 324 | } |
||
| 325 |