Skip to content

Commit

Permalink
initial working commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alfhen committed Dec 23, 2016
0 parents commit 573ad59
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# bbpress-better-permalinks
114 changes: 114 additions & 0 deletions bbpress-better-permalinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/**
* Plugin Name: Bonnier bbPress better permalinks
* Version: 0.1.0
* Plugin URI: https://github.com/BenjaminMedia/bbpress-better-permalinks
* Description: This plugin gives you the ability to select a post in bbPress as the best answer
* Author: Bonnier - Michael Sørensen
* License: MIT
*/

namespace Bonnier\WP\BBPress\BetterPermalinks;


// Do not access this file directly
if (!defined('ABSPATH')) {
exit;
}

// Handle autoload so we can use namespaces
spl_autoload_register(function ($className) {
if (strpos($className, __NAMESPACE__) !== false) {
$className = str_replace("\\", DIRECTORY_SEPARATOR, $className);
require_once(__DIR__ . DIRECTORY_SEPARATOR . Plugin::CLASS_DIR . DIRECTORY_SEPARATOR . $className . '.php');
}
});


class Plugin
{
/**
* Text domain for translators
*/
const TEXT_DOMAIN = 'bbpress-better-permalinks';

const CLASS_DIR = 'src';

/**
* @var object Instance of this class.
*/
private static $instance;

public $settings;

/**
* @var string Filename of this class.
*/
public $file;

/**
* @var string Basename of this class.
*/
public $basename;

/**
* @var string Plugins directory for this plugin.
*/
public $plugin_dir;

/**
* @var string Plugins url for this plugin.
*/
public $plugin_url;

/**
* Do not load this more than once.
*/
private function __construct()
{
// Set plugin file variables
$this->file = __FILE__;
$this->basename = plugin_basename($this->file);
$this->plugin_dir = plugin_dir_path($this->file);
$this->plugin_url = plugin_dir_url($this->file);

// Load textdomain
load_plugin_textdomain(self::TEXT_DOMAIN, false, dirname($this->basename) . '/languages');
}

private function bootstrap()
{
Permalinks::bootstrap();
}

/**
* Returns the instance of this class.
*/
public static function instance()
{
if (!self::$instance) {
self::$instance = new self;
global $bbpress_better_permalinks;
$bbpress_better_permalinks = self::$instance;
self::$instance->bootstrap();

/**
* Run after the plugin has been loaded.
*/
do_action('bbpress_better_permalinks_loaded');
}

return self::$instance;
}

}

/**
* @return Plugin $instance returns an instance of the plugin
*/
function instance()
{
return Plugin::instance();
}

add_action('plugins_loaded', __NAMESPACE__ . '\instance', 0);
36 changes: 36 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"name": "bonnier/bbpress-better-permalinks",
"description": "Better permalinks for topics in bbPress",
"type": "wordpress-plugin",
"keywords": ["wordpress", "plugin", "bbPress"],
"homepage": "https://github.com/BenjaminMedia/bbpress-better-permalinks",
"repositories":[
{
"type": "composer",
"url": "http://wpackagist.org"
}
],
"authors": [
{
"name": "Bonnier Publications",
"email": "[email protected]",
"homepage": "http://bonnierpublications.com"
},
{
"name": "Alf Henderson",
"email": "[email protected]"
}
],
"support": {
"issues": "https://github.com/BenjaminMedia/bbpress-better-permalinks/issues",
"source": "https://github.com/BenjaminMedia/bbpress-better-permalinks"
},
"autoload": {
"psr-4": {
"Bonnier\\WP\\BBPress\\BetterPermalinks\\": "src/Bonnier/WP/BBPress/BetterPermalinks/"
}
},
"require": {
"php": ">=5.6"
}
}
107 changes: 107 additions & 0 deletions src/Bonnier/WP/BBPress/BetterPermalinks/Permalinks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Bonnier\WP\BBPress\BetterPermalinks;

/**
* Class Permalinks
*
* @package \Bonnier\WP\BBPress\BetterPermalinks
*/
class Permalinks
{
public static function bootstrap()
{
if (!did_action('bbp_after_setup_actions')) {
add_action('bbp_after_setup_actions', [__CLASS__, 'after_bbPress_setup']);
} else {
static::after_bbPress_setup();
}
}

/**
* After bbPress Setup.
*
* @author Nashwan Doaqan
*/
static function after_bbPress_setup()
{

if (function_exists('is_bbpress')) {
add_action('registered_post_type', [__CLASS__, 'add_post_types_rewrite'], 1, 2);
add_filter('post_type_link', [__CLASS__, 'filter_post_type_link'], 1, 2);
}

}

/**
* Add the forum parents slug structure to bbPress topics
*
*
* @param $postType
* @param $args
*/
static function add_post_types_rewrite($postType, $args)
{
switch ($postType) {
case bbp_get_topic_post_type():
add_rewrite_rule(
'^'.bbp_get_forum_slug().'(.+?)'.bbp_get_topic_slug().'/(.+?)/?$',
'index.php?forumnames=$matches[1]&name=$matches[2]&post_type='.$postType,
'top'
);
add_permastruct($postType, bbp_get_forum_slug()."%forumnames%".bbp_get_topic_slug()."/%postname%/", $args->rewrite);
break;
}
}

/**
* Change bbPress topic links to prefix forum slugs
*
* @author Nashwan Doaqan
*
* @param $post_link
* @param $_post
*
* @return mixed|string|void
*/
static function filter_post_type_link($post_link, $_post)
{
global $wp_rewrite;
if (empty($_post) || $_post->post_status === 'auto-draft' || strpos('post_type', $post_link))
return $post_link;
switch ($_post->post_type) {
case bbp_get_topic_post_type():
$post_link = $wp_rewrite->get_extra_permastruct($_post->post_type);
$post_link = str_replace("%forumnames%", static::get_topic_parent_forums_slug($_post->ID), $post_link);
$post_link = str_replace("%postname%", static::get_topic_name_slug($_post), $post_link);
$post_link = home_url(user_trailingslashit($post_link));
break;
}
return $post_link;

}

static function get_topic_parent_forums_slug($topicId) {
$forumId = bbp_get_topic_forum_id($topicId);
$forumSlugs = [];
if($forumId === 0) { // means the topic belongs to no forum
return '/no-forum/';
}
$forum = get_post($forumId);
$hasParent = true;
while ($hasParent) {
$forumSlugs[] = $forum->post_name;
if($forum->post_parent === 0) {
$hasParent = false;
} else {
$forum = get_post($forum->post_parent);
}
}
return '/' . implode('/', array_reverse($forumSlugs)) . '/';
}

static function get_topic_name_slug($post) {
return empty($post->post_name) ? sanitize_title_with_dashes($post->post_title) : $post->post_name;
}

}

0 comments on commit 573ad59

Please sign in to comment.