Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions inc/meta-box-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,14 +305,14 @@ public function boolean_input( $meta_key, $value, $required, $label, $form_id =

public function link_input( $meta_key, $value, $required, $label, $form_id = NULL ) {
$post_id = get_the_ID();
$existing = get_post_meta( $post_id, $meta_key, false);
$existing = get_post_meta( $post_id, $meta_key, true);
?><div class="link-field <?php echo "{$meta_key}" ?>"><?php
if ( ! isset( $existing[0] ) || ! isset( $existing[1] ) ) {
if ( ! isset( $existing['url'] ) || ! isset( $existing['text'] ) ) {
$this->single_input( $meta_key . "_text", $value, 'text', $required, NULL, 'Link Text', 'Url text here', $form_id );
$this->single_input( $meta_key . "_url", $value, 'text', $required, NULL, 'Link URL', 'Url here', $form_id );
} else {
$this->single_input( $meta_key . "_text", $existing[1], 'text', $required, NULL, 'Link Text', NULL, $form_id );
$this->single_input( $meta_key . "_url", $existing[0], 'text', $required, NULL, 'Link URL', NULL, $form_id );
$this->single_input( $meta_key . "_text", $existing['text'], 'text', $required, NULL, 'Link Text', NULL, $form_id );
$this->single_input( $meta_key . "_url", $existing['url'], 'text', $required, NULL, 'Link URL', NULL, $form_id );
}
?></div><?php
}
Expand Down
10 changes: 4 additions & 6 deletions inc/meta-box-models.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,16 @@ public function validate_link( $field, $post_ID ) {
and isset( $_POST["{$field['meta_key']}_text"] ) ) {
$url = $_POST["{$field['meta_key']}_url"];
$text = $_POST["{$field['meta_key']}_text"];
$full_link = array( 0 => $url, 1 => $text );
$existing = get_post_meta( $post_ID, $field['meta_key'], $single = false );
$full_link = array( 'url' => $url, 'text' => $text );
$existing = get_post_meta( $post_ID, $field['meta_key'], $single = true );

if ( empty( $_POST["{$field['meta_key']}_url"] )
or empty( $_POST["{$field['meta_key']}_text"] ) ) {
delete_post_meta( $post_ID, $field['meta_key']);
} elseif ( empty($existing) ) {
add_post_meta( $post_ID, $field['meta_key'], $url, false );
add_post_meta( $post_ID, $field['meta_key'], $text, false );
add_post_meta( $post_ID, $field['meta_key'], $full_link, false );
} elseif ( $existing != $full_link ) {
update_post_meta( $post_ID, $field['meta_key'], $url, $existing[0] );
update_post_meta( $post_ID, $field['meta_key'], $text, $existing[1] );
update_post_meta( $post_ID, $field['meta_key'], $full_link, $existing );
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test-meta_box_models.php
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ function testValidateLinkExpectsAddPostMetaTwice() {
$form->fields = $field;
\WP_Mock::wpFunction(
'add_post_meta',
array( 'times' => 2, 'return' => true )
array( 'times' => 1, 'return' => true )
);
$post = new \StdClass;
\WP_Mock::wpFunction('get_post_meta', array( 'times' => 1, 'return' => false) );
Expand Down Expand Up @@ -1334,7 +1334,7 @@ function testValidateLinkCountNotGivenExpectsUpdatePostMetaCalledOnce() {
$form->fields = $field;
\WP_Mock::wpFunction(
'add_post_meta',
array( 'times' => 2, 'return' => true )
array( 'times' => 1, 'return' => true )
);
$post = new \StdClass;
\WP_Mock::wpFunction('get_post_meta', array( 'times' => 1, 'return' => false) );
Expand Down Expand Up @@ -1375,7 +1375,7 @@ function testValidateLinkWithExistingDataExpectsDataDeletedAndReplaced() {
// );
\WP_Mock::wpFunction(
'update_post_meta',
array( 'times' => 2, 'return' => true)
array( 'times' => 1, 'return' => true)
);
\WP_Mock::wpFunction(
'get_post_meta',
Expand Down Expand Up @@ -1411,7 +1411,7 @@ function testValidateLinkWithExistingDataMatchingSubmittedExpectsNoaction() {
'meta_key' => 'link',
'howto' => "Some howto text",
);
$existing = array( 'http://example.com', 'example.com');
$existing = array( 'url' => 'http://example.com', 'text' => 'example.com');
$form = new TestNumberField();
$post_id = 1;
$form->fields = $field;
Expand Down