Custom Post Type Permalinks - Version 3.3.0

Version Description

  • Add disable option for date / author and post type archive.
  • Bug fix for parse_request.
Download this release

Release Info

Developer Toro_Unit
Plugin Icon 128x128 Custom Post Type Permalinks
Version 3.3.0
Comparing to
See all releases

Code changes from version 3.2.2 to 3.3.0

.svnignore DELETED
@@ -1,34 +0,0 @@
1
- # A set of files you probably don't want in your WordPress.org distribution
2
- .svnignore
3
- .editorconfig
4
- .git
5
- .gitignore
6
- .gitlab-ci.yml
7
- .travis.yml
8
- .vscode
9
- .DS_Store
10
- Thumbs.db
11
- behat.yml
12
- bin
13
- circle.yml
14
- composer.json
15
- composer.lock
16
- Gruntfile.js
17
- package.json
18
- package-lock.json
19
- phpunit.xml
20
- phpunit.xml.dist
21
- multisite.xml
22
- multisite.xml.dist
23
- phpcs.ruleset.xml
24
- codesniffer.ruleset.xml
25
- README.md
26
- wp-cli.local.yml
27
- tests
28
- vendor
29
- node_modules
30
- *.sql
31
- *.tar.gz
32
- *.zip
33
- .release-it.json
34
- .env
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
CPTP/Module/Rewrite.php CHANGED
@@ -29,8 +29,8 @@ class CPTP_Module_Rewrite extends CPTP_Module {
29
  * @version 1.1
30
  * @since 0.9
31
  *
32
- * @param string $post_type Post type.
33
- * @param WP_Post_Type $args Arguments used to register the post type.
34
  */
35
  public function register_post_type_rules( $post_type, $args ) {
36
 
@@ -86,29 +86,31 @@ class CPTP_Module_Rewrite extends CPTP_Module {
86
  $slug = substr( $wp_rewrite->front, 1 ) . $slug;
87
  }
88
 
89
- $date_front = CPTP_Util::get_date_front( $post_type );
90
-
91
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' );
92
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' );
93
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top' );
94
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type=' . $post_type, 'top' );
95
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' );
96
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' );
97
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type=' . $post_type, 'top' );
98
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type=' . $post_type, 'top' );
99
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' );
100
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' );
101
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
102
- add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top' );
103
- add_rewrite_rule( $slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
104
- add_rewrite_rule( $slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top' );
 
 
 
 
 
105
 
106
  if ( in_array( 'category', $args->taxonomies, true ) ) {
107
 
108
- $category_base = get_option( 'category_base' );
109
- if ( ! $category_base ) {
110
- $category_base = 'category';
111
- }
112
 
113
  add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
114
  add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' );
@@ -169,7 +171,7 @@ class CPTP_Module_Rewrite extends CPTP_Module {
169
  }
170
 
171
  if ( 'category' === $taxonomy ) {
172
- $cb = get_option( 'category_base' );
173
  $taxonomy_slug = ( $cb ) ? $cb : $taxonomy;
174
  $taxonomy_key = 'category_name';
175
  } else {
@@ -268,13 +270,16 @@ class CPTP_Module_Rewrite extends CPTP_Module {
268
  * @param WP $obj WP instance.
269
  */
270
  public function parse_request( $obj ) {
271
- $taxes = CPTP_Util::get_taxonomies();
272
  foreach ( $taxes as $key => $tax ) {
273
- if ( isset( $obj->query_vars[ $tax ] ) && is_string( $obj->query_vars[ $tax ] ) ) {
274
- if ( false !== strpos( $obj->query_vars[ $tax ], '/' ) ) {
275
- $query_vars = explode( '/', $obj->query_vars[ $tax ] );
276
- if ( is_array( $query_vars ) ) {
277
- $obj->query_vars[ $tax ] = array_pop( $query_vars );
 
 
 
278
  }
279
  }
280
  }
29
  * @version 1.1
30
  * @since 0.9
31
  *
32
+ * @param string $post_type Post type.
33
+ * @param WP_Post_Type $args Arguments used to register the post type.
34
  */
35
  public function register_post_type_rules( $post_type, $args ) {
36
 
86
  $slug = substr( $wp_rewrite->front, 1 ) . $slug;
87
  }
88
 
89
+ if ( CPTP_Util::get_post_type_date_archive_support( $post_type ) ) {
90
+ $date_front = CPTP_Util::get_date_front( $post_type );
91
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' );
92
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&feed=$matches[4]&post_type=' . $post_type, 'top' );
93
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&paged=$matches[4]&post_type=' . $post_type, 'top' );
94
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&day=$matches[3]&post_type=' . $post_type, 'top' );
95
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' );
96
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&feed=$matches[3]&post_type=' . $post_type, 'top' );
97
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&paged=$matches[3]&post_type=' . $post_type, 'top' );
98
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/([0-9]{1,2})/?$', 'index.php?year=$matches[1]&monthnum=$matches[2]&post_type=' . $post_type, 'top' );
99
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' );
100
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/(feed|rdf|rss|rss2|atom)/?$', 'index.php?year=$matches[1]&feed=$matches[2]&post_type=' . $post_type, 'top' );
101
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/page/?([0-9]{1,})/?$', 'index.php?year=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
102
+ add_rewrite_rule( $slug . $date_front . '/([0-9]{4})/?$', 'index.php?year=$matches[1]&post_type=' . $post_type, 'top' );
103
+ }
104
+
105
+ if ( CPTP_Util::get_post_type_author_archive_support( $post_type ) ) {
106
+
107
+ add_rewrite_rule( $slug . '/author/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?author_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
108
+ add_rewrite_rule( $slug . '/author/([^/]+)/?$', 'index.php?author_name=$matches[1]&post_type=' . $post_type, 'top' );
109
+ }
110
 
111
  if ( in_array( 'category', $args->taxonomies, true ) ) {
112
 
113
+ $category_base = get_option( 'category_base', 'category' );
 
 
 
114
 
115
  add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?category_name=$matches[1]&paged=$matches[2]&post_type=' . $post_type, 'top' );
116
  add_rewrite_rule( $slug . '/' . $category_base . '/([^/]+)/?$', 'index.php?category_name=$matches[1]&post_type=' . $post_type, 'top' );
171
  }
172
 
173
  if ( 'category' === $taxonomy ) {
174
+ $cb = get_option( 'category_base' );
175
  $taxonomy_slug = ( $cb ) ? $cb : $taxonomy;
176
  $taxonomy_key = 'category_name';
177
  } else {
270
  * @param WP $obj WP instance.
271
  */
272
  public function parse_request( $obj ) {
273
+ $taxes = CPTP_Util::get_taxonomies( true );
274
  foreach ( $taxes as $key => $tax ) {
275
+ $name = $tax->name;
276
+ if ( $tax->hierarchical ) {
277
+ if ( isset( $obj->query_vars[ $name ] ) && is_string( $obj->query_vars[ $name ] ) ) {
278
+ if ( false !== strpos( $obj->query_vars[ $name ], '/' ) ) {
279
+ $query_vars = explode( '/', $obj->query_vars[ $name ] );
280
+ if ( is_array( $query_vars ) ) {
281
+ $obj->query_vars[ $name ] = array_pop( $query_vars );
282
+ }
283
  }
284
  }
285
  }
CPTP/Util.php CHANGED
@@ -171,25 +171,68 @@ class CPTP_Util {
171
  *
172
  * @since 0.9.6
173
  *
174
- * @param string|object $post_type post type name. / object post type object.
175
  *
176
  * @return string post type structure.
177
  */
178
  public static function get_permalink_structure( $post_type ) {
179
  if ( is_string( $post_type ) ) {
180
- $pt_object = get_post_type_object( $post_type );
181
- } else {
182
- $pt_object = $post_type;
183
  }
184
 
185
- if ( ! empty( $pt_object->cptp_permalink_structure ) ) {
186
- $structure = $pt_object->cptp_permalink_structure;
 
 
187
  } else {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
- $structure = get_option( $pt_object->name . '_structure' );
 
190
  }
191
 
192
- return apply_filters( 'CPTP_' . $pt_object->name . '_structure', $structure );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
  }
194
 
195
 
171
  *
172
  * @since 0.9.6
173
  *
174
+ * @param string|WP_Post_Type $post_type post type name. / object post type object.
175
  *
176
  * @return string post type structure.
177
  */
178
  public static function get_permalink_structure( $post_type ) {
179
  if ( is_string( $post_type ) ) {
180
+ $post_type = get_post_type_object( $post_type );
 
 
181
  }
182
 
183
+ if ( ! empty( $post_type->cptp ) && ! empty( $post_type->cptp->permalink_structure ) ) {
184
+ $structure = $post_type->cptp->permalink_structure;
185
+ } else if ( ! empty( $post_type->cptp_permalink_structure ) ) {
186
+ $structure = $post_type->cptp_permalink_structure;
187
  } else {
188
+ $structure = get_option( $post_type->name . '_structure' );
189
+ }
190
+ $structure = '/' . ltrim( $structure, '/' );
191
+
192
+ return apply_filters( 'CPTP_' . $post_type->name . '_structure', $structure );
193
+ }
194
+
195
+ /**
196
+ * Check support date archive.
197
+ *
198
+ * @since 3.3.0
199
+ *
200
+ * @param string|WP_Post_Type $post_type post type name. / object post type object.
201
+ *
202
+ * @return bool
203
+ */
204
+ public static function get_post_type_date_archive_support( $post_type ) {
205
+ if ( is_string( $post_type ) ) {
206
+ $post_type = get_post_type_object( $post_type );
207
+ }
208
 
209
+ if ( ! empty( $post_type->cptp ) && isset( $post_type->cptp['date_archive'] ) ) {
210
+ return ! ! $post_type->cptp['date_archive'];
211
  }
212
 
213
+ return true;
214
+ }
215
+
216
+ /**
217
+ * Check support author archive.
218
+ *
219
+ * @since 3.3.0
220
+ *
221
+ * @param string|WP_Post_Type $post_type post type name. / object post type object.
222
+ *
223
+ * @return bool
224
+ */
225
+ public static function get_post_type_author_archive_support( $post_type ) {
226
+ if ( is_string( $post_type ) ) {
227
+ $post_type = get_post_type_object( $post_type );
228
+ }
229
+
230
+ if ( ! empty( $post_type->cptp ) && isset( $post_type->cptp['author_archive'] ) ) {
231
+ return ! ! $post_type->cptp['author_archive'];
232
+ }
233
+
234
+ return true;
235
+
236
  }
237
 
238
 
custom-post-type-permalinks.php CHANGED
@@ -5,13 +5,13 @@
5
  * Description: Add post archives of custom post type and customizable permalinks.
6
  * Author: Toro_Unit
7
  * Author URI: https://torounit.com/
8
- * Version: 3.2.2
9
  * Text Domain: custom-post-type-permalinks
10
  * License: GPL2 or later
11
  * Domain Path: /language/
12
  *
13
  * @package Custom_Post_Type_Permalinks
14
- * @version 3.2.2
15
  */
16
 
17
  define( 'CPTP_PLUGIN_FILE', __FILE__ );
5
  * Description: Add post archives of custom post type and customizable permalinks.
6
  * Author: Toro_Unit
7
  * Author URI: https://torounit.com/
8
+ * Version: 3.3.0
9
  * Text Domain: custom-post-type-permalinks
10
  * License: GPL2 or later
11
  * Domain Path: /language/
12
  *
13
  * @package Custom_Post_Type_Permalinks
14
+ * @version 3.3.0
15
  */
16
 
17
  define( 'CPTP_PLUGIN_FILE', __FILE__ );
readme.txt CHANGED
@@ -3,9 +3,9 @@ Contributors: Toro_Unit,inc2734,ixkaito,keita_kobayashi
3
  Donate link: https://www.paypal.me/torounit
4
  Tags: permalink,permalinks,custom post type,custom taxonomy,cms
5
  Requires at least: 4.3
6
- Tested up to: 4.9
7
  Requires PHP: 5.3
8
- Stable tag: 3.2.2
9
  License: GPLv2 or Later
10
 
11
  Edit the permalink of custom post type.
@@ -68,6 +68,10 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
68
 
69
  == Changelog ==
70
 
 
 
 
 
71
  = 3.2.2 =
72
  * Fix readme.txt
73
 
3
  Donate link: https://www.paypal.me/torounit
4
  Tags: permalink,permalinks,custom post type,custom taxonomy,cms
5
  Requires at least: 4.3
6
+ Tested up to: 5.0
7
  Requires PHP: 5.3
8
+ Stable tag: 3.3.0
9
  License: GPLv2 or Later
10
 
11
  Edit the permalink of custom post type.
68
 
69
  == Changelog ==
70
 
71
+ = 3.3.0 =
72
+ * Add disable option for date / author and post type archive.
73
+ * Bug fix for `parse_request`.
74
+
75
  = 3.2.2 =
76
  * Fix readme.txt
77