Skip to content

Commit 16f808c

Browse files
committed
Merge branch 'master' of github.com:athlan/wordpress-custom-fields-permalink-plugin
2 parents ad4cdec + e4deeb7 commit 16f808c

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
class PostWithDuplicatedMetaKey extends WP_UnitTestCase {
4+
5+
/**
6+
* @var PermalinkSteps
7+
*/
8+
private $permalinkSteps;
9+
10+
/**
11+
* @var PermalinkAsserter
12+
*/
13+
private $permalinkAsserter;
14+
15+
public function setUp() {
16+
parent::setUp();
17+
18+
$this->permalinkSteps = new PermalinkSteps($this);
19+
$this->permalinkAsserter = new PermalinkAsserter($this);
20+
}
21+
22+
function test_generates_permalink_to_post_while_duplicated_meta_key() {
23+
// given
24+
$this->permalinkSteps->givenPermalinkStructure("/%field_some_meta_key%/%postname%/");
25+
26+
$someMetaKey = 'some_meta_key';
27+
$postParams = [
28+
'post_title' => 'Some post title',
29+
'meta_input' => [
30+
$someMetaKey => 'Some meta value',
31+
],
32+
];
33+
$createdPostId = $this->factory()->post->create($postParams);
34+
add_post_meta($createdPostId, $someMetaKey, 'Some duplicated meta value');
35+
36+
// when
37+
$createdPostMetaValues = get_post_meta($createdPostId);
38+
39+
// then
40+
$this->assertCount(2, $createdPostMetaValues[$someMetaKey]);
41+
$this->permalinkAsserter->hasPermalink($createdPostId, "/some-meta-value/some-post-title/");
42+
}
43+
44+
function test_go_to_post_when_duplicated_meta_key_and_use_first_one() {
45+
// given
46+
$this->permalinkSteps->givenPermalinkStructure("/%field_some_meta_key%/%postname%/");
47+
48+
$someMetaKey = 'some_meta_key';
49+
$postParams = [
50+
'post_title' => 'Some post title',
51+
'meta_input' => [
52+
$someMetaKey => 'Some meta value',
53+
],
54+
];
55+
$createdPostId = $this->factory()->post->create($postParams);
56+
add_post_meta($createdPostId, $someMetaKey, "Some duplicated meta value");
57+
58+
// when
59+
$this->go_to('/some-meta-value/some-post-title/');
60+
61+
// then
62+
$this->assertFalse(is_404());
63+
$this->assertEquals($createdPostId, get_the_ID());
64+
}
65+
66+
function test_go_to_post_when_duplicated_meta_key_and_use_duplicate_one() {
67+
// given
68+
$this->permalinkSteps->givenPermalinkStructure("/%field_some_meta_key%/%postname%/");
69+
70+
$someMetaKey = 'some_meta_key';
71+
$postParams = [
72+
'post_title' => 'Some post title',
73+
'meta_input' => [
74+
$someMetaKey => 'Some meta value',
75+
],
76+
];
77+
$createdPostId = $this->factory()->post->create($postParams);
78+
add_post_meta($createdPostId, $someMetaKey, "Some duplicated meta value");
79+
80+
// when
81+
$this->go_to('/some-duplicated-meta-value/some-post-title/');
82+
83+
// then
84+
$this->assertFalse(is_404());
85+
$this->assertEquals($createdPostId, get_the_ID());
86+
}
87+
}

wordpress-custom-fields-permalink-plugin.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public static function linkRewriteFieldsExtract($post, $fieldName) {
3636

3737
if(!isset($postMeta[$fieldName]))
3838
return '';
39-
40-
$value = implode('', $postMeta[$fieldName]);
39+
40+
$value = $postMeta[$fieldName][0];
4141

4242
$value = sanitize_title($value);
4343

0 commit comments

Comments
 (0)