diff --git a/README.md b/README.md index 90d33d3..f09132f 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ **Donate link:** https://indieweb.org/how-to-sponsor **Tags:** deleted posts, 410, 451, indieweb **Requires at least:** 4.6 -**Tested up to:** 4.6.2 -**Stable tag:** 1.0.0 +**Tested up to:** 5.0.3 +**Stable tag:** 1.0.1 **License:** MIT **License URI:** http://opensource.org/licenses/MIT diff --git a/deleted-posts.php b/deleted-posts.php index 77cd34b..fd3f383 100644 --- a/deleted-posts.php +++ b/deleted-posts.php @@ -5,66 +5,60 @@ * Description: Sends a HTTP 410 (Gone) response to requests for trashed posts/pages. * Author: IndieWordPress Team * Author URI: https://github.com/indiewordpress - * Version: 1.0.0 + * Version: 1.0.1 * License: MIT * License URI: http://opensource.org/licenses/MIT * Text Domain: deleted-posts */ -add_action( 'plugins_loaded', array( 'DeletedPosts_Plugin', 'init' ) ); +add_action( 'template_redirect', 'IndieWordPress_handle_410', 99 ); /** * Deleted Posts Plugin Class * * @author Matthias Pfefferle */ -class DeletedPosts_Plugin { - - /** - * Initialize Deleted Posts Plugin - */ - public static function init() { - add_action( 'template_redirect', array( 'DeletedPosts_Plugin', 'handle_410' ), 99 ); +function IndieWordPress_handle_410() { + + if ( ! is_404() ) { + return; } - public static function handle_410() { - if ( ! is_404() ) { - return; - } + global $wp_query; - global $wp_query; + $defaultArguments = [ + 'pagename' => ( empty( $wp_query->query['pagename'] ) ? $wp_query->query['pagename'] : false ), + 'name' => ( empty( $wp_query->query['name'] ) ? $wp_query->query['name'] : false), + 'post_status' => 'trash', + ]; - // check slug - if ( ! empty( $wp_query->query['pagename'] ) ) { - $query = new WP_Query( - array( - 'pagename' => $wp_query->query['pagename'] . '__trashed', - 'post_status' => 'trash', - ) - ); - } elseif ( ! empty( $wp_query->query['name'] ) ) { - $query = new WP_Query( - array( - 'name' => $wp_query->query['name'] . '__trashed', - 'post_status' => 'trash', - ) - ); - } else { - return; + $filteredArguments = array_filter($defaultArguments, function($value){ + if($value === false || $value == 'trash'){ + return $value; } - // return 410 - if ( $query->get_posts() ) { - status_header( 410 ); + return $value . '__trashed'; + }); - // check if theme has a 410.php template - $template_410 = get_query_template( 410 ); + if ( count($filteredArguments) <= 1 ) { + return; + } - // return 410 template - if ( $template_410 ) { - load_template( $template_410 ); - exit; - } - } + $query = new WP_Query($filteredArguments); + + // return 410 + if ( count($query->get_posts()) <= 0 ) { + return; + } + + status_header( 410 ); + + // check if theme has a 410.php template + $template_410 = get_query_template( 410 ); + + // return 410 template + if ( $template_410 ) { + load_template( $template_410 ); + exit; } } diff --git a/readme.txt b/readme.txt index f8f5e53..deae230 100755 --- a/readme.txt +++ b/readme.txt @@ -4,7 +4,7 @@ Donate link: https://indieweb.org/how-to-sponsor Tags: deleted posts, 410, 451, indieweb Requires at least: 4.6 Tested up to: 5.0.3 -Stable tag: 1.0.0 +Stable tag: 1.0.1 License: MIT License URI: http://opensource.org/licenses/MIT