Plugin Directory

Changeset 2586072

Timestamp:
08/20/2021 03:47:41 PM (3 years ago)
Author:
bradparbs
Message:

Update to version 1.1.0 from GitHub

Location:
static-404
Files:
2 deleted
8 edited
1 copied

Legend:

Unmodified
Added
Removed
  • static-404/tags/1.1.0/readme.txt

    r2539076 r2586072  
    11=== Static 404 ===
    2 Contributors: bradparbs
     2Contributors: bradparbs
    33Tags: performance, 404, errors
    44Requires at least: 5.2
    5 Tested up to: 5.7.2
    6 Stable tag: 1.0.3
     5Tested up to: 5.
     6Stable tag: 1.
    77License: GPLv2 or later
    88Requires PHP: 5.6
     
    2222The output is a static page with the text `404 Not Found`, this text can be edited by filtering `static_404_message`.
    2323
     24
     25
     26
     27
    2428
    2529== Installation ==
     
    2731 - Install the plugin.
    2832 - 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  
    33 * Plugin Name: Static 404
    44 * Description: Quickly output a 404 for static files that aren't found, rather than loading the normal 404 page.
    5  * Version:     1.0.3
     5 * Version:     1.
    66 * Author:      Brad Parbs
    77 * Author URI:  https://bradparbs.com/
     
    1313 */
    1414
    15 if ( ! defined( 'ABSPATH' ) ) {
    16     exit;
     15namespace Static404;
     16
     17defined( '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 */
     24add_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 */
     30function 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() );
    1755}
    1856
    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 */
     63function get_early_action_to_use() {
     64    if ( ! did_action( 'muplugins_loaded' )  ) {
     65        return 'muplugins_loaded';
     66    } else {
     67        return 'plugins_loaded';
     68    }
     69}
    2270
    2371/**
    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.
    2675 */
    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         }
     76function 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 ) );
    3679
    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}
    5483
    5584/**
     
    5988 * @return array Array of file extensions that could be static-404ed.
    6089 */
    61 function static_404_get_extensions() {
    62 
     90function get_extensions() {
    6391    $wp_ext_types = wp_get_ext_types();
    6492    $extensions   = [];
     
    84112 * @return string 404 error message.
    85113 */
    86 function static_404_get_message() {
     114function get_message() {
    87115    return apply_filters( 'static_404_message', '404 ' . get_status_header_desc( 404 ) );
    88116}
     117
     118
     119
     120
     121
     122
     123
     124
     125
  • static-404/trunk/readme.txt

    r2539076 r2586072  
    11=== Static 404 ===
    2 Contributors: bradparbs
     2Contributors: bradparbs
    33Tags: performance, 404, errors
    44Requires at least: 5.2
    5 Tested up to: 5.7.2
    6 Stable tag: 1.0.3
     5Tested up to: 5.
     6Stable tag: 1.
    77License: GPLv2 or later
    88Requires PHP: 5.6
     
    2222The output is a static page with the text `404 Not Found`, this text can be edited by filtering `static_404_message`.
    2323
     24
     25
     26
     27
    2428
    2529== Installation ==
     
    2731 - Install the plugin.
    2832 - Magically have faster and less expensive 404s!
     33
     34
     35
     36
     37
     38
     39
     40
     41
  • static-404/trunk/static-404.php

    r2539076 r2586072  
    33 * Plugin Name: Static 404
    44 * Description: Quickly output a 404 for static files that aren't found, rather than loading the normal 404 page.
    5  * Version:     1.0.3
     5 * Version:     1.
    66 * Author:      Brad Parbs
    77 * Author URI:  https://bradparbs.com/
     
    1313 */
    1414
    15 if ( ! defined( 'ABSPATH' ) ) {
    16     exit;
     15namespace Static404;
     16
     17defined( '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 */
     24add_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 */
     30function 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() );
    1755}
    1856
    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 */
     63function get_early_action_to_use() {
     64    if ( ! did_action( 'muplugins_loaded' )  ) {
     65        return 'muplugins_loaded';
     66    } else {
     67        return 'plugins_loaded';
     68    }
     69}
    2270
    2371/**
    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.
    2675 */
    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         }
     76function 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 ) );
    3679
    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}
    5483
    5584/**
     
    5988 * @return array Array of file extensions that could be static-404ed.
    6089 */
    61 function static_404_get_extensions() {
    62 
     90function get_extensions() {
    6391    $wp_ext_types = wp_get_ext_types();
    6492    $extensions   = [];
     
    84112 * @return string 404 error message.
    85113 */
    86 function static_404_get_message() {
     114function get_message() {
    87115    return apply_filters( 'static_404_message', '404 ' . get_status_header_desc( 404 ) );
    88116}
     117
     118
     119
     120
     121
     122
     123
     124
     125
Note: See TracChangeset for help on using the changeset viewer.