Changeset 2586072
- Timestamp:
- 08/20/2021 03:47:41 PM (3 years ago)
- Location:
- static-404
- Files:
-
- 2 deleted
- 8 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
static-404/tags/1.1.0/readme.txt
r2539076 r2586072 1 1 === Static 404 === 2 Contributors: bradparbs 2 Contributors: bradparbs 3 3 Tags: performance, 404, errors 4 4 Requires at least: 5.2 5 Tested up to: 5. 7.26 Stable tag: 1. 0.35 Tested up to: 5. 6 Stable tag: 1. 7 7 License: GPLv2 or later 8 8 Requires PHP: 5.6 … … 22 22 The output is a static page with the text `404 Not Found`, this text can be edited by filtering `static_404_message`. 23 23 24 25 26 27 24 28 25 29 == Installation == … … 27 31 - Install the plugin. 28 32 - Magically have faster and less expensive 404s! 33 34 35 36 37 38 39 40 41 -
static-404/tags/1.1.0/static-404.php
r2539076 r2586072 3 3 * Plugin Name: Static 404 4 4 * Description: Quickly output a 404 for static files that aren't found, rather than loading the normal 404 page. 5 * Version: 1. 0.35 * Version: 1. 6 6 * Author: Brad Parbs 7 7 * Author URI: https://bradparbs.com/ … … 13 13 */ 14 14 15 if ( ! defined( 'ABSPATH' ) ) { 16 exit; 15 namespace Static404; 16 17 defined( 'ABSPATH' ) || die(); 18 19 /** 20 * 21 * If the plugin is installed as an mu-plugin, then we can hook in earlier, 22 * otherwise we need to wait until plugins_loaded. 23 */ 24 add_action( get_early_action_to_use(), __NAMESPACE__ . '\\process_request' ); 25 26 /** 27 * If a request comes in for a static file and the webserver hasn't already 28 * handled it, then we want to 404 as quickly as possible without loading WordPress. 29 */ 30 function process_request() { 31 // If we don't have a request, then bail out. 32 // We'll be parsing this request to determine 33 // if a static file is being requested. 34 if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { 35 return; 36 } 37 38 // Allow short-circuiting the logic. 39 if ( should_process_request() ) { 40 return; 41 } 42 43 // Bail out if we don't have an extension or if the extension isn't in the list. ) 44 $req_ext = get_request_extension(); 45 if ( ! $req_ext || ! in_array( $req_ext, get_extensions(), true ) ) { 46 return; 47 } 48 49 // Set a 404. 50 http_response_code( apply_filters( 'static_404_response_code', 404 ) ); 51 52 // Kill the request. 53 // phpcs:ignore 54 wp_die( get_message() ); 17 55 } 18 56 19 // If the plugin is installed as an mu-plugin, then we can hook in earlier, 20 // otherwise we need to wait until plugins_loaded. 21 $static_404_hook = ! did_action( 'muplugins_loaded' ) ? 'muplugins_loaded' : 'plugins_loaded'; 57 /** 58 * Determine the earliest `loaded` action we can use. If we're in an mu-plugin, 59 * then fire it on mu-plugins_loaded, otherwise we need to wait until plugins_loaded. 60 * 61 * @return string Action to use. 62 */ 63 function get_early_action_to_use() { 64 if ( ! did_action( 'muplugins_loaded' ) ) { 65 return 'muplugins_loaded'; 66 } else { 67 return 'plugins_loaded'; 68 } 69 } 22 70 23 71 /** 24 * If a request comes in for a static file and the webserver hasn't already handled it, 25 * then we want to 404 as quickly as possible without loading WordPress. 72 * Get the extension of the request. 73 * 74 * @return string The extension of the request. 26 75 */ 27 add_action( 28 $static_404_hook, 29 function () { 30 // If we don't have a request, then bail out. 31 // We'll be parsing this request to determine 32 // if a static file is being requested. 33 if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { 34 return; 35 } 76 function get_request_extension() { 77 // Grab the file extension from our request. 78 $request = wp_check_filetype( wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ); 36 79 37 // Grab the file extension from our request. 38 $request = wp_check_filetype( wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ); 39 $req_ext = isset( $request['ext'] ) ? $request['ext'] : ''; 40 41 // Bail out if we don't have an extension or if the extension isn't in the list. 42 if ( ! $req_ext || ! in_array( $req_ext, static_404_get_extensions(), true ) ) { 43 return; 44 } 45 46 // Set a 404. 47 http_response_code( 404 ); 48 49 // Kill the request. 50 // phpcs:ignore 51 wp_die( static_404_get_message() ); 52 } 53 ); 80 // If we have a file extension, then return it. 81 return isset( $request['ext'] ) ? $request['ext'] : ''; 82 } 54 83 55 84 /** … … 59 88 * @return array Array of file extensions that could be static-404ed. 60 89 */ 61 function static_404_get_extensions() { 62 90 function get_extensions() { 63 91 $wp_ext_types = wp_get_ext_types(); 64 92 $extensions = []; … … 84 112 * @return string 404 error message. 85 113 */ 86 function static_404_get_message() {114 function get_message() { 87 115 return apply_filters( 'static_404_message', '404 ' . get_status_header_desc( 404 ) ); 88 116 } 117 118 119 120 121 122 123 124 125 -
static-404/trunk/readme.txt
r2539076 r2586072 1 1 === Static 404 === 2 Contributors: bradparbs 2 Contributors: bradparbs 3 3 Tags: performance, 404, errors 4 4 Requires at least: 5.2 5 Tested up to: 5. 7.26 Stable tag: 1. 0.35 Tested up to: 5. 6 Stable tag: 1. 7 7 License: GPLv2 or later 8 8 Requires PHP: 5.6 … … 22 22 The output is a static page with the text `404 Not Found`, this text can be edited by filtering `static_404_message`. 23 23 24 25 26 27 24 28 25 29 == Installation == … … 27 31 - Install the plugin. 28 32 - Magically have faster and less expensive 404s! 33 34 35 36 37 38 39 40 41 -
static-404/trunk/static-404.php
r2539076 r2586072 3 3 * Plugin Name: Static 404 4 4 * Description: Quickly output a 404 for static files that aren't found, rather than loading the normal 404 page. 5 * Version: 1. 0.35 * Version: 1. 6 6 * Author: Brad Parbs 7 7 * Author URI: https://bradparbs.com/ … … 13 13 */ 14 14 15 if ( ! defined( 'ABSPATH' ) ) { 16 exit; 15 namespace Static404; 16 17 defined( 'ABSPATH' ) || die(); 18 19 /** 20 * 21 * If the plugin is installed as an mu-plugin, then we can hook in earlier, 22 * otherwise we need to wait until plugins_loaded. 23 */ 24 add_action( get_early_action_to_use(), __NAMESPACE__ . '\\process_request' ); 25 26 /** 27 * If a request comes in for a static file and the webserver hasn't already 28 * handled it, then we want to 404 as quickly as possible without loading WordPress. 29 */ 30 function process_request() { 31 // If we don't have a request, then bail out. 32 // We'll be parsing this request to determine 33 // if a static file is being requested. 34 if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { 35 return; 36 } 37 38 // Allow short-circuiting the logic. 39 if ( should_process_request() ) { 40 return; 41 } 42 43 // Bail out if we don't have an extension or if the extension isn't in the list. ) 44 $req_ext = get_request_extension(); 45 if ( ! $req_ext || ! in_array( $req_ext, get_extensions(), true ) ) { 46 return; 47 } 48 49 // Set a 404. 50 http_response_code( apply_filters( 'static_404_response_code', 404 ) ); 51 52 // Kill the request. 53 // phpcs:ignore 54 wp_die( get_message() ); 17 55 } 18 56 19 // If the plugin is installed as an mu-plugin, then we can hook in earlier, 20 // otherwise we need to wait until plugins_loaded. 21 $static_404_hook = ! did_action( 'muplugins_loaded' ) ? 'muplugins_loaded' : 'plugins_loaded'; 57 /** 58 * Determine the earliest `loaded` action we can use. If we're in an mu-plugin, 59 * then fire it on mu-plugins_loaded, otherwise we need to wait until plugins_loaded. 60 * 61 * @return string Action to use. 62 */ 63 function get_early_action_to_use() { 64 if ( ! did_action( 'muplugins_loaded' ) ) { 65 return 'muplugins_loaded'; 66 } else { 67 return 'plugins_loaded'; 68 } 69 } 22 70 23 71 /** 24 * If a request comes in for a static file and the webserver hasn't already handled it, 25 * then we want to 404 as quickly as possible without loading WordPress. 72 * Get the extension of the request. 73 * 74 * @return string The extension of the request. 26 75 */ 27 add_action( 28 $static_404_hook, 29 function () { 30 // If we don't have a request, then bail out. 31 // We'll be parsing this request to determine 32 // if a static file is being requested. 33 if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { 34 return; 35 } 76 function get_request_extension() { 77 // Grab the file extension from our request. 78 $request = wp_check_filetype( wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ); 36 79 37 // Grab the file extension from our request. 38 $request = wp_check_filetype( wp_parse_url( $_SERVER['REQUEST_URI'], PHP_URL_PATH ) ); 39 $req_ext = isset( $request['ext'] ) ? $request['ext'] : ''; 40 41 // Bail out if we don't have an extension or if the extension isn't in the list. 42 if ( ! $req_ext || ! in_array( $req_ext, static_404_get_extensions(), true ) ) { 43 return; 44 } 45 46 // Set a 404. 47 http_response_code( 404 ); 48 49 // Kill the request. 50 // phpcs:ignore 51 wp_die( static_404_get_message() ); 52 } 53 ); 80 // If we have a file extension, then return it. 81 return isset( $request['ext'] ) ? $request['ext'] : ''; 82 } 54 83 55 84 /** … … 59 88 * @return array Array of file extensions that could be static-404ed. 60 89 */ 61 function static_404_get_extensions() { 62 90 function get_extensions() { 63 91 $wp_ext_types = wp_get_ext_types(); 64 92 $extensions = []; … … 84 112 * @return string 404 error message. 85 113 */ 86 function static_404_get_message() {114 function get_message() { 87 115 return apply_filters( 'static_404_message', '404 ' . get_status_header_desc( 404 ) ); 88 116 } 117 118 119 120 121 122 123 124 125
Note: See TracChangeset
for help on using the changeset viewer.