-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin.php
More file actions
259 lines (241 loc) · 7.54 KB
/
Copy pathplugin.php
File metadata and controls
259 lines (241 loc) · 7.54 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* Plugin Name: bbPress vBulletin Permalinks
* Plugin URI: https://wordpress.org/
* Description: vBulletin Like Permalinks.
* Author: Coffeeblack
* Author URI: http://www.coffeeblack.co.uk/
* Version: 1.0.1
* Text Domain: bbpress-vbulletin-permalinks
* Domain Path: /languages/
* Requires at least: 4.1.1
* Tested up to: 4.5.0
* License: GPLv2 or later
*/
/*
* Add plugin actions and filters at bbp_init action which triggered only if bbPress activated.
*
* @since 1.0.0
*/
function bbp_permalinks_init() {
$structure = get_option( 'permalink_structure' );
if( $structure ) {
// Run (add rewrite rules) only if WordPress permalink settings not default (default looks like site.com/?p=123)
add_action( 'flush_rewrite_rules', 'bbp_permalinks_rewrites_init', 0 );
add_action( 'bbp_add_rewrite_rules', 'bbp_permalinks_rewrites_init', 3 );
// Create valid URL for our new rewrite rules
add_filter( 'post_type_link', 'bbp_permalinks_post_type_link_pretty', 99, 2 );
}
else {
// If permalink settings is default only change permalinks
add_filter( 'post_type_link', 'bbp_permalinks_post_type_link_not_pretty', 99, 2 );
}
}
add_action( 'bbp_init', 'bbp_permalinks_init' );
/*
* Generate pretty permalinks for forums and topics.
*
* @since 1.0.0
* @param string $link URL.
* @param object $post An WordPress post object.
*/
function bbp_permalinks_post_type_link_pretty( $link, $post = 0 ) {
if( $post->post_type == bbp_get_forum_post_type() ) {
$parent = bbp_get_forum_parent_id($post->ID);
if($parent == 0) {
return home_url(
user_trailingslashit( 'forums/' .$post->post_name .'-f'.$post->ID)
);
} else {
$parents = bbp_get_forum_ancestors($post->ID);
asort($parents);
$url = 'forums/';
foreach($parents as $parent) {
$add = bbp_get_forum($parent);
$url .= $add->post_name.'/';
}
$url .= $post->post_name.'-f'.$post->ID;
return home_url(
user_trailingslashit($url)
);
}
}
elseif( $post->post_type == bbp_get_topic_post_type() ) {
// site.com/forums/topic/ID/
$parent = bbp_get_forum_parent_id($post->ID);
$parent = bbp_get_forum($parent);
$parentofparent = bbp_get_forum_parent_id($parent->ID);
if($parentofparent == 0) {
return home_url(
user_trailingslashit( 'forums/'.$parent->post_name.'/' .$post->ID .'-'.$post->post_name)
);
} else {
$parents = bbp_get_forum_ancestors($parent->ID);
asort($parents);
$url = 'forums/';
foreach($parents as $parent1) {
$add = bbp_get_forum($parent1);
$url .= $add->post_name.'/';
}
$url .= $parent->post_name.'/';
$url .= $post->ID .'-'.$post->post_name;
return home_url(
user_trailingslashit($url)
);
}
}
elseif( $post->post_type == bbp_get_reply_post_type() ) {
// site.com/forums/topic/ID/
$topic = bbp_get_reply_topic_id();
$topic = bbp_get_topic($topic);
//var_dump($topic);
$forum = bbp_get_forum_id($topic);
$forum = bbp_get_forum($forum);
//var_dump($forumcrumbs);
$parents = bbp_get_forum_ancestors($forum);
//var_dump($parents);
asort($parents);
$url = 'forums/';
foreach($parents as $parent1) {
$add = bbp_get_forum($parent1);
$url .= $add->post_name.'/';
}
$url .= $forum->post_name.'/';
$url .= $topic->post_name.'/';
$url .= $post->ID;
return home_url(
user_trailingslashit($url)
);
}
return $link;
}
/*
* Generate default permalinks for forums and topics.
*
* @since 1.0.0
* @param string $link URL.
* @param object $post An WordPress post object.
*/
function bbp_permalinks_post_type_link_not_pretty( $link, $post = 0 ) {
if( $post->post_type == bbp_get_forum_post_type() ) {
// site.com/?post_type=forum&p=ID
return home_url( '?post_type=' . bbp_get_forum_post_type() . '&p=' . $post->post_name );
}
elseif( $post->post_type == bbp_get_topic_post_type() ) {
// site.com/?post_type=topic&p=ID
return home_url( '?post_type=' . bbp_get_topic_post_type() . '&p=' . $post->post_name );
}
elseif( $post->post_type == bbp_get_reply_post_type() ) {
// site.com/?post_type=topic&p=ID
return home_url( '?post_type=' . bbp_get_reply_post_type() . '&p=' . $post->post_name );
}
return $link;
}
/*
* Generate rewrite rules for forums and topics based on bbPress settings.
*
* @since 1.0.0
*/
function bbp_permalinks_rewrites_init() {
$priority = 'top';
$edit_slug = 'edit';
$cat_regex = '/[0-99](?=\/)/';
$ids_regex = '/([0-9]+)/';
$forum_slug = bbp_get_forum_slug(); // string 'forum'
$topic_slug = bbp_get_topic_slug(); // string 'topic'
$reply_slug = bbp_get_reply_slug(); // string 'slug'
$paged_slug = bbp_get_paged_slug(); // string 'page'
$paged_rule = '/([^/]+)/' . $paged_slug . '/?([0-9]{1,})/?$';
$paged_rule_ids = $ids_regex . $paged_slug . '/?([0-9]{1,})/?$';
$view_id = bbp_get_view_rewrite_id();
$paged_id = bbp_get_paged_rewrite_id();
$edit_rule = $ids_regex . $edit_slug . '/?$'; // for edit links
$edit_id = bbp_get_edit_rewrite_id(); // for edit links
/* From bbpress/bbpress.php (816 line)
* Edit Forum|Topic|Reply|Topic-tag
* forums/forum/ID/edit/
*/
add_rewrite_rule(
'forums\/[\s\S]+\-f([0-9]+)\/edit/?$',
'index.php?post_type=' . bbp_get_forum_post_type() . '&p=$matches[1]&' . $edit_id . '=1',
$priority
);
// forums/topic/ID/edit/
add_rewrite_rule(
'forums\/[\s\S]+\/([0-9]+)\-[\s\S]+\/edit/?$',
'index.php?post_type=' . bbp_get_topic_post_type() . '&p=$matches[1]&' . $edit_id . '=1',
$priority
);
// forums/reply/ID/edit/
add_rewrite_rule(
'forums\/[\s\S]+\/([0-9]+)\/edit/?$',
'index.php?post_type=' . bbp_get_reply_post_type() . '&p=$matches[1]&' . $edit_id . '=1',
$priority
);
/* Forums
* /forums/forum/ID/page/2
*/
add_rewrite_rule(
'forums\/[\s\S]+\-f([0-9]+)\/page\/([0-9]+)/?$',
'index.php?post_type=' . bbp_get_forum_post_type() . '&p=$matches[1]&' . $paged_id .'=$matches[2]',
$priority
);
// /forums/forum/ID/
add_rewrite_rule(
'forums\/[\s\S]+\-f([0-9]+)/?$',
'index.php?post_type=' . bbp_get_forum_post_type() . '&p=$matches[1]',
$priority
);
/* Topics
* /forums/topic/ID/page/2/
*/
add_rewrite_rule(
'forums\/[\s\S]+\/([0-9]+)\-[\s\S]+\/page\/([0-9]+)/?$',
'index.php?post_type=' . bbp_get_topic_post_type() . '&p=$matches[1]&' . $paged_id . '=$matches[2]',
$priority
);
// /forums/topic/ID/
add_rewrite_rule(
'forums\/[\s\S]+\/([0-9]+)\-[\s\S]+/?$',
'index.php?post_type=' . bbp_get_topic_post_type() .'&p=$matches[1]',
$priority
);
}
/*
* Activation callback. Check if bbPress activated. Check permalink structure settings in WordPress.
* If both of conditions comes to true then add new rewrite rules and flush it.
*
* @since 1.0.0
*/
function bbp_permalinks_activate() {
/*
* We need add new rewrite rules first and only after this call flush_rewrite_rules
* In other ways flush_rewrite_rules doesn't work.
*/
if( function_exists( 'bbpress' ) ) {
/*
* Check if bbPress plugin activated
* bbp_permalinks_rewrites_init use bbPress links and if bbPress not activated we call undefined functions
* and got a fatal error.
*/
$structure = get_option( 'permalink_structure' );
if( $structure ) {
// Run (add rewrite rules) only if WordPress permalink settings not default (site.com/?p=123)
bbp_permalinks_rewrites_init();
flush_rewrite_rules( false );
}
}
}
// This stuff not working (Currently in progress)
//register_activation_hook( __FILE__, 'bbp_permalinks_activate' );
/*
* Deactivation callback. Flush rewrite rules.
*
* @since 1.0.0
*/
function bbp_permalinks_deactivate() {
flush_rewrite_rules( false );
}
// This stuff not working (Currently in progress)
// register_deactivation_hook( __FILE__, 'bbp_permalinks_deactivate' );
?>