Custom Permalinks - Version 1.2.9

Version Description

  • Enhancements
    • Added Filter to enable the like query
    • Bugs
    • PHP error displayed on all pages using custom permalinks
    • Removed LIKE Query in default. It only works if the site uses PolyLang, AMP Plugins or separately enabled using the provided filter.
Download this release

Release Info

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

Code changes from version 1.2.8 to 1.2.9

admin/class-custom-permalinks-admin.php CHANGED
@@ -1,405 +1,405 @@
1
- <?php
2
- /**
3
- * @package CustomPermalinks\Admin
4
- */
5
-
6
- class Custom_Permalinks_Admin {
7
-
8
- /**
9
- * Initializes WordPress hooks
10
- */
11
- function __construct() {
12
- add_action( 'admin_menu', array( $this, 'custom_permalinks_menu' ) );
13
- }
14
-
15
- /**
16
- * Added Pages in Menu for Settings
17
- */
18
- public function custom_permalinks_menu() {
19
- add_menu_page( 'PostTypes Permalinks', 'Custom Permalinks', 'administrator', 'custom-permalinks-post-permalinks', array( $this,'custom_permalinks_post_permalinks' ), 'dashicons-admin-links' );
20
- add_submenu_page( 'custom-permalinks-post-permalinks', 'PostTypes Permalinks', 'PostTypes Permalinks', 'administrator', 'custom-permalinks-post-permalinks', array( $this, 'custom_permalinks_post_permalinks' ) );
21
- add_submenu_page( 'custom-permalinks-post-permalinks', 'Category Permalinks', 'Category Permalinks', 'administrator', 'custom-permalinks-category-permalinks', array( $this, 'custom_permalinks_category_permalinks' ) );
22
- }
23
-
24
- /**
25
- * Shows all the Permalinks created by using this Plugin with Pager and Search Functionality of Posts/Pages
26
- */
27
- public function custom_permalinks_post_permalinks() {
28
- global $wpdb;
29
- $filter_options = '';
30
- $filter_permalink = '';
31
- $search_permalink = '';
32
- $html = '';
33
- $error = '';
34
-
35
- // Handle Bulk Operations
36
- if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
37
- || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
38
- && isset( $_POST['permalink'] ) && ! empty( $_POST['permalink'] ) ) {
39
- $post_ids = implode( ',', $_POST['permalink'] );
40
- if ( preg_match( '/^\d+(?:,\d+)*$/', $post_ids ) ) {
41
- $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids) AND meta_key = 'custom_permalink'" );
42
- } else {
43
- $error = '<div id="message" class="error"><p>' . __( 'There is some error to proceed your request. Please retry with your request or contact to the plugin author.', 'custom-permalinks' ) . '</p></div>';
44
- }
45
- }
46
- $html .= '<div class="wrap">
47
- <h1 class="wp-heading-inline">' . __( 'PostTypes Permalinks', 'custom-permalinks' ) . '</h1>';
48
- $html .= $error;
49
-
50
- $search_value = '';
51
- if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) {
52
- $filter_permalink = 'AND pm.meta_value LIKE "%' . $_GET['s'] . '%"';
53
- $search_permalink = '&s=' . $_GET['s'] . '';
54
- $search_value = ltrim( htmlspecialchars( $_GET['s'] ), '/' );
55
- $html .= '<span class="subtitle">Search results for "' . $search_value . '"</span>';
56
- }
57
- $page_limit = 'LIMIT 0, 20';
58
- if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 1 ) {
59
- $pager = 20 * ( $_GET['paged'] - 1 );
60
- $page_limit = 'LIMIT ' . $pager . ', 20';
61
- }
62
- $sorting_by = 'ORDER By p.ID DESC';
63
- $order_by = 'asc';
64
- $order_by_class = 'desc';
65
- if ( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'title' ) {
66
- $filter_options .= '<input type="hidden" name="orderby" value="title" />';
67
- if ( isset( $_GET['order'] ) && $_GET['order'] == 'desc' ) {
68
- $sorting_by = 'ORDER By p.post_title DESC';
69
- $order_by = 'asc';
70
- $order_by_class = 'desc';
71
- $filter_options .= '<input type="hidden" name="order" value="desc" />';
72
- } else {
73
- $sorting_by = 'ORDER By p.post_title';
74
- $order_by = 'desc';
75
- $order_by_class = 'asc';
76
- $filter_options .= '<input type="hidden" name="order" value="asc" />';
77
- }
78
- }
79
- $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 . "";
80
- $count_posts = $wpdb->get_row( $count_query );
81
-
82
- $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="get">';
83
- $html .= '<p class="search-box">';
84
- $html .= '<input type="hidden" name="page" value="custom-permalinks-post-permalinks" />';
85
- $html .= $filter_options;
86
- $html .= '<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>';
87
- $html .= '<input type="search" id="custom-permalink-search-input" name="s" value="' . $search_value . '">';
88
- $html .= '<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>';
89
- $html .= '</form>';
90
- $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="post">';
91
- $html .= '<div class="tablenav top">';
92
- $html .= '<div class="alignleft actions bulkactions">
93
- <label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
94
- <select name="action" id="bulk-action-selector-top">
95
- <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
96
- <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
97
- </select>
98
- <input type="submit" id="doaction" class="button action" value="Apply">
99
- </div>';
100
-
101
- $posts = 0;
102
- if ( isset( $count_posts->total_permalinks ) && $count_posts->total_permalinks > 0 ) {
103
-
104
- $html .= '<h2 class="screen-reader-text">Custom Permalink navigation</h2>';
105
-
106
- $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 . "";
107
- $posts = $wpdb->get_results( $query );
108
-
109
- $pagination_html = '';
110
- $total_pages = ceil( $count_posts->total_permalinks / 20 );
111
- if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 0 ) {
112
- $pagination_html = $this->custom_permalinks_pager( $count_posts->total_permalinks, $_GET['paged'], $total_pages );
113
- if ( $_GET['paged'] > $total_pages ) {
114
- $redirect_uri = explode( '&paged=' . $_GET['paged'] . '', $_SERVER['REQUEST_URI'] );
115
- header( 'Location: ' . $redirect_uri[0], 301 );
116
- exit();
117
- }
118
- } elseif ( ! isset( $_GET['paged'] ) ) {
119
- $pagination_html = $this->custom_permalinks_pager( $count_posts->total_permalinks, 1, $total_pages );
120
- }
121
-
122
- $html .= $pagination_html;
123
- }
124
- $table_navigation = $this->custom_permalink_tablenav_posts( $order_by_class, $order_by, $search_permalink );
125
-
126
- $html .= '</div>';
127
- $html .= '<table class="wp-list-table widefat fixed striped posts">
128
- <thead>' . $table_navigation . '</thead>
129
- <tbody>';
130
- if ( $posts != 0 && ! empty( $posts ) ) {
131
- foreach ( $posts as $post ) {
132
- $html .= '<tr valign="top">';
133
- $html .= '<th scope="row" class="check-column"><input type="checkbox" name="permalink[]" value="' . $post->ID . '" /></th>';
134
- $html .= '<td><strong><a class="row-title" href="post.php?action=edit&post=' . $post->ID . '">' . $post->post_title . '</a></strong></td>';
135
- $html .= '<td>' . ucwords( $post->post_type ) . '</td>';
136
- $html .= '<td><a href="/' . $post->meta_value . '" target="_blank" title="' . __( "Visit " . $post->post_title, "custom-permalinks" ) . '">/' . urldecode( $post->meta_value ) . '</a></td></tr>';
137
- }
138
- } else {
139
- $html .= '<tr class="no-items"><td class="colspanchange" colspan="10">No permalinks found.</td></tr>';
140
- }
141
- $html .= '</tbody>
142
- <tfoot>' . $table_navigation . '</tfoot>
143
- </table>';
144
-
145
- $html .= '<div class="tablenav bottom">
146
- <div class="alignleft actions bulkactions">
147
- <label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
148
- <select name="action2" id="bulk-action-selector-bottom">
149
- <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
150
- <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
151
- </select>
152
- <input type="submit" id="doaction2" class="button action" value="Apply">
153
- </div>
154
- ' . $pagination_html . '
155
- </div>';
156
- $html .= '</form>
157
- </div>';
158
- echo $html;
159
- }
160
-
161
- /**
162
- * Return the Navigation row HTML same as Default Posts page for PostTypes
163
- */
164
- private function custom_permalink_tablenav_posts( $order_by_class, $order_by, $search_permalink ) {
165
- $nav = '<tr>
166
- <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>
167
- <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>
168
- <th scope="col">' . __( "Type", "custom-permalinks" ) . '</th>
169
- <th scope="col">' . __( "Permalink", "custom-permalinks" ) . '</th>
170
- </tr>';
171
- return $nav;
172
- }
173
-
174
- /**
175
- * Shows all the Permalinks created by using this Plugin with Pager and Search Functionality of Category/Tags
176
- */
177
- public function custom_permalinks_category_permalinks() {
178
-
179
- $search_permalink = '';
180
- $html = '';
181
-
182
- // Handle Bulk Operations
183
- if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
184
- || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
185
- && isset( $_POST['permalink'] ) && ! empty( $_POST['permalink'] ) ) {
186
- $remove_perm = $_POST['permalink'];
187
- $data = get_option( 'custom_permalink_table' );
188
- if ( isset( $data ) && is_array( $data ) ) {
189
- $i = 0;
190
- foreach ( $data as $link => $info ) {
191
- if ( in_array( $info['id'], $remove_perm ) ) {
192
- unset( $data[$link] );
193
- unset( $remove_perm[$i] );
194
- if ( ! is_array( $remove_perm ) || empty( $remove_perm ) )
195
- break;
196
- }
197
- $i++;
198
- }
199
- }
200
- update_option( 'custom_permalink_table', $data );
201
- }
202
- $html .= '<div class="wrap">
203
- <h1 class="wp-heading-inline">' . __( 'Category/Tags Permalinks', 'custom-permalinks' ) . '</h1>';
204
-
205
- $search_value = '';
206
- if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) {
207
- $search_permalink = '&s=' . $_GET['s'] . '';
208
- $search_value = ltrim( htmlspecialchars( $_GET['s'] ), '/' );
209
- $html .= '<span class="subtitle">Search results for "' . $search_value . '"</span>';
210
- }
211
- $pager_offset = '0';
212
- $page_limit = 20;
213
- if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 1 ) {
214
- $pager_offset = 20 * ( $_GET['paged'] - 1 );
215
- $page_limit = $pager_offset + 20;
216
- }
217
- $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="get">';
218
- $html .= '<p class="search-box">';
219
- $html .= '<input type="hidden" name="page" value="custom-permalinks-category-permalinks" />';
220
- $html .= '<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>';
221
- $html .= '<input type="search" id="custom-permalink-search-input" name="s" value="' . $search_value . '">';
222
- $html .= '<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>';
223
- $html .= '</form>';
224
- $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="post">';
225
- $html .= '<div class="tablenav top">';
226
- $html .= '<div class="alignleft actions bulkactions">
227
- <label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
228
- <select name="action" id="bulk-action-selector-top">
229
- <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
230
- <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
231
- </select>
232
- <input type="submit" id="doaction" class="button action" value="Apply">
233
- </div>';
234
-
235
- $posts = 0;
236
- $table = get_option( 'custom_permalink_table' );
237
- $count_tags = count( $table );
238
- $pagination_html = '';
239
- if ( isset( $table ) && is_array( $table ) && $count_tags > 0 ) {
240
-
241
- $filtered = array();
242
- if ( $search_value != '' ) {
243
- foreach ( $table as $key => $value ) {
244
- if( preg_match( '/' . $search_value . '/', $key) ) {
245
- $filtered[$key] = $value;
246
- }
247
- }
248
- $table = $filtered;
249
- $count_tags = count( $table );
250
- }
251
-
252
- $html .= '<h2 class="screen-reader-text">Custom Permalink navigation</h2>';
253
-
254
- $total_pages = ceil( $count_tags / 20 );
255
- if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 0 ) {
256
- $pagination_html = $this->custom_permalinks_pager( $count_tags, $_GET['paged'], $total_pages);
257
- if ( $_GET['paged'] > $total_pages ) {
258
- $redirect_uri = explode( '&paged=' . $_GET['paged'] . '', $_SERVER['REQUEST_URI'] );
259
- header( 'Location: ' . $redirect_uri[0], 301 );
260
- exit();
261
- }
262
- } elseif ( ! isset( $_GET['paged'] ) ) {
263
- $pagination_html = $this->custom_permalinks_pager( $count_tags, 1, $total_pages );
264
- }
265
-
266
- $html .= $pagination_html;
267
- }
268
- $table_navigation = $this->custom_permalink_tablenav_category( $search_permalink );
269
-
270
- $html .= '</div>';
271
- $html .= '<table class="wp-list-table widefat fixed striped posts">
272
- <thead>' . $table_navigation . '</thead>
273
- <tbody>';
274
-
275
- if ( $table && is_array( $table ) && $count_tags > 0 ) {
276
- uasort( $table, array( 'Custom_Permalinks_Admin', 'custom_permalinks_sort_array' ) );
277
- $i = -1;
278
- foreach ( $table as $permalink => $info ) {
279
- $i++;
280
- if ( $i < $pager_offset )
281
- continue;
282
-
283
- if ( $i >= $page_limit )
284
- break;
285
-
286
- $type = $info['kind'] == 'tag' ? 'post_tag' : 'category';
287
- $term = get_term( $info['id'], $type );
288
- $html .= '<tr valign="top">';
289
- $html .= '<th scope="row" class="check-column"><input type="checkbox" name="permalink[]" value="' . $info['id'] . '" /></th>';
290
- $html .= '<td><strong><a class="row-title" href="edit-tags.php?action=edit&taxonomy=' . $type . '&tag_ID=' . $info['id'] . ' ">' . $term->name . '</a></strong></td>';
291
- $html .= '<td>' . ucwords( $info['kind'] ) . '</td>';
292
- $html .= '<td><a href="/' . $permalink . '" target="_blank" title="' . __( "Visit " . $term->name, "custom-permalinks" ) . '">/' . $permalink . '</a></td></tr>';
293
- }
294
- } else {
295
- $html .= '<tr class="no-items"><td class="colspanchange" colspan="10">No permalinks found.</td></tr>';
296
- }
297
- $html .= '</tbody>
298
- <tfoot>' . $table_navigation . '</tfoot>
299
- </table>';
300
-
301
- $html .= '<div class="tablenav bottom">
302
- <div class="alignleft actions bulkactions">
303
- <label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
304
- <select name="action2" id="bulk-action-selector-bottom">
305
- <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
306
- <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
307
- </select>
308
- <input type="submit" id="doaction2" class="button action" value="Apply">
309
- </div>
310
- ' . $pagination_html . '
311
- </div>';
312
- $html .= '</form>
313
- </div>';
314
- echo $html;
315
- }
316
-
317
- /**
318
- * Sort the terms array in desc order using term id
319
- */
320
- private static function custom_permalinks_sort_array( $a, $b ) {
321
- return $b['id'] - $a['id'];
322
- }
323
-
324
- /**
325
- * Return the Navigation row HTML same as Default Posts page for Category
326
- */
327
- private function custom_permalink_tablenav_category( $search_permalink ) {
328
- $nav = '<tr>
329
- <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>
330
- <th scope="col" id="title" class="manage-column column-title column-primary">' . __( "Title", "custom-permalinks" ) . '</th>
331
- <th scope="col">' . __( "Type", "custom-permalinks" ) . '</th>
332
- <th scope="col">' . __( "Permalink", "custom-permalinks" ) . '</th>
333
- </tr>';
334
- return $nav;
335
- }
336
-
337
- /**
338
- * Return the Pager HTML
339
- */
340
- private function custom_permalinks_pager( $total_permalinks, $current_pager_value = 1, $total_pager = 0 ) {
341
-
342
- if ( $total_pager == 0 ) return;
343
-
344
- if ( $total_pager == 1 ) {
345
- $pagination_html = '<div class="tablenav-pages one-page">
346
- <span class="displaying-num">' . $total_permalinks . ' items</span>
347
- </div>';
348
- return $pagination_html;
349
- }
350
-
351
- $remove_pager_uri = explode( '&paged=' . $current_pager_value . '', $_SERVER['REQUEST_URI'] );
352
- $pagination_html = '<div class="tablenav-pages">
353
- <span class="displaying-num">' . $total_permalinks . ' items</span>
354
- <span class="pagination-links">';
355
-
356
- if ( $current_pager_value == 1 ) {
357
- $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo; </span>
358
- <span class="tablenav-pages-navspan" aria-hidden="true">&lsaquo; </span>';
359
- } else {
360
- $prev_page = $current_pager_value - 1;
361
- if ( $prev_page == 1 ) {
362
- $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo;</span>';
363
- } else {
364
- $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=1" title="First page" class="first-page">
365
- <span class="screen-reader-text">First page</span>
366
- <span aria-hidden="true">&laquo;</span>
367
- </a> ';
368
- }
369
- $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=' . $prev_page . '" title="Previous page" class="prev-page">
370
- <span class="screen-reader-text">Previous page</span>
371
- <span aria-hidden="true">&lsaquo;</span>
372
- </a> ';
373
- }
374
-
375
- $pagination_html .= '<span class="paging-input">
376
- <label for="current-page-selector" class="screen-reader-text">Current Page</label>
377
- <input class="current-page" id="current-page-selector" type="text" name="paged" value="' . $current_pager_value . '" size="1" aria-describedby="table-paging" />
378
- <span class="tablenav-paging-text"> of <span class="total-pages">' . $total_pager . ' </span> </span>
379
- </span>';
380
-
381
- if ( $current_pager_value == $total_pager ) {
382
- $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&rsaquo; </span>
383
- <span class="tablenav-pages-navspan" aria-hidden="true">&raquo; </span>';
384
- } else {
385
- $next_page = $current_pager_value + 1;
386
- $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=' . $next_page . '" title="Next page" class="next-page">
387
- <span class="screen-reader-text">Next page</span>
388
- <span aria-hidden="true">&rsaquo;</span>
389
- </a> ';
390
- if ( $total_pager == $next_page) {
391
- $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&raquo;</span>';
392
- } else {
393
- $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=' . $total_pager . '" title="Last page" class="last-page">
394
- <span class="screen-reader-text">Last page</span>
395
- <span aria-hidden="true">&raquo;</span>
396
- </a> ';
397
- }
398
-
399
- }
400
- $pagination_html .= '</span></div>';
401
-
402
- return $pagination_html;
403
- }
404
-
405
- }
1
+ <?php
2
+ /**
3
+ * @package CustomPermalinks\Admin
4
+ */
5
+
6
+ class Custom_Permalinks_Admin {
7
+
8
+ /**
9
+ * Initializes WordPress hooks
10
+ */
11
+ function __construct() {
12
+ add_action( 'admin_menu', array( $this, 'custom_permalinks_menu' ) );
13
+ }
14
+
15
+ /**
16
+ * Added Pages in Menu for Settings
17
+ */
18
+ public function custom_permalinks_menu() {
19
+ add_menu_page( 'PostTypes Permalinks', 'Custom Permalinks', 'administrator', 'custom-permalinks-post-permalinks', array( $this,'custom_permalinks_post_permalinks' ), 'dashicons-admin-links' );
20
+ add_submenu_page( 'custom-permalinks-post-permalinks', 'PostTypes Permalinks', 'PostTypes Permalinks', 'administrator', 'custom-permalinks-post-permalinks', array( $this, 'custom_permalinks_post_permalinks' ) );
21
+ add_submenu_page( 'custom-permalinks-post-permalinks', 'Category Permalinks', 'Category Permalinks', 'administrator', 'custom-permalinks-category-permalinks', array( $this, 'custom_permalinks_category_permalinks' ) );
22
+ }
23
+
24
+ /**
25
+ * Shows all the Permalinks created by using this Plugin with Pager and Search Functionality of Posts/Pages
26
+ */
27
+ public function custom_permalinks_post_permalinks() {
28
+ global $wpdb;
29
+ $filter_options = '';
30
+ $filter_permalink = '';
31
+ $search_permalink = '';
32
+ $html = '';
33
+ $error = '';
34
+
35
+ // Handle Bulk Operations
36
+ if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
37
+ || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
38
+ && isset( $_POST['permalink'] ) && ! empty( $_POST['permalink'] ) ) {
39
+ $post_ids = implode( ',', $_POST['permalink'] );
40
+ if ( preg_match( '/^\d+(?:,\d+)*$/', $post_ids ) ) {
41
+ $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE post_id IN ($post_ids) AND meta_key = 'custom_permalink'" );
42
+ } else {
43
+ $error = '<div id="message" class="error"><p>' . __( 'There is some error to proceed your request. Please retry with your request or contact to the plugin author.', 'custom-permalinks' ) . '</p></div>';
44
+ }
45
+ }
46
+ $html .= '<div class="wrap">
47
+ <h1 class="wp-heading-inline">' . __( 'PostTypes Permalinks', 'custom-permalinks' ) . '</h1>';
48
+ $html .= $error;
49
+
50
+ $search_value = '';
51
+ if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) {
52
+ $filter_permalink = 'AND pm.meta_value LIKE "%' . $_GET['s'] . '%"';
53
+ $search_permalink = '&s=' . $_GET['s'] . '';
54
+ $search_value = ltrim( htmlspecialchars( $_GET['s'] ), '/' );
55
+ $html .= '<span class="subtitle">Search results for "' . $search_value . '"</span>';
56
+ }
57
+ $page_limit = 'LIMIT 0, 20';
58
+ if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 1 ) {
59
+ $pager = 20 * ( $_GET['paged'] - 1 );
60
+ $page_limit = 'LIMIT ' . $pager . ', 20';
61
+ }
62
+ $sorting_by = 'ORDER By p.ID DESC';
63
+ $order_by = 'asc';
64
+ $order_by_class = 'desc';
65
+ if ( isset( $_GET['orderby'] ) && $_GET['orderby'] == 'title' ) {
66
+ $filter_options .= '<input type="hidden" name="orderby" value="title" />';
67
+ if ( isset( $_GET['order'] ) && $_GET['order'] == 'desc' ) {
68
+ $sorting_by = 'ORDER By p.post_title DESC';
69
+ $order_by = 'asc';
70
+ $order_by_class = 'desc';
71
+ $filter_options .= '<input type="hidden" name="order" value="desc" />';
72
+ } else {
73
+ $sorting_by = 'ORDER By p.post_title';
74
+ $order_by = 'desc';
75
+ $order_by_class = 'asc';
76
+ $filter_options .= '<input type="hidden" name="order" value="asc" />';
77
+ }
78
+ }
79
+ $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 . "";
80
+ $count_posts = $wpdb->get_row( $count_query );
81
+
82
+ $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="get">';
83
+ $html .= '<p class="search-box">';
84
+ $html .= '<input type="hidden" name="page" value="custom-permalinks-post-permalinks" />';
85
+ $html .= $filter_options;
86
+ $html .= '<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>';
87
+ $html .= '<input type="search" id="custom-permalink-search-input" name="s" value="' . $search_value . '">';
88
+ $html .= '<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>';
89
+ $html .= '</form>';
90
+ $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="post">';
91
+ $html .= '<div class="tablenav top">';
92
+ $html .= '<div class="alignleft actions bulkactions">
93
+ <label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
94
+ <select name="action" id="bulk-action-selector-top">
95
+ <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
96
+ <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
97
+ </select>
98
+ <input type="submit" id="doaction" class="button action" value="Apply">
99
+ </div>';
100
+
101
+ $posts = 0;
102
+ if ( isset( $count_posts->total_permalinks ) && $count_posts->total_permalinks > 0 ) {
103
+
104
+ $html .= '<h2 class="screen-reader-text">Custom Permalink navigation</h2>';
105
+
106
+ $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 . "";
107
+ $posts = $wpdb->get_results( $query );
108
+
109
+ $pagination_html = '';
110
+ $total_pages = ceil( $count_posts->total_permalinks / 20 );
111
+ if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 0 ) {
112
+ $pagination_html = $this->custom_permalinks_pager( $count_posts->total_permalinks, $_GET['paged'], $total_pages );
113
+ if ( $_GET['paged'] > $total_pages ) {
114
+ $redirect_uri = explode( '&paged=' . $_GET['paged'] . '', $_SERVER['REQUEST_URI'] );
115
+ header( 'Location: ' . $redirect_uri[0], 301 );
116
+ exit();
117
+ }
118
+ } elseif ( ! isset( $_GET['paged'] ) ) {
119
+ $pagination_html = $this->custom_permalinks_pager( $count_posts->total_permalinks, 1, $total_pages );
120
+ }
121
+
122
+ $html .= $pagination_html;
123
+ }
124
+ $table_navigation = $this->custom_permalink_tablenav_posts( $order_by_class, $order_by, $search_permalink );
125
+
126
+ $html .= '</div>';
127
+ $html .= '<table class="wp-list-table widefat fixed striped posts">
128
+ <thead>' . $table_navigation . '</thead>
129
+ <tbody>';
130
+ if ( $posts != 0 && ! empty( $posts ) ) {
131
+ foreach ( $posts as $post ) {
132
+ $html .= '<tr valign="top">';
133
+ $html .= '<th scope="row" class="check-column"><input type="checkbox" name="permalink[]" value="' . $post->ID . '" /></th>';
134
+ $html .= '<td><strong><a class="row-title" href="post.php?action=edit&post=' . $post->ID . '">' . $post->post_title . '</a></strong></td>';
135
+ $html .= '<td>' . ucwords( $post->post_type ) . '</td>';
136
+ $html .= '<td><a href="/' . $post->meta_value . '" target="_blank" title="' . __( "Visit " . $post->post_title, "custom-permalinks" ) . '">/' . urldecode( $post->meta_value ) . '</a></td></tr>';
137
+ }
138
+ } else {
139
+ $html .= '<tr class="no-items"><td class="colspanchange" colspan="10">No permalinks found.</td></tr>';
140
+ }
141
+ $html .= '</tbody>
142
+ <tfoot>' . $table_navigation . '</tfoot>
143
+ </table>';
144
+
145
+ $html .= '<div class="tablenav bottom">
146
+ <div class="alignleft actions bulkactions">
147
+ <label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
148
+ <select name="action2" id="bulk-action-selector-bottom">
149
+ <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
150
+ <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
151
+ </select>
152
+ <input type="submit" id="doaction2" class="button action" value="Apply">
153
+ </div>
154
+ ' . $pagination_html . '
155
+ </div>';
156
+ $html .= '</form>
157
+ </div>';
158
+ echo $html;
159
+ }
160
+
161
+ /**
162
+ * Return the Navigation row HTML same as Default Posts page for PostTypes
163
+ */
164
+ private function custom_permalink_tablenav_posts( $order_by_class, $order_by, $search_permalink ) {
165
+ $nav = '<tr>
166
+ <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>
167
+ <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>
168
+ <th scope="col">' . __( "Type", "custom-permalinks" ) . '</th>
169
+ <th scope="col">' . __( "Permalink", "custom-permalinks" ) . '</th>
170
+ </tr>';
171
+ return $nav;
172
+ }
173
+
174
+ /**
175
+ * Shows all the Permalinks created by using this Plugin with Pager and Search Functionality of Category/Tags
176
+ */
177
+ public function custom_permalinks_category_permalinks() {
178
+
179
+ $search_permalink = '';
180
+ $html = '';
181
+
182
+ // Handle Bulk Operations
183
+ if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'delete' )
184
+ || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'delete' )
185
+ && isset( $_POST['permalink'] ) && ! empty( $_POST['permalink'] ) ) {
186
+ $remove_perm = $_POST['permalink'];
187
+ $data = get_option( 'custom_permalink_table' );
188
+ if ( isset( $data ) && is_array( $data ) ) {
189
+ $i = 0;
190
+ foreach ( $data as $link => $info ) {
191
+ if ( in_array( $info['id'], $remove_perm ) ) {
192
+ unset( $data[$link] );
193
+ unset( $remove_perm[$i] );
194
+ if ( ! is_array( $remove_perm ) || empty( $remove_perm ) )
195
+ break;
196
+ }
197
+ $i++;
198
+ }
199
+ }
200
+ update_option( 'custom_permalink_table', $data );
201
+ }
202
+ $html .= '<div class="wrap">
203
+ <h1 class="wp-heading-inline">' . __( 'Category/Tags Permalinks', 'custom-permalinks' ) . '</h1>';
204
+
205
+ $search_value = '';
206
+ if ( isset( $_GET['s'] ) && ! empty( $_GET['s'] ) ) {
207
+ $search_permalink = '&s=' . $_GET['s'] . '';
208
+ $search_value = ltrim( htmlspecialchars( $_GET['s'] ), '/' );
209
+ $html .= '<span class="subtitle">Search results for "' . $search_value . '"</span>';
210
+ }
211
+ $pager_offset = '0';
212
+ $page_limit = 20;
213
+ if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 1 ) {
214
+ $pager_offset = 20 * ( $_GET['paged'] - 1 );
215
+ $page_limit = $pager_offset + 20;
216
+ }
217
+ $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="get">';
218
+ $html .= '<p class="search-box">';
219
+ $html .= '<input type="hidden" name="page" value="custom-permalinks-category-permalinks" />';
220
+ $html .= '<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>';
221
+ $html .= '<input type="search" id="custom-permalink-search-input" name="s" value="' . $search_value . '">';
222
+ $html .= '<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>';
223
+ $html .= '</form>';
224
+ $html .= '<form action="' . $_SERVER["REQUEST_URI"] . '" method="post">';
225
+ $html .= '<div class="tablenav top">';
226
+ $html .= '<div class="alignleft actions bulkactions">
227
+ <label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>
228
+ <select name="action" id="bulk-action-selector-top">
229
+ <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
230
+ <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
231
+ </select>
232
+ <input type="submit" id="doaction" class="button action" value="Apply">
233
+ </div>';
234
+
235
+ $posts = 0;
236
+ $table = get_option( 'custom_permalink_table' );
237
+ $count_tags = count( $table );
238
+ $pagination_html = '';
239
+ if ( isset( $table ) && is_array( $table ) && $count_tags > 0 ) {
240
+
241
+ $filtered = array();
242
+ if ( $search_value != '' ) {
243
+ foreach ( $table as $key => $value ) {
244
+ if( preg_match( '/' . $search_value . '/', $key) ) {
245
+ $filtered[$key] = $value;
246
+ }
247
+ }
248
+ $table = $filtered;
249
+ $count_tags = count( $table );
250
+ }
251
+
252
+ $html .= '<h2 class="screen-reader-text">Custom Permalink navigation</h2>';
253
+
254
+ $total_pages = ceil( $count_tags / 20 );
255
+ if ( isset( $_GET['paged'] ) && is_numeric( $_GET['paged'] ) && $_GET['paged'] > 0 ) {
256
+ $pagination_html = $this->custom_permalinks_pager( $count_tags, $_GET['paged'], $total_pages);
257
+ if ( $_GET['paged'] > $total_pages ) {
258
+ $redirect_uri = explode( '&paged=' . $_GET['paged'] . '', $_SERVER['REQUEST_URI'] );
259
+ header( 'Location: ' . $redirect_uri[0], 301 );
260
+ exit();
261
+ }
262
+ } elseif ( ! isset( $_GET['paged'] ) ) {
263
+ $pagination_html = $this->custom_permalinks_pager( $count_tags, 1, $total_pages );
264
+ }
265
+
266
+ $html .= $pagination_html;
267
+ }
268
+ $table_navigation = $this->custom_permalink_tablenav_category( $search_permalink );
269
+
270
+ $html .= '</div>';
271
+ $html .= '<table class="wp-list-table widefat fixed striped posts">
272
+ <thead>' . $table_navigation . '</thead>
273
+ <tbody>';
274
+
275
+ if ( $table && is_array( $table ) && $count_tags > 0 ) {
276
+ uasort( $table, array( 'Custom_Permalinks_Admin', 'custom_permalinks_sort_array' ) );
277
+ $i = -1;
278
+ foreach ( $table as $permalink => $info ) {
279
+ $i++;
280
+ if ( $i < $pager_offset )
281
+ continue;
282
+
283
+ if ( $i >= $page_limit )
284
+ break;
285
+
286
+ $type = $info['kind'] == 'tag' ? 'post_tag' : 'category';
287
+ $term = get_term( $info['id'], $type );
288
+ $html .= '<tr valign="top">';
289
+ $html .= '<th scope="row" class="check-column"><input type="checkbox" name="permalink[]" value="' . $info['id'] . '" /></th>';
290
+ $html .= '<td><strong><a class="row-title" href="edit-tags.php?action=edit&taxonomy=' . $type . '&tag_ID=' . $info['id'] . ' ">' . $term->name . '</a></strong></td>';
291
+ $html .= '<td>' . ucwords( $info['kind'] ) . '</td>';
292
+ $html .= '<td><a href="/' . $permalink . '" target="_blank" title="' . __( "Visit " . $term->name, "custom-permalinks" ) . '">/' . $permalink . '</a></td></tr>';
293
+ }
294
+ } else {
295
+ $html .= '<tr class="no-items"><td class="colspanchange" colspan="10">No permalinks found.</td></tr>';
296
+ }
297
+ $html .= '</tbody>
298
+ <tfoot>' . $table_navigation . '</tfoot>
299
+ </table>';
300
+
301
+ $html .= '<div class="tablenav bottom">
302
+ <div class="alignleft actions bulkactions">
303
+ <label for="bulk-action-selector-bottom" class="screen-reader-text">Select bulk action</label>
304
+ <select name="action2" id="bulk-action-selector-bottom">
305
+ <option value="-1">' . __( "Bulk Actions", "custom-permalinks" ) . '</option>
306
+ <option value="delete">' . __( "Delete Permalinks", "custom-permalinks" ) . '</option>
307
+ </select>
308
+ <input type="submit" id="doaction2" class="button action" value="Apply">
309
+ </div>
310
+ ' . $pagination_html . '
311
+ </div>';
312
+ $html .= '</form>
313
+ </div>';
314
+ echo $html;
315
+ }
316
+
317
+ /**
318
+ * Sort the terms array in desc order using term id
319
+ */
320
+ private static function custom_permalinks_sort_array( $a, $b ) {
321
+ return $b['id'] - $a['id'];
322
+ }
323
+
324
+ /**
325
+ * Return the Navigation row HTML same as Default Posts page for Category
326
+ */
327
+ private function custom_permalink_tablenav_category( $search_permalink ) {
328
+ $nav = '<tr>
329
+ <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>
330
+ <th scope="col" id="title" class="manage-column column-title column-primary">' . __( "Title", "custom-permalinks" ) . '</th>
331
+ <th scope="col">' . __( "Type", "custom-permalinks" ) . '</th>
332
+ <th scope="col">' . __( "Permalink", "custom-permalinks" ) . '</th>
333
+ </tr>';
334
+ return $nav;
335
+ }
336
+
337
+ /**
338
+ * Return the Pager HTML
339
+ */
340
+ private function custom_permalinks_pager( $total_permalinks, $current_pager_value = 1, $total_pager = 0 ) {
341
+
342
+ if ( $total_pager == 0 ) return;
343
+
344
+ if ( $total_pager == 1 ) {
345
+ $pagination_html = '<div class="tablenav-pages one-page">
346
+ <span class="displaying-num">' . $total_permalinks . ' items</span>
347
+ </div>';
348
+ return $pagination_html;
349
+ }
350
+
351
+ $remove_pager_uri = explode( '&paged=' . $current_pager_value . '', $_SERVER['REQUEST_URI'] );
352
+ $pagination_html = '<div class="tablenav-pages">
353
+ <span class="displaying-num">' . $total_permalinks . ' items</span>
354
+ <span class="pagination-links">';
355
+
356
+ if ( $current_pager_value == 1 ) {
357
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo; </span>
358
+ <span class="tablenav-pages-navspan" aria-hidden="true">&lsaquo; </span>';
359
+ } else {
360
+ $prev_page = $current_pager_value - 1;
361
+ if ( $prev_page == 1 ) {
362
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&laquo;</span>';
363
+ } else {
364
+ $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=1" title="First page" class="first-page">
365
+ <span class="screen-reader-text">First page</span>
366
+ <span aria-hidden="true">&laquo;</span>
367
+ </a> ';
368
+ }
369
+ $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=' . $prev_page . '" title="Previous page" class="prev-page">
370
+ <span class="screen-reader-text">Previous page</span>
371
+ <span aria-hidden="true">&lsaquo;</span>
372
+ </a> ';
373
+ }
374
+
375
+ $pagination_html .= '<span class="paging-input">
376
+ <label for="current-page-selector" class="screen-reader-text">Current Page</label>
377
+ <input class="current-page" id="current-page-selector" type="text" name="paged" value="' . $current_pager_value . '" size="1" aria-describedby="table-paging" />
378
+ <span class="tablenav-paging-text"> of <span class="total-pages">' . $total_pager . ' </span> </span>
379
+ </span>';
380
+
381
+ if ( $current_pager_value == $total_pager ) {
382
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&rsaquo; </span>
383
+ <span class="tablenav-pages-navspan" aria-hidden="true">&raquo; </span>';
384
+ } else {
385
+ $next_page = $current_pager_value + 1;
386
+ $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=' . $next_page . '" title="Next page" class="next-page">
387
+ <span class="screen-reader-text">Next page</span>
388
+ <span aria-hidden="true">&rsaquo;</span>
389
+ </a> ';
390
+ if ( $total_pager == $next_page) {
391
+ $pagination_html .= '<span class="tablenav-pages-navspan" aria-hidden="true">&raquo;</span>';
392
+ } else {
393
+ $pagination_html .= ' <a href="' . $remove_pager_uri[0] . '&paged=' . $total_pager . '" title="Last page" class="last-page">
394
+ <span class="screen-reader-text">Last page</span>
395
+ <span aria-hidden="true">&raquo;</span>
396
+ </a> ';
397
+ }
398
+
399
+ }
400
+ $pagination_html .= '</span></div>';
401
+
402
+ return $pagination_html;
403
+ }
404
+
405
+ }
custom-permalinks-main.php CHANGED
@@ -1,43 +1,43 @@
1
- <?php
2
- /**
3
- * @package CustomPermalinks\Main
4
- */
5
-
6
- // Make sure we don't expose any info if called directly
7
- if ( ! defined( 'ABSPATH' ) ) {
8
- echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
9
- exit;
10
- }
11
-
12
- if ( ! function_exists( "add_action" ) || ! function_exists( "add_filter" ) ) {
13
- header( 'Status: 403 Forbidden' );
14
- header( 'HTTP/1.1 403 Forbidden' );
15
- exit();
16
- }
17
-
18
- define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.2.8' );
19
-
20
- if ( ! defined( 'CUSTOM_PERMALINKS_PATH' ) ) {
21
- define( 'CUSTOM_PERMALINKS_PATH', plugin_dir_path( __FILE__ ) );
22
- }
23
-
24
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
25
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
26
- $custom_permalinks_frontend->init();
27
-
28
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-form.php' );
29
- $custom_permalinks_form = new Custom_Permalinks_Form();
30
- $custom_permalinks_form->init();
31
-
32
- if ( is_admin() ) {
33
- require_once( CUSTOM_PERMALINKS_PATH . 'admin/class-custom-permalinks-admin.php' );
34
- new Custom_Permalinks_Admin();
35
- }
36
-
37
- /**
38
- * Add textdomain hook for translation
39
- */
40
- function custom_permalinks_translation_capability() {
41
- load_plugin_textdomain( 'custom-permalinks', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
42
- }
43
  add_action( 'plugins_loaded', 'custom_permalinks_translation_capability' );
1
+ <?php
2
+ /**
3
+ * @package CustomPermalinks\Main
4
+ */
5
+
6
+ // Make sure we don't expose any info if called directly
7
+ if ( ! defined( 'ABSPATH' ) ) {
8
+ echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
9
+ exit;
10
+ }
11
+
12
+ if ( ! function_exists( 'add_action' ) || ! function_exists( 'add_filter' ) ) {
13
+ header( 'Status: 403 Forbidden' );
14
+ header( 'HTTP/1.1 403 Forbidden' );
15
+ exit();
16
+ }
17
+
18
+ define( 'CUSTOM_PERMALINKS_PLUGIN_VERSION', '1.2.9' );
19
+
20
+ if ( ! defined( 'CUSTOM_PERMALINKS_PATH' ) ) {
21
+ define( 'CUSTOM_PERMALINKS_PATH', plugin_dir_path( __FILE__ ) );
22
+ }
23
+
24
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
25
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
26
+ $custom_permalinks_frontend->init();
27
+
28
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-form.php' );
29
+ $custom_permalinks_form = new Custom_Permalinks_Form();
30
+ $custom_permalinks_form->init();
31
+
32
+ if ( is_admin() ) {
33
+ require_once( CUSTOM_PERMALINKS_PATH . 'admin/class-custom-permalinks-admin.php' );
34
+ new Custom_Permalinks_Admin();
35
+ }
36
+
37
+ /**
38
+ * Add textdomain hook for translation
39
+ */
40
+ function custom_permalinks_translation_capability() {
41
+ load_plugin_textdomain( 'custom-permalinks', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
42
+ }
43
  add_action( 'plugins_loaded', 'custom_permalinks_translation_capability' );
custom-permalinks.php CHANGED
@@ -1,54 +1,48 @@
1
- <?php
2
-
3
- /**
4
- * Plugin Name: Custom Permalinks
5
- * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
6
- * Description: Set custom permalinks on a per-post basis
7
- * Version: 1.2.8
8
- * Author: Sami Ahmed Siddiqui
9
- * Author URI: https://www.yasglobal.com/web-design-development/wordpress/custom-permalinks/
10
- * Donate link: https://www.paypal.me/yasglobal
11
- * License: GPLv2 or later
12
- *
13
- * Text Domain: custom-permalinks
14
- * Domain Path: /languages/
15
- *
16
- * @package CustomPermalinks
17
- */
18
-
19
- /**
20
- * Custom Permalinks Plugin
21
- * Copyright 2008-2017 Sami Ahmed Siddiqui <sami@samisiddiqui.com>
22
- *
23
- * This program is free software; you can redistribute it and/or modify
24
- * it under the terms of the GNU General Public License as published by
25
- * the Free Software Foundation; either version 2 of the License, or
26
- * (at your option) any later version.
27
- *
28
- * This program is distributed in the hope that it will be useful,
29
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
- * GNU General Public License for more details.
32
- *
33
- * You should have received a copy of the GNU General Public License
34
- * along with this program; if not, write to the Free Software
35
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36
- */
37
-
38
- // Make sure we don't expose any info if called directly
39
- if ( ! defined( 'ABSPATH' ) ) {
40
- echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
41
- exit;
42
- }
43
-
44
- if ( ! function_exists( "add_action" ) || ! function_exists( "add_filter" ) ) {
45
- header( 'Status: 403 Forbidden' );
46
- header( 'HTTP/1.1 403 Forbidden' );
47
- exit();
48
- }
49
-
50
- if ( ! defined( 'CUSTOM_PERMALINKS_FILE' ) ) {
51
- define( 'CUSTOM_PERMALINKS_FILE', __FILE__ );
52
- }
53
-
54
- require_once( dirname( CUSTOM_PERMALINKS_FILE ) . '/custom-permalinks-main.php' );
1
+ <?php
2
+
3
+ /**
4
+ * Plugin Name: Custom Permalinks
5
+ * Plugin URI: https://wordpress.org/plugins/custom-permalinks/
6
+ * Description: Set custom permalinks on a per-post basis
7
+ * Version: 1.2.9
8
+ * Author: Sami Ahmed Siddiqui
9
+ * Author URI: https://www.custompermalinks.com
10
+ * Donate link: https://www.paypal.me/yasglobal
11
+ * License: GPLv2 or later
12
+ *
13
+ * Text Domain: custom-permalinks
14
+ * Domain Path: /languages/
15
+ *
16
+ * @package CustomPermalinks
17
+ */
18
+
19
+ /**
20
+ * Custom Permalinks Plugin
21
+ * Copyright 2008-2018 Sami Ahmed Siddiqui <sami@samisiddiqui.com>
22
+ *
23
+ * This program is free software; you can redistribute it and/or modify
24
+ * it under the terms of the GNU General Public License as published by
25
+ * the Free Software Foundation; either version 2 of the License, or
26
+ * (at your option) any later version.
27
+ *
28
+ * This program is distributed in the hope that it will be useful,
29
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
30
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31
+ * GNU General Public License for more details.
32
+ *
33
+ * You should have received a copy of the GNU General Public License
34
+ * along with this program; if not, write to the Free Software
35
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36
+ */
37
+
38
+ // Make sure we don't expose any info if called directly
39
+ if ( ! defined( 'ABSPATH' ) ) {
40
+ echo 'Hi there! I\'m just a plugin, not much I can do when called directly.';
41
+ exit;
42
+ }
43
+
44
+ if ( ! defined( 'CUSTOM_PERMALINKS_FILE' ) ) {
45
+ define( 'CUSTOM_PERMALINKS_FILE', __FILE__ );
46
+ }
47
+
48
+ require_once( dirname( CUSTOM_PERMALINKS_FILE ) . '/custom-permalinks-main.php' );
 
 
 
 
 
 
frontend/class-custom-permalinks-form.php CHANGED
@@ -1,346 +1,346 @@
1
- <?php
2
- /**
3
- * @package CustomPermalinks\Admin\Form
4
- */
5
-
6
- class Custom_Permalinks_Form {
7
-
8
- /**
9
- * Initialize WordPress Hooks
10
- */
11
- public function init() {
12
-
13
- add_filter( 'get_sample_permalink_html', array( $this, 'custom_permalinks_get_sample_permalink_html' ), 10, 4 );
14
-
15
- add_action( 'save_post', array( $this, 'custom_permalinks_save_post' ), 10, 3 );
16
- add_action( 'delete_post', array( $this, 'custom_permalinks_delete_permalink' ), 10 );
17
-
18
- add_action( 'edit_tag_form', array( $this, 'custom_permalinks_term_options' ) );
19
- add_action( 'add_tag_form', array( $this, 'custom_permalinks_term_options' ) );
20
- add_action( 'edit_category_form', array( $this, 'custom_permalinks_term_options' ) );
21
-
22
- add_action( 'edited_post_tag', array( $this, 'custom_permalinks_save_tag' ) );
23
- add_action( 'edited_category', array( $this, 'custom_permalinks_save_category' ) );
24
- add_action( 'create_post_tag', array( $this, 'custom_permalinks_save_tag' ) );
25
- add_action( 'create_category', array( $this, 'custom_permalinks_save_category' ) );
26
- add_action( 'delete_post_tag', array( $this, 'custom_permalinks_delete_term' ) );
27
- add_action( 'delete_post_category', array( $this, 'custom_permalinks_delete_term' ) );
28
- }
29
-
30
- /**
31
- * Save per-post options
32
- */
33
- public function custom_permalinks_save_post( $id ) {
34
- if ( ! isset( $_REQUEST['custom_permalinks_edit'] ) ) {
35
- return;
36
- }
37
-
38
- delete_post_meta( $id, 'custom_permalink' );
39
-
40
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
41
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
42
- $original_link = $custom_permalinks_frontend->custom_permalinks_original_post_link( $id );
43
- $permalink_structure = get_option( 'permalink_structure' );
44
-
45
- if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
46
- add_post_meta( $id, 'custom_permalink',
47
- str_replace( '%2F', '/',
48
- urlencode( ltrim( stripcslashes( $_REQUEST['custom_permalink'] ), "/" ) )
49
- )
50
- );
51
- }
52
- }
53
-
54
- /**
55
- * Delete post
56
- */
57
- public function custom_permalinks_delete_permalink( $id ) {
58
- global $wpdb;
59
- $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = 'custom_permalink' AND post_id = %d", $id ) );
60
- }
61
-
62
- /**
63
- * Per-post/page options (Wordpress > 2.9)
64
- */
65
- public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_title, $new_slug ) {
66
- $permalink = get_post_meta( $id, 'custom_permalink', true );
67
- $post = get_post( $id );
68
-
69
- if ( $post->post_type == 'attachment' || $post->ID == get_option( 'page_on_front' ) ) {
70
- return $html;
71
- }
72
-
73
- $exclude_post_types = $post->post_type;
74
- $excluded = apply_filters( 'custom_permalinks_exclude_post_type', $exclude_post_types );
75
- if ( '__true' === $excluded ) {
76
- return $html;
77
- }
78
-
79
- ob_start();
80
-
81
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
82
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
83
- if ( $post->post_type == "page" ) {
84
- $original_page_url = $custom_permalinks_frontend->custom_permalinks_original_page_link( $id );
85
- $this->custom_permalinks_get_form( $permalink, $original_page_url, false, $post->post_name );
86
- } else {
87
- $original_post_url = $custom_permalinks_frontend->custom_permalinks_original_post_link( $id );
88
- $this->custom_permalinks_get_form( $permalink, $original_post_url, false, $post->post_name );
89
- }
90
-
91
- $content = ob_get_contents();
92
- ob_end_clean();
93
-
94
- if ( 'publish' == $post->post_status ) {
95
- $view_post = 'page' == $post->post_type ? __( 'View Page', 'custom-permalinks' ) : __( 'View Post', 'custom-permalinks' );
96
- }
97
-
98
- if ( preg_match( "@view-post-btn.*?href='([^']+)'@s", $html, $matches ) ) {
99
- $permalink = $matches[1];
100
- } else {
101
- list( $permalink, $post_name ) = get_sample_permalink( $post->ID, $new_title, $new_slug );
102
- if ( false !== strpos( $permalink, '%postname%' )
103
- || false !== strpos( $permalink, '%pagename%' ) ) {
104
- $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
105
- }
106
- }
107
-
108
- return '<strong>' . __( 'Permalink:', 'custom-permalinks' ) . "</strong>\n" . $content .
109
- ( isset( $view_post ) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
110
- }
111
-
112
- /**
113
- * Per-post options (Wordpress < 2.9)
114
- */
115
- public function custom_permalinks_post_options() {
116
- global $post;
117
- $post_id = $post;
118
- if ( is_object( $post_id ) ) {
119
- $post_id = $post_id->ID;
120
- }
121
-
122
- $permalink = get_post_meta( $post_id, 'custom_permalink', true );
123
- ?>
124
- <div class="postbox closed">
125
- <h3><?php _e( 'Custom Permalink', 'custom-permalinks' ) ?></h3>
126
- <div class="inside">
127
- <?php
128
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
129
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
130
- $custom_permalinks_frontend->custom_permalinks_get_form( $permalink, $custom_permalinks_frontend->custom_permalinks_original_post_link( $post_id ) );
131
- ?>
132
- </div>
133
- </div>
134
- <?php
135
- }
136
-
137
- /**
138
- * Per-page options (Wordpress < 2.9)
139
- */
140
- public function custom_permalinks_page_options() {
141
- global $post;
142
- $post_id = $post;
143
- if (is_object( $post_id ) ) {
144
- $post_id = $post_id->ID;
145
- }
146
-
147
- $permalink = get_post_meta( $post_id, 'custom_permalink', true );
148
-
149
- ?>
150
- <div class="postbox closed">
151
- <h3><?php _e( 'Custom Permalink', 'custom-permalinks' ); ?></h3>
152
- <div class="inside">
153
- <?php
154
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
155
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
156
- $page_permalink = $custom_permalinks_frontend->custom_permalinks_original_page_link( $post_id );
157
- $this->custom_permalinks_get_form( $permalink, $page_permalink );
158
- ?>
159
- </div>
160
- </div>
161
- <?php
162
- }
163
-
164
- /**
165
- * Per-category/tag options
166
- */
167
- public function custom_permalinks_term_options( $object ) {
168
- if ( is_object( $object ) && isset( $object->term_id ) ) {
169
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
170
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
171
- $permalink = $custom_permalinks_frontend->custom_permalinks_permalink_for_term( $object->term_id );
172
-
173
- if ( $object->term_id ) {
174
- if ( $object->taxonomy == 'post_tag' ) {
175
- $originalPermalink = $custom_permalinks_frontend->custom_permalinks_original_tag_link( $object->term_id );
176
- } else {
177
- $originalPermalink = $custom_permalinks_frontend->custom_permalinks_original_category_link( $object->term_id );
178
- }
179
- }
180
-
181
- $this->custom_permalinks_get_form( $permalink, $originalPermalink );
182
- } else {
183
- $this->custom_permalinks_get_form( '' );
184
- }
185
-
186
- // Move the save button to above this form
187
- wp_enqueue_script( 'jquery' );
188
- ?>
189
- <script type="text/javascript">
190
- jQuery(document).ready(function() {
191
- var button = jQuery('#custom_permalink_form').parent().find('.submit');
192
- button.remove().insertAfter(jQuery('#custom_permalink_form'));
193
- });
194
- </script>
195
- <?php
196
- }
197
-
198
- /**
199
- * Helper function to render form
200
- */
201
- private function custom_permalinks_get_form( $permalink, $original = "", $renderContainers = true, $postname = "" ) {
202
- ?>
203
- <input value="true" type="hidden" name="custom_permalinks_edit" />
204
- <input value="<?php echo home_url(); ?>" type="hidden" name="custom_permalinks_home_url" id="custom_permalinks_home_url" />
205
- <input value="<?php echo htmlspecialchars( urldecode( $permalink ) ); ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
206
-
207
- <?php
208
- if ( $renderContainers ) :
209
- ?>
210
- <table class="form-table" id="custom_permalink_form">
211
- <tr>
212
- <th scope="row"><?php _e( 'Custom Permalink', 'custom-permalinks' ); ?></th>
213
- <td>
214
- <?php
215
- endif;
216
- if ( $permalink == '' ) {
217
- $original = $this->custom_permalinks_check_conflicts( $original );
218
- }
219
- $post_slug = htmlspecialchars( $permalink ? urldecode( $permalink ) : urldecode( $original ) );
220
- $original_encoded_url = htmlspecialchars( urldecode( $original ) );
221
- wp_enqueue_script( 'custom-permalinks-form', plugins_url( '/js/script-form.min.js', __FILE__ ), array(), false, true );
222
- $postname_html = '';
223
- if ( isset( $postname ) && $postname != "" ) {
224
- $postname_html = '<input type="hidden" id="new-post-slug" class="text" value="' . $postname . '" />';
225
- }
226
-
227
- echo home_url() . '/<span id="editable-post-name" title="Click to edit this part of the permalink">' . $postname_html;
228
-
229
- ?>
230
- <input type="text" id="custom-permalinks-post-slug" class="text" value="<?php echo $post_slug; ?>"
231
- style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd'; ?>"
232
- onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
233
- onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo $original_encoded_url; ?>' ) { this.value = '<?php echo $original_encoded_url; ?>'; this.style.color = '#ddd'; }" />
234
- </span>
235
-
236
- <?php if ( $renderContainers ) : ?>
237
- <br />
238
- <small><?php _e( 'Leave blank to disable', 'custom-permalinks' ); ?></small>
239
- </td>
240
- </tr>
241
- </table>
242
- <?php
243
- endif;
244
- }
245
-
246
- /**
247
- * Save per-tag options
248
- */
249
- public function custom_permalinks_save_tag( $id ) {
250
- if ( ! isset( $_REQUEST['custom_permalinks_edit'] )
251
- || isset( $_REQUEST['post_ID'] ) ) {
252
- return;
253
- }
254
- $newPermalink = ltrim( stripcslashes( $_REQUEST['custom_permalink'] ), "/" );
255
-
256
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
257
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
258
- if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_tag_link( $id ) ) {
259
- return;
260
- }
261
-
262
- $term = get_term( $id, 'post_tag' );
263
- $this->custom_permalinks_save_term( $term, str_replace( '%2F', '/', urlencode( $newPermalink ) ) );
264
- }
265
-
266
- /**
267
- * Save per-category options
268
- */
269
- public function custom_permalinks_save_category( $id ) {
270
- if ( ! isset( $_REQUEST['custom_permalinks_edit'] )
271
- || isset( $_REQUEST['post_ID'] ) ) {
272
- return;
273
- }
274
- $newPermalink = ltrim( stripcslashes( $_REQUEST['custom_permalink'] ), "/" );
275
-
276
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
277
- $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
278
- if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_category_link( $id ) ) {
279
- return;
280
- }
281
-
282
- $term = get_term( $id, 'category' );
283
- $this->custom_permalinks_save_term( $term, str_replace( '%2F', '/', urlencode( $newPermalink ) ) );
284
- }
285
-
286
- /**
287
- * Save term (common to tags and categories)
288
- */
289
- public function custom_permalinks_save_term( $term, $permalink ) {
290
-
291
- $this->custom_permalinks_delete_term( $term->term_id );
292
- $table = get_option( 'custom_permalink_table' );
293
- if ( $permalink ) {
294
- $table[$permalink] = array(
295
- 'id' => $term->term_id,
296
- 'kind' => ( $term->taxonomy == 'category' ? 'category' : 'tag' ),
297
- 'slug' => $term->slug
298
- );
299
- }
300
-
301
- update_option( 'custom_permalink_table', $table );
302
- }
303
-
304
- /**
305
- * Delete term
306
- */
307
- public function custom_permalinks_delete_term( $id ) {
308
- $table = get_option( 'custom_permalink_table' );
309
- if ( $table ) {
310
- foreach ( $table as $link => $info ) {
311
- if ( $info['id'] == $id ) {
312
- unset( $table[$link] );
313
- break;
314
- }
315
- }
316
- }
317
- update_option( 'custom_permalink_table', $table );
318
- }
319
-
320
- /**
321
- * Check Conflicts and resolve it (e.g: Polylang)
322
- */
323
- public function custom_permalinks_check_conflicts( $requested_url = '' ) {
324
- if ( $requested_url == '' ) {
325
- return;
326
- }
327
-
328
- // Check if the Polylang Plugin is installed so, make changes in the URL
329
- if ( defined( 'POLYLANG_VERSION' ) ) {
330
- $polylang_config = get_option( 'polylang' );
331
- if ( $polylang_config['force_lang'] == 1 ) {
332
-
333
- if ( strpos( $requested_url, 'language/' ) !== false ) {
334
- $requested_url = str_replace( "language/", "", $requested_url );
335
- }
336
-
337
- $remove_lang = ltrim( strstr( $requested_url, '/' ), '/' );
338
- if ( $remove_lang != '' ) {
339
- return $remove_lang;
340
- }
341
- }
342
- }
343
- return $requested_url;
344
- }
345
-
346
- }
1
+ <?php
2
+ /**
3
+ * @package CustomPermalinks\Admin\Form
4
+ */
5
+
6
+ class Custom_Permalinks_Form {
7
+
8
+ /**
9
+ * Initialize WordPress Hooks
10
+ */
11
+ public function init() {
12
+
13
+ add_filter( 'get_sample_permalink_html', array( $this, 'custom_permalinks_get_sample_permalink_html' ), 10, 4 );
14
+
15
+ add_action( 'save_post', array( $this, 'custom_permalinks_save_post' ), 10, 3 );
16
+ add_action( 'delete_post', array( $this, 'custom_permalinks_delete_permalink' ), 10 );
17
+
18
+ add_action( 'edit_tag_form', array( $this, 'custom_permalinks_term_options' ) );
19
+ add_action( 'add_tag_form', array( $this, 'custom_permalinks_term_options' ) );
20
+ add_action( 'edit_category_form', array( $this, 'custom_permalinks_term_options' ) );
21
+
22
+ add_action( 'edited_post_tag', array( $this, 'custom_permalinks_save_tag' ) );
23
+ add_action( 'edited_category', array( $this, 'custom_permalinks_save_category' ) );
24
+ add_action( 'create_post_tag', array( $this, 'custom_permalinks_save_tag' ) );
25
+ add_action( 'create_category', array( $this, 'custom_permalinks_save_category' ) );
26
+ add_action( 'delete_post_tag', array( $this, 'custom_permalinks_delete_term' ) );
27
+ add_action( 'delete_post_category', array( $this, 'custom_permalinks_delete_term' ) );
28
+ }
29
+
30
+ /**
31
+ * Save per-post options
32
+ */
33
+ public function custom_permalinks_save_post( $id ) {
34
+ if ( ! isset( $_REQUEST['custom_permalinks_edit'] ) ) {
35
+ return;
36
+ }
37
+
38
+ delete_post_meta( $id, 'custom_permalink' );
39
+
40
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
41
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
42
+ $original_link = $custom_permalinks_frontend->custom_permalinks_original_post_link( $id );
43
+ $permalink_structure = get_option( 'permalink_structure' );
44
+
45
+ if ( $_REQUEST['custom_permalink'] && $_REQUEST['custom_permalink'] != $original_link ) {
46
+ add_post_meta( $id, 'custom_permalink',
47
+ str_replace( '%2F', '/',
48
+ urlencode( ltrim( stripcslashes( $_REQUEST['custom_permalink'] ), "/" ) )
49
+ )
50
+ );
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Delete post
56
+ */
57
+ public function custom_permalinks_delete_permalink( $id ) {
58
+ global $wpdb;
59
+ $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->postmeta WHERE meta_key = 'custom_permalink' AND post_id = %d", $id ) );
60
+ }
61
+
62
+ /**
63
+ * Per-post/page options (Wordpress > 2.9)
64
+ */
65
+ public function custom_permalinks_get_sample_permalink_html( $html, $id, $new_title, $new_slug ) {
66
+ $permalink = get_post_meta( $id, 'custom_permalink', true );
67
+ $post = get_post( $id );
68
+
69
+ if ( $post->post_type == 'attachment' || $post->ID == get_option( 'page_on_front' ) ) {
70
+ return $html;
71
+ }
72
+
73
+ $exclude_post_types = $post->post_type;
74
+ $excluded = apply_filters( 'custom_permalinks_exclude_post_type', $exclude_post_types );
75
+ if ( '__true' === $excluded ) {
76
+ return $html;
77
+ }
78
+
79
+ ob_start();
80
+
81
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
82
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
83
+ if ( $post->post_type == "page" ) {
84
+ $original_page_url = $custom_permalinks_frontend->custom_permalinks_original_page_link( $id );
85
+ $this->custom_permalinks_get_form( $permalink, $original_page_url, false, $post->post_name );
86
+ } else {
87
+ $original_post_url = $custom_permalinks_frontend->custom_permalinks_original_post_link( $id );
88
+ $this->custom_permalinks_get_form( $permalink, $original_post_url, false, $post->post_name );
89
+ }
90
+
91
+ $content = ob_get_contents();
92
+ ob_end_clean();
93
+
94
+ if ( 'publish' == $post->post_status ) {
95
+ $view_post = 'page' == $post->post_type ? __( 'View Page', 'custom-permalinks' ) : __( 'View Post', 'custom-permalinks' );
96
+ }
97
+
98
+ if ( preg_match( "@view-post-btn.*?href='([^']+)'@s", $html, $matches ) ) {
99
+ $permalink = $matches[1];
100
+ } else {
101
+ list( $permalink, $post_name ) = get_sample_permalink( $post->ID, $new_title, $new_slug );
102
+ if ( false !== strpos( $permalink, '%postname%' )
103
+ || false !== strpos( $permalink, '%pagename%' ) ) {
104
+ $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
105
+ }
106
+ }
107
+
108
+ return '<strong>' . __( 'Permalink:', 'custom-permalinks' ) . "</strong>\n" . $content .
109
+ ( isset( $view_post ) ? "<span id='view-post-btn'><a href='$permalink' class='button button-small' target='_blank'>$view_post</a></span>\n" : "" );
110
+ }
111
+
112
+ /**
113
+ * Per-post options (Wordpress < 2.9)
114
+ */
115
+ public function custom_permalinks_post_options() {
116
+ global $post;
117
+ $post_id = $post;
118
+ if ( is_object( $post_id ) ) {
119
+ $post_id = $post_id->ID;
120
+ }
121
+
122
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
123
+ ?>
124
+ <div class="postbox closed">
125
+ <h3><?php _e( 'Custom Permalink', 'custom-permalinks' ) ?></h3>
126
+ <div class="inside">
127
+ <?php
128
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
129
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
130
+ $custom_permalinks_frontend->custom_permalinks_get_form( $permalink, $custom_permalinks_frontend->custom_permalinks_original_post_link( $post_id ) );
131
+ ?>
132
+ </div>
133
+ </div>
134
+ <?php
135
+ }
136
+
137
+ /**
138
+ * Per-page options (Wordpress < 2.9)
139
+ */
140
+ public function custom_permalinks_page_options() {
141
+ global $post;
142
+ $post_id = $post;
143
+ if (is_object( $post_id ) ) {
144
+ $post_id = $post_id->ID;
145
+ }
146
+
147
+ $permalink = get_post_meta( $post_id, 'custom_permalink', true );
148
+
149
+ ?>
150
+ <div class="postbox closed">
151
+ <h3><?php _e( 'Custom Permalink', 'custom-permalinks' ); ?></h3>
152
+ <div class="inside">
153
+ <?php
154
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
155
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
156
+ $page_permalink = $custom_permalinks_frontend->custom_permalinks_original_page_link( $post_id );
157
+ $this->custom_permalinks_get_form( $permalink, $page_permalink );
158
+ ?>
159
+ </div>
160
+ </div>
161
+ <?php
162
+ }
163
+
164
+ /**
165
+ * Per-category/tag options
166
+ */
167
+ public function custom_permalinks_term_options( $object ) {
168
+ if ( is_object( $object ) && isset( $object->term_id ) ) {
169
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
170
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
171
+ $permalink = $custom_permalinks_frontend->custom_permalinks_permalink_for_term( $object->term_id );
172
+
173
+ if ( $object->term_id ) {
174
+ if ( $object->taxonomy == 'post_tag' ) {
175
+ $originalPermalink = $custom_permalinks_frontend->custom_permalinks_original_tag_link( $object->term_id );
176
+ } else {
177
+ $originalPermalink = $custom_permalinks_frontend->custom_permalinks_original_category_link( $object->term_id );
178
+ }
179
+ }
180
+
181
+ $this->custom_permalinks_get_form( $permalink, $originalPermalink );
182
+ } else {
183
+ $this->custom_permalinks_get_form( '' );
184
+ }
185
+
186
+ // Move the save button to above this form
187
+ wp_enqueue_script( 'jquery' );
188
+ ?>
189
+ <script type="text/javascript">
190
+ jQuery(document).ready(function() {
191
+ var button = jQuery('#custom_permalink_form').parent().find('.submit');
192
+ button.remove().insertAfter(jQuery('#custom_permalink_form'));
193
+ });
194
+ </script>
195
+ <?php
196
+ }
197
+
198
+ /**
199
+ * Helper function to render form
200
+ */
201
+ private function custom_permalinks_get_form( $permalink, $original = "", $renderContainers = true, $postname = "" ) {
202
+ ?>
203
+ <input value="true" type="hidden" name="custom_permalinks_edit" />
204
+ <input value="<?php echo home_url(); ?>" type="hidden" name="custom_permalinks_home_url" id="custom_permalinks_home_url" />
205
+ <input value="<?php echo htmlspecialchars( urldecode( $permalink ) ); ?>" type="hidden" name="custom_permalink" id="custom_permalink" />
206
+
207
+ <?php
208
+ if ( $renderContainers ) :
209
+ ?>
210
+ <table class="form-table" id="custom_permalink_form">
211
+ <tr>
212
+ <th scope="row"><?php _e( 'Custom Permalink', 'custom-permalinks' ); ?></th>
213
+ <td>
214
+ <?php
215
+ endif;
216
+ if ( $permalink == '' ) {
217
+ $original = $this->custom_permalinks_check_conflicts( $original );
218
+ }
219
+ $post_slug = htmlspecialchars( $permalink ? urldecode( $permalink ) : urldecode( $original ) );
220
+ $original_encoded_url = htmlspecialchars( urldecode( $original ) );
221
+ wp_enqueue_script( 'custom-permalinks-form', plugins_url( '/js/script-form.min.js', __FILE__ ), array(), false, true );
222
+ $postname_html = '';
223
+ if ( isset( $postname ) && $postname != "" ) {
224
+ $postname_html = '<input type="hidden" id="new-post-slug" class="text" value="' . $postname . '" />';
225
+ }
226
+
227
+ echo home_url() . '/<span id="editable-post-name" title="Click to edit this part of the permalink">' . $postname_html;
228
+
229
+ ?>
230
+ <input type="text" id="custom-permalinks-post-slug" class="text" value="<?php echo $post_slug; ?>"
231
+ style="width: 250px; <?php if ( !$permalink ) echo 'color: #ddd'; ?>"
232
+ onfocus="if ( this.style.color = '#ddd' ) { this.style.color = '#000'; }"
233
+ onblur="document.getElementById('custom_permalink').value = this.value; if ( this.value == '' || this.value == '<?php echo $original_encoded_url; ?>' ) { this.value = '<?php echo $original_encoded_url; ?>'; this.style.color = '#ddd'; }" />
234
+ </span>
235
+
236
+ <?php if ( $renderContainers ) : ?>
237
+ <br />
238
+ <small><?php _e( 'Leave blank to disable', 'custom-permalinks' ); ?></small>
239
+ </td>
240
+ </tr>
241
+ </table>
242
+ <?php
243
+ endif;
244
+ }
245
+
246
+ /**
247
+ * Save per-tag options
248
+ */
249
+ public function custom_permalinks_save_tag( $id ) {
250
+ if ( ! isset( $_REQUEST['custom_permalinks_edit'] )
251
+ || isset( $_REQUEST['post_ID'] ) ) {
252
+ return;
253
+ }
254
+ $newPermalink = ltrim( stripcslashes( $_REQUEST['custom_permalink'] ), "/" );
255
+
256
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
257
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
258
+ if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_tag_link( $id ) ) {
259
+ return;
260
+ }
261
+
262
+ $term = get_term( $id, 'post_tag' );
263
+ $this->custom_permalinks_save_term( $term, str_replace( '%2F', '/', urlencode( $newPermalink ) ) );
264
+ }
265
+
266
+ /**
267
+ * Save per-category options
268
+ */
269
+ public function custom_permalinks_save_category( $id ) {
270
+ if ( ! isset( $_REQUEST['custom_permalinks_edit'] )
271
+ || isset( $_REQUEST['post_ID'] ) ) {
272
+ return;
273
+ }
274
+ $newPermalink = ltrim( stripcslashes( $_REQUEST['custom_permalink'] ), "/" );
275
+
276
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-frontend.php' );
277
+ $custom_permalinks_frontend = new Custom_Permalinks_Frontend();
278
+ if ( $newPermalink == $custom_permalinks_frontend->custom_permalinks_original_category_link( $id ) ) {
279
+ return;
280
+ }
281
+
282
+ $term = get_term( $id, 'category' );
283
+ $this->custom_permalinks_save_term( $term, str_replace( '%2F', '/', urlencode( $newPermalink ) ) );
284
+ }
285
+
286
+ /**
287
+ * Save term (common to tags and categories)
288
+ */
289
+ public function custom_permalinks_save_term( $term, $permalink ) {
290
+
291
+ $this->custom_permalinks_delete_term( $term->term_id );
292
+ $table = get_option( 'custom_permalink_table' );
293
+ if ( $permalink ) {
294
+ $table[$permalink] = array(
295
+ 'id' => $term->term_id,
296
+ 'kind' => ( $term->taxonomy == 'category' ? 'category' : 'tag' ),
297
+ 'slug' => $term->slug
298
+ );
299
+ }
300
+
301
+ update_option( 'custom_permalink_table', $table );
302
+ }
303
+
304
+ /**
305
+ * Delete term
306
+ */
307
+ public function custom_permalinks_delete_term( $id ) {
308
+ $table = get_option( 'custom_permalink_table' );
309
+ if ( $table ) {
310
+ foreach ( $table as $link => $info ) {
311
+ if ( $info['id'] == $id ) {
312
+ unset( $table[$link] );
313
+ break;
314
+ }
315
+ }
316
+ }
317
+ update_option( 'custom_permalink_table', $table );
318
+ }
319
+
320
+ /**
321
+ * Check Conflicts and resolve it (e.g: Polylang)
322
+ */
323
+ public function custom_permalinks_check_conflicts( $requested_url = '' ) {
324
+ if ( $requested_url == '' ) {
325
+ return;
326
+ }
327
+
328
+ // Check if the Polylang Plugin is installed so, make changes in the URL
329
+ if ( defined( 'POLYLANG_VERSION' ) ) {
330
+ $polylang_config = get_option( 'polylang' );
331
+ if ( $polylang_config['force_lang'] == 1 ) {
332
+
333
+ if ( strpos( $requested_url, 'language/' ) !== false ) {
334
+ $requested_url = str_replace( "language/", "", $requested_url );
335
+ }
336
+
337
+ $remove_lang = ltrim( strstr( $requested_url, '/' ), '/' );
338
+ if ( $remove_lang != '' ) {
339
+ return $remove_lang;
340
+ }
341
+ }
342
+ }
343
+ return $requested_url;
344
+ }
345
+
346
+ }
frontend/class-custom-permalinks-frontend.php CHANGED
@@ -1,445 +1,456 @@
1
- <?php
2
- /**
3
- * @package CustomPermalinks\Frontend
4
- */
5
-
6
- class Custom_Permalinks_Frontend {
7
-
8
- /**
9
- * Initialize WordPress Hooks
10
- */
11
- public function init() {
12
- add_filter( 'request', array( $this, 'custom_permalinks_request' ), 10, 1 );
13
-
14
- add_action( 'template_redirect', array( $this, 'custom_permalinks_redirect' ), 5 );
15
-
16
- add_filter( 'post_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
17
- add_filter( 'post_type_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
18
- add_filter( 'page_link', array( $this, 'custom_permalinks_page_link' ), 10, 2 );
19
-
20
- add_filter( 'tag_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
21
- add_filter( 'category_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
22
-
23
- add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
24
- }
25
-
26
- /**
27
- * Filter to rewrite the query if we have a matching post
28
- */
29
- public function custom_permalinks_request( $query ) {
30
- global $wpdb;
31
- global $_CPRegisteredURL;
32
-
33
- // First, search for a matching custom permalink,
34
- // and if found, generate the corresponding original URL
35
-
36
- $original_url = NULL;
37
-
38
- // Get request URI, strip parameters and /'s
39
- $url = parse_url( get_bloginfo( 'url' ) );
40
- $url = isset( $url['path'] ) ? $url['path'] : '';
41
- $request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
42
- $pos = strpos( $request, '?' );
43
- if ( $pos ) {
44
- $request = substr( $request, 0, $pos );
45
- }
46
-
47
- if ( ! $request ) {
48
- return $query;
49
- }
50
-
51
- $ignore = apply_filters( 'custom_permalinks_request_ignore', $request );
52
-
53
- if ( '__true' === $ignore ) {
54
- return $query;
55
- }
56
-
57
- if ( defined( 'POLYLANG_VERSION' ) ) {
58
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-form.php' );
59
- $custom_permalinks_form = new Custom_Permalinks_Form();
60
- $request = $custom_permalinks_form->custom_permalinks_check_conflicts( $request );
61
- }
62
- $request_noslash = preg_replace( '@/+@','/', trim( $request, '/' ) );
63
-
64
- $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status " .
65
- " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " .
66
- " WHERE pm.meta_key = 'custom_permalink' " .
67
- " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " .
68
- " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " .
69
- " ORDER BY FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
70
- " FIELD(post_type,'post','page') LIMIT 1", $request_noslash, $request_noslash . "/" );
71
-
72
- $posts = $wpdb->get_results( $sql );
73
-
74
- if ( ! $posts ) {
75
- $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status FROM $wpdb->posts AS p " .
76
- " LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE " .
77
- " meta_key = 'custom_permalink' AND meta_value != '' AND " .
78
- " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR " .
79
- " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " .
80
- " AND post_status != 'trash' AND post_type != 'nav_menu_item'" .
81
- " ORDER BY LENGTH(meta_value) DESC, " .
82
- " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
83
- " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1",
84
- $request_noslash, $request_noslash . "/" );
85
-
86
- $posts = $wpdb->get_results( $sql );
87
- }
88
-
89
- if ( $posts ) {
90
- // A post matches our request
91
-
92
- // Preserve this url for later if it's the same as the permalink (no extra stuff)
93
- if ( $request_noslash == trim( $posts[0]->meta_value, '/' ) ) {
94
- $_CPRegisteredURL = $request;
95
- }
96
-
97
- if ( $posts[0]->post_status == 'draft' ) {
98
- if ( $posts[0]->post_type == 'page' ) {
99
- $original_url = '?page_id=' . $posts[0]->ID;
100
- } else {
101
- $original_url = '?post_type=' . $posts[0]->post_type . '&p=' . $posts[0]->ID;
102
- }
103
- } else {
104
- $post_meta = trim( strtolower( $posts[0]->meta_value ), '/' );
105
- if ( $posts[0]->post_type == 'page' ) {
106
- $get_original_url = $this->custom_permalinks_original_page_link( $posts[0]->ID );
107
- $original_url = preg_replace( '@/+@', '/',
108
- str_replace( $post_meta, $get_original_url, strtolower( $request_noslash ) )
109
- );
110
- } else {
111
- $get_original_url = $this->custom_permalinks_original_post_link( $posts[0]->ID );
112
- $original_url = preg_replace( '@/+@', '/',
113
- str_replace( $post_meta, $get_original_url, strtolower( $request_noslash ) )
114
- );
115
- }
116
- }
117
- }
118
-
119
- if ( $original_url === NULL ) {
120
- // See if any terms have a matching permalink
121
- $table = get_option( 'custom_permalink_table' );
122
- if ( ! $table ) {
123
- return $query;
124
- }
125
-
126
- foreach ( array_keys( $table ) as $permalink ) {
127
- if ( $permalink == substr( $request_noslash, 0, strlen( $permalink ) )
128
- || $permalink == substr( $request_noslash . '/', 0, strlen( $permalink ) ) ) {
129
- $term = $table[$permalink];
130
-
131
- // Preserve this url for later if it's the same as the permalink (no extra stuff)
132
- if ( $request_noslash == trim( $permalink, '/' ) ) {
133
- $_CPRegisteredURL = $request;
134
- }
135
-
136
- if ( $term['kind'] == 'category' ) {
137
- $category_link = $this->custom_permalinks_original_category_link( $term['id'] );
138
- $original_url = str_replace( trim( $permalink, '/' ), $category_link, trim( $request, '/' ) );
139
- } else {
140
- $category_link = $this->custom_permalinks_original_tag_link( $term['id'] );
141
- $original_url = str_replace( trim( $permalink, '/' ), $category_link, trim( $request, '/' ) );
142
- }
143
- }
144
- }
145
- }
146
-
147
- if ( $original_url !== NULL ) {
148
- $original_url = str_replace( '//', '/', $original_url );
149
-
150
- if ( ( $pos = strpos( $_SERVER['REQUEST_URI'], '?' ) ) !== false ) {
151
- $query_vars = substr( $_SERVER['REQUEST_URI'], $pos + 1);
152
- $original_url .= ( strpos( $original_url, '?' ) === false ? '?' : '&' ) . $query_vars;
153
- }
154
-
155
- // Now we have the original URL, run this back through WP->parse_request, in order to
156
- // parse parameters properly. We set $_SERVER variables to fool the function.
157
- $old_request_uri = $_SERVER['REQUEST_URI']; $old_query_string = $_SERVER['QUERY_STRING'];
158
- $_SERVER['REQUEST_URI'] = '/' . ltrim( $original_url, '/' );
159
- $_SERVER['QUERY_STRING'] = ( ( $pos = strpos( $original_url, '?' ) ) !== false ? substr( $original_url, $pos + 1 ) : '' );
160
- parse_str( $_SERVER['QUERY_STRING'], $query_array );
161
- $old_values = array();
162
- if ( is_array( $query_array ) ) {
163
- foreach ( $query_array as $key => $value ) {
164
- $old_values[$key] = $_REQUEST[$key];
165
- $_REQUEST[$key] = $_GET[$key] = $value;
166
- }
167
- }
168
-
169
- // Re-run the filter, now with original environment in place
170
- remove_filter( 'request', array( $this, 'custom_permalinks_request' ), 10, 1 );
171
- global $wp;
172
- $wp->parse_request();
173
- $query = $wp->query_vars;
174
- add_filter( 'request', array( $this, 'custom_permalinks_request' ), 10, 1 );
175
-
176
- // Restore values
177
- $_SERVER['REQUEST_URI'] = $old_request_uri; $_SERVER['QUERY_STRING'] = $old_query_string;
178
- foreach ( $old_values as $key => $value ) {
179
- $_REQUEST[$key] = $value;
180
- }
181
- }
182
-
183
- return $query;
184
- }
185
-
186
- /**
187
- * Action to redirect to the custom permalink
188
- */
189
- public function custom_permalinks_redirect() {
190
- global $wpdb;
191
-
192
- $custom_permalink = '';
193
- $original_permalink = '';
194
-
195
- // Get request URI, strip parameters
196
- $url = parse_url( get_bloginfo( 'url' ) );
197
- $url = isset( $url['path'] ) ? $url['path'] : '';
198
- $request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
199
- $pos = strpos( $request, '?' );
200
- if ( $pos ) {
201
- $request = substr( $request, 0, $pos );
202
- }
203
-
204
- if ( defined( 'POLYLANG_VERSION' ) ) {
205
- require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-form.php' );
206
- $custom_permalinks_form = new Custom_Permalinks_Form();
207
- $request = $custom_permalinks_form->custom_permalinks_check_conflicts( $request );
208
- }
209
- $request_noslash = preg_replace( '@/+@','/', trim( $request, '/' ) );
210
-
211
- $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status " .
212
- " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " .
213
- " WHERE pm.meta_key = 'custom_permalink' " .
214
- " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " .
215
- " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " .
216
- " ORDER BY FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
217
- " FIELD(post_type,'post','page') LIMIT 1", $request_noslash, $request_noslash . "/" );
218
- $posts = $wpdb->get_results( $sql );
219
-
220
- if ( ! $posts ) {
221
- $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status FROM $wpdb->posts AS p " .
222
- " LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE " .
223
- " meta_key = 'custom_permalink' AND meta_value != '' AND " .
224
- " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR " .
225
- " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " .
226
- " AND post_status != 'trash' AND post_type != 'nav_menu_item'" .
227
- " ORDER BY LENGTH(meta_value) DESC, " .
228
- " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
229
- " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1",
230
- $request_noslash, $request_noslash . "/" );
231
-
232
- $posts = $wpdb->get_results( $sql );
233
- }
234
-
235
- if ( ! isset( $posts[0]->ID ) || ! isset( $posts[0]->meta_value )
236
- || empty( $posts[0]->meta_value ) ) {
237
- global $wp_query;
238
-
239
- // If the post/tag/category we're on has a custom permalink, get it and check against the request
240
- if ( ( is_single() || is_page() ) && ! empty( $wp_query->post ) ) {
241
- $post = $wp_query->post;
242
- $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
243
- if ( $post->post_type == 'page' ) {
244
- $original_permalink = $this->custom_permalinks_original_page_link( $post->ID );
245
- } else {
246
- $original_permalink = $this->custom_permalinks_original_post_link( $post->ID );
247
- }
248
- } elseif ( is_tag() || is_category() ) {
249
- $theTerm = $wp_query->get_queried_object();
250
- $custom_permalink = $this->custom_permalinks_permalink_for_term( $theTerm->term_id );
251
- if ( is_tag() ) {
252
- $original_permalink = $this->custom_permalinks_original_tag_link( $theTerm->term_id );
253
- } else {
254
- $original_permalink = $this->custom_permalinks_original_category_link( $theTerm->term_id );
255
- }
256
- }
257
- } else {
258
- $custom_permalink = $posts[0]->meta_value;
259
- if ( $posts[0]->post_type == 'page' ) {
260
- $original_permalink = $this->custom_permalinks_original_page_link( $posts[0]->ID );
261
- } else {
262
- $original_permalink = $this->custom_permalinks_original_post_link( $posts[0]->ID );
263
- }
264
- }
265
-
266
- if ( $custom_permalink
267
- && ( substr( $request, 0, strlen( $custom_permalink ) ) != $custom_permalink
268
- || $request == $custom_permalink . '/' ) ) {
269
-
270
- // Request doesn't match permalink - redirect
271
- $url = $custom_permalink;
272
-
273
- if ( substr( $request, 0, strlen( $original_permalink ) ) == $original_permalink
274
- && trim( $request, '/' ) != trim( $original_permalink, '/' ) ) {
275
- // This is the original link; we can use this url to derive the new one
276
- $url = preg_replace( '@//*@', '/', str_replace( trim( $original_permalink, '/' ), trim( $custom_permalink, '/' ), $request ) );
277
- $url = preg_replace( '@([^?]*)&@', '\1?', $url );
278
- }
279
-
280
- // Append any query compenent
281
- $url .= strstr( $_SERVER['REQUEST_URI'], '?' );
282
-
283
- wp_redirect( home_url() . '/' . $url, 301 );
284
- exit();
285
- }
286
- }
287
-
288
- /**
289
- * Filter to replace the post permalink with the custom one
290
- */
291
- public function custom_permalinks_post_link( $permalink, $post ) {
292
- $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
293
- if ( $custom_permalink ) {
294
- $post_type = isset( $post->post_type ) ? $post->post_type : 'post';
295
- $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post->ID, 'element_type' => $post_type ) );
296
- if ( $language_code )
297
- return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code );
298
- else
299
- return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink );
300
- }
301
-
302
- return $permalink;
303
- }
304
-
305
- /**
306
- * Filter to replace the page permalink with the custom one
307
- */
308
- public function custom_permalinks_page_link( $permalink, $page ) {
309
- $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
310
- if ( $custom_permalink ) {
311
- $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $page, 'element_type' => 'page' ) );
312
- if ( $language_code )
313
- return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code );
314
- else
315
- return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink );
316
- }
317
-
318
- return $permalink;
319
- }
320
-
321
- /**
322
- * Filter to replace the term permalink with the custom one
323
- */
324
- public function custom_permalinks_term_link( $permalink, $term ) {
325
- $table = get_option( 'custom_permalink_table' );
326
- if ( is_object( $term ) ) {
327
- $term = $term->term_id;
328
- }
329
-
330
- $custom_permalink = $this->custom_permalinks_permalink_for_term( $term );
331
- if ( $custom_permalink ) {
332
- $taxonomy = get_term( $term );
333
- if ( isset( $taxonomy ) && isset( $taxonomy->term_taxonomy_id ) ) {
334
- $term_type = isset( $taxonomy->taxonomy ) ? $taxonomy->taxonomy : 'category';
335
- $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $taxonomy->term_taxonomy_id, 'element_type' => $term_type ) );
336
- return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code );
337
- } else {
338
- return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink );
339
- }
340
- }
341
-
342
- return $permalink;
343
- }
344
-
345
- /**
346
- * Get original permalink for post
347
- */
348
- public function custom_permalinks_original_post_link( $post_id ) {
349
- remove_filter( 'post_link', array( $this, 'custom_permalinks_post_link' ), 10, 3 );
350
- remove_filter( 'post_type_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
351
-
352
- require_once ABSPATH . '/wp-admin/includes/post.php';
353
- list( $permalink, $post_name ) = get_sample_permalink( $post_id );
354
- $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
355
- $permalink = ltrim( str_replace( home_url(), '', $permalink ), '/' );
356
-
357
- add_filter( 'post_link', array( $this, 'custom_permalinks_post_link' ), 10, 3 );
358
- add_filter( 'post_type_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
359
-
360
- return $permalink;
361
- }
362
-
363
- /**
364
- * Get original permalink for page
365
- */
366
- public function custom_permalinks_original_page_link( $post_id ) {
367
- remove_filter( 'page_link', array( $this, 'custom_permalinks_page_link' ), 10, 2 );
368
- remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
369
-
370
- require_once ABSPATH . '/wp-admin/includes/post.php';
371
- list( $permalink, $post_name ) = get_sample_permalink( $post_id );
372
- $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
373
- $permalink = ltrim( str_replace( home_url(), '', $permalink ), '/' );
374
-
375
- add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
376
- add_filter( 'page_link', array( $this, 'custom_permalinks_page_link' ), 10, 2 );
377
- return $permalink;
378
- }
379
-
380
- /**
381
- * Get original permalink for tag
382
- */
383
- public function custom_permalinks_original_tag_link( $tag_id ) {
384
- remove_filter( 'tag_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
385
- remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
386
- $originalPermalink = ltrim( str_replace( home_url(), '', get_tag_link( $tag_id ) ), '/' );
387
- add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
388
- add_filter( 'tag_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
389
- return $originalPermalink;
390
- }
391
-
392
- /**
393
- * Get original permalink for category
394
- */
395
- public function custom_permalinks_original_category_link( $category_id ) {
396
- remove_filter( 'category_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
397
- remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
398
- $originalPermalink = ltrim( str_replace( home_url(), '', get_category_link( $category_id ) ), '/' );
399
- add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
400
- add_filter( 'category_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
401
- return $originalPermalink;
402
- }
403
-
404
- /**
405
- * Filter to handle trailing slashes correctly
406
- */
407
- public function custom_permalinks_trailingslash( $string, $type ) {
408
- global $_CPRegisteredURL;
409
-
410
- remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
411
- $url = parse_url( get_bloginfo( 'url' ) );
412
- $request = ltrim( isset( $url['path'] ) ? substr( $string, strlen( $url['path'] ) ) : $string, '/' );
413
- add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
414
-
415
- if ( ! trim( $request ) ) {
416
- return $string;
417
- }
418
-
419
- if ( trim( $_CPRegisteredURL, '/' ) == trim( $request, '/' ) ) {
420
- if ( isset( $url['path'] ) ) {
421
- return ( $string{0} == '/' ? '/' : '' ) . trailingslashit( $url['path'] ) . $_CPRegisteredURL;
422
- } else {
423
- return ( $string{0} == '/' ? '/' : '' ) . $_CPRegisteredURL;
424
- }
425
- }
426
- return $string;
427
- }
428
-
429
-
430
- /**
431
- * Get permalink for term
432
- */
433
- public function custom_permalinks_permalink_for_term( $id ) {
434
- $table = get_option( 'custom_permalink_table' );
435
- if ( $table ) {
436
- foreach ( $table as $link => $info ) {
437
- if ( $info['id'] == $id ) {
438
- return $link;
439
- }
440
- }
441
- }
442
- return false;
443
- }
444
-
445
- }
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @package CustomPermalinks\Frontend
4
+ */
5
+
6
+ class Custom_Permalinks_Frontend {
7
+
8
+ /**
9
+ * Initialize WordPress Hooks
10
+ */
11
+ public function init() {
12
+ add_filter( 'request', array( $this, 'custom_permalinks_request' ), 10, 1 );
13
+
14
+ add_action( 'template_redirect', array( $this, 'custom_permalinks_redirect' ), 5 );
15
+
16
+ add_filter( 'post_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
17
+ add_filter( 'post_type_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
18
+ add_filter( 'page_link', array( $this, 'custom_permalinks_page_link' ), 10, 2 );
19
+
20
+ add_filter( 'tag_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
21
+ add_filter( 'category_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
22
+
23
+ add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
24
+ }
25
+
26
+ /**
27
+ * Filter to rewrite the query if we have a matching post
28
+ */
29
+ public function custom_permalinks_request( $query ) {
30
+ global $wpdb;
31
+ global $_CPRegisteredURL;
32
+
33
+ // First, search for a matching custom permalink,
34
+ // and if found, generate the corresponding original URL
35
+
36
+ $original_url = NULL;
37
+
38
+ // Get request URI, strip parameters and /'s
39
+ $url = parse_url( get_bloginfo( 'url' ) );
40
+ $url = isset( $url['path'] ) ? $url['path'] : '';
41
+ $request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
42
+ $pos = strpos( $request, '?' );
43
+ if ( $pos ) {
44
+ $request = substr( $request, 0, $pos );
45
+ }
46
+
47
+ if ( ! $request ) {
48
+ return $query;
49
+ }
50
+
51
+ $ignore = apply_filters( 'custom_permalinks_request_ignore', $request );
52
+ $def_query = apply_filters( 'custom_permalinks_like_query', '__false' );
53
+
54
+ if ( '__true' === $ignore ) {
55
+ return $query;
56
+ }
57
+
58
+ if ( defined( 'POLYLANG_VERSION' ) ) {
59
+ require_once(
60
+ CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-form.php'
61
+ );
62
+ $cp_form = new Custom_Permalinks_Form();
63
+ $request = $cp_form->custom_permalinks_check_conflicts( $request );
64
+ }
65
+ $request_noslash = preg_replace( '@/+@','/', trim( $request, '/' ) );
66
+
67
+ $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status " .
68
+ " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " .
69
+ " WHERE pm.meta_key = 'custom_permalink' " .
70
+ " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " .
71
+ " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " .
72
+ " ORDER BY FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
73
+ " FIELD(post_type,'post','page') LIMIT 1", $request_noslash, $request_noslash . "/" );
74
+
75
+ $posts = $wpdb->get_results( $sql );
76
+
77
+ if ( ! $posts && ( defined( 'POLYLANG_VERSION' )
78
+ || defined( 'AMP__VERSION' ) || '__false' !== $def_query ) ) {
79
+ $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status FROM $wpdb->posts AS p " .
80
+ " LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE " .
81
+ " meta_key = 'custom_permalink' AND meta_value != '' AND " .
82
+ " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR " .
83
+ " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " .
84
+ " AND post_status != 'trash' AND post_type != 'nav_menu_item'" .
85
+ " ORDER BY LENGTH(meta_value) DESC, " .
86
+ " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
87
+ " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1",
88
+ $request_noslash, $request_noslash . "/" );
89
+
90
+ $posts = $wpdb->get_results( $sql );
91
+ }
92
+
93
+ if ( $posts ) {
94
+ // A post matches our request
95
+
96
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
97
+ if ( $request_noslash == trim( $posts[0]->meta_value, '/' ) ) {
98
+ $_CPRegisteredURL = $request;
99
+ }
100
+
101
+ if ( $posts[0]->post_status == 'draft' ) {
102
+ if ( $posts[0]->post_type == 'page' ) {
103
+ $original_url = '?page_id=' . $posts[0]->ID;
104
+ } else {
105
+ $original_url = '?post_type=' . $posts[0]->post_type . '&p=' . $posts[0]->ID;
106
+ }
107
+ } else {
108
+ $post_meta = trim( strtolower( $posts[0]->meta_value ), '/' );
109
+ if ( $posts[0]->post_type == 'page' ) {
110
+ $get_original_url = $this->custom_permalinks_original_page_link( $posts[0]->ID );
111
+ $original_url = preg_replace( '@/+@', '/',
112
+ str_replace( $post_meta, $get_original_url, strtolower( $request_noslash ) )
113
+ );
114
+ } else {
115
+ $get_original_url = $this->custom_permalinks_original_post_link( $posts[0]->ID );
116
+ $original_url = preg_replace( '@/+@', '/',
117
+ str_replace( $post_meta, $get_original_url, strtolower( $request_noslash ) )
118
+ );
119
+ }
120
+ }
121
+ }
122
+
123
+ if ( $original_url === NULL ) {
124
+ // See if any terms have a matching permalink
125
+ $table = get_option( 'custom_permalink_table' );
126
+ if ( ! $table ) {
127
+ return $query;
128
+ }
129
+
130
+ foreach ( array_keys( $table ) as $permalink ) {
131
+ if ( $permalink == substr( $request_noslash, 0, strlen( $permalink ) )
132
+ || $permalink == substr( $request_noslash . '/', 0, strlen( $permalink ) ) ) {
133
+ $term = $table[$permalink];
134
+
135
+ // Preserve this url for later if it's the same as the permalink (no extra stuff)
136
+ if ( $request_noslash == trim( $permalink, '/' ) ) {
137
+ $_CPRegisteredURL = $request;
138
+ }
139
+
140
+ if ( $term['kind'] == 'category' ) {
141
+ $category_link = $this->custom_permalinks_original_category_link( $term['id'] );
142
+ } else {
143
+ $category_link = $this->custom_permalinks_original_tag_link( $term['id'] );
144
+ }
145
+
146
+ $original_url = str_replace(
147
+ trim( $permalink, '/' ), $category_link, trim( $request, '/' )
148
+ );
149
+ }
150
+ }
151
+ }
152
+
153
+ if ( $original_url !== NULL ) {
154
+ $original_url = str_replace( '//', '/', $original_url );
155
+
156
+ if ( ( $pos = strpos( $_SERVER['REQUEST_URI'], '?' ) ) !== false ) {
157
+ $query_vars = substr( $_SERVER['REQUEST_URI'], $pos + 1);
158
+ $original_url .= ( strpos( $original_url, '?' ) === false ? '?' : '&' ) . $query_vars;
159
+ }
160
+
161
+ // Now we have the original URL, run this back through WP->parse_request, in order to
162
+ // parse parameters properly. We set $_SERVER variables to fool the function.
163
+ $old_request_uri = $_SERVER['REQUEST_URI'];
164
+ $old_query_string = '';
165
+ if ( isset( $_SERVER['QUERY_STRING'] ) ) {
166
+ $old_query_string = $_SERVER['QUERY_STRING'];
167
+ }
168
+ $_SERVER['REQUEST_URI'] = '/' . ltrim( $original_url, '/' );
169
+ $_SERVER['QUERY_STRING'] = ( ( $pos = strpos( $original_url, '?' ) ) !== false ? substr( $original_url, $pos + 1 ) : '' );
170
+ parse_str( $_SERVER['QUERY_STRING'], $query_array );
171
+ $old_values = array();
172
+ if ( is_array( $query_array ) ) {
173
+ foreach ( $query_array as $key => $value ) {
174
+ $old_values[$key] = $_REQUEST[$key];
175
+ $_REQUEST[$key] = $_GET[$key] = $value;
176
+ }
177
+ }
178
+
179
+ // Re-run the filter, now with original environment in place
180
+ remove_filter( 'request', array( $this, 'custom_permalinks_request' ), 10, 1 );
181
+ global $wp;
182
+ $wp->parse_request();
183
+ $query = $wp->query_vars;
184
+ add_filter( 'request', array( $this, 'custom_permalinks_request' ), 10, 1 );
185
+
186
+ // Restore values
187
+ $_SERVER['REQUEST_URI'] = $old_request_uri;
188
+ $_SERVER['QUERY_STRING'] = $old_query_string;
189
+ foreach ( $old_values as $key => $value ) {
190
+ $_REQUEST[$key] = $value;
191
+ }
192
+ }
193
+
194
+ return $query;
195
+ }
196
+
197
+ /**
198
+ * Action to redirect to the custom permalink
199
+ */
200
+ public function custom_permalinks_redirect() {
201
+ global $wpdb;
202
+
203
+ $custom_permalink = '';
204
+ $original_permalink = '';
205
+
206
+ // Get request URI, strip parameters
207
+ $url = parse_url( get_bloginfo( 'url' ) );
208
+ $url = isset( $url['path'] ) ? $url['path'] : '';
209
+ $request = ltrim( substr( $_SERVER['REQUEST_URI'], strlen( $url ) ), '/' );
210
+ $pos = strpos( $request, '?' );
211
+ if ( $pos ) {
212
+ $request = substr( $request, 0, $pos );
213
+ }
214
+
215
+ if ( defined( 'POLYLANG_VERSION' ) ) {
216
+ require_once( CUSTOM_PERMALINKS_PATH . 'frontend/class-custom-permalinks-form.php' );
217
+ $cp_form = new Custom_Permalinks_Form();
218
+ $request = $cp_form->custom_permalinks_check_conflicts( $request );
219
+ }
220
+ $request_noslash = preg_replace( '@/+@','/', trim( $request, '/' ) );
221
+
222
+ $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status " .
223
+ " FROM $wpdb->posts AS p INNER JOIN $wpdb->postmeta AS pm ON (pm.post_id = p.ID) " .
224
+ " WHERE pm.meta_key = 'custom_permalink' " .
225
+ " AND (pm.meta_value = '%s' OR pm.meta_value = '%s') " .
226
+ " AND p.post_status != 'trash' AND p.post_type != 'nav_menu_item' " .
227
+ " ORDER BY FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
228
+ " FIELD(post_type,'post','page') LIMIT 1", $request_noslash, $request_noslash . "/" );
229
+ $posts = $wpdb->get_results( $sql );
230
+
231
+ if ( ! $posts ) {
232
+ $sql = $wpdb->prepare( "SELECT p.ID, pm.meta_value, p.post_type, p.post_status FROM $wpdb->posts AS p " .
233
+ " LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE " .
234
+ " meta_key = 'custom_permalink' AND meta_value != '' AND " .
235
+ " ( LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) OR " .
236
+ " LOWER(meta_value) = LEFT(LOWER('%s'), LENGTH(meta_value)) ) " .
237
+ " AND post_status != 'trash' AND post_type != 'nav_menu_item'" .
238
+ " ORDER BY LENGTH(meta_value) DESC, " .
239
+ " FIELD(post_status,'publish','private','draft','auto-draft','inherit')," .
240
+ " FIELD(post_type,'post','page'), p.ID ASC LIMIT 1",
241
+ $request_noslash, $request_noslash . "/" );
242
+
243
+ $posts = $wpdb->get_results( $sql );
244
+ }
245
+
246
+ if ( ! isset( $posts[0]->ID ) || ! isset( $posts[0]->meta_value )
247
+ || empty( $posts[0]->meta_value ) ) {
248
+ global $wp_query;
249
+
250
+ // If the post/tag/category we're on has a custom permalink, get it and check against the request
251
+ if ( ( is_single() || is_page() ) && ! empty( $wp_query->post ) ) {
252
+ $post = $wp_query->post;
253
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
254
+ if ( $post->post_type == 'page' ) {
255
+ $original_permalink = $this->custom_permalinks_original_page_link( $post->ID );
256
+ } else {
257
+ $original_permalink = $this->custom_permalinks_original_post_link( $post->ID );
258
+ }
259
+ } elseif ( is_tag() || is_category() ) {
260
+ $theTerm = $wp_query->get_queried_object();
261
+ $custom_permalink = $this->custom_permalinks_permalink_for_term( $theTerm->term_id );
262
+ if ( is_tag() ) {
263
+ $original_permalink = $this->custom_permalinks_original_tag_link( $theTerm->term_id );
264
+ } else {
265
+ $original_permalink = $this->custom_permalinks_original_category_link( $theTerm->term_id );
266
+ }
267
+ }
268
+ } else {
269
+ $custom_permalink = $posts[0]->meta_value;
270
+ if ( $posts[0]->post_type == 'page' ) {
271
+ $original_permalink = $this->custom_permalinks_original_page_link( $posts[0]->ID );
272
+ } else {
273
+ $original_permalink = $this->custom_permalinks_original_post_link( $posts[0]->ID );
274
+ }
275
+ }
276
+
277
+ if ( $custom_permalink
278
+ && ( substr( $request, 0, strlen( $custom_permalink ) ) != $custom_permalink
279
+ || $request == $custom_permalink . '/' ) ) {
280
+
281
+ // Request doesn't match permalink - redirect
282
+ $url = $custom_permalink;
283
+
284
+ if ( substr( $request, 0, strlen( $original_permalink ) ) == $original_permalink
285
+ && trim( $request, '/' ) != trim( $original_permalink, '/' ) ) {
286
+ // This is the original link; we can use this url to derive the new one
287
+ $url = preg_replace( '@//*@', '/', str_replace( trim( $original_permalink, '/' ), trim( $custom_permalink, '/' ), $request ) );
288
+ $url = preg_replace( '@([^?]*)&@', '\1?', $url );
289
+ }
290
+
291
+ // Append any query compenent
292
+ $url .= strstr( $_SERVER['REQUEST_URI'], '?' );
293
+
294
+ wp_redirect( home_url() . '/' . $url, 301 );
295
+ exit();
296
+ }
297
+ }
298
+
299
+ /**
300
+ * Filter to replace the post permalink with the custom one
301
+ */
302
+ public function custom_permalinks_post_link( $permalink, $post ) {
303
+ $custom_permalink = get_post_meta( $post->ID, 'custom_permalink', true );
304
+ if ( $custom_permalink ) {
305
+ $post_type = isset( $post->post_type ) ? $post->post_type : 'post';
306
+ $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $post->ID, 'element_type' => $post_type ) );
307
+ if ( $language_code )
308
+ return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code );
309
+ else
310
+ return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink );
311
+ }
312
+
313
+ return $permalink;
314
+ }
315
+
316
+ /**
317
+ * Filter to replace the page permalink with the custom one
318
+ */
319
+ public function custom_permalinks_page_link( $permalink, $page ) {
320
+ $custom_permalink = get_post_meta( $page, 'custom_permalink', true );
321
+ if ( $custom_permalink ) {
322
+ $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $page, 'element_type' => 'page' ) );
323
+ if ( $language_code )
324
+ return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code );
325
+ else
326
+ return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink );
327
+ }
328
+
329
+ return $permalink;
330
+ }
331
+
332
+ /**
333
+ * Filter to replace the term permalink with the custom one
334
+ */
335
+ public function custom_permalinks_term_link( $permalink, $term ) {
336
+ $table = get_option( 'custom_permalink_table' );
337
+ if ( is_object( $term ) ) {
338
+ $term = $term->term_id;
339
+ }
340
+
341
+ $custom_permalink = $this->custom_permalinks_permalink_for_term( $term );
342
+ if ( $custom_permalink ) {
343
+ $taxonomy = get_term( $term );
344
+ if ( isset( $taxonomy ) && isset( $taxonomy->term_taxonomy_id ) ) {
345
+ $term_type = isset( $taxonomy->taxonomy ) ? $taxonomy->taxonomy : 'category';
346
+ $language_code = apply_filters( 'wpml_element_language_code', null, array( 'element_id' => $taxonomy->term_taxonomy_id, 'element_type' => $term_type ) );
347
+ return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink, $language_code );
348
+ } else {
349
+ return apply_filters( 'wpml_permalink', home_url() . '/' . $custom_permalink );
350
+ }
351
+ }
352
+
353
+ return $permalink;
354
+ }
355
+
356
+ /**
357
+ * Get original permalink for post
358
+ */
359
+ public function custom_permalinks_original_post_link( $post_id ) {
360
+ remove_filter( 'post_link', array( $this, 'custom_permalinks_post_link' ), 10, 3 );
361
+ remove_filter( 'post_type_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
362
+
363
+ require_once ABSPATH . '/wp-admin/includes/post.php';
364
+ list( $permalink, $post_name ) = get_sample_permalink( $post_id );
365
+ $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
366
+ $permalink = ltrim( str_replace( home_url(), '', $permalink ), '/' );
367
+
368
+ add_filter( 'post_link', array( $this, 'custom_permalinks_post_link' ), 10, 3 );
369
+ add_filter( 'post_type_link', array( $this, 'custom_permalinks_post_link' ), 10, 2 );
370
+
371
+ return $permalink;
372
+ }
373
+
374
+ /**
375
+ * Get original permalink for page
376
+ */
377
+ public function custom_permalinks_original_page_link( $post_id ) {
378
+ remove_filter( 'page_link', array( $this, 'custom_permalinks_page_link' ), 10, 2 );
379
+ remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
380
+
381
+ require_once ABSPATH . '/wp-admin/includes/post.php';
382
+ list( $permalink, $post_name ) = get_sample_permalink( $post_id );
383
+ $permalink = str_replace( array( '%pagename%','%postname%' ), $post_name, $permalink );
384
+ $permalink = ltrim( str_replace( home_url(), '', $permalink ), '/' );
385
+
386
+ add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
387
+ add_filter( 'page_link', array( $this, 'custom_permalinks_page_link' ), 10, 2 );
388
+ return $permalink;
389
+ }
390
+
391
+ /**
392
+ * Get original permalink for tag
393
+ */
394
+ public function custom_permalinks_original_tag_link( $tag_id ) {
395
+ remove_filter( 'tag_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
396
+ remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
397
+ $originalPermalink = ltrim( str_replace( home_url(), '', get_tag_link( $tag_id ) ), '/' );
398
+ add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
399
+ add_filter( 'tag_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
400
+ return $originalPermalink;
401
+ }
402
+
403
+ /**
404
+ * Get original permalink for category
405
+ */
406
+ public function custom_permalinks_original_category_link( $category_id ) {
407
+ remove_filter( 'category_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
408
+ remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
409
+ $originalPermalink = ltrim( str_replace( home_url(), '', get_category_link( $category_id ) ), '/' );
410
+ add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
411
+ add_filter( 'category_link', array( $this, 'custom_permalinks_term_link' ), 10, 2 );
412
+ return $originalPermalink;
413
+ }
414
+
415
+ /**
416
+ * Filter to handle trailing slashes correctly
417
+ */
418
+ public function custom_permalinks_trailingslash( $string, $type ) {
419
+ global $_CPRegisteredURL;
420
+
421
+ remove_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
422
+ $url = parse_url( get_bloginfo( 'url' ) );
423
+ $request = ltrim( isset( $url['path'] ) ? substr( $string, strlen( $url['path'] ) ) : $string, '/' );
424
+ add_filter( 'user_trailingslashit', array( $this, 'custom_permalinks_trailingslash' ), 10, 2 );
425
+
426
+ if ( ! trim( $request ) ) {
427
+ return $string;
428
+ }
429
+
430
+ if ( trim( $_CPRegisteredURL, '/' ) == trim( $request, '/' ) ) {
431
+ if ( isset( $url['path'] ) ) {
432
+ return ( $string{0} == '/' ? '/' : '' ) . trailingslashit( $url['path'] ) . $_CPRegisteredURL;
433
+ } else {
434
+ return ( $string{0} == '/' ? '/' : '' ) . $_CPRegisteredURL;
435
+ }
436
+ }
437
+ return $string;
438
+ }
439
+
440
+
441
+ /**
442
+ * Get permalink for term
443
+ */
444
+ public function custom_permalinks_permalink_for_term( $id ) {
445
+ $table = get_option( 'custom_permalink_table' );
446
+ if ( $table ) {
447
+ foreach ( $table as $link => $info ) {
448
+ if ( $info['id'] == $id ) {
449
+ return $link;
450
+ }
451
+ }
452
+ }
453
+ return false;
454
+ }
455
+
456
+ }
frontend/js/script-form.js CHANGED
@@ -1,51 +1,51 @@
1
- var getHomeURL = document.getElementById( 'custom_permalinks_home_url' ),
2
- getPermalink = document.getElementById( 'custom_permalink' ),
3
- checkYoastSEO = document.getElementById( 'wpseo_meta' );
4
-
5
- function changeSEOLinkOnBlur () {
6
- var snippetCiteBase = document.getElementById( 'snippet_citeBase' );
7
- if ( snippetCiteBase && getHomeURL && getHomeURL.value != "" && getPermalink && getPermalink.value ) {
8
- var i = 0;
9
- var urlChanged = setInterval( function() {
10
- i++;
11
- snippetCiteBase.innerHTML = getHomeURL.value + '/' + getPermalink.value;
12
- if (i === 5) {
13
- clearInterval(urlChanged);
14
- }
15
- }, 1000);
16
- }
17
- }
18
-
19
- function changeSEOLink () {
20
- var snippetCiteBase = document.getElementById( 'snippet_citeBase' );
21
- if ( snippetCiteBase && getHomeURL && getHomeURL.value != "" && getPermalink && getPermalink.value ) {
22
- var i = 0;
23
- var urlChanged = setInterval( function() {
24
- i++;
25
- snippetCiteBase.innerHTML = getHomeURL.value + '/' + getPermalink.value;
26
- if (i === 5) {
27
- clearInterval(urlChanged);
28
- }
29
- }, 1000);
30
- var snippetEditorTitle = document.getElementById( 'snippet-editor-title' ),
31
- snippetEditorSlug = document.getElementById( 'snippet-editor-slug' ),
32
- snippetEditorDesc = document.getElementById( 'snippet-editor-meta-description' ),
33
- snippetCite = document.getElementById( 'snippet_cite' );
34
- if ( snippetEditorTitle ) {
35
- snippetEditorTitle.addEventListener("blur", changeSEOLinkOnBlur, false);
36
- }
37
- if ( snippetEditorSlug ) {
38
- snippetEditorSlug.addEventListener("blur", changeSEOLinkOnBlur, false);
39
- }
40
- if ( snippetEditorDesc ) {
41
- snippetEditorDesc.addEventListener("blur", changeSEOLinkOnBlur, false);
42
- }
43
- if ( snippetCite ) {
44
- snippetCite.style.display = 'none';
45
- }
46
- }
47
- }
48
-
49
- if ( checkYoastSEO ) {
50
- window.addEventListener("load", changeSEOLink, false);
51
  }
1
+ var getHomeURL = document.getElementById( 'custom_permalinks_home_url' ),
2
+ getPermalink = document.getElementById( 'custom_permalink' ),
3
+ checkYoastSEO = document.getElementById( 'wpseo_meta' );
4
+
5
+ function changeSEOLinkOnBlur () {
6
+ var snippetCiteBase = document.getElementById( 'snippet_citeBase' );
7
+ if ( snippetCiteBase && getHomeURL && getHomeURL.value != "" && getPermalink && getPermalink.value ) {
8
+ var i = 0;
9
+ var urlChanged = setInterval( function() {
10
+ i++;
11
+ snippetCiteBase.innerHTML = getHomeURL.value + '/' + getPermalink.value;
12
+ if (i === 5) {
13
+ clearInterval(urlChanged);
14
+ }
15
+ }, 1000);
16
+ }
17
+ }
18
+
19
+ function changeSEOLink () {
20
+ var snippetCiteBase = document.getElementById( 'snippet_citeBase' );
21
+ if ( snippetCiteBase && getHomeURL && getHomeURL.value != "" && getPermalink && getPermalink.value ) {
22
+ var i = 0;
23
+ var urlChanged = setInterval( function() {
24
+ i++;
25
+ snippetCiteBase.innerHTML = getHomeURL.value + '/' + getPermalink.value;
26
+ if (i === 5) {
27
+ clearInterval(urlChanged);
28
+ }
29
+ }, 1000);
30
+ var snippetEditorTitle = document.getElementById( 'snippet-editor-title' ),
31
+ snippetEditorSlug = document.getElementById( 'snippet-editor-slug' ),
32
+ snippetEditorDesc = document.getElementById( 'snippet-editor-meta-description' ),
33
+ snippetCite = document.getElementById( 'snippet_cite' );
34
+ if ( snippetEditorTitle ) {
35
+ snippetEditorTitle.addEventListener("blur", changeSEOLinkOnBlur, false);
36
+ }
37
+ if ( snippetEditorSlug ) {
38
+ snippetEditorSlug.addEventListener("blur", changeSEOLinkOnBlur, false);
39
+ }
40
+ if ( snippetEditorDesc ) {
41
+ snippetEditorDesc.addEventListener("blur", changeSEOLinkOnBlur, false);
42
+ }
43
+ if ( snippetCite ) {
44
+ snippetCite.style.display = 'none';
45
+ }
46
+ }
47
+ }
48
+
49
+ if ( checkYoastSEO ) {
50
+ window.addEventListener("load", changeSEOLink, false);
51
  }
readme.txt CHANGED
@@ -1,360 +1,377 @@
1
- === Custom Permalinks ===
2
-
3
- Contributors: sasiddiqui, michaeltyson
4
- 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.9
8
- Stable tag: 1.2.8
9
- License: GPLv2 or later
10
- License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
-
12
- Set custom permalinks on a per-post, per-tag or per-category basis.
13
-
14
- == Description ==
15
-
16
- Lay out your site the way *you* want it. Set the URL of any post, page, tag or category to anything you want.
17
- Old permalinks will redirect properly to the new address. Custom Permalinks gives you ultimate control
18
- over your site structure.
19
-
20
- Be warned: *This plugin is not a replacement for WordPress's built-in permalink system*. Check your WordPress
21
- administration's "Permalinks" settings page first, to make sure that this doesn't already meet your needs.
22
-
23
- This plugin is only useful for assigning custom permalinks for *individual* posts, pages, tags or categories.
24
- It will not apply whole permalink structures, or automatically apply a category's custom permalink to the posts
25
- within that category.
26
-
27
- > If anyone wants the different Structure Tags for their Post types or use symbols in the URLs So, use the [Permalinks Customizer](https://wordpress.org/plugins/permalinks-customizer/) which is a fork of this plugin and contains the enhancement of this plugin.
28
-
29
- == Filters ==
30
-
31
- If you want to exclude some Permalink to processed with the plugin so, just add the filter looks like this:
32
- `
33
- function check_xml_sitemap_url( $permalink ) {
34
- if ( false !== strpos( $permalink, 'sitemap.xml' )) {
35
- return '__true';
36
- }
37
- return;
38
- }
39
- add_filter( 'custom_permalinks_request_ignore', 'check_xml_sitemap_url' );
40
- `
41
-
42
- If you want to exclude permalink from any post type so, use `custom_permalinks_exclude_post_type` filter.
43
-
44
- custom_permalinks_exclude_post_type filter looks like this:
45
- `
46
- function yasglobal_exclude_post_types( $post_type ) {
47
- if ( $post_type == 'custompost' ) {
48
- return '__true';
49
- }
50
- return '__false';
51
- }
52
- add_filter( 'custom_permalinks_exclude_post_type', 'yasglobal_exclude_post_types');
53
- `
54
- Note: custom_permalinks_exclude_post_type doesn't work on the posts permalink which has been created previously.
55
-
56
- == Thanks for the Support! ==
57
-
58
- The support from the users that love Custom Permalinks is huge. You can support Custom Permalinks's future development and help to make it even better by donating or even giving a 5 star rating with a nice message to me :)
59
-
60
- [Donate to Custom Permalinks](https://www.paypal.me/yasglobal)
61
-
62
- == Installation ==
63
-
64
- 1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory
65
- 2. Activate the plugin through the 'Plugins' menu in WordPress
66
- 3. Edit any post, page, tag or category to set a custom permalink.
67
-
68
- == Changelog ==
69
-
70
- = 1.2.8 =
71
-
72
- * Add Order by in request query
73
-
74
- = 1.2.7 =
75
-
76
- * Fixed Parse Error
77
-
78
- = 1.2.6 =
79
-
80
- * Enhancements
81
- * Added Filter to Exclude Post types
82
- * Bugs
83
- * Fixed Query Issue on parse_request
84
- * Resolving Issues with Cornerstone
85
-
86
- = 1.2.5 =
87
-
88
- * Fixed Category/Tag Update Issue + Typo on Admin Page
89
-
90
- = 1.2.4 =
91
-
92
- * Fixed Slug issue with Yoast SEO
93
-
94
- = 1.2.3 =
95
-
96
- * Fixed PHP Notice on Edit Post Page
97
-
98
- = 1.2.2 =
99
-
100
- * Fixed Typo of Class Object for term on Admin Page
101
-
102
- = 1.2.1 =
103
- * Fixed Class Typo
104
-
105
- = 1.2 =
106
-
107
- * Enhancements
108
- * Added Filter to Exclude/Ignore URL to be processed
109
- * Added Translation Capability
110
- * Split the Code using OOPS Concept to improve performance and applied the filters according to the need
111
- * Removed some unnecessary filters
112
- * Bugs
113
- * Fixed Vulnerability Issues
114
-
115
- = 1.1 =
116
-
117
- * Enhancements
118
- * Added PostTypes Permalinks Page
119
- * View all the PostTypes permalinks
120
- * Search Permalinks
121
- * Sort by Title
122
- * Pagination
123
- * Added Categories Permalinks Page
124
- * View all the Category/Tags permalinks
125
- * Search Permalinks
126
- * Pagination
127
-
128
- * Bug Fixes
129
- * 404 Issues
130
- * Child pages bug
131
-
132
- = 1.0.2 =
133
-
134
- * Fixed Notice and some URL Issues
135
-
136
- = 1.0.1 =
137
-
138
- * Fixed issue with AMP Pages
139
-
140
- = 1.0 =
141
-
142
- * Updated Query on the `custom_permalinks_request` Function
143
-
144
- = 0.9.3 =
145
-
146
- * Fixed PolyLang Conflicts
147
-
148
- = 0.9.2 =
149
-
150
- * Fixed WPML Conflicts
151
-
152
- = 0.9.1 =
153
-
154
- * Fixed issues of Filters and Actions (Replaces 'edit_files' with 10)
155
-
156
- = 0.9 =
157
-
158
- * Resolved the conflict with PolyLang Plugin
159
-
160
- = 0.8 =
161
-
162
- * Fixed (Draft preview issue for custom post types + some PHP Warnings)
163
-
164
- = 0.7.28 =
165
-
166
- * Fixed draft preview issue(posts + pages)
167
-
168
- = 0.7.27 =
169
-
170
- * Fixed Loop Redirecting issue
171
-
172
- = 0.7.26 =
173
-
174
- * Fixed PHP Notice issue
175
-
176
- = 0.7.25 =
177
-
178
- * Fixed draft preview issue
179
-
180
- = 0.7.24 =
181
-
182
- * Fixed a problem with page URLs
183
-
184
- = 0.7.23 =
185
-
186
- * Fixed a problem with permalinks with "/" components
187
-
188
- = 0.7.22 =
189
-
190
- * Fixed PHP warning
191
- * Fixed initial permalink display for new posts/pages
192
-
193
- = 0.7.21 =
194
-
195
- * Minor internationalization fixes
196
-
197
- = 0.7.20 =
198
-
199
- * Addressed a noisy warning
200
- * Revised addition of admin forms js (don't use is_admin())
201
- * Updated Roles and Capabilities from depreciated numerical to label capabilities (by OF-6)
202
- * Added css/html to match WP 3.5+ layout (by OF-6)
203
-
204
- = 0.7.19 =
205
-
206
- * WP 3.9 compatibility fix, thanks to Sowmedia
207
-
208
- = 0.7.18 =
209
-
210
- * Patch to address 404 errors when displaying a page/post that shares a permalink with a trashed page/post, thanks to Tor Johnson
211
-
212
- = 0.7.17 =
213
-
214
- * Patch to address SSL problems, thanks to Amin Mirzaee
215
-
216
- = 0.7.16 =
217
-
218
- * Security and compatibility fixes by Hans-Michael Varbaek of Sense of Security
219
-
220
- = 0.7.15 =
221
-
222
- * Permalinks are now case-insensitive (thanks to @ericmann)
223
-
224
- = 0.7.14 =
225
-
226
- * Delete permalinks upon page/post deletion
227
-
228
- = 0.7.13 =
229
-
230
- * Fixed issue with term permalinks not working properly on some installations
231
-
232
- = 0.7.12 =
233
-
234
- * Fixed issue with feed URLs in non-webroot blog installations
235
-
236
- = 0.7.11 =
237
-
238
- * Fixed issue with pending/draft posts with permalinks
239
- * Fixed infinite redirect issue with permalinks without trailing slash, on blogs not hosted in the webroot
240
-
241
- = 0.7.10 =
242
-
243
- * Fix for 404 error on static front page with custom permalink set, by Eric TF Bat
244
-
245
- = 0.7.9 =
246
-
247
- * Support for custom post types, by Balázs Németh
248
-
249
- = 0.7.8 =
250
-
251
- * Support for non-ASCII characters in URLs
252
- * Fixed bug where adding a new tag when saving a post with a custom permalink attaches that permalink to the new tag
253
- * Some compatibility fixes for WP 3.2.1
254
-
255
- = 0.7.7 =
256
-
257
- * Minor change to permalink saving routine to fix some possible issues
258
- * Fixed issue with %-encoded permalinks
259
-
260
- = 0.7.6 =
261
-
262
- * Fixed permalink saving issue when not using ".../%postname%" or similar permalink structure
263
-
264
- = 0.7.5 =
265
-
266
- * Fixed issue where changes to trailing "/" aren't saved
267
-
268
- = 0.7.4 =
269
-
270
- * Added support for changing post/page slug only
271
- * Fixed incorrect admin edit link
272
-
273
- = 0.7.3 =
274
-
275
- * Fix problem with /page/# URLs on WP 3.1.3
276
-
277
- = 0.7.2 =
278
-
279
- * Don't clobber query parameters when redirecting to the custom permalink from the original URL
280
-
281
- = 0.7.1 =
282
-
283
- * Compatiblity fix for last update
284
-
285
- = 0.7 =
286
-
287
- * Added support for SSL sites, thanks to Dan from todaywasawesome.com
288
-
289
- = 0.6.1 =
290
-
291
- * Fix bug causing incorrect link from "View Post"/"View Page" button in post/page editor
292
-
293
- = 0.6 =
294
-
295
- * Fix infinite redirect for permalinks ending in a / (critical fix)
296
- * Moved post/page permalinks settings to top of edit form, replacing prior permalink display
297
-
298
- = 0.5.3 =
299
-
300
- * Fix for invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html) when using permalinks without a trailing slash (like .../%postname%.html)
301
-
302
- = 0.5.2 =
303
-
304
- * Bugfix for matching posts when there are multiple posts that match parts of the query
305
-
306
- = 0.5.1 =
307
-
308
- * Compatibility fix for WP 2.7's tag/category pages
309
-
310
- = 0.5 =
311
-
312
- * Support for Wordpress sites in subdirectories (i.e., not located at the webroot)
313
-
314
- = 0.4.1 =
315
-
316
- * WP 2.7 compatability fixes; fix for bug encountered when publishing a draft, or reverting to draft status, and fix for placeholder permalink value for pages
317
-
318
- = 0.4 =
319
-
320
- * Support for pages, and a fix for draft posts/pages
321
-
322
- = 0.3.1 =
323
-
324
- * Discovered a typo that broke categories
325
-
326
- = 0.3 =
327
-
328
- * Largely rewritten to provide more robust handling of trailing slashes, proper support for trailing URL components (eg. paging)
329
-
330
- = 0.2.2 =
331
-
332
- * Fixed bug with not matching permalinks when / appended to the URL, and workaround for infinite redirect when another plugin is enforcing trailing /
333
-
334
- = 0.2.1 =
335
-
336
- * Better handling of trailing slashes
337
-
338
- = 0.2 =
339
-
340
- * Added 'Custom Permalinks' section under 'Manage' to show existing custom permalinks, and allow reverting to the defaults
341
-
342
- = 0.1.1 =
343
-
344
- * Fixed bug with categories
345
-
346
- == Upgrade Notice ==
347
-
348
- = 0.6.1 =
349
-
350
- * This release fixes a bug causing incorrect link from the "View Post"/"View Page" button in the editor
351
-
352
- = 0.6 =
353
-
354
- In the process of fixing one issue, version 0.5.3 broke permalinks ending with a "/". Update now to fix this, and sorry for the inconvenience!
355
-
356
- = 0.5.3 =
357
-
358
- If you are having problems with Custom Permalinks causing an invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html),
359
- upgrade: This has now been fixed.
360
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Custom Permalinks ===
2
+
3
+ Contributors: sasiddiqui, michaeltyson
4
+ 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.9
8
+ Stable tag: 1.2.9
9
+ License: GPLv2 or later
10
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+
12
+ Set custom permalinks on a per-post, per-tag or per-category basis.
13
+
14
+ == Description ==
15
+
16
+ Lay out your site the way *you* want it. Set the URL of any post, page, tag or category to anything you want.
17
+ Old permalinks will redirect properly to the new address. Custom Permalinks gives you ultimate control
18
+ over your site structure.
19
+
20
+ Be warned: *This plugin is not a replacement for WordPress's built-in permalink system*. Check your WordPress
21
+ administration's "Permalinks" settings page first, to make sure that this doesn't already meet your needs.
22
+
23
+ This plugin is only useful for assigning custom permalinks for *individual* posts, pages, tags or categories.
24
+ It will not apply whole permalink structures, or automatically apply a category's custom permalink to the posts
25
+ within that category.
26
+
27
+ > If anyone wants the different Structure Tags for their Post types or use symbols in the URLs So, use the [Permalinks Customizer](https://wordpress.org/plugins/permalinks-customizer/) which is a fork of this plugin and contains the enhancement of this plugin.
28
+
29
+ == Filters ==
30
+
31
+ Plugin provides some filter which maybe used according to your needs.
32
+
33
+ To exclude any Permalink to processed with the plugin so, just add the filter looks like this:
34
+ `
35
+ function check_xml_sitemap_url( $permalink ) {
36
+ if ( false !== strpos( $permalink, 'sitemap.xml' )) {
37
+ return '__true';
38
+ }
39
+ return;
40
+ }
41
+ add_filter( 'custom_permalinks_request_ignore', 'check_xml_sitemap_url' );
42
+ `
43
+
44
+ To exclude permalink from any post type so, just add the filter looks like this:
45
+ `
46
+ function yasglobal_exclude_post_types( $post_type ) {
47
+ if ( $post_type == 'custompost' ) {
48
+ return '__true';
49
+ }
50
+ return '__false';
51
+ }
52
+ add_filter( 'custom_permalinks_exclude_post_type', 'yasglobal_exclude_post_types');
53
+ `
54
+
55
+ Note: custom_permalinks_exclude_post_type doesn't work on the posts permalink which has been created previously.
56
+
57
+ To make the like query works as it was before so, just add this line in your theme's functions.php:
58
+ `
59
+ add_filter( 'custom_permalinks_like_query', '__return_true');
60
+ `
61
+
62
+ Note: Use `custom_permalinks_like_query` filter if the URLs doesn't works for you after upgrading to v1.2.9
63
+
64
+ == Thanks for the Support! ==
65
+
66
+ The support from the users that love Custom Permalinks is huge. You can support Custom Permalinks's future development and help to make it even better by donating or even giving a 5 star rating with a nice message to me :)
67
+
68
+ [Donate to Custom Permalinks](https://www.paypal.me/yasglobal)
69
+
70
+ == Installation ==
71
+
72
+ 1. Unzip the package, and upload `custom-permalinks` to the `/wp-content/plugins/` directory
73
+ 2. Activate the plugin through the 'Plugins' menu in WordPress
74
+ 3. Edit any post, page, tag or category to set a custom permalink.
75
+
76
+ == Changelog ==
77
+
78
+ = 1.2.9 =
79
+
80
+ * Enhancements
81
+ * Added Filter to enable the like query
82
+ * Bugs
83
+ * PHP error displayed on all pages using custom permalinks
84
+ * Removed LIKE Query in default. It only works if the site uses PolyLang,
85
+ AMP Plugins or separately enabled using the provided filter.
86
+
87
+ = 1.2.8 =
88
+
89
+ * Add Order by in request query
90
+
91
+ = 1.2.7 =
92
+
93
+ * Fixed Parse Error
94
+
95
+ = 1.2.6 =
96
+
97
+ * Enhancements
98
+ * Added Filter to Exclude Post types
99
+ * Bugs
100
+ * Fixed Query Issue on parse_request
101
+ * Resolving Issues with Cornerstone
102
+
103
+ = 1.2.5 =
104
+
105
+ * Fixed Category/Tag Update Issue + Typo on Admin Page
106
+
107
+ = 1.2.4 =
108
+
109
+ * Fixed Slug issue with Yoast SEO
110
+
111
+ = 1.2.3 =
112
+
113
+ * Fixed PHP Notice on Edit Post Page
114
+
115
+ = 1.2.2 =
116
+
117
+ * Fixed Typo of Class Object for term on Admin Page
118
+
119
+ = 1.2.1 =
120
+ * Fixed Class Typo
121
+
122
+ = 1.2 =
123
+
124
+ * Enhancements
125
+ * Added Filter to Exclude/Ignore URL to be processed
126
+ * Added Translation Capability
127
+ * Split the Code using OOPS Concept to improve performance and applied the filters according to the need
128
+ * Removed some unnecessary filters
129
+ * Bugs
130
+ * Fixed Vulnerability Issues
131
+
132
+ = 1.1 =
133
+
134
+ * Enhancements
135
+ * Added PostTypes Permalinks Page
136
+ * View all the PostTypes permalinks
137
+ * Search Permalinks
138
+ * Sort by Title
139
+ * Pagination
140
+ * Added Categories Permalinks Page
141
+ * View all the Category/Tags permalinks
142
+ * Search Permalinks
143
+ * Pagination
144
+
145
+ * Bug Fixes
146
+ * 404 Issues
147
+ * Child pages bug
148
+
149
+ = 1.0.2 =
150
+
151
+ * Fixed Notice and some URL Issues
152
+
153
+ = 1.0.1 =
154
+
155
+ * Fixed issue with AMP Pages
156
+
157
+ = 1.0 =
158
+
159
+ * Updated Query on the `custom_permalinks_request` Function
160
+
161
+ = 0.9.3 =
162
+
163
+ * Fixed PolyLang Conflicts
164
+
165
+ = 0.9.2 =
166
+
167
+ * Fixed WPML Conflicts
168
+
169
+ = 0.9.1 =
170
+
171
+ * Fixed issues of Filters and Actions (Replaces 'edit_files' with 10)
172
+
173
+ = 0.9 =
174
+
175
+ * Resolved the conflict with PolyLang Plugin
176
+
177
+ = 0.8 =
178
+
179
+ * Fixed (Draft preview issue for custom post types + some PHP Warnings)
180
+
181
+ = 0.7.28 =
182
+
183
+ * Fixed draft preview issue(posts + pages)
184
+
185
+ = 0.7.27 =
186
+
187
+ * Fixed Loop Redirecting issue
188
+
189
+ = 0.7.26 =
190
+
191
+ * Fixed PHP Notice issue
192
+
193
+ = 0.7.25 =
194
+
195
+ * Fixed draft preview issue
196
+
197
+ = 0.7.24 =
198
+
199
+ * Fixed a problem with page URLs
200
+
201
+ = 0.7.23 =
202
+
203
+ * Fixed a problem with permalinks with "/" components
204
+
205
+ = 0.7.22 =
206
+
207
+ * Fixed PHP warning
208
+ * Fixed initial permalink display for new posts/pages
209
+
210
+ = 0.7.21 =
211
+
212
+ * Minor internationalization fixes
213
+
214
+ = 0.7.20 =
215
+
216
+ * Addressed a noisy warning
217
+ * Revised addition of admin forms js (don't use is_admin())
218
+ * Updated Roles and Capabilities from depreciated numerical to label capabilities (by OF-6)
219
+ * Added css/html to match WP 3.5+ layout (by OF-6)
220
+
221
+ = 0.7.19 =
222
+
223
+ * WP 3.9 compatibility fix, thanks to Sowmedia
224
+
225
+ = 0.7.18 =
226
+
227
+ * Patch to address 404 errors when displaying a page/post that shares a permalink with a trashed page/post, thanks to Tor Johnson
228
+
229
+ = 0.7.17 =
230
+
231
+ * Patch to address SSL problems, thanks to Amin Mirzaee
232
+
233
+ = 0.7.16 =
234
+
235
+ * Security and compatibility fixes by Hans-Michael Varbaek of Sense of Security
236
+
237
+ = 0.7.15 =
238
+
239
+ * Permalinks are now case-insensitive (thanks to @ericmann)
240
+
241
+ = 0.7.14 =
242
+
243
+ * Delete permalinks upon page/post deletion
244
+
245
+ = 0.7.13 =
246
+
247
+ * Fixed issue with term permalinks not working properly on some installations
248
+
249
+ = 0.7.12 =
250
+
251
+ * Fixed issue with feed URLs in non-webroot blog installations
252
+
253
+ = 0.7.11 =
254
+
255
+ * Fixed issue with pending/draft posts with permalinks
256
+ * Fixed infinite redirect issue with permalinks without trailing slash, on blogs not hosted in the webroot
257
+
258
+ = 0.7.10 =
259
+
260
+ * Fix for 404 error on static front page with custom permalink set, by Eric TF Bat
261
+
262
+ = 0.7.9 =
263
+
264
+ * Support for custom post types, by Balázs Németh
265
+
266
+ = 0.7.8 =
267
+
268
+ * Support for non-ASCII characters in URLs
269
+ * Fixed bug where adding a new tag when saving a post with a custom permalink attaches that permalink to the new tag
270
+ * Some compatibility fixes for WP 3.2.1
271
+
272
+ = 0.7.7 =
273
+
274
+ * Minor change to permalink saving routine to fix some possible issues
275
+ * Fixed issue with %-encoded permalinks
276
+
277
+ = 0.7.6 =
278
+
279
+ * Fixed permalink saving issue when not using ".../%postname%" or similar permalink structure
280
+
281
+ = 0.7.5 =
282
+
283
+ * Fixed issue where changes to trailing "/" aren't saved
284
+
285
+ = 0.7.4 =
286
+
287
+ * Added support for changing post/page slug only
288
+ * Fixed incorrect admin edit link
289
+
290
+ = 0.7.3 =
291
+
292
+ * Fix problem with /page/# URLs on WP 3.1.3
293
+
294
+ = 0.7.2 =
295
+
296
+ * Don't clobber query parameters when redirecting to the custom permalink from the original URL
297
+
298
+ = 0.7.1 =
299
+
300
+ * Compatiblity fix for last update
301
+
302
+ = 0.7 =
303
+
304
+ * Added support for SSL sites, thanks to Dan from todaywasawesome.com
305
+
306
+ = 0.6.1 =
307
+
308
+ * Fix bug causing incorrect link from "View Post"/"View Page" button in post/page editor
309
+
310
+ = 0.6 =
311
+
312
+ * Fix infinite redirect for permalinks ending in a / (critical fix)
313
+ * Moved post/page permalinks settings to top of edit form, replacing prior permalink display
314
+
315
+ = 0.5.3 =
316
+
317
+ * Fix for invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html) when using permalinks without a trailing slash (like .../%postname%.html)
318
+
319
+ = 0.5.2 =
320
+
321
+ * Bugfix for matching posts when there are multiple posts that match parts of the query
322
+
323
+ = 0.5.1 =
324
+
325
+ * Compatibility fix for WP 2.7's tag/category pages
326
+
327
+ = 0.5 =
328
+
329
+ * Support for Wordpress sites in subdirectories (i.e., not located at the webroot)
330
+
331
+ = 0.4.1 =
332
+
333
+ * WP 2.7 compatability fixes; fix for bug encountered when publishing a draft, or reverting to draft status, and fix for placeholder permalink value for pages
334
+
335
+ = 0.4 =
336
+
337
+ * Support for pages, and a fix for draft posts/pages
338
+
339
+ = 0.3.1 =
340
+
341
+ * Discovered a typo that broke categories
342
+
343
+ = 0.3 =
344
+
345
+ * Largely rewritten to provide more robust handling of trailing slashes, proper support for trailing URL components (eg. paging)
346
+
347
+ = 0.2.2 =
348
+
349
+ * Fixed bug with not matching permalinks when / appended to the URL, and workaround for infinite redirect when another plugin is enforcing trailing /
350
+
351
+ = 0.2.1 =
352
+
353
+ * Better handling of trailing slashes
354
+
355
+ = 0.2 =
356
+
357
+ * Added 'Custom Permalinks' section under 'Manage' to show existing custom permalinks, and allow reverting to the defaults
358
+
359
+ = 0.1.1 =
360
+
361
+ * Fixed bug with categories
362
+
363
+ == Upgrade Notice ==
364
+
365
+ = 0.6.1 =
366
+
367
+ * This release fixes a bug causing incorrect link from the "View Post"/"View Page" button in the editor
368
+
369
+ = 0.6 =
370
+
371
+ In the process of fixing one issue, version 0.5.3 broke permalinks ending with a "/". Update now to fix this, and sorry for the inconvenience!
372
+
373
+ = 0.5.3 =
374
+
375
+ If you are having problems with Custom Permalinks causing an invalid URL redirect (eg. http://domain.comfolder/file.html instead of http://domain.com/folder/file.html),
376
+ upgrade: This has now been fixed.
377
+