that has a default value. Relies on woocommerce_wp_select.
*
* field['default_value']: The default value (if omitted the first option will be default)
* field['append_default_label']: If true or omitted the text '(DEFAULT)' will be appended to the default option caption
*
* @param array $field see wc-meta-box-functions.php:woocommerce_wp_select
*/
public static function render_select_with_default($field)
{
global $thepostid, $post;
$thepostid = empty($thepostid) ? $post->ID : $thepostid;
reset($field['options']); // move to first for key()
$default_value = $field['default_value'] ?? key($field['options']);
$append_default_label = $field['append_default_label'] ?? true;
if ($append_default_label) {
// translators: 1: Default value
$field['options'][$default_value] = sprintf(__('%s (Default)', 'woocommerce-jos-autocoupon'), $field['options'][$default_value]);
}
if (!isset($field['value'])) {
$field['value'] = get_post_meta($thepostid, $field['id'], true);
}
if (empty($field['value'])) {
$field['value'] = $default_value;
}
woocommerce_wp_select($field);
}
public static function render_admin_cat_selector($dom_id, $field_name, $selected_ids, $placeholder = null)
{
if (is_null($placeholder)) {
$placeholder = __('Search for a product…', 'woocommerce');
}
// Categories
?>
plugin_url()).'/assets/images/help.png" height="16" width="16" />';
}
/**
* Renders a product selection . Will use either select2 v4 (WC3.0+) select2 v3 (WC2.3+) or chosen (< WC2.3).
*
* @param string $dom_id
* @param string $field_name
* @param array $selected_ids Array of integers
* @param null|string $placeholder
*/
public static function render_admin_product_selector($dom_id, $field_name, $selected_ids, $placeholder = null)
{
$product_key_values = [];
foreach ($selected_ids as $product_id) {
$product = wc_get_product($product_id);
if (is_object($product)) {
$product_key_values[esc_attr($product_id)] = wp_kses_post($product->get_formatted_name());
}
}
if (is_null($placeholder)) {
$placeholder = __('Search for a product…', 'woocommerce');
}
// In WooCommerce version 3.0 select2 v3 was replaced by select2 v4
self::render_admin_select2_v4_product_selector($dom_id, $field_name, $product_key_values, $placeholder);
}
/**
* Renders a customer selection . Will use either select2 v4 (WC3.0+) or select2 v3 (WC2.3+).
*
* @param string $dom_id
* @param string $field_name
* @param array $selected_customer_ids Array of integers
* @param null|string $placeholder
*/
public static function render_admin_customer_selector($dom_id, $field_name, $selected_customer_ids, $placeholder = null)
{
$selected_keys_and_values = [];
foreach ($selected_customer_ids as $customer_id) {
$customer = get_userdata($customer_id);
if (is_object($customer)) {
$selected_keys_and_values[$customer_id] = $customer->display_name.' (#'.$customer->ID.' – '.sanitize_email($customer->user_email).')';
}
}
if (is_null($placeholder)) {
$placeholder = __('Any customer', 'woocommerce-jos-autocoupon');
}
// In WooCommerce version 3.0 select2 v3 was replaced by select2 v4
self::render_admin_select2_v4_customer_selector($dom_id, $field_name, $selected_keys_and_values, $placeholder);
}
// ============================================
// Simple html output
/**
* Renders a
* $args should be an array in this form:
* [ 'type' => 'text', name' => 'field-name', 'id' => 'dom-id', 'value' => 'value', 'class' => 'css-class' ].
*
* @param array $args
*/
public static function render_input($args)
{
if (!isset($args['type'])) {
throw new Exception('Type field is obligatory.');
}
$fields = [];
switch ($args['type']) {
case 'text':
$fields['type'] = 'text';
if (isset($args['value'])) {
$fields['value'] = esc_attr($args['value']);
}
break;
case 'hidden':
$fields['type'] = 'hidden';
if (isset($args['value'])) {
$fields['value'] = esc_attr($args['value']);
}
break;
case 'radio':
$fields['type'] = 'radio';
if (isset($args['checked']) && $args['checked']) {
$fields['checked'] = esc_attr($args['checked']);
}
if (isset($args['disabled']) && $args['disabled']) {
$fields['disabled'] = 'disabled';
}
if (isset($args['value'])) {
$fields['value'] = esc_attr($args['value']);
}
break;
case 'checkbox':
$fields['type'] = 'checkbox';
if (isset($args['disabled']) && $args['disabled']) {
$fields['disabled'] = 'disabled';
}
$fields['value'] = $args['cbvalue'] ?? 'yes';
if (isset($args['value']) && (string) $args['value'] === (string) $fields['value']) {
$fields['checked'] = 'checked';
}
if (!empty($field['description']) && false === $field['desc_tip']) {
echo ''.wp_kses_post($field['description']).'';
}
break;
default:
throw new Exception(sprintf('Unknown field type "%s" is obligatory.', $args['type']));
}
// var_dump($args);
if (isset($args['name'])) {
$fields['name'] = esc_attr($args['name']);
}
if (isset($args['id'])) {
$fields['id'] = esc_attr($args['id']);
}
if (isset($args['class'])) {
$fields['class'] = esc_attr($args['class']);
}
self::render_tag('input', $fields);
}
/**
* Renders a html tag:
* e.g. contents
*
* This function does not escape the fields! Keys must be valid attribute names and values MUST be properly escaped!!!
*
* @param string $name
* @param array $fields E.g. [ 'class' => 'clearfix' ] yields: class="clearfix"
* @param null|string $contents If null the html tag will be self closing
*/
public static function render_tag($name, $fields = [], $contents = null)
{
echo self::tag($name, $fields, $contents);
}
/**
* Returns a html tag:
* e.g. contents
*
* This function does not escape the fields! Keys must be valid attribute names and values MUST be properly escaped!!!
*
* @param string $name
* @param array $fields E.g. [ 'class' => 'clearfix' ] yields: class="clearfix"
* @param null|string $contents If null the html tag will be self closing
*
* @return string
*/
protected static function tag($name, $fields = [], $contents = null)
{
if (is_null($contents)) {
return sprintf('<%s %s/>', $name, self::extract_attributes($fields));
}
return sprintf('<%s %s>%s%s>', $name, self::extract_attributes($fields), $contents, $name);
}
/**
* Extracts fields to attributes for html tags.
* E.g. [ 'class' => 'clearfix' ] yields: class="clearfix".
*
* This function does not escape the fields! Keys must be valid attribute names and values MUST be properly escaped!!!
*
* @param array $fields
*
* @return string
*/
protected static function extract_attributes($fields)
{
// NOTE: attrib and value MUST be properly escaped!!!
$extracted = '';
foreach ($fields as $field => $value) {
$extracted .= sprintf('%s="%s" ', $field, $value);
}
return $extracted;
}
/**
* Renders a product selection .
* Select2 version 4 (Since WC 3.0).
*
* @param string $dom_id
* @param string $field_name
* @param string $selected_keys_and_values
* @param string $placeholder
*/
private static function render_admin_select2_v4_product_selector($dom_id, $field_name, $selected_keys_and_values, $placeholder)
{
// $selected_keys_and_values must be an array of [ id => name ]
$json_encoded = esc_attr(json_encode($selected_keys_and_values));
echo '';
}
private static function render_admin_select2_v4_customer_selector($dom_id, $field_name, $selected_keys_and_values, $placeholder)
{
// $selected_keys_and_values must be an array of [ id => name ]
$json_encoded = esc_attr(json_encode($selected_keys_and_values));
echo '';
}
}