Skip to content

Commit

Permalink
Optimize code for saving tax adv. value
Browse files Browse the repository at this point in the history
  • Loading branch information
rilwis committed Nov 8, 2018
1 parent 1b741cd commit e950256
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
23 changes: 13 additions & 10 deletions inc/fields/taxonomy-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ public static function save( $new, $old, $post_id, $field ) {
}
$storage = $field['storage'];

if ( $new ) {
if ( $field['clone'] && $field['clone_as_multiple'] ) {
$storage->delete( $post_id, $field['id'] );
foreach ( $new as $key => $value ) {
$storage->add( $post_id, $field['id'], $value );
}
return;
}
$storage->update( $post_id, $field['id'], $new );
} else {
if ( ! $new ) {
$storage->delete( $post_id, $field['id'] );
return;
}

if ( ! $field['clone'] || ! $field['clone_as_multiple'] ) {
$storage->update( $post_id, $field['id'], $new );
return;
}

// clone and clone_as_multiple.
$storage->delete( $post_id, $field['id'] );
foreach ( $new as $value ) {
$storage->add( $post_id, $field['id'], $value );
}
}

Expand Down
29 changes: 29 additions & 0 deletions tests/clone-taxonomy-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,33 @@
],
];
return $meta_boxes;
} );

add_filter( 'the_content', function( $content ) {
if ( ! is_single() ) {
return $content;
}
ob_start();
$fields = range( 1, 6 );
foreach ( $fields as $field ) {
$field = "ta$field";
echo "<h1>Field $field</h1>";

echo '<h3>rwmb_meta()</h3>';
$value = rwmb_meta( $field );
echo '<pre>';
print_r( $value );
echo '</pre>';

echo '<h3>rwmb_get_value()</h3>';
$value = rwmb_get_value( $field );
echo '<pre>';
print_r( $value );
echo '</pre>';

echo '<h3>rwmb_the_value()</h3>';
rwmb_the_value( $field );
}

return $content . ob_get_clean();
} );

0 comments on commit e950256

Please sign in to comment.