Custom Permalinks - Version 1.1

Version Description

  • Enhancements

    • Added PostTypes Permalinks Page
      • View all the PostTypes permalinks
      • Search Permalinks
      • Sort by Title
      • Pagination
    • Added Categories Permalinks Page
      • View all the Category/Tags permalinks
      • Search Permalinks
      • Pagination
    • Bug Fixes
      • 404 Issues
      • Child pages bug
Download this release

Release Info

Developer sasiddiqui
Plugin Icon Custom Permalinks
Version 1.1
Comparing to
See all releases

Code changes from version 1.0.2 to 1.1

admin/class-custom-permalinks-admin.php ADDED
@@ -0,0 +1,395 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @package CustomPermalinks\Admin
5
+ */
6
+
7
+ class Custom_Permalinks_Admin {
8
+
9
+ /**
10
+ * Initializes WordPress hooks
11
+ */
12
+ function __construct() {
13
+ add_action( 'admin_menu', array($this, 'custom_permalinks_menu') );
14
+ }
15
+
16
+ /**
17
+ * Added Pages in Menu for Settings
18
+ */
19
+ public function custom_permalinks_menu() {
20
+ add_menu_page( 'PostTypes Permalinks', 'Custom Permalinks', 'administrator', 'custom-permalinks-post-permalinks', array($this,'custom_permalinks_post_permalinks'), 'dashicons-admin-links' );
21
+ add_submenu_page( 'custom-permalinks-post-permalinks', 'PostTypes Permalinks', 'PostTypes Permalinks', 'administrator', 'custom-permalinks-post-permalinks', array($this, 'custom_permalinks_post_permalinks') );
22
+ add_submenu_page( 'custom-permalinks-post-permalinks', 'Category Permalinks', 'Category Permalinks', 'administrator', 'custom-permalinks-category-permalinks', array($this, 'custom_permalinks_category_permalinks') );
23
+ }
24
+
25
+ /**
26
+ * Shows all the Permalinks created by using this Plugin with Pager and Search Functionality of Posts/Pages
27
+ */
28
+ public function custom_permalinks_post_permalinks() {
29
+ global $wpdb;
30
+ $filter_options = '';
31
+ $search_permalink = '';
32
+ $html = '';
33
+
34
+ // Handle Bulk Operations
35
+ if ( (isset($_POST['action']) && $_POST['action'] == 'delete') || (isset($_POST['action2']) && $_POST['action2'] == 'delete') && isset($_POST['permalink']) && !empty($_POST['permalink']) ) {
36
+ $post_ids = implode(', ', $_POST['permalink']);
37
+ $wpdb->query("DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids) AND meta_key = 'custom_permalink'");
38
+ }
39
+ $html .= '<div class="wrap">
40
+ <h1 class="wp-heading-inline">'.__('PostTypes Permalinks', 'custom-permalinks').'</h1>';
41
+
42
+ $search_value = '';
43
+ if (isset($_GET['s']) && !empty($_GET['s'])) {
44
+ $filter_permalink = 'AND pm.meta_value LIKE "%'.$_GET['s'].'%"';
45
+ $search_permalink = '&s='.$_GET['s'].'';
46
+ $html .= '<span class="subtitle">Search results for "'.$_GET['s'].'"</span>';
47
+ $search_value = htmlspecialchars($_GET['s']);
48
+ }
49
+ $page_limit = 'LIMIT 0, 20';
50
+ if (isset($_GET['paged']) && is_numeric($_GET['paged']) && $_GET['paged'] > 1) {
51
+ $pager = 20 * ($_GET['paged'] - 1);
52
+ $page_limit = 'LIMIT '.$pager.', 20';
53
+ }
54
+ $sorting_by = 'ORDER By p.ID DESC';
55
+ $order_by = 'asc';
56
+ $order_by_class = 'desc';
57
+ if (isset($_GET['orderby']) && $_GET['orderby'] == 'title') {
58
+ $filter_options .= '<input type="hidden" name="orderby" value="title" />';
59
+ if (isset($_GET['order']) && $_GET['order'] == 'desc') {
60
+ $sorting_by = 'ORDER By p.post_title DESC';
61
+ $order_by = 'asc';
62
+ $order_by_class = 'desc';
63
+ $filter_options .= '<input type="hidden" name="order" value="desc" />';
64
+ } else {
65
+ $sorting_by = 'ORDER By p.post_title';
66
+ $order_by = 'desc';
67
+ $order_by_class = 'asc';
68
+ $filter_options .= '<input type="hidden" name="order" value="asc" />';
69
+ }
70
+ }
71
+ $count_query = "SELECT COUNT(p.ID) AS total_permalinks FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE pm.meta_key = 'custom_permalink' AND pm.meta_value != '' ". $filter_permalink ."";
72
+ $count_posts = $wpdb->get_row($count_query);
73
+
74
+ $html .= '<form action="'.$_SERVER["REQUEST_URI"].'" method="get">';
75
+ $html .= '<p class="search-box">';
76
+ $html .= '<input type="hidden" name="page" value="custom-permalinks-post-permalinks" />';
77
+ $html .= $filter_options;
78
+ $html .= '<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>';
79
+ $html .= '<input type="search" id="custom-permalink-search-input" name="s" value="'. $search_value .'">';
80
+ $html .= '<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>';
81
+ $html .= '</form>';
82
+ $html .= '<form action="'.$_SERVER["REQUEST_URI"].'" method="post">';
83
+ $html .= '<div class="tablenav top">';
84
+ $html .= '<div class="alignleft actions bulkactions">
85
+ <label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
86
+ <select name="action" id="bulk-action-selector-top">
87
+ <option value="-1">'. __("Bulk Actions", "custom-permalinks") .'</option>
88
+ <option value="delete">'. __("Delete Permalinks", "custom-permalinks") .'</option>
89
+ </select>
90
+ <input type="submit" id="doaction" class="button action" value="Apply">
91
+ </div>';
92
+
93
+ $posts = 0;
94
+ if (isset($count_posts->total_permalinks) && $count_posts->total_permalinks > 0) {
95
+
96
+ $html .= '<h2 class="screen-reader-text">Custom Permalink navigation</h2>';
97
+
98
+ $query = "SELECT p.ID, p.post_title, p.post_type, pm.meta_value FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE pm.meta_key = 'custom_permalink' AND pm.meta_value != '' ". $filter_permalink ." ". $sorting_by ." ". $page_limit ."";
99
+ $posts = $wpdb->get_results($query);
100
+
101
+ $pagination_html = '';
102
+ $total_pages = ceil($count_posts->total_permalinks / 20);
103
+ if (isset($_GET['paged']) && is_numeric($_GET['paged']) && $_GET['paged'] > 0) {
104
+ $pagination_html = $this->custom_permalinks_pager($count_posts->total_permalinks, $_GET['paged'], $total_pages);
105
+ if ( $_GET['paged'] > $total_pages ) {
106
+ $redirect_uri = explode('&paged='. $_GET['paged'] .'', $_SERVER['REQUEST_URI']);
107
+ header('Location: ' . $redirect_uri[0], 301);
108
+ exit();
109
+ }
110
+ } else if (!isset($_GET['paged'])) {
111
+ $pagination_html = $this->custom_permalinks_pager($count_posts->total_permalinks, 1, $total_pages);
112
+ }
113
+
114
+ $html .= $pagination_html;
115
+ }
116
+ $table_navigation = $this->custom_permalink_tablenav_posts($order_by_class, $order_by, $search_permalink);
117
+
118
+ $html .= '</div>';
119
+ $html .= '<table class="wp-list-table widefat fixed striped posts">
120
+ <thead>'. $table_navigation .'</thead>
121
+ <tbody>';
122
+ if ($posts != 0 && !empty($posts)) {
123
+ foreach ( $posts as $post ) {
124
+ $html .= '<tr valign="top">';
125
+ $html .= '<th scope="row" class="check-column"><input type="checkbox" name="permalink[]" value="'.$post->ID.'" /></th>';
126
+ $html .= '<td><strong><a class="row-title" href="post.php?action=edit&post='. $post->ID .'">'. $post->post_title .'</a></strong></td>';
127
+ $html .= '<td>'. ucwords( $post->post_type ).'</td>';
128
+ $html .= '<td><a href="/'.$post->meta_value.'" target="_blank" title="'.__("Visit ".$post->post_title, "custom-permalinks").'">/'. urldecode($post->meta_value) .'</a></td></tr>';
129
+ }
130
+ } else {
131
+ $html .= '<tr class="no-items"><td class="colspanchange" colspan="10">No permalinks found.</td></tr>';
132
+ }
133
+ $html .= '</tbody>
134
+ <tfoot>'. $table_navigation .'</tfoot>
135
+ </table>';
136
+
137
+ $html .= '<div class="tablenav bottom">
138
+ <div class="alignleft actions bulkactions">
139
+ <label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
140
+ <select name="action2" id="bulk-action-selector-bottom">
141
+ <option value="-1">'. __("Bulk Actions", "custom-permalinks") .'</option>
142
+ <option value="delete">'. __("Delete Permalinks", "custom-permalinks") .'</option>
143
+ </select>
144
+ <input type="submit" id="doaction2" class="button action" value="Apply">
145
+ </div>
146
+ '. $pagination_html .'
147
+ </div>';
148
+ $html .= '</form>
149
+ </div>';
150
+ echo $html;
151
+ }
152
+
153
+ /**
154
+ * Return the Navigation row HTML same as Default Posts page for PostTypes
155
+ */
156
+ private function custom_permalink_tablenav_posts($order_by_class, $order_by, $search_permalink) {
157
+ $nav = '<tr>
158
+ <td id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1">Select All</label><input id="cb-select-all-1" type="checkbox"></td>
159
+ <th scope="col" id="title" class="manage-column column-title column-primary sortable '. $order_by_class .'"><a href="/wp-admin/admin.php?page=custom-permalinks-post-permalinks&amp;orderby=title&amp;order='. $order_by . $search_permalink .'"><span>'.__("Title", "custom-permalinks").'</span><span class="sorting-indicator"></span></a></th>
160
+ <th scope="col">'.__("Type", "custom-permalinks").'</th>
161
+ <th scope="col">'.__("Permalink", "custom-permalinks").'</th>
162
+ </tr>';
163
+ return $nav;
164
+ }
165
+
166
+ /**
167
+ * Shows all the Permalinks created by using this Plugin with Pager and Search Functionality of Category/Tags
168
+ */
169
+ public function custom_permalinks_category_permalinks() {
170
+
171
+ $search_permalink = '';
172
+ $html = '';
173
+
174
+ // Handle Bulk Operations
175
+ if ( (isset($_POST['action']) && $_POST['action'] == 'delete') || (isset($_POST['action2']) && $_POST['action2'] == 'delete') && isset($_POST['permalink']) && !empty($_POST['permalink']) ) {
176
+ $remove_perm = $_POST['permalink'];
177
+ $data = get_option('custom_permalink_table');
178
+ if ( isset($data) && is_array($data) ) {
179
+ $i = 0;
180
+ foreach ( $data as $link => $info ) {
181
+ if ( in_array($info['id'], $remove_perm) ) {
182
+ unset($data[$link]);
183
+ unset($remove_perm[$i]);
184
+ if ( !is_array($remove_perm) || empty($remove_perm) )
185
+ break;
186
+ }
187
+ $i++;
188
+ }
189
+ }
190
+ update_option('custom_permalink_table', $data);
191
+ }
192
+ $html .= '<div class="wrap">
193
+ <h1 class="wp-heading-inline">'.__('Category/Tags Permalinks', 'custom-permalinks').'</h1>';
194
+
195
+ $search_value = '';
196
+ if (isset($_GET['s']) && !empty($_GET['s'])) {
197
+ $search_permalink = '&s='.$_GET['s'].'';
198
+ $html .= '<span class="subtitle">Search results for "'.$_GET['s'].'"</span>';
199
+ $search_value = htmlspecialchars($_GET['s']);
200
+ }
201
+ $pager_offset = '0';
202
+ $page_limit = 20;
203
+ if (isset($_GET['paged']) && is_numeric($_GET['paged']) && $_GET['paged'] > 1) {
204
+ $pager_offset = 20 * ($_GET['paged'] - 1);
205
+ $page_limit = $pager_offset + 20;
206
+ }
207
+ $html .= '<form action="'. $_SERVER["REQUEST_URI"] .'" method="get">';
208
+ $html .= '<p class="search-box">';
209
+ $html .= '<input type="hidden" name="page" value="custom-permalinks-category-permalinks" />';
210
+ $html .= '<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>';
211
+ $html .= '<input type="search" id="custom-permalink-search-input" name="s" value="'. $search_value .'">';
212
+ $html .= '<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>';
213
+ $html .= '</form>';
214
+ $html .= '<form action="'. $_SERVER["REQUEST_URI"] .'" method="post">';
215
+ $html .= '<div class="tablenav top">';
216
+ $html .= '<div class="alignleft actions bulkactions">
217
+ <label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
218
+ <select name="action" id="bulk-action-selector-top">
219
+ <option value="-1">'. __("Bulk Actions", "custom-permalinks") .'</option>
220
+ <option value="delete">'. __("Delete Permalinks", "custom-permalinks") .'</option>
221
+ </select>
222
+ <input type="submit" id="doaction" class="button action" value="Apply">
223
+ </div>';
224
+
225
+ $posts = 0;
226
+ $table = get_option('custom_permalink_table');
227
+ $count_tags = count($table);
228
+ if ( isset($table) && is_array($table) && $count_tags > 0) {
229
+
230
+ $filtered = array();
231
+ if ($search_value != '') {
232
+ foreach($table as $key => $value) {
233
+ if(preg_match('/'. $search_value .'/',$key)) {
234
+ $filtered[$key] = $value;
235
+ }
236
+ }
237
+ $table = $filtered;
238
+ $count_tags = count($table);
239
+ }
240
+
241
+ $html .= '<h2 class="screen-reader-text">Custom Permalink navigation</h2>';
242
+
243
+ $pagination_html = '';
244
+ $total_pages = ceil($count_tags / 20);
245
+ if (isset($_GET['paged']) && is_numeric($_GET['paged']) && $_GET['paged'] > 0) {
246
+ $pagination_html = $this->custom_permalinks_pager($count_tags, $_GET['paged'], $total_pages);
247
+ if ( $_GET['paged'] > $total_pages ) {
248
+ $redirect_uri = explode('&paged='. $_GET['paged'] .'', $_SERVER['REQUEST_URI']);
249
+ header('Location: ' . $redirect_uri[0], 301);
250
+ exit();
251
+ }
252
+ } else if (!isset($_GET['paged'])) {
253
+ $pagination_html = $this->custom_permalinks_pager($count_tags, 1, $total_pages);
254
+ }
255
+
256
+ $html .= $pagination_html;
257
+ }
258
+ $table_navigation = $this->custom_permalink_tablenav_category($search_permalink);
259
+
260
+ $html .= '</div>';
261
+ $html .= '<table class="wp-list-table widefat fixed striped posts">
262
+ <thead>'. $table_navigation .'</thead>
263
+ <tbody>';
264
+
265
+ if ( $table && is_array($table) && $count_tags > 0) {
266
+ uasort($table, array('Custom_Permalinks_Admin', 'custom_permalinks_sort_array'));
267
+ $i = -1;
268
+ foreach ( $table as $permalink => $info ) {
269
+ $i++;
270
+ if ($i < $pager_offset)
271
+ continue;
272
+
273
+ if ($i >= $page_limit)
274
+ break;
275
+
276
+ $type = $info['kind'] == 'tag' ? 'post_tag' : 'category';
277
+ $term = get_term($info['id'], $type);
278
+ $html .= '<tr valign="top">';
279
+ $html .= '<th scope="row" class="check-column"><input type="checkbox" name="permalink[]" value="'. $info['id'] .'" /></th>';
280
+ $html .= '<td><strong><a class="row-title" href="edit-tags.php?action=edit&taxonomy='. $type .'&tag_ID='. $info['id'] .' ">'. $term->name .'</a></strong></td>';
281
+ $html .= '<td>'. ucwords( $info['kind'] ) .'</td>';
282
+ $html .= '<td><a href="/'. $permalink .'" target="_blank" title="'.__("Visit ". $term->name, "custom-permalinks").'">/'. $permalink .'</a></td></tr>';
283
+ }
284
+ } else {
285
+ $html .= '<tr class="no-items"><td class="colspanchange" colspan="10">No permalinks found.</td></tr>';
286
+ }
287
+ $html .= '</tbody>
288
+ <tfoot>'. $table_navigation .'</tfoot>
289
+ </table>';
290
+
291
+ $html .= '<div class="tablenav bottom">
292
+ <div class="alignleft actions bulkactions">
293
+ <label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
294
+ <select name="action2" id="bulk-action-selector-bottom">
295
+ <option value="-1">'. __("Bulk Actions", "custom-permalinks") .'</option>
296
+ <option value="delete">'. __("Delete Permalinks", "custom-permalinks") .'</option>
297
+ </select>
298
+ <input type="submit" id="doaction2" class="button action" value="Apply">
299
+ </div>
300
+ '. $pagination_html .'
301
+ </div>';
302
+ $html .= '</form>
303
+ </div>';
304
+ echo $html;
305
+ }
306
+
307
+ /**
308
+ * Sort the terms array in desc order using term id
309
+ */
310
+ private static function custom_permalinks_sort_array($a, $b) {
311
+ return $b['id'] - $a['id'];
312
+ }
313
+
314
+ /**
315
+ * Return the Navigation row HTML same as Default Posts page for Category
316
+ */
317
+ private function custom_permalink_tablenav_category($search_permalink) {
318
+ $nav = '<tr>
319
+ <td id="cb" class="manage-column column-cb check-column"><label class="screen-reader-text" for="cb-select-all-1">Select All</label><input id="cb-select-all-1" type="checkbox"></td>
320
+ <th scope="col" id="title" class="manage-column column-title column-primary">'. __("Title", "custom-permalinks") .'</th>
321
+ <th scope="col">'.__("Type", "custom-permalinks").'</th>
322
+ <th scope="col">'.__("Permalink", "custom-permalinks").'</th>
323
+ </tr>';
324
+ return $nav;
325
+ }
326
+
327
+ /**
328
+ * Return the Pager HTML
329
+ */
330
+ private function custom_permalinks_pager($total_permalinks, $current_pager_value = 1, $total_pager = 0) {
331
+
332
+ if ($total_pager == 0) return;
333
+
334
+ if ($total_pager == 1) {
335
+ $pagination_html = '<div class="tablenav-pages one-page">
336
+ <span class="displaying-num">'.$total_permalinks.' items</span>
337
+ </div>';
338
+ return $pagination_html;
339
+ }
340
+
341
+ $remove_pager_uri = explode('&paged='. $current_pager_value .'', $_SERVER['REQUEST_URI']);
342
+ $pagination_html = '<div class="tablenav-pages">
343
+ <span class="displaying-num">'.$total_permalinks.' items</span>
344
+ <span class="pagination-links">';
345
+
346
+ if ( $current_pager_value == 1 ) {
347
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo; </span>
348
+ <span class="tablenav-pages-navspan" aria-hidden="true">&lsaquo; </span>';
349
+ } else {
350
+ $prev_page = $current_pager_value - 1;
351
+ if ($prev_page == 1) {
352
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo;</span>';
353
+ } else {
354
+ $pagination_html .= ' <a href="'. $remove_pager_uri[0] .'&paged=1" title="First page" class="first-page">
355
+ <span class="screen-reader-text">First page</span>
356
+ <span aria-hidden="true">&laquo;</span>
357
+ </a> ';
358
+ }
359
+ $pagination_html .= ' <a href="'. $remove_pager_uri[0] .'&paged='. $prev_page .'" title="Previous page" class="prev-page">
360
+ <span class="screen-reader-text">Previous page</span>
361
+ <span aria-hidden="true">&lsaquo;</span>
362
+ </a> ';
363
+ }
364
+
365
+ $pagination_html .= '<span class="paging-input">
366
+ <label for="current-page-selector" class="screen-reader-text">Current Page</label>
367
+ <input class="current-page" id="current-page-selector" type="text" name="paged" value="'. $current_pager_value .'" size="1" aria-describedby="table-paging" />
368
+ <span class="tablenav-paging-text"> of <span class="total-pages">'. $total_pager .' </span> </span>
369
+ </span>';
370
+
371
+ if ( $current_pager_value == $total_pager ) {
372
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&rsaquo; </span>
373
+ <span class="tablenav-pages-navspan" aria-hidden="true">&raquo; </span>';
374
+ } else {
375
+ $next_page = $current_pager_value + 1;
376
+ $pagination_html .= ' <a href="'. $remove_pager_uri[0] .'&paged='. $next_page .'" title="Next page" class="next-page">
377
+ <span class="screen-reader-text">Next page</span>
378
+ <span aria-hidden="true">&rsaquo;</span>
379
+ </a> ';
380
+ if ( $total_pager == $next_page) {
381
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&raquo;</span>';
382
+ } else {
383
+ $pagination_html .= ' <a href="'. $remove_pager_uri[0] .'&paged='. $total_pager .'" title="Last page" class="last-page">
384
+ <span class="screen-reader-text">Last page</span>
385
+ <span aria-hidden="true">&raquo;</span>
386
+ </a> ';
387
+ }
388
+
389
+ }
390
+ $pagination_html .= '</span></div>';
391
+
392
+ return $pagination_html;
393
+ }
394
+
395
+ }
admin/index.php ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Silence is golden
4
+ */
custom-permalinks-main.php ADDED
@@ -0,0 +1,770 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @package CustomPermalinks\Main
5
+ */
6
+
7
+ // Make sure we don't expose any info if called directly
8
+ if ( !defined( 'ABSPATH' ) ) {
9
+ echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
10
+ exit;
11
+ }
12
+
13
+ if ( !function_exists("add_action") || !function_exists("add_filter") ) {
14
+ header( 'Status: 403 Forbidden' );
15
+ header( 'HTTP/1.1 403 Forbidden' );
16
+ exit();
17
+ }
18
+
19
+ define('CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.1');
20
+
21
+ if ( !defined('CUSTOM_PERMALINKS_PATH') ) {
22
+ define('CUSTOM_PERMALINKS_PATH', plugin_dir_path( __FILE__ ));
23
+ }
24
+
25
+ if ( is_admin() ) {
26
+ require_once(CUSTOM_PERMALINKS_PATH.'admin/class-custom-permalinks-admin.php');
27
+ new Custom_Permalinks_Admin();
28
+ }
29
+
30
+ /**
31
+ * Filter to replace the post permalink with the custom one
32
+ */
33
+ function custom_permalinks_post_link($permalink, $post) {
34
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
35
+ if ( $custom_permalink ) {
36
+ $post_type = isset($post->post_type) ? $post->post_type : 'post';
37
+ $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post->ID, 'element_type' => $post_type ) );
38
+ if ( $language_code )
39
+ return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink, $language_code );
40
+ else
41
+ return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink );
42
+ }
43
+
44
+ return $permalink;
45
+ }
46
+
47
+ /**
48
+ * Filter to replace the page permalink with the custom one
49
+ */
50
+ function custom_permalinks_page_link($permalink, $page) {
51
+ $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
52
+ if ( $custom_permalink ) {
53
+ $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $page, 'element_type' => 'page' ) );
54
+ if ( $language_code )
55
+ return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink, $language_code );
56
+ else
57
+ return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink );
58
+ }
59
+
60
+ return $permalink;
61
+ }
62
+
63
+ /**
64
+ * Filter to replace the term permalink with the custom one
65
+ */
66
+ function custom_permalinks_term_link($permalink, $term) {
67
+ $table = get_option('custom_permalink_table');
68
+ if ( is_object($term) ) $term = $term->term_id;
69
+
70
+ $custom_permalink = custom_permalinks_permalink_for_term($term);
71
+ if ( $custom_permalink ) {
72
+ $taxonomy = get_term($term);
73
+ if ( isset($taxonomy) && isset($taxonomy->term_taxonomy_id) ) {
74
+ $term_type = isset($taxonomy->taxonomy) ? $taxonomy->taxonomy : 'category';
75
+ $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $taxonomy->term_taxonomy_id, 'element_type' => $term_type ) );
76
+ return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink, $language_code );
77
+ } else {
78
+ return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink );
79
+ }
80
+ }
81
+
82
+ return $permalink;
83
+ }
84
+
85
+ /**
86
+ * Action to redirect to the custom permalink
87
+ */
88
+ function custom_permalinks_redirect() {
89
+ // Get request URI, strip parameters
90
+ $url = parse_url(get_bloginfo('url'));
91
+ $url = isset($url['path']) ? $url['path'] : '';
92
+ $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
93
+ if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
94
+
95
+ $request = custom_permalinks_check_conflicts($request);
96
+
97
+ global $wp_query;
98
+
99
+ $custom_permalink = '';
100
+ $original_permalink = '';
101
+
102
+ // If the post/tag/category we're on has a custom permalink, get it and check against the request
103
+ if ( (is_single() || is_page()) && !empty($wp_query->post) ) {
104
+ $post = $wp_query->post;
105
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
106
+ $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
107
+ } else if ( is_tag() || is_category() ) {
108
+ $theTerm = $wp_query->get_queried_object();
109
+ $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
110
+ $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) : custom_permalinks_original_category_link($theTerm->term_id));
111
+ }
112
+
113
+ if ( $custom_permalink && (substr($request, 0, strlen($custom_permalink)) != $custom_permalink || $request == $custom_permalink."/" ) ) {
114
+ // Request doesn't match permalink - redirect
115
+ $url = $custom_permalink;
116
+
117
+ if ( substr($request, 0, strlen($original_permalink)) == $original_permalink && trim($request,'/') != trim($original_permalink,'/') ) {
118
+ // This is the original link; we can use this url to derive the new one
119
+ $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
120
+ $url = preg_replace('@([^?]*)&@', '\1?', $url);
121
+ }
122
+
123
+ // Append any query compenent
124
+ $url .= strstr($_SERVER['REQUEST_URI'], "?");
125
+
126
+ wp_redirect( home_url()."/".$url, 301 );
127
+ exit();
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Filter to rewrite the query if we have a matching post
133
+ */
134
+ function custom_permalinks_request($query) {
135
+ global $wpdb;
136
+ global $_CPRegisteredURL;
137
+
138
+ // First, search for a matching custom permalink, and if found, generate the corresponding
139
+ // original URL
140
+
141
+ $originalUrl = NULL;
142
+
143
+ // Get request URI, strip parameters and /'s
144
+ $url = parse_url(get_bloginfo('url'));
145
+ $url = isset($url['path']) ? $url['path'] : '';
146
+ $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
147
+ $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
148
+
149
+ if ( !$request ) return $query;
150
+
151
+ $request = custom_permalinks_check_conflicts($request);
152
+ $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
153
+
154
+ $sql = $wpdb->prepare("SELECT $wpdb->posts.ID, $wpdb->postmeta.meta_value, $wpdb->posts.post_type, $wpdb->posts.post_status FROM $wpdb->posts ".
155
+ "LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE ".
156
+ " meta_key = 'custom_permalink' AND ".
157
+ " meta_value != '' AND ".
158
+ " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR ".
159
+ " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) ".
160
+ " AND post_status != 'trash' AND post_type != 'nav_menu_item'".
161
+ " ORDER BY LENGTH(meta_value) DESC, ".
162
+ " FIELD(post_status,'publish','private','draft','auto-draft','inherit'),".
163
+ " FIELD(post_type,'post','page'),".
164
+ "$wpdb->posts.ID ASC LIMIT 1",
165
+ $request_noslash,
166
+ $request_noslash."/");
167
+
168
+ $posts = $wpdb->get_results($sql);
169
+
170
+ if ( $posts ) {
171
+ // A post matches our request
172
+
173
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
174
+ if ( $request_noslash == trim($posts[0]->meta_value,'/') )
175
+ $_CPRegisteredURL = $request;
176
+
177
+ if ( $posts[0]->post_status == 'draft' ) {
178
+ if( $posts[0]->post_type == 'page' ) {
179
+ $originalUrl = "?page_id=" . $posts[0]->ID;
180
+ } else {
181
+ $originalUrl = "?post_type=".$posts[0]->post_type."&p=" . $posts[0]->ID;
182
+ }
183
+ } else {
184
+ $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
185
+ ( $posts[0]->post_type == 'page' ?
186
+ custom_permalinks_original_page_link($posts[0]->ID)
187
+ : custom_permalinks_original_post_link($posts[0]->ID) ),
188
+ strtolower($request_noslash) ) );
189
+ }
190
+ }
191
+
192
+ if ( $originalUrl === NULL ) {
193
+ // See if any terms have a matching permalink
194
+ $table = get_option('custom_permalink_table');
195
+ if ( !$table ) return $query;
196
+
197
+ foreach ( array_keys($table) as $permalink ) {
198
+ if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
199
+ $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
200
+ $term = $table[$permalink];
201
+
202
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
203
+ if ( $request_noslash == trim($permalink,'/') )
204
+ $_CPRegisteredURL = $request;
205
+
206
+
207
+ if ( $term['kind'] == 'category') {
208
+ $originalUrl = str_replace(trim($permalink,'/'),
209
+ custom_permalinks_original_category_link($term['id']),
210
+ trim($request,'/'));
211
+ } else {
212
+ $originalUrl = str_replace(trim($permalink,'/'),
213
+ custom_permalinks_original_tag_link($term['id']),
214
+ trim($request,'/'));
215
+ }
216
+ }
217
+ }
218
+ }
219
+
220
+ if ( $originalUrl !== NULL ) {
221
+ $originalUrl = str_replace('//', '/', $originalUrl);
222
+
223
+ if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
224
+ $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
225
+ $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
226
+ }
227
+
228
+ // Now we have the original URL, run this back through WP->parse_request, in order to
229
+ // parse parameters properly. We set $_SERVER variables to fool the function.
230
+ $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
231
+ $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
232
+ $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
233
+ parse_str($_SERVER['QUERY_STRING'], $queryArray);
234
+ $oldValues = array();
235
+ if ( is_array($queryArray) )
236
+ foreach ( $queryArray as $key => $value ) {
237
+ $oldValues[$key] = $_REQUEST[$key];
238
+ $_REQUEST[$key] = $_GET[$key] = $value;
239
+ }
240
+
241
+ // Re-run the filter, now with original environment in place
242
+ remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
243
+ global $wp;
244
+ $wp->parse_request();
245
+ $query = $wp->query_vars;
246
+ add_filter( 'request', 'custom_permalinks_request', 10, 1 );
247
+
248
+ // Restore values
249
+ $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
250
+ foreach ( $oldValues as $key => $value ) {
251
+ $_REQUEST[$key] = $value;
252
+ }
253
+ }
254
+
255
+ return $query;
256
+ }
257
+
258
+ /**
259
+ * Filter to handle trailing slashes correctly
260
+ */
261
+ function custom_permalinks_trailingslash($string, $type) {
262
+ global $_CPRegisteredURL;
263
+
264
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
265
+ $url = parse_url(get_bloginfo('url'));
266
+ $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
267
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
268
+
269
+ if ( !trim($request) ) return $string;
270
+
271
+ if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
272
+ if( isset($url['path']) ) {
273
+ return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
274
+ } else {
275
+ return ($string{0} == '/' ? '/' : '') . $_CPRegisteredURL;
276
+ }
277
+ }
278
+ return $string;
279
+ }
280
+
281
+ /**
282
+ * Per-post/page options (Wordpress > 2.9)
283
+ */
284
+ function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
285
+ $permalink = get_post_meta( $id, 'custom_permalink', true );
286
+ $post = get_post($id);
287
+
288
+ ob_start();
289
+ ?>
290
+ <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
291
+ <?php
292
+ $content = ob_get_contents();
293
+ ob_end_clean();
294
+
295
+ if ( 'publish' == $post->post_status ) {
296
+ $view_post = 'page' == $post->post_type ? __('View Page', 'custom-permalinks') : __('View Post', 'custom-permalinks');
297
+ }
298
+ if ( $post->post_type == 'attachment' || $post->ID == get_option('page_on_front') ) {
299
+ return $html;
300
+ }
301
+
302
+ if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
303
+ $permalink = $matches[1];
304
+ } else {
305
+ list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
306
+ if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
307
+ $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
308
+ }
309
+ }
310
+
311
+ return '<strong>' . __('Permalink:', 'custom-permalinks') . "</strong>\n" . $content .
312
+ ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
313
+ }
314
+
315
+
316
+ /**
317
+ * Per-post options (Wordpress < 2.9)
318
+ */
319
+ function custom_permalinks_post_options() {
320
+ global $post;
321
+ $post_id = $post;
322
+ if (is_object($post_id)) {
323
+ $post_id = $post_id->ID;
324
+ }
325
+
326
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
327
+
328
+ ?>
329
+ <div class="postbox closed">
330
+ <h3><?php _e('Custom Permalink', 'custom-permalinks') ?></h3>
331
+ <div class="inside">
332
+ <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
333
+ </div>
334
+ </div>
335
+ <?php
336
+ }
337
+
338
+ /**
339
+ * Per-page options (Wordpress < 2.9)
340
+ */
341
+ function custom_permalinks_page_options() {
342
+ global $post;
343
+ $post_id = $post;
344
+ if (is_object($post_id)) {
345
+ $post_id = $post_id->ID;
346
+ }
347
+
348
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
349
+
350
+ ?>
351
+ <div class="postbox closed">
352
+ <h3><?php _e('Custom Permalink', 'custom-permalinks') ?></h3>
353
+ <div class="inside">
354
+ <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
355
+ </div>
356
+ </div>
357
+ <?php
358
+ }
359
+
360
+ /**
361
+ * Per-category/tag options
362
+ */
363
+ function custom_permalinks_term_options($object) {
364
+ if ( is_object($object) && isset($object->term_id) ) {
365
+ $permalink = custom_permalinks_permalink_for_term($object->term_id);
366
+
367
+ if ( $object->term_id ) {
368
+ $originalPermalink = ($object->taxonomy == 'post_tag' ?
369
+ custom_permalinks_original_tag_link($object->term_id) :
370
+ custom_permalinks_original_category_link($object->term_id) );
371
+ }
372
+
373
+ custom_permalinks_form($permalink, $originalPermalink);
374
+ } else {
375
+ custom_permalinks_form('');
376
+ }
377
+
378
+ // Move the save button to above this form
379
+ wp_enqueue_script('jquery');
380
+ ?>
381
+ <script type="text/javascript">
382
+ jQuery(document).ready(function() {
383
+ var button = jQuery('#custom_permalink_form').parent().find('.submit');
384
+ button.remove().insertAfter(jQuery('#custom_permalink_form'));
385
+ });
386
+ </script>
387
+ <?php
388
+ }
389
+
390
+ /**
391
+ * Helper function to render form
392
+ */
393
+ function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
394
+ ?>
395
+ <input value="true" type="hidden" name="custom_permalinks_edit" />
396
+ <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
397
+
398
+ <?php if ( $renderContainers ) : ?>
399
+ <table class="form-table" id="custom_permalink_form">
400
+ <tr>
401
+ <th scope="row"><?php _e('Custom Permalink', 'custom-permalinks') ?></th>
402
+ <td>
403
+ <?php endif; ?>
404
+
405
+ <?php
406
+ if ($permalink == '') {
407
+ $original = custom_permalinks_check_conflicts($original);
408
+ }
409
+ ?>
410
+
411
+ <?php echo home_url() ?>/
412
+ <span id="editable-post-name" title="Click to edit this part of the permalink">
413
+ <input type="text" id="new-post-slug" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
414
+ style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
415
+ onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
416
+ onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo htmlspecialchars(urldecode($original)) ?>' ) { this.value = '<?php echo htmlspecialchars(urldecode($original)) ?>'; this.style.color = '#ddd'; }"/>
417
+ </span>
418
+ <?php if ( $renderContainers ) : ?>
419
+ <br />
420
+ <small><?php _e('Leave blank to disable', 'custom-permalinks') ?></small>
421
+
422
+ </td>
423
+ </tr>
424
+ </table>
425
+ <?php
426
+ endif;
427
+ }
428
+
429
+ /**
430
+ * Save per-post options
431
+ */
432
+ function custom_permalinks_save_post($id) {
433
+ if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
434
+
435
+ delete_post_meta( $id, 'custom_permalink' );
436
+
437
+ $original_link = custom_permalinks_original_post_link($id);
438
+ $permalink_structure = get_option('permalink_structure');
439
+
440
+ if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
441
+ add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"))) );
442
+ }
443
+ }
444
+
445
+ /**
446
+ * Save per-tag options
447
+ */
448
+ function custom_permalinks_save_tag($id) {
449
+ if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
450
+ $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
451
+
452
+ if ( $newPermalink == custom_permalinks_original_tag_link($id) )
453
+ $newPermalink = '';
454
+
455
+ $term = get_term($id, 'post_tag');
456
+ custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
457
+ }
458
+
459
+ /**
460
+ * Save per-category options
461
+ */
462
+ function custom_permalinks_save_category($id) {
463
+ if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
464
+ $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
465
+
466
+ if ( $newPermalink == custom_permalinks_original_category_link($id) )
467
+ $newPermalink = '';
468
+
469
+ $term = get_term($id, 'category');
470
+ custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
471
+ }
472
+
473
+ /**
474
+ * Save term (common to tags and categories)
475
+ */
476
+ function custom_permalinks_save_term($term, $permalink) {
477
+
478
+ custom_permalinks_delete_term($term->term_id);
479
+ $table = get_option('custom_permalink_table');
480
+ if ( $permalink )
481
+ $table[$permalink] = array(
482
+ 'id' => $term->term_id,
483
+ 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
484
+ 'slug' => $term->slug);
485
+
486
+ update_option('custom_permalink_table', $table);
487
+ }
488
+
489
+ /**
490
+ * Delete post
491
+ */
492
+ function custom_permalinks_delete_permalink( $id ){
493
+ global $wpdb;
494
+ // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
495
+ $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = %d", $id));
496
+ }
497
+
498
+ /**
499
+ * Delete term
500
+ */
501
+ function custom_permalinks_delete_term($id) {
502
+
503
+ $table = get_option('custom_permalink_table');
504
+ if ( $table )
505
+ foreach ( $table as $link => $info ) {
506
+ if ( $info['id'] == $id ) {
507
+ unset($table[$link]);
508
+ break;
509
+ }
510
+ }
511
+
512
+ update_option('custom_permalink_table', $table);
513
+ }
514
+
515
+ /**
516
+ * Options page
517
+ */
518
+ function custom_permalinks_options_page() {
519
+
520
+ // Handle revert
521
+ if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
522
+ check_admin_referer('custom-permalinks-bulk');
523
+ foreach ( (array)$_REQUEST['revert'] as $identifier ) {
524
+ list($kind, $id) = explode('.', $identifier);
525
+ switch ( $kind ) {
526
+ case 'post':
527
+ case 'page':
528
+ delete_post_meta( $id, 'custom_permalink' );
529
+ break;
530
+ case 'tag':
531
+ case 'category':
532
+ custom_permalinks_delete_term($id);
533
+ break;
534
+ }
535
+ }
536
+
537
+ // Redirect
538
+ $redirectUrl = $_SERVER['REQUEST_URI'];
539
+ ?>
540
+ <script type="text/javascript">
541
+ document.location = '<?php echo $redirectUrl ?>'
542
+ </script>
543
+ <?php ;
544
+ }
545
+
546
+ ?>
547
+ <div class="wrap">
548
+ <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
549
+
550
+ <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
551
+ <?php wp_nonce_field('custom-permalinks-bulk') ?>
552
+
553
+ <div class="tablenav">
554
+ <div class="alignleft">
555
+ <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
556
+ </div>
557
+ <br class="clear" />
558
+ </div>
559
+ <br class="clear" />
560
+ <table class="widefat">
561
+ <thead>
562
+ <tr>
563
+ <th scope="col" class="check-column"><input type="checkbox" /></th>
564
+ <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
565
+ <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
566
+ <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
567
+ </tr>
568
+ </thead>
569
+ <tbody>
570
+ <?php
571
+ $rows = custom_permalinks_admin_rows();
572
+ foreach ( $rows as $row ) {
573
+ ?>
574
+ <tr valign="top">
575
+ <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
576
+ <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
577
+ <td><?php echo htmlspecialchars($row['type']) ?></td>
578
+ <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="<?php printf(__('Visit %s', 'custom-permalinks'), htmlspecialchars($row['title'])) ?>">
579
+ <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
580
+ </a>
581
+ </td>
582
+ </tr>
583
+ <?php
584
+ }
585
+ ?>
586
+ </tbody>
587
+ </table>
588
+ </form>
589
+ </div>
590
+ <?php
591
+ }
592
+
593
+ /**
594
+ * Get rows for management view
595
+ */
596
+ function custom_permalinks_admin_rows() {
597
+ $rows = array();
598
+
599
+ // List tags/categories
600
+ $table = get_option('custom_permalink_table');
601
+ if ( $table && is_array($table) ) {
602
+ foreach ( $table as $permalink => $info ) {
603
+ $row = array();
604
+ $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
605
+ $row['id'] = $info['kind'].'.'.$info['id'];
606
+ $row['permalink'] = home_url()."/".$permalink;
607
+ $row['type'] = ucwords($info['kind']);
608
+ $row['title'] = $term->name;
609
+ $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$info['id'] : 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$info['id'] );
610
+ $rows[] = $row;
611
+ }
612
+ }
613
+
614
+ // List posts/pages
615
+ global $wpdb;
616
+ $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
617
+ $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
618
+ $posts = $wpdb->get_results($query);
619
+ foreach ( $posts as $post ) {
620
+ $row = array();
621
+ $row['id'] = 'post.'.$post->ID;
622
+ $row['permalink'] = get_permalink($post->ID);
623
+ $row['type'] = ucwords( $post->post_type );
624
+ $row['title'] = $post->post_title;
625
+ $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
626
+ $rows[] = $row;
627
+ }
628
+
629
+ return $rows;
630
+ }
631
+
632
+ /**
633
+ * Get original permalink for post
634
+ */
635
+ function custom_permalinks_original_post_link($post_id) {
636
+ remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 3 ); // original hook
637
+ remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
638
+
639
+ require_once ABSPATH . '/wp-admin/includes/post.php';
640
+ list( $permalink, $post_name ) = get_sample_permalink( $post_id );
641
+ $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
642
+ $permalink = ltrim(str_replace(home_url(), '', $permalink), '/');
643
+
644
+ add_filter( 'post_link', 'custom_permalinks_post_link', 10, 3 ); // original hook
645
+ add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
646
+
647
+ return $permalink;
648
+ }
649
+
650
+ /**
651
+ * Get original permalink for page
652
+ */
653
+ function custom_permalinks_original_page_link($post_id) {
654
+ remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
655
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
656
+
657
+ require_once ABSPATH . '/wp-admin/includes/post.php';
658
+ list( $permalink, $post_name ) = get_sample_permalink( $post_id );
659
+ $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
660
+ $permalink = ltrim(str_replace(home_url(), '', $permalink), '/');
661
+
662
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
663
+ add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
664
+ return $permalink;
665
+ }
666
+
667
+ /**
668
+ * Get original permalink for tag
669
+ */
670
+ function custom_permalinks_original_tag_link($tag_id) {
671
+ remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
672
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
673
+ $originalPermalink = ltrim(str_replace(home_url(), '', get_tag_link($tag_id)), '/');
674
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
675
+ add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
676
+ return $originalPermalink;
677
+ }
678
+
679
+ /**
680
+ * Get original permalink for category
681
+ */
682
+ function custom_permalinks_original_category_link($category_id) {
683
+ remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
684
+ remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
685
+ $originalPermalink = ltrim(str_replace(home_url(), '', get_category_link($category_id)), '/');
686
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
687
+ add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
688
+ return $originalPermalink;
689
+ }
690
+
691
+ /**
692
+ * Get permalink for term
693
+ */
694
+ function custom_permalinks_permalink_for_term($id) {
695
+ $table = get_option('custom_permalink_table');
696
+ if ( $table )
697
+ foreach ( $table as $link => $info ) {
698
+ if ( $info['id'] == $id ) {
699
+ return $link;
700
+ }
701
+ }
702
+ return false;
703
+ }
704
+
705
+ /**
706
+ * Set up administration menu
707
+ */
708
+ function custom_permalinks_setup_admin_menu() {
709
+ add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'edit_others_pages', 'custom_permalinks', 'custom_permalinks_options_page' );
710
+ }
711
+
712
+ /**
713
+ * Check Conflicts and resolve it (e.g: Polylang)
714
+ */
715
+ function custom_permalinks_check_conflicts($requested_url = '') {
716
+
717
+ if ($requested_url == '') return;
718
+
719
+ // Check if the Polylang Plugin is installed so, make changes in the URL
720
+ if (defined( 'POLYLANG_VERSION' )) {
721
+ $polylang_config = get_option('polylang');
722
+ if ($polylang_config['force_lang'] == 1) {
723
+
724
+ if(strpos($requested_url, 'language/') !== false)
725
+ $requested_url = str_replace("language/", "", $requested_url);
726
+
727
+ $remove_lang = ltrim(strstr($requested_url, '/'), '/');
728
+ if ($remove_lang != '')
729
+ return $remove_lang;
730
+ }
731
+ }
732
+
733
+ return $requested_url;
734
+ }
735
+
736
+ if (function_exists("add_action") && function_exists("add_filter")) {
737
+ add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
738
+ add_filter( 'post_link', 'custom_permalinks_post_link', 10, 3 );
739
+ add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
740
+ add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
741
+ add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
742
+ add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
743
+ add_filter( 'request', 'custom_permalinks_request', 10, 1 );
744
+ add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
745
+
746
+ if (function_exists("get_bloginfo")) {
747
+ $v = explode('.', get_bloginfo('version'));
748
+ }
749
+
750
+ if ( $v[0] >= 2 ) {
751
+ add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
752
+ } else {
753
+ add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
754
+ add_action( 'edit_page_form', 'custom_permalinks_page_options' );
755
+ }
756
+
757
+ add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
758
+ add_action( 'add_tag_form', 'custom_permalinks_term_options' );
759
+ add_action( 'edit_category_form', 'custom_permalinks_term_options' );
760
+ add_action( 'save_post', 'custom_permalinks_save_post' );
761
+ add_action( 'save_page', 'custom_permalinks_save_post' );
762
+ add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
763
+ add_action( 'edited_category', 'custom_permalinks_save_category' );
764
+ add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
765
+ add_action( 'create_category', 'custom_permalinks_save_category' );
766
+ add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10);
767
+ add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
768
+ add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
769
+ add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
770
+ }
custom-permalinks.php CHANGED
@@ -1,859 +1,52 @@
1
- <?php
2
-
3
- /**
4
- * Plugin Name: Custom Permalinks
5
- * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
6
- * Donate link: https://www.paypal.me/yasglobal
7
- * Description: Set custom permalinks on a per-post basis
8
- * Version: 1.0.2
9
- * Author: Sami Ahmed Siddiqui
10
- * Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/
11
- * Text Domain: custom-permalinks
12
- */
13
-
14
- /**
15
- * Copyright 2008-2017 Sami Ahmed Siddiqui <sami@samisiddiqui.com>
16
- *
17
- * This program is free software; you can redistribute it and/or modify
18
- * it under the terms of the GNU General Public License as published by
19
- * the Free Software Foundation; either version 2 of the License, or
20
- * (at your option) any later version.
21
- *
22
- * This program is distributed in the hope that it will be useful,
23
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
24
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25
- * GNU General Public License for more details.
26
- *
27
- * You should have received a copy of the GNU General Public License
28
- * along with this program; if not, write to the Free Software
29
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30
- */
31
-
32
- // Make sure we don't expose any info if called directly
33
- if ( ! defined( 'ABSPATH' ) ) {
34
- echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
35
- exit;
36
- }
37
-
38
- /**
39
- ** Actions and filters
40
- **
41
- **/
42
-
43
- /**
44
- * Filter to replace the post permalink with the custom one
45
- */
46
- function custom_permalinks_post_link($permalink, $post) {
47
- $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
48
- if ( $custom_permalink ) {
49
- $post_type = isset($post->post_type) ? $post->post_type : 'post';
50
- $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post->ID, 'element_type' => $post_type ) );
51
- if ( $language_code )
52
- return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink, $language_code );
53
- else
54
- return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink );
55
- }
56
-
57
- return $permalink;
58
- }
59
-
60
- /**
61
- * Filter to replace the page permalink with the custom one
62
- */
63
- function custom_permalinks_page_link($permalink, $page) {
64
- $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
65
- if ( $custom_permalink ) {
66
- $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $page, 'element_type' => 'page' ) );
67
- if ( $language_code )
68
- return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink, $language_code );
69
- else
70
- return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink );
71
- }
72
-
73
- return $permalink;
74
- }
75
-
76
- /**
77
- * Filter to replace the term permalink with the custom one
78
- */
79
- function custom_permalinks_term_link($permalink, $term) {
80
- $table = get_option('custom_permalink_table');
81
- if ( is_object($term) ) $term = $term->term_id;
82
-
83
- $custom_permalink = custom_permalinks_permalink_for_term($term);
84
- if ( $custom_permalink ) {
85
- $taxonomy = get_term($term);
86
- if ( isset($taxonomy) && isset($taxonomy->term_taxonomy_id) ) {
87
- $term_type = isset($taxonomy->taxonomy) ? $taxonomy->taxonomy : 'category';
88
- $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $taxonomy->term_taxonomy_id, 'element_type' => $term_type ) );
89
- return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink, $language_code );
90
- } else {
91
- return apply_filters( 'wpml_permalink', home_url()."/".$custom_permalink );
92
- }
93
- }
94
-
95
- return $permalink;
96
- }
97
-
98
- /**
99
- * Action to redirect to the custom permalink
100
- *
101
- * @package CustomPermalinks
102
- * @since 0.1
103
- */
104
- function custom_permalinks_redirect() {
105
- // Get request URI, strip parameters
106
- $url = parse_url(get_bloginfo('url'));
107
- $url = isset($url['path']) ? $url['path'] : '';
108
- $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
109
- if ( ($pos=strpos($request, "?")) ) $request = substr($request, 0, $pos);
110
-
111
- $request = custom_permalinks_check_conflicts($request);
112
-
113
- global $wp_query;
114
-
115
- $custom_permalink = '';
116
- $original_permalink = '';
117
-
118
- // If the post/tag/category we're on has a custom permalink, get it and check against the request
119
- if ( (is_single() || is_page()) && !empty($wp_query->post) ) {
120
- $post = $wp_query->post;
121
- $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
122
- $original_permalink = ( $post->post_type == 'page' ? custom_permalinks_original_page_link( $post->ID ) : custom_permalinks_original_post_link( $post->ID ) );
123
- } else if ( is_tag() || is_category() ) {
124
- $theTerm = $wp_query->get_queried_object();
125
- $custom_permalink = custom_permalinks_permalink_for_term($theTerm->term_id);
126
- $original_permalink = (is_tag() ? custom_permalinks_original_tag_link($theTerm->term_id) :
127
- custom_permalinks_original_category_link($theTerm->term_id));
128
- }
129
-
130
- if ( $custom_permalink &&
131
- (substr($request, 0, strlen($custom_permalink)) != $custom_permalink ||
132
- $request == $custom_permalink."/" ) ) {
133
- // Request doesn't match permalink - redirect
134
- $url = $custom_permalink;
135
-
136
- if ( substr($request, 0, strlen($original_permalink)) == $original_permalink &&
137
- trim($request,'/') != trim($original_permalink,'/') ) {
138
- // This is the original link; we can use this url to derive the new one
139
- $url = preg_replace('@//*@', '/', str_replace(trim($original_permalink,'/'), trim($custom_permalink,'/'), $request));
140
- $url = preg_replace('@([^?]*)&@', '\1?', $url);
141
- }
142
-
143
- // Append any query compenent
144
- $url .= strstr($_SERVER['REQUEST_URI'], "?");
145
-
146
- wp_redirect( home_url()."/".$url, 301 );
147
- exit();
148
- }
149
- }
150
-
151
- /**
152
- * Filter to rewrite the query if we have a matching post
153
- *
154
- * @package CustomPermalinks
155
- * @since 0.1
156
- */
157
- function custom_permalinks_request($query) {
158
- global $wpdb;
159
- global $_CPRegisteredURL;
160
-
161
- // First, search for a matching custom permalink, and if found, generate the corresponding
162
- // original URL
163
-
164
- $originalUrl = NULL;
165
-
166
- // Get request URI, strip parameters and /'s
167
- $url = parse_url(get_bloginfo('url'));
168
- $url = isset($url['path']) ? $url['path'] : '';
169
- $request = ltrim(substr($_SERVER['REQUEST_URI'], strlen($url)),'/');
170
- $request = (($pos=strpos($request, '?')) ? substr($request, 0, $pos) : $request);
171
-
172
- if ( !$request ) return $query;
173
-
174
- $request = custom_permalinks_check_conflicts($request);
175
- $request_noslash = preg_replace('@/+@','/', trim($request, '/'));
176
-
177
- $sql = $wpdb->prepare("SELECT p.ID as ID, pm.meta_value as meta_value, p.post_type as post_type, p.post_status as post_status FROM $wpdb->posts AS p, $wpdb->postmeta AS pm WHERE p.ID = pm.post_id AND pm.meta_key = 'custom_permalink' AND meta_value != '' AND ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' ORDER BY LENGTH(meta_value) DESC, p.ID LIMIT 1", $request_noslash, $request_noslash."/");
178
-
179
- $posts = $wpdb->get_results($sql);
180
-
181
- if ( $posts ) {
182
- // A post matches our request
183
-
184
- // Preserve this url for later if it's the same as the permalink (no extra stuff)
185
- if ( $request_noslash == trim($posts[0]->meta_value,'/') )
186
- $_CPRegisteredURL = $request;
187
-
188
- if ( $posts[0]->post_status == 'draft' ) {
189
- if( $posts[0]->post_type == 'page' ) {
190
- $originalUrl = "?page_id=" . $posts[0]->ID;
191
- } else {
192
- $originalUrl = "?post_type=".$posts[0]->post_type."&p=" . $posts[0]->ID;
193
- }
194
- } else {
195
- $originalUrl = preg_replace( '@/+@', '/', str_replace( trim( strtolower($posts[0]->meta_value),'/' ),
196
- ( $posts[0]->post_type == 'page' ?
197
- custom_permalinks_original_page_link($posts[0]->ID)
198
- : custom_permalinks_original_post_link($posts[0]->ID) ),
199
- strtolower($request_noslash) ) );
200
- }
201
- }
202
-
203
- if ( $originalUrl === NULL ) {
204
- // See if any terms have a matching permalink
205
- $table = get_option('custom_permalink_table');
206
- if ( !$table ) return $query;
207
-
208
- foreach ( array_keys($table) as $permalink ) {
209
- if ( $permalink == substr($request_noslash, 0, strlen($permalink)) ||
210
- $permalink == substr($request_noslash."/", 0, strlen($permalink)) ) {
211
- $term = $table[$permalink];
212
-
213
- // Preserve this url for later if it's the same as the permalink (no extra stuff)
214
- if ( $request_noslash == trim($permalink,'/') )
215
- $_CPRegisteredURL = $request;
216
-
217
-
218
- if ( $term['kind'] == 'category') {
219
- $originalUrl = str_replace(trim($permalink,'/'),
220
- custom_permalinks_original_category_link($term['id']),
221
- trim($request,'/'));
222
- } else {
223
- $originalUrl = str_replace(trim($permalink,'/'),
224
- custom_permalinks_original_tag_link($term['id']),
225
- trim($request,'/'));
226
- }
227
- }
228
- }
229
- }
230
-
231
- if ( $originalUrl !== NULL ) {
232
- $originalUrl = str_replace('//', '/', $originalUrl);
233
-
234
- if ( ($pos=strpos($_SERVER['REQUEST_URI'], '?')) !== false ) {
235
- $queryVars = substr($_SERVER['REQUEST_URI'], $pos+1);
236
- $originalUrl .= (strpos($originalUrl, '?') === false ? '?' : '&') . $queryVars;
237
- }
238
-
239
- // Now we have the original URL, run this back through WP->parse_request, in order to
240
- // parse parameters properly. We set $_SERVER variables to fool the function.
241
- $oldRequestUri = $_SERVER['REQUEST_URI']; $oldQueryString = $_SERVER['QUERY_STRING'];
242
- $_SERVER['REQUEST_URI'] = '/'.ltrim($originalUrl,'/');
243
- $_SERVER['QUERY_STRING'] = (($pos=strpos($originalUrl, '?')) !== false ? substr($originalUrl, $pos+1) : '');
244
- parse_str($_SERVER['QUERY_STRING'], $queryArray);
245
- $oldValues = array();
246
- if ( is_array($queryArray) )
247
- foreach ( $queryArray as $key => $value ) {
248
- $oldValues[$key] = $_REQUEST[$key];
249
- $_REQUEST[$key] = $_GET[$key] = $value;
250
- }
251
-
252
- // Re-run the filter, now with original environment in place
253
- remove_filter( 'request', 'custom_permalinks_request', 10, 1 );
254
- global $wp;
255
- $wp->parse_request();
256
- $query = $wp->query_vars;
257
- add_filter( 'request', 'custom_permalinks_request', 10, 1 );
258
-
259
- // Restore values
260
- $_SERVER['REQUEST_URI'] = $oldRequestUri; $_SERVER['QUERY_STRING'] = $oldQueryString;
261
- foreach ( $oldValues as $key => $value ) {
262
- $_REQUEST[$key] = $value;
263
- }
264
- }
265
-
266
- return $query;
267
- }
268
-
269
- /**
270
- * Filter to handle trailing slashes correctly
271
- *
272
- * @package CustomPermalinks
273
- * @since 0.3
274
- */
275
- function custom_permalinks_trailingslash($string, $type) {
276
- global $_CPRegisteredURL;
277
-
278
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
279
- $url = parse_url(get_bloginfo('url'));
280
- $request = ltrim(isset($url['path']) ? substr($string, strlen($url['path'])) : $string, '/');
281
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
282
-
283
- if ( !trim($request) ) return $string;
284
-
285
- if ( trim($_CPRegisteredURL,'/') == trim($request,'/') ) {
286
- if( isset($url['path']) ) {
287
- return ($string{0} == '/' ? '/' : '') . trailingslashit($url['path']) . $_CPRegisteredURL;
288
- } else {
289
- return ($string{0} == '/' ? '/' : '') . $_CPRegisteredURL;
290
- }
291
- }
292
- return $string;
293
- }
294
-
295
- /**
296
- ** Administration
297
- **
298
- **/
299
-
300
- /**
301
- * Per-post/page options (Wordpress > 2.9)
302
- *
303
- * @package CustomPermalinks
304
- * @since 0.6
305
- */
306
- function custom_permalink_get_sample_permalink_html($html, $id, $new_title, $new_slug) {
307
- $permalink = get_post_meta( $id, 'custom_permalink', true );
308
- $post = get_post($id);
309
-
310
- ob_start();
311
- ?>
312
- <?php custom_permalinks_form($permalink, ($post->post_type == "page" ? custom_permalinks_original_page_link($id) : custom_permalinks_original_post_link($id)), false); ?>
313
- <?php
314
- $content = ob_get_contents();
315
- ob_end_clean();
316
-
317
- if ( 'publish' == $post->post_status ) {
318
- $view_post = 'page' == $post->post_type ? __('View Page', 'custom-permalinks') : __('View Post', 'custom-permalinks');
319
- }
320
-
321
- if ( preg_match("@view-post-btn.*?href='([^']+)'@s", $html, $matches) ) {
322
- $permalink = $matches[1];
323
- } else {
324
- list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
325
- if ( false !== strpos($permalink, '%postname%') || false !== strpos($permalink, '%pagename%') ) {
326
- $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
327
- }
328
- }
329
-
330
- return '<strong>' . __('Permalink:', 'custom-permalinks') . "</strong>\n" . $content .
331
- ( isset($view_post) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
332
- }
333
-
334
-
335
- /**
336
- * Per-post options (Wordpress < 2.9)
337
- *
338
- * @package CustomPermalinks
339
- * @since 0.1
340
- */
341
- function custom_permalinks_post_options() {
342
- global $post;
343
- $post_id = $post;
344
- if (is_object($post_id)) {
345
- $post_id = $post_id->ID;
346
- }
347
-
348
- $permalink = get_post_meta( $post_id, 'custom_permalink', true );
349
-
350
- ?>
351
- <div class="postbox closed">
352
- <h3><?php _e('Custom Permalink', 'custom-permalinks') ?></h3>
353
- <div class="inside">
354
- <?php custom_permalinks_form($permalink, custom_permalinks_original_post_link($post_id)); ?>
355
- </div>
356
- </div>
357
- <?php
358
- }
359
-
360
- /**
361
- * Per-page options (Wordpress < 2.9)
362
- *
363
- * @package CustomPermalinks
364
- * @since 0.4
365
- */
366
- function custom_permalinks_page_options() {
367
- global $post;
368
- $post_id = $post;
369
- if (is_object($post_id)) {
370
- $post_id = $post_id->ID;
371
- }
372
-
373
- $permalink = get_post_meta( $post_id, 'custom_permalink', true );
374
-
375
- ?>
376
- <div class="postbox closed">
377
- <h3><?php _e('Custom Permalink', 'custom-permalinks') ?></h3>
378
- <div class="inside">
379
- <?php custom_permalinks_form($permalink, custom_permalinks_original_page_link($post_id)); ?>
380
- </div>
381
- </div>
382
- <?php
383
- }
384
-
385
- /**
386
- * Per-category/tag options
387
- *
388
- * @package CustomPermalinks
389
- * @since 0.1
390
- */
391
- function custom_permalinks_term_options($object) {
392
- if ( is_object($object) && isset($object->term_id) ) {
393
- $permalink = custom_permalinks_permalink_for_term($object->term_id);
394
-
395
- if ( $object->term_id ) {
396
- $originalPermalink = ($object->taxonomy == 'post_tag' ?
397
- custom_permalinks_original_tag_link($object->term_id) :
398
- custom_permalinks_original_category_link($object->term_id) );
399
- }
400
-
401
- custom_permalinks_form($permalink, $originalPermalink);
402
- } else {
403
- custom_permalinks_form('');
404
- }
405
-
406
- // Move the save button to above this form
407
- wp_enqueue_script('jquery');
408
- ?>
409
- <script type="text/javascript">
410
- jQuery(document).ready(function() {
411
- var button = jQuery('#custom_permalink_form').parent().find('.submit');
412
- button.remove().insertAfter(jQuery('#custom_permalink_form'));
413
- });
414
- </script>
415
- <?php
416
- }
417
-
418
- /**
419
- * Helper function to render form
420
- *
421
- * @package CustomPermalinks
422
- * @since 0.1
423
- */
424
- function custom_permalinks_form($permalink, $original="", $renderContainers=true) {
425
- ?>
426
- <input value="true" type="hidden" name="custom_permalinks_edit" />
427
- <input value="<?php echo htmlspecialchars(urldecode($permalink)) ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
428
-
429
- <?php if ( $renderContainers ) : ?>
430
- <table class="form-table" id="custom_permalink_form">
431
- <tr>
432
- <th scope="row"><?php _e('Custom Permalink', 'custom-permalinks') ?></th>
433
- <td>
434
- <?php endif; ?>
435
-
436
- <?php
437
- if ($permalink == '') {
438
- $original = custom_permalinks_check_conflicts($original);
439
- }
440
- ?>
441
-
442
- <?php echo home_url() ?>/
443
- <span id="editable-post-name" title="Click to edit this part of the permalink">
444
- <input type="text" id="new-post-slug" class="text" value="<?php echo htmlspecialchars($permalink ? urldecode($permalink) : urldecode($original)) ?>"
445
- style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd;' ?>"
446
- onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
447
- onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo htmlspecialchars(urldecode($original)) ?>' ) { this.value = '<?php echo htmlspecialchars(urldecode($original)) ?>'; this.style.color = '#ddd'; }"/>
448
- </span>
449
- <?php if ( $renderContainers ) : ?>
450
- <br />
451
- <small><?php _e('Leave blank to disable', 'custom-permalinks') ?></small>
452
-
453
- </td>
454
- </tr>
455
- </table>
456
- <?php
457
- endif;
458
- }
459
-
460
- /**
461
- * Save per-post options
462
- *
463
- * @package CustomPermalinks
464
- * @since 0.1
465
- */
466
- function custom_permalinks_save_post($id) {
467
- if ( !isset($_REQUEST['custom_permalinks_edit']) ) return;
468
-
469
- delete_post_meta( $id, 'custom_permalink' );
470
-
471
- $original_link = custom_permalinks_original_post_link($id);
472
- $permalink_structure = get_option('permalink_structure');
473
-
474
- if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
475
- add_post_meta( $id, 'custom_permalink', str_replace('%2F', '/', urlencode(ltrim(stripcslashes($_REQUEST['custom_permalink']),"/"))) );
476
- }
477
- }
478
-
479
- /**
480
- * Save per-tag options
481
- *
482
- * @package CustomPermalinks
483
- * @since 0.1
484
- */
485
- function custom_permalinks_save_tag($id) {
486
- if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
487
- $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
488
-
489
- if ( $newPermalink == custom_permalinks_original_tag_link($id) )
490
- $newPermalink = '';
491
-
492
- $term = get_term($id, 'post_tag');
493
- custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
494
- }
495
-
496
- /**
497
- * Save per-category options
498
- *
499
- * @package CustomPermalinks
500
- * @since 0.1
501
- */
502
- function custom_permalinks_save_category($id) {
503
- if ( !isset($_REQUEST['custom_permalinks_edit']) || isset($_REQUEST['post_ID']) ) return;
504
- $newPermalink = ltrim(stripcslashes($_REQUEST['custom_permalink']),"/");
505
-
506
- if ( $newPermalink == custom_permalinks_original_category_link($id) )
507
- $newPermalink = '';
508
-
509
- $term = get_term($id, 'category');
510
- custom_permalinks_save_term($term, str_replace('%2F', '/', urlencode($newPermalink)));
511
- }
512
-
513
- /**
514
- * Save term (common to tags and categories)
515
- *
516
- * @package CustomPermalinks
517
- * @since 0.1
518
- */
519
- function custom_permalinks_save_term($term, $permalink) {
520
-
521
- custom_permalinks_delete_term($term->term_id);
522
- $table = get_option('custom_permalink_table');
523
- if ( $permalink )
524
- $table[$permalink] = array(
525
- 'id' => $term->term_id,
526
- 'kind' => ($term->taxonomy == 'category' ? 'category' : 'tag'),
527
- 'slug' => $term->slug);
528
-
529
- update_option('custom_permalink_table', $table);
530
- }
531
-
532
- /**
533
- * Delete post
534
- *
535
- * @package CustomPermalinks
536
- * @since 0.7.14
537
- * @author Piero <maltesepiero@gmail.com>
538
- */
539
- function custom_permalinks_delete_permalink( $id ){
540
- global $wpdb;
541
- // Queries are now WP3.9 compatible (by Steve from Sowmedia.nl)
542
- $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE `meta_key` = 'custom_permalink' AND `post_id` = %d",$id));
543
- }
544
-
545
- /**
546
- * Delete term
547
- *
548
- * @package CustomPermalinks
549
- * @since 0.1
550
- */
551
- function custom_permalinks_delete_term($id) {
552
-
553
- $table = get_option('custom_permalink_table');
554
- if ( $table )
555
- foreach ( $table as $link => $info ) {
556
- if ( $info['id'] == $id ) {
557
- unset($table[$link]);
558
- break;
559
- }
560
- }
561
-
562
- update_option('custom_permalink_table', $table);
563
- }
564
-
565
- /**
566
- * Options page
567
- *
568
- * @package CustomPermalinks
569
- * @since 0.1
570
- */
571
- function custom_permalinks_options_page() {
572
-
573
- // Handle revert
574
- if ( isset($_REQUEST['revertit']) && isset($_REQUEST['revert']) ) {
575
- check_admin_referer('custom-permalinks-bulk');
576
- foreach ( (array)$_REQUEST['revert'] as $identifier ) {
577
- list($kind, $id) = explode('.', $identifier);
578
- switch ( $kind ) {
579
- case 'post':
580
- case 'page':
581
- delete_post_meta( $id, 'custom_permalink' );
582
- break;
583
- case 'tag':
584
- case 'category':
585
- custom_permalinks_delete_term($id);
586
- break;
587
- }
588
- }
589
-
590
- // Redirect
591
- $redirectUrl = $_SERVER['REQUEST_URI'];
592
- ?>
593
- <script type="text/javascript">
594
- document.location = '<?php echo $redirectUrl ?>'
595
- </script>
596
- <?php ;
597
- }
598
-
599
- ?>
600
- <div class="wrap">
601
- <h2><?php _e('Custom Permalinks', 'custom-permalinks') ?></h2>
602
-
603
- <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
604
- <?php wp_nonce_field('custom-permalinks-bulk') ?>
605
-
606
- <div class="tablenav">
607
- <div class="alignleft">
608
- <input type="submit" value="<?php _e('Revert', 'custom-permalinks'); ?>" name="revertit" class="button-secondary delete" />
609
- </div>
610
- <br class="clear" />
611
- </div>
612
- <br class="clear" />
613
- <table class="widefat">
614
- <thead>
615
- <tr>
616
- <th scope="col" class="check-column"><input type="checkbox" /></th>
617
- <th scope="col"><?php _e('Title', 'custom-permalinks') ?></th>
618
- <th scope="col"><?php _e('Type', 'custom-permalinks') ?></th>
619
- <th scope="col"><?php _e('Permalink', 'custom-permalinks') ?></th>
620
- </tr>
621
- </thead>
622
- <tbody>
623
- <?php
624
- $rows = custom_permalinks_admin_rows();
625
- foreach ( $rows as $row ) {
626
- ?>
627
- <tr valign="top">
628
- <th scope="row" class="check-column"><input type="checkbox" name="revert[]" value="<?php echo $row['id'] ?>" /></th>
629
- <td><strong><a class="row-title" href="<?php echo htmlspecialchars($row['editlink']) ?>"><?php echo htmlspecialchars($row['title']) ?></a></strong></td>
630
- <td><?php echo htmlspecialchars($row['type']) ?></td>
631
- <td><a href="<?php echo $row['permalink'] ?>" target="_blank" title="<?php printf(__('Visit %s', 'custom-permalinks'), htmlspecialchars($row['title'])) ?>">
632
- <?php echo htmlspecialchars(urldecode($row['permalink'])) ?>
633
- </a>
634
- </td>
635
- </tr>
636
- <?php
637
- }
638
- ?>
639
- </tbody>
640
- </table>
641
- </form>
642
- </div>
643
- <?php
644
- }
645
-
646
- /**
647
- * Get rows for management view
648
- *
649
- * @package CustomPermalinks
650
- * @since 0.1
651
- */
652
- function custom_permalinks_admin_rows() {
653
- $rows = array();
654
-
655
- // List tags/categories
656
- $table = get_option('custom_permalink_table');
657
- if ( $table && is_array($table) ) {
658
- foreach ( $table as $permalink => $info ) {
659
- $row = array();
660
- $term = get_term($info['id'], ($info['kind'] == 'tag' ? 'post_tag' : 'category'));
661
- $row['id'] = $info['kind'].'.'.$info['id'];
662
- $row['permalink'] = home_url()."/".$permalink;
663
- $row['type'] = ucwords($info['kind']);
664
- $row['title'] = $term->name;
665
- $row['editlink'] = ( $info['kind'] == 'tag' ? 'edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$info['id'] : 'edit-tags.php?action=edit&taxonomy=category&tag_ID='.$info['id'] );
666
- $rows[] = $row;
667
- }
668
- }
669
-
670
- // List posts/pages
671
- global $wpdb;
672
- $query = "SELECT $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id) WHERE
673
- $wpdb->postmeta.meta_key = 'custom_permalink' AND $wpdb->postmeta.meta_value != '';";
674
- $posts = $wpdb->get_results($query);
675
- foreach ( $posts as $post ) {
676
- $row = array();
677
- $row['id'] = 'post.'.$post->ID;
678
- $row['permalink'] = get_permalink($post->ID);
679
- $row['type'] = ucwords( $post->post_type );
680
- $row['title'] = $post->post_title;
681
- $row['editlink'] = 'post.php?action=edit&post='.$post->ID;
682
- $rows[] = $row;
683
- }
684
-
685
- return $rows;
686
- }
687
-
688
- /**
689
- * Get original permalink for post
690
- *
691
- * @package CustomPermalinks
692
- * @since 0.1
693
- */
694
- function custom_permalinks_original_post_link($post_id) {
695
- remove_filter( 'post_link', 'custom_permalinks_post_link', 10, 3 ); // original hook
696
- remove_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
697
-
698
- require_once ABSPATH . '/wp-admin/includes/post.php';
699
- list( $permalink, $post_name ) = get_sample_permalink( $post_id );
700
- $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
701
- $permalink = ltrim(str_replace(home_url(), '', $permalink), '/');
702
-
703
- add_filter( 'post_link', 'custom_permalinks_post_link', 10, 3 ); // original hook
704
- add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
705
-
706
- return $permalink;
707
- }
708
-
709
- /**
710
- * Get original permalink for page
711
- *
712
- * @package CustomPermalinks
713
- * @since 0.4
714
- */
715
- function custom_permalinks_original_page_link($post_id) {
716
- remove_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
717
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
718
-
719
- require_once ABSPATH . '/wp-admin/includes/post.php';
720
- list( $permalink, $post_name ) = get_sample_permalink( $post_id );
721
- $permalink = str_replace(array('%pagename%','%postname%'), $post_name, $permalink);
722
- $permalink = ltrim(str_replace(home_url(), '', $permalink), '/');
723
-
724
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
725
- add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
726
- return $permalink;
727
- }
728
-
729
- /**
730
- * Get original permalink for tag
731
- *
732
- * @package CustomPermalinks
733
- * @since 0.1
734
- */
735
- function custom_permalinks_original_tag_link($tag_id) {
736
- remove_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
737
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
738
- $originalPermalink = ltrim(str_replace(home_url(), '', get_tag_link($tag_id)), '/');
739
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
740
- add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
741
- return $originalPermalink;
742
- }
743
-
744
- /**
745
- * Get original permalink for category
746
- *
747
- * @package CustomPermalinks
748
- * @since 0.1
749
- */
750
- function custom_permalinks_original_category_link($category_id) {
751
- remove_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
752
- remove_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
753
- $originalPermalink = ltrim(str_replace(home_url(), '', get_category_link($category_id)), '/');
754
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
755
- add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
756
- return $originalPermalink;
757
- }
758
-
759
- /**
760
- * Get permalink for term
761
- *
762
- * @package CustomPermalinks
763
- * @since 0.1
764
- */
765
- function custom_permalinks_permalink_for_term($id) {
766
- $table = get_option('custom_permalink_table');
767
- if ( $table )
768
- foreach ( $table as $link => $info ) {
769
- if ( $info['id'] == $id ) {
770
- return $link;
771
- }
772
- }
773
- return false;
774
- }
775
-
776
- /**
777
- * Set up administration menu
778
- *
779
- * @package CustomPermalinks
780
- * @since 0.1
781
- */
782
- function custom_permalinks_setup_admin_menu() {
783
- add_management_page( 'Custom Permalinks', 'Custom Permalinks', 'edit_others_pages', 'custom_permalinks', 'custom_permalinks_options_page' );
784
- }
785
-
786
- /**
787
- * Set up administration header
788
- *
789
- * @package CustomPermalinks
790
- * @since 0.7.20
791
- */
792
- function custom_permalinks_setup_admin_head() {
793
- wp_enqueue_script('admin-forms');
794
- }
795
-
796
- /**
797
- * Check Conflicts and resolve it (e.g: Polylang)
798
- *
799
- * @package CustomPermalinks
800
- * @since 0.9
801
- */
802
- function custom_permalinks_check_conflicts($requested_url = '') {
803
-
804
- if ($requested_url == '') return;
805
-
806
- // Check if the Polylang Plugin is installed so, make changes in the URL
807
- if (defined( 'POLYLANG_VERSION' )) {
808
- $polylang_config = get_option('polylang');
809
- if ($polylang_config['force_lang'] == 1) {
810
-
811
- if(strpos($requested_url, 'language/') !== false)
812
- $requested_url = str_replace("language/", "", $requested_url);
813
-
814
- $remove_lang = ltrim(strstr($requested_url, '/'), '/');
815
- if ($remove_lang != '')
816
- return $remove_lang;
817
- }
818
- }
819
-
820
- return $requested_url;
821
- }
822
-
823
- if (function_exists("add_action") && function_exists("add_filter")) {
824
- add_action( 'template_redirect', 'custom_permalinks_redirect', 5 );
825
- add_filter( 'post_link', 'custom_permalinks_post_link', 10, 3 );
826
- add_filter( 'post_type_link', 'custom_permalinks_post_link', 10, 2 );
827
- add_filter( 'page_link', 'custom_permalinks_page_link', 10, 2 );
828
- add_filter( 'tag_link', 'custom_permalinks_term_link', 10, 2 );
829
- add_filter( 'category_link', 'custom_permalinks_term_link', 10, 2 );
830
- add_filter( 'request', 'custom_permalinks_request', 10, 1 );
831
- add_filter( 'user_trailingslashit', 'custom_permalinks_trailingslash', 10, 2 );
832
-
833
- if (function_exists("get_bloginfo")) {
834
- $v = explode('.', get_bloginfo('version'));
835
- }
836
-
837
- if ( $v[0] >= 2 ) {
838
- add_filter( 'get_sample_permalink_html', 'custom_permalink_get_sample_permalink_html', 10, 4 );
839
- } else {
840
- add_action( 'edit_form_advanced', 'custom_permalinks_post_options' );
841
- add_action( 'edit_page_form', 'custom_permalinks_page_options' );
842
- }
843
-
844
- add_action( 'edit_tag_form', 'custom_permalinks_term_options' );
845
- add_action( 'add_tag_form', 'custom_permalinks_term_options' );
846
- add_action( 'edit_category_form', 'custom_permalinks_term_options' );
847
- add_action( 'save_post', 'custom_permalinks_save_post' );
848
- add_action( 'save_page', 'custom_permalinks_save_post' );
849
- add_action( 'edited_post_tag', 'custom_permalinks_save_tag' );
850
- add_action( 'edited_category', 'custom_permalinks_save_category' );
851
- add_action( 'create_post_tag', 'custom_permalinks_save_tag' );
852
- add_action( 'create_category', 'custom_permalinks_save_category' );
853
- add_action( 'delete_post', 'custom_permalinks_delete_permalink', 10);
854
- add_action( 'delete_post_tag', 'custom_permalinks_delete_term' );
855
- add_action( 'delete_post_category', 'custom_permalinks_delete_term' );
856
- add_action( 'admin_head', 'custom_permalinks_setup_admin_head' );
857
- add_action( 'admin_menu', 'custom_permalinks_setup_admin_menu' );
858
- }
859
- ?>
1
+ <?php
2
+
3
+ /**
4
+ * @package CustomPermalinks
5
+ */
6
+
7
+ /**
8
+ * Plugin Name: Custom Permalinks
9
+ * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
10
+ * Donate link: https://www.paypal.me/yasglobal
11
+ * Description: Set custom permalinks on a per-post basis
12
+ * Version: 1.1
13
+ * Author: Sami Ahmed Siddiqui
14
+ * Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/
15
+ * Text Domain: custom-permalinks
16
+ */
17
+
18
+ /**
19
+ * Copyright 2008-2017 Sami Ahmed Siddiqui <sami@samisiddiqui.com>
20
+ *
21
+ * This program is free software; you can redistribute it and/or modify
22
+ * it under the terms of the GNU General Public License as published by
23
+ * the Free Software Foundation; either version 2 of the License, or
24
+ * (at your option) any later version.
25
+ *
26
+ * This program is distributed in the hope that it will be useful,
27
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
28
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29
+ * GNU General Public License for more details.
30
+ *
31
+ * You should have received a copy of the GNU General Public License
32
+ * along with this program; if not, write to the Free Software
33
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34
+ */
35
+
36
+ // Make sure we don't expose any info if called directly
37
+ if ( ! defined( 'ABSPATH' ) ) {
38
+ echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
39
+ exit;
40
+ }
41
+
42
+ if ( !function_exists("add_action") || !function_exists("add_filter") ) {
43
+ header( 'Status: 403 Forbidden' );
44
+ header( 'HTTP/1.1 403 Forbidden' );
45
+ exit();
46
+ }
47
+
48
+ if ( ! defined( 'CUSTOM_PERMALINKS_FILE' ) ) {
49
+ define( 'CUSTOM_PERMALINKS_FILE', __FILE__ );
50
+ }
51
+
52
+ require_once( dirname( CUSTOM_PERMALINKS_FILE ) . '/custom-permalinks-main.php' );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
readme.txt CHANGED
@@ -5,7 +5,7 @@ Donate link: https://www.paypal.me/yasglobal
5
  Tags: permalink, url, link, address, custom, redirect, custom post type
6
  Requires at least: 2.6
7
  Tested up to: 4.8
8
- Stable tag: 1.0.2
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
@@ -34,6 +34,23 @@ within that category.
34
 
35
  == Changelog ==
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  = 1.0.2 =
38
 
39
  * Fixed Notice and some URL Issues
5
  Tags: permalink, url, link, address, custom, redirect, custom post type
6
  Requires at least: 2.6
7
  Tested up to: 4.8
8
+ Stable tag: 1.1
9
  License: GPLv2 or later
10
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
 
34
 
35
  == Changelog ==
36
 
37
+ = 1.1 =
38
+
39
+ * Enhancements
40
+ * Added PostTypes Permalinks Page
41
+ * View all the PostTypes permalinks
42
+ * Search Permalinks
43
+ * Sort by Title
44
+ * Pagination
45
+ * Added Categories Permalinks Page
46
+ * View all the Category/Tags permalinks
47
+ * Search Permalinks
48
+ * Pagination
49
+
50
+ * Bug Fixes
51
+ * 404 Issues
52
+ * Child pages bug
53
+
54
  = 1.0.2 =
55
 
56
  * Fixed Notice and some URL Issues