-
Notifications
You must be signed in to change notification settings - Fork 2
/
bbpress-better-permalinks.php
129 lines (102 loc) · 3.24 KB
/
bbpress-better-permalinks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
* Plugin Name: Bonnier bbPress better permalinks
* Version: 0.1.5
* Plugin URI: https://github.com/BenjaminMedia/bbpress-better-permalinks
* Description: This plugin alters the url structure for topics in bbpress so they belong to the forum they are attached to
* Author: Bonnier - Alf Henderson
* 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 FLUSH_REWRITE_RULES_FLAG = 'bbpress-better-permalinks-rewrite-flush-rules-flag';
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();
}
// Register a flag to flush the rewrite rules after the custom rules have been added
register_activation_hook( __FILE__, function(){
update_option( Plugin::FLUSH_REWRITE_RULES_FLAG, true );
});
// Flush rewrite rules to generate new permalinks when plugin is deactivated
register_deactivation_hook( __FILE__, 'flush_rewrite_rules');
// If the plugin is currently being deactivated we do no want to register our
if (isset($_GET['action'], $_GET['plugin']) && 'deactivate' === $_GET['action'] && plugin_basename(__FILE__) === $_GET['plugin'])
return;
add_action('plugins_loaded', __NAMESPACE__ . '\instance');