Skip to content

Commit a03633e

Browse files
authored
Merge pull request #1 from BenjaminMedia/feature-flush-rewrite-rules
Added support for flushing rewrite rules on activation/deactvation
2 parents 573ad59 + 0901a38 commit a03633e

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

bbpress-better-permalinks.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
/**
33
* Plugin Name: Bonnier bbPress better permalinks
4-
* Version: 0.1.0
4+
* Version: 0.1.1
55
* Plugin URI: https://github.com/BenjaminMedia/bbpress-better-permalinks
6-
* Description: This plugin gives you the ability to select a post in bbPress as the best answer
7-
* Author: Bonnier - Michael Sørensen
6+
* Description: This plugin alters the url structure for topics in bbpress so they belong to the forum they are attached to
7+
* Author: Bonnier - Alf Henderson
88
* License: MIT
99
*/
1010

@@ -31,6 +31,7 @@ class Plugin
3131
* Text domain for translators
3232
*/
3333
const TEXT_DOMAIN = 'bbpress-better-permalinks';
34+
const FLUSH_REWRITE_RULES_FLAG = 'bbpress-better-permalinks-rewrite-flush-rules-flag';
3435

3536
const CLASS_DIR = 'src';
3637

@@ -111,4 +112,18 @@ function instance()
111112
return Plugin::instance();
112113
}
113114

114-
add_action('plugins_loaded', __NAMESPACE__ . '\instance', 0);
115+
// Register a flag to flush the rewrite rules after the custom rules have been added
116+
register_activation_hook( __FILE__, function(){
117+
update_option( Plugin::FLUSH_REWRITE_RULES_FLAG, true );
118+
});
119+
120+
// Flush rewrite rules to generate new permalinks when plugin is deactivated
121+
register_deactivation_hook( __FILE__, 'flush_rewrite_rules');
122+
123+
// If the plugin is currently being deactivated we do no want to register our
124+
if (isset($_GET['action'], $_GET['plugin']) && 'deactivate' === $_GET['action'] && plugin_basename(__FILE__) === $_GET['plugin'])
125+
return;
126+
127+
add_action('plugins_loaded', __NAMESPACE__ . '\instance');
128+
129+

src/Bonnier/WP/BBPress/BetterPermalinks/Permalinks.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@ public static function bootstrap()
2323
*
2424
* @author Nashwan Doaqan
2525
*/
26-
static function after_bbPress_setup()
26+
public static function after_bbPress_setup()
2727
{
28-
2928
if (function_exists('is_bbpress')) {
3029
add_action('registered_post_type', [__CLASS__, 'add_post_types_rewrite'], 1, 2);
3130
add_filter('post_type_link', [__CLASS__, 'filter_post_type_link'], 1, 2);
3231
}
33-
3432
}
3533

3634
/**
@@ -40,7 +38,7 @@ static function after_bbPress_setup()
4038
* @param $postType
4139
* @param $args
4240
*/
43-
static function add_post_types_rewrite($postType, $args)
41+
public static function add_post_types_rewrite($postType, $args)
4442
{
4543
switch ($postType) {
4644
case bbp_get_topic_post_type():
@@ -50,6 +48,7 @@ static function add_post_types_rewrite($postType, $args)
5048
'top'
5149
);
5250
add_permastruct($postType, bbp_get_forum_slug()."%forumnames%".bbp_get_topic_slug()."/%postname%/", $args->rewrite);
51+
static::flush_rewrite_rules_if_needed();
5352
break;
5453
}
5554
}
@@ -64,11 +63,12 @@ static function add_post_types_rewrite($postType, $args)
6463
*
6564
* @return mixed|string|void
6665
*/
67-
static function filter_post_type_link($post_link, $_post)
66+
public static function filter_post_type_link($post_link, $_post)
6867
{
6968
global $wp_rewrite;
70-
if (empty($_post) || $_post->post_status === 'auto-draft' || strpos('post_type', $post_link))
69+
if (empty($_post) || $_post->post_status === 'auto-draft' || strpos('post_type', $post_link)) {
7170
return $post_link;
71+
}
7272
switch ($_post->post_type) {
7373
case bbp_get_topic_post_type():
7474
$post_link = $wp_rewrite->get_extra_permastruct($_post->post_type);
@@ -78,20 +78,20 @@ static function filter_post_type_link($post_link, $_post)
7878
break;
7979
}
8080
return $post_link;
81-
8281
}
8382

84-
static function get_topic_parent_forums_slug($topicId) {
83+
public static function get_topic_parent_forums_slug($topicId)
84+
{
8585
$forumId = bbp_get_topic_forum_id($topicId);
8686
$forumSlugs = [];
87-
if($forumId === 0) { // means the topic belongs to no forum
87+
if ($forumId === 0) { // means the topic belongs to no forum
8888
return '/no-forum/';
8989
}
9090
$forum = get_post($forumId);
9191
$hasParent = true;
9292
while ($hasParent) {
9393
$forumSlugs[] = $forum->post_name;
94-
if($forum->post_parent === 0) {
94+
if ($forum->post_parent === 0) {
9595
$hasParent = false;
9696
} else {
9797
$forum = get_post($forum->post_parent);
@@ -100,8 +100,15 @@ static function get_topic_parent_forums_slug($topicId) {
100100
return '/' . implode('/', array_reverse($forumSlugs)) . '/';
101101
}
102102

103-
static function get_topic_name_slug($post) {
103+
public static function get_topic_name_slug($post)
104+
{
104105
return empty($post->post_name) ? sanitize_title_with_dashes($post->post_title) : $post->post_name;
105106
}
106107

108+
private static function flush_rewrite_rules_if_needed() {
109+
if ( get_option( Plugin::FLUSH_REWRITE_RULES_FLAG ) ) {
110+
flush_rewrite_rules();
111+
delete_option( Plugin::FLUSH_REWRITE_RULES_FLAG );
112+
}
113+
}
107114
}

0 commit comments

Comments
 (0)