Skip to content

Commit 573ad59

Browse files
committed
initial working commit
0 parents  commit 573ad59

File tree

4 files changed

+258
-0
lines changed

4 files changed

+258
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# bbpress-better-permalinks

bbpress-better-permalinks.php

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Plugin Name: Bonnier bbPress better permalinks
4+
* Version: 0.1.0
5+
* 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
8+
* License: MIT
9+
*/
10+
11+
namespace Bonnier\WP\BBPress\BetterPermalinks;
12+
13+
14+
// Do not access this file directly
15+
if (!defined('ABSPATH')) {
16+
exit;
17+
}
18+
19+
// Handle autoload so we can use namespaces
20+
spl_autoload_register(function ($className) {
21+
if (strpos($className, __NAMESPACE__) !== false) {
22+
$className = str_replace("\\", DIRECTORY_SEPARATOR, $className);
23+
require_once(__DIR__ . DIRECTORY_SEPARATOR . Plugin::CLASS_DIR . DIRECTORY_SEPARATOR . $className . '.php');
24+
}
25+
});
26+
27+
28+
class Plugin
29+
{
30+
/**
31+
* Text domain for translators
32+
*/
33+
const TEXT_DOMAIN = 'bbpress-better-permalinks';
34+
35+
const CLASS_DIR = 'src';
36+
37+
/**
38+
* @var object Instance of this class.
39+
*/
40+
private static $instance;
41+
42+
public $settings;
43+
44+
/**
45+
* @var string Filename of this class.
46+
*/
47+
public $file;
48+
49+
/**
50+
* @var string Basename of this class.
51+
*/
52+
public $basename;
53+
54+
/**
55+
* @var string Plugins directory for this plugin.
56+
*/
57+
public $plugin_dir;
58+
59+
/**
60+
* @var string Plugins url for this plugin.
61+
*/
62+
public $plugin_url;
63+
64+
/**
65+
* Do not load this more than once.
66+
*/
67+
private function __construct()
68+
{
69+
// Set plugin file variables
70+
$this->file = __FILE__;
71+
$this->basename = plugin_basename($this->file);
72+
$this->plugin_dir = plugin_dir_path($this->file);
73+
$this->plugin_url = plugin_dir_url($this->file);
74+
75+
// Load textdomain
76+
load_plugin_textdomain(self::TEXT_DOMAIN, false, dirname($this->basename) . '/languages');
77+
}
78+
79+
private function bootstrap()
80+
{
81+
Permalinks::bootstrap();
82+
}
83+
84+
/**
85+
* Returns the instance of this class.
86+
*/
87+
public static function instance()
88+
{
89+
if (!self::$instance) {
90+
self::$instance = new self;
91+
global $bbpress_better_permalinks;
92+
$bbpress_better_permalinks = self::$instance;
93+
self::$instance->bootstrap();
94+
95+
/**
96+
* Run after the plugin has been loaded.
97+
*/
98+
do_action('bbpress_better_permalinks_loaded');
99+
}
100+
101+
return self::$instance;
102+
}
103+
104+
}
105+
106+
/**
107+
* @return Plugin $instance returns an instance of the plugin
108+
*/
109+
function instance()
110+
{
111+
return Plugin::instance();
112+
}
113+
114+
add_action('plugins_loaded', __NAMESPACE__ . '\instance', 0);

composer.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "bonnier/bbpress-better-permalinks",
3+
"description": "Better permalinks for topics in bbPress",
4+
"type": "wordpress-plugin",
5+
"keywords": ["wordpress", "plugin", "bbPress"],
6+
"homepage": "https://github.com/BenjaminMedia/bbpress-better-permalinks",
7+
"repositories":[
8+
{
9+
"type": "composer",
10+
"url": "http://wpackagist.org"
11+
}
12+
],
13+
"authors": [
14+
{
15+
"name": "Bonnier Publications",
16+
"email": "[email protected]",
17+
"homepage": "http://bonnierpublications.com"
18+
},
19+
{
20+
"name": "Alf Henderson",
21+
"email": "[email protected]"
22+
}
23+
],
24+
"support": {
25+
"issues": "https://github.com/BenjaminMedia/bbpress-better-permalinks/issues",
26+
"source": "https://github.com/BenjaminMedia/bbpress-better-permalinks"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Bonnier\\WP\\BBPress\\BetterPermalinks\\": "src/Bonnier/WP/BBPress/BetterPermalinks/"
31+
}
32+
},
33+
"require": {
34+
"php": ">=5.6"
35+
}
36+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace Bonnier\WP\BBPress\BetterPermalinks;
4+
5+
/**
6+
* Class Permalinks
7+
*
8+
* @package \Bonnier\WP\BBPress\BetterPermalinks
9+
*/
10+
class Permalinks
11+
{
12+
public static function bootstrap()
13+
{
14+
if (!did_action('bbp_after_setup_actions')) {
15+
add_action('bbp_after_setup_actions', [__CLASS__, 'after_bbPress_setup']);
16+
} else {
17+
static::after_bbPress_setup();
18+
}
19+
}
20+
21+
/**
22+
* After bbPress Setup.
23+
*
24+
* @author Nashwan Doaqan
25+
*/
26+
static function after_bbPress_setup()
27+
{
28+
29+
if (function_exists('is_bbpress')) {
30+
add_action('registered_post_type', [__CLASS__, 'add_post_types_rewrite'], 1, 2);
31+
add_filter('post_type_link', [__CLASS__, 'filter_post_type_link'], 1, 2);
32+
}
33+
34+
}
35+
36+
/**
37+
* Add the forum parents slug structure to bbPress topics
38+
*
39+
*
40+
* @param $postType
41+
* @param $args
42+
*/
43+
static function add_post_types_rewrite($postType, $args)
44+
{
45+
switch ($postType) {
46+
case bbp_get_topic_post_type():
47+
add_rewrite_rule(
48+
'^'.bbp_get_forum_slug().'(.+?)'.bbp_get_topic_slug().'/(.+?)/?$',
49+
'index.php?forumnames=$matches[1]&name=$matches[2]&post_type='.$postType,
50+
'top'
51+
);
52+
add_permastruct($postType, bbp_get_forum_slug()."%forumnames%".bbp_get_topic_slug()."/%postname%/", $args->rewrite);
53+
break;
54+
}
55+
}
56+
57+
/**
58+
* Change bbPress topic links to prefix forum slugs
59+
*
60+
* @author Nashwan Doaqan
61+
*
62+
* @param $post_link
63+
* @param $_post
64+
*
65+
* @return mixed|string|void
66+
*/
67+
static function filter_post_type_link($post_link, $_post)
68+
{
69+
global $wp_rewrite;
70+
if (empty($_post) || $_post->post_status === 'auto-draft' || strpos('post_type', $post_link))
71+
return $post_link;
72+
switch ($_post->post_type) {
73+
case bbp_get_topic_post_type():
74+
$post_link = $wp_rewrite->get_extra_permastruct($_post->post_type);
75+
$post_link = str_replace("%forumnames%", static::get_topic_parent_forums_slug($_post->ID), $post_link);
76+
$post_link = str_replace("%postname%", static::get_topic_name_slug($_post), $post_link);
77+
$post_link = home_url(user_trailingslashit($post_link));
78+
break;
79+
}
80+
return $post_link;
81+
82+
}
83+
84+
static function get_topic_parent_forums_slug($topicId) {
85+
$forumId = bbp_get_topic_forum_id($topicId);
86+
$forumSlugs = [];
87+
if($forumId === 0) { // means the topic belongs to no forum
88+
return '/no-forum/';
89+
}
90+
$forum = get_post($forumId);
91+
$hasParent = true;
92+
while ($hasParent) {
93+
$forumSlugs[] = $forum->post_name;
94+
if($forum->post_parent === 0) {
95+
$hasParent = false;
96+
} else {
97+
$forum = get_post($forum->post_parent);
98+
}
99+
}
100+
return '/' . implode('/', array_reverse($forumSlugs)) . '/';
101+
}
102+
103+
static function get_topic_name_slug($post) {
104+
return empty($post->post_name) ? sanitize_title_with_dashes($post->post_title) : $post->post_name;
105+
}
106+
107+
}

0 commit comments

Comments
 (0)