Skip to content

Commit 79b19f7

Browse files
Fix 0.4.9 custom post type archive redirect (#43)
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.
1 parent 8cbd4a7 commit 79b19f7

File tree

7 files changed

+56
-50
lines changed

7 files changed

+56
-50
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
### Changelog
22

3+
##### 0.4.10
4+
- 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.
5+
36
##### 0.4.9
47
- **Notice:** We've added the minimum PHP version requirement of 5.3, which was not explicitly set before now.
58
- **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.**

disable-blog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Plugin Name: Disable Blog
1616
* Plugin URI: https://wordpress.org/plugins/disable-blog/
1717
* Description: Go blog-less with WordPress. This plugin disables all blog-related functionality (by hiding, removing, and redirecting).
18-
* Version: 0.4.9
18+
* Version: 0.4.10
1919
* Author: Joshua Nelson
2020
* Author URI: http://joshuadnelson.com
2121
* License: GPL-2.0+

includes/class-disable-blog-admin.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public function modify_post_type_arguments() {
9191

9292
// remove supports.
9393
$wp_post_types['post']->supports = array();
94+
9495
}
9596

9697
}

includes/class-disable-blog-public.php

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ public function redirect_posts() {
193193
*
194194
* @since 0.2.0
195195
* @since 0.4.0 added remove_post_from_array_in_query function
196-
* @since 0.4.9 remove 'post' from all archives
196+
* @since 0.4.9 remove 'post' from all archives.
197+
* @since 0.4.10 update to just remove 'post' from built-in taxonomy archives,
197198
* @param object $query the query object.
198199
* @return void
199200
*/
@@ -204,65 +205,55 @@ public function modify_query( $query ) {
204205
return;
205206
}
206207

207-
// Remove 'post' post_type from search results, replace with page.
208-
if ( $query->is_search() ) {
209-
$in_search_post_types = get_post_types(
210-
array(
211-
'exclude_from_search' => false,
212-
'public' => true,
213-
'publicly_queryable' => true,
214-
)
215-
);
216-
$this->remove_post_from_array_in_query( $query, $in_search_post_types, 'dwpb_search_post_types' );
217-
}
208+
// Let's see if there are any post types supporting build-in taxonomies.
209+
$tag_post_types = dwpb_post_types_with_tax( 'post_tag' );
210+
$category_post_types = dwpb_post_types_with_tax( 'category' );
211+
212+
// Remove existing posts from built-in taxonomy archives, if they are supported by another post type.
213+
if ( $query->is_tag() && $tag_post_types ) {
214+
215+
$this->set_post_types_in_query( $query, $tag_post_types, 'dwpb_tag_post_types' );
216+
217+
} elseif ( $query->is_category() && $category_post_types ) {
218+
219+
$this->set_post_types_in_query( $query, $category_post_types, 'dwpb_category_post_types' );
218220

219-
// Remove Posts from archives.
220-
if ( $query->is_archive() ) {
221-
$archive_post_types = get_post_types(
222-
array(
223-
'publicly_queryable' => true,
224-
)
225-
);
226-
$this->remove_post_from_array_in_query( $query, $archive_post_types, 'dwpb_archive_post_types' );
227221
}
228222

229223
}
230224

231225
/**
232-
* Remove post type from query array.
226+
* Set post types for tag and category archive queries, excluding 'post' as the default type.
233227
*
234-
* Used in $this->modify_query to remove 'post' type from specific queries.
228+
* Used in $this->modify_query to remove 'post' type from built-in archive queries.
235229
*
236230
* @since 0.4.0
237231
*
238-
* @param object $query the main query object.
239-
* @param array $array the array of post types.
240-
* @param string $filter the filter to be applied.
232+
* @param object $query the main query object.
233+
* @param array $post_types the array of post types.
234+
* @param string $filter the filter to be applied.
241235
*
242236
* @return bool
243237
*/
244-
public function remove_post_from_array_in_query( $query, $array, $filter = '' ) {
245-
246-
if ( is_array( $array ) && in_array( 'post', $array, true ) ) {
247-
unset( $array['post'] );
238+
public function set_post_types_in_query( $query, $post_types = array(), $filter = '' ) {
248239

249-
/**
250-
* If there is a filter name passed, then a filter is applied on the array and query.
251-
*
252-
* Used for 'dwpb_search_post_types' and 'dwpb_author_post_types' filters.
253-
*
254-
* @see Disable_Blog_Public->modify_query
255-
*
256-
* @since 0.4.0
257-
*
258-
* @param array $array
259-
* @param object $query
260-
*/
261-
$set_to = empty( $filter ) ? $array : apply_filters( $filter, $array, $query );
262-
if ( ! empty( $set_to ) && method_exists( $query, 'set' ) ) {
263-
$query->set( 'post_type', $set_to );
264-
return true;
265-
}
240+
/**
241+
* If there is a filter name passed, then a filter is applied on the array and query.
242+
*
243+
* Used for 'dwpb_tag_post_types' and 'dwpb_category_post_types' filters.
244+
*
245+
* @see Disable_Blog_Public->modify_query
246+
*
247+
* @since 0.4.0
248+
* @since 0.4.10 fix bug in 0.4.9 causing cpt weirdness, now always using the filter.
249+
*
250+
* @param array $array
251+
* @param object $query
252+
*/
253+
$set_to = apply_filters( $filter, $post_types, $query );
254+
if ( ! empty( $set_to ) && method_exists( $query, 'set' ) ) {
255+
$query->set( 'post_type', $set_to );
256+
return true;
266257
}
267258

268259
return false;

includes/class-disable-blog.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class Disable_Blog {
7676
public function __construct() {
7777

7878
$this->plugin_name = 'disable-blog';
79-
$this->version = '0.4.9';
79+
$this->version = '0.4.10';
8080

8181
do_action( 'dwpb_init' );
8282

@@ -98,6 +98,11 @@ public function __construct() {
9898
*/
9999
private static function upgrade_check() {
100100

101+
// let's only run these checks on the admin page load.
102+
if ( ! is_admin() ) {
103+
return;
104+
}
105+
101106
// Get the current version option.
102107
$current_version = get_option( 'dwpb_version', false );
103108

readme.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Tags: remove blog, disable blog, disable settings, disable blogging, disable fee
55
Requires at least: 3.1.0
66
Requires PHP: 5.3
77
Tested up to: 5.5.1
8-
Stable tag: 0.4.9
8+
Stable tag: 0.4.10
99
License: GPLv2 or later
1010
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1111

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

6262
== Changelog ==
6363

64+
= 0.4.10 =
65+
- 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.
66+
6467
= 0.4.9 =
6568
- **Notice:** We've added the minimum PHP version requirement of 5.3, which was not explicitly set before now.
6669
- **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.**
@@ -173,6 +176,9 @@ A bunch of stuff:
173176

174177
== Upgrade Notice ==
175178

179+
= 0.4.10 =
180+
- Fix a bug from v0.4.9 that caused redirects on custom post type archives.
181+
176182
= 0.4.9 =
177183
- **Notice:** We've added the minimum PHP version requirement of 5.3, which was not explicitly set before now.
178184
- **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.**

tests/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
* @package Disable_Blog
77
*/
88

9-
define( 'DWPB_VERSION', '0.4.9' );
9+
define( 'DWPB_VERSION', '0.4.10' );

0 commit comments

Comments
 (0)