I have a conditional inside a class that I would like to be transformed into a single printf
. The only difference between if
and else
strings is the <p>
tag with description and as a consequence 1 argument more/less. Is there any simple way to achieve it? Or maybe considering the alternatives, my current code is good enough?
if ( $add_info ) {
printf (
'<fieldset>
<label><input id="%1$s" type="checkbox" name="eswc_settingz[%1$s]" value="1" %2$s />%3$s</label>
</fieldset>
<p class="description">%4$s</p>',
esc_attr( $field ),
isset( $this->options[$field] ) && ( 1 == $this->options[$field] ) ? 'checked="checked" ':'',
__( $this->settings_list[$field]['descr'], 'extra-settings-for-woocommerce' ),
__( $this->settings_list[$field]['info'], 'extra-settings-for-woocommerce' )
);
} else {
printf (
'<fieldset>
<label><input id="%1$s" type="checkbox" name="eswc_settingz[%1$s]" value="1" %2$s />%3$s</label>
</fieldset>',
esc_attr( $field ),
isset( $this->options[$field] ) && ( 1 == $this->options[$field] ) ? 'checked="checked" ':'',
__( $this->settings_list[$field]['descr'], 'extra-settings-for-woocommerce' )
);
}
2
Answers
Since the bit you’re adding comes after everything else (not merged into the middle or anything which would make it more complex), how about simply having a second printf statement to add only the extra part, when needed?
It doesn’t result in a single printf statement, but it does mean you won’t have any repetition in your code, which I think was probably your main concern.
e.g.
You can simplify it like this: