Merge pull request #6375 from dominic-p/settings-desc

Moved description and tip generation to a separate method
This commit is contained in:
Mike Jolley 2014-10-17 16:19:02 +01:00
commit 21784e80cd
1 changed files with 48 additions and 30 deletions

View File

@ -239,36 +239,7 @@ class WC_Admin_Settings {
} }
// Description handling // Description handling
if ( true === $value['desc_tip'] ) { extract( self::get_description_tip( $value ) );
$description = '';
$tip = $value['desc'];
} elseif ( ! empty( $value['desc_tip'] ) ) {
$description = $value['desc'];
$tip = $value['desc_tip'];
} elseif ( ! empty( $value['desc'] ) ) {
$description = $value['desc'];
$tip = '';
} else {
$description = $tip = '';
}
if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) {
$description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
} elseif ( $description && in_array( $value['type'], array( 'checkbox' ) ) ) {
$description = wp_kses_post( $description );
} elseif ( $description ) {
$description = '<span class="description">' . wp_kses_post( $description ) . '</span>';
}
if ( $tip && in_array( $value['type'], array( 'checkbox' ) ) ) {
$tip = '<p class="description">' . $tip . '</p>';
} elseif ( $tip ) {
$tip = '<img class="help_tip" data-tip="' . esc_attr( $tip ) . '" src="' . WC()->plugin_url() . '/assets/images/help.png" height="16" width="16" />';
}
// Switch based on type // Switch based on type
switch ( $value['type'] ) { switch ( $value['type'] ) {
@ -621,6 +592,53 @@ class WC_Admin_Settings {
} }
} }
/**
* Helper function to get the formated description and tip HTML for a
* given form field. Plugins can call this when implementing their own custom
* settings types.
*
* @param array $value The form field value array
* @returns array The description and tip as a 2 element array
*/
public static function get_description_tip( $value ) {
if ( true === $value['desc_tip'] ) {
$description = '';
$tip = $value['desc'];
} elseif ( ! empty( $value['desc_tip'] ) ) {
$description = $value['desc'];
$tip = $value['desc_tip'];
} elseif ( ! empty( $value['desc'] ) ) {
$description = $value['desc'];
$tip = '';
} else {
$description = $tip = '';
}
if ( $description && in_array( $value['type'], array( 'textarea', 'radio' ) ) ) {
$description = '<p style="margin-top:0">' . wp_kses_post( $description ) . '</p>';
} elseif ( $description && in_array( $value['type'], array( 'checkbox' ) ) ) {
$description = wp_kses_post( $description );
} elseif ( $description ) {
$description = '<span class="description">' . wp_kses_post( $description ) . '</span>';
}
if ( $tip && in_array( $value['type'], array( 'checkbox' ) ) ) {
$tip = '<p class="description">' . $tip . '</p>';
} elseif ( $tip ) {
$tip = '<img class="help_tip" data-tip="' . esc_attr( $tip ) . '" src="' . WC()->plugin_url() . '/assets/images/help.png" height="16" width="16" />';
}
return array(
'description' => $description,
'tip' => $tip
);
}
/** /**
* Save admin fields. * Save admin fields.
* *