Skip to content

Commit

Permalink
Merge pull request #99 from torounit/pr/98
Browse files Browse the repository at this point in the history
WPML Support and Drop WP < 4.7 and PHP < 5.6
  • Loading branch information
torounit committed Jul 21, 2020
2 parents aa93e2b + 24feacb commit 614deb4
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [ "." ],
"port": 50080,
"testsPort": 50081
}
3 changes: 1 addition & 2 deletions CPTP/Module/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function settings_api_init() {
)
);

register_setting( 'permalink', 'no_taxonomy_structure' );
register_setting( 'permalink', 'add_post_type_for_tax' );
}

/**
Expand Down Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions CPTP/Module/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
23 changes: 22 additions & 1 deletion CPTP/Module/Permalink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}


Expand Down Expand Up @@ -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 = '';
Expand Down Expand Up @@ -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 );
}
}
18 changes: 15 additions & 3 deletions CPTP/Module/Rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) :
Expand Down Expand Up @@ -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%';
}
}
2 changes: 1 addition & 1 deletion codesniffer.ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<config name="testVersion" value="5.3-"/>
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP"/>
<config name="minimum_supported_wp_version" value="4.3"/>
<config name="minimum_supported_wp_version" value="4.7"/>
<rule ref="WordPress-Core">
<exclude name="WordPress.Files.FileName" />
<exclude name="WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase" />
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite>
<testsuite name="custom-post-type-permalinks">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
Expand Down
4 changes: 2 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
23 changes: 22 additions & 1 deletion tests/test-cptp-module-permalink.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down Expand Up @@ -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
);
}
}

0 comments on commit 614deb4

Please sign in to comment.