Custom Post Type Permalinks - Version 0.9.6

Version Description

  • Category and author.
  • French Transration. Thanks Geoffrey!
  • Hierarchial Term Fix.
Download this release

Release Info

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

Code changes from version 0.9.5.6 to 0.9.6

.travis.yml ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ language: php
2
+
3
+ php:
4
+ - 5.3
5
+ - 5.4
6
+
7
+ env:
8
+ - WP_VERSION=latest WP_MULTISITE=0
9
+ - WP_VERSION=latest WP_MULTISITE=1
10
+ - WP_VERSION=3.8 WP_MULTISITE=0
11
+ - WP_VERSION=3.8 WP_MULTISITE=1
12
+
13
+ before_script:
14
+ - bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION
15
+
16
+ script: phpunit
CPTP.php CHANGED
@@ -11,7 +11,7 @@
11
  * */
12
 
13
 
14
- define( "CPTP_VERSION", "0.9.5.6" );
15
  define( "CPTP_DEFAULT_PERMALINK", "/%postname%/" );
16
  define( "CPTP_DIR", dirname( __FILE__ ) );
17
 
11
  * */
12
 
13
 
14
+ define( "CPTP_VERSION", "0.9.6" );
15
  define( "CPTP_DEFAULT_PERMALINK", "/%postname%/" );
16
  define( "CPTP_DIR", dirname( __FILE__ ) );
17
 
CPTP/Module/Permalink.php CHANGED
@@ -35,9 +35,7 @@ class CPTP_Module_Permalink extends CPTP_Module {
35
  *
36
  */
37
  public function post_type_link( $post_link, $post, $leavename ) {
38
-
39
  global $wp_rewrite;
40
-
41
  $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
42
  if( $draft_or_pending and !$leavename )
43
  return $post_link;
@@ -71,7 +69,7 @@ class CPTP_Module_Permalink extends CPTP_Module {
71
  //%post_id%/attachment/%attachement_name%;
72
  //画像の編集ページでのリンク
73
  if( isset($_GET["post"]) && $_GET["post"] != $post->ID ) {
74
- $parent_structure = trim(get_option( $post->post_type.'_structure' ), "/");
75
  $parent_dirs = explode( "/", $parent_structure );
76
  if(is_array($parent_dirs)) {
77
  $last_dir = array_pop( $parent_dirs );
@@ -93,11 +91,33 @@ class CPTP_Module_Permalink extends CPTP_Module {
93
  $replace = $replace + $replace_tag["replace"];
94
 
95
 
96
- $user = get_userdata( $post->post_author );
97
- if(isset($user->user_nicename)) {
98
- $permalink = str_replace( "%author%", $user->user_nicename, $permalink );
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  }
100
 
 
101
  $post_date = strtotime( $post->post_date );
102
  $permalink = str_replace(array(
103
  "%year%",
@@ -105,14 +125,20 @@ class CPTP_Module_Permalink extends CPTP_Module {
105
  "%day%",
106
  "%hour%",
107
  "%minute%",
108
- "%second%"
 
 
 
109
  ), array(
110
  date("Y",$post_date),
111
  date("m",$post_date),
112
  date("d",$post_date),
113
  date("H",$post_date),
114
  date("i",$post_date),
115
- date("s",$post_date)
 
 
 
116
  ), $permalink );
117
  $permalink = str_replace($search, $replace, $permalink);
118
  $permalink = rtrim( home_url(),"/")."/".ltrim( $permalink ,"/" );
@@ -137,45 +163,46 @@ class CPTP_Module_Permalink extends CPTP_Module {
137
  //運用でケアすべきかも。
138
 
139
  foreach ( $taxonomies as $taxonomy => $objects ) {
140
- $term = null;
141
  if ( strpos($permalink, "%$taxonomy%") !== false ) {
142
  $terms = wp_get_post_terms( $post_id, $taxonomy, array('orderby' => 'term_id'));
143
 
144
- if ( $terms and count($terms) > 1 ) {
145
- if(reset($terms)->parent == 0){
146
 
147
- $keys = array_keys($terms);
148
- $term = $terms[$keys[1]]->slug;
149
- if ( $terms[$keys[0]]->term_id == $terms[$keys[1]]->parent ) {
150
- $term = CPTP_Util::get_taxonomy_parents( $terms[$keys[1]]->parent,$taxonomy, false, '/', true ) . $term;
151
- }
152
- }else{
153
- $keys = array_keys($terms);
154
- $term = $terms[$keys[0]]->slug;
155
- if ( $terms[$keys[1]]->term_id == $terms[$keys[0]]->parent ) {
156
- $term = CPTP_Util::get_taxonomy_parents( $terms[$keys[0]]->parent,$taxonomy, false, '/', true ) . $term;
157
  }
158
  }
159
- }else if( $terms ){
160
 
161
- $term_obj = array_shift($terms);
162
- $term = $term_obj->slug;
 
163
 
164
  if(isset($term_obj->parent) and $term_obj->parent != 0) {
165
- $term = CPTP_Util::get_taxonomy_parents( $term_obj->parent,$taxonomy, false, '/', true ) . $term;
166
  }
167
  }
168
 
169
- if( isset($term) ) {
170
  $search[] = "%$taxonomy%";
171
- $replace[] = $term;
172
  }
173
 
174
  }
175
  }
 
176
  return array("search" => $search, "replace" => $replace );
177
  }
178
 
 
 
 
 
 
 
179
 
180
 
181
  /**
@@ -193,7 +220,7 @@ class CPTP_Module_Permalink extends CPTP_Module {
193
  return $link;
194
  }
195
  $post_parent = get_post( $post->post_parent );
196
- $permalink = get_option( $post_parent->post_type.'_structure' );
197
  $post_type = get_post_type_object( $post_parent->post_type );
198
 
199
  if( $post_type->_builtin == false ) {
35
  *
36
  */
37
  public function post_type_link( $post_link, $post, $leavename ) {
 
38
  global $wp_rewrite;
 
39
  $draft_or_pending = isset( $post->post_status ) && in_array( $post->post_status, array( 'draft', 'pending', 'auto-draft' ) );
40
  if( $draft_or_pending and !$leavename )
41
  return $post_link;
69
  //%post_id%/attachment/%attachement_name%;
70
  //画像の編集ページでのリンク
71
  if( isset($_GET["post"]) && $_GET["post"] != $post->ID ) {
72
+ $parent_structure = trim( CPTP_Util::get_permalink_structure( $this->post_type ), "/");
73
  $parent_dirs = explode( "/", $parent_structure );
74
  if(is_array($parent_dirs)) {
75
  $last_dir = array_pop( $parent_dirs );
91
  $replace = $replace + $replace_tag["replace"];
92
 
93
 
94
+ //from get_permalink.
95
+ $category = '';
96
+ if ( strpos($permalink, '%category%') !== false ) {
97
+ $cats = get_the_category($post->ID);
98
+ if ( $cats ) {
99
+ usort($cats, '_usort_terms_by_ID'); // order by ID
100
+ $category_object = apply_filters( 'post_link_category', $cats[0], $cats, $post );
101
+ $category_object = get_term( $category_object, 'category' );
102
+ $category = $category_object->slug;
103
+ if ( $parent = $category_object->parent )
104
+ $category = get_category_parents($parent, false, '/', true) . $category;
105
+ }
106
+ // show default category in permalinks, without
107
+ // having to assign it explicitly
108
+ if ( empty($category) ) {
109
+ $default_category = get_term( get_option( 'default_category' ), 'category' );
110
+ $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
111
+ }
112
+ }
113
+
114
+ $author = '';
115
+ if ( strpos($permalink, '%author%') !== false ) {
116
+ $authordata = get_userdata($post->post_author);
117
+ $author = $authordata->user_nicename;
118
  }
119
 
120
+
121
  $post_date = strtotime( $post->post_date );
122
  $permalink = str_replace(array(
123
  "%year%",
125
  "%day%",
126
  "%hour%",
127
  "%minute%",
128
+ "%second%",
129
+ '%category%',
130
+ '%author%'
131
+
132
  ), array(
133
  date("Y",$post_date),
134
  date("m",$post_date),
135
  date("d",$post_date),
136
  date("H",$post_date),
137
  date("i",$post_date),
138
+ date("s",$post_date),
139
+ $category,
140
+ $author
141
+
142
  ), $permalink );
143
  $permalink = str_replace($search, $replace, $permalink);
144
  $permalink = rtrim( home_url(),"/")."/".ltrim( $permalink ,"/" );
163
  //運用でケアすべきかも。
164
 
165
  foreach ( $taxonomies as $taxonomy => $objects ) {
166
+
167
  if ( strpos($permalink, "%$taxonomy%") !== false ) {
168
  $terms = wp_get_post_terms( $post_id, $taxonomy, array('orderby' => 'term_id'));
169
 
170
+ if( $terms and !is_wp_error($terms) ) {
 
171
 
172
+ $parents = array_map( array(__CLASS__, 'get_term_parent'), $terms); //親の一覧
173
+ $newTerms = array();
174
+ foreach ($terms as $key => $term) {
175
+ if( !in_array($term->term_id, $parents )) {
176
+ $newTerms[] = $term;
 
 
 
 
 
177
  }
178
  }
 
179
 
180
+ //このブロックだけで良いはず。
181
+ $term_obj = reset($newTerms); //最初のOBjectのみを対象。
182
+ $term_slug = $term_obj->slug;
183
 
184
  if(isset($term_obj->parent) and $term_obj->parent != 0) {
185
+ $term_slug = CPTP_Util::get_taxonomy_parents( $term_obj->parent,$taxonomy, false, '/', true ) . $term_slug;
186
  }
187
  }
188
 
189
+ if( isset($term_slug) ) {
190
  $search[] = "%$taxonomy%";
191
+ $replace[] = $term_slug;
192
  }
193
 
194
  }
195
  }
196
+
197
  return array("search" => $search, "replace" => $replace );
198
  }
199
 
200
+ private static function get_term_parent($term) {
201
+ if(isset($term->parent) and $term->parent > 0) {
202
+ return $term->parent;
203
+ }
204
+ }
205
+
206
 
207
 
208
  /**
220
  return $link;
221
  }
222
  $post_parent = get_post( $post->post_parent );
223
+ $permalink = CPTP_Util::get_permalink_structure( $post_parent->post_type );
224
  $post_type = get_post_type_object( $post_parent->post_type );
225
 
226
  if( $post_type->_builtin == false ) {
CPTP/Module/Rewrite.php CHANGED
@@ -36,7 +36,7 @@ class CPTP_Module_Rewrite extends CPTP_Module {
36
  if( $args->_builtin or !$args->publicly_queryable or !$args->show_ui ){
37
  return false;
38
  }
39
- $permalink = get_option( $post_type.'_structure' );
40
 
41
  if( !$permalink ) {
42
  $permalink = CPTP_DEFAULT_PERMALINK;
36
  if( $args->_builtin or !$args->publicly_queryable or !$args->show_ui ){
37
  return false;
38
  }
39
+ $permalink = CPTP_Util::get_permalink_structure($post_type);
40
 
41
  if( !$permalink ) {
42
  $permalink = CPTP_DEFAULT_PERMALINK;
CPTP/Util.php CHANGED
@@ -61,6 +61,10 @@ class CPTP_Util {
61
  return $chain;
62
  }
63
 
 
 
 
 
64
 
65
 
66
  }
61
  return $chain;
62
  }
63
 
64
+ public static function get_permalink_structure($post_type) {
65
+ return apply_filters( "CPTP_".$post_type."_structure", get_option($post_type."_structure") );
66
+ }
67
+
68
 
69
 
70
  }
bin/install-wp-tests.sh ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ if [ $# -lt 3 ]; then
4
+ echo "usage: $0 <db-name> <db-user> <db-pass> [db-host] [wp-version]"
5
+ exit 1
6
+ fi
7
+
8
+ DB_NAME=$1
9
+ DB_USER=$2
10
+ DB_PASS=$3
11
+ DB_HOST=${4-localhost}
12
+ WP_VERSION=${5-latest}
13
+
14
+ WP_TESTS_DIR=${WP_TESTS_DIR-/tmp/wordpress-tests-lib}
15
+ WP_CORE_DIR=/tmp/wordpress/
16
+
17
+ set -ex
18
+
19
+ install_wp() {
20
+ mkdir -p $WP_CORE_DIR
21
+
22
+ if [ $WP_VERSION == 'latest' ]; then
23
+ local ARCHIVE_NAME='latest'
24
+ else
25
+ local ARCHIVE_NAME="wordpress-$WP_VERSION"
26
+ fi
27
+
28
+ wget -nv --no-check-certificate -O /tmp/wordpress.tar.gz http://wordpress.org/${ARCHIVE_NAME}.tar.gz
29
+ tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C $WP_CORE_DIR
30
+
31
+ wget -nv --no-check-certificate -O $WP_CORE_DIR/wp-content/db.php https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
32
+ }
33
+
34
+ install_test_suite() {
35
+ # portable in-place argument for both GNU sed and Mac OSX sed
36
+ if [[ $(uname -s) == 'Darwin' ]]; then
37
+ local ioption='-i .bak'
38
+ else
39
+ local ioption='-i'
40
+ fi
41
+
42
+ # set up testing suite
43
+ mkdir -p $WP_TESTS_DIR
44
+ cd $WP_TESTS_DIR
45
+ svn co --quiet http://develop.svn.wordpress.org/trunk/tests/phpunit/includes/
46
+
47
+ wget --no-check-certificate -nv -O wp-tests-config.php http://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php
48
+ sed $ioption "s:dirname( __FILE__ ) . '/src/':'$WP_CORE_DIR':" wp-tests-config.php
49
+ sed $ioption "s/youremptytestdbnamehere/$DB_NAME/" wp-tests-config.php
50
+ sed $ioption "s/yourusernamehere/$DB_USER/" wp-tests-config.php
51
+ sed $ioption "s/yourpasswordhere/$DB_PASS/" wp-tests-config.php
52
+ sed $ioption "s|localhost|${DB_HOST}|" wp-tests-config.php
53
+ }
54
+
55
+ install_db() {
56
+ # parse DB_HOST for port or socket references
57
+ local PARTS=(${DB_HOST//\:/ })
58
+ local DB_HOSTNAME=${PARTS[0]};
59
+ local DB_SOCK_OR_PORT=${PARTS[1]};
60
+ local EXTRA=""
61
+
62
+ if ! [ -z $DB_HOSTNAME ] ; then
63
+ if [[ "$DB_SOCK_OR_PORT" =~ ^[0-9]+$ ]] ; then
64
+ EXTRA=" --host=$DB_HOSTNAME --port=$DB_SOCK_OR_PORT --protocol=tcp"
65
+ elif ! [ -z $DB_SOCK_OR_PORT ] ; then
66
+ EXTRA=" --socket=$DB_SOCK_OR_PORT"
67
+ elif ! [ -z $DB_HOSTNAME ] ; then
68
+ EXTRA=" --host=$DB_HOSTNAME --protocol=tcp"
69
+ fi
70
+ fi
71
+
72
+ # create database
73
+ mysqladmin create $DB_NAME --user="$DB_USER" --password="$DB_PASS"$EXTRA
74
+ }
75
+
76
+ install_wp
77
+ install_test_suite
78
+ install_db
custom-post-type-permalinks.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.torounit.com
5
  Description: Add post archives of custom post type and customizable permalinks.
6
  Author: Toro_Unit
7
  Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/
8
- Version: 0.9.5.6
9
  Text Domain: cptp
10
  License: GPL2 or later
11
  Domain Path: /language/
5
  Description: Add post archives of custom post type and customizable permalinks.
6
  Author: Toro_Unit
7
  Author URI: http://www.torounit.com/plugins/custom-post-type-permalinks/
8
+ Version: 0.9.6
9
  Text Domain: cptp
10
  License: GPL2 or later
11
  Domain Path: /language/
phpunit.xml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <phpunit
2
+ bootstrap="tests/bootstrap.php"
3
+ backupGlobals="false"
4
+ colors="true"
5
+ convertErrorsToExceptions="true"
6
+ convertNoticesToExceptions="true"
7
+ convertWarningsToExceptions="true"
8
+ >
9
+ <testsuites>
10
+ <testsuite>
11
+ <directory prefix="test-" suffix=".php">./tests/</directory>
12
+ </testsuite>
13
+ </testsuites>
14
+ </phpunit>
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
- === Custom Post Type Permalinks ===
2
  Contributors: Toro_Unit
3
  Tags: permalink,permalinks,custom post type,custom taxonomy,cms
4
  Requires at least: 3.7
5
- Tested up to: 3.9
6
- Stable tag: 0.9.5.6
7
 
8
  Lets you edit the permalink of custom post type.
9
 
@@ -20,6 +20,7 @@ And support wp_get_archives( "post_type=foo" ).
20
  Donation: Please send amazon.co.jp Gift to donate[at]torounit.com.
21
 
22
  = Translators =
 
23
  * Japanese(ja) - [Toro_Unit](http://www.torounit.com/)
24
  * Russian(ru_RU) - [Olart](http://olart.ru)
25
 
@@ -41,6 +42,11 @@ That's it. You can access the permalinks setting by going to *Settings -> Permal
41
 
42
  == Changelog ==
43
 
 
 
 
 
 
44
  = 0.9.5.6 =
45
  * Strict Standard Error Fix.
46
 
1
+ === Custom Post Type Permalinks ===
2
  Contributors: Toro_Unit
3
  Tags: permalink,permalinks,custom post type,custom taxonomy,cms
4
  Requires at least: 3.7
5
+ Tested up to: 4.0
6
+ Stable tag: 0.9.6
7
 
8
  Lets you edit the permalink of custom post type.
9
 
20
  Donation: Please send amazon.co.jp Gift to donate[at]torounit.com.
21
 
22
  = Translators =
23
+ * French(fr_FR) - [Geoffrey Crofte](http://geoffrey.crofte.fr/)
24
  * Japanese(ja) - [Toro_Unit](http://www.torounit.com/)
25
  * Russian(ru_RU) - [Olart](http://olart.ru)
26
 
42
 
43
  == Changelog ==
44
 
45
+ = 0.9.6 =
46
+ * Category and author.
47
+ * French Transration. Thanks Geoffrey!
48
+ * Hierarchial Term Fix.
49
+
50
  = 0.9.5.6 =
51
  * Strict Standard Error Fix.
52
 
tests/bootstrap.php ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ $_tests_dir = getenv('WP_TESTS_DIR');
4
+ if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';
5
+
6
+ require_once $_tests_dir . '/includes/functions.php';
7
+
8
+ function _manually_load_plugin() {
9
+ require dirname( __FILE__ ) . '/../custom-post-type-permalinks.php';
10
+ }
11
+ tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
12
+
13
+ require $_tests_dir . '/includes/bootstrap.php';
14
+
tests/test-cptp.php ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class CPTPTest extends WP_UnitTestCase {
4
+
5
+
6
+ public function setUp() {
7
+ global $wp_rewrite;
8
+ parent::setUp();
9
+
10
+ $wp_rewrite->init();
11
+ $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
12
+ create_initial_taxonomies();
13
+ $wp_rewrite->flush_rules();
14
+ cptp_init_instance();
15
+ }
16
+
17
+
18
+ public function test_url_to_postid_cpt_year_monthnum_day_post_id () {
19
+ delete_option( 'rewrite_rules' );
20
+ $post_type = rand_str( 12 );
21
+ register_post_type( $post_type, array( "public" => true ) );
22
+
23
+ update_option($post_type."_structure", "/%year%/%monthnum%/%day%/%post_id%/" );
24
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
25
+ $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
26
+
27
+ _unregister_post_type( $post_type );
28
+ }
29
+
30
+ public function test_url_to_postid_cpt_year_monthnum_day_postname () {
31
+ delete_option( 'rewrite_rules' );
32
+ $post_type = rand_str( 12 );
33
+ update_option($post_type."_structure", "/%year%/%monthnum%/%day%/%postname%/" );
34
+ register_post_type( $post_type, array( "public" => true ) );
35
+
36
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
37
+ $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
38
+
39
+ _unregister_post_type( $post_type );
40
+ }
41
+
42
+ public function test_url_to_postid_cpt_term_post_id () {
43
+ delete_option( 'rewrite_rules' );
44
+ $post_type = rand_str( 12 );
45
+ $taxonomy = rand_str( 12 );
46
+ update_option($post_type."_structure", "/%".$taxonomy."%/%post_id%/" );
47
+ register_taxonomy( $taxonomy, $post_type, array( "public" => true ) );
48
+ register_post_type( $post_type, array( "public" => true ) );
49
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
50
+ wp_set_post_terms( $id, rand_str( 12 ) , $taxonomy );
51
+ $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
52
+
53
+ _unregister_post_type( $post_type );
54
+ _unregister_taxonomy( $taxonomy, $post_type );
55
+ }
56
+
57
+
58
+ public function test_url_to_postid_cpt_hierarchial_term_post_id() {
59
+ delete_option( 'rewrite_rules' );
60
+ $post_type = rand_str( 12 );
61
+ $taxonomy = rand_str( 12 );
62
+
63
+ update_option($post_type."_structure", "/%".$taxonomy."%/%post_id%/" );
64
+ register_taxonomy( $taxonomy, $post_type, array( "public" => true ,"hierarchial" => true) );
65
+ register_post_type( $post_type, array( "public" => true ) );
66
+
67
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
68
+
69
+ $term_id = 0;
70
+ for ($i=0; $i < 10; $i++) {
71
+ $slug = rand_str( 12 );
72
+ $term = wp_insert_term( $slug, $taxonomy, array("parent" => $term_id, "slug" => $slug) );
73
+ $term_id = $term["term_id"];
74
+ }
75
+ wp_set_post_terms( $id, get_term( $term_id, $taxonomy )->slug, $taxonomy );
76
+
77
+ $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
78
+ _unregister_post_type( $post_type );
79
+ _unregister_taxonomy( $taxonomy, $post_type );
80
+ }
81
+
82
+ public function test_url_cpt_hierarchial_url_to_that_all() {
83
+ delete_option( 'rewrite_rules' );
84
+ $post_type = rand_str( 12 );
85
+ $taxonomy = rand_str( 12 );
86
+
87
+ update_option($post_type."_structure", "/%".$taxonomy."%/%postname%/" );
88
+ register_taxonomy( $taxonomy, $post_type, array( "public" => true ,"hierarchial" => true) );
89
+ register_post_type( $post_type, array( "public" => true ) );
90
+
91
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ,"post_name" => "foo") );
92
+ $id_all_term = $this->factory->post->create( array( 'post_type' => $post_type ,"post_name" => "foo_all_term") );
93
+
94
+ $term_id = 0;
95
+ $slug_list = array();
96
+ for ($i=0; $i < 10; $i++) {
97
+ $slug = rand_str( 12 );
98
+ $term = wp_insert_term( $slug, $taxonomy, array("parent" => $term_id, "slug" => $slug) );
99
+ $term_id = $term["term_id"];
100
+ $slug_list[] = get_term( $term_id, $taxonomy )->slug;
101
+
102
+ }
103
+ wp_set_post_terms( $id, get_term( $term_id, $taxonomy )->slug, $taxonomy );
104
+ wp_set_post_terms( $id_all_term, $slug_list, $taxonomy );
105
+
106
+ $this->assertEquals( get_permalink( $id ), str_replace("foo_all_term", "foo", get_permalink( $id_all_term )) );
107
+
108
+ _unregister_post_type( $post_type );
109
+ _unregister_taxonomy( $taxonomy, $post_type );
110
+ }
111
+
112
+
113
+ public function test_url_to_postid_category_post_id () {
114
+ delete_option( 'rewrite_rules' );
115
+ $post_type = rand_str( 12 );
116
+ $taxonomy = rand_str( 12 );
117
+ update_option($post_type."_structure", "/%category%/%post_id%/" );
118
+ register_post_type( $post_type, array( "public" => true , 'taxonomies' => array('category')) );
119
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ) );
120
+ $term = wp_insert_term( rand_str( 12 ), "category" );
121
+ wp_set_post_categories( $id, array($term["term_id"]) );
122
+
123
+ $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
124
+
125
+ _unregister_post_type( $post_type );
126
+ _unregister_taxonomy( $taxonomy, $post_type );
127
+ }
128
+
129
+
130
+ public function test_url_to_postid_cpt_author_postname () {
131
+ delete_option( 'rewrite_rules' );
132
+ $post_type = rand_str( 12 );
133
+ update_option($post_type."_structure", "/%author%/%postname%/" );
134
+ register_post_type( $post_type, array( "public" => true ) );
135
+ $user_id = $this->factory->user->create();
136
+ $id = $this->factory->post->create( array( 'post_type' => $post_type ,"post_author" => $user_id ) );
137
+ $this->assertEquals( $id, url_to_postid( get_permalink( $id ) ) );
138
+ _unregister_post_type( $post_type );
139
+ }
140
+
141
+
142
+ }
143
+
tests/test-sample.php ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class SampleTest extends WP_UnitTestCase {
4
+
5
+ function testSample() {
6
+ // replace this with some actual testing code
7
+ $this->assertTrue( true );
8
+ }
9
+
10
+ }
11
+