Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Blog Category links not working - Issue 37 #43

Merged
merged 2 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/includes/class-boldgrid-framework-404.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function boldgrid_404_template() {

<?php the_widget( 'WP_Widget_Recent_Posts' ); ?>

<?php if ( boldgrid_categorized_blog() ) : // Only show the widget if site has multiple categories. ?>
<div class="widget widget_categories">
<h2 class="widget-title"><?php esc_html_e( 'Most Used Categories', 'crio' ); ?></h2>
<ul>
Expand All @@ -80,7 +79,6 @@ public function boldgrid_404_template() {
?>
</ul>
</div><!-- .widget -->
<?php endif; ?>

<?php
/* translators: %1$s: smiley */
Expand Down
54 changes: 54 additions & 0 deletions src/includes/class-boldgrid-framework-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,58 @@ public function remove_hooks() {
remove_all_filters( 'plugins_api', 11 );
}
}

/**
* Filter category rewrite rules
*
* @since 2.19.1
*
* @param array $rules Rewrite rules.
*
* @return array $rules Filtered Rewrite rules.
*/
public function category_rewrite_rules( $rules ) {
if ( '.' !== get_option( 'category_base' ) ) {
return $rules;
}
$category_rewrite = array();
$categories = get_categories( array( 'hide_empty' => false ) );

foreach ( $categories as $category ) {
$category_nicename = $category->slug;
if ( $category->parent === $category->cat_ID ) {
$category->parent = 0;
} elseif ( 0 !== $category->parent ) {
$category_nicename = get_category_parents( $category->parent, false, '/', true ) . $category_nicename;
}

$category_rewrite[ '(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';

$category_rewrite[ '(' . $category_nicename . ')/page/?([0-9]{1,})/?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[2]';

$category_rewrite[ '(' . $category_nicename . ')/?$' ] = 'index.php?category_name=$matches[1]';
}

global $wp_rewrite;

$old_base = $wp_rewrite->get_category_permastruct();
$old_base = str_replace( '%category%', '(.+)', $old_base );
$old_base = trim( $old_base, '/' );

$category_rewrite[ $old_base . '$' ] = 'index.php?category_redirect=$matches[1]';

return $category_rewrite;
}

/**
* Schedule Flush
*
* Flush rewrite rules on category additions,
* changes, or deletions.
*
* @since 2.19.1
*/
public function schedule_flush() {
add_action( 'shutdown', 'flush_rewrite_rules' );
}
}
2 changes: 0 additions & 2 deletions src/includes/class-boldgrid-framework-comments.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,6 @@ public function boldgrid_bootstrap_comment( $comment, $args, $depth ) {
$button_class = get_theme_mod( 'bgtfw_comment_reply_button_class', 'button-primary' );
$classes = apply_filters( 'bgtfw_button_classes', array() );

error_log( 'button_class: ' . $button_class );
error_log( 'isset button_class: ' . isset( $classes[ $button_class ] ) );
if ( isset( $classes[ $button_class ] ) ) {
$comment_reply_link = preg_replace(
'/comment-reply-link/',
Expand Down
6 changes: 6 additions & 0 deletions src/includes/class-boldgrid-framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,12 @@ private function define_admin_hooks() {
$this->loader->add_action( 'init', $admin, 'remove_hooks' );

$this->loader->add_action( 'bgtfw_pro_feature_cards', $pro_feature_cards, 'print_cards' );

// Filter category rewrite rules.
$this->loader->add_filter( 'category_rewrite_rules', $admin, 'category_rewrite_rules' );
$this->loader->add_action( 'created_category', $admin, 'schedule_flush' );
$this->loader->add_action( 'edited_category', $admin, 'schedule_flush' );
$this->loader->add_action( 'delete_category', $admin, 'schedule_flush' );
}

/**
Expand Down
32 changes: 1 addition & 31 deletions src/includes/partials/template-tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function boldgrid_entry_footer() {
$categories_list = get_the_category_list( esc_html__( ', ', 'crio' ) );
$categories_count = count( explode( ', ', $categories_list ) );

if ( $categories_list && boldgrid_categorized_blog() ) {
if ( $categories_list ) {
$class = 'singular';
$icon = is_single() ? get_theme_mod( 'bgtfw_posts_cat_icon' ) : get_theme_mod( 'bgtfw_blog_post_cat_icon' );

Expand Down Expand Up @@ -236,36 +236,6 @@ function boldgrid_entry_footer() {
}
endif;

/**
* Returns true if a blog has more than 1 category.
*
* @return bool
*/
function boldgrid_categorized_blog() {
if ( false === ( $all_the_cool_cats = get_transient( 'boldgrid_categories' ) ) ) {
// Create an array of all the categories that are attached to posts.
$all_the_cool_cats = get_categories( array(
'fields' => 'ids',
'hide_empty' => 1,
// We only need to know if there is more than one category.
'number' => 2,
) );

// Count the number of categories that are attached to the posts.
$all_the_cool_cats = count( $all_the_cool_cats );

set_transient( 'boldgrid_categories', $all_the_cool_cats );
}

if ( $all_the_cool_cats > 1 ) {
// This blog has more than 1 category so boldgrid_categorized_blog should return true.
return true;
} else {
// This blog has only 1 category so boldgrid_categorized_blog should return false.
return false;
}
}

/**
* Flush out the transients used in boldgrid_categorized_blog.
*/
Expand Down