Skip to content

Commit

Permalink
Fix 0.4.9 custom post type archive redirect (#43)
Browse files Browse the repository at this point in the history
Fix a bug from v0.4.9 that caused redirects on custom post type archives, correcting the modify_query function to only remove posts from built-in taxonomy archives, as that was the original intent.
  • Loading branch information
joshuadavidnelson authored Sep 14, 2020
1 parent 8cbd4a7 commit 79b19f7
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 50 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
### Changelog

##### 0.4.10
- Fix a bug from v0.4.9 that caused redirects on custom post type archives, correcting the `modify_query` function to only remove posts from built-in taxonomy archives, as that was the original intent.

##### 0.4.9
- **Notice:** We've added the minimum PHP version requirement of 5.3, which was not explicitly set before now.
- **Big change:** the plugin now changes the `post_type` arguments for posts so they are no longer public and removes all post_type support parameters. This disables the post-related admin redirects, as WordPress will now show users an error page stating "Sorry, you are not allowed to edit posts in this post type." It also pulls posts out of a lot of other locations (menus, etc) and is a much more efficient method of "disabling" the post type. This method is also used on built-in taxonomies, unless another post type supports them. **This change may impact other plugins or themes, be sure to back up your site and, if you can, test these changes prior to updating the plugin on a production site.**
Expand Down
2 changes: 1 addition & 1 deletion disable-blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* Plugin Name: Disable Blog
* Plugin URI: https://wordpress.org/plugins/disable-blog/
* Description: Go blog-less with WordPress. This plugin disables all blog-related functionality (by hiding, removing, and redirecting).
* Version: 0.4.9
* Version: 0.4.10
* Author: Joshua Nelson
* Author URI: http://joshuadnelson.com
* License: GPL-2.0+
Expand Down
1 change: 1 addition & 0 deletions includes/class-disable-blog-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public function modify_post_type_arguments() {

// remove supports.
$wp_post_types['post']->supports = array();

}

}
Expand Down
83 changes: 37 additions & 46 deletions includes/class-disable-blog-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ public function redirect_posts() {
*
* @since 0.2.0
* @since 0.4.0 added remove_post_from_array_in_query function
* @since 0.4.9 remove 'post' from all archives
* @since 0.4.9 remove 'post' from all archives.
* @since 0.4.10 update to just remove 'post' from built-in taxonomy archives,
* @param object $query the query object.
* @return void
*/
Expand All @@ -204,65 +205,55 @@ public function modify_query( $query ) {
return;
}

// Remove 'post' post_type from search results, replace with page.
if ( $query->is_search() ) {
$in_search_post_types = get_post_types(
array(
'exclude_from_search' => false,
'public' => true,
'publicly_queryable' => true,
)
);
$this->remove_post_from_array_in_query( $query, $in_search_post_types, 'dwpb_search_post_types' );
}
// Let's see if there are any post types supporting build-in taxonomies.
$tag_post_types = dwpb_post_types_with_tax( 'post_tag' );
$category_post_types = dwpb_post_types_with_tax( 'category' );

// Remove existing posts from built-in taxonomy archives, if they are supported by another post type.
if ( $query->is_tag() && $tag_post_types ) {

$this->set_post_types_in_query( $query, $tag_post_types, 'dwpb_tag_post_types' );

} elseif ( $query->is_category() && $category_post_types ) {

$this->set_post_types_in_query( $query, $category_post_types, 'dwpb_category_post_types' );

// Remove Posts from archives.
if ( $query->is_archive() ) {
$archive_post_types = get_post_types(
array(
'publicly_queryable' => true,
)
);
$this->remove_post_from_array_in_query( $query, $archive_post_types, 'dwpb_archive_post_types' );
}

}

/**
* Remove post type from query array.
* Set post types for tag and category archive queries, excluding 'post' as the default type.
*
* Used in $this->modify_query to remove 'post' type from specific queries.
* Used in $this->modify_query to remove 'post' type from built-in archive queries.
*
* @since 0.4.0
*
* @param object $query the main query object.
* @param array $array the array of post types.
* @param string $filter the filter to be applied.
* @param object $query the main query object.
* @param array $post_types the array of post types.
* @param string $filter the filter to be applied.
*
* @return bool
*/
public function remove_post_from_array_in_query( $query, $array, $filter = '' ) {

if ( is_array( $array ) && in_array( 'post', $array, true ) ) {
unset( $array['post'] );
public function set_post_types_in_query( $query, $post_types = array(), $filter = '' ) {

/**
* If there is a filter name passed, then a filter is applied on the array and query.
*
* Used for 'dwpb_search_post_types' and 'dwpb_author_post_types' filters.
*
* @see Disable_Blog_Public->modify_query
*
* @since 0.4.0
*
* @param array $array
* @param object $query
*/
$set_to = empty( $filter ) ? $array : apply_filters( $filter, $array, $query );
if ( ! empty( $set_to ) && method_exists( $query, 'set' ) ) {
$query->set( 'post_type', $set_to );
return true;
}
/**
* If there is a filter name passed, then a filter is applied on the array and query.
*
* Used for 'dwpb_tag_post_types' and 'dwpb_category_post_types' filters.
*
* @see Disable_Blog_Public->modify_query
*
* @since 0.4.0
* @since 0.4.10 fix bug in 0.4.9 causing cpt weirdness, now always using the filter.
*
* @param array $array
* @param object $query
*/
$set_to = apply_filters( $filter, $post_types, $query );
if ( ! empty( $set_to ) && method_exists( $query, 'set' ) ) {
$query->set( 'post_type', $set_to );
return true;
}

return false;
Expand Down
7 changes: 6 additions & 1 deletion includes/class-disable-blog.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Disable_Blog {
public function __construct() {

$this->plugin_name = 'disable-blog';
$this->version = '0.4.9';
$this->version = '0.4.10';

do_action( 'dwpb_init' );

Expand All @@ -98,6 +98,11 @@ public function __construct() {
*/
private static function upgrade_check() {

// let's only run these checks on the admin page load.
if ( ! is_admin() ) {
return;
}

// Get the current version option.
$current_version = get_option( 'dwpb_version', false );

Expand Down
8 changes: 7 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: remove blog, disable blog, disable settings, disable blogging, disable fee
Requires at least: 3.1.0
Requires PHP: 5.3
Tested up to: 5.5.1
Stable tag: 0.4.9
Stable tag: 0.4.10
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -61,6 +61,9 @@ There are numerous filters available to change the way this plugin works. Refer

== Changelog ==

= 0.4.10 =
- Fix a bug from v0.4.9 that caused redirects on custom post type archives, correcting the `modify_query` function to only remove posts from built-in taxonomy archives, as that was the original intent.

= 0.4.9 =
- **Notice:** We've added the minimum PHP version requirement of 5.3, which was not explicitly set before now.
- **Big change:** the plugin now changes the `post_type` arguments for posts so they are no longer public and removes all post_type support parameters. This disables the post-related admin redirects, as WordPress will now show users an error page stating "Sorry, you are not allowed to edit posts in this post type." It also pulls posts out of a lot of other locations (menus, etc) and is a much more efficient method of "disabling" the post type. This method is also used on built-in taxonomies, unless another post type supports them. **This change may impact other plugins or themes, be sure to back up your site and, if you can, test these changes prior to updating the plugin on a production site.**
Expand Down Expand Up @@ -173,6 +176,9 @@ A bunch of stuff:

== Upgrade Notice ==

= 0.4.10 =
- Fix a bug from v0.4.9 that caused redirects on custom post type archives.

= 0.4.9 =
- **Notice:** We've added the minimum PHP version requirement of 5.3, which was not explicitly set before now.
- **Big change:** the plugin now changes the `post_type` arguments for posts so they are no longer public and removes all post_type support parameters. This disables the post-related admin redirects, as WordPress will now show users an error page stating "Sorry, you are not allowed to edit posts in this post type." It also pulls posts out of a lot of other locations (menus, etc) and is a much more efficient method of "disabling" the post type. This method is also used on built-in taxonomies, unless another post type supports them. **This change may impact other plugins or themes, be sure to back up your site and, if you can, test these changes prior to updating the plugin on a production site.**
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
* @package Disable_Blog
*/

define( 'DWPB_VERSION', '0.4.9' );
define( 'DWPB_VERSION', '0.4.10' );

0 comments on commit 79b19f7

Please sign in to comment.