diff --git a/.travis.yml b/.travis.yml index 61ebd09..193cb2d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -25,7 +25,7 @@ matrix: - php: 7.0 env: WP_VERSION=latest - php: 5.6 - env: WP_VERSION=4.3 + env: WP_VERSION=4.7 - php: 5.6 env: WP_TRAVISCI=phpcs before_script: @@ -40,7 +40,7 @@ before_script: - | if [[ ! -z "$WP_VERSION" ]] ; then bash bin/install-wp-tests.sh wordpress_test root '' localhost $WP_VERSION - composer global require "phpunit/phpunit=4.8.*|5.7.*" + composer global require "phpunit/phpunit=5.7.*|6.*.*|7.*.*" fi - | if [[ "$WP_TRAVISCI" == "phpcs" ]] ; then diff --git a/.wp-env.json b/.wp-env.json new file mode 100644 index 0000000..4dce042 --- /dev/null +++ b/.wp-env.json @@ -0,0 +1,5 @@ +{ + "plugins": [ "." ], + "port": 50080, + "testsPort": 50081 +} diff --git a/CPTP/Module/Admin.php b/CPTP/Module/Admin.php index 51442e8..0b53fd2 100644 --- a/CPTP/Module/Admin.php +++ b/CPTP/Module/Admin.php @@ -76,7 +76,7 @@ public function settings_api_init() { ) ); - register_setting( 'permalink', 'no_taxonomy_structure' ); + register_setting( 'permalink', 'add_post_type_for_tax' ); } /** @@ -175,7 +175,6 @@ public function add_post_type_for_tax_callback_function() { esc_html_e( 'There are cases when the template to be loaded is changed.', 'custom-post-type-permalinks' ); } - /** * Enqueue css and js * diff --git a/CPTP/Module/Option.php b/CPTP/Module/Option.php index 57b3e78..ac64e2b 100644 --- a/CPTP/Module/Option.php +++ b/CPTP/Module/Option.php @@ -72,8 +72,8 @@ public function save_options() { $no_taxonomy_structure = ! filter_input( INPUT_POST, 'no_taxonomy_structure' ); $add_post_type_for_tax = filter_input( INPUT_POST, 'add_post_type_for_tax' ); - update_option( 'no_taxonomy_structure', $no_taxonomy_structure ); - update_option( 'add_post_type_for_tax', $add_post_type_for_tax ); + update_option( 'no_taxonomy_structure', ! ! $no_taxonomy_structure ); + update_option( 'add_post_type_for_tax', ! ! $add_post_type_for_tax ); update_option( 'cptp_permalink_checked', CPTP_VERSION ); } diff --git a/CPTP/Module/Permalink.php b/CPTP/Module/Permalink.php index a6c54cb..207f263 100644 --- a/CPTP/Module/Permalink.php +++ b/CPTP/Module/Permalink.php @@ -37,6 +37,13 @@ public function add_hook() { apply_filters( 'cptp_attachment_link_priority', 20 ), 2 ); + + add_filter( + 'wpml_st_post_type_link_filter_original_slug', + array( $this, 'replace_post_slug_with_placeholder' ), + 10, + 3 + ); } @@ -90,7 +97,7 @@ public function post_type_link( $post_link, $post, $leavename ) { $permalink = $wp_rewrite->get_extra_permastruct( $post_type ); $permalink = str_replace( '%post_id%', $post->ID, $permalink ); - $permalink = str_replace( '%' . $post_type . '_slug%', $pt_object->rewrite['slug'], $permalink ); + $permalink = str_replace( CPTP_Module_Rewrite::get_slug_placeholder( $post_type ), $pt_object->rewrite['slug'], $permalink ); // has parent. $parentsDirs = ''; @@ -388,4 +395,18 @@ public function term_link( $termlink, $term, $taxonomy ) { return $termlink; } + + /** + * This filter is needed for WPML's compatibility. It will return + * the slug placeholder instead of the original CPT slug. + * + * @param string $original_slug The original CPT slug. + * @param string $post_link The post link. + * @param WP_Post $post The post. + * + * @return string + */ + public function replace_post_slug_with_placeholder( $original_slug, $post_link, $post ) { + return CPTP_Module_Rewrite::get_slug_placeholder( $post->post_type ); + } } diff --git a/CPTP/Module/Rewrite.php b/CPTP/Module/Rewrite.php index 826ca50..1ac3593 100644 --- a/CPTP/Module/Rewrite.php +++ b/CPTP/Module/Rewrite.php @@ -59,10 +59,11 @@ public function register_post_type_rules( $post_type, $args ) { $permalink = CPTP_DEFAULT_PERMALINK; } - $permalink = '%' . $post_type . '_slug%' . $permalink; - $permalink = str_replace( '%postname%', '%' . $post_type . '%', $permalink ); + $slug_placeholder = self::get_slug_placeholder( $post_type ); + $permalink = $slug_placeholder . $permalink; + $permalink = str_replace( '%postname%', '%' . $post_type . '%', $permalink ); - add_rewrite_tag( '%' . $post_type . '_slug%', '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=' ); + add_rewrite_tag( $slug_placeholder, '(' . $args->rewrite['slug'] . ')', 'post_type=' . $post_type . '&slug=' ); $taxonomies = CPTP_Util::get_taxonomies( true ); foreach ( $taxonomies as $taxonomy => $objects ) : @@ -280,4 +281,15 @@ public function parse_request( $obj ) { } } } + + /** + * Returns the slug placeholder user in the permalink structure. + * + * @param string $post_type The post type. + * + * @return string + */ + public static function get_slug_placeholder( $post_type ) { + return '%' . $post_type . '_slug%'; + } } diff --git a/codesniffer.ruleset.xml b/codesniffer.ruleset.xml index 7c08d42..98d4608 100644 --- a/codesniffer.ruleset.xml +++ b/codesniffer.ruleset.xml @@ -22,7 +22,7 @@ - + diff --git a/phpunit.xml b/phpunit.xml index b019054..a224a8c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,7 +7,7 @@ convertWarningsToExceptions="true" > - + ./tests/ diff --git a/readme.txt b/readme.txt index 50bf9bf..39a9241 100644 --- a/readme.txt +++ b/readme.txt @@ -2,9 +2,9 @@ Contributors: Toro_Unit,inc2734,ixkaito,keita_kobayashi Donate link: https://www.paypal.me/torounit Tags: permalink,permalinks,custom post type,custom taxonomy,cms -Requires at least: 4.3 +Requires at least: 4.7 Tested up to: 5.4 -Requires PHP: 5.3 +Requires PHP: 5.6 Stable tag: 3.3.5 License: GPLv2 or Later diff --git a/tests/test-cptp-module-permalink.php b/tests/test-cptp-module-permalink.php index 602159c..1cf8d43 100644 --- a/tests/test-cptp-module-permalink.php +++ b/tests/test-cptp-module-permalink.php @@ -14,7 +14,6 @@ public function setUp() { add_option( 'category_base', rand_str( 12 ) ); delete_option( 'tag_base' ); add_option( 'tag_base', rand_str( 12 ) ); - $wp_rewrite->init(); $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' ); delete_option( 'no_taxonomy_structure' ); @@ -412,6 +411,28 @@ public function test_to_disable_post_type( $structure ) { $this->assertTrue( is_attachment() ); } + /** + * @test + * @group permalink + * @group wpml + */ + public function test_wpml_st_post_type_link_filter_original_slug() { + register_post_type( $this->post_type, array( + 'public' => true, + ) ); + + $user_id = $this->factory->user->create(); + $post = $this->factory->post->create_and_get( array( + 'post_type' => $this->post_type, + 'post_author' => $user_id, + ) ); + + $actual = apply_filters( 'wpml_st_post_type_link_filter_original_slug', 'ignored', 'ignored', $post ); + $this->assertEquals( + CPTP_Module_Rewrite::get_slug_placeholder( $post->post_type ), + $actual + ); + } }