/** * Outputs a form field * * @param array $args contains a list of args for showing the field, merged with defaults (below) * @return string */ public static function address_form_field($args) { $defaults = array('type' => 'text', 'name' => '', 'label' => '', 'placeholder' => '', 'required' => false, 'class' => array(), 'label_class' => array(), 'rel' => '', 'return' => false, 'options' => array(), 'value' => ''); $args = wp_parse_args($args, $defaults); if ($args['required']) { $required = ' <span class="required">*</span>'; $input_required = ' input-required'; } else { $required = ''; $input_required = ''; } if (in_array('form-row-last', $args['class'])) { $after = '<div class="clear"></div>'; } else { $after = ''; } switch ($args['type']) { case "country": $current_c = self::get_value($args['name']); $is_shipping_c = strpos($args['name'], 'shipping'); if (!$current_c) { if ($is_shipping_c === false) { $current_c = jigoshop_customer::get_country(); } else { $current_c = jigoshop_customer::get_shipping_country(); } } // Remove 'Select a Country' option from drop-down menu for countries. // There is no need to have it, because was assume when user hasn't selected // a country that they are from the shop base country. $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . esc_attr(implode(' ', $args['label_class'])) . '">' . $args['label'] . $required . '</label> <select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="country_to_state" rel="' . esc_attr($args['rel']) . '">'; foreach (jigoshop_countries::get_allowed_countries() as $key => $value) { $field .= '<option value="' . esc_attr($key) . '"'; if (self::get_value($args['name']) == $key) { $field .= ' selected="selected"'; } elseif (self::get_value($args['name']) && $current_c == $key) { $field .= ' selected="selected"'; } $field .= '>' . __($value, 'jigoshop') . '</option>'; } $field .= '</select></p>' . $after; break; case "state": $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>'; $is_shipping_s = strpos($args['name'], 'shipping'); $current_cc = self::get_value($args['rel']); if (!$current_cc) { if ($is_shipping_s === false) { $current_cc = jigoshop_customer::get_country(); } else { $current_cc = jigoshop_customer::get_shipping_country(); } } $current_r = self::get_value($args['name']); if (!$current_r) { if ($is_shipping_s === false) { $current_r = jigoshop_customer::get_state(); } else { $current_r = jigoshop_customer::get_shipping_state(); } } $states = jigoshop_countries::get_states($current_cc); if (!empty($states)) { // Dropdown $field .= '<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="' . esc_attr($input_required) . '"><option value="">' . __('Select a state…', 'jigoshop') . '</option>'; foreach ($states as $key => $value) { $field .= '<option value="' . esc_attr($key) . '"'; if ($current_r == $key) { $field .= ' selected="selected"'; } $field .= '>' . __($value, 'jigoshop') . '</option>'; } $field .= '</select>'; } else { // Input $field .= '<input type="text" class="input-text" value="' . esc_attr($current_r) . '" placeholder="' . __('State/Province', 'jigoshop') . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" />'; } $field .= '</p>' . $after; break; case "postcode": $current_pc = self::get_value($args['name']); $is_shipping_pc = strpos($args['name'], 'shipping'); if (!$current_pc) { if ($is_shipping_pc === false) { $current_pc = jigoshop_customer::get_postcode(); } else { $current_pc = jigoshop_customer::get_shipping_postcode(); } } $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <input type="text" class="input-text" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . esc_attr($current_pc) . '" /> </p>' . $after; break; case "textarea": $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <textarea name="' . esc_attr($args['name']) . '" class="input-text' . esc_attr($input_required) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" cols="5" rows="2">' . esc_textarea(self::get_value($args['name'])) . '</textarea> </p>' . $after; break; //Adds a drop down custom type //Adds a drop down custom type case "select": $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>'; $field .= '<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="' . esc_attr($input_required) . '">'; foreach ($args['options'] as $value => $label) { $field .= '<option value="' . esc_attr($value) . '"'; if (self::get_value($args['name']) == $value) { $field .= ' selected="selected"'; } $field .= '>' . __($label, 'jigoshop') . '</option>'; } $field .= '</select></p>' . $after; break; default: $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <input type="' . $args['type'] . '" class="input-text' . esc_attr($input_required) . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . self::get_value($args['name']) . '" /> </p>' . $after; break; } $field = apply_filters('jigoshop_address_field_types', $field, $args); if ($args['return']) { return $field; } else { echo $field; return null; } }
/** * Outputs a form field * * @param array $args contains a list of args for showing the field, merged with defaults (below) * @return string */ public function field($args) { $defaults = array('type' => 'text', 'name' => '', 'label' => '', 'placeholder' => '', 'required' => false, 'class' => array(), 'label_class' => array(), 'options' => array(), 'selected' => '', 'rel' => '', 'echo' => true, 'return' => false); $args = wp_parse_args($args, $defaults); if ($args['return']) { $args['echo'] = false; } $required = ''; $input_required = ''; $after = ''; if ($args['name'] == 'billing_state' || $args['name'] == 'shipping_state') { if (jigoshop_customer::has_valid_shipping_state()) { $args['required'] = false; } } if ($args['required']) { $required = ' <span class="required">*</span>'; $input_required = ' input-required'; } if (in_array('form-row-last', $args['class'])) { $after = '<div class="clear"></div>'; } switch ($args['type']) { case 'country': $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . esc_attr(implode(' ', $args['label_class'])) . '">' . $args['label'] . $required . '</label> <select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="country_to_state' . esc_attr($input_required) . '" rel="' . esc_attr($args['rel']) . '">'; $countries = jigoshop_countries::get_allowed_countries(); if (Jigoshop_Base::get_options()->get('jigoshop_default_country_for_customer') == -1) { $countries = array_merge(array(-1 => __('Select your country', 'jigoshop')), $countries); } foreach ($countries as $key => $value) { $field .= '<option value="' . esc_attr($key) . '"'; if ($this->get_value($args['name']) == $key) { $field .= ' selected="selected"'; } elseif (!$this->get_value($args['name']) && jigoshop_customer::get_country() == $key) { $field .= ' selected="selected"'; } $field .= '>' . __($value, 'jigoshop') . '</option>'; } $field .= '</select></p>' . $after; break; case 'state': $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label>'; $allowed_countries = Jigoshop_Base::get_options()->get('jigoshop_allowed_countries'); $current_cc = $this->get_value($args['rel']); if (!$current_cc) { $current_cc = jigoshop_customer::get_country(); } if ($allowed_countries === 'specific') { $specific_countries = Jigoshop_Base::get_options()->get('jigoshop_specific_allowed_countries'); $base_cc = jigoshop_countries::get_base_country(); if (!in_array($current_cc, $specific_countries)) { if (in_array($base_cc, $specific_countries)) { $current_cc = $base_cc; } else { $current_cc = array_shift($specific_countries); } } } $current_r = $this->get_value($args['name']); if (!$current_r) { $current_r = jigoshop_customer::get_state(); } $states = jigoshop_countries::get_states($current_cc); $state_keys = array_keys($states); if (jigoshop_countries::country_has_states($current_cc) && !in_array($current_r, $state_keys)) { $base_r = jigoshop_countries::get_base_state(); if (in_array($base_r, $state_keys)) { $current_r = $base_r; } else { $current_r = array_shift($state_keys); } } if (jigoshop_countries::country_has_states($current_cc)) { // Dropdown $field .= '<select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="' . esc_attr($input_required) . '"><option value="">' . __('Select a state…', 'jigoshop') . '</option>'; foreach ($states as $key => $value) { $field .= '<option value="' . esc_attr($key) . '"'; if ($current_r == $key) { $field .= ' selected="selected"'; } $field .= '>' . __($value, 'jigoshop') . '</option>'; } $field .= '</select>'; } else { // Input $field .= '<input type="text" class="input-text' . esc_attr($input_required) . '" value="' . esc_attr($current_r) . '" placeholder="' . __('State/Province', 'jigoshop') . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" />'; } $field .= '</p>' . $after; break; case 'postcode': $current_pc = $this->get_value($args['name']); if (!$current_pc) { $current_pc = $args['rel'] == 'shipping_country' ? jigoshop_customer::get_shipping_postcode() : jigoshop_customer::get_postcode(); } $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <input type="text" class="input-text' . esc_attr($input_required) . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . esc_attr($current_pc) . '" rel="' . esc_attr($args['rel']) . '" /> </p>' . $after; break; case 'textarea': $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <textarea name="' . esc_attr($args['name']) . '" class="input-text' . esc_attr($input_required) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" cols="5" rows="2" rel="' . esc_attr($args['rel']) . '">' . esc_textarea($this->get_value($args['name'])) . '</textarea> </p>' . $after; break; case 'select': $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <select name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" class="input-text' . esc_attr($input_required) . '" rel="' . esc_attr($args['rel']) . '">'; foreach ($args['options'] as $key => $value) { $field .= '<option value="' . esc_attr($key) . '"'; if (esc_attr($args['selected']) == $key) { $field .= ' selected="selected"'; } $field .= '>' . __($value, 'jigoshop') . '</option>'; } $field .= '</select></p>' . $after; break; case 'text': case 'password': $field = '<p class="form-row ' . implode(' ', $args['class']) . '"> <label for="' . esc_attr($args['name']) . '" class="' . implode(' ', $args['label_class']) . '">' . $args['label'] . $required . '</label> <input type="' . $args['type'] . '" class="input-text' . esc_attr($input_required) . '" name="' . esc_attr($args['name']) . '" id="' . esc_attr($args['name']) . '" placeholder="' . $args['placeholder'] . '" value="' . $this->get_value($args['name']) . '" rel="' . esc_attr($args['rel']) . '" /> </p>' . $after; break; default: ob_start(); do_action('jigoshop_display_checkout_field', $args['type'], $args, $this->get_value($args['name'])); echo $after; $field = ob_get_clean(); break; } if ($args['echo']) { echo $field; } return $field; }